1 /* 2 * Copyright (C) 2017 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.settings.deviceinfo.storage; 18 19 import static android.content.pm.ApplicationInfo.CATEGORY_AUDIO; 20 import static android.content.pm.ApplicationInfo.CATEGORY_GAME; 21 import static android.content.pm.ApplicationInfo.CATEGORY_VIDEO; 22 23 import android.content.Context; 24 import android.content.pm.ApplicationInfo; 25 import android.content.pm.PackageManager.NameNotFoundException; 26 import android.content.pm.UserInfo; 27 import android.os.UserHandle; 28 import android.util.ArraySet; 29 import android.util.Log; 30 import android.util.SparseArray; 31 32 import com.android.settings.applications.PackageManagerWrapper; 33 import com.android.settings.applications.UserManagerWrapper; 34 import com.android.settings.utils.AsyncLoader; 35 import com.android.settingslib.applications.StorageStatsSource; 36 37 import java.io.IOException; 38 import java.util.Collections; 39 import java.util.Comparator; 40 import java.util.List; 41 42 /** 43 * StorageAsyncLoader is a Loader which loads categorized app information and external stats for all 44 * users 45 */ 46 public class StorageAsyncLoader 47 extends AsyncLoader<SparseArray<StorageAsyncLoader.AppsStorageResult>> { 48 private UserManagerWrapper mUserManager; 49 private static final String TAG = "StorageAsyncLoader"; 50 51 private String mUuid; 52 private StorageStatsSource mStatsManager; 53 private PackageManagerWrapper mPackageManager; 54 private ArraySet<String> mSeenPackages; 55 StorageAsyncLoader(Context context, UserManagerWrapper userManager, String uuid, StorageStatsSource source, PackageManagerWrapper pm)56 public StorageAsyncLoader(Context context, UserManagerWrapper userManager, 57 String uuid, StorageStatsSource source, PackageManagerWrapper pm) { 58 super(context); 59 mUserManager = userManager; 60 mUuid = uuid; 61 mStatsManager = source; 62 mPackageManager = pm; 63 } 64 65 @Override loadInBackground()66 public SparseArray<AppsStorageResult> loadInBackground() { 67 return loadApps(); 68 } 69 loadApps()70 private SparseArray<AppsStorageResult> loadApps() { 71 mSeenPackages = new ArraySet<>(); 72 SparseArray<AppsStorageResult> result = new SparseArray<>(); 73 List<UserInfo> infos = mUserManager.getUsers(); 74 // Sort the users by user id ascending. 75 Collections.sort( 76 infos, 77 new Comparator<UserInfo>() { 78 @Override 79 public int compare(UserInfo userInfo, UserInfo otherUser) { 80 return Integer.compare(userInfo.id, otherUser.id); 81 } 82 }); 83 for (int i = 0, userCount = infos.size(); i < userCount; i++) { 84 UserInfo info = infos.get(i); 85 result.put(info.id, getStorageResultForUser(info.id)); 86 } 87 return result; 88 } 89 getStorageResultForUser(int userId)90 private AppsStorageResult getStorageResultForUser(int userId) { 91 Log.d(TAG, "Loading apps"); 92 List<ApplicationInfo> applicationInfos = 93 mPackageManager.getInstalledApplicationsAsUser(0, userId); 94 AppsStorageResult result = new AppsStorageResult(); 95 UserHandle myUser = UserHandle.of(userId); 96 for (int i = 0, size = applicationInfos.size(); i < size; i++) { 97 ApplicationInfo app = applicationInfos.get(i); 98 99 StorageStatsSource.AppStorageStats stats; 100 try { 101 stats = mStatsManager.getStatsForPackage(mUuid, app.packageName, myUser); 102 } catch (NameNotFoundException | IOException e) { 103 // This may happen if the package was removed during our calculation. 104 Log.w(TAG, "App unexpectedly not found", e); 105 continue; 106 } 107 108 final long dataSize = stats.getDataBytes(); 109 final long cacheQuota = mStatsManager.getCacheQuotaBytes(mUuid, app.uid); 110 final long cacheBytes = stats.getCacheBytes(); 111 long blamedSize = dataSize; 112 // Technically, we could overages as freeable on the storage settings screen. 113 // If the app is using more cache than its quota, we would accidentally subtract the 114 // overage from the system size (because it shows up as unused) during our attribution. 115 // Thus, we cap the attribution at the quota size. 116 if (cacheQuota < cacheBytes) { 117 blamedSize = blamedSize - cacheBytes + cacheQuota; 118 } 119 120 // This isn't quite right because it slams the first user by user id with the whole code 121 // size, but this ensures that we count all apps seen once. 122 if (!mSeenPackages.contains(app.packageName)) { 123 blamedSize += stats.getCodeBytes(); 124 mSeenPackages.add(app.packageName); 125 } 126 127 switch (app.category) { 128 case CATEGORY_GAME: 129 result.gamesSize += blamedSize; 130 break; 131 case CATEGORY_AUDIO: 132 result.musicAppsSize += blamedSize; 133 break; 134 case CATEGORY_VIDEO: 135 result.videoAppsSize += blamedSize; 136 break; 137 default: 138 // The deprecated game flag does not set the category. 139 if ((app.flags & ApplicationInfo.FLAG_IS_GAME) != 0) { 140 result.gamesSize += blamedSize; 141 break; 142 } 143 result.otherAppsSize += blamedSize; 144 break; 145 } 146 } 147 148 Log.d(TAG, "Loading external stats"); 149 try { 150 result.externalStats = mStatsManager.getExternalStorageStats(mUuid, 151 UserHandle.of(userId)); 152 } catch (IOException e) { 153 Log.w(TAG, e); 154 } 155 Log.d(TAG, "Obtaining result completed"); 156 return result; 157 } 158 159 @Override onDiscardResult(SparseArray<AppsStorageResult> result)160 protected void onDiscardResult(SparseArray<AppsStorageResult> result) { 161 } 162 163 public static class AppsStorageResult { 164 public long gamesSize; 165 public long musicAppsSize; 166 public long videoAppsSize; 167 public long otherAppsSize; 168 public long cacheSize; 169 public StorageStatsSource.ExternalStorageStats externalStats; 170 } 171 172 /** 173 * ResultHandler defines a destination of data which can handle a result from 174 * {@link StorageAsyncLoader}. 175 */ 176 public interface ResultHandler { handleResult(SparseArray<AppsStorageResult> result)177 void handleResult(SparseArray<AppsStorageResult> result); 178 } 179 } 180