Query storage size instead of calculate size of installed APP
From android S, Storage Settings show files of images/videos/audios category instead of installed APP of each category. So it's necessary to change the way to calculate size information. This change also - StorageItemPreference shows changing storage size units instead of fixed GB. It helps UX for categories of only small size files. - Query media provider for size of Documents and others. - Query media provider for size of Trash. Bug: 170918505 Bug: 177892478 Bug: 179871408 Bug: 184379946 Bug: 186077224 Bug: 187128447 Test: atest com.android.settings.deviceinfo atest com.android.settings.deviceinfo.storage make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.deviceinfo make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.deviceinfo.storage manual visual Click each file category to count files size is the same as displayed in Storage Settings. Change-Id: I37c7b3a4b5860323cb55581b23a90f583f4af216
This commit is contained in:
@@ -70,7 +70,7 @@ import java.util.List;
|
||||
*/
|
||||
public class StorageCategoryFragment extends DashboardFragment
|
||||
implements
|
||||
LoaderManager.LoaderCallbacks<SparseArray<StorageAsyncLoader.AppsStorageResult>>,
|
||||
LoaderManager.LoaderCallbacks<SparseArray<StorageAsyncLoader.StorageResult>>,
|
||||
Preference.OnPreferenceClickListener {
|
||||
private static final String TAG = "StorageCategoryFrag";
|
||||
private static final String SUMMARY_PREF_KEY = "storage_summary";
|
||||
@@ -83,7 +83,7 @@ public class StorageCategoryFragment extends DashboardFragment
|
||||
private UserManager mUserManager;
|
||||
private StorageEntry mSelectedStorageEntry;
|
||||
private PrivateStorageInfo mStorageInfo;
|
||||
private SparseArray<StorageAsyncLoader.AppsStorageResult> mAppsResult;
|
||||
private SparseArray<StorageAsyncLoader.StorageResult> mAppsResult;
|
||||
private CachedStorageValuesHelper mCachedStorageValuesHelper;
|
||||
|
||||
private StorageItemPreferenceController mPreferenceController;
|
||||
@@ -232,7 +232,7 @@ public class StorageCategoryFragment extends DashboardFragment
|
||||
* Updates the secondary user controller sizes.
|
||||
*/
|
||||
private void updateSecondaryUserControllers(List<AbstractPreferenceController> controllers,
|
||||
SparseArray<StorageAsyncLoader.AppsStorageResult> stats) {
|
||||
SparseArray<StorageAsyncLoader.StorageResult> stats) {
|
||||
for (int i = 0, size = controllers.size(); i < size; i++) {
|
||||
final AbstractPreferenceController controller = controllers.get(i);
|
||||
if (controller instanceof StorageAsyncLoader.ResultHandler) {
|
||||
@@ -244,7 +244,7 @@ public class StorageCategoryFragment extends DashboardFragment
|
||||
}
|
||||
|
||||
@Override
|
||||
public Loader<SparseArray<StorageAsyncLoader.AppsStorageResult>> onCreateLoader(int id,
|
||||
public Loader<SparseArray<StorageAsyncLoader.StorageResult>> onCreateLoader(int id,
|
||||
Bundle args) {
|
||||
final Context context = getContext();
|
||||
return new StorageAsyncLoader(context, mUserManager,
|
||||
@@ -254,15 +254,15 @@ public class StorageCategoryFragment extends DashboardFragment
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<SparseArray<StorageAsyncLoader.AppsStorageResult>> loader,
|
||||
SparseArray<StorageAsyncLoader.AppsStorageResult> data) {
|
||||
public void onLoadFinished(Loader<SparseArray<StorageAsyncLoader.StorageResult>> loader,
|
||||
SparseArray<StorageAsyncLoader.StorageResult> data) {
|
||||
mAppsResult = data;
|
||||
maybeCacheFreshValues();
|
||||
onReceivedSizes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoaderReset(Loader<SparseArray<StorageAsyncLoader.AppsStorageResult>> loader) {
|
||||
public void onLoaderReset(Loader<SparseArray<StorageAsyncLoader.StorageResult>> loader) {
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -296,20 +296,20 @@ public class StorageCategoryFragment extends DashboardFragment
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public SparseArray<StorageAsyncLoader.AppsStorageResult> getAppsStorageResult() {
|
||||
public SparseArray<StorageAsyncLoader.StorageResult> getStorageResult() {
|
||||
return mAppsResult;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void setAppsStorageResult(SparseArray<StorageAsyncLoader.AppsStorageResult> info) {
|
||||
public void setStorageResult(SparseArray<StorageAsyncLoader.StorageResult> info) {
|
||||
mAppsResult = info;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void initializeCachedValues() {
|
||||
final PrivateStorageInfo info = mCachedStorageValuesHelper.getCachedPrivateStorageInfo();
|
||||
final SparseArray<StorageAsyncLoader.AppsStorageResult> loaderResult =
|
||||
mCachedStorageValuesHelper.getCachedAppsStorageResult();
|
||||
final SparseArray<StorageAsyncLoader.StorageResult> loaderResult =
|
||||
mCachedStorageValuesHelper.getCachedStorageResult();
|
||||
if (info == null || loaderResult == null) {
|
||||
return;
|
||||
}
|
||||
|
@@ -85,7 +85,7 @@ import java.util.List;
|
||||
@SearchIndexable
|
||||
public class StorageDashboardFragment extends DashboardFragment
|
||||
implements
|
||||
LoaderManager.LoaderCallbacks<SparseArray<StorageAsyncLoader.AppsStorageResult>>,
|
||||
LoaderManager.LoaderCallbacks<SparseArray<StorageAsyncLoader.StorageResult>>,
|
||||
Preference.OnPreferenceClickListener {
|
||||
private static final String TAG = "StorageDashboardFrag";
|
||||
private static final String SUMMARY_PREF_KEY = "storage_summary";
|
||||
@@ -100,7 +100,7 @@ public class StorageDashboardFragment extends DashboardFragment
|
||||
private final List<StorageEntry> mStorageEntries = new ArrayList<>();
|
||||
private StorageEntry mSelectedStorageEntry;
|
||||
private PrivateStorageInfo mStorageInfo;
|
||||
private SparseArray<StorageAsyncLoader.AppsStorageResult> mAppsResult;
|
||||
private SparseArray<StorageAsyncLoader.StorageResult> mAppsResult;
|
||||
private CachedStorageValuesHelper mCachedStorageValuesHelper;
|
||||
|
||||
private StorageItemPreferenceController mPreferenceController;
|
||||
@@ -414,7 +414,7 @@ public class StorageDashboardFragment extends DashboardFragment
|
||||
* Updates the secondary user controller sizes.
|
||||
*/
|
||||
private void updateSecondaryUserControllers(List<AbstractPreferenceController> controllers,
|
||||
SparseArray<StorageAsyncLoader.AppsStorageResult> stats) {
|
||||
SparseArray<StorageAsyncLoader.StorageResult> stats) {
|
||||
for (int i = 0, size = controllers.size(); i < size; i++) {
|
||||
final AbstractPreferenceController controller = controllers.get(i);
|
||||
if (controller instanceof StorageAsyncLoader.ResultHandler) {
|
||||
@@ -455,7 +455,7 @@ public class StorageDashboardFragment extends DashboardFragment
|
||||
};
|
||||
|
||||
@Override
|
||||
public Loader<SparseArray<StorageAsyncLoader.AppsStorageResult>> onCreateLoader(int id,
|
||||
public Loader<SparseArray<StorageAsyncLoader.StorageResult>> onCreateLoader(int id,
|
||||
Bundle args) {
|
||||
final Context context = getContext();
|
||||
return new StorageAsyncLoader(context, mUserManager,
|
||||
@@ -465,15 +465,15 @@ public class StorageDashboardFragment extends DashboardFragment
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<SparseArray<StorageAsyncLoader.AppsStorageResult>> loader,
|
||||
SparseArray<StorageAsyncLoader.AppsStorageResult> data) {
|
||||
public void onLoadFinished(Loader<SparseArray<StorageAsyncLoader.StorageResult>> loader,
|
||||
SparseArray<StorageAsyncLoader.StorageResult> data) {
|
||||
mAppsResult = data;
|
||||
maybeCacheFreshValues();
|
||||
onReceivedSizes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoaderReset(Loader<SparseArray<StorageAsyncLoader.AppsStorageResult>> loader) {
|
||||
public void onLoaderReset(Loader<SparseArray<StorageAsyncLoader.StorageResult>> loader) {
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -507,20 +507,20 @@ public class StorageDashboardFragment extends DashboardFragment
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public SparseArray<StorageAsyncLoader.AppsStorageResult> getAppsStorageResult() {
|
||||
public SparseArray<StorageAsyncLoader.StorageResult> getStorageResult() {
|
||||
return mAppsResult;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void setAppsStorageResult(SparseArray<StorageAsyncLoader.AppsStorageResult> info) {
|
||||
public void setStorageResult(SparseArray<StorageAsyncLoader.StorageResult> info) {
|
||||
mAppsResult = info;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void initializeCachedValues() {
|
||||
final PrivateStorageInfo info = mCachedStorageValuesHelper.getCachedPrivateStorageInfo();
|
||||
final SparseArray<StorageAsyncLoader.AppsStorageResult> loaderResult =
|
||||
mCachedStorageValuesHelper.getCachedAppsStorageResult();
|
||||
final SparseArray<StorageAsyncLoader.StorageResult> loaderResult =
|
||||
mCachedStorageValuesHelper.getCachedStorageResult();
|
||||
if (info == null || loaderResult == null) {
|
||||
return;
|
||||
}
|
||||
|
@@ -17,7 +17,8 @@
|
||||
package com.android.settings.deviceinfo;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.text.TextUtils;
|
||||
import android.text.format.Formatter;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.ProgressBar;
|
||||
|
||||
@@ -25,7 +26,6 @@ import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.utils.FileSizeFormatter;
|
||||
|
||||
public class StorageItemPreference extends Preference {
|
||||
public int userHandle;
|
||||
@@ -49,12 +49,8 @@ public class StorageItemPreference extends Preference {
|
||||
|
||||
public void setStorageSize(long size, long total) {
|
||||
mStorageSize = size;
|
||||
setSummary(
|
||||
FileSizeFormatter.formatFileSize(
|
||||
getContext(),
|
||||
size,
|
||||
getGigabyteSuffix(getContext().getResources()),
|
||||
FileSizeFormatter.GIGABYTE_IN_BYTES));
|
||||
setSummary(getStorageSummary(size));
|
||||
|
||||
if (total == 0) {
|
||||
mProgressPercent = 0;
|
||||
} else {
|
||||
@@ -82,7 +78,10 @@ public class StorageItemPreference extends Preference {
|
||||
super.onBindViewHolder(view);
|
||||
}
|
||||
|
||||
private static int getGigabyteSuffix(Resources res) {
|
||||
return res.getIdentifier("gigabyteShort", "string", "android");
|
||||
private String getStorageSummary(long bytes) {
|
||||
final Formatter.BytesResult result = Formatter.formatBytes(getContext().getResources(),
|
||||
bytes, Formatter.FLAG_SHORTER);
|
||||
return TextUtils.expandTemplate(getContext().getText(R.string.storage_size_large),
|
||||
result.value, result.units).toString();
|
||||
}
|
||||
}
|
||||
|
@@ -35,9 +35,11 @@ public class CachedStorageValuesHelper {
|
||||
public static final String FREE_BYTES_KEY = "free_bytes";
|
||||
public static final String TOTAL_BYTES_KEY = "total_bytes";
|
||||
public static final String GAME_APPS_SIZE_KEY = "game_apps_size";
|
||||
public static final String MUSIC_APPS_SIZE_KEY = "music_apps_size";
|
||||
public static final String VIDEO_APPS_SIZE_KEY = "video_apps_size";
|
||||
public static final String PHOTO_APPS_SIZE_KEY = "photo_apps_size";
|
||||
public static final String AUDIO_SIZE_KEY = "audio_size";
|
||||
public static final String VIDEOS_SIZE_KEY = "videos_size";
|
||||
public static final String IMAGES_SIZE_KEY = "images_size";
|
||||
public static final String DOCUMENTS_AND_OTHER_SIZE_KEY = "documents_and_other_size";
|
||||
public static final String TRASH_SIZE_KEY = "trash_size";
|
||||
public static final String OTHER_APPS_SIZE_KEY = "other_apps_size";
|
||||
public static final String CACHE_APPS_SIZE_KEY = "cache_apps_size";
|
||||
public static final String EXTERNAL_TOTAL_BYTES = "external_total_bytes";
|
||||
@@ -78,21 +80,27 @@ public class CachedStorageValuesHelper {
|
||||
return new PrivateStorageInfo(freeBytes, totalBytes);
|
||||
}
|
||||
|
||||
public SparseArray<StorageAsyncLoader.AppsStorageResult> getCachedAppsStorageResult() {
|
||||
/** Returns cached storage result or null if it's not available. */
|
||||
public SparseArray<StorageAsyncLoader.StorageResult> getCachedStorageResult() {
|
||||
if (!isDataValid()) {
|
||||
return null;
|
||||
}
|
||||
final long gamesSize = mSharedPreferences.getLong(GAME_APPS_SIZE_KEY, -1);
|
||||
final long musicAppsSize = mSharedPreferences.getLong(MUSIC_APPS_SIZE_KEY, -1);
|
||||
final long videoAppsSize = mSharedPreferences.getLong(VIDEO_APPS_SIZE_KEY, -1);
|
||||
final long photoAppSize = mSharedPreferences.getLong(PHOTO_APPS_SIZE_KEY, -1);
|
||||
final long otherAppsSize = mSharedPreferences.getLong(OTHER_APPS_SIZE_KEY, -1);
|
||||
final long audioSize = mSharedPreferences.getLong(AUDIO_SIZE_KEY, -1);
|
||||
final long videosSize = mSharedPreferences.getLong(VIDEOS_SIZE_KEY, -1);
|
||||
final long imagesSize = mSharedPreferences.getLong(IMAGES_SIZE_KEY, -1);
|
||||
final long documentsAndOtherSize =
|
||||
mSharedPreferences.getLong(DOCUMENTS_AND_OTHER_SIZE_KEY, -1);
|
||||
final long trashSize = mSharedPreferences.getLong(TRASH_SIZE_KEY, -1);
|
||||
final long allAppsExceptGamesSize = mSharedPreferences.getLong(OTHER_APPS_SIZE_KEY, -1);
|
||||
final long cacheSize = mSharedPreferences.getLong(CACHE_APPS_SIZE_KEY, -1);
|
||||
if (gamesSize < 0
|
||||
|| musicAppsSize < 0
|
||||
|| videoAppsSize < 0
|
||||
|| photoAppSize < 0
|
||||
|| otherAppsSize < 0
|
||||
|| audioSize < 0
|
||||
|| videosSize < 0
|
||||
|| imagesSize < 0
|
||||
|| documentsAndOtherSize < 0
|
||||
|| trashSize < 0
|
||||
|| allAppsExceptGamesSize < 0
|
||||
|| cacheSize < 0) {
|
||||
return null;
|
||||
}
|
||||
@@ -117,31 +125,34 @@ public class CachedStorageValuesHelper {
|
||||
externalVideoBytes,
|
||||
externalImageBytes,
|
||||
externalAppBytes);
|
||||
final StorageAsyncLoader.AppsStorageResult result =
|
||||
new StorageAsyncLoader.AppsStorageResult();
|
||||
final StorageAsyncLoader.StorageResult result = new StorageAsyncLoader.StorageResult();
|
||||
result.gamesSize = gamesSize;
|
||||
result.musicAppsSize = musicAppsSize;
|
||||
result.videoAppsSize = videoAppsSize;
|
||||
result.photosAppsSize = photoAppSize;
|
||||
result.otherAppsSize = otherAppsSize;
|
||||
result.audioSize = audioSize;
|
||||
result.videosSize = videosSize;
|
||||
result.imagesSize = imagesSize;
|
||||
result.documentsAndOtherSize = documentsAndOtherSize;
|
||||
result.trashSize = trashSize;
|
||||
result.allAppsExceptGamesSize = allAppsExceptGamesSize;
|
||||
result.cacheSize = cacheSize;
|
||||
result.externalStats = externalStats;
|
||||
final SparseArray<StorageAsyncLoader.AppsStorageResult> resultArray = new SparseArray<>();
|
||||
final SparseArray<StorageAsyncLoader.StorageResult> resultArray = new SparseArray<>();
|
||||
resultArray.append(mUserId, result);
|
||||
return resultArray;
|
||||
}
|
||||
|
||||
public void cacheResult(
|
||||
PrivateStorageInfo storageInfo, StorageAsyncLoader.AppsStorageResult result) {
|
||||
PrivateStorageInfo storageInfo, StorageAsyncLoader.StorageResult result) {
|
||||
mSharedPreferences
|
||||
.edit()
|
||||
.putLong(FREE_BYTES_KEY, storageInfo.freeBytes)
|
||||
.putLong(TOTAL_BYTES_KEY, storageInfo.totalBytes)
|
||||
.putLong(GAME_APPS_SIZE_KEY, result.gamesSize)
|
||||
.putLong(MUSIC_APPS_SIZE_KEY, result.musicAppsSize)
|
||||
.putLong(VIDEO_APPS_SIZE_KEY, result.videoAppsSize)
|
||||
.putLong(PHOTO_APPS_SIZE_KEY, result.photosAppsSize)
|
||||
.putLong(OTHER_APPS_SIZE_KEY, result.otherAppsSize)
|
||||
.putLong(AUDIO_SIZE_KEY, result.audioSize)
|
||||
.putLong(VIDEOS_SIZE_KEY, result.videosSize)
|
||||
.putLong(IMAGES_SIZE_KEY, result.imagesSize)
|
||||
.putLong(DOCUMENTS_AND_OTHER_SIZE_KEY, result.documentsAndOtherSize)
|
||||
.putLong(TRASH_SIZE_KEY, result.trashSize)
|
||||
.putLong(OTHER_APPS_SIZE_KEY, result.allAppsExceptGamesSize)
|
||||
.putLong(CACHE_APPS_SIZE_KEY, result.cacheSize)
|
||||
.putLong(EXTERNAL_TOTAL_BYTES, result.externalStats.totalBytes)
|
||||
.putLong(EXTERNAL_AUDIO_BYTES, result.externalStats.audioBytes)
|
||||
|
@@ -168,9 +168,9 @@ public class SecondaryUserController extends AbstractPreferenceController implem
|
||||
mTotalSizeBytes = totalSizeBytes;
|
||||
}
|
||||
|
||||
public void handleResult(SparseArray<StorageAsyncLoader.AppsStorageResult> stats) {
|
||||
int userId = getUser().id;
|
||||
StorageAsyncLoader.AppsStorageResult result = stats.get(userId);
|
||||
@Override
|
||||
public void handleResult(SparseArray<StorageAsyncLoader.StorageResult> stats) {
|
||||
final StorageAsyncLoader.StorageResult result = stats.get(getUser().id);
|
||||
if (result != null) {
|
||||
setSize(result.externalStats.totalBytes);
|
||||
}
|
||||
|
@@ -21,13 +21,20 @@ import static android.content.pm.ApplicationInfo.CATEGORY_GAME;
|
||||
import static android.content.pm.ApplicationInfo.CATEGORY_IMAGE;
|
||||
import static android.content.pm.ApplicationInfo.CATEGORY_VIDEO;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.provider.MediaStore;
|
||||
import android.provider.MediaStore.Files.FileColumns;
|
||||
import android.provider.MediaStore.MediaColumns;
|
||||
import android.util.ArraySet;
|
||||
import android.util.Log;
|
||||
import android.util.SparseArray;
|
||||
@@ -37,7 +44,6 @@ import com.android.settingslib.utils.AsyncLoaderCompat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -45,7 +51,7 @@ import java.util.List;
|
||||
* users
|
||||
*/
|
||||
public class StorageAsyncLoader
|
||||
extends AsyncLoaderCompat<SparseArray<StorageAsyncLoader.AppsStorageResult>> {
|
||||
extends AsyncLoaderCompat<SparseArray<StorageAsyncLoader.StorageResult>> {
|
||||
private UserManager mUserManager;
|
||||
private static final String TAG = "StorageAsyncLoader";
|
||||
|
||||
@@ -64,38 +70,81 @@ public class StorageAsyncLoader
|
||||
}
|
||||
|
||||
@Override
|
||||
public SparseArray<AppsStorageResult> loadInBackground() {
|
||||
return loadApps();
|
||||
public SparseArray<StorageResult> loadInBackground() {
|
||||
return getStorageResultsForUsers();
|
||||
}
|
||||
|
||||
private SparseArray<AppsStorageResult> loadApps() {
|
||||
private SparseArray<StorageResult> getStorageResultsForUsers() {
|
||||
mSeenPackages = new ArraySet<>();
|
||||
SparseArray<AppsStorageResult> result = new SparseArray<>();
|
||||
List<UserInfo> infos = mUserManager.getUsers();
|
||||
final SparseArray<StorageResult> results = new SparseArray<>();
|
||||
final List<UserInfo> infos = mUserManager.getUsers();
|
||||
|
||||
// Sort the users by user id ascending.
|
||||
Collections.sort(
|
||||
infos,
|
||||
new Comparator<UserInfo>() {
|
||||
@Override
|
||||
public int compare(UserInfo userInfo, UserInfo otherUser) {
|
||||
return Integer.compare(userInfo.id, otherUser.id);
|
||||
}
|
||||
});
|
||||
for (int i = 0, userCount = infos.size(); i < userCount; i++) {
|
||||
final UserInfo info = infos.get(i);
|
||||
result.put(info.id, getStorageResultForUser(info.id));
|
||||
Collections.sort(infos,
|
||||
(userInfo, otherUser) -> Integer.compare(userInfo.id, otherUser.id));
|
||||
|
||||
for (UserInfo info : infos) {
|
||||
final StorageResult result = getAppsAndGamesSize(info.id);
|
||||
|
||||
result.imagesSize = getFilesSize(info.id, MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
||||
null /* queryArgs */);
|
||||
result.videosSize = getFilesSize(info.id, MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
|
||||
null /* queryArgs */);
|
||||
result.audioSize = getFilesSize(info.id, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
null /* queryArgs */);
|
||||
|
||||
final Bundle documentsAndOtherQueryArgs = new Bundle();
|
||||
documentsAndOtherQueryArgs.putString(ContentResolver.QUERY_ARG_SQL_SELECTION,
|
||||
FileColumns.MEDIA_TYPE + "!=" + FileColumns.MEDIA_TYPE_IMAGE
|
||||
+ " AND " + FileColumns.MEDIA_TYPE + "!=" + FileColumns.MEDIA_TYPE_VIDEO
|
||||
+ " AND " + FileColumns.MEDIA_TYPE + "!=" + FileColumns.MEDIA_TYPE_AUDIO
|
||||
+ " AND " + FileColumns.MIME_TYPE + " IS NOT NULL");
|
||||
result.documentsAndOtherSize = getFilesSize(info.id,
|
||||
MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL),
|
||||
documentsAndOtherQueryArgs);
|
||||
|
||||
final Bundle trashQueryArgs = new Bundle();
|
||||
trashQueryArgs.putInt(MediaStore.QUERY_ARG_MATCH_TRASHED, MediaStore.MATCH_ONLY);
|
||||
result.trashSize = getFilesSize(info.id,
|
||||
MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL), trashQueryArgs);
|
||||
|
||||
results.put(info.id, result);
|
||||
}
|
||||
return result;
|
||||
return results;
|
||||
}
|
||||
|
||||
private AppsStorageResult getStorageResultForUser(int userId) {
|
||||
private long getFilesSize(int userId, Uri uri, Bundle queryArgs) {
|
||||
final Context perUserContext;
|
||||
try {
|
||||
perUserContext = getContext().createPackageContextAsUser(
|
||||
getContext().getApplicationContext().getPackageName(),
|
||||
0 /* flags= */,
|
||||
UserHandle.of(userId));
|
||||
} catch (NameNotFoundException e) {
|
||||
Log.e(TAG, "Not able to get Context for user ID " + userId);
|
||||
return 0;
|
||||
}
|
||||
|
||||
try (Cursor cursor = perUserContext.getContentResolver().query(
|
||||
uri,
|
||||
new String[] {"sum(" + MediaColumns.SIZE + ")"},
|
||||
queryArgs,
|
||||
null /* cancellationSignal */)) {
|
||||
if (cursor == null) {
|
||||
return 0;
|
||||
}
|
||||
return cursor.moveToFirst() ? cursor.getInt(0) : 0;
|
||||
}
|
||||
}
|
||||
|
||||
private StorageResult getAppsAndGamesSize(int userId) {
|
||||
Log.d(TAG, "Loading apps");
|
||||
List<ApplicationInfo> applicationInfos =
|
||||
final List<ApplicationInfo> applicationInfos =
|
||||
mPackageManager.getInstalledApplicationsAsUser(0, userId);
|
||||
AppsStorageResult result = new AppsStorageResult();
|
||||
UserHandle myUser = UserHandle.of(userId);
|
||||
final StorageResult result = new StorageResult();
|
||||
final UserHandle myUser = UserHandle.of(userId);
|
||||
for (int i = 0, size = applicationInfos.size(); i < size; i++) {
|
||||
ApplicationInfo app = applicationInfos.get(i);
|
||||
final ApplicationInfo app = applicationInfos.get(i);
|
||||
|
||||
StorageStatsSource.AppStorageStats stats;
|
||||
try {
|
||||
@@ -131,28 +180,9 @@ public class StorageAsyncLoader
|
||||
result.gamesSize += blamedSize;
|
||||
break;
|
||||
case CATEGORY_AUDIO:
|
||||
// TODO(b/170918505): Should revamp audio size calculation with the data
|
||||
// from media provider.
|
||||
result.musicAppsSize += blamedSize;
|
||||
result.musicAppsSize -= stats.getCodeBytes();
|
||||
|
||||
result.otherAppsSize += blamedSize;
|
||||
break;
|
||||
case CATEGORY_VIDEO:
|
||||
// TODO(b/170918505): Should revamp video size calculation with the data
|
||||
// from media provider.
|
||||
result.videoAppsSize += blamedSize;
|
||||
result.videoAppsSize -= stats.getCodeBytes();
|
||||
|
||||
result.otherAppsSize += blamedSize;
|
||||
break;
|
||||
case CATEGORY_IMAGE:
|
||||
// TODO(b/170918505): Should revamp image size calculation with the data
|
||||
// from media provider.
|
||||
result.photosAppsSize += blamedSize;
|
||||
result.photosAppsSize -= stats.getCodeBytes();
|
||||
|
||||
result.otherAppsSize += blamedSize;
|
||||
result.allAppsExceptGamesSize += blamedSize;
|
||||
break;
|
||||
default:
|
||||
// The deprecated game flag does not set the category.
|
||||
@@ -160,7 +190,7 @@ public class StorageAsyncLoader
|
||||
result.gamesSize += blamedSize;
|
||||
break;
|
||||
}
|
||||
result.otherAppsSize += blamedSize;
|
||||
result.allAppsExceptGamesSize += blamedSize;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -177,15 +207,22 @@ public class StorageAsyncLoader
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDiscardResult(SparseArray<AppsStorageResult> result) {
|
||||
protected void onDiscardResult(SparseArray<StorageResult> result) {
|
||||
}
|
||||
|
||||
public static class AppsStorageResult {
|
||||
/** Storage result for displaying file categories size in Storage Settings. */
|
||||
public static class StorageResult {
|
||||
// APP based sizes.
|
||||
public long gamesSize;
|
||||
public long musicAppsSize;
|
||||
public long photosAppsSize;
|
||||
public long videoAppsSize;
|
||||
public long otherAppsSize;
|
||||
public long allAppsExceptGamesSize;
|
||||
|
||||
// File based sizes.
|
||||
public long audioSize;
|
||||
public long imagesSize;
|
||||
public long videosSize;
|
||||
public long documentsAndOtherSize;
|
||||
public long trashSize;
|
||||
|
||||
public long cacheSize;
|
||||
public long duplicateCodeSize;
|
||||
public StorageStatsSource.ExternalStorageStats externalStats;
|
||||
@@ -196,6 +233,7 @@ public class StorageAsyncLoader
|
||||
* {@link StorageAsyncLoader}.
|
||||
*/
|
||||
public interface ResultHandler {
|
||||
void handleResult(SparseArray<AppsStorageResult> result);
|
||||
/** Overrides this method to get storage result once it's available. */
|
||||
void handleResult(SparseArray<StorageResult> result);
|
||||
}
|
||||
}
|
||||
|
@@ -357,18 +357,18 @@ public class StorageItemPreferenceController extends AbstractPreferenceControlle
|
||||
updatePrivateStorageCategoryPreferencesOrder();
|
||||
}
|
||||
|
||||
public void onLoadFinished(SparseArray<StorageAsyncLoader.AppsStorageResult> result,
|
||||
/** Fragments use it to set storage result and update UI of this controller. */
|
||||
public void onLoadFinished(SparseArray<StorageAsyncLoader.StorageResult> result,
|
||||
int userId) {
|
||||
final StorageAsyncLoader.AppsStorageResult data = result.get(userId);
|
||||
final StorageAsyncLoader.StorageResult data = result.get(userId);
|
||||
|
||||
mImagesPreference.setStorageSize(getImagesSize(data), mTotalSize);
|
||||
mVideosPreference.setStorageSize(getVideosSize(data), mTotalSize);
|
||||
mAudioPreference.setStorageSize(getAudioSize(data), mTotalSize);
|
||||
mAppsPreference.setStorageSize(getAppsSize(data), mTotalSize);
|
||||
mGamesPreference.setStorageSize(getGamesSize(data), mTotalSize);
|
||||
mDocumentsAndOtherPreference.setStorageSize(getDocumentsAndOtherSize(data),
|
||||
mTotalSize);
|
||||
mTrashPreference.setStorageSize(getTrashSize(data), mTotalSize);
|
||||
mImagesPreference.setStorageSize(data.imagesSize, mTotalSize);
|
||||
mVideosPreference.setStorageSize(data.videosSize, mTotalSize);
|
||||
mAudioPreference.setStorageSize(data.audioSize, mTotalSize);
|
||||
mAppsPreference.setStorageSize(data.allAppsExceptGamesSize, mTotalSize);
|
||||
mGamesPreference.setStorageSize(data.gamesSize, mTotalSize);
|
||||
mDocumentsAndOtherPreference.setStorageSize(data.documentsAndOtherSize, mTotalSize);
|
||||
mTrashPreference.setStorageSize(data.trashSize, mTotalSize);
|
||||
|
||||
if (mSystemPreference != null) {
|
||||
// Everything else that hasn't already been attributed is tracked as
|
||||
@@ -377,13 +377,15 @@ public class StorageItemPreferenceController extends AbstractPreferenceControlle
|
||||
// from media provider.
|
||||
long attributedSize = 0;
|
||||
for (int i = 0; i < result.size(); i++) {
|
||||
final StorageAsyncLoader.AppsStorageResult otherData = result.valueAt(i);
|
||||
final StorageAsyncLoader.StorageResult otherData = result.valueAt(i);
|
||||
attributedSize +=
|
||||
otherData.gamesSize
|
||||
+ otherData.musicAppsSize
|
||||
+ otherData.videoAppsSize
|
||||
+ otherData.photosAppsSize
|
||||
+ otherData.otherAppsSize;
|
||||
+ otherData.audioSize
|
||||
+ otherData.videosSize
|
||||
+ otherData.imagesSize
|
||||
+ otherData.documentsAndOtherSize
|
||||
+ otherData.trashSize
|
||||
+ otherData.allAppsExceptGamesSize;
|
||||
attributedSize += otherData.externalStats.totalBytes
|
||||
- otherData.externalStats.appBytes;
|
||||
attributedSize -= otherData.duplicateCodeSize;
|
||||
@@ -418,18 +420,6 @@ public class StorageItemPreferenceController extends AbstractPreferenceControlle
|
||||
mContext.startActivityAsUser(intent, new UserHandle(mUserId));
|
||||
}
|
||||
|
||||
private long getImagesSize(StorageAsyncLoader.AppsStorageResult data) {
|
||||
return data.photosAppsSize + data.externalStats.imageBytes + data.externalStats.videoBytes;
|
||||
}
|
||||
|
||||
private long getVideosSize(StorageAsyncLoader.AppsStorageResult data) {
|
||||
return data.videoAppsSize;
|
||||
}
|
||||
|
||||
private long getAudioSize(StorageAsyncLoader.AppsStorageResult data) {
|
||||
return data.musicAppsSize + data.externalStats.audioBytes;
|
||||
}
|
||||
|
||||
private void launchAppsIntent() {
|
||||
final Bundle args = getWorkAnnotatedBundle(3);
|
||||
args.putString(ManageApplications.EXTRA_CLASSNAME,
|
||||
@@ -446,10 +436,6 @@ public class StorageItemPreferenceController extends AbstractPreferenceControlle
|
||||
Utils.launchIntent(mFragment, intent);
|
||||
}
|
||||
|
||||
private long getAppsSize(StorageAsyncLoader.AppsStorageResult data) {
|
||||
return data.otherAppsSize;
|
||||
}
|
||||
|
||||
private void launchGamesIntent() {
|
||||
final Bundle args = getWorkAnnotatedBundle(1);
|
||||
args.putString(ManageApplications.EXTRA_CLASSNAME,
|
||||
@@ -464,10 +450,6 @@ public class StorageItemPreferenceController extends AbstractPreferenceControlle
|
||||
Utils.launchIntent(mFragment, intent);
|
||||
}
|
||||
|
||||
private long getGamesSize(StorageAsyncLoader.AppsStorageResult data) {
|
||||
return data.gamesSize;
|
||||
}
|
||||
|
||||
private Bundle getWorkAnnotatedBundle(int additionalCapacity) {
|
||||
final Bundle args = new Bundle(1 + additionalCapacity);
|
||||
args.putInt(SettingsActivity.EXTRA_SHOW_FRAGMENT_TAB,
|
||||
@@ -475,14 +457,6 @@ public class StorageItemPreferenceController extends AbstractPreferenceControlle
|
||||
return args;
|
||||
}
|
||||
|
||||
private long getDocumentsAndOtherSize(StorageAsyncLoader.AppsStorageResult data) {
|
||||
return data.externalStats.totalBytes
|
||||
- data.externalStats.audioBytes
|
||||
- data.externalStats.videoBytes
|
||||
- data.externalStats.imageBytes
|
||||
- data.externalStats.appBytes;
|
||||
}
|
||||
|
||||
private void launchTrashIntent() {
|
||||
final Intent intent = new Intent("android.settings.VIEW_TRASH");
|
||||
|
||||
@@ -493,11 +467,6 @@ public class StorageItemPreferenceController extends AbstractPreferenceControlle
|
||||
}
|
||||
}
|
||||
|
||||
private long getTrashSize(StorageAsyncLoader.AppsStorageResult data) {
|
||||
// TODO(170918505): Implement it.
|
||||
return 0L;
|
||||
}
|
||||
|
||||
private static long totalValues(StorageMeasurement.MeasurementDetails details, int userId,
|
||||
String... keys) {
|
||||
long total = 0;
|
||||
|
Reference in New Issue
Block a user