2014-06-30 14:15:31 -07:00
|
|
|
package com.android.launcher3;
|
|
|
|
|
|
2019-09-11 16:51:50 -07:00
|
|
|
import static android.os.Process.myUserHandle;
|
|
|
|
|
|
2014-06-30 14:15:31 -07:00
|
|
|
import android.appwidget.AppWidgetHost;
|
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
|
|
|
import android.appwidget.AppWidgetProviderInfo;
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
2019-12-10 12:19:13 -08:00
|
|
|
import androidx.annotation.WorkerThread;
|
|
|
|
|
|
2014-06-30 14:15:31 -07:00
|
|
|
import com.android.launcher3.LauncherSettings.Favorites;
|
2017-06-06 10:46:59 -07:00
|
|
|
import com.android.launcher3.model.LoaderTask;
|
2019-09-11 16:51:50 -07:00
|
|
|
import com.android.launcher3.model.WidgetsModel;
|
2020-04-06 15:11:17 -07:00
|
|
|
import com.android.launcher3.model.data.LauncherAppWidgetInfo;
|
2019-12-10 12:19:13 -08:00
|
|
|
import com.android.launcher3.pm.UserCache;
|
2017-07-12 12:09:37 -07:00
|
|
|
import com.android.launcher3.provider.RestoreDbTask;
|
2017-01-05 21:50:27 -08:00
|
|
|
import com.android.launcher3.util.ContentWriter;
|
2021-02-22 14:03:44 +00:00
|
|
|
import com.android.launcher3.widget.LauncherAppWidgetHost;
|
2014-06-30 14:15:31 -07:00
|
|
|
|
|
|
|
|
public class AppWidgetsRestoredReceiver extends BroadcastReceiver {
|
|
|
|
|
|
2016-03-18 16:25:05 -07:00
|
|
|
private static final String TAG = "AWRestoredReceiver";
|
2014-06-30 14:15:31 -07:00
|
|
|
|
|
|
|
|
@Override
|
2017-01-10 15:29:18 -08:00
|
|
|
public void onReceive(final Context context, Intent intent) {
|
2014-06-30 14:15:31 -07:00
|
|
|
if (AppWidgetManager.ACTION_APPWIDGET_HOST_RESTORED.equals(intent.getAction())) {
|
2017-07-12 12:09:37 -07:00
|
|
|
int hostId = intent.getIntExtra(AppWidgetManager.EXTRA_HOST_ID, 0);
|
|
|
|
|
Log.d(TAG, "Widget ID map received for host:" + hostId);
|
2017-07-03 13:50:52 -07:00
|
|
|
if (hostId != LauncherAppWidgetHost.APPWIDGET_HOST_ID) {
|
2017-07-12 12:09:37 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-10 15:29:18 -08:00
|
|
|
final int[] oldIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_OLD_IDS);
|
|
|
|
|
final int[] newIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
|
2019-05-13 14:57:50 -07:00
|
|
|
if (oldIds != null && newIds != null && oldIds.length == newIds.length) {
|
|
|
|
|
RestoreDbTask.setRestoredAppWidgetIds(context, oldIds, newIds);
|
2014-06-30 14:15:31 -07:00
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Invalid host restored received");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the app widgets whose id has changed during the restore process.
|
|
|
|
|
*/
|
2017-07-12 12:09:37 -07:00
|
|
|
@WorkerThread
|
2019-05-13 14:57:50 -07:00
|
|
|
public static void restoreAppWidgetIds(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
|
2017-07-03 13:50:52 -07:00
|
|
|
AppWidgetHost appWidgetHost = new LauncherAppWidgetHost(context);
|
2019-09-11 16:51:50 -07:00
|
|
|
if (WidgetsModel.GO_DISABLE_WIDGETS) {
|
2017-07-03 13:50:52 -07:00
|
|
|
Log.e(TAG, "Skipping widget ID remap as widgets not supported");
|
|
|
|
|
appWidgetHost.deleteHost();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-07-12 12:09:37 -07:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-06-30 14:15:31 -07:00
|
|
|
final ContentResolver cr = context.getContentResolver();
|
|
|
|
|
final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < oldWidgetIds.length; i++) {
|
|
|
|
|
Log.i(TAG, "Widget state restore id " + oldWidgetIds[i] + " => " + newWidgetIds[i]);
|
|
|
|
|
|
|
|
|
|
final AppWidgetProviderInfo provider = widgets.getAppWidgetInfo(newWidgetIds[i]);
|
2014-07-23 13:58:07 -07:00
|
|
|
final int state;
|
2017-06-06 10:46:59 -07:00
|
|
|
if (LoaderTask.isValidProvider(provider)) {
|
2015-08-12 15:12:16 -07:00
|
|
|
// This will ensure that we show 'Click to setup' UI if required.
|
|
|
|
|
state = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
|
2014-07-23 13:58:07 -07:00
|
|
|
} else {
|
|
|
|
|
state = LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
|
|
|
|
|
}
|
2014-06-30 14:15:31 -07:00
|
|
|
|
2019-07-30 09:03:12 -07:00
|
|
|
// b/135926478: Work profile widget restore is broken in platform. This forces us to
|
|
|
|
|
// recreate the widget during loading with the correct host provider.
|
2019-12-10 12:19:13 -08:00
|
|
|
long mainProfileId = UserCache.INSTANCE.get(context)
|
2019-07-30 09:03:12 -07:00
|
|
|
.getSerialNumberForUser(myUserHandle());
|
|
|
|
|
String oldWidgetId = Integer.toString(oldWidgetIds[i]);
|
2020-10-28 16:30:42 -07:00
|
|
|
final String where = "appWidgetId=? and (restored & 1) = 1 and profileId=?";
|
|
|
|
|
final String[] args = new String[] { oldWidgetId, Long.toString(mainProfileId) };
|
|
|
|
|
int result = new ContentWriter(context, new ContentWriter.CommitParams(where, args))
|
2017-01-05 21:50:27 -08:00
|
|
|
.put(LauncherSettings.Favorites.APPWIDGET_ID, newWidgetIds[i])
|
|
|
|
|
.put(LauncherSettings.Favorites.RESTORED, state)
|
|
|
|
|
.commit();
|
2014-06-30 14:15:31 -07:00
|
|
|
if (result == 0) {
|
|
|
|
|
Cursor cursor = cr.query(Favorites.CONTENT_URI,
|
|
|
|
|
new String[] {Favorites.APPWIDGET_ID},
|
2019-07-30 09:03:12 -07:00
|
|
|
"appWidgetId=?", new String[] { oldWidgetId }, null);
|
2014-06-30 14:15:31 -07:00
|
|
|
try {
|
|
|
|
|
if (!cursor.moveToFirst()) {
|
|
|
|
|
// The widget no long exists.
|
2016-03-18 16:25:05 -07:00
|
|
|
appWidgetHost.deleteAppWidgetId(newWidgetIds[i]);
|
2014-06-30 14:15:31 -07:00
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
cursor.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-28 16:30:42 -07:00
|
|
|
// attempt to update widget id in backup table as well
|
2020-11-20 18:18:52 -08:00
|
|
|
new ContentWriter(context, ContentWriter.CommitParams.backupCommitParams(
|
|
|
|
|
"appWidgetId=? and profileId=?", args))
|
2020-10-28 16:30:42 -07:00
|
|
|
.put(LauncherSettings.Favorites.APPWIDGET_ID, newWidgetIds[i])
|
|
|
|
|
.put(LauncherSettings.Favorites.RESTORED, state)
|
|
|
|
|
.commit();
|
2014-06-30 14:15:31 -07:00
|
|
|
}
|
2015-03-30 11:11:46 -07:00
|
|
|
|
|
|
|
|
LauncherAppState app = LauncherAppState.getInstanceNoCreate();
|
|
|
|
|
if (app != null) {
|
2017-02-17 11:22:34 -08:00
|
|
|
app.getModel().forceReload();
|
2015-03-30 11:11:46 -07:00
|
|
|
}
|
2014-06-30 14:15:31 -07:00
|
|
|
}
|
|
|
|
|
}
|