blob: 203bc25238f562e8f307dcc6f5a1cb11fdb3835b [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;
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070022import android.content.SharedPreferences;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080023import android.content.pm.LauncherActivityInfo;
24import android.content.pm.PackageInstaller;
25import android.content.pm.PackageInstaller.SessionInfo;
26import android.os.Process;
27import android.os.UserHandle;
28import android.text.TextUtils;
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070029import android.util.Log;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080030
31import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070032import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080033
34import java.util.List;
35
36/**
37 * BroadcastReceiver to handle session commit intent.
38 */
39public class SessionCommitReceiver extends BroadcastReceiver {
40
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070041 private static final long SESSION_IGNORE_DURATION = 3 * 60 * 60 * 1000; // 3 hours
42
Sunny Goyal4179e9b2017-03-08 14:25:09 -080043 // Preference key for automatically adding icon to homescreen.
44 public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home";
45
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070046 private static final String KEY_FIRST_TIME = "first_session_broadcast_time";
47
Sunny Goyal4179e9b2017-03-08 14:25:09 -080048 @Override
49 public void onReceive(Context context, Intent intent) {
50 if (!isEnabled(context)) {
51 // User has decided to not add icons on homescreen.
52 return;
53 }
54
55 SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION);
56 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
57 // TODO: Verify install reason
58 if (TextUtils.isEmpty(info.getAppPackageName())) {
59 return;
60 }
61
62 if (!Process.myUserHandle().equals(user)) {
63 // Managed profile is handled using ManagedProfileHeuristic
64 return;
65 }
66
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070067 // STOPSHIP: Remove this workaround when we start getting proper install reason
68 SharedPreferences prefs = context
69 .getSharedPreferences(LauncherFiles.DEVICE_PREFERENCES_KEY, 0);
70 long now = System.currentTimeMillis();
71 long firstTime = prefs.getLong(KEY_FIRST_TIME, now);
72 prefs.edit().putLong(KEY_FIRST_TIME, firstTime).apply();
73 if ((now - firstTime) < SESSION_IGNORE_DURATION) {
74 Log.d("SessionCommitReceiver", "Temporarily ignoring session broadcast");
75 return;
76 }
77
Sunny Goyal4179e9b2017-03-08 14:25:09 -080078 List<LauncherActivityInfo> activities = LauncherAppsCompat.getInstance(context)
79 .getActivityList(info.getAppPackageName(), user);
80 if (activities == null || activities.isEmpty()) {
81 // no activity found
82 return;
83 }
84 InstallShortcutReceiver.queueActivityInfo(activities.get(0), context);
85 }
86
87 public static boolean isEnabled(Context context) {
88 return Utilities.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true);
89 }
90}