blob: f0bae021d5199c2ae494dde77dc721b39345f380 [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 Goyal73b5a272019-12-09 14:55:56 -080019import static com.android.launcher3.pm.InstallSessionHelper.getUserHandle;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070020
Sunny Goyal3e443a22017-04-10 10:49:01 -070021import android.annotation.TargetApi;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080022import android.content.BroadcastReceiver;
Jon Mirandac84168d2019-08-19 13:32:09 -070023import android.content.ComponentName;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080024import android.content.Context;
25import android.content.Intent;
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070026import android.content.SharedPreferences;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080027import android.content.pm.LauncherActivityInfo;
Sunny Goyale7b00122019-10-02 16:13:34 -070028import android.content.pm.LauncherApps;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080029import android.content.pm.PackageInstaller;
30import android.content.pm.PackageInstaller.SessionInfo;
Sunny Goyal3e443a22017-04-10 10:49:01 -070031import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
33import android.database.Cursor;
Jon Mirandac84168d2019-08-19 13:32:09 -070034import android.graphics.Bitmap;
Sunny Goyal3e443a22017-04-10 10:49:01 -070035import android.net.Uri;
36import android.os.AsyncTask;
37import android.os.Build;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080038import android.os.UserHandle;
Sunny Goyal3e443a22017-04-10 10:49:01 -070039import android.provider.Settings;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080040import android.text.TextUtils;
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070041import android.util.Log;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080042
Sunny Goyal73b5a272019-12-09 14:55:56 -080043import com.android.launcher3.pm.InstallSessionHelper;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070044import com.android.launcher3.util.Executors;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080045
46import java.util.List;
47
48/**
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
Sunny Goyal73b5a272019-12-09 14:55:56 -080080 InstallSessionHelper packageInstallerCompat = InstallSessionHelper.INSTANCE.get(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();
Sunny Goyale7b00122019-10-02 16:13:34 -070093 if (context.getSystemService(LauncherApps.class)
94 .getActivityList(packageName, getUserHandle(sessionInfo)).isEmpty()) {
Jon Mirandac84168d2019-08-19 13:32:09 -070095 // Ensure application isn't already installed.
96 queueAppIconAddition(context, packageName, sessionInfo.getAppLabel(),
97 sessionInfo.getAppIcon(), getUserHandle(sessionInfo));
98 }
99 }
100
Sunny Goyala474a9b2017-05-04 16:47:11 -0700101 public static void queueAppIconAddition(Context context, String packageName, UserHandle user) {
Sunny Goyale7b00122019-10-02 16:13:34 -0700102 List<LauncherActivityInfo> activities = context.getSystemService(LauncherApps.class)
Sunny Goyala474a9b2017-05-04 16:47:11 -0700103 .getActivityList(packageName, user);
Sunny Goyale7b00122019-10-02 16:13:34 -0700104 if (activities.isEmpty()) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800105 // no activity found
106 return;
107 }
Jon Mirandac84168d2019-08-19 13:32:09 -0700108 queueAppIconAddition(context, packageName, activities.get(0).getLabel(), null, user);
109 }
110
111 private static void queueAppIconAddition(Context context, String packageName,
112 CharSequence label, Bitmap icon, UserHandle user) {
113 Intent data = new Intent();
114 data.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setComponent(
115 new ComponentName(packageName, "")).setPackage(packageName));
116 data.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
117 data.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
118
119 InstallShortcutReceiver.queueApplication(data, user, context);
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800120 }
121
122 public static boolean isEnabled(Context context) {
123 return Utilities.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true);
124 }
Sunny Goyal3e443a22017-04-10 10:49:01 -0700125
126 public static void applyDefaultUserPrefs(final Context context) {
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700127 if (!Utilities.ATLEAST_OREO) {
Sunny Goyal3e443a22017-04-10 10:49:01 -0700128 return;
129 }
130 SharedPreferences prefs = Utilities.getPrefs(context);
131 if (prefs.getAll().isEmpty()) {
132 // This logic assumes that the code is the first thing that is executed (before any
133 // shared preference is written).
134 // TODO: Move this logic to DB upgrade once we have proper support for db downgrade
135 // If it is a fresh start, just apply the default value. We use prefs.isEmpty() to infer
136 // a fresh start as put preferences always contain some values corresponding to current
137 // grid.
138 prefs.edit().putBoolean(ADD_ICON_PREFERENCE_KEY, true).apply();
139 } else if (!prefs.contains(ADD_ICON_PREFERENCE_INITIALIZED_KEY)) {
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700140 new PrefInitTask(context).executeOnExecutor(Executors.THREAD_POOL_EXECUTOR);
Sunny Goyal3e443a22017-04-10 10:49:01 -0700141 }
142 }
143
144 private static class PrefInitTask extends AsyncTask<Void, Void, Void> {
145 private final Context mContext;
146
147 PrefInitTask(Context context) {
148 mContext = context;
149 }
150
151 @Override
152 protected Void doInBackground(Void... voids) {
153 boolean addIconToHomeScreenEnabled = readValueFromMarketApp();
154 Utilities.getPrefs(mContext).edit()
155 .putBoolean(ADD_ICON_PREFERENCE_KEY, addIconToHomeScreenEnabled)
156 .putBoolean(ADD_ICON_PREFERENCE_INITIALIZED_KEY, true)
157 .apply();
158 return null;
159 }
160
161 public boolean readValueFromMarketApp() {
162 // Get the marget package
163 ResolveInfo ri = mContext.getPackageManager().resolveActivity(
164 new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET),
165 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_SYSTEM_ONLY);
166 if (ri == null) {
167 return true;
168 }
169
170 Cursor c = null;
171 try {
172 c = mContext.getContentResolver().query(
173 Uri.parse("content://" + ri.activityInfo.packageName
174 + MARKER_PROVIDER_PREFIX),
175 null, null, null, null);
176 if (c.moveToNext()) {
177 return c.getInt(c.getColumnIndexOrThrow(Settings.NameValueTable.VALUE)) != 0;
178 }
179 } catch (Exception e) {
180 Log.d(TAG, "Error reading add to homescreen preference", e);
181 } finally {
182 if (c != null) {
183 c.close();
184 }
185 }
186 return true;
187 }
188 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800189}