From 3e90b4c7aea374411f23e908b1da46331db64b59 Mon Sep 17 00:00:00 2001 From: Jon Miranda Date: Tue, 30 Jul 2019 09:03:12 -0700 Subject: [PATCH] Fix bug where work profile widgets are restored with main profile provider. * Platform bug causes work profile widgets to be restored with main profile provider. * If we skip restore the widgets under the work profile during restore, then launcher will re-create the widget but with the correct profile provider. * Needs to be fixed in platform, but this launcher side change will work for now. Bug: 135926478 Change-Id: I80300c83e82cea9da84fdcdf7ad03188769b6cac --- .../launcher3/AppWidgetsRestoredReceiver.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/com/android/launcher3/AppWidgetsRestoredReceiver.java b/src/com/android/launcher3/AppWidgetsRestoredReceiver.java index d9491419fd..e3ef5d64e7 100644 --- a/src/com/android/launcher3/AppWidgetsRestoredReceiver.java +++ b/src/com/android/launcher3/AppWidgetsRestoredReceiver.java @@ -11,6 +11,7 @@ import android.database.Cursor; import android.util.Log; import com.android.launcher3.LauncherSettings.Favorites; +import com.android.launcher3.compat.UserManagerCompat; import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.model.LoaderTask; import com.android.launcher3.provider.RestoreDbTask; @@ -18,6 +19,8 @@ import com.android.launcher3.util.ContentWriter; import androidx.annotation.WorkerThread; +import static android.os.Process.myUserHandle; + public class AppWidgetsRestoredReceiver extends BroadcastReceiver { private static final String TAG = "AWRestoredReceiver"; @@ -77,9 +80,14 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver { state = LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY; } - String[] widgetIdParams = new String[] { Integer.toString(oldWidgetIds[i]) }; + // b/135926478: Work profile widget restore is broken in platform. This forces us to + // recreate the widget during loading with the correct host provider. + long mainProfileId = UserManagerCompat.getInstance(context) + .getSerialNumberForUser(myUserHandle()); + String oldWidgetId = Integer.toString(oldWidgetIds[i]); int result = new ContentWriter(context, new ContentWriter.CommitParams( - "appWidgetId=? and (restored & 1) = 1", widgetIdParams)) + "appWidgetId=? and (restored & 1) = 1 and profileId=?", + new String[] { oldWidgetId, Long.toString(mainProfileId) })) .put(LauncherSettings.Favorites.APPWIDGET_ID, newWidgetIds[i]) .put(LauncherSettings.Favorites.RESTORED, state) .commit(); @@ -87,7 +95,7 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver { if (result == 0) { Cursor cursor = cr.query(Favorites.CONTENT_URI, new String[] {Favorites.APPWIDGET_ID}, - "appWidgetId=?", widgetIdParams, null); + "appWidgetId=?", new String[] { oldWidgetId }, null); try { if (!cursor.moveToFirst()) { // The widget no long exists.