mirror of
https://github.com/LawnchairLauncher/lawnchair.git
synced 2026-02-20 03:08:19 +00:00
1. Create feature flag for new backup & restore flow. 2. For each restore session (install reason is restore), if its creation time is newer than the one we have in SharedPreference, we update the entry and restores favorite table from backup. 3. The restore operation is debounced so that when multiple restore session is created within a small amount of time, only the last invocation will get executed. Bug: 141472083 Change-Id: I7b5b63ec28741ba2b02ccfd13f591c961362ba36 Test: 1. apply on master, build & flash on physical device. 2. factory reset the device. 3. go through SuW, perform restore, exit without adding work profile. 4. settings -> account -> add work profile account. 5. finish work profile setup, verify work profiles is restored as well.
191 lines
7.7 KiB
Java
191 lines
7.7 KiB
Java
/*
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package com.android.launcher3;
|
|
|
|
import static com.android.launcher3.pm.InstallSessionHelper.getUserHandle;
|
|
|
|
import android.annotation.TargetApi;
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.ComponentName;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.content.pm.LauncherActivityInfo;
|
|
import android.content.pm.LauncherApps;
|
|
import android.content.pm.PackageInstaller;
|
|
import android.content.pm.PackageInstaller.SessionInfo;
|
|
import android.content.pm.PackageManager;
|
|
import android.content.pm.ResolveInfo;
|
|
import android.database.Cursor;
|
|
import android.graphics.Bitmap;
|
|
import android.net.Uri;
|
|
import android.os.AsyncTask;
|
|
import android.os.Build;
|
|
import android.os.UserHandle;
|
|
import android.provider.Settings;
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
|
|
import com.android.launcher3.pm.InstallSessionHelper;
|
|
import com.android.launcher3.util.Executors;
|
|
|
|
import java.util.List;
|
|
|
|
/**
|
|
* BroadcastReceiver to handle session commit intent.
|
|
*/
|
|
@TargetApi(Build.VERSION_CODES.O)
|
|
public class SessionCommitReceiver extends BroadcastReceiver {
|
|
|
|
private static final String TAG = "SessionCommitReceiver";
|
|
|
|
// The content provider for the add to home screen setting. It should be of the format:
|
|
// <package name>.addtohomescreen
|
|
private static final String MARKER_PROVIDER_PREFIX = ".addtohomescreen";
|
|
|
|
// Preference key for automatically adding icon to homescreen.
|
|
public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home";
|
|
public static final String ADD_ICON_PREFERENCE_INITIALIZED_KEY =
|
|
"pref_add_icon_to_home_initialized";
|
|
|
|
@Override
|
|
public void onReceive(Context context, Intent intent) {
|
|
if (!isEnabled(context) || !Utilities.ATLEAST_OREO) {
|
|
// User has decided to not add icons on homescreen.
|
|
return;
|
|
}
|
|
|
|
SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION);
|
|
UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
|
|
if (!PackageInstaller.ACTION_SESSION_COMMITTED.equals(intent.getAction())
|
|
|| info == null || user == null) {
|
|
// Invalid intent.
|
|
return;
|
|
}
|
|
|
|
InstallSessionHelper packageInstallerCompat = InstallSessionHelper.INSTANCE.get(context);
|
|
packageInstallerCompat.restoreDbIfApplicable(info);
|
|
if (TextUtils.isEmpty(info.getAppPackageName())
|
|
|| info.getInstallReason() != PackageManager.INSTALL_REASON_USER
|
|
|| packageInstallerCompat.promiseIconAddedForId(info.getSessionId())) {
|
|
packageInstallerCompat.removePromiseIconId(info.getSessionId());
|
|
return;
|
|
}
|
|
|
|
queueAppIconAddition(context, info.getAppPackageName(), user);
|
|
}
|
|
|
|
public static void queuePromiseAppIconAddition(Context context, SessionInfo sessionInfo) {
|
|
String packageName = sessionInfo.getAppPackageName();
|
|
if (context.getSystemService(LauncherApps.class)
|
|
.getActivityList(packageName, getUserHandle(sessionInfo)).isEmpty()) {
|
|
// Ensure application isn't already installed.
|
|
queueAppIconAddition(context, packageName, sessionInfo.getAppLabel(),
|
|
sessionInfo.getAppIcon(), getUserHandle(sessionInfo));
|
|
}
|
|
}
|
|
|
|
public static void queueAppIconAddition(Context context, String packageName, UserHandle user) {
|
|
List<LauncherActivityInfo> activities = context.getSystemService(LauncherApps.class)
|
|
.getActivityList(packageName, user);
|
|
if (activities.isEmpty()) {
|
|
// no activity found
|
|
return;
|
|
}
|
|
queueAppIconAddition(context, packageName, activities.get(0).getLabel(), null, user);
|
|
}
|
|
|
|
private static void queueAppIconAddition(Context context, String packageName,
|
|
CharSequence label, Bitmap icon, UserHandle user) {
|
|
Intent data = new Intent();
|
|
data.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setComponent(
|
|
new ComponentName(packageName, "")).setPackage(packageName));
|
|
data.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
|
|
data.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
|
|
|
|
InstallShortcutReceiver.queueApplication(data, user, context);
|
|
}
|
|
|
|
public static boolean isEnabled(Context context) {
|
|
return Utilities.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true);
|
|
}
|
|
|
|
public static void applyDefaultUserPrefs(final Context context) {
|
|
if (!Utilities.ATLEAST_OREO) {
|
|
return;
|
|
}
|
|
SharedPreferences prefs = Utilities.getPrefs(context);
|
|
if (prefs.getAll().isEmpty()) {
|
|
// This logic assumes that the code is the first thing that is executed (before any
|
|
// shared preference is written).
|
|
// TODO: Move this logic to DB upgrade once we have proper support for db downgrade
|
|
// If it is a fresh start, just apply the default value. We use prefs.isEmpty() to infer
|
|
// a fresh start as put preferences always contain some values corresponding to current
|
|
// grid.
|
|
prefs.edit().putBoolean(ADD_ICON_PREFERENCE_KEY, true).apply();
|
|
} else if (!prefs.contains(ADD_ICON_PREFERENCE_INITIALIZED_KEY)) {
|
|
new PrefInitTask(context).executeOnExecutor(Executors.THREAD_POOL_EXECUTOR);
|
|
}
|
|
}
|
|
|
|
private static class PrefInitTask extends AsyncTask<Void, Void, Void> {
|
|
private final Context mContext;
|
|
|
|
PrefInitTask(Context context) {
|
|
mContext = context;
|
|
}
|
|
|
|
@Override
|
|
protected Void doInBackground(Void... voids) {
|
|
boolean addIconToHomeScreenEnabled = readValueFromMarketApp();
|
|
Utilities.getPrefs(mContext).edit()
|
|
.putBoolean(ADD_ICON_PREFERENCE_KEY, addIconToHomeScreenEnabled)
|
|
.putBoolean(ADD_ICON_PREFERENCE_INITIALIZED_KEY, true)
|
|
.apply();
|
|
return null;
|
|
}
|
|
|
|
public boolean readValueFromMarketApp() {
|
|
// Get the marget package
|
|
ResolveInfo ri = mContext.getPackageManager().resolveActivity(
|
|
new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET),
|
|
PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_SYSTEM_ONLY);
|
|
if (ri == null) {
|
|
return true;
|
|
}
|
|
|
|
Cursor c = null;
|
|
try {
|
|
c = mContext.getContentResolver().query(
|
|
Uri.parse("content://" + ri.activityInfo.packageName
|
|
+ MARKER_PROVIDER_PREFIX),
|
|
null, null, null, null);
|
|
if (c.moveToNext()) {
|
|
return c.getInt(c.getColumnIndexOrThrow(Settings.NameValueTable.VALUE)) != 0;
|
|
}
|
|
} catch (Exception e) {
|
|
Log.d(TAG, "Error reading add to homescreen preference", e);
|
|
} finally {
|
|
if (c != null) {
|
|
c.close();
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|