blob: a8d6490a5b2fcf38f1f89486c1a7e2837a11e471 [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;
22import android.content.pm.LauncherActivityInfo;
Sunny Goyale7b00122019-10-02 16:13:34 -070023import android.content.pm.LauncherApps;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080024import android.content.pm.PackageInstaller;
25import android.content.pm.PackageInstaller.SessionInfo;
Sunny Goyal3e443a22017-04-10 10:49:01 -070026import android.content.pm.PackageManager;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080027import android.os.UserHandle;
28import android.text.TextUtils;
29
Sunny Goyal73b5a272019-12-09 14:55:56 -080030import com.android.launcher3.pm.InstallSessionHelper;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080031
32import java.util.List;
33
34/**
35 * BroadcastReceiver to handle session commit intent.
36 */
37public class SessionCommitReceiver extends BroadcastReceiver {
38
39 // Preference key for automatically adding icon to homescreen.
40 public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home";
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070041
Sunny Goyal4179e9b2017-03-08 14:25:09 -080042 @Override
43 public void onReceive(Context context, Intent intent) {
Sunny Goyaleaf7a952020-07-29 16:54:20 -070044 if (!isEnabled(context)) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -080045 // User has decided to not add icons on homescreen.
46 return;
47 }
48
49 SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION);
50 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Jon Miranda7e048872019-11-08 13:02:52 -080051 if (!PackageInstaller.ACTION_SESSION_COMMITTED.equals(intent.getAction())
52 || info == null || user == null) {
53 // Invalid intent.
54 return;
55 }
Sunny Goyal3e443a22017-04-10 10:49:01 -070056
Sunny Goyal73b5a272019-12-09 14:55:56 -080057 InstallSessionHelper packageInstallerCompat = InstallSessionHelper.INSTANCE.get(context);
Pinyao Tingad5f2402019-12-10 14:56:34 -080058 packageInstallerCompat.restoreDbIfApplicable(info);
Jon Mirandac84168d2019-08-19 13:32:09 -070059 if (TextUtils.isEmpty(info.getAppPackageName())
60 || info.getInstallReason() != PackageManager.INSTALL_REASON_USER
61 || packageInstallerCompat.promiseIconAddedForId(info.getSessionId())) {
62 packageInstallerCompat.removePromiseIconId(info.getSessionId());
Tony Mak1b6826c2018-02-27 15:06:12 +000063 return;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080064 }
65
Sunny Goyala474a9b2017-05-04 16:47:11 -070066 queueAppIconAddition(context, info.getAppPackageName(), user);
67 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -080068
Sunny Goyala474a9b2017-05-04 16:47:11 -070069 public static void queueAppIconAddition(Context context, String packageName, UserHandle user) {
Sunny Goyale7b00122019-10-02 16:13:34 -070070 List<LauncherActivityInfo> activities = context.getSystemService(LauncherApps.class)
Sunny Goyala474a9b2017-05-04 16:47:11 -070071 .getActivityList(packageName, user);
Sunny Goyale7b00122019-10-02 16:13:34 -070072 if (activities.isEmpty()) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -080073 // no activity found
74 return;
75 }
Jon Mirandac84168d2019-08-19 13:32:09 -070076
Sunny Goyaleaf7a952020-07-29 16:54:20 -070077 InstallShortcutReceiver.queueApplication(activities.get(0), context);
Sunny Goyal4179e9b2017-03-08 14:25:09 -080078 }
79
80 public static boolean isEnabled(Context context) {
81 return Utilities.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true);
82 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -080083}