blob: a1c77ef1d1e6d5207d9b3b99f407ad54d06b7025 [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
Sunny Goyal3e443a22017-04-10 10:49:01 -070019import android.annotation.TargetApi;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080020import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070023import android.content.SharedPreferences;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080024import android.content.pm.LauncherActivityInfo;
25import android.content.pm.PackageInstaller;
26import android.content.pm.PackageInstaller.SessionInfo;
Sunny Goyal3e443a22017-04-10 10:49:01 -070027import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
29import android.database.Cursor;
30import android.net.Uri;
31import android.os.AsyncTask;
32import android.os.Build;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080033import android.os.UserHandle;
Sunny Goyal3e443a22017-04-10 10:49:01 -070034import android.provider.Settings;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080035import android.text.TextUtils;
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070036import android.util.Log;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080037
38import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal6fe3eec2019-08-15 14:53:41 -070039import com.android.launcher3.util.Executors;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080040
41import java.util.List;
42
43/**
44 * BroadcastReceiver to handle session commit intent.
45 */
Sunny Goyal3e443a22017-04-10 10:49:01 -070046@TargetApi(Build.VERSION_CODES.O)
Sunny Goyal4179e9b2017-03-08 14:25:09 -080047public class SessionCommitReceiver extends BroadcastReceiver {
48
Sunny Goyal3e443a22017-04-10 10:49:01 -070049 private static final String TAG = "SessionCommitReceiver";
50
51 // The content provider for the add to home screen setting. It should be of the format:
52 // <package name>.addtohomescreen
53 private static final String MARKER_PROVIDER_PREFIX = ".addtohomescreen";
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070054
Sunny Goyal4179e9b2017-03-08 14:25:09 -080055 // Preference key for automatically adding icon to homescreen.
56 public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home";
Sunny Goyal3e443a22017-04-10 10:49:01 -070057 public static final String ADD_ICON_PREFERENCE_INITIALIZED_KEY =
58 "pref_add_icon_to_home_initialized";
Sunny Goyal0d7f19a2017-03-24 09:07:05 -070059
Sunny Goyal4179e9b2017-03-08 14:25:09 -080060 @Override
61 public void onReceive(Context context, Intent intent) {
Hyunyoung Songe24cb632017-09-11 11:18:03 -070062 if (!isEnabled(context) || !Utilities.ATLEAST_OREO) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -080063 // User has decided to not add icons on homescreen.
64 return;
65 }
66
67 SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION);
68 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyal3e443a22017-04-10 10:49:01 -070069
Tony Mak1b6826c2018-02-27 15:06:12 +000070 if (TextUtils.isEmpty(info.getAppPackageName()) ||
71 info.getInstallReason() != PackageManager.INSTALL_REASON_USER) {
72 return;
Sunny Goyal4179e9b2017-03-08 14:25:09 -080073 }
74
Sunny Goyala474a9b2017-05-04 16:47:11 -070075 queueAppIconAddition(context, info.getAppPackageName(), user);
76 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -080077
Sunny Goyala474a9b2017-05-04 16:47:11 -070078 public static void queueAppIconAddition(Context context, String packageName, UserHandle user) {
Sunny Goyal4179e9b2017-03-08 14:25:09 -080079 List<LauncherActivityInfo> activities = LauncherAppsCompat.getInstance(context)
Sunny Goyala474a9b2017-05-04 16:47:11 -070080 .getActivityList(packageName, user);
Sunny Goyal4179e9b2017-03-08 14:25:09 -080081 if (activities == null || activities.isEmpty()) {
82 // no activity found
83 return;
84 }
85 InstallShortcutReceiver.queueActivityInfo(activities.get(0), context);
86 }
87
88 public static boolean isEnabled(Context context) {
89 return Utilities.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true);
90 }
Sunny Goyal3e443a22017-04-10 10:49:01 -070091
92 public static void applyDefaultUserPrefs(final Context context) {
Hyunyoung Songe24cb632017-09-11 11:18:03 -070093 if (!Utilities.ATLEAST_OREO) {
Sunny Goyal3e443a22017-04-10 10:49:01 -070094 return;
95 }
96 SharedPreferences prefs = Utilities.getPrefs(context);
97 if (prefs.getAll().isEmpty()) {
98 // This logic assumes that the code is the first thing that is executed (before any
99 // shared preference is written).
100 // TODO: Move this logic to DB upgrade once we have proper support for db downgrade
101 // If it is a fresh start, just apply the default value. We use prefs.isEmpty() to infer
102 // a fresh start as put preferences always contain some values corresponding to current
103 // grid.
104 prefs.edit().putBoolean(ADD_ICON_PREFERENCE_KEY, true).apply();
105 } else if (!prefs.contains(ADD_ICON_PREFERENCE_INITIALIZED_KEY)) {
Sunny Goyal6fe3eec2019-08-15 14:53:41 -0700106 new PrefInitTask(context).executeOnExecutor(Executors.THREAD_POOL_EXECUTOR);
Sunny Goyal3e443a22017-04-10 10:49:01 -0700107 }
108 }
109
110 private static class PrefInitTask extends AsyncTask<Void, Void, Void> {
111 private final Context mContext;
112
113 PrefInitTask(Context context) {
114 mContext = context;
115 }
116
117 @Override
118 protected Void doInBackground(Void... voids) {
119 boolean addIconToHomeScreenEnabled = readValueFromMarketApp();
120 Utilities.getPrefs(mContext).edit()
121 .putBoolean(ADD_ICON_PREFERENCE_KEY, addIconToHomeScreenEnabled)
122 .putBoolean(ADD_ICON_PREFERENCE_INITIALIZED_KEY, true)
123 .apply();
124 return null;
125 }
126
127 public boolean readValueFromMarketApp() {
128 // Get the marget package
129 ResolveInfo ri = mContext.getPackageManager().resolveActivity(
130 new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET),
131 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_SYSTEM_ONLY);
132 if (ri == null) {
133 return true;
134 }
135
136 Cursor c = null;
137 try {
138 c = mContext.getContentResolver().query(
139 Uri.parse("content://" + ri.activityInfo.packageName
140 + MARKER_PROVIDER_PREFIX),
141 null, null, null, null);
142 if (c.moveToNext()) {
143 return c.getInt(c.getColumnIndexOrThrow(Settings.NameValueTable.VALUE)) != 0;
144 }
145 } catch (Exception e) {
146 Log.d(TAG, "Error reading add to homescreen preference", e);
147 } finally {
148 if (c != null) {
149 c.close();
150 }
151 }
152 return true;
153 }
154 }
Sunny Goyal4179e9b2017-03-08 14:25:09 -0800155}