Merge "Do not use notifyAsUser or cancelAsUser" am: 6f999ea9d2 am: d43187494c
am: e48b34ae63

Change-Id: Ib3c0330b3a24503b42ccbd14e707d5e2babe27c0
diff --git a/src/com/android/phone/NotificationMgr.java b/src/com/android/phone/NotificationMgr.java
index df4e379..f0473c9 100644
--- a/src/com/android/phone/NotificationMgr.java
+++ b/src/com/android/phone/NotificationMgr.java
@@ -27,6 +27,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.content.pm.UserInfo;
 import android.content.res.Resources;
@@ -112,7 +113,6 @@
     private PhoneGlobals mApp;
 
     private Context mContext;
-    private NotificationManager mNotificationManager;
     private StatusBarManager mStatusBarManager;
     private UserManager mUserManager;
     private Toast mToast;
@@ -160,8 +160,6 @@
     private NotificationMgr(PhoneGlobals app) {
         mApp = app;
         mContext = app;
-        mNotificationManager =
-                (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
         mStatusBarManager =
                 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
         mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
@@ -386,7 +384,7 @@
                         && !user.isManagedProfile()) {
                     if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, vmNumber,
                             pendingIntent, isSettingsIntent, userHandle, isRefresh)) {
-                        mNotificationManager.notifyAsUser(
+                        notifyAsUser(
                                 Integer.toString(subId) /* tag */,
                                 VOICEMAIL_NOTIFICATION,
                                 notification,
@@ -404,7 +402,7 @@
                         && !user.isManagedProfile()) {
                     if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, 0, null, null,
                             false, userHandle, isRefresh)) {
-                        mNotificationManager.cancelAsUser(
+                        cancelAsUser(
                                 Integer.toString(subId) /* tag */,
                                 VOICEMAIL_NOTIFICATION,
                                 userHandle);
@@ -548,7 +546,7 @@
                     intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
             builder.setContentIntent(PendingIntent.getActivity(mContext, subId /* requestCode */,
                     intent, 0));
-            mNotificationManager.notifyAsUser(
+            notifyAsUser(
                     Integer.toString(subId) /* tag */,
                     CALL_FORWARD_NOTIFICATION,
                     builder.build(),
@@ -560,7 +558,7 @@
                     continue;
                 }
                 UserHandle userHandle = user.getUserHandle();
-                mNotificationManager.cancelAsUser(
+                cancelAsUser(
                         Integer.toString(subId) /* tag */,
                         CALL_FORWARD_NOTIFICATION,
                         userHandle);
@@ -608,8 +606,35 @@
                 .setContentIntent(contentIntent);
         final Notification notif =
                 new Notification.BigTextStyle(builder).bigText(contentText).build();
-        mNotificationManager.notifyAsUser(
-                null /* tag */, DATA_ROAMING_NOTIFICATION, notif, UserHandle.ALL);
+        notifyAsUser(null /* tag */, DATA_ROAMING_NOTIFICATION, notif, UserHandle.ALL);
+    }
+
+    private void notifyAsUser(String tag, int id, Notification notification, UserHandle user) {
+        try {
+            Context contextForUser =
+                    mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user);
+            NotificationManager notificationManager =
+                    (NotificationManager) contextForUser.getSystemService(
+                            Context.NOTIFICATION_SERVICE);
+            notificationManager.notify(tag, id, notification);
+        } catch (PackageManager.NameNotFoundException e) {
+            Log.e(LOG_TAG, "unable to notify for user " + user);
+            e.printStackTrace();
+        }
+    }
+
+    private void cancelAsUser(String tag, int id, UserHandle user) {
+        try {
+            Context contextForUser =
+                    mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user);
+            NotificationManager notificationManager =
+                    (NotificationManager) contextForUser.getSystemService(
+                            Context.NOTIFICATION_SERVICE);
+            notificationManager.cancel(tag, id);
+        } catch (PackageManager.NameNotFoundException e) {
+            Log.e(LOG_TAG, "unable to cancel for user " + user);
+            e.printStackTrace();
+        }
     }
 
     /**
@@ -617,7 +642,7 @@
      */
     /* package */ void hideDataRoamingNotification() {
         if (DBG) log("hideDataRoamingNotification()...");
-        mNotificationManager.cancel(DATA_ROAMING_NOTIFICATION);
+        cancelAsUser(null, DATA_ROAMING_NOTIFICATION, UserHandle.ALL);
     }
 
     /**
@@ -732,7 +757,7 @@
                 mContext.getString(R.string.mobile_network_settings_class)));
         intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, subId);
         builder.setContentIntent(PendingIntent.getActivity(mContext, 0, intent, 0));
-        mNotificationManager.notifyAsUser(
+        notifyAsUser(
                 Integer.toString(subId) /* tag */,
                 SELECTED_OPERATOR_FAIL_NOTIFICATION,
                 builder.build(),
@@ -745,7 +770,7 @@
      */
     private void cancelNetworkSelection(int subId) {
         if (DBG) log("cancelNetworkSelection()...");
-        mNotificationManager.cancelAsUser(
+        cancelAsUser(
                 Integer.toString(subId) /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION,
                 UserHandle.ALL);
     }