OSDN Git Service

support locale change for notification channels
authorfionaxu <fionaxu@google.com>
Tue, 2 May 2017 22:56:58 +0000 (15:56 -0700)
committerfionaxu <fionaxu@google.com>
Wed, 3 May 2017 01:17:20 +0000 (18:17 -0700)
Bug: 37911731
Test: Manual test with different languages
Change-Id: I36ce985dfc8fdb0f6c2b8b20c411b63099942f44

packages/CarrierDefaultApp/AndroidManifest.xml
packages/CarrierDefaultApp/src/com/android/carrierdefaultapp/CarrierActionUtils.java
packages/CarrierDefaultApp/src/com/android/carrierdefaultapp/CarrierDefaultBroadcastReceiver.java

index 2ef1cf5..c309133 100644 (file)
@@ -34,6 +34,7 @@
             <intent-filter>
                 <action android:name="com.android.internal.telephony.CARRIER_SIGNAL_REDIRECTED" />
                 <action android:name="com.android.internal.telephony.CARRIER_SIGNAL_RESET" />
+                <action android:name="android.intent.action.LOCALE_CHANGED" />
             </intent-filter>
         </receiver>
         <service android:name="com.android.carrierdefaultapp.ProvisionObserver"
index 7fd1601..0213306 100644 (file)
@@ -112,8 +112,6 @@ public class CarrierActionUtils {
 
     private static void onShowCaptivePortalNotification(Intent intent, Context context) {
         logd("onShowCaptivePortalNotification");
-        final NotificationManager notificationMgr = context.getSystemService(
-                NotificationManager.class);
         Intent portalIntent = new Intent(context, CaptivePortalLoginActivity.class);
         portalIntent.putExtras(intent);
         portalIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
@@ -123,7 +121,8 @@ public class CarrierActionUtils {
         Notification notification = getNotification(context, R.string.portal_notification_id,
                 R.string.portal_notification_detail, pendingIntent);
         try {
-            notificationMgr.notify(PORTAL_NOTIFICATION_TAG, PORTAL_NOTIFICATION_ID, notification);
+            context.getSystemService(NotificationManager.class)
+                    .notify(PORTAL_NOTIFICATION_TAG, PORTAL_NOTIFICATION_ID, notification);
         } catch (NullPointerException npe) {
             loge("setNotificationVisible: " + npe);
         }
@@ -131,12 +130,11 @@ public class CarrierActionUtils {
 
     private static void onShowNoDataServiceNotification(Context context) {
         logd("onShowNoDataServiceNotification");
-        final NotificationManager notificationMgr = context.getSystemService(
-                NotificationManager.class);
         Notification notification = getNotification(context, R.string.no_data_notification_id,
                 R.string.no_data_notification_detail, null);
         try {
-            notificationMgr.notify(NO_DATA_NOTIFICATION_TAG, NO_DATA_NOTIFICATION_ID, notification);
+            context.getSystemService(NotificationManager.class)
+                    .notify(NO_DATA_NOTIFICATION_TAG, NO_DATA_NOTIFICATION_ID, notification);
         } catch (NullPointerException npe) {
             loge("setNotificationVisible: " + npe);
         }
@@ -144,26 +142,16 @@ public class CarrierActionUtils {
 
     private static void onCancelAllNotifications(Context context) {
         logd("onCancelAllNotifications");
-        final NotificationManager notificationMgr = context.getSystemService(
-                NotificationManager.class);
-        notificationMgr.cancelAll();
+        context.getSystemService(NotificationManager.class).cancelAll();
     }
 
     private static Notification getNotification(Context context, int titleId, int textId,
                                          PendingIntent pendingIntent) {
         final TelephonyManager telephonyMgr = context.getSystemService(TelephonyManager.class);
-        final NotificationManager notificationManager = context.getSystemService(
-                NotificationManager.class);
         final Resources resources = context.getResources();
         final Bundle extras = Bundle.forPair(Notification.EXTRA_SUBSTITUTE_APP_NAME,
                 resources.getString(R.string.android_system_label));
-        /* Creates the notification channel and registers it with NotificationManager. If a channel
-         * with the same ID is already registered, NotificationManager will ignore this call.
-         */
-        notificationManager.createNotificationChannel(new NotificationChannel(
-                NOTIFICATION_CHANNEL_ID_MOBILE_DATA_STATUS,
-                resources.getString(R.string.mobile_data_status_notification_channel_name),
-                NotificationManager.IMPORTANCE_DEFAULT));
+        createNotificationChannels(context);
         Notification.Builder builder = new Notification.Builder(context)
                 .setContentTitle(resources.getString(titleId))
                 .setContentText(String.format(resources.getString(textId),
@@ -187,6 +175,19 @@ public class CarrierActionUtils {
         return builder.build();
     }
 
+    /**
+     * Creates the notification channel and registers it with NotificationManager. Also used to
+     * update an existing channel's name.
+     */
+    static void createNotificationChannels(Context context) {
+        context.getSystemService(NotificationManager.class)
+                .createNotificationChannel(new NotificationChannel(
+                NOTIFICATION_CHANNEL_ID_MOBILE_DATA_STATUS,
+                context.getResources().getString(
+                        R.string.mobile_data_status_notification_channel_name),
+                NotificationManager.IMPORTANCE_DEFAULT));
+    }
+
     private static void logd(String s) {
         Log.d(TAG, s);
     }
index 3fd89d9..3f55ff5 100644 (file)
@@ -32,6 +32,10 @@ public class CarrierDefaultBroadcastReceiver extends BroadcastReceiver{
             Log.d(TAG, "skip carrier actions during provisioning");
             return;
         }
+        if (Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) {
+            CarrierActionUtils.createNotificationChannels(context);
+            return;
+        }
         List<Integer> actionList = CustomConfigLoader.loadCarrierActionList(context, intent);
         for (int actionIdx : actionList) {
             Log.d(TAG, "apply carrier action idx: " + actionIdx);