blob: 0b51f9cc7b7cd62d394e7f03e8917a752a0183c4 [file] [log] [blame]
Daniel Nishic7d9de52016-12-12 15:16:00 -08001/*
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
17package com.android.server.storage;
18
Daniel Nishid54f3a42017-02-21 16:04:20 -080019import android.annotation.NonNull;
Daniel Nishid7b03292017-02-27 17:16:01 -080020import android.app.usage.StorageStats;
21import android.app.usage.StorageStatsManager;
Daniel Nishic7d9de52016-12-12 15:16:00 -080022import android.content.Context;
23import android.content.pm.ApplicationInfo;
Daniel Nishic7d9de52016-12-12 15:16:00 -080024import android.content.pm.PackageManager;
Jeff Sharkey789a8fc2017-04-16 13:18:35 -060025import android.content.pm.PackageManager.NameNotFoundException;
Daniel Nishic7d9de52016-12-12 15:16:00 -080026import android.content.pm.PackageStats;
27import android.content.pm.UserInfo;
28import android.os.Handler;
Daniel Nishic7d9de52016-12-12 15:16:00 -080029import android.os.Looper;
30import android.os.Message;
Daniel Nishic7d9de52016-12-12 15:16:00 -080031import android.os.UserManager;
32import android.os.storage.VolumeInfo;
33import android.util.Log;
Jeff Sharkey789a8fc2017-04-16 13:18:35 -060034
Daniel Nishic7d9de52016-12-12 15:16:00 -080035import com.android.internal.os.BackgroundThread;
Daniel Nishid54f3a42017-02-21 16:04:20 -080036import com.android.internal.util.Preconditions;
Daniel Nishic7d9de52016-12-12 15:16:00 -080037
Jeff Sharkey789a8fc2017-04-16 13:18:35 -060038import java.io.IOException;
Daniel Nishic7d9de52016-12-12 15:16:00 -080039import java.util.ArrayList;
40import java.util.List;
41import java.util.Objects;
42import java.util.concurrent.CompletableFuture;
43import java.util.concurrent.ExecutionException;
44import java.util.concurrent.TimeUnit;
45import java.util.concurrent.TimeoutException;
Daniel Nishic7d9de52016-12-12 15:16:00 -080046
47/**
48 * AppCollector asynchronously collects package sizes.
49 */
50public class AppCollector {
51 private static String TAG = "AppCollector";
52
53 private CompletableFuture<List<PackageStats>> mStats;
54 private final BackgroundHandler mBackgroundHandler;
55
56 /**
57 * Constrcuts a new AppCollector which runs on the provided volume.
58 * @param context Android context used to get
59 * @param volume Volume to check for apps.
60 */
Daniel Nishid54f3a42017-02-21 16:04:20 -080061 public AppCollector(Context context, @NonNull VolumeInfo volume) {
62 Preconditions.checkNotNull(volume);
63
Daniel Nishic7d9de52016-12-12 15:16:00 -080064 mBackgroundHandler = new BackgroundHandler(BackgroundThread.get().getLooper(),
65 volume,
66 context.getPackageManager(),
Daniel Nishid7b03292017-02-27 17:16:01 -080067 (UserManager) context.getSystemService(Context.USER_SERVICE),
68 (StorageStatsManager) context.getSystemService(Context.STORAGE_STATS_SERVICE));
Daniel Nishic7d9de52016-12-12 15:16:00 -080069 }
70
71 /**
72 * Returns a list of package stats for the context and volume. Note that in a multi-user
73 * environment, this may return stats for the same package multiple times. These "duplicate"
74 * entries will have the package stats for the package for a given user, not the package in
75 * aggregate.
76 * @param timeoutMillis Milliseconds before timing out and returning early with null.
77 */
78 public List<PackageStats> getPackageStats(long timeoutMillis) {
79 synchronized(this) {
80 if (mStats == null) {
81 mStats = new CompletableFuture<>();
82 mBackgroundHandler.sendEmptyMessage(BackgroundHandler.MSG_START_LOADING_SIZES);
83 }
84 }
85
86 List<PackageStats> value = null;
87 try {
88 value = mStats.get(timeoutMillis, TimeUnit.MILLISECONDS);
89 } catch (InterruptedException | ExecutionException e) {
90 Log.e(TAG, "An exception occurred while getting app storage", e);
91 } catch (TimeoutException e) {
92 Log.e(TAG, "AppCollector timed out");
93 }
94 return value;
95 }
96
Daniel Nishic7d9de52016-12-12 15:16:00 -080097 private class BackgroundHandler extends Handler {
98 static final int MSG_START_LOADING_SIZES = 0;
99 private final VolumeInfo mVolume;
100 private final PackageManager mPm;
101 private final UserManager mUm;
Daniel Nishid7b03292017-02-27 17:16:01 -0800102 private final StorageStatsManager mStorageStatsManager;
Daniel Nishic7d9de52016-12-12 15:16:00 -0800103
Daniel Nishid7b03292017-02-27 17:16:01 -0800104 BackgroundHandler(Looper looper, @NonNull VolumeInfo volume,
105 PackageManager pm, UserManager um, StorageStatsManager storageStatsManager) {
Daniel Nishic7d9de52016-12-12 15:16:00 -0800106 super(looper);
107 mVolume = volume;
108 mPm = pm;
109 mUm = um;
Daniel Nishid7b03292017-02-27 17:16:01 -0800110 mStorageStatsManager = storageStatsManager;
Daniel Nishic7d9de52016-12-12 15:16:00 -0800111 }
112
113 @Override
114 public void handleMessage(Message msg) {
115 switch (msg.what) {
116 case MSG_START_LOADING_SIZES: {
Daniel Nishid7b03292017-02-27 17:16:01 -0800117 List<PackageStats> stats = new ArrayList<>();
Daniel Nishi579e5582017-03-09 11:43:21 -0800118 List<UserInfo> users = mUm.getUsers();
119 for (int userCount = 0, userSize = users.size();
120 userCount < userSize; userCount++) {
121 UserInfo user = users.get(userCount);
122 final List<ApplicationInfo> apps = mPm.getInstalledApplicationsAsUser(
123 PackageManager.MATCH_DISABLED_COMPONENTS, user.id);
124
125 for (int appCount = 0, size = apps.size(); appCount < size; appCount++) {
126 ApplicationInfo app = apps.get(appCount);
127 if (!Objects.equals(app.volumeUuid, mVolume.getFsUuid())) {
128 continue;
129 }
130
131 try {
132 StorageStats storageStats =
Jeff Sharkey789a8fc2017-04-16 13:18:35 -0600133 mStorageStatsManager.queryStatsForPackage(app.storageUuid,
Daniel Nishi579e5582017-03-09 11:43:21 -0800134 app.packageName, user.getUserHandle());
135 PackageStats packageStats = new PackageStats(app.packageName,
136 user.id);
137 packageStats.cacheSize = storageStats.getCacheBytes();
Daniel Nishib6cc8382017-09-14 17:10:00 -0700138 packageStats.codeSize = storageStats.getAppBytes();
Daniel Nishi579e5582017-03-09 11:43:21 -0800139 packageStats.dataSize = storageStats.getDataBytes();
140 stats.add(packageStats);
Jeff Sharkey789a8fc2017-04-16 13:18:35 -0600141 } catch (NameNotFoundException | IOException e) {
Daniel Nishi579e5582017-03-09 11:43:21 -0800142 Log.e(TAG, "An exception occurred while fetching app size", e);
143 }
Daniel Nishic7d9de52016-12-12 15:16:00 -0800144 }
145 }
Daniel Nishid7b03292017-02-27 17:16:01 -0800146
147 mStats.complete(stats);
Daniel Nishic7d9de52016-12-12 15:16:00 -0800148 }
149 }
150 }
151 }
152}