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 import android.util.Log; 28 29 import androidx.annotation.WorkerThread; 30 31 import com.android.launcher3.logging.FileLog; 32 import com.android.launcher3.model.ItemInstallQueue; 33 import com.android.launcher3.pm.InstallSessionHelper; 34 import com.android.launcher3.testing.shared.TestProtocol; 35 import com.android.launcher3.util.Executors; 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 if (TestProtocol.sDebugTracing) { 57 Log.d(TestProtocol.MISSING_PROMISE_ICON, LOG + " not enabled"); 58 } 59 return; 60 } 61 62 SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION); 63 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER); 64 if (!PackageInstaller.ACTION_SESSION_COMMITTED.equals(intent.getAction()) 65 || info == null || user == null) { 66 // Invalid intent. 67 if (TestProtocol.sDebugTracing) { 68 Log.d(TestProtocol.MISSING_PROMISE_ICON, LOG + " invalid intent"); 69 } 70 return; 71 } 72 73 InstallSessionHelper packageInstallerCompat = InstallSessionHelper.INSTANCE.get(context); 74 packageInstallerCompat.restoreDbIfApplicable(info); 75 if (TextUtils.isEmpty(info.getAppPackageName()) 76 || info.getInstallReason() != PackageManager.INSTALL_REASON_USER 77 || packageInstallerCompat.promiseIconAddedForId(info.getSessionId())) { 78 packageInstallerCompat.removePromiseIconId(info.getSessionId()); 79 if (TestProtocol.sDebugTracing) { 80 int id = info.getSessionId(); 81 Log.d(TestProtocol.MISSING_PROMISE_ICON, LOG 82 + ", TextUtils.isEmpty=" + TextUtils.isEmpty(info.getAppPackageName()) 83 + ", info.getInstallReason()=" + info.getInstallReason() 84 + ", INSTALL_REASON_USER=" + PackageManager.INSTALL_REASON_USER 85 + ", icon added=" + packageInstallerCompat.promiseIconAddedForId(id) 86 ); 87 } 88 return; 89 } 90 91 FileLog.d(LOG, 92 "Adding package name to install queue. Package name: " + info.getAppPackageName() 93 + ", has app icon: " + (info.getAppIcon() != null) 94 + ", has app label: " + !TextUtils.isEmpty(info.getAppLabel())); 95 96 ItemInstallQueue.INSTANCE.get(context) 97 .queueItem(info.getAppPackageName(), user); 98 } 99 isEnabled(Context context)100 public static boolean isEnabled(Context context) { 101 return LauncherPrefs.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true); 102 } 103 } 104