blob: a87c4465856affb301d930fdc59e34b34600d5cd [file] [log] [blame]
Sunny Goyal4179e9b2017-03-08 14:25:09 -08001/*
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
17package com.android.launcher3;
18
Sunny Goyal3e443a22017-04-10 10:49:01 -070019import android.annotation.TargetApi;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080020import android.content.BroadcastReceiver;
Jon Mirandac84168d2019-08-19 13:32:09 -070021import android.content.ComponentName;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080022import android.content.Context;
23import android.content.Intent;
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070024import android.content.SharedPreferences;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080025import android.content.pm.LauncherActivityInfo;
26import android.content.pm.PackageInstaller;
27import android.content.pm.PackageInstaller.SessionInfo;
Sunny Goyal3e443a22017-04-10 10:49:01 -070028import android.content.pm.PackageManager;
29import android.content.pm.ResolveInfo;
30import android.database.Cursor;
Jon Mirandac84168d2019-08-19 13:32:09 -070031import android.graphics.Bitmap;
Sunny Goyal3e443a22017-04-10 10:49:01 -070032import android.net.Uri;
33import android.os.AsyncTask;
34import android.os.Build;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080035import android.os.UserHandle;
Sunny Goyal3e443a22017-04-10 10:49:01 -070036import android.provider.Settings;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080037import android.text.TextUtils;
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070038import android.util.Log;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080039
40import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070041import com.android.launcher3.util.Executors;
Jon Mirandac84168d2019-08-19 13:32:09 -070042import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080043
44import java.util.List;
45
Jon Mirandac84168d2019-08-19 13:32:09 -070046import static com.android.launcher3.compat.PackageInstallerCompat.getUserHandle;
47
Sunny Goyal4179e9b2017-03-08 14:25:09 -080048/**
49 * BroadcastReceiver to handle session commit intent.
50 */
Sunny Goyal3e443a22017-04-10 10:49:01 -070051@TargetApi(Build.VERSION_CODES.O)
Sunny Goyal4179e9b2017-03-08 14:25:09 -080052public class SessionCommitReceiver extends BroadcastReceiver {
53
Sunny Goyal3e443a22017-04-10 10:49:01 -070054 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 Goyal0d7f19a2017-03-24 09:07:05 -070059
Sunny Goyal4179e9b2017-03-08 14:25:09 -080060 // Preference key for automatically adding icon to homescreen.
61 public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home";
Sunny Goyal3e443a22017-04-10 10:49:01 -070062 public static final String ADD_ICON_PREFERENCE_INITIALIZED_KEY =
63 "pref_add_icon_to_home_initialized";
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070064
Sunny Goyal4179e9b2017-03-08 14:25:09 -080065 @Override
66 public void onReceive(Context context, Intent intent) {
Hyunyoung Songe24cb632017-09-11 11:18:03 -070067 if (!isEnabled(context) || !Utilities.ATLEAST_OREO) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -080068 // 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 Miranda7e048872019-11-08 13:02:52 -080074 if (!PackageInstaller.ACTION_SESSION_COMMITTED.equals(intent.getAction())
75 || info == null || user == null) {
76 // Invalid intent.
77 return;
78 }
Sunny Goyal3e443a22017-04-10 10:49:01 -070079
Jon Miranda7e048872019-11-08 13:02:52 -080080 PackageInstallerCompat packageInstallerCompat = PackageInstallerCompat.getInstance(context);
Jon Mirandac84168d2019-08-19 13:32:09 -070081 if (TextUtils.isEmpty(info.getAppPackageName())
82 || info.getInstallReason() != PackageManager.INSTALL_REASON_USER
83 || packageInstallerCompat.promiseIconAddedForId(info.getSessionId())) {
84 packageInstallerCompat.removePromiseIconId(info.getSessionId());
Tony Mak1b6826c2018-02-27 15:06:12 +000085 return;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080086 }
87
Sunny Goyala474a9b2017-05-04 16:47:11 -070088 queueAppIconAddition(context, info.getAppPackageName(), user);
89 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -080090
Jon Mirandac84168d2019-08-19 13:32:09 -070091 public static void queuePromiseAppIconAddition(Context context, SessionInfo sessionInfo) {
92 String packageName = sessionInfo.getAppPackageName();
93 List<LauncherActivityInfo> activities = LauncherAppsCompat.getInstance(context)
94 .getActivityList(packageName, getUserHandle(sessionInfo));
95 if (activities == null || activities.isEmpty()) {
96 // Ensure application isn't already installed.
97 queueAppIconAddition(context, packageName, sessionInfo.getAppLabel(),
98 sessionInfo.getAppIcon(), getUserHandle(sessionInfo));
99 }
100 }
101
Sunny Goyala474a9b2017-05-04 16:47:11 -0700102 public static void queueAppIconAddition(Context context, String packageName, UserHandle user) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800103 List<LauncherActivityInfo> activities = LauncherAppsCompat.getInstance(context)
Sunny Goyala474a9b2017-05-04 16:47:11 -0700104 .getActivityList(packageName, user);
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800105 if (activities == null || activities.isEmpty()) {
106 // no activity found
107 return;
108 }
Jon Mirandac84168d2019-08-19 13:32:09 -0700109 queueAppIconAddition(context, packageName, activities.get(0).getLabel(), null, user);
110 }
111
112 private static void queueAppIconAddition(Context context, String packageName,
113 CharSequence label, Bitmap icon, UserHandle user) {
114 Intent data = new Intent();
115 data.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setComponent(
116 new ComponentName(packageName, "")).setPackage(packageName));
117 data.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
118 data.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
119
120 InstallShortcutReceiver.queueApplication(data, user, context);
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800121 }
122
123 public static boolean isEnabled(Context context) {
124 return Utilities.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true);
125 }
Sunny Goyal3e443a22017-04-10 10:49:01 -0700126
127 public static void applyDefaultUserPrefs(final Context context) {
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700128 if (!Utilities.ATLEAST_OREO) {
Sunny Goyal3e443a22017-04-10 10:49:01 -0700129 return;
130 }
131 SharedPreferences prefs = Utilities.getPrefs(context);
132 if (prefs.getAll().isEmpty()) {
133 // This logic assumes that the code is the first thing that is executed (before any
134 // shared preference is written).
135 // TODO: Move this logic to DB upgrade once we have proper support for db downgrade
136 // If it is a fresh start, just apply the default value. We use prefs.isEmpty() to infer
137 // a fresh start as put preferences always contain some values corresponding to current
138 // grid.
139 prefs.edit().putBoolean(ADD_ICON_PREFERENCE_KEY, true).apply();
140 } else if (!prefs.contains(ADD_ICON_PREFERENCE_INITIALIZED_KEY)) {
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700141 new PrefInitTask(context).executeOnExecutor(Executors.THREAD_POOL_EXECUTOR);
Sunny Goyal3e443a22017-04-10 10:49:01 -0700142 }
143 }
144
145 private static class PrefInitTask extends AsyncTask<Void, Void, Void> {
146 private final Context mContext;
147
148 PrefInitTask(Context context) {
149 mContext = context;
150 }
151
152 @Override
153 protected Void doInBackground(Void... voids) {
154 boolean addIconToHomeScreenEnabled = readValueFromMarketApp();
155 Utilities.getPrefs(mContext).edit()
156 .putBoolean(ADD_ICON_PREFERENCE_KEY, addIconToHomeScreenEnabled)
157 .putBoolean(ADD_ICON_PREFERENCE_INITIALIZED_KEY, true)
158 .apply();
159 return null;
160 }
161
162 public boolean readValueFromMarketApp() {
163 // Get the marget package
164 ResolveInfo ri = mContext.getPackageManager().resolveActivity(
165 new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET),
166 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_SYSTEM_ONLY);
167 if (ri == null) {
168 return true;
169 }
170
171 Cursor c = null;
172 try {
173 c = mContext.getContentResolver().query(
174 Uri.parse("content://" + ri.activityInfo.packageName
175 + MARKER_PROVIDER_PREFIX),
176 null, null, null, null);
177 if (c.moveToNext()) {
178 return c.getInt(c.getColumnIndexOrThrow(Settings.NameValueTable.VALUE)) != 0;
179 }
180 } catch (Exception e) {
181 Log.d(TAG, "Error reading add to homescreen preference", e);
182 } finally {
183 if (c != null) {
184 c.close();
185 }
186 }
187 return true;
188 }
189 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800190}