Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.launcher3; |
| 18 | |
Sunny Goyal | 045b4fa | 2019-09-20 12:51:37 -0700 | [diff] [blame] | 19 | import static com.android.launcher3.pm.PackageInstallerCompat.getUserHandle; |
| 20 | |
Sunny Goyal | 3e443a2 | 2017-04-10 10:49:01 -0700 | [diff] [blame] | 21 | import android.annotation.TargetApi; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
Jon Miranda | c84168d | 2019-08-19 13:32:09 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Sunny Goyal | 0d7f19a | 2017-03-24 09:07:05 -0700 | [diff] [blame] | 26 | import android.content.SharedPreferences; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 27 | import android.content.pm.LauncherActivityInfo; |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame^] | 28 | import android.content.pm.LauncherApps; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 29 | import android.content.pm.PackageInstaller; |
| 30 | import android.content.pm.PackageInstaller.SessionInfo; |
Sunny Goyal | 3e443a2 | 2017-04-10 10:49:01 -0700 | [diff] [blame] | 31 | import android.content.pm.PackageManager; |
| 32 | import android.content.pm.ResolveInfo; |
| 33 | import android.database.Cursor; |
Jon Miranda | c84168d | 2019-08-19 13:32:09 -0700 | [diff] [blame] | 34 | import android.graphics.Bitmap; |
Sunny Goyal | 3e443a2 | 2017-04-10 10:49:01 -0700 | [diff] [blame] | 35 | import android.net.Uri; |
| 36 | import android.os.AsyncTask; |
| 37 | import android.os.Build; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 38 | import android.os.UserHandle; |
Sunny Goyal | 3e443a2 | 2017-04-10 10:49:01 -0700 | [diff] [blame] | 39 | import android.provider.Settings; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 40 | import android.text.TextUtils; |
Sunny Goyal | 0d7f19a | 2017-03-24 09:07:05 -0700 | [diff] [blame] | 41 | import android.util.Log; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 42 | |
Sunny Goyal | 045b4fa | 2019-09-20 12:51:37 -0700 | [diff] [blame] | 43 | import com.android.launcher3.pm.PackageInstallerCompat; |
Sunny Goyal | 6fe3eec | 2019-08-15 14:53:41 -0700 | [diff] [blame] | 44 | import com.android.launcher3.util.Executors; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 45 | |
| 46 | import java.util.List; |
| 47 | |
| 48 | /** |
| 49 | * BroadcastReceiver to handle session commit intent. |
| 50 | */ |
Sunny Goyal | 3e443a2 | 2017-04-10 10:49:01 -0700 | [diff] [blame] | 51 | @TargetApi(Build.VERSION_CODES.O) |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 52 | public class SessionCommitReceiver extends BroadcastReceiver { |
| 53 | |
Sunny Goyal | 3e443a2 | 2017-04-10 10:49:01 -0700 | [diff] [blame] | 54 | private static final String TAG = "SessionCommitReceiver"; |
| 55 | |
| 56 | // The content provider for the add to home screen setting. It should be of the format: |
| 57 | // <package name>.addtohomescreen |
| 58 | private static final String MARKER_PROVIDER_PREFIX = ".addtohomescreen"; |
Sunny Goyal | 0d7f19a | 2017-03-24 09:07:05 -0700 | [diff] [blame] | 59 | |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 60 | // Preference key for automatically adding icon to homescreen. |
| 61 | public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home"; |
Sunny Goyal | 3e443a2 | 2017-04-10 10:49:01 -0700 | [diff] [blame] | 62 | public static final String ADD_ICON_PREFERENCE_INITIALIZED_KEY = |
| 63 | "pref_add_icon_to_home_initialized"; |
Sunny Goyal | 0d7f19a | 2017-03-24 09:07:05 -0700 | [diff] [blame] | 64 | |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 65 | @Override |
| 66 | public void onReceive(Context context, Intent intent) { |
Hyunyoung Song | e24cb63 | 2017-09-11 11:18:03 -0700 | [diff] [blame] | 67 | if (!isEnabled(context) || !Utilities.ATLEAST_OREO) { |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 68 | // User has decided to not add icons on homescreen. |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION); |
| 73 | UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER); |
Jon Miranda | c84168d | 2019-08-19 13:32:09 -0700 | [diff] [blame] | 74 | PackageInstallerCompat packageInstallerCompat = PackageInstallerCompat.getInstance(context); |
Sunny Goyal | 3e443a2 | 2017-04-10 10:49:01 -0700 | [diff] [blame] | 75 | |
Jon Miranda | c84168d | 2019-08-19 13:32:09 -0700 | [diff] [blame] | 76 | if (TextUtils.isEmpty(info.getAppPackageName()) |
| 77 | || info.getInstallReason() != PackageManager.INSTALL_REASON_USER |
| 78 | || packageInstallerCompat.promiseIconAddedForId(info.getSessionId())) { |
| 79 | packageInstallerCompat.removePromiseIconId(info.getSessionId()); |
Tony Mak | 1b6826c | 2018-02-27 15:06:12 +0000 | [diff] [blame] | 80 | return; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Sunny Goyal | a474a9b | 2017-05-04 16:47:11 -0700 | [diff] [blame] | 83 | queueAppIconAddition(context, info.getAppPackageName(), user); |
| 84 | } |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 85 | |
Jon Miranda | c84168d | 2019-08-19 13:32:09 -0700 | [diff] [blame] | 86 | public static void queuePromiseAppIconAddition(Context context, SessionInfo sessionInfo) { |
| 87 | String packageName = sessionInfo.getAppPackageName(); |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame^] | 88 | if (context.getSystemService(LauncherApps.class) |
| 89 | .getActivityList(packageName, getUserHandle(sessionInfo)).isEmpty()) { |
Jon Miranda | c84168d | 2019-08-19 13:32:09 -0700 | [diff] [blame] | 90 | // Ensure application isn't already installed. |
| 91 | queueAppIconAddition(context, packageName, sessionInfo.getAppLabel(), |
| 92 | sessionInfo.getAppIcon(), getUserHandle(sessionInfo)); |
| 93 | } |
| 94 | } |
| 95 | |
Sunny Goyal | a474a9b | 2017-05-04 16:47:11 -0700 | [diff] [blame] | 96 | public static void queueAppIconAddition(Context context, String packageName, UserHandle user) { |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame^] | 97 | List<LauncherActivityInfo> activities = context.getSystemService(LauncherApps.class) |
Sunny Goyal | a474a9b | 2017-05-04 16:47:11 -0700 | [diff] [blame] | 98 | .getActivityList(packageName, user); |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame^] | 99 | if (activities.isEmpty()) { |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 100 | // no activity found |
| 101 | return; |
| 102 | } |
Jon Miranda | c84168d | 2019-08-19 13:32:09 -0700 | [diff] [blame] | 103 | queueAppIconAddition(context, packageName, activities.get(0).getLabel(), null, user); |
| 104 | } |
| 105 | |
| 106 | private static void queueAppIconAddition(Context context, String packageName, |
| 107 | CharSequence label, Bitmap icon, UserHandle user) { |
| 108 | Intent data = new Intent(); |
| 109 | data.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setComponent( |
| 110 | new ComponentName(packageName, "")).setPackage(packageName)); |
| 111 | data.putExtra(Intent.EXTRA_SHORTCUT_NAME, label); |
| 112 | data.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon); |
| 113 | |
| 114 | InstallShortcutReceiver.queueApplication(data, user, context); |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | public static boolean isEnabled(Context context) { |
| 118 | return Utilities.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true); |
| 119 | } |
Sunny Goyal | 3e443a2 | 2017-04-10 10:49:01 -0700 | [diff] [blame] | 120 | |
| 121 | public static void applyDefaultUserPrefs(final Context context) { |
Hyunyoung Song | e24cb63 | 2017-09-11 11:18:03 -0700 | [diff] [blame] | 122 | if (!Utilities.ATLEAST_OREO) { |
Sunny Goyal | 3e443a2 | 2017-04-10 10:49:01 -0700 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | SharedPreferences prefs = Utilities.getPrefs(context); |
| 126 | if (prefs.getAll().isEmpty()) { |
| 127 | // This logic assumes that the code is the first thing that is executed (before any |
| 128 | // shared preference is written). |
| 129 | // TODO: Move this logic to DB upgrade once we have proper support for db downgrade |
| 130 | // If it is a fresh start, just apply the default value. We use prefs.isEmpty() to infer |
| 131 | // a fresh start as put preferences always contain some values corresponding to current |
| 132 | // grid. |
| 133 | prefs.edit().putBoolean(ADD_ICON_PREFERENCE_KEY, true).apply(); |
| 134 | } else if (!prefs.contains(ADD_ICON_PREFERENCE_INITIALIZED_KEY)) { |
Sunny Goyal | 6fe3eec | 2019-08-15 14:53:41 -0700 | [diff] [blame] | 135 | new PrefInitTask(context).executeOnExecutor(Executors.THREAD_POOL_EXECUTOR); |
Sunny Goyal | 3e443a2 | 2017-04-10 10:49:01 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | private static class PrefInitTask extends AsyncTask<Void, Void, Void> { |
| 140 | private final Context mContext; |
| 141 | |
| 142 | PrefInitTask(Context context) { |
| 143 | mContext = context; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | protected Void doInBackground(Void... voids) { |
| 148 | boolean addIconToHomeScreenEnabled = readValueFromMarketApp(); |
| 149 | Utilities.getPrefs(mContext).edit() |
| 150 | .putBoolean(ADD_ICON_PREFERENCE_KEY, addIconToHomeScreenEnabled) |
| 151 | .putBoolean(ADD_ICON_PREFERENCE_INITIALIZED_KEY, true) |
| 152 | .apply(); |
| 153 | return null; |
| 154 | } |
| 155 | |
| 156 | public boolean readValueFromMarketApp() { |
| 157 | // Get the marget package |
| 158 | ResolveInfo ri = mContext.getPackageManager().resolveActivity( |
| 159 | new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET), |
| 160 | PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_SYSTEM_ONLY); |
| 161 | if (ri == null) { |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | Cursor c = null; |
| 166 | try { |
| 167 | c = mContext.getContentResolver().query( |
| 168 | Uri.parse("content://" + ri.activityInfo.packageName |
| 169 | + MARKER_PROVIDER_PREFIX), |
| 170 | null, null, null, null); |
| 171 | if (c.moveToNext()) { |
| 172 | return c.getInt(c.getColumnIndexOrThrow(Settings.NameValueTable.VALUE)) != 0; |
| 173 | } |
| 174 | } catch (Exception e) { |
| 175 | Log.d(TAG, "Error reading add to homescreen preference", e); |
| 176 | } finally { |
| 177 | if (c != null) { |
| 178 | c.close(); |
| 179 | } |
| 180 | } |
| 181 | return true; |
| 182 | } |
| 183 | } |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 184 | } |