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; 22 import android.content.pm.PackageInstaller; 23 import android.content.pm.PackageInstaller.SessionInfo; 24 import android.content.pm.PackageManager; 25 import android.os.UserHandle; 26 import android.text.TextUtils; 27 28 import androidx.annotation.WorkerThread; 29 30 import com.android.launcher3.logging.FileLog; 31 import com.android.launcher3.model.ItemInstallQueue; 32 import com.android.launcher3.pm.InstallSessionHelper; 33 import com.android.launcher3.pm.UserCache; 34 import com.android.launcher3.util.Executors; 35 36 import java.util.Locale; 37 38 /** 39 * BroadcastReceiver to handle session commit intent. 40 */ 41 public class SessionCommitReceiver extends BroadcastReceiver { 42 43 private static final String LOG = "SessionCommitReceiver"; 44 45 // Preference key for automatically adding icon to homescreen. 46 public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home"; 47 48 @Override onReceive(Context context, Intent intent)49 public void onReceive(Context context, Intent intent) { 50 Executors.MODEL_EXECUTOR.execute(() -> processIntent(context, intent)); 51 } 52 53 @WorkerThread processIntent(Context context, Intent intent)54 private static void processIntent(Context context, Intent intent) { 55 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER); 56 if (!isEnabled(context, user)) { 57 // User has decided to not add icons on homescreen. 58 return; 59 } 60 61 SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION); 62 if (!PackageInstaller.ACTION_SESSION_COMMITTED.equals(intent.getAction()) 63 || info == null || user == null) { 64 // Invalid intent. 65 return; 66 } 67 68 InstallSessionHelper packageInstallerCompat = InstallSessionHelper.INSTANCE.get(context); 69 boolean alreadyAddedPromiseIcon = 70 packageInstallerCompat.promiseIconAddedForId(info.getSessionId()); 71 if (TextUtils.isEmpty(info.getAppPackageName()) 72 || info.getInstallReason() != PackageManager.INSTALL_REASON_USER 73 || alreadyAddedPromiseIcon) { 74 FileLog.d(LOG, 75 String.format(Locale.ENGLISH, 76 "Removing PromiseIcon for package: %s, install reason: %d," 77 + " alreadyAddedPromiseIcon: %s", 78 info.getAppPackageName(), 79 info.getInstallReason(), 80 alreadyAddedPromiseIcon 81 ) 82 ); 83 packageInstallerCompat.removePromiseIconId(info.getSessionId()); 84 return; 85 } 86 87 FileLog.d(LOG, 88 "Adding package name to install queue. Package name: " + info.getAppPackageName() 89 + ", has app icon: " + (info.getAppIcon() != null) 90 + ", has app label: " + !TextUtils.isEmpty(info.getAppLabel())); 91 92 ItemInstallQueue.INSTANCE.get(context) 93 .queueItem(info.getAppPackageName(), user); 94 } 95 96 /** 97 * Returns whether adding Installed App Icons to home screen is allowed or not. 98 * Not allowed when: 99 * - User belongs to {@link com.android.launcher3.util.UserIconInfo.TYPE_PRIVATE} or 100 * - Home Settings preference to add App Icons on Home Screen is set as disabled 101 */ isEnabled(Context context, UserHandle user)102 public static boolean isEnabled(Context context, UserHandle user) { 103 if (Flags.privateSpaceRestrictItemDrag() && user != null 104 && UserCache.getInstance(context).getUserInfo(user).isPrivate()) { 105 return false; 106 } 107 return LauncherPrefs.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true); 108 } 109 } 110