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.util.Executors; 34 35 import java.util.Locale; 36 37 /** 38 * BroadcastReceiver to handle session commit intent. 39 */ 40 public class SessionCommitReceiver extends BroadcastReceiver { 41 42 private static final String LOG = "SessionCommitReceiver"; 43 44 // Preference key for automatically adding icon to homescreen. 45 public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home"; 46 47 @Override onReceive(Context context, Intent intent)48 public void onReceive(Context context, Intent intent) { 49 Executors.MODEL_EXECUTOR.execute(() -> processIntent(context, intent)); 50 } 51 52 @WorkerThread processIntent(Context context, Intent intent)53 private static void processIntent(Context context, Intent intent) { 54 if (!isEnabled(context)) { 55 // User has decided to not add icons on homescreen. 56 return; 57 } 58 59 SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION); 60 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER); 61 if (!PackageInstaller.ACTION_SESSION_COMMITTED.equals(intent.getAction()) 62 || info == null || user == null) { 63 // Invalid intent. 64 return; 65 } 66 67 InstallSessionHelper packageInstallerCompat = InstallSessionHelper.INSTANCE.get(context); 68 boolean alreadyAddedPromiseIcon = 69 packageInstallerCompat.promiseIconAddedForId(info.getSessionId()); 70 if (TextUtils.isEmpty(info.getAppPackageName()) 71 || info.getInstallReason() != PackageManager.INSTALL_REASON_USER 72 || alreadyAddedPromiseIcon) { 73 FileLog.d(LOG, 74 String.format(Locale.ENGLISH, 75 "Removing PromiseIcon for package: %s, install reason: %d," 76 + " alreadyAddedPromiseIcon: %s", 77 info.getAppPackageName(), 78 info.getInstallReason(), 79 alreadyAddedPromiseIcon 80 ) 81 ); 82 packageInstallerCompat.removePromiseIconId(info.getSessionId()); 83 return; 84 } 85 86 FileLog.d(LOG, 87 "Adding package name to install queue. Package name: " + info.getAppPackageName() 88 + ", has app icon: " + (info.getAppIcon() != null) 89 + ", has app label: " + !TextUtils.isEmpty(info.getAppLabel())); 90 91 ItemInstallQueue.INSTANCE.get(context) 92 .queueItem(info.getAppPackageName(), user); 93 } 94 isEnabled(Context context)95 public static boolean isEnabled(Context context) { 96 return LauncherPrefs.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true); 97 } 98 } 99