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.annotation.TargetApi; 20 import android.content.BroadcastReceiver; 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.SharedPreferences; 25 import android.content.pm.LauncherActivityInfo; 26 import android.content.pm.PackageInstaller; 27 import android.content.pm.PackageInstaller.SessionInfo; 28 import android.content.pm.PackageManager; 29 import android.content.pm.ResolveInfo; 30 import android.database.Cursor; 31 import android.graphics.Bitmap; 32 import android.net.Uri; 33 import android.os.AsyncTask; 34 import android.os.Build; 35 import android.os.UserHandle; 36 import android.provider.Settings; 37 import android.text.TextUtils; 38 import android.util.Log; 39 40 import com.android.launcher3.compat.LauncherAppsCompat; 41 import com.android.launcher3.util.Executors; 42 import com.android.launcher3.compat.PackageInstallerCompat; 43 44 import java.util.List; 45 46 import static com.android.launcher3.compat.PackageInstallerCompat.getUserHandle; 47 48 /** 49 * BroadcastReceiver to handle session commit intent. 50 */ 51 @TargetApi(Build.VERSION_CODES.O) 52 public class SessionCommitReceiver extends BroadcastReceiver { 53 54 private static final String TAG = "SessionCommitReceiver"; 55 56 // The content provider for the add to home screen setting. It should be of the format: 57 // <package name>.addtohomescreen 58 private static final String MARKER_PROVIDER_PREFIX = ".addtohomescreen"; 59 60 // Preference key for automatically adding icon to homescreen. 61 public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home"; 62 public static final String ADD_ICON_PREFERENCE_INITIALIZED_KEY = 63 "pref_add_icon_to_home_initialized"; 64 65 @Override onReceive(Context context, Intent intent)66 public void onReceive(Context context, Intent intent) { 67 if (!isEnabled(context) || !Utilities.ATLEAST_OREO) { 68 // User has decided to not add icons on homescreen. 69 return; 70 } 71 72 SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION); 73 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER); 74 if (!PackageInstaller.ACTION_SESSION_COMMITTED.equals(intent.getAction()) 75 || info == null || user == null) { 76 // Invalid intent. 77 return; 78 } 79 80 PackageInstallerCompat packageInstallerCompat = PackageInstallerCompat.getInstance(context); 81 if (TextUtils.isEmpty(info.getAppPackageName()) 82 || info.getInstallReason() != PackageManager.INSTALL_REASON_USER 83 || packageInstallerCompat.promiseIconAddedForId(info.getSessionId())) { 84 packageInstallerCompat.removePromiseIconId(info.getSessionId()); 85 return; 86 } 87 88 queueAppIconAddition(context, info.getAppPackageName(), user); 89 } 90 queuePromiseAppIconAddition(Context context, SessionInfo sessionInfo)91 public static void queuePromiseAppIconAddition(Context context, SessionInfo sessionInfo) { 92 String packageName = sessionInfo.getAppPackageName(); 93 List<LauncherActivityInfo> activities = LauncherAppsCompat.getInstance(context) 94 .getActivityList(packageName, getUserHandle(sessionInfo)); 95 if (activities == null || activities.isEmpty()) { 96 // Ensure application isn't already installed. 97 queueAppIconAddition(context, packageName, sessionInfo.getAppLabel(), 98 sessionInfo.getAppIcon(), getUserHandle(sessionInfo)); 99 } 100 } 101 queueAppIconAddition(Context context, String packageName, UserHandle user)102 public static void queueAppIconAddition(Context context, String packageName, UserHandle user) { 103 List<LauncherActivityInfo> activities = LauncherAppsCompat.getInstance(context) 104 .getActivityList(packageName, user); 105 if (activities == null || activities.isEmpty()) { 106 // no activity found 107 return; 108 } 109 queueAppIconAddition(context, packageName, activities.get(0).getLabel(), null, user); 110 } 111 queueAppIconAddition(Context context, String packageName, CharSequence label, Bitmap icon, UserHandle user)112 private static void queueAppIconAddition(Context context, String packageName, 113 CharSequence label, Bitmap icon, UserHandle user) { 114 Intent data = new Intent(); 115 data.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setComponent( 116 new ComponentName(packageName, "")).setPackage(packageName)); 117 data.putExtra(Intent.EXTRA_SHORTCUT_NAME, label); 118 data.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon); 119 120 InstallShortcutReceiver.queueApplication(data, user, context); 121 } 122 isEnabled(Context context)123 public static boolean isEnabled(Context context) { 124 return Utilities.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true); 125 } 126 applyDefaultUserPrefs(final Context context)127 public static void applyDefaultUserPrefs(final Context context) { 128 if (!Utilities.ATLEAST_OREO) { 129 return; 130 } 131 SharedPreferences prefs = Utilities.getPrefs(context); 132 if (prefs.getAll().isEmpty()) { 133 // This logic assumes that the code is the first thing that is executed (before any 134 // shared preference is written). 135 // TODO: Move this logic to DB upgrade once we have proper support for db downgrade 136 // If it is a fresh start, just apply the default value. We use prefs.isEmpty() to infer 137 // a fresh start as put preferences always contain some values corresponding to current 138 // grid. 139 prefs.edit().putBoolean(ADD_ICON_PREFERENCE_KEY, true).apply(); 140 } else if (!prefs.contains(ADD_ICON_PREFERENCE_INITIALIZED_KEY)) { 141 new PrefInitTask(context).executeOnExecutor(Executors.THREAD_POOL_EXECUTOR); 142 } 143 } 144 145 private static class PrefInitTask extends AsyncTask<Void, Void, Void> { 146 private final Context mContext; 147 PrefInitTask(Context context)148 PrefInitTask(Context context) { 149 mContext = context; 150 } 151 152 @Override doInBackground(Void... voids)153 protected Void doInBackground(Void... voids) { 154 boolean addIconToHomeScreenEnabled = readValueFromMarketApp(); 155 Utilities.getPrefs(mContext).edit() 156 .putBoolean(ADD_ICON_PREFERENCE_KEY, addIconToHomeScreenEnabled) 157 .putBoolean(ADD_ICON_PREFERENCE_INITIALIZED_KEY, true) 158 .apply(); 159 return null; 160 } 161 readValueFromMarketApp()162 public boolean readValueFromMarketApp() { 163 // Get the marget package 164 ResolveInfo ri = mContext.getPackageManager().resolveActivity( 165 new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET), 166 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_SYSTEM_ONLY); 167 if (ri == null) { 168 return true; 169 } 170 171 Cursor c = null; 172 try { 173 c = mContext.getContentResolver().query( 174 Uri.parse("content://" + ri.activityInfo.packageName 175 + MARKER_PROVIDER_PREFIX), 176 null, null, null, null); 177 if (c.moveToNext()) { 178 return c.getInt(c.getColumnIndexOrThrow(Settings.NameValueTable.VALUE)) != 0; 179 } 180 } catch (Exception e) { 181 Log.d(TAG, "Error reading add to homescreen preference", e); 182 } finally { 183 if (c != null) { 184 c.close(); 185 } 186 } 187 return true; 188 } 189 } 190 } 191