blob: e8bf0a5c36bc4c741c09c6100d61782e620da26b [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;
23import android.content.pm.PackageInstaller;
24import android.content.pm.PackageInstaller.SessionInfo;
25import android.os.Process;
26import android.os.UserHandle;
27import android.text.TextUtils;
28
29import com.android.launcher3.compat.LauncherAppsCompat;
30
31import java.util.List;
32
33/**
34 * BroadcastReceiver to handle session commit intent.
35 */
36public class SessionCommitReceiver extends BroadcastReceiver {
37
38 // Preference key for automatically adding icon to homescreen.
39 public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home";
40
41 @Override
42 public void onReceive(Context context, Intent intent) {
43 if (!isEnabled(context)) {
44 // User has decided to not add icons on homescreen.
45 return;
46 }
47
48 SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION);
49 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
50 // TODO: Verify install reason
51 if (TextUtils.isEmpty(info.getAppPackageName())) {
52 return;
53 }
54
55 if (!Process.myUserHandle().equals(user)) {
56 // Managed profile is handled using ManagedProfileHeuristic
57 return;
58 }
59
60 List<LauncherActivityInfo> activities = LauncherAppsCompat.getInstance(context)
61 .getActivityList(info.getAppPackageName(), user);
62 if (activities == null || activities.isEmpty()) {
63 // no activity found
64 return;
65 }
66 InstallShortcutReceiver.queueActivityInfo(activities.get(0), context);
67 }
68
69 public static boolean isEnabled(Context context) {
70 return Utilities.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true);
71 }
72}