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 | |
Sunny Goyal | 73b5a27 | 2019-12-09 14:55:56 -0800 | [diff] [blame] | 28 | import com.android.launcher3.pm.InstallSessionHelper; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 29 | |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 30 | /** |
| 31 | * BroadcastReceiver to handle session commit intent. |
| 32 | */ |
| 33 | public class SessionCommitReceiver extends BroadcastReceiver { |
| 34 | |
| 35 | // Preference key for automatically adding icon to homescreen. |
| 36 | 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] | 37 | |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 38 | @Override |
| 39 | public void onReceive(Context context, Intent intent) { |
Sunny Goyal | eaf7a95 | 2020-07-29 16:54:20 -0700 | [diff] [blame] | 40 | if (!isEnabled(context)) { |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 41 | // User has decided to not add icons on homescreen. |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION); |
| 46 | UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER); |
Jon Miranda | 7e04887 | 2019-11-08 13:02:52 -0800 | [diff] [blame] | 47 | if (!PackageInstaller.ACTION_SESSION_COMMITTED.equals(intent.getAction()) |
| 48 | || info == null || user == null) { |
| 49 | // Invalid intent. |
| 50 | return; |
| 51 | } |
Sunny Goyal | 3e443a2 | 2017-04-10 10:49:01 -0700 | [diff] [blame] | 52 | |
Sunny Goyal | 73b5a27 | 2019-12-09 14:55:56 -0800 | [diff] [blame] | 53 | InstallSessionHelper packageInstallerCompat = InstallSessionHelper.INSTANCE.get(context); |
Pinyao Ting | ad5f240 | 2019-12-10 14:56:34 -0800 | [diff] [blame] | 54 | packageInstallerCompat.restoreDbIfApplicable(info); |
Jon Miranda | c84168d | 2019-08-19 13:32:09 -0700 | [diff] [blame] | 55 | if (TextUtils.isEmpty(info.getAppPackageName()) |
| 56 | || info.getInstallReason() != PackageManager.INSTALL_REASON_USER |
| 57 | || packageInstallerCompat.promiseIconAddedForId(info.getSessionId())) { |
| 58 | packageInstallerCompat.removePromiseIconId(info.getSessionId()); |
Tony Mak | 1b6826c | 2018-02-27 15:06:12 +0000 | [diff] [blame] | 59 | return; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Sunny Goyal | 53116c6 | 2020-08-07 16:32:18 -0700 | [diff] [blame] | 62 | InstallShortcutReceiver.queueApplication(info.getAppPackageName(), user, context); |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | public static boolean isEnabled(Context context) { |
| 66 | return Utilities.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true); |
| 67 | } |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 68 | } |