blob: 89f0a3d286b4542d50eeff9aed0c327cb2233c39 [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);
Pinyao Tingad5f2402019-12-10 14:56:34 -080081 packageInstallerCompat.restoreDbIfApplicable(info);
Jon Mirandac84168d2019-08-19 13:32:09 -070082 if (TextUtils.isEmpty(info.getAppPackageName())
83 || info.getInstallReason() != PackageManager.INSTALL_REASON_USER
84 || packageInstallerCompat.promiseIconAddedForId(info.getSessionId())) {
85 packageInstallerCompat.removePromiseIconId(info.getSessionId());
Tony Mak1b6826c2018-02-27 15:06:12 +000086 return;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080087 }
88
Sunny Goyala474a9b2017-05-04 16:47:11 -070089 queueAppIconAddition(context, info.getAppPackageName(), user);
90 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -080091
Jon Mirandac84168d2019-08-19 13:32:09 -070092 public static void queuePromiseAppIconAddition(Context context, SessionInfo sessionInfo) {
93 String packageName = sessionInfo.getAppPackageName();
Sunny Goyale7b00122019-10-02 16:13:34 -070094 if (context.getSystemService(LauncherApps.class)
95 .getActivityList(packageName, getUserHandle(sessionInfo)).isEmpty()) {
Jon Mirandac84168d2019-08-19 13:32:09 -070096 // 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 Goyale7b00122019-10-02 16:13:34 -0700103 List<LauncherActivityInfo> activities = context.getSystemService(LauncherApps.class)
Sunny Goyala474a9b2017-05-04 16:47:11 -0700104 .getActivityList(packageName, user);
Sunny Goyale7b00122019-10-02 16:13:34 -0700105 if (activities.isEmpty()) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800106 // 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}