blob: d460ba866853342b7d6195aa2dd435ff375bdd8b [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;
Winson Chungf9935182020-10-23 09:26:44 -070033import com.android.launcher3.util.Executors;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080034
Charlie Andersonc9d11e82023-08-02 11:41:55 -040035import java.util.Locale;
36
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.
56 return;
57 }
58
59 SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION);
60 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Jon Miranda7e048872019-11-08 13:02:52 -080061 if (!PackageInstaller.ACTION_SESSION_COMMITTED.equals(intent.getAction())
62 || info == null || user == null) {
63 // Invalid intent.
64 return;
65 }
Sunny Goyal3e443a22017-04-10 10:49:01 -070066
Sunny Goyal73b5a272019-12-09 14:55:56 -080067 InstallSessionHelper packageInstallerCompat = InstallSessionHelper.INSTANCE.get(context);
Charlie Andersonc9d11e82023-08-02 11:41:55 -040068 boolean alreadyAddedPromiseIcon =
69 packageInstallerCompat.promiseIconAddedForId(info.getSessionId());
Jon Mirandac84168d2019-08-19 13:32:09 -070070 if (TextUtils.isEmpty(info.getAppPackageName())
71 || info.getInstallReason() != PackageManager.INSTALL_REASON_USER
Charlie Andersonc9d11e82023-08-02 11:41:55 -040072 || alreadyAddedPromiseIcon) {
73 FileLog.d(LOG,
74 String.format(Locale.ENGLISH,
75 "Removing PromiseIcon for package: %s, install reason: %d,"
76 + " alreadyAddedPromiseIcon: %s",
77 info.getAppPackageName(),
78 info.getInstallReason(),
79 alreadyAddedPromiseIcon
80 )
81 );
Jon Mirandac84168d2019-08-19 13:32:09 -070082 packageInstallerCompat.removePromiseIconId(info.getSessionId());
Tony Mak1b6826c2018-02-27 15:06:12 +000083 return;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080084 }
85
Schneider Victor-tulias344b2002021-05-04 11:40:05 -070086 FileLog.d(LOG,
Schneider Victor-tuliasbf694d62021-03-25 13:51:20 -070087 "Adding package name to install queue. Package name: " + info.getAppPackageName()
88 + ", has app icon: " + (info.getAppIcon() != null)
89 + ", has app label: " + !TextUtils.isEmpty(info.getAppLabel()));
90
Sunny Goyal60e68c92020-08-12 13:59:27 -070091 ItemInstallQueue.INSTANCE.get(context)
92 .queueItem(info.getAppPackageName(), user);
Sunny Goyal4179e9b2017-03-08 14:25:09 -080093 }
94
95 public static boolean isEnabled(Context context) {
Stefan Andonian146701c2022-11-10 23:07:40 +000096 return LauncherPrefs.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true);
Sunny Goyal4179e9b2017-03-08 14:25:09 -080097 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -080098}