Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame^] | 1 | package com.android.launcher3; |
| 2 | |
| 3 | import android.appwidget.AppWidgetHost; |
| 4 | import android.appwidget.AppWidgetManager; |
| 5 | import android.appwidget.AppWidgetProviderInfo; |
| 6 | import android.content.BroadcastReceiver; |
| 7 | import android.content.ContentResolver; |
| 8 | import android.content.ContentValues; |
| 9 | import android.content.Context; |
| 10 | import android.content.Intent; |
| 11 | import android.database.Cursor; |
| 12 | import android.os.AsyncTask; |
| 13 | import android.util.Log; |
| 14 | |
| 15 | import com.android.launcher3.LauncherSettings.Favorites; |
| 16 | |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.List; |
| 19 | |
| 20 | public class AppWidgetsRestoredReceiver extends BroadcastReceiver { |
| 21 | |
| 22 | private static final String TAG = "AppWidgetsRestoredReceiver"; |
| 23 | |
| 24 | @Override |
| 25 | public void onReceive(Context context, Intent intent) { |
| 26 | if (AppWidgetManager.ACTION_APPWIDGET_HOST_RESTORED.equals(intent.getAction())) { |
| 27 | int[] oldIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_OLD_IDS); |
| 28 | int[] newIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS); |
| 29 | if (oldIds.length == newIds.length) { |
| 30 | restoreAppWidgetIds(context, oldIds, newIds); |
| 31 | } else { |
| 32 | Log.e(TAG, "Invalid host restored received"); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Updates the app widgets whose id has changed during the restore process. |
| 39 | */ |
| 40 | static void restoreAppWidgetIds(Context context, int[] oldWidgetIds, int[] newWidgetIds) { |
| 41 | final ContentResolver cr = context.getContentResolver(); |
| 42 | final List<Integer> idsToRemove = new ArrayList<>(); |
| 43 | final AppWidgetManager widgets = AppWidgetManager.getInstance(context); |
| 44 | |
| 45 | for (int i = 0; i < oldWidgetIds.length; i++) { |
| 46 | Log.i(TAG, "Widget state restore id " + oldWidgetIds[i] + " => " + newWidgetIds[i]); |
| 47 | |
| 48 | final AppWidgetProviderInfo provider = widgets.getAppWidgetInfo(newWidgetIds[i]); |
| 49 | |
| 50 | ContentValues values = new ContentValues(); |
| 51 | values.put(LauncherSettings.Favorites.APPWIDGET_ID, newWidgetIds[i]); |
| 52 | values.put(LauncherSettings.Favorites.RESTORED, LauncherModel.isValidProvider(provider) |
| 53 | ? LauncherAppWidgetInfo.RESTORE_COMPLETED |
| 54 | : LauncherAppWidgetInfo.RESTORE_PROVIDER_PENDING); |
| 55 | |
| 56 | String[] widgetIdParams = new String[] { Integer.toString(oldWidgetIds[i]) }; |
| 57 | |
| 58 | int result = cr.update(Favorites.CONTENT_URI, values, |
| 59 | "appWidgetId=? and restored=1", widgetIdParams); |
| 60 | if (result == 0) { |
| 61 | Cursor cursor = cr.query(Favorites.CONTENT_URI, |
| 62 | new String[] {Favorites.APPWIDGET_ID}, |
| 63 | "appWidgetId=?", widgetIdParams, null); |
| 64 | try { |
| 65 | if (!cursor.moveToFirst()) { |
| 66 | // The widget no long exists. |
| 67 | idsToRemove.add(newWidgetIds[i]); |
| 68 | } |
| 69 | } finally { |
| 70 | cursor.close(); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | // Unregister the widget IDs which are not present on the workspace. This could happen |
| 75 | // when a widget place holder is removed from workspace, before this method is called. |
| 76 | if (!idsToRemove.isEmpty()) { |
| 77 | final AppWidgetHost appWidgetHost = |
| 78 | new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID); |
| 79 | new AsyncTask<Void, Void, Void>() { |
| 80 | public Void doInBackground(Void ... args) { |
| 81 | for (Integer id : idsToRemove) { |
| 82 | appWidgetHost.deleteAppWidgetId(id); |
| 83 | Log.e(TAG, "Widget no longer present, appWidgetId=" + id); |
| 84 | } |
| 85 | return null; |
| 86 | } |
| 87 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null); |
| 88 | } |
| 89 | } |
| 90 | } |