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 | 0d7f19a | 2017-03-24 09:07:05 -0700 | [diff] [blame] | 22 | import android.content.SharedPreferences; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 23 | import android.content.pm.LauncherActivityInfo; |
| 24 | import android.content.pm.PackageInstaller; |
| 25 | import android.content.pm.PackageInstaller.SessionInfo; |
| 26 | import android.os.Process; |
| 27 | import android.os.UserHandle; |
| 28 | import android.text.TextUtils; |
Sunny Goyal | 0d7f19a | 2017-03-24 09:07:05 -0700 | [diff] [blame] | 29 | import android.util.Log; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 30 | |
| 31 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 0d7f19a | 2017-03-24 09:07:05 -0700 | [diff] [blame] | 32 | import com.android.launcher3.compat.UserManagerCompat; |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 33 | |
| 34 | import java.util.List; |
| 35 | |
| 36 | /** |
| 37 | * BroadcastReceiver to handle session commit intent. |
| 38 | */ |
| 39 | public class SessionCommitReceiver extends BroadcastReceiver { |
| 40 | |
Sunny Goyal | 0d7f19a | 2017-03-24 09:07:05 -0700 | [diff] [blame] | 41 | private static final long SESSION_IGNORE_DURATION = 3 * 60 * 60 * 1000; // 3 hours |
| 42 | |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 43 | // Preference key for automatically adding icon to homescreen. |
| 44 | public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home"; |
| 45 | |
Sunny Goyal | 0d7f19a | 2017-03-24 09:07:05 -0700 | [diff] [blame] | 46 | private static final String KEY_FIRST_TIME = "first_session_broadcast_time"; |
| 47 | |
Sunny Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 48 | @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 Goyal | 0d7f19a | 2017-03-24 09:07:05 -0700 | [diff] [blame] | 67 | // 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 Goyal | 4179e9b | 2017-03-08 14:25:09 -0800 | [diff] [blame] | 78 | 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 | } |