Revert "Do not use notifyAsUser or cancelAsUser"

This reverts commit 07b661fb3947f0034be5083109aaab1610e498c1.

Reason for revert: broken build

Change-Id: If5c1e015885a39727239be4e1f018f84d7ba1bc2
diff --git a/src/com/android/phone/NotificationMgr.java b/src/com/android/phone/NotificationMgr.java
index b3a6e71..df4e379 100644
--- a/src/com/android/phone/NotificationMgr.java
+++ b/src/com/android/phone/NotificationMgr.java
@@ -27,7 +27,6 @@
 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;
@@ -113,6 +112,7 @@
     private PhoneGlobals mApp;
 
     private Context mContext;
+    private NotificationManager mNotificationManager;
     private StatusBarManager mStatusBarManager;
     private UserManager mUserManager;
     private Toast mToast;
@@ -160,6 +160,8 @@
     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);
@@ -384,7 +386,7 @@
                         && !user.isManagedProfile()) {
                     if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, vmNumber,
                             pendingIntent, isSettingsIntent, userHandle, isRefresh)) {
-                        notifyAsUser(
+                        mNotificationManager.notifyAsUser(
                                 Integer.toString(subId) /* tag */,
                                 VOICEMAIL_NOTIFICATION,
                                 notification,
@@ -402,7 +404,7 @@
                         && !user.isManagedProfile()) {
                     if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, 0, null, null,
                             false, userHandle, isRefresh)) {
-                        cancelAsUser(
+                        mNotificationManager.cancelAsUser(
                                 Integer.toString(subId) /* tag */,
                                 VOICEMAIL_NOTIFICATION,
                                 userHandle);
@@ -546,7 +548,7 @@
                     intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
             builder.setContentIntent(PendingIntent.getActivity(mContext, subId /* requestCode */,
                     intent, 0));
-            notifyAsUser(
+            mNotificationManager.notifyAsUser(
                     Integer.toString(subId) /* tag */,
                     CALL_FORWARD_NOTIFICATION,
                     builder.build(),
@@ -558,7 +560,7 @@
                     continue;
                 }
                 UserHandle userHandle = user.getUserHandle();
-                cancelAsUser(
+                mNotificationManager.cancelAsUser(
                         Integer.toString(subId) /* tag */,
                         CALL_FORWARD_NOTIFICATION,
                         userHandle);
@@ -606,35 +608,8 @@
                 .setContentIntent(contentIntent);
         final Notification notif =
                 new Notification.BigTextStyle(builder).bigText(contentText).build();
-        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();
-        }
+        mNotificationManager.notifyAsUser(
+                null /* tag */, DATA_ROAMING_NOTIFICATION, notif, UserHandle.ALL);
     }
 
     /**
@@ -642,7 +617,7 @@
      */
     /* package */ void hideDataRoamingNotification() {
         if (DBG) log("hideDataRoamingNotification()...");
-        cancelAsUser(null, DATA_ROAMING_NOTIFICATION, UserHandle.ALL);
+        mNotificationManager.cancel(DATA_ROAMING_NOTIFICATION);
     }
 
     /**
@@ -684,8 +659,9 @@
         final Notification notification = new Notification.BigTextStyle(builder).bigText(
                 contentText).build();
 
-        notifyAsUser(Integer.toString(subId), LIMITED_SIM_FUNCTION_NOTIFICATION, notification,
-                UserHandle.ALL);
+        mNotificationManager.notifyAsUser(Integer.toString(subId),
+                LIMITED_SIM_FUNCTION_NOTIFICATION,
+                notification, UserHandle.ALL);
         mLimitedSimFunctionNotify.add(subId);
     }
 
@@ -697,12 +673,12 @@
         if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
             // dismiss all notifications
             for (int id : mLimitedSimFunctionNotify) {
-                cancelAsUser(Integer.toString(id),
+                mNotificationManager.cancelAsUser(Integer.toString(id),
                         LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
             }
             mLimitedSimFunctionNotify.clear();
         } else if (mLimitedSimFunctionNotify.contains(subId)) {
-            cancelAsUser(Integer.toString(subId),
+            mNotificationManager.cancelAsUser(Integer.toString(subId),
                     LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
             mLimitedSimFunctionNotify.remove(subId);
         }
@@ -718,7 +694,7 @@
         // from the old SIM if both old & new SIM configured to display the notification.
         mLimitedSimFunctionNotify.removeIf(id -> {
             if (!mSubscriptionManager.isActiveSubId(id)) {
-                cancelAsUser(Integer.toString(id),
+                mNotificationManager.cancelAsUser(Integer.toString(id),
                         LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
                 return true;
             }
@@ -756,7 +732,7 @@
                 mContext.getString(R.string.mobile_network_settings_class)));
         intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, subId);
         builder.setContentIntent(PendingIntent.getActivity(mContext, 0, intent, 0));
-        notifyAsUser(
+        mNotificationManager.notifyAsUser(
                 Integer.toString(subId) /* tag */,
                 SELECTED_OPERATOR_FAIL_NOTIFICATION,
                 builder.build(),
@@ -769,7 +745,7 @@
      */
     private void cancelNetworkSelection(int subId) {
         if (DBG) log("cancelNetworkSelection()...");
-        cancelAsUser(
+        mNotificationManager.cancelAsUser(
                 Integer.toString(subId) /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION,
                 UserHandle.ALL);
     }