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

Commit 0a4bbff8 authored by Mill Chen's avatar Mill Chen Committed by Automerger Merge Worker
Browse files

Merge "Add cache mechanism for secondary users in Storage" into tm-dev am: 34eecaad

parents b9ecb6ca 34eecaad
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -110,7 +110,7 @@ public class StorageCategoryFragment extends DashboardFragment
        if (mStorageCacheHelper.hasCachedSizeInfo() && mSelectedStorageEntry.isPrivate()) {
            StorageCacheHelper.StorageCache cachedData = mStorageCacheHelper.retrieveCachedSize();
            mPreferenceController.setVolume(mSelectedStorageEntry.getVolumeInfo());
            mPreferenceController.setUsedSize(cachedData.usedSize);
            mPreferenceController.setUsedSize(cachedData.totalUsedSize);
            mPreferenceController.setTotalSize(cachedData.totalSize);
        }
        if (mSelectedStorageEntry.isPrivate()) {
@@ -215,7 +215,8 @@ public class StorageCategoryFragment extends DashboardFragment
        mPreferenceController.setUsedSize(privateUsedBytes);
        mPreferenceController.setTotalSize(mStorageInfo.totalBytes);
        // Cache total size infor and used size info
        mStorageCacheHelper.cacheTotalSizeAndUsedSize(mStorageInfo.totalBytes, privateUsedBytes);
        mStorageCacheHelper
                .cacheTotalSizeAndTotalUsedSize(mStorageInfo.totalBytes, privateUsedBytes);
        for (int i = 0, size = mSecondaryUsers.size(); i < size; i++) {
            final AbstractPreferenceController controller = mSecondaryUsers.get(i);
            if (controller instanceof SecondaryUserController) {
+3 −2
Original line number Diff line number Diff line
@@ -240,7 +240,7 @@ public class StorageDashboardFragment extends DashboardFragment
        if (mStorageCacheHelper.hasCachedSizeInfo() && mSelectedStorageEntry.isPrivate()) {
            StorageCacheHelper.StorageCache cachedData = mStorageCacheHelper.retrieveCachedSize();
            mPreferenceController.setVolume(mSelectedStorageEntry.getVolumeInfo());
            mPreferenceController.setUsedSize(cachedData.usedSize);
            mPreferenceController.setUsedSize(cachedData.totalUsedSize);
            mPreferenceController.setTotalSize(cachedData.totalSize);
        }

@@ -388,7 +388,8 @@ public class StorageDashboardFragment extends DashboardFragment
        mPreferenceController.setUsedSize(privateUsedBytes);
        mPreferenceController.setTotalSize(mStorageInfo.totalBytes);
        // Cache total size and used size
        mStorageCacheHelper.cacheTotalSizeAndUsedSize(mStorageInfo.totalBytes, privateUsedBytes);
        mStorageCacheHelper
                .cacheTotalSizeAndTotalUsedSize(mStorageInfo.totalBytes, privateUsedBytes);
        for (int i = 0, size = mSecondaryUsers.size(); i < size; i++) {
            final AbstractPreferenceController controller = mSecondaryUsers.get(i);
            if (controller instanceof SecondaryUserController) {
+9 −7
Original line number Diff line number Diff line
@@ -46,7 +46,6 @@ public class SecondaryUserController extends AbstractPreferenceController implem
    // PreferenceGroupKey to try to add our preference onto.
    private static final String TARGET_PREFERENCE_GROUP_KEY = "pref_secondary_users";
    private static final String PREFERENCE_KEY_BASE = "pref_user_";
    private static final int USER_PROFILE_INSERTION_LOCATION = 6;
    private static final int SIZE_NOT_SET = -1;

    private @NonNull
@@ -58,6 +57,7 @@ public class SecondaryUserController extends AbstractPreferenceController implem
    private long mSize;
    private long mTotalSizeBytes;
    private boolean mIsVisible;
    private StorageCacheHelper mStorageCacheHelper;

    /**
     * Adds the appropriate controllers to a controller list for handling all secondary users on
@@ -110,6 +110,7 @@ public class SecondaryUserController extends AbstractPreferenceController implem
        super(context);
        mUser = info;
        mSize = SIZE_NOT_SET;
        mStorageCacheHelper = new StorageCacheHelper(context, info.id);
    }

    @Override
@@ -120,9 +121,7 @@ public class SecondaryUserController extends AbstractPreferenceController implem
            mPreferenceGroup = screen.findPreference(TARGET_PREFERENCE_GROUP_KEY);
            mStoragePreference.setTitle(mUser.name);
            mStoragePreference.setKey(PREFERENCE_KEY_BASE + mUser.id);
            if (mSize != SIZE_NOT_SET) {
                mStoragePreference.setStorageSize(mSize, mTotalSizeBytes);
            }
            setSize(mStorageCacheHelper.retrieveUsedSize(), false /* animate */);

            mPreferenceGroup.setVisible(mIsVisible);
            mPreferenceGroup.addPreference(mStoragePreference);
@@ -153,10 +152,10 @@ public class SecondaryUserController extends AbstractPreferenceController implem
     *
     * @param size Size in bytes.
     */
    public void setSize(long size) {
    public void setSize(long size, boolean animate) {
        mSize = size;
        if (mStoragePreference != null) {
            mStoragePreference.setStorageSize(mSize, mTotalSizeBytes);
            mStoragePreference.setStorageSize(mSize, mTotalSizeBytes, animate);
        }
    }

@@ -184,11 +183,14 @@ public class SecondaryUserController extends AbstractPreferenceController implem
    @Override
    public void handleResult(SparseArray<StorageAsyncLoader.StorageResult> stats) {
        if (stats == null) {
            setSize(mStorageCacheHelper.retrieveUsedSize(), false /* animate */);
            return;
        }
        final StorageAsyncLoader.StorageResult result = stats.get(getUser().id);
        if (result != null) {
            setSize(result.externalStats.totalBytes);
            setSize(result.externalStats.totalBytes, true /* animate */);
            // TODO(b/171758224): Update the source of size info
            mStorageCacheHelper.cacheUsedSize(result.externalStats.totalBytes);
        }
    }

+21 −6
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ public class StorageCacheHelper {

    private static final String SHARED_PREFERENCE_NAME = "StorageCache";
    private static final String TOTAL_SIZE_KEY = "total_size_key";
    private static final String USED_SIZE_KEY = "used_size_key";
    private static final String TOTAL_USED_SIZE_KEY = "total_used_size_key";
    private static final String IMAGES_SIZE_KEY = "images_size_key";
    private static final String VIDEOS_SIZE_KEY = "videos_size_key";
    private static final String AUDIO_SIZE_KEY = "audio_size_key";
@@ -35,6 +35,7 @@ public class StorageCacheHelper {
    private static final String DOCUMENTS_AND_OTHER_SIZE_KEY = "documents_and_other_size_key";
    private static final String TRASH_SIZE_KEY = "trash_size_key";
    private static final String SYSTEM_SIZE_KEY = "system_size_key";
    private static final String USED_SIZE_KEY = "used_size_key";

    private final SharedPreferences mSharedPreferences;

@@ -69,23 +70,37 @@ public class StorageCacheHelper {
    }

    /**
     * Cache total size and used size
     * Cache total size and total used size
     */
    public void cacheTotalSizeAndUsedSize(long totalSize, long usedSize) {
    public void cacheTotalSizeAndTotalUsedSize(long totalSize, long totalUsedSize) {
        mSharedPreferences
                .edit()
                .putLong(TOTAL_SIZE_KEY, totalSize)
                .putLong(USED_SIZE_KEY, usedSize)
                .putLong(TOTAL_USED_SIZE_KEY, totalUsedSize)
                .apply();
    }

    /**
     * Cache used size info when a user is treated as a secondary user.
     */
    public void cacheUsedSize(long usedSize) {
        mSharedPreferences.edit().putLong(USED_SIZE_KEY, usedSize).apply();
    }

    /**
     * Returns used size for secondary user.
     */
    public long retrieveUsedSize() {
        return mSharedPreferences.getLong(USED_SIZE_KEY, 0);
    }

    /**
     * Returns a cached data about all file size information.
     */
    public StorageCache retrieveCachedSize() {
        StorageCache result = new StorageCache();
        result.totalSize = mSharedPreferences.getLong(TOTAL_SIZE_KEY, 0);
        result.usedSize = mSharedPreferences.getLong(USED_SIZE_KEY, 0);
        result.totalUsedSize = mSharedPreferences.getLong(TOTAL_USED_SIZE_KEY, 0);
        result.imagesSize = mSharedPreferences.getLong(IMAGES_SIZE_KEY, 0);
        result.videosSize = mSharedPreferences.getLong(VIDEOS_SIZE_KEY, 0);
        result.audioSize = mSharedPreferences.getLong(AUDIO_SIZE_KEY, 0);
@@ -102,7 +117,7 @@ public class StorageCacheHelper {
     */
    public static class StorageCache {
        public long totalSize;
        public long usedSize;
        public long totalUsedSize;
        public long gamesSize;
        public long allAppsExceptGamesSize;
        public long audioSize;
+1 −1
Original line number Diff line number Diff line
@@ -78,7 +78,7 @@ public class StorageUsageProgressBarPreferenceController extends BasePreferenceC
        if (mStorageEntry != null && mStorageEntry.isMounted() && mStorageEntry.isPrivate()) {
            StorageCacheHelper.StorageCache cachedData = mStorageCacheHelper.retrieveCachedSize();
            mTotalBytes = cachedData.totalSize;
            mUsedBytes = cachedData.usedSize;
            mUsedBytes = cachedData.totalUsedSize;
            mIsUpdateStateFromSelectedStorageEntry = true;
            updateState(mUsageProgressBarPreference);
        }
Loading