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