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; |
| 27 | |
Winson Chung | f993518 | 2020-10-23 09:26:44 -0700 | [diff] [blame] | 28 | import androidx.annotation.WorkerThread; |
| 29 | |
Schneider Victor-tulias | 344b200 | 2021-05-04 11:40:05 -0700 | [diff] [blame^] | 30 | import com.android.launcher3.logging.FileLog; |
Sunny Goyal | 60e68c9 | 2020-08-12 13:59:27 -0700 | [diff] [blame] | 31 | import com.android.launcher3.model.ItemInstallQueue; |
Sunny Goyal | 73b5a27 | 2019-12-09 14:55:56 -0800 | [diff] [blame] | 32 | import com.android.launcher3.pm.InstallSessionHelper; |
Winson Chung | f993518 | 2020-10-23 09:26:44 -0700 | [diff] [blame] | 33 | import com.android.launcher3.util.Executors; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 34 | |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 35 | /** |
| 36 | * BroadcastReceiver to handle session commit intent. |
| 37 | */ |
| 38 | public class SessionCommitReceiver extends BroadcastReceiver { |
| 39 | |
Schneider Victor-tulias | bf694d6 | 2021-03-25 13:51:20 -0700 | [diff] [blame] | 40 | private static final String LOG = "SessionCommitReceiver"; |
| 41 | |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 42 | // Preference key for automatically adding icon to homescreen. |
| 43 | 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] | 44 | |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 45 | @Override |
| 46 | public void onReceive(Context context, Intent intent) { |
Winson Chung | f993518 | 2020-10-23 09:26:44 -0700 | [diff] [blame] | 47 | Executors.MODEL_EXECUTOR.execute(() -> processIntent(context, intent)); |
| 48 | } |
| 49 | |
| 50 | @WorkerThread |
| 51 | private static void processIntent(Context context, Intent intent) { |
Sunny Goyal | eaf7a95 | 2020-07-29 16:54:20 -0700 | [diff] [blame] | 52 | if (!isEnabled(context)) { |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 53 | // User has decided to not add icons on homescreen. |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION); |
| 58 | UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER); |
Jon Miranda | 7e04887 | 2019-11-08 13:02:52 -0800 | [diff] [blame] | 59 | if (!PackageInstaller.ACTION_SESSION_COMMITTED.equals(intent.getAction()) |
| 60 | || info == null || user == null) { |
| 61 | // Invalid intent. |
| 62 | return; |
| 63 | } |
Sunny Goyal | 3e443a2 | 2017-04-10 10:49:01 -0700 | [diff] [blame] | 64 | |
Sunny Goyal | 73b5a27 | 2019-12-09 14:55:56 -0800 | [diff] [blame] | 65 | InstallSessionHelper packageInstallerCompat = InstallSessionHelper.INSTANCE.get(context); |
Pinyao Ting | ad5f240 | 2019-12-10 14:56:34 -0800 | [diff] [blame] | 66 | packageInstallerCompat.restoreDbIfApplicable(info); |
Jon Miranda | c84168d | 2019-08-19 13:32:09 -0700 | [diff] [blame] | 67 | if (TextUtils.isEmpty(info.getAppPackageName()) |
| 68 | || info.getInstallReason() != PackageManager.INSTALL_REASON_USER |
| 69 | || packageInstallerCompat.promiseIconAddedForId(info.getSessionId())) { |
| 70 | packageInstallerCompat.removePromiseIconId(info.getSessionId()); |
Tony Mak | 1b6826c | 2018-02-27 15:06:12 +0000 | [diff] [blame] | 71 | return; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Schneider Victor-tulias | 344b200 | 2021-05-04 11:40:05 -0700 | [diff] [blame^] | 74 | FileLog.d(LOG, |
Schneider Victor-tulias | bf694d6 | 2021-03-25 13:51:20 -0700 | [diff] [blame] | 75 | "Adding package name to install queue. Package name: " + info.getAppPackageName() |
| 76 | + ", has app icon: " + (info.getAppIcon() != null) |
| 77 | + ", has app label: " + !TextUtils.isEmpty(info.getAppLabel())); |
| 78 | |
Sunny Goyal | 60e68c9 | 2020-08-12 13:59:27 -0700 | [diff] [blame] | 79 | ItemInstallQueue.INSTANCE.get(context) |
| 80 | .queueItem(info.getAppPackageName(), user); |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | public static boolean isEnabled(Context context) { |
| 84 | return Utilities.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true); |
| 85 | } |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 86 | } |