Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a4490181 authored by Kenny Guy's avatar Kenny Guy Committed by Android (Google) Code Review
Browse files

Merge "Ensure all RemoteViews use myUserId rather than context." into lmp-dev

parents 8e315233 879ebec4
Loading
Loading
Loading
Loading
+16 −27
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.annotation.SdkConstant;
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;
@@ -815,13 +816,6 @@ public class Notification implements Parcelable
     */
    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.
@@ -1801,10 +1795,12 @@ public class Notification implements Parcelable
                = "android.rebuild.hudViewActionCount";

        /**
         * The package name of the context used to create the notification via a Builder.
         * The ApplicationInfo of the package that created the notification, used to create
         * a context to rebuild the notification via a Builder.
         * @hide
         */
        private static final String EXTRA_REBUILD_CONTEXT_PACKAGE =
                "android.rebuild.contextPackage";
        private static final String EXTRA_REBUILD_CONTEXT_APPLICATION_INFO =
                "android.rebuild.applicationInfo";

        // Whether to enable stripping (at post time) & rebuilding (at listener receive time) of
        // memory intensive resources.
@@ -1854,11 +1850,6 @@ public class Notification implements Parcelable
        private ArrayList<String> mPeople;
        private int mColor = COLOR_DEFAULT;

        /**
         * The user that built the notification originally.
         */
        private int mOriginatingUserId;

        /**
         * Contains extras related to rebuilding during the build phase.
         */
@@ -2579,7 +2570,7 @@ public class Notification implements Parcelable
            // 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(mOriginatingUserId), 0);
            return userManager.getBadgeForUser(new UserHandle(mContext.getUserId()), 0);
        }

        private Bitmap getProfileBadge() {
@@ -2658,8 +2649,7 @@ public class Notification implements Parcelable
         * @param hasProgress whether the progress bar should be shown and set
         */
        private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
            RemoteViews contentView = new BuilderRemoteViews(mContext.getPackageName(),
                    mOriginatingUserId, resId);
            RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);

            resetStandardTemplate(contentView);

@@ -3051,8 +3041,8 @@ public class Notification implements Parcelable
         */
        public void populateExtras(Bundle extras) {
            // Store original information used in the construction of this object
            extras.putInt(EXTRA_ORIGINATING_USERID, mOriginatingUserId);
            extras.putString(EXTRA_REBUILD_CONTEXT_PACKAGE, mContext.getPackageName());
            extras.putParcelable(EXTRA_REBUILD_CONTEXT_APPLICATION_INFO,
                    mContext.getApplicationInfo());
            extras.putCharSequence(EXTRA_TITLE, mContentTitle);
            extras.putCharSequence(EXTRA_TEXT, mContentText);
            extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
@@ -3140,13 +3130,14 @@ public class Notification implements Parcelable
            extras.remove(EXTRA_NEEDS_REBUILD);

            // Re-create notification context so we can access app resources.
            String packageName = extras.getString(EXTRA_REBUILD_CONTEXT_PACKAGE);
            ApplicationInfo applicationInfo = extras.getParcelable(
                    EXTRA_REBUILD_CONTEXT_APPLICATION_INFO);
            Context builderContext;
            try {
                builderContext = context.createPackageContext(packageName,
                builderContext = context.createApplicationContext(applicationInfo,
                        Context.CONTEXT_RESTRICTED);
            } catch (NameNotFoundException e) {
                Log.e(TAG, "Package name " + packageName + " not found");
                Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
                builderContext = context;  // try with our context
            }

@@ -3281,7 +3272,6 @@ public class Notification implements Parcelable

            // 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);
@@ -3314,7 +3304,6 @@ public class Notification implements Parcelable
         * object.
         */
        public Notification build() {
            mOriginatingUserId = mContext.getUserId();
            mHasThreeLines = hasThreeLines();

            Notification n = buildUnstyled();
@@ -4814,8 +4803,8 @@ public class Notification implements Parcelable
            super(parcel);
        }

        public BuilderRemoteViews(String packageName, int userId, int layoutId) {
            super(packageName, userId, layoutId);
        public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
            super(appInfo, layoutId);
        }

        @Override
+17 −4
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.text.format;

import android.content.Context;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
@@ -128,8 +129,14 @@ public class DateFormat {
     * @return true if 24 hour time format is selected, false otherwise.
     */
    public static boolean is24HourFormat(Context context) {
        String value = Settings.System.getString(context.getContentResolver(),
                Settings.System.TIME_12_24);
        // 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());

        if (value == null) {
            Locale locale = context.getResources().getConfiguration().locale;
@@ -227,8 +234,14 @@ public class DateFormat {
     * @return the {@link java.text.DateFormat} object that properly formats the date.
     */
    public static java.text.DateFormat getDateFormat(Context context) {
        String value = Settings.System.getString(context.getContentResolver(),
                Settings.System.DATE_FORMAT);
        // 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());

        return getDateFormatForSetting(context, value);
    }
+22 −4
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.util.Log;
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;
@@ -190,8 +191,15 @@ public class DateTimeView extends TextView {
    }

    private DateFormat getDateFormat() {
        String format = Settings.System.getString(getContext().getContentResolver(),
                Settings.System.DATE_FORMAT);
        // 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());
        if (format == null || "".equals(format)) {
            return DateFormat.getDateInstance(DateFormat.SHORT);
        } else {
@@ -212,10 +220,20 @@ public class DateTimeView extends TextView {
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        context.registerReceiver(mBroadcastReceiver, filter);

        // 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);

        Uri uri = Settings.System.getUriFor(Settings.System.DATE_FORMAT);
        context.getContentResolver().registerContentObserver(uri, true, mContentObserver);
        context.getContentResolver().registerContentObserver(uri, true, mContentObserver,
                UserHandle.myUserId());
    }

    private void unregisterReceivers() {
+3 −1
Original line number Diff line number Diff line
@@ -1653,8 +1653,10 @@ public class RemoteViews implements Parcelable, Filter {
     *
     * @param application The application whose content is shown by the views.
     * @param layoutId The id of the layout resource.
     *
     * @hide
     */
    private RemoteViews(ApplicationInfo application, int layoutId) {
    protected RemoteViews(ApplicationInfo application, int layoutId) {
        mApplication = application;
        mLayoutId = layoutId;
        mBitmapCache = new BitmapCache();
+19 −2
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.database.ContentObserver;
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;
@@ -495,12 +496,28 @@ public class TextClock extends TextView {
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);

        getContext().registerReceiver(mIntentReceiver, filter, null, getHandler());
        // 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());
    }

    private void registerObserver() {
        final ContentResolver resolver = getContext().getContentResolver();
        resolver.registerContentObserver(Settings.System.CONTENT_URI, true, mFormatChangeObserver);
        // 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());
    }

    private void unregisterReceiver() {
Loading