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