blob: 6168e414645a19ea1caf4e400c3b92d0a6318938 [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;
27
Winson Chungf9935182020-10-23 09:26:44 -070028import androidx.annotation.WorkerThread;
29
Schneider Victor-tulias344b2002021-05-04 11:40:05 -070030import com.android.launcher3.logging.FileLog;
Sunny Goyal60e68c92020-08-12 13:59:27 -070031import com.android.launcher3.model.ItemInstallQueue;
Sunny Goyal73b5a272019-12-09 14:55:56 -080032import com.android.launcher3.pm.InstallSessionHelper;
Himanshu Gupta4499fa62024-02-15 20:00:33 +000033import com.android.launcher3.pm.UserCache;
Winson Chungf9935182020-10-23 09:26:44 -070034import com.android.launcher3.util.Executors;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080035
Charlie Andersonc9d11e82023-08-02 11:41:55 -040036import java.util.Locale;
37
Sunny Goyal4179e9b2017-03-08 14:25:09 -080038/**
39 * BroadcastReceiver to handle session commit intent.
40 */
41public class SessionCommitReceiver extends BroadcastReceiver {
42
Schneider Victor-tuliasbf694d62021-03-25 13:51:20 -070043 private static final String LOG = "SessionCommitReceiver";
44
Sunny Goyal4179e9b2017-03-08 14:25:09 -080045 // Preference key for automatically adding icon to homescreen.
46 public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home";
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070047
Sunny Goyal4179e9b2017-03-08 14:25:09 -080048 @Override
49 public void onReceive(Context context, Intent intent) {
Winson Chungf9935182020-10-23 09:26:44 -070050 Executors.MODEL_EXECUTOR.execute(() -> processIntent(context, intent));
51 }
52
53 @WorkerThread
54 private static void processIntent(Context context, Intent intent) {
Himanshu Gupta4499fa62024-02-15 20:00:33 +000055 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
56 if (!isEnabled(context, user)) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -080057 // User has decided to not add icons on homescreen.
58 return;
59 }
60
61 SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION);
Jon Miranda7e048872019-11-08 13:02:52 -080062 if (!PackageInstaller.ACTION_SESSION_COMMITTED.equals(intent.getAction())
63 || info == null || user == null) {
64 // Invalid intent.
65 return;
66 }
Sunny Goyal3e443a22017-04-10 10:49:01 -070067
Sunny Goyal73b5a272019-12-09 14:55:56 -080068 InstallSessionHelper packageInstallerCompat = InstallSessionHelper.INSTANCE.get(context);
Charlie Andersonc9d11e82023-08-02 11:41:55 -040069 boolean alreadyAddedPromiseIcon =
70 packageInstallerCompat.promiseIconAddedForId(info.getSessionId());
Jon Mirandac84168d2019-08-19 13:32:09 -070071 if (TextUtils.isEmpty(info.getAppPackageName())
72 || info.getInstallReason() != PackageManager.INSTALL_REASON_USER
Charlie Andersonc9d11e82023-08-02 11:41:55 -040073 || alreadyAddedPromiseIcon) {
74 FileLog.d(LOG,
75 String.format(Locale.ENGLISH,
76 "Removing PromiseIcon for package: %s, install reason: %d,"
77 + " alreadyAddedPromiseIcon: %s",
78 info.getAppPackageName(),
79 info.getInstallReason(),
80 alreadyAddedPromiseIcon
81 )
82 );
Jon Mirandac84168d2019-08-19 13:32:09 -070083 packageInstallerCompat.removePromiseIconId(info.getSessionId());
Tony Mak1b6826c2018-02-27 15:06:12 +000084 return;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080085 }
86
Schneider Victor-tulias344b2002021-05-04 11:40:05 -070087 FileLog.d(LOG,
Schneider Victor-tuliasbf694d62021-03-25 13:51:20 -070088 "Adding package name to install queue. Package name: " + info.getAppPackageName()
89 + ", has app icon: " + (info.getAppIcon() != null)
90 + ", has app label: " + !TextUtils.isEmpty(info.getAppLabel()));
91
Sunny Goyal60e68c92020-08-12 13:59:27 -070092 ItemInstallQueue.INSTANCE.get(context)
93 .queueItem(info.getAppPackageName(), user);
Sunny Goyal4179e9b2017-03-08 14:25:09 -080094 }
95
Himanshu Gupta4499fa62024-02-15 20:00:33 +000096 /**
97 * Returns whether adding Installed App Icons to home screen is allowed or not.
98 * Not allowed when:
99 * - User belongs to {@link com.android.launcher3.util.UserIconInfo.TYPE_PRIVATE} or
100 * - Home Settings preference to add App Icons on Home Screen is set as disabled
101 */
102 public static boolean isEnabled(Context context, UserHandle user) {
103 if (Flags.privateSpaceRestrictItemDrag() && user != null
104 && UserCache.getInstance(context).getUserInfo(user).isPrivate()) {
105 return false;
106 }
Stefan Andonian146701c2022-11-10 23:07:40 +0000107 return LauncherPrefs.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true);
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800108 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800109}