1 /* 2 * Copyright (C) 2016 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.model; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.LauncherApps; 23 import android.os.UserHandle; 24 25 import com.android.launcher3.LauncherModel; 26 import com.android.launcher3.util.ApplicationInfoWrapper; 27 import com.android.launcher3.util.PackageUserKey; 28 29 import java.util.ArrayList; 30 import java.util.Set; 31 32 /** 33 * Helper class to re-query app status when SD-card becomes available. 34 * 35 * During first load, just after reboot, some apps on sdcard might not be available immediately due 36 * to some race conditions in the system. We wait for ACTION_BOOT_COMPLETED and process such 37 * apps again. 38 */ 39 public class SdCardAvailableReceiver extends BroadcastReceiver { 40 41 private final LauncherModel mModel; 42 private final Context mContext; 43 private final Set<PackageUserKey> mPackages; 44 SdCardAvailableReceiver( Context context, LauncherModel model, Set<PackageUserKey> packages)45 public SdCardAvailableReceiver( 46 Context context, LauncherModel model, Set<PackageUserKey> packages) { 47 mContext = context; 48 mModel = model; 49 mPackages = packages; 50 } 51 52 @Override onReceive(Context context, Intent intent)53 public void onReceive(Context context, Intent intent) { 54 final LauncherApps launcherApps = context.getSystemService(LauncherApps.class); 55 for (PackageUserKey puk : mPackages) { 56 UserHandle user = puk.mUser; 57 58 final ArrayList<String> packagesRemoved = new ArrayList<>(); 59 final ArrayList<String> packagesUnavailable = new ArrayList<>(); 60 61 if (!launcherApps.isPackageEnabled(puk.mPackageName, user)) { 62 if (new ApplicationInfoWrapper(context, puk.mPackageName, user).isOnSdCard()) { 63 packagesUnavailable.add(puk.mPackageName); 64 } else { 65 packagesRemoved.add(puk.mPackageName); 66 } 67 } 68 if (!packagesRemoved.isEmpty()) { 69 mModel.newModelCallbacks().onPackagesRemoved(user, packagesRemoved); 70 } 71 if (!packagesUnavailable.isEmpty()) { 72 mModel.newModelCallbacks().onPackagesUnavailable( 73 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), 74 user, false); 75 } 76 } 77 78 // Unregister the broadcast receiver, just in case 79 mContext.unregisterReceiver(this); 80 } 81 } 82