blob: 6853bf69414026794c6dc967d650336ab075559b [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 Mirandac84168d2019-08-19 13:32:09 -070074 PackageInstallerCompat packageInstallerCompat = PackageInstallerCompat.getInstance(context);
Sunny Goyal3e443a22017-04-10 10:49:01 -070075
Jon Mirandac84168d2019-08-19 13:32:09 -070076 if (TextUtils.isEmpty(info.getAppPackageName())
77 || info.getInstallReason() != PackageManager.INSTALL_REASON_USER
78 || packageInstallerCompat.promiseIconAddedForId(info.getSessionId())) {
79 packageInstallerCompat.removePromiseIconId(info.getSessionId());
Tony Mak1b6826c2018-02-27 15:06:12 +000080 return;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080081 }
82
Sunny Goyala474a9b2017-05-04 16:47:11 -070083 queueAppIconAddition(context, info.getAppPackageName(), user);
84 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -080085
Jon Mirandac84168d2019-08-19 13:32:09 -070086 public static void queuePromiseAppIconAddition(Context context, SessionInfo sessionInfo) {
87 String packageName = sessionInfo.getAppPackageName();
88 List<LauncherActivityInfo> activities = LauncherAppsCompat.getInstance(context)
89 .getActivityList(packageName, getUserHandle(sessionInfo));
90 if (activities == null || activities.isEmpty()) {
91 // Ensure application isn't already installed.
92 queueAppIconAddition(context, packageName, sessionInfo.getAppLabel(),
93 sessionInfo.getAppIcon(), getUserHandle(sessionInfo));
94 }
95 }
96
Sunny Goyala474a9b2017-05-04 16:47:11 -070097 public static void queueAppIconAddition(Context context, String packageName, UserHandle user) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -080098 List<LauncherActivityInfo> activities = LauncherAppsCompat.getInstance(context)
Sunny Goyala474a9b2017-05-04 16:47:11 -070099 .getActivityList(packageName, user);
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800100 if (activities == null || activities.isEmpty()) {
101 // no activity found
102 return;
103 }
Jon Mirandac84168d2019-08-19 13:32:09 -0700104 queueAppIconAddition(context, packageName, activities.get(0).getLabel(), null, user);
105 }
106
107 private static void queueAppIconAddition(Context context, String packageName,
108 CharSequence label, Bitmap icon, UserHandle user) {
109 Intent data = new Intent();
110 data.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setComponent(
111 new ComponentName(packageName, "")).setPackage(packageName));
112 data.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
113 data.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
114
115 InstallShortcutReceiver.queueApplication(data, user, context);
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800116 }
117
118 public static boolean isEnabled(Context context) {
119 return Utilities.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true);
120 }
Sunny Goyal3e443a22017-04-10 10:49:01 -0700121
122 public static void applyDefaultUserPrefs(final Context context) {
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700123 if (!Utilities.ATLEAST_OREO) {
Sunny Goyal3e443a22017-04-10 10:49:01 -0700124 return;
125 }
126 SharedPreferences prefs = Utilities.getPrefs(context);
127 if (prefs.getAll().isEmpty()) {
128 // This logic assumes that the code is the first thing that is executed (before any
129 // shared preference is written).
130 // TODO: Move this logic to DB upgrade once we have proper support for db downgrade
131 // If it is a fresh start, just apply the default value. We use prefs.isEmpty() to infer
132 // a fresh start as put preferences always contain some values corresponding to current
133 // grid.
134 prefs.edit().putBoolean(ADD_ICON_PREFERENCE_KEY, true).apply();
135 } else if (!prefs.contains(ADD_ICON_PREFERENCE_INITIALIZED_KEY)) {
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700136 new PrefInitTask(context).executeOnExecutor(Executors.THREAD_POOL_EXECUTOR);
Sunny Goyal3e443a22017-04-10 10:49:01 -0700137 }
138 }
139
140 private static class PrefInitTask extends AsyncTask<Void, Void, Void> {
141 private final Context mContext;
142
143 PrefInitTask(Context context) {
144 mContext = context;
145 }
146
147 @Override
148 protected Void doInBackground(Void... voids) {
149 boolean addIconToHomeScreenEnabled = readValueFromMarketApp();
150 Utilities.getPrefs(mContext).edit()
151 .putBoolean(ADD_ICON_PREFERENCE_KEY, addIconToHomeScreenEnabled)
152 .putBoolean(ADD_ICON_PREFERENCE_INITIALIZED_KEY, true)
153 .apply();
154 return null;
155 }
156
157 public boolean readValueFromMarketApp() {
158 // Get the marget package
159 ResolveInfo ri = mContext.getPackageManager().resolveActivity(
160 new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET),
161 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_SYSTEM_ONLY);
162 if (ri == null) {
163 return true;
164 }
165
166 Cursor c = null;
167 try {
168 c = mContext.getContentResolver().query(
169 Uri.parse("content://" + ri.activityInfo.packageName
170 + MARKER_PROVIDER_PREFIX),
171 null, null, null, null);
172 if (c.moveToNext()) {
173 return c.getInt(c.getColumnIndexOrThrow(Settings.NameValueTable.VALUE)) != 0;
174 }
175 } catch (Exception e) {
176 Log.d(TAG, "Error reading add to homescreen preference", e);
177 } finally {
178 if (c != null) {
179 c.close();
180 }
181 }
182 return true;
183 }
184 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800185}