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;
|
|
|
|
|
|
|
|
|
|
import com.android.launcher3.LauncherSettings.Favorites;
|
2019-07-30 09:03:12 -07:00
|
|
|
import com.android.launcher3.compat.UserManagerCompat;
|
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;
|
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;
|
2014-06-30 14:15:31 -07:00
|
|
|
|
2018-08-14 15:21:45 -07:00
|
|
|
import androidx.annotation.WorkerThread;
|
|
|
|
|
|
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.
|
|
|
|
|
long mainProfileId = UserManagerCompat.getInstance(context)
|
|
|
|
|
.getSerialNumberForUser(myUserHandle());
|
|
|
|
|
String oldWidgetId = Integer.toString(oldWidgetIds[i]);
|
2017-01-05 21:50:27 -08:00
|
|
|
int result = new ContentWriter(context, new ContentWriter.CommitParams(
|
2019-07-30 09:03:12 -07:00
|
|
|
"appWidgetId=? and (restored & 1) = 1 and profileId=?",
|
|
|
|
|
new String[] { oldWidgetId, Long.toString(mainProfileId) }))
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
}
|