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

Commit 2eb9cc48 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Fixing AppWidgetsRestoredReceiver updating model on UI thread

Bug: 34203314
Change-Id: If2c149f353109350e45e581a654956bec78bce96
parent 3e9be43b
Loading
Loading
Loading
Loading
+15 −5
Original line number Original line Diff line number Diff line
@@ -8,6 +8,7 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
import android.database.Cursor;
import android.database.Cursor;
import android.os.Handler;
import android.util.Log;
import android.util.Log;


import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.LauncherSettings.Favorites;
@@ -18,12 +19,19 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
    private static final String TAG = "AWRestoredReceiver";
    private static final String TAG = "AWRestoredReceiver";


    @Override
    @Override
    public void onReceive(Context context, Intent intent) {
    public void onReceive(final Context context, Intent intent) {
        if (AppWidgetManager.ACTION_APPWIDGET_HOST_RESTORED.equals(intent.getAction())) {
        if (AppWidgetManager.ACTION_APPWIDGET_HOST_RESTORED.equals(intent.getAction())) {
            int[] oldIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_OLD_IDS);
            final int[] oldIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_OLD_IDS);
            int[] newIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
            final int[] newIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
            if (oldIds.length == newIds.length) {
            if (oldIds.length == newIds.length) {
                restoreAppWidgetIds(context, oldIds, newIds);
                final PendingResult asyncResult = goAsync();
                new Handler(LauncherModel.getWorkerLooper())
                        .postAtFrontOfQueue(new Runnable() {
                            @Override
                            public void run() {
                                restoreAppWidgetIds(context, asyncResult, oldIds, newIds);
                            }
                        });
            } else {
            } else {
                Log.e(TAG, "Invalid host restored received");
                Log.e(TAG, "Invalid host restored received");
            }
            }
@@ -33,7 +41,8 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
    /**
    /**
     * Updates the app widgets whose id has changed during the restore process.
     * Updates the app widgets whose id has changed during the restore process.
     */
     */
    static void restoreAppWidgetIds(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
    static void restoreAppWidgetIds(Context context, PendingResult asyncResult,
            int[] oldWidgetIds, int[] newWidgetIds) {
        final ContentResolver cr = context.getContentResolver();
        final ContentResolver cr = context.getContentResolver();
        final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
        final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
        AppWidgetHost appWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
        AppWidgetHost appWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
@@ -76,5 +85,6 @@ public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
        if (app != null) {
        if (app != null) {
            app.reloadWorkspace();
            app.reloadWorkspace();
        }
        }
        asyncResult.finish();
    }
    }
}
}