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

Commit c566b43d authored by Amith Yamasani's avatar Amith Yamasani
Browse files

Fix crosstalk between users for widgets hosted in lockscreen

This was initially about the Clock widget crashing repeatedly on some
devices with multiple users. Turned out that there were race conditions
when switching users that could result in remote views of one user calling
back to the RemoteViewsAdapter in keyguard that in turn sent an incorrect widget id
to a different user's widget, resulting in a crash.

Since KeyguardHostView is instantiated in the same process for different users,
it needs to carry a user identity to pass along to AppWidgetService so that
remote views services were bound to the correct user and callbacks were attached and
detached properly.

Added some aidl calls that take the userId to do the binding properly. A more
complete fix might be needed in the future so that all calls from Keyguard carry
the user id.

Also, there was a problem in comparing host uid for secondary users, since Settings
for a secondary user has a different uid than keyguard. Not an issue on single-user
systems. Changed the host.uid comparison to accomodate for the secondary user.

Bug: 7450247
Change-Id: Idbc36e3c60023cac74174f6cb7f2b2130dd3052c
parent 20b76b9f
Loading
Loading
Loading
Loading
+28 −2
Original line number Diff line number Diff line
@@ -154,6 +154,15 @@ public class AppWidgetHost {
     * becomes visible, i.e. from onStart() in your Activity.
     */
    public void startListening() {
        startListeningAsUser(UserHandle.myUserId());
    }

    /**
     * Start receiving onAppWidgetChanged calls for your AppWidgets.  Call this when your activity
     * becomes visible, i.e. from onStart() in your Activity.
     * @hide
     */
    public void startListeningAsUser(int userId) {
        int[] updatedIds;
        ArrayList<RemoteViews> updatedViews = new ArrayList<RemoteViews>();

@@ -161,7 +170,8 @@ public class AppWidgetHost {
            if (mPackageName == null) {
                mPackageName = mContext.getPackageName();
            }
            updatedIds = sService.startListening(mCallbacks, mPackageName, mHostId, updatedViews);
            updatedIds = sService.startListeningAsUser(
                    mCallbacks, mPackageName, mHostId, updatedViews, userId);
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
@@ -179,11 +189,27 @@ public class AppWidgetHost {
     */
    public void stopListening() {
        try {
            sService.stopListening(mHostId);
            sService.stopListeningAsUser(mHostId, UserHandle.myUserId());
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
    }

    /**
     * Stop receiving onAppWidgetChanged calls for your AppWidgets.  Call this when your activity is
     * no longer visible, i.e. from onStop() in your Activity.
     * @hide
     */
    public void stopListeningAsUser(int userId) {
        try {
            sService.stopListeningAsUser(mHostId, userId);
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
        // Also clear the views
        clearViews();
    }

    /**
+9 −4
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.RemoteViews;
@@ -749,11 +750,14 @@ public class AppWidgetManager {
     * @param intent        The intent of the service which will be providing the data to the
     *                      RemoteViewsAdapter.
     * @param connection    The callback interface to be notified when a connection is made or lost.
     * @param userHandle    The user to bind to.
     * @hide
     */
    public void bindRemoteViewsService(int appWidgetId, Intent intent, IBinder connection) {
    public void bindRemoteViewsService(int appWidgetId, Intent intent, IBinder connection,
            UserHandle userHandle) {
        try {
            sService.bindRemoteViewsService(appWidgetId, intent, connection);
            sService.bindRemoteViewsService(appWidgetId, intent, connection,
                    userHandle.getIdentifier());
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
@@ -769,11 +773,12 @@ public class AppWidgetManager {
     * @param appWidgetId   The AppWidget instance for which to bind the RemoteViewsService.
     * @param intent        The intent of the service which will be providing the data to the
     *                      RemoteViewsAdapter.
     * @param userHandle    The user to unbind from.
     * @hide
     */
    public void unbindRemoteViewsService(int appWidgetId, Intent intent) {
    public void unbindRemoteViewsService(int appWidgetId, Intent intent, UserHandle userHandle) {
        try {
            sService.unbindRemoteViewsService(appWidgetId, intent);
            sService.unbindRemoteViewsService(appWidgetId, intent, userHandle.getIdentifier());
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
+28 −2
Original line number Diff line number Diff line
@@ -29,7 +29,9 @@ import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
@@ -40,6 +42,7 @@ import android.widget.RemoteViews.OnClickHandler;

import com.android.internal.widget.IRemoteViewsAdapterConnection;
import com.android.internal.widget.IRemoteViewsFactory;
import com.android.internal.widget.LockPatternUtils;

/**
 * An adapter to a RemoteViewsService which fetches and caches RemoteViews
@@ -106,6 +109,8 @@ public class RemoteViewsAdapter extends BaseAdapter implements Handler.Callback
    // construction (happens when we have a cached FixedSizeRemoteViewsCache).
    private boolean mDataReady = false;

    int mUserId;

    /**
     * An interface for the RemoteAdapter to notify other classes when adapters
     * are actually connected to/disconnected from their actual services.
@@ -146,8 +151,16 @@ public class RemoteViewsAdapter extends BaseAdapter implements Handler.Callback
        public synchronized void bind(Context context, int appWidgetId, Intent intent) {
            if (!mIsConnecting) {
                try {
                    RemoteViewsAdapter adapter;
                    final AppWidgetManager mgr = AppWidgetManager.getInstance(context);
                    mgr.bindRemoteViewsService(appWidgetId, intent, asBinder());
                    if (Process.myUid() == Process.SYSTEM_UID
                            && (adapter = mAdapter.get()) != null) {
                        mgr.bindRemoteViewsService(appWidgetId, intent, asBinder(),
                                new UserHandle(adapter.mUserId));
                    } else {
                        mgr.bindRemoteViewsService(appWidgetId, intent, asBinder(),
                                Process.myUserHandle());
                    }
                    mIsConnecting = true;
                } catch (Exception e) {
                    Log.e("RemoteViewsAdapterServiceConnection", "bind(): " + e.getMessage());
@@ -159,8 +172,15 @@ public class RemoteViewsAdapter extends BaseAdapter implements Handler.Callback

        public synchronized void unbind(Context context, int appWidgetId, Intent intent) {
            try {
                RemoteViewsAdapter adapter;
                final AppWidgetManager mgr = AppWidgetManager.getInstance(context);
                mgr.unbindRemoteViewsService(appWidgetId, intent);
                if (Process.myUid() == Process.SYSTEM_UID
                        && (adapter = mAdapter.get()) != null) {
                    mgr.unbindRemoteViewsService(appWidgetId, intent,
                            new UserHandle(adapter.mUserId));
                } else {
                    mgr.unbindRemoteViewsService(appWidgetId, intent, Process.myUserHandle());
                }
                mIsConnecting = false;
            } catch (Exception e) {
                Log.e("RemoteViewsAdapterServiceConnection", "unbind(): " + e.getMessage());
@@ -761,6 +781,12 @@ public class RemoteViewsAdapter extends BaseAdapter implements Handler.Callback
        }
        mRequestedViews = new RemoteViewsFrameLayoutRefSet();

        if (Process.myUid() == Process.SYSTEM_UID) {
            mUserId = new LockPatternUtils(context).getCurrentUser();
        } else {
            mUserId = UserHandle.myUserId();
        }

        // Strip the previously injected app widget id from service intent
        if (intent.hasExtra(RemoteViews.EXTRA_REMOTEADAPTER_APPWIDGET_ID)) {
            intent.removeExtra(RemoteViews.EXTRA_REMOTEADAPTER_APPWIDGET_ID);
+5 −2
Original line number Diff line number Diff line
@@ -32,7 +32,10 @@ interface IAppWidgetService {
    //
    int[] startListening(IAppWidgetHost host, String packageName, int hostId,
            out List<RemoteViews> updatedViews);
    int[] startListeningAsUser(IAppWidgetHost host, String packageName, int hostId,
            out List<RemoteViews> updatedViews, int userId);
    void stopListening(int hostId);
    void stopListeningAsUser(int hostId, int userId);
    int allocateAppWidgetId(String packageName, int hostId);
    void deleteAppWidgetId(int appWidgetId);
    void deleteHost(int hostId);
@@ -56,8 +59,8 @@ interface IAppWidgetService {
    void bindAppWidgetId(int appWidgetId, in ComponentName provider, in Bundle options);
    boolean bindAppWidgetIdIfAllowed(
            in String packageName, int appWidgetId, in ComponentName provider, in Bundle options);
    void bindRemoteViewsService(int appWidgetId, in Intent intent, in IBinder connection);
    void unbindRemoteViewsService(int appWidgetId, in Intent intent);
    void bindRemoteViewsService(int appWidgetId, in Intent intent, in IBinder connection, int userId);
    void unbindRemoteViewsService(int appWidgetId, in Intent intent, int userId);
    int[] getAppWidgetIds(in ComponentName provider);

}
+5 −2
Original line number Diff line number Diff line
@@ -101,6 +101,8 @@ public class KeyguardHostView extends KeyguardViewBase {
    private boolean mSafeModeEnabled;

    private boolean mUserSetupCompleted;
    // User for whom this host view was created
    private int mUserId;

    /*package*/ interface TransportCallback {
        void onListenerDetached();
@@ -127,6 +129,7 @@ public class KeyguardHostView extends KeyguardViewBase {
    public KeyguardHostView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mLockPatternUtils = new LockPatternUtils(context);
        mUserId = mLockPatternUtils.getCurrentUser();
        mAppWidgetHost = new AppWidgetHost(
                context, APPWIDGET_HOST_ID, mOnClickHandler, Looper.myLooper());
        cleanupAppWidgetIds();
@@ -338,14 +341,14 @@ public class KeyguardHostView extends KeyguardViewBase {
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mAppWidgetHost.startListening();
        mAppWidgetHost.startListeningAsUser(mUserId);
        KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mUpdateMonitorCallbacks);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mAppWidgetHost.stopListening();
        mAppWidgetHost.stopListeningAsUser(mUserId);
        KeyguardUpdateMonitor.getInstance(mContext).removeCallback(mUpdateMonitorCallbacks);
    }

Loading