Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 22 | import android.content.pm.PackageInstaller; |
| 23 | import android.content.pm.PackageInstaller.SessionInfo; |
Sunny Goyal | 3e443a2 | 2017-04-10 10:49:01 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 25 | import android.os.UserHandle; |
| 26 | import android.text.TextUtils; |
Jon Miranda | 701774d | 2022-02-10 11:09:04 -0800 | [diff] [blame] | 27 | import android.util.Log; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 28 | |
Winson Chung | f993518 | 2020-10-23 09:26:44 -0700 | [diff] [blame] | 29 | import androidx.annotation.WorkerThread; |
| 30 | |
Schneider Victor-tulias | 344b200 | 2021-05-04 11:40:05 -0700 | [diff] [blame] | 31 | import com.android.launcher3.logging.FileLog; |
Sunny Goyal | 60e68c9 | 2020-08-12 13:59:27 -0700 | [diff] [blame] | 32 | import com.android.launcher3.model.ItemInstallQueue; |
Sunny Goyal | 73b5a27 | 2019-12-09 14:55:56 -0800 | [diff] [blame] | 33 | import com.android.launcher3.pm.InstallSessionHelper; |
vadimt | f6ef879 | 2022-07-26 13:54:31 -0700 | [diff] [blame] | 34 | import com.android.launcher3.testing.shared.TestProtocol; |
Winson Chung | f993518 | 2020-10-23 09:26:44 -0700 | [diff] [blame] | 35 | import com.android.launcher3.util.Executors; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 36 | |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 37 | /** |
| 38 | * BroadcastReceiver to handle session commit intent. |
| 39 | */ |
| 40 | public class SessionCommitReceiver extends BroadcastReceiver { |
| 41 | |
Schneider Victor-tulias | bf694d6 | 2021-03-25 13:51:20 -0700 | [diff] [blame] | 42 | private static final String LOG = "SessionCommitReceiver"; |
| 43 | |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 44 | // Preference key for automatically adding icon to homescreen. |
| 45 | public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home"; |
Sunny Goyal | 0d7f19a | 2017-03-24 09:07:05 -0700 | [diff] [blame] | 46 | |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 47 | @Override |
| 48 | public void onReceive(Context context, Intent intent) { |
Winson Chung | f993518 | 2020-10-23 09:26:44 -0700 | [diff] [blame] | 49 | Executors.MODEL_EXECUTOR.execute(() -> processIntent(context, intent)); |
| 50 | } |
| 51 | |
| 52 | @WorkerThread |
| 53 | private static void processIntent(Context context, Intent intent) { |
Sunny Goyal | eaf7a95 | 2020-07-29 16:54:20 -0700 | [diff] [blame] | 54 | if (!isEnabled(context)) { |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 55 | // User has decided to not add icons on homescreen. |
Jon Miranda | 701774d | 2022-02-10 11:09:04 -0800 | [diff] [blame] | 56 | if (TestProtocol.sDebugTracing) { |
| 57 | Log.d(TestProtocol.MISSING_PROMISE_ICON, LOG + " not enabled"); |
| 58 | } |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 59 | return; |
| 60 | } |
| 61 | |
| 62 | SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION); |
| 63 | UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER); |
Jon Miranda | 7e04887 | 2019-11-08 13:02:52 -0800 | [diff] [blame] | 64 | if (!PackageInstaller.ACTION_SESSION_COMMITTED.equals(intent.getAction()) |
| 65 | || info == null || user == null) { |
| 66 | // Invalid intent. |
Jon Miranda | 701774d | 2022-02-10 11:09:04 -0800 | [diff] [blame] | 67 | if (TestProtocol.sDebugTracing) { |
| 68 | Log.d(TestProtocol.MISSING_PROMISE_ICON, LOG + " invalid intent"); |
| 69 | } |
Jon Miranda | 7e04887 | 2019-11-08 13:02:52 -0800 | [diff] [blame] | 70 | return; |
| 71 | } |
Sunny Goyal | 3e443a2 | 2017-04-10 10:49:01 -0700 | [diff] [blame] | 72 | |
Sunny Goyal | 73b5a27 | 2019-12-09 14:55:56 -0800 | [diff] [blame] | 73 | InstallSessionHelper packageInstallerCompat = InstallSessionHelper.INSTANCE.get(context); |
Pinyao Ting | ad5f240 | 2019-12-10 14:56:34 -0800 | [diff] [blame] | 74 | packageInstallerCompat.restoreDbIfApplicable(info); |
Jon Miranda | c84168d | 2019-08-19 13:32:09 -0700 | [diff] [blame] | 75 | if (TextUtils.isEmpty(info.getAppPackageName()) |
| 76 | || info.getInstallReason() != PackageManager.INSTALL_REASON_USER |
| 77 | || packageInstallerCompat.promiseIconAddedForId(info.getSessionId())) { |
| 78 | packageInstallerCompat.removePromiseIconId(info.getSessionId()); |
Jon Miranda | 701774d | 2022-02-10 11:09:04 -0800 | [diff] [blame] | 79 | 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 Mak | 1b6826c | 2018-02-27 15:06:12 +0000 | [diff] [blame] | 88 | return; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Schneider Victor-tulias | 344b200 | 2021-05-04 11:40:05 -0700 | [diff] [blame] | 91 | FileLog.d(LOG, |
Schneider Victor-tulias | bf694d6 | 2021-03-25 13:51:20 -0700 | [diff] [blame] | 92 | "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 Goyal | 60e68c9 | 2020-08-12 13:59:27 -0700 | [diff] [blame] | 96 | ItemInstallQueue.INSTANCE.get(context) |
| 97 | .queueItem(info.getAppPackageName(), user); |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | public static boolean isEnabled(Context context) { |
Stefan Andonian | 146701c | 2022-11-10 23:07:40 +0000 | [diff] [blame^] | 101 | return LauncherPrefs.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true); |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 102 | } |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 103 | } |