Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow end-users to override yielded number logic #156

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.klinker.android.logger.Log;

import com.klinker.android.send_message.R;
import com.klinker.android.send_message.Utils;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -559,13 +560,14 @@ private static String getLine1(Context context, int subId) {
Context.TELEPHONY_SERVICE);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
return telephonyManager.getLine1Number();
return Utils.getMyPhoneNumber(context);
} else {
try {
Method method = telephonyManager.getClass().getMethod("getLine1NumberForSubscriber", int.class);
return (String) method.invoke(telephonyManager, subId);
String number = (String) method.invoke(telephonyManager, subId);
return number;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the only part I was a little sketchy on. In this implementation, the lib will still call getLine1NumberForSubscriber rather than Utils.getMyPhoneNumber

How do you think we should handle this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used for dual SIM devices, and we don't want to lose this functionality. I would probably make some kind of condition here

  1. If the user has more than one SIM card, then use this original reflection. You can find how many sim cards the user has, by querying this method and getting the length of the array: https://developer.android.com/reference/android/telephony/SubscriptionManager.html#getActiveSubscriptionInfoList()
  2. If the user has not provided a custom override, then use this original reflection.

So, only use the new Utils.getMyPhoneNumber(context) if the user has one SIM card and the implementing app has actually provided an override.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks Luke! will look to find some time this weekend to finish it off 🙂

} catch (Exception e) {
return telephonyManager.getLine1Number();
return Utils.getMyPhoneNumber(context);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import com.android.mms.logs.LogTag;
import com.android.mms.MmsConfig;
import com.klinker.android.send_message.Utils;

public class HttpUtils {
private static final String TAG = LogTag.TRANSACTION;
Expand Down Expand Up @@ -173,9 +174,7 @@ public static byte[] httpConnection(Context context, long token,
String extraHttpParams = MmsConfig.getHttpParams();

if (extraHttpParams != null) {
String line1Number = ((TelephonyManager)context
.getSystemService(Context.TELEPHONY_SERVICE))
.getLine1Number();
String line1Number = Utils.getMyPhoneNumber(context);
String line1Key = MmsConfig.getHttpParamsLine1Key();
String paramList[] = extraHttpParams.split("\\|");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.google.android.mms.util_alt.PduCacheEntry;
import com.google.android.mms.util_alt.SqliteWrapper;
import com.klinker.android.logger.Log;
import com.klinker.android.send_message.Utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down Expand Up @@ -1516,7 +1517,7 @@ private void loadRecipients(int addressType, HashSet<String> recipients,
if (excludeMyNumber && array.length == 1 && addressType == PduHeaders.TO) {
return;
}
String myNumber = excludeMyNumber ? mTelephonyManager.getLine1Number() : null;
String myNumber = excludeMyNumber ? Utils.getMyPhoneNumber(mContext) : null;
for (EncodedStringValue v : array) {
if (v != null) {
String number = v.getString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.klinker.android.send_message;

import android.content.Context;

public abstract class OwnNumber {
public String yield(Context context) {
return onYield(context);
}
public abstract String onYield(Context context);
}
24 changes: 20 additions & 4 deletions library/src/main/java/com/klinker/android/send_message/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,33 @@ public class Utils {
private static final String TAG = "Utils";
public static final int DEFAULT_SUBSCRIPTION_ID = 1;

private static final OwnNumber DEFAULT_OWN_NUMBER = new OwnNumber(){
@Override
public String onYield(Context context) {
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}
};

private static OwnNumber OVERRIDEN_OWN_NUMBER;

/**
* Gets the current users phone number
*
* @param context is the context of the activity or service
* @return a string of the phone number on the device
*/
public static String getMyPhoneNumber(Context context) {
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
if (OVERRIDEN_OWN_NUMBER != null) {
return OVERRIDEN_OWN_NUMBER.yield(context);
}
return DEFAULT_OWN_NUMBER.yield(context);
}

public static void setOwnNumberOverride(OwnNumber ownNumber) {
OVERRIDEN_OWN_NUMBER = ownNumber;
}

public interface Task<T> {
Expand Down