blob: 8dedc6c498106c249af86b9fc2580131ad67604e [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 Goyal045b4fa2019-09-20 12:51:37 -070019import static com.android.launcher3.pm.PackageInstallerCompat.getUserHandle;
20
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 Goyal045b4fa2019-09-20 12:51:37 -070043import com.android.launcher3.pm.PackageInstallerCompat;
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 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();
Sunny Goyale7b00122019-10-02 16:13:34 -070088 if (context.getSystemService(LauncherApps.class)
89 .getActivityList(packageName, getUserHandle(sessionInfo)).isEmpty()) {
Jon Mirandac84168d2019-08-19 13:32:09 -070090 // Ensure application isn't already installed.
91 queueAppIconAddition(context, packageName, sessionInfo.getAppLabel(),
92 sessionInfo.getAppIcon(), getUserHandle(sessionInfo));
93 }
94 }
95
Sunny Goyala474a9b2017-05-04 16:47:11 -070096 public static void queueAppIconAddition(Context context, String packageName, UserHandle user) {
Sunny Goyale7b00122019-10-02 16:13:34 -070097 List<LauncherActivityInfo> activities = context.getSystemService(LauncherApps.class)
Sunny Goyala474a9b2017-05-04 16:47:11 -070098 .getActivityList(packageName, user);
Sunny Goyale7b00122019-10-02 16:13:34 -070099 if (activities.isEmpty()) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800100 // no activity found
101 return;
102 }
Jon Mirandac84168d2019-08-19 13:32:09 -0700103 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 Goyal4179e9b2017-03-08 14:25:09 -0800115 }
116
117 public static boolean isEnabled(Context context) {
118 return Utilities.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true);
119 }
Sunny Goyal3e443a22017-04-10 10:49:01 -0700120
121 public static void applyDefaultUserPrefs(final Context context) {
Hyunyoung Songe24cb632017-09-11 11:18:03 -0700122 if (!Utilities.ATLEAST_OREO) {
Sunny Goyal3e443a22017-04-10 10:49:01 -0700123 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 Goyal6fe3eec2019-08-15 14:53:41 -0700135 new PrefInitTask(context).executeOnExecutor(Executors.THREAD_POOL_EXECUTOR);
Sunny Goyal3e443a22017-04-10 10:49:01 -0700136 }
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 Goyal4179e9b2017-03-08 14:25:09 -0800184}