blob: 50ad2be5053d4244cb3c4bb56c41b63c5e68de71 [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
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080022import android.content.pm.PackageInstaller;
23import android.content.pm.PackageInstaller.SessionInfo;
Sunny Goyal3e443a22017-04-10 10:49:01 -070024import android.content.pm.PackageManager;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080025import android.os.UserHandle;
26import android.text.TextUtils;
Jon Miranda701774d2022-02-10 11:09:04 -080027import android.util.Log;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080028
Winson Chungf9935182020-10-23 09:26:44 -070029import androidx.annotation.WorkerThread;
30
Schneider Victor-tulias344b2002021-05-04 11:40:05 -070031import com.android.launcher3.logging.FileLog;
Sunny Goyal60e68c92020-08-12 13:59:27 -070032import com.android.launcher3.model.ItemInstallQueue;
Sunny Goyal73b5a272019-12-09 14:55:56 -080033import com.android.launcher3.pm.InstallSessionHelper;
vadimtf6ef8792022-07-26 13:54:31 -070034import com.android.launcher3.testing.shared.TestProtocol;
Winson Chungf9935182020-10-23 09:26:44 -070035import com.android.launcher3.util.Executors;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080036
Sunny Goyal4179e9b2017-03-08 14:25:09 -080037/**
38 * BroadcastReceiver to handle session commit intent.
39 */
40public class SessionCommitReceiver extends BroadcastReceiver {
41
Schneider Victor-tuliasbf694d62021-03-25 13:51:20 -070042 private static final String LOG = "SessionCommitReceiver";
43
Sunny Goyal4179e9b2017-03-08 14:25:09 -080044 // Preference key for automatically adding icon to homescreen.
45 public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home";
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070046
Sunny Goyal4179e9b2017-03-08 14:25:09 -080047 @Override
48 public void onReceive(Context context, Intent intent) {
Winson Chungf9935182020-10-23 09:26:44 -070049 Executors.MODEL_EXECUTOR.execute(() -> processIntent(context, intent));
50 }
51
52 @WorkerThread
53 private static void processIntent(Context context, Intent intent) {
Sunny Goyaleaf7a952020-07-29 16:54:20 -070054 if (!isEnabled(context)) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -080055 // User has decided to not add icons on homescreen.
Jon Miranda701774d2022-02-10 11:09:04 -080056 if (TestProtocol.sDebugTracing) {
57 Log.d(TestProtocol.MISSING_PROMISE_ICON, LOG + " not enabled");
58 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -080059 return;
60 }
61
62 SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION);
63 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Jon Miranda7e048872019-11-08 13:02:52 -080064 if (!PackageInstaller.ACTION_SESSION_COMMITTED.equals(intent.getAction())
65 || info == null || user == null) {
66 // Invalid intent.
Jon Miranda701774d2022-02-10 11:09:04 -080067 if (TestProtocol.sDebugTracing) {
68 Log.d(TestProtocol.MISSING_PROMISE_ICON, LOG + " invalid intent");
69 }
Jon Miranda7e048872019-11-08 13:02:52 -080070 return;
71 }
Sunny Goyal3e443a22017-04-10 10:49:01 -070072
Sunny Goyal73b5a272019-12-09 14:55:56 -080073 InstallSessionHelper packageInstallerCompat = InstallSessionHelper.INSTANCE.get(context);
Pinyao Tingad5f2402019-12-10 14:56:34 -080074 packageInstallerCompat.restoreDbIfApplicable(info);
Jon Mirandac84168d2019-08-19 13:32:09 -070075 if (TextUtils.isEmpty(info.getAppPackageName())
76 || info.getInstallReason() != PackageManager.INSTALL_REASON_USER
77 || packageInstallerCompat.promiseIconAddedForId(info.getSessionId())) {
78 packageInstallerCompat.removePromiseIconId(info.getSessionId());
Jon Miranda701774d2022-02-10 11:09:04 -080079 if (TestProtocol.sDebugTracing) {
80 int id = info.getSessionId();
81 Log.d(TestProtocol.MISSING_PROMISE_ICON, LOG
82 + ", TextUtils.isEmpty=" + TextUtils.isEmpty(info.getAppPackageName())
83 + ", info.getInstallReason()=" + info.getInstallReason()
84 + ", INSTALL_REASON_USER=" + PackageManager.INSTALL_REASON_USER
85 + ", icon added=" + packageInstallerCompat.promiseIconAddedForId(id)
86 );
87 }
Tony Mak1b6826c2018-02-27 15:06:12 +000088 return;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080089 }
90
Schneider Victor-tulias344b2002021-05-04 11:40:05 -070091 FileLog.d(LOG,
Schneider Victor-tuliasbf694d62021-03-25 13:51:20 -070092 "Adding package name to install queue. Package name: " + info.getAppPackageName()
93 + ", has app icon: " + (info.getAppIcon() != null)
94 + ", has app label: " + !TextUtils.isEmpty(info.getAppLabel()));
95
Sunny Goyal60e68c92020-08-12 13:59:27 -070096 ItemInstallQueue.INSTANCE.get(context)
97 .queueItem(info.getAppPackageName(), user);
Sunny Goyal4179e9b2017-03-08 14:25:09 -080098 }
99
100 public static boolean isEnabled(Context context) {
Stefan Andonian146701c2022-11-10 23:07:40 +0000101 return LauncherPrefs.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true);
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800102 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800103}