Merge "Revert "Ensure all RemoteViews use myUserId rather than context."" into lmp-dev
diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java
index f8dfdd9..5921477 100644
--- a/core/java/android/app/Notification.java
+++ b/core/java/android/app/Notification.java
@@ -21,7 +21,6 @@
 import android.annotation.SdkConstant.SdkConstantType;
 import android.content.Context;
 import android.content.Intent;
-import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
@@ -825,6 +824,13 @@
     public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
 
     /**
+     * {@link #extras} key: the user that built the notification.
+     *
+     * @hide
+     */
+    public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
+
+    /**
      * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification should not be
      * displayed in the heads up space.
      *
@@ -1807,12 +1813,10 @@
                 = "android.rebuild.hudViewActionCount";
 
         /**
-         * The ApplicationInfo of the package that created the notification, used to create
-         * a context to rebuild the notification via a Builder.
-         * @hide
+         * The package name of the context used to create the notification via a Builder.
          */
-        private static final String EXTRA_REBUILD_CONTEXT_APPLICATION_INFO =
-                "android.rebuild.applicationInfo";
+        private static final String EXTRA_REBUILD_CONTEXT_PACKAGE =
+                "android.rebuild.contextPackage";
 
         // Whether to enable stripping (at post time) & rebuilding (at listener receive time) of
         // memory intensive resources.
@@ -1863,6 +1867,11 @@
         private int mColor = COLOR_DEFAULT;
 
         /**
+         * The user that built the notification originally.
+         */
+        private int mOriginatingUserId;
+
+        /**
          * Contains extras related to rebuilding during the build phase.
          */
         private Bundle mRebuildBundle = new Bundle();
@@ -2582,7 +2591,7 @@
             // Note: This assumes that the current user can read the profile badge of the
             // originating user.
             UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
-            return userManager.getBadgeForUser(new UserHandle(mContext.getUserId()), 0);
+            return userManager.getBadgeForUser(new UserHandle(mOriginatingUserId), 0);
         }
 
         private Bitmap getProfileBadge() {
@@ -2661,7 +2670,8 @@
          * @param hasProgress whether the progress bar should be shown and set
          */
         private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
-            RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
+            RemoteViews contentView = new BuilderRemoteViews(mContext.getPackageName(),
+                    mOriginatingUserId, resId);
 
             resetStandardTemplate(contentView);
 
@@ -3053,8 +3063,8 @@
          */
         public void populateExtras(Bundle extras) {
             // Store original information used in the construction of this object
-            extras.putParcelable(EXTRA_REBUILD_CONTEXT_APPLICATION_INFO,
-                    mContext.getApplicationInfo());
+            extras.putInt(EXTRA_ORIGINATING_USERID, mOriginatingUserId);
+            extras.putString(EXTRA_REBUILD_CONTEXT_PACKAGE, mContext.getPackageName());
             extras.putCharSequence(EXTRA_TITLE, mContentTitle);
             extras.putCharSequence(EXTRA_TEXT, mContentText);
             extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
@@ -3142,14 +3152,13 @@
             extras.remove(EXTRA_NEEDS_REBUILD);
 
             // Re-create notification context so we can access app resources.
-            ApplicationInfo applicationInfo = extras.getParcelable(
-                    EXTRA_REBUILD_CONTEXT_APPLICATION_INFO);
+            String packageName = extras.getString(EXTRA_REBUILD_CONTEXT_PACKAGE);
             Context builderContext;
             try {
-                builderContext = context.createApplicationContext(applicationInfo,
+                builderContext = context.createPackageContext(packageName,
                         Context.CONTEXT_RESTRICTED);
             } catch (NameNotFoundException e) {
-                Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
+                Log.e(TAG, "Package name " + packageName + " not found");
                 builderContext = context;  // try with our context
             }
 
@@ -3284,6 +3293,7 @@
 
             // Extras.
             Bundle extras = n.extras;
+            mOriginatingUserId = extras.getInt(EXTRA_ORIGINATING_USERID);
             mContentTitle = extras.getCharSequence(EXTRA_TITLE);
             mContentText = extras.getCharSequence(EXTRA_TEXT);
             mSubText = extras.getCharSequence(EXTRA_SUB_TEXT);
@@ -3316,6 +3326,7 @@
          * object.
          */
         public Notification build() {
+            mOriginatingUserId = mContext.getUserId();
             mHasThreeLines = hasThreeLines();
 
             Notification n = buildUnstyled();
@@ -4815,8 +4826,8 @@
             super(parcel);
         }
 
-        public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
-            super(appInfo, layoutId);
+        public BuilderRemoteViews(String packageName, int userId, int layoutId) {
+            super(packageName, userId, layoutId);
         }
 
         @Override
diff --git a/core/java/android/text/format/DateFormat.java b/core/java/android/text/format/DateFormat.java
index 1e411b4..9fec9a1 100755
--- a/core/java/android/text/format/DateFormat.java
+++ b/core/java/android/text/format/DateFormat.java
@@ -17,7 +17,6 @@
 package android.text.format;
 
 import android.content.Context;
-import android.os.UserHandle;
 import android.provider.Settings;
 import android.text.SpannableStringBuilder;
 import android.text.Spanned;
@@ -129,14 +128,8 @@
      * @return true if 24 hour time format is selected, false otherwise.
      */
     public static boolean is24HourFormat(Context context) {
-        // This method is called by View classes that can be used by RemoteViews
-        // and rendered in another user. The context therefore may reference
-        // a user that would require interact accross users to access. So
-        // use the user id we are running as.
-        // This is the case when we have widgets from a user profile added
-        // to the homescreen.
-        String value = Settings.System.getStringForUser(context.getContentResolver(),
-                Settings.System.TIME_12_24, UserHandle.myUserId());
+        String value = Settings.System.getString(context.getContentResolver(),
+                Settings.System.TIME_12_24);
 
         if (value == null) {
             Locale locale = context.getResources().getConfiguration().locale;
@@ -234,14 +227,8 @@
      * @return the {@link java.text.DateFormat} object that properly formats the date.
      */
     public static java.text.DateFormat getDateFormat(Context context) {
-        // This method is called by View classes that can be used by RemoteViews
-        // and rendered in another user. The context therefore may reference
-        // a user that would require interact accross users to access. So
-        // use the user id we are running as.
-        // This is the case when we have widgets from a user profile added
-        // to the homescreen.
-        String value = Settings.System.getStringForUser(context.getContentResolver(),
-                Settings.System.DATE_FORMAT, UserHandle.myUserId());
+        String value = Settings.System.getString(context.getContentResolver(),
+                Settings.System.DATE_FORMAT);
 
         return getDateFormatForSetting(context, value);
     }
diff --git a/core/java/android/widget/DateTimeView.java b/core/java/android/widget/DateTimeView.java
index b3ff88b..45d1403 100644
--- a/core/java/android/widget/DateTimeView.java
+++ b/core/java/android/widget/DateTimeView.java
@@ -29,7 +29,6 @@
 import android.provider.Settings;
 import android.widget.TextView;
 import android.widget.RemoteViews.RemoteView;
-import android.os.UserHandle;
 
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
@@ -191,15 +190,8 @@
     }
 
     private DateFormat getDateFormat() {
-        // OK, this is gross but needed. This class is supported by the
-        // remote views mechanism and as a part of that the remote views
-        // can be inflated by a context for another user without the app
-        // having interact users permission - just for loading resources.
-        // For example, when adding widgets from a user profile to the
-        // home screen. Therefore, we access settings as the user the app
-        // is running as not the one the context is for.
-        String format = Settings.System.getStringForUser(getContext().getContentResolver(),
-                Settings.System.DATE_FORMAT, UserHandle.myUserId());
+        String format = Settings.System.getString(getContext().getContentResolver(),
+                Settings.System.DATE_FORMAT);
         if (format == null || "".equals(format)) {
             return DateFormat.getDateInstance(DateFormat.SHORT);
         } else {
@@ -220,20 +212,10 @@
         filter.addAction(Intent.ACTION_TIME_CHANGED);
         filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
         filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
-
-        // OK, this is gross but needed. This class is supported by the
-        // remote views mechanism and as a part of that the remote views
-        // can be inflated by a context for another user without the app
-        // having interact users permission - just for loading resources.
-        // For example, when adding widgets from a user profile to the
-        // home screen. Therefore, we register the receiver and content
-        // observer as the user the app is running as not the one the context is for.
-        context.registerReceiverAsUser(mBroadcastReceiver, android.os.Process.myUserHandle(),
-                filter, null, null);
+        context.registerReceiver(mBroadcastReceiver, filter);
 
         Uri uri = Settings.System.getUriFor(Settings.System.DATE_FORMAT);
-        context.getContentResolver().registerContentObserver(uri, true, mContentObserver,
-                UserHandle.myUserId());
+        context.getContentResolver().registerContentObserver(uri, true, mContentObserver);
     }
 
     private void unregisterReceivers() {
diff --git a/core/java/android/widget/RemoteViews.java b/core/java/android/widget/RemoteViews.java
index 9de69f2..69d5f40 100644
--- a/core/java/android/widget/RemoteViews.java
+++ b/core/java/android/widget/RemoteViews.java
@@ -1653,10 +1653,8 @@
      *
      * @param application The application whose content is shown by the views.
      * @param layoutId The id of the layout resource.
-     *
-     * @hide
      */
-    protected RemoteViews(ApplicationInfo application, int layoutId) {
+    private RemoteViews(ApplicationInfo application, int layoutId) {
         mApplication = application;
         mLayoutId = layoutId;
         mBitmapCache = new BitmapCache();
diff --git a/core/java/android/widget/TextClock.java b/core/java/android/widget/TextClock.java
index acfc543..4c5c71d 100644
--- a/core/java/android/widget/TextClock.java
+++ b/core/java/android/widget/TextClock.java
@@ -26,7 +26,6 @@
 import android.net.Uri;
 import android.os.Handler;
 import android.os.SystemClock;
-import android.os.UserHandle;
 import android.provider.Settings;
 import android.text.format.DateFormat;
 import android.util.AttributeSet;
@@ -496,28 +495,12 @@
         filter.addAction(Intent.ACTION_TIME_CHANGED);
         filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
 
-        // OK, this is gross but needed. This class is supported by the
-        // remote views mechanism and as a part of that the remote views
-        // can be inflated by a context for another user without the app
-        // having interact users permission - just for loading resources.
-        // For example, when adding widgets from a user profile to the
-        // home screen. Therefore, we register the receiver as the user
-        // the app is running as not the one the context is for.
-        getContext().registerReceiverAsUser(mIntentReceiver, android.os.Process.myUserHandle(),
-                filter, null, getHandler());
+        getContext().registerReceiver(mIntentReceiver, filter, null, getHandler());
     }
 
     private void registerObserver() {
         final ContentResolver resolver = getContext().getContentResolver();
-        // OK, this is gross but needed. This class is supported by the
-        // remote views mechanism and as a part of that the remote views
-        // can be inflated by a context for another user without the app
-        // having interact users permission - just for loading resources.
-        // For example, when adding widgets from a user profile to the
-        // home screen. Therefore, we register the content observer
-        // as the user the app is running as not the one the context is for.
-        resolver.registerContentObserver(Settings.System.CONTENT_URI, true,
-                mFormatChangeObserver, UserHandle.myUserId());
+        resolver.registerContentObserver(Settings.System.CONTENT_URI, true, mFormatChangeObserver);
     }
 
     private void unregisterReceiver() {
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index 188a3e9..3e1b674 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -41,7 +41,6 @@
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.os.SystemClock;
-import android.os.UserHandle;
 import android.provider.Settings;
 import android.text.BoringLayout;
 import android.text.DynamicLayout;
@@ -8338,15 +8337,8 @@
      * to speak passwords.
      */
     private boolean shouldSpeakPasswordsForAccessibility() {
-        // OK, this is gross but needed. This class is supported by the
-        // remote views mechanism and as a part of that the remote views
-        // can be inflated by a context for another user without the app
-        // having interact users permission - just for loading resources.
-        // For example, when adding widgets from a user profile to the
-        // home screen. Therefore, we access settings as user the app is
-        // running as not the one the context is for.
-        return (Settings.Secure.getIntForUser(mContext.getContentResolver(),
-                Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD, 0, UserHandle.myUserId()) == 1);
+        return (Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD, 0) == 1);
     }
 
     @Override