SessionCommitReceiver.java revision 0d7f19a56649076b08b88fcba83cb81ddacc4242
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
17package com.android.launcher3;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.SharedPreferences;
23import 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;
29import android.util.Log;
30
31import com.android.launcher3.compat.LauncherAppsCompat;
32import com.android.launcher3.compat.UserManagerCompat;
33
34import java.util.List;
35
36/**
37 * BroadcastReceiver to handle session commit intent.
38 */
39public class SessionCommitReceiver extends BroadcastReceiver {
40
41    private static final long SESSION_IGNORE_DURATION = 3 * 60 * 60 * 1000; // 3 hours
42
43    // Preference key for automatically adding icon to homescreen.
44    public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home";
45
46    private static final String KEY_FIRST_TIME = "first_session_broadcast_time";
47
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
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
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}
91