Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 044c91cc authored by Arc Wang's avatar Arc Wang
Browse files

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
parent 24f2dda9
Loading
Loading
Loading
Loading
+11 −11
Original line number Diff line number Diff line
@@ -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;
        }
+11 −11
Original line number Diff line number Diff line
@@ -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;
        }
+9 −10
Original line number Diff line number Diff line
@@ -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 −24
Original line number Diff line number Diff line
@@ -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)
+3 −3
Original line number Diff line number Diff line
@@ -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);
        }
Loading