Do not use notifyAsUser or cancelAsUser

Instead we can create context as user and call the public notify/cancel
methods.

Bug: 137202333
Test: manual
Change-Id: Ic54921df028ccdb12a8c61d8ab5f27d9af6e2945
Merged-In: Ic54921df028ccdb12a8c61d8ab5f27d9af6e2945
diff --git a/src/com/android/phone/NotificationMgr.java b/src/com/android/phone/NotificationMgr.java
index e556775..5a5b444 100644
--- a/src/com/android/phone/NotificationMgr.java
+++ b/src/com/android/phone/NotificationMgr.java
@@ -26,6 +26,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;
@@ -107,7 +108,6 @@
     private PhoneGlobals mApp;
 
     private Context mContext;
-    private NotificationManager mNotificationManager;
     private StatusBarManager mStatusBarManager;
     private UserManager mUserManager;
     private Toast mToast;
@@ -152,8 +152,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);
@@ -378,7 +376,7 @@
                         && !user.isManagedProfile()) {
                     if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, vmNumber,
                             pendingIntent, isSettingsIntent, userHandle, isRefresh)) {
-                        mNotificationManager.notifyAsUser(
+                        notifyAsUser(
                                 Integer.toString(subId) /* tag */,
                                 VOICEMAIL_NOTIFICATION,
                                 notification,
@@ -396,7 +394,7 @@
                         && !user.isManagedProfile()) {
                     if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, 0, null, null,
                             false, userHandle, isRefresh)) {
-                        mNotificationManager.cancelAsUser(
+                        cancelAsUser(
                                 Integer.toString(subId) /* tag */,
                                 VOICEMAIL_NOTIFICATION,
                                 userHandle);
@@ -540,7 +538,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(),
@@ -552,7 +550,7 @@
                     continue;
                 }
                 UserHandle userHandle = user.getUserHandle();
-                mNotificationManager.cancelAsUser(
+                cancelAsUser(
                         Integer.toString(subId) /* tag */,
                         CALL_FORWARD_NOTIFICATION,
                         userHandle);
@@ -600,8 +598,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();
+        }
     }
 
     /**
@@ -609,7 +634,7 @@
      */
     /* package */ void hideDataRoamingNotification() {
         if (DBG) log("hideDataRoamingNotification()...");
-        mNotificationManager.cancel(DATA_ROAMING_NOTIFICATION);
+        cancelAsUser(null, DATA_ROAMING_NOTIFICATION, UserHandle.ALL);
     }
 
     /**
@@ -642,7 +667,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(),
@@ -655,7 +680,7 @@
      */
     private void cancelNetworkSelection(int subId) {
         if (DBG) log("cancelNetworkSelection()...");
-        mNotificationManager.cancelAsUser(
+        cancelAsUser(
                 Integer.toString(subId) /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION,
                 UserHandle.ALL);
     }