diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2019-03-28 17:09:41 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2019-03-28 17:09:41 +0000 |
commit | 32c9f38bdc5968b229cc32f043fae952439ff77e (patch) | |
tree | 91a7d8f833d322e4e5dad39997385b03afc60a8f | |
parent | 4aca515f71fe8d7559883e8b2e782a0ac675296a (diff) | |
parent | 53f3a56cd77cccf7b5c6dcd2682676977fa5ec5d (diff) | |
download | CellBroadcastReceiver-32c9f38bdc5968b229cc32f043fae952439ff77e.tar.gz |
Merge "Use app resources for default SMS subscriptionId always"
10 files changed, 98 insertions, 50 deletions
diff --git a/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java b/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java index e75d09b1f..ee8db74e2 100644 --- a/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java +++ b/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java @@ -345,13 +345,16 @@ public class CellBroadcastAlertAudio extends Service implements TextToSpeech.OnI stop(); log("playAlertTone: alertType=" + alertType); + Resources res = + CellBroadcastSettings.getResourcesForDefaultSmsSubscriptionId( + getApplicationContext()); // Vibration duration in milliseconds long vibrateDuration = 0; // Get the alert tone duration. Negative tone duration value means we only play the tone // once, not repeat it. - int customAlertDuration = getResources().getInteger(R.integer.alert_duration); + int customAlertDuration = res.getInteger(R.integer.alert_duration); // Start the vibration first. if (mEnableVibrate) { @@ -404,26 +407,21 @@ public class CellBroadcastAlertAudio extends Service implements TextToSpeech.OnI } try { - log("Locale=" + getResources().getConfiguration().getLocales() - + ", alertType=" + alertType); + log("Locale=" + res.getConfiguration().getLocales() + ", alertType=" + alertType); // Load the tones based on type switch (alertType) { case ETWS_EARTHQUAKE: - setDataSourceFromResource(getResources(), mMediaPlayer, - R.raw.etws_earthquake); + setDataSourceFromResource(res, mMediaPlayer, R.raw.etws_earthquake); break; case ETWS_TSUNAMI: - setDataSourceFromResource(getResources(), mMediaPlayer, - R.raw.etws_tsunami); + setDataSourceFromResource(res, mMediaPlayer, R.raw.etws_tsunami); break; case OTHER: - setDataSourceFromResource(getResources(), mMediaPlayer, - R.raw.etws_other_disaster); + setDataSourceFromResource(res, mMediaPlayer, R.raw.etws_other_disaster); break; case ETWS_DEFAULT: - setDataSourceFromResource(getResources(), mMediaPlayer, - R.raw.etws_default); + setDataSourceFromResource(res, mMediaPlayer, R.raw.etws_default); break; case INFO: // for non-emergency alerts, we are using system default notification sound. @@ -435,8 +433,7 @@ public class CellBroadcastAlertAudio extends Service implements TextToSpeech.OnI case TEST: case DEFAULT: default: - setDataSourceFromResource(getResources(), mMediaPlayer, - R.raw.default_tone); + setDataSourceFromResource(res, mMediaPlayer, R.raw.default_tone); } // Request audio focus (though we're going to play even if we don't get it) diff --git a/src/com/android/cellbroadcastreceiver/CellBroadcastAlertDialog.java b/src/com/android/cellbroadcastreceiver/CellBroadcastAlertDialog.java index 54ee71dca..a9495c965 100644 --- a/src/com/android/cellbroadcastreceiver/CellBroadcastAlertDialog.java +++ b/src/com/android/cellbroadcastreceiver/CellBroadcastAlertDialog.java @@ -350,15 +350,16 @@ public class CellBroadcastAlertDialog extends Activity { * @param message CB message which is used to update alert text. */ private void updateAlertText(CellBroadcastMessage message) { - int titleId = CellBroadcastResources.getDialogTitleResource( - getApplicationContext(), message); + Context context = getApplicationContext(); + int titleId = CellBroadcastResources.getDialogTitleResource(context, message); String title = getText(titleId).toString(); TextView titleTextView = findViewById(R.id.alertTitle); - if (getApplicationContext().getResources().getBoolean(R.bool.show_date_time_title)) { + if (CellBroadcastSettings.getResourcesForDefaultSmsSubscriptionId(context) + .getBoolean(R.bool.show_date_time_title)) { titleTextView.setSingleLine(false); - title += "\n" + message.getDateString(getApplicationContext()); + title += "\n" + message.getDateString(context); } setTitle(title); diff --git a/src/com/android/cellbroadcastreceiver/CellBroadcastAlertService.java b/src/com/android/cellbroadcastreceiver/CellBroadcastAlertService.java index 5edb3e095..ffa05728a 100644 --- a/src/com/android/cellbroadcastreceiver/CellBroadcastAlertService.java +++ b/src/com/android/cellbroadcastreceiver/CellBroadcastAlertService.java @@ -306,7 +306,9 @@ public class CellBroadcastAlertService extends Service { // Check if message body should be used for duplicate detection. boolean shouldCompareMessageBody = - getApplicationContext().getResources().getBoolean(R.bool.duplicate_compare_body); + CellBroadcastSettings.getResourcesForDefaultSmsSubscriptionId( + getApplicationContext()) + .getBoolean(R.bool.duplicate_compare_body); int hashCode = shouldCompareMessageBody ? message.getMessageBody().hashCode() : 0; @@ -601,10 +603,13 @@ public class CellBroadcastAlertService extends Service { CellBroadcastChannelRange range = CellBroadcastChannelManager .getCellBroadcastChannelRangeFromMessage(getApplicationContext(), message); audioIntent.putExtra(CellBroadcastAlertAudio.ALERT_AUDIO_TONE_TYPE, alertType); - audioIntent.putExtra(CellBroadcastAlertAudio.ALERT_AUDIO_VIBRATION_PATTERN_EXTRA, - (range != null) ? range.mVibrationPattern - : getApplicationContext().getResources().getIntArray( - R.array.default_vibration_pattern)); + audioIntent.putExtra( + CellBroadcastAlertAudio.ALERT_AUDIO_VIBRATION_PATTERN_EXTRA, + (range != null) + ? range.mVibrationPattern + : CellBroadcastSettings.getResourcesForDefaultSmsSubscriptionId( + getApplicationContext()) + .getIntArray(R.array.default_vibration_pattern)); String messageBody = message.getMessageBody(); @@ -647,6 +652,7 @@ public class CellBroadcastAlertService extends Service { static void addToNotificationBar(CellBroadcastMessage message, ArrayList<CellBroadcastMessage> messageList, Context context, boolean fromSaveState) { + Resources res = CellBroadcastSettings.getResourcesForDefaultSmsSubscriptionId(context); int channelTitleId = CellBroadcastResources.getDialogTitleResource(context, message); CharSequence channelName = context.getText(channelTitleId); String messageBody = message.getMessageBody(); @@ -677,15 +683,16 @@ public class CellBroadcastAlertService extends Service { final String channelId = CellBroadcastChannelManager.isEmergencyMessage(context, message) ? NOTIFICATION_CHANNEL_EMERGENCY_ALERTS : NOTIFICATION_CHANNEL_NON_EMERGENCY_ALERTS; // use default sound/vibration/lights for non-emergency broadcasts - Notification.Builder builder = new Notification.Builder(context, channelId) - .setSmallIcon(R.drawable.ic_warning_googred) - .setTicker(channelName) - .setWhen(System.currentTimeMillis()) - .setCategory(Notification.CATEGORY_SYSTEM) - .setPriority(Notification.PRIORITY_HIGH) - .setColor(context.getResources().getColor(R.color.notification_color)) - .setVisibility(Notification.VISIBILITY_PUBLIC) - .setOngoing(message.isEmergencyAlertMessage()); + Notification.Builder builder = + new Notification.Builder(context, channelId) + .setSmallIcon(R.drawable.ic_warning_googred) + .setTicker(channelName) + .setWhen(System.currentTimeMillis()) + .setCategory(Notification.CATEGORY_SYSTEM) + .setPriority(Notification.PRIORITY_HIGH) + .setColor(res.getColor(R.color.notification_color)) + .setVisibility(Notification.VISIBILITY_PUBLIC) + .setOngoing(message.isEmergencyAlertMessage()); if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH)) { builder.setDeleteIntent(pi); @@ -719,7 +726,7 @@ public class CellBroadcastAlertService extends Service { // Alert Dialog, it will call this and override the emergency audio tone. if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH) && !CellBroadcastChannelManager.isEmergencyMessage(context, message)) { - if (context.getResources().getBoolean(R.bool.watch_enable_non_emergency_audio)) { + if (res.getBoolean(R.bool.watch_enable_non_emergency_audio)) { // start audio/vibration/speech service for non emergency alerts Intent audioIntent = new Intent(context, CellBroadcastAlertAudio.class); audioIntent.setAction(CellBroadcastAlertAudio.ACTION_START_ALERT_AUDIO); diff --git a/src/com/android/cellbroadcastreceiver/CellBroadcastChannelManager.java b/src/com/android/cellbroadcastreceiver/CellBroadcastChannelManager.java index 979715516..d209c8f4d 100644 --- a/src/com/android/cellbroadcastreceiver/CellBroadcastChannelManager.java +++ b/src/com/android/cellbroadcastreceiver/CellBroadcastChannelManager.java @@ -114,8 +114,9 @@ public class CellBroadcastChannelManager { mEmergencyLevel = LEVEL_UNKNOWN; mRat = SmsManager.CELL_BROADCAST_RAN_TYPE_GSM; mScope = SCOPE_UNKNOWN; - mVibrationPattern = context.getResources().getIntArray( - R.array.default_vibration_pattern); + mVibrationPattern = + CellBroadcastSettings.getResourcesForDefaultSmsSubscriptionId(context) + .getIntArray(R.array.default_vibration_pattern); mFilterLanguage = false; int colonIndex = channelRange.indexOf(':'); @@ -214,7 +215,9 @@ public class CellBroadcastChannelManager { public static ArrayList<CellBroadcastChannelRange> getCellBroadcastChannelRanges( Context context, int key) { ArrayList<CellBroadcastChannelRange> result = new ArrayList<>(); - String[] ranges = context.getResources().getStringArray(key); + String[] ranges = + CellBroadcastSettings.getResourcesForDefaultSmsSubscriptionId(context) + .getStringArray(key); if (ranges != null) { for (String range : ranges) { diff --git a/src/com/android/cellbroadcastreceiver/CellBroadcastReceiver.java b/src/com/android/cellbroadcastreceiver/CellBroadcastReceiver.java index f761ab018..1c40d1690 100644 --- a/src/com/android/cellbroadcastreceiver/CellBroadcastReceiver.java +++ b/src/com/android/cellbroadcastreceiver/CellBroadcastReceiver.java @@ -113,8 +113,9 @@ public class CellBroadcastReceiver extends BroadcastReceiver { String currentIntervalDefault = sp.getString(CURRENT_INTERVAL_DEFAULT, "0"); // If interval default changes, reset the interval to the new default value. - String newIntervalDefault = context.getResources().getString( - R.string.alert_reminder_interval_default_value); + String newIntervalDefault = + CellBroadcastSettings.getResourcesForDefaultSmsSubscriptionId(context) + .getString(R.string.alert_reminder_interval_default_value); if (!newIntervalDefault.equals(currentIntervalDefault)) { Log.d(TAG, "Default interval changed from " + currentIntervalDefault + " to " + newIntervalDefault); diff --git a/src/com/android/cellbroadcastreceiver/CellBroadcastSearchIndexableProvider.java b/src/com/android/cellbroadcastreceiver/CellBroadcastSearchIndexableProvider.java index d750bac3b..9f078f4d8 100644 --- a/src/com/android/cellbroadcastreceiver/CellBroadcastSearchIndexableProvider.java +++ b/src/com/android/cellbroadcastreceiver/CellBroadcastSearchIndexableProvider.java @@ -89,7 +89,8 @@ public class CellBroadcastSearchIndexableProvider extends SearchIndexablesProvid @Override public Cursor queryRawData(String[] projection) { MatrixCursor cursor = new MatrixCursor(INDEXABLES_RAW_COLUMNS); - final Resources res = getContext().getResources(); + final Resources res = + CellBroadcastSettings.getResourcesForDefaultSmsSubscriptionId(getContext()); Object[] raw = new Object[INDEXABLES_RAW_COLUMNS.length]; raw[COLUMN_INDEX_RAW_TITLE] = res.getString(R.string.sms_cb_settings); @@ -130,7 +131,7 @@ public class CellBroadcastSearchIndexableProvider extends SearchIndexablesProvid boolean enableDevSettings = Settings.Global.getInt(getContext().getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0; - Resources res = getContext().getResources(); + Resources res = CellBroadcastSettings.getResourcesForDefaultSmsSubscriptionId(getContext()); Object[] ref; ref = new Object[1]; diff --git a/src/com/android/cellbroadcastreceiver/CellBroadcastSettings.java b/src/com/android/cellbroadcastreceiver/CellBroadcastSettings.java index 0f6357365..f3029998d 100644 --- a/src/com/android/cellbroadcastreceiver/CellBroadcastSettings.java +++ b/src/com/android/cellbroadcastreceiver/CellBroadcastSettings.java @@ -276,8 +276,7 @@ public class CellBroadcastSettings extends Activity { boolean enableDevSettings = Settings.Global.getInt(getContext().getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0; - Resources res = getResources(); - + Resources res = getResourcesForDefaultSmsSubscriptionId(getContext()); initReminderIntervalList(); boolean emergencyAlertOnOffOptionEnabled = isFeatureEnabled(getContext(), @@ -426,11 +425,11 @@ public class CellBroadcastSettings extends Activity { } private void initReminderIntervalList() { + Resources res = getResourcesForDefaultSmsSubscriptionId(getContext()); String[] activeValues = - getResources().getStringArray(R.array.alert_reminder_interval_active_values); - String[] allEntries = - getResources().getStringArray(R.array.alert_reminder_interval_entries); + res.getStringArray(R.array.alert_reminder_interval_active_values); + String[] allEntries = res.getStringArray(R.array.alert_reminder_interval_entries); String[] newEntries = new String[activeValues.length]; // Only add active interval to the list @@ -520,4 +519,15 @@ public class CellBroadcastSettings extends Activity { return defaultValue; } + + public static Resources getResourcesForDefaultSmsSubscriptionId(Context context) { + int subId = SubscriptionManager.getDefaultSmsSubscriptionId(); + if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { + subId = SubscriptionManager.getDefaultSubscriptionId(); + if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { + return context.getResources(); + } + } + return SubscriptionManager.getResourcesForSubId(context, subId); + } } diff --git a/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastBootupConfigTest.java b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastBootupConfigTest.java index b63c7abd4..f2c1ae774 100644 --- a/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastBootupConfigTest.java +++ b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastBootupConfigTest.java @@ -70,8 +70,6 @@ public class CellBroadcastBootupConfigTest extends @Captor private ArgumentCaptor<Integer> mTypes; - private MockedServiceManager mMockedServiceManager; - public CellBroadcastBootupConfigTest() { super(CellBroadcastConfigService.class); } @@ -81,7 +79,6 @@ public class CellBroadcastBootupConfigTest extends super.setUp(); doReturn(mSmsService).when(mSmsService).queryLocalInterface(anyString()); - mMockedServiceManager = new MockedServiceManager(); mMockedServiceManager.replaceService("isms", mSmsService); putResources(R.array.cmas_presidential_alerts_channels_range_strings, new String[]{ "0x1112-0x1112:rat=gsm", @@ -111,7 +108,6 @@ public class CellBroadcastBootupConfigTest extends @After public void tearDown() throws Exception { - mMockedServiceManager.restoreAllServices(); super.tearDown(); } @@ -190,4 +186,4 @@ public class CellBroadcastBootupConfigTest extends assertEquals("i=" + i, configs[i].type, mTypes.getAllValues().get(i).intValue()); } } -}
\ No newline at end of file +} diff --git a/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastServiceTestCase.java b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastServiceTestCase.java index d62019bd5..c29dff499 100644 --- a/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastServiceTestCase.java +++ b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastServiceTestCase.java @@ -17,6 +17,7 @@ package com.android.cellbroadcastreceiver; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doReturn; import android.app.Service; @@ -26,9 +27,13 @@ import android.content.ContextWrapper; import android.content.Intent; import android.content.res.Resources; import android.telephony.CarrierConfigManager; +import android.telephony.SubscriptionManager; import android.test.ServiceTestCase; import android.util.Log; +import com.android.internal.telephony.ISub; + +import org.junit.After; import org.junit.Before; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -39,6 +44,10 @@ public abstract class CellBroadcastServiceTestCase<T extends Service> extends Se protected CarrierConfigManager mMockedCarrierConfigManager; @Mock Resources mResources; + @Mock + protected ISub.Stub mSubService; + + MockedServiceManager mMockedServiceManager; Intent mServiceIntentToVerify; @@ -98,12 +107,23 @@ public abstract class CellBroadcastServiceTestCase<T extends Service> extends Se @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); + // A hack to return mResources from static method + // CellBroadcastSettings.getResourcesForDefaultSmsSubscriptionId(context). + doReturn(mSubService).when(mSubService).queryLocalInterface(anyString()); + doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mSubService).getDefaultSubId(); + doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mSubService).getDefaultSmsSubId(); + mMockedServiceManager = new MockedServiceManager(); + mMockedServiceManager.replaceService("isub", mSubService); mContext = new TestContextWrapper(getContext()); setContext(mContext); } + @After + public void tearDown() throws Exception { + mMockedServiceManager.restoreAllServices(); + } + void putResources(int id, String[] values) { doReturn(values).when(mResources).getStringArray(eq(id)); } } - diff --git a/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastTest.java b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastTest.java index bda8cb2a1..c97e6382f 100644 --- a/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastTest.java +++ b/tests/unit/src/com/android/cellbroadcastreceiver/CellBroadcastTest.java @@ -16,6 +16,7 @@ package com.android.cellbroadcastreceiver; +import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.eq; @@ -23,9 +24,12 @@ import android.content.Context; import android.content.res.Resources; import android.os.PersistableBundle; import android.telephony.CarrierConfigManager; +import android.telephony.SubscriptionManager; import android.util.Log; import android.util.SparseArray; +import com.android.internal.telephony.ISub; + import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -43,11 +47,19 @@ public abstract class CellBroadcastTest { CarrierConfigManager mCarrierConfigManager; @Mock Resources mResources; + @Mock + ISub.Stub mSubService; protected void setUp(String tag) throws Exception { TAG = tag; MockitoAnnotations.initMocks(this); + // A hack to return mResources from static method + // CellBroadcastSettings.getResourcesForDefaultSmsSubscriptionId(context). + doReturn(mSubService).when(mSubService).queryLocalInterface(anyString()); + doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mSubService).getDefaultSubId(); + doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mSubService).getDefaultSmsSubId(); mMockedServiceManager = new MockedServiceManager(); + mMockedServiceManager.replaceService("isub", mSubService); initContext(); } |