diff options
Diffstat (limited to 'src/com/android/cellbroadcastreceiver/CellBroadcastReceiver.java')
-rw-r--r-- | src/com/android/cellbroadcastreceiver/CellBroadcastReceiver.java | 151 |
1 files changed, 123 insertions, 28 deletions
diff --git a/src/com/android/cellbroadcastreceiver/CellBroadcastReceiver.java b/src/com/android/cellbroadcastreceiver/CellBroadcastReceiver.java index 0a4cf0732..63be97ffd 100644 --- a/src/com/android/cellbroadcastreceiver/CellBroadcastReceiver.java +++ b/src/com/android/cellbroadcastreceiver/CellBroadcastReceiver.java @@ -32,7 +32,6 @@ import android.os.Bundle; import android.os.RemoteException; import android.os.SystemProperties; import android.os.UserManager; -import androidx.preference.PreferenceManager; import android.provider.Telephony; import android.provider.Telephony.CellBroadcasts; import android.telephony.CarrierConfigManager; @@ -46,12 +45,13 @@ import android.util.Log; import android.widget.Toast; import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import androidx.preference.PreferenceManager; import com.android.cellbroadcastservice.CellBroadcastStatsLog; import com.android.internal.annotations.VisibleForTesting; import java.util.ArrayList; - +import java.util.Arrays; public class CellBroadcastReceiver extends BroadcastReceiver { private static final String TAG = "CellBroadcastReceiver"; @@ -69,6 +69,9 @@ public class CellBroadcastReceiver extends BroadcastReceiver { // Key to access the shared preference of service state. private static final String SERVICE_STATE = "service_state"; + // Key to access the shared preference of roaming operator. + private static final String ROAMING_OPERATOR_SUPPORTED = "roaming_operator_supported"; + // shared preference under developer settings private static final String ENABLE_ALERT_MASTER_PREF = "enable_alerts_master_toggle"; @@ -94,6 +97,11 @@ public class CellBroadcastReceiver extends BroadcastReceiver { public static final String ACTION_TESTING_MODE_CHANGED = "com.android.cellbroadcastreceiver.intent.ACTION_TESTING_MODE_CHANGED"; + // System property to set roaming network config which can be multiple items split by + // comma, and matched in sequence. This config will insert before the overlay. + private static final String ROAMING_PLMN_SUPPORTED_PROPERTY_KEY = + "persist.cellbroadcast.roaming_plmn_supported"; + private Context mContext; /** @@ -121,6 +129,7 @@ public class CellBroadcastReceiver extends BroadcastReceiver { } else if (CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED.equals(action)) { if (!intent.getBooleanExtra( "android.telephony.extra.REBROADCAST_ON_UNLOCK", false)) { + resetCellBroadcastChannelRanges(); int subId = intent.getIntExtra(CarrierConfigManager.EXTRA_SUBSCRIPTION_INDEX, SubscriptionManager.INVALID_SUBSCRIPTION_ID); initializeSharedPreference(context, subId); @@ -146,13 +155,8 @@ public class CellBroadcastReceiver extends BroadcastReceiver { // configurations once moving back from APM. This should be fixed in lower layer // going forward. int ss = intent.getIntExtra(EXTRA_VOICE_REG_STATE, ServiceState.STATE_IN_SERVICE); - if (ss != ServiceState.STATE_POWER_OFF - && getServiceState(context) == ServiceState.STATE_POWER_OFF) { - startConfigServiceToEnableChannels(); - } - setServiceState(ss); - } else if (CELLBROADCAST_START_CONFIG_ACTION.equals(action) - || SubscriptionManager.ACTION_DEFAULT_SMS_SUBSCRIPTION_CHANGED.equals(action)) { + onServiceStateChanged(context, res, ss); + } else if (SubscriptionManager.ACTION_DEFAULT_SMS_SUBSCRIPTION_CHANGED.equals(action)) { startConfigServiceToEnableChannels(); } else if (Telephony.Sms.Intents.ACTION_SMS_EMERGENCY_CB_RECEIVED.equals(action) || Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION.equals(action)) { @@ -174,14 +178,17 @@ public class CellBroadcastReceiver extends BroadcastReceiver { // rename registered notification channels on locale change CellBroadcastAlertService.createNotificationChannels(mContext); } else if (TelephonyManager.ACTION_SECRET_CODE.equals(action)) { - setTestingMode(!isTestingMode(mContext)); - int msgId = (isTestingMode(mContext)) ? R.string.testing_mode_enabled - : R.string.testing_mode_disabled; - String msg = res.getString(msgId); - Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show(); - LocalBroadcastManager.getInstance(mContext) - .sendBroadcast(new Intent(ACTION_TESTING_MODE_CHANGED)); - log(msg); + if (SystemProperties.getInt("ro.debuggable", 0) == 1 + || res.getBoolean(R.bool.allow_testing_mode_on_user_build)) { + setTestingMode(!isTestingMode(mContext)); + int msgId = (isTestingMode(mContext)) ? R.string.testing_mode_enabled + : R.string.testing_mode_disabled; + String msg = res.getString(msgId); + Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show(); + LocalBroadcastManager.getInstance(mContext) + .sendBroadcast(new Intent(ACTION_TESTING_MODE_CHANGED)); + log(msg); + } } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { new CellBroadcastContentProvider.AsyncCellBroadcastTask( mContext.getContentResolver()).execute((CellBroadcastContentProvider @@ -189,19 +196,66 @@ public class CellBroadcastReceiver extends BroadcastReceiver { provider.resyncToSmsInbox(mContext); return true; }); - } else if (TelephonyManager.ACTION_SIM_CARD_STATE_CHANGED.equals(action)) { - int sim_state = intent.getIntExtra( - TelephonyManager.EXTRA_SIM_STATE, TelephonyManager.SIM_STATE_UNKNOWN); - - if (sim_state == TelephonyManager.SIM_STATE_ABSENT - || sim_state == TelephonyManager.SIM_STATE_PRESENT) { - CellBroadcastChannelManager.clearAllCellBroadcastChannelRanges(); - } } else { Log.w(TAG, "onReceive() unexpected action " + action); } } + private void onServiceStateChanged(Context context, Resources res, int ss) { + logd("onServiceStateChanged, ss: " + ss); + // check whether to support roaming network + String roamingOperator = null; + if (ss == ServiceState.STATE_IN_SERVICE || ss == ServiceState.STATE_EMERGENCY_ONLY) { + TelephonyManager tm = context.getSystemService(TelephonyManager.class); + String networkOperator = tm.getNetworkOperator(); + logd("networkOperator: " + networkOperator); + + // check roaming config only if the network oprator is not empty as the config + // is based on operator numeric + if (!networkOperator.isEmpty()) { + // No roaming supported by default + roamingOperator = ""; + if ((tm.isNetworkRoaming() || ss == ServiceState.STATE_EMERGENCY_ONLY) + && !networkOperator.equals(tm.getSimOperator())) { + String propRoamingPlmn = SystemProperties.get( + ROAMING_PLMN_SUPPORTED_PROPERTY_KEY, "").trim(); + String[] roamingNetworks = propRoamingPlmn.isEmpty() ? res.getStringArray( + R.array.cmas_roaming_network_strings) : propRoamingPlmn.split(","); + logd("roamingNetworks: " + Arrays.toString(roamingNetworks)); + + for (String r : roamingNetworks) { + r = r.trim(); + if (r.equals("XXXXXX")) { + //match any roaming network, store mcc+mnc + roamingOperator = networkOperator; + break; + } else if (r.equals("XXX")) { + //match any roaming network, only store mcc + roamingOperator = networkOperator.substring(0, 3); + break; + } else if (networkOperator.startsWith(r)) { + roamingOperator = r; + break; + } + } + } + } + } + + if ((ss != ServiceState.STATE_POWER_OFF + && getServiceState(context) == ServiceState.STATE_POWER_OFF) + || (roamingOperator != null && !roamingOperator.equals( + getRoamingOperatorSupported(context)))) { + startConfigServiceToEnableChannels(); + } + setServiceState(ss); + + if (roamingOperator != null) { + log("update supported roaming operator as " + roamingOperator); + setRoamingOperatorSupported(roamingOperator); + } + } + /** * Send an intent to reset the users WEA settings if there is a new carrier on the default subId * @@ -213,7 +267,7 @@ public class CellBroadcastReceiver extends BroadcastReceiver { * @param context the context * @param subId subId of the carrier config event */ - private void resetSettingsIfCarrierChanged(Context context, int subId) { + private void resetSettingsAsNeeded(Context context, int subId) { // subId may be -1 if carrier config broadcast is being sent on SIM removal if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { if (getPreviousCarrierIdForDefaultSub() == NO_PREVIOUS_CARRIER_ID) { @@ -256,6 +310,17 @@ public class CellBroadcastReceiver extends BroadcastReceiver { return; } + /** When user_build_mode is true and alow_testing_mode_on_user_build is false + * then testing_mode is not able to be true at all. + */ + Resources res = getResourcesMethod(); + if (!res.getBoolean(R.bool.allow_testing_mode_on_user_build) + && SystemProperties.getInt("ro.debuggable", 0) == 0 + && CellBroadcastReceiver.isTestingMode(context)) { + Log.d(TAG, "it can't be testing_mode at all"); + setTestingMode(false); + } + if (carrierId != previousCarrierId) { saveCarrierIdForDefaultSub(carrierId); startConfigService(context, @@ -276,7 +341,7 @@ public class CellBroadcastReceiver extends BroadcastReceiver { .apply(); } - /** + /** * Enable/disable cell broadcast receiver testing mode. * * @param on {@code true} if testing mode is on, otherwise off. @@ -307,6 +372,14 @@ public class CellBroadcastReceiver extends BroadcastReceiver { } /** + * Store the roaming operator + */ + private void setRoamingOperatorSupported(String roamingOperator) { + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext); + sp.edit().putString(ROAMING_OPERATOR_SUPPORTED, roamingOperator).commit(); + } + + /** * @return the stored voice registration service state */ private static int getServiceState(Context context) { @@ -315,6 +388,14 @@ public class CellBroadcastReceiver extends BroadcastReceiver { } /** + * @return the supported roaming operator + */ + public static String getRoamingOperatorSupported(Context context) { + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); + return sp.getString(ROAMING_OPERATOR_SUPPORTED, ""); + } + + /** * update reminder interval */ @VisibleForTesting @@ -367,7 +448,7 @@ public class CellBroadcastReceiver extends BroadcastReceiver { if (isSystemUser()) { Log.d(TAG, "initializeSharedPreference"); - resetSettingsIfCarrierChanged(context, subId); + resetSettingsAsNeeded(context, subId); SharedPreferences sp = getDefaultSharedPreferences(); @@ -620,10 +701,24 @@ public class CellBroadcastReceiver extends BroadcastReceiver { } } + /** + * Reset cached CellBroadcastChannelRanges + * + * This method's purpose is to enable unit testing + */ + @VisibleForTesting + public void resetCellBroadcastChannelRanges() { + CellBroadcastChannelManager.clearAllCellBroadcastChannelRanges(); + } + private static void log(String msg) { Log.d(TAG, msg); } + private static void logd(String msg) { + if (DBG) Log.d(TAG, msg); + } + private static void loge(String msg) { Log.e(TAG, msg); } |