Fixing widget id restore broadcast.

Ignoring thw broadcast if its not for the main widget host, or if
the Launcher DB is already in use. Launcher already handles missing
widget-Id map broadcast, by binding a new widgetId at runtime.

Bug: 63389280
Change-Id: Iaa9774d6d7adde3711cba9615328020e2b2e66aa
This commit is contained in:
Sunny Goyal
2017-07-12 12:09:37 -07:00
parent a9f4bffbfc
commit f5b4b80972
2 changed files with 22 additions and 2 deletions
@@ -9,10 +9,12 @@ import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Handler;
import android.support.annotation.WorkerThread;
import android.util.Log;
import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.model.LoaderTask;
import com.android.launcher3.provider.RestoreDbTask;
import com.android.launcher3.util.ContentWriter;
public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
@@ -22,6 +24,12 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
if (AppWidgetManager.ACTION_APPWIDGET_HOST_RESTORED.equals(intent.getAction())) {
int hostId = intent.getIntExtra(AppWidgetManager.EXTRA_HOST_ID, 0);
Log.d(TAG, "Widget ID map received for host:" + hostId);
if (hostId != Launcher.APPWIDGET_HOST_ID) {
return;
}
final int[] oldIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_OLD_IDS);
final int[] newIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
if (oldIds.length == newIds.length) {
@@ -42,11 +50,23 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
/**
* Updates the app widgets whose id has changed during the restore process.
*/
@WorkerThread
static void restoreAppWidgetIds(Context context, PendingResult asyncResult,
int[] oldWidgetIds, int[] newWidgetIds) {
AppWidgetHost appWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
if (!RestoreDbTask.isPending(context)) {
// Someone has already gone through our DB once, probably LoaderTask. Skip any further
// modifications of the DB.
Log.e(TAG, "Skipping widget ID remap as DB already in use");
for (int widgetId : newWidgetIds) {
Log.d(TAG, "Deleting widgetId: " + widgetId);
appWidgetHost.deleteAppWidgetId(widgetId);
}
asyncResult.finish();
return;
}
final ContentResolver cr = context.getContentResolver();
final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
AppWidgetHost appWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
for (int i = 0; i < oldWidgetIds.length; i++) {
Log.i(TAG, "Widget state restore id " + oldWidgetIds[i] + " => " + newWidgetIds[i]);
@@ -134,7 +134,7 @@ public class RestoreDbTask {
}
public static void setPending(Context context, boolean isPending) {
FileLog.d(TAG, "Restore data received through full backup");
FileLog.d(TAG, "Restore data received through full backup " + isPending);
Utilities.getPrefs(context).edit().putBoolean(RESTORE_TASK_PENDING, isPending).commit();
}
}