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

Commit e98a5a99 authored by Arc Wang's avatar Arc Wang Committed by Android (Google) Code Review
Browse files

Merge "Only show storage category stats preferences for private volumes" into sc-dev

parents caf50d12 40396d53
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -248,13 +248,15 @@ public class StorageDashboardFragment extends DashboardFragment

        mPreferenceController.setVolume(mSelectedStorageEntry.getVolumeInfo());

        if (mSelectedStorageEntry.isMounted()) {
        if (mSelectedStorageEntry.isPrivate() && mSelectedStorageEntry.isMounted()) {
            // Stats data is only available on private volumes.
            getLoaderManager().restartLoader(STORAGE_JOB_ID, Bundle.EMPTY, this);
            getLoaderManager()
                 .restartLoader(VOLUME_SIZE_JOB_ID, Bundle.EMPTY, new VolumeSizeCallbacks());
            getLoaderManager().restartLoader(ICON_JOB_ID, Bundle.EMPTY, new IconLoaderCallbacks());
        } else {
            mPreferenceController.clearStorageSizeDisplay();
            // Set null volume to hide category stats.
            mPreferenceController.setVolume(null);
        }
    }

+22 −13
Original line number Diff line number Diff line
@@ -192,8 +192,28 @@ public class StorageItemPreferenceController extends AbstractPreferenceControlle
     */
    public void setVolume(VolumeInfo volume) {
        mVolume = volume;
        updateCategoryPreferencesVisibility();
    }

    private void updateCategoryPreferencesVisibility() {
        // Stats data is only available on private volumes.
        final boolean isValidVolume = mVolume != null
                && mVolume.getType() == VolumeInfo.TYPE_PRIVATE
                && (mVolume.getState() == VolumeInfo.STATE_MOUNTED
                || mVolume.getState() == VolumeInfo.STATE_MOUNTED_READ_ONLY);

        mPhotoPreference.setVisible(isValidVolume);
        mAudioPreference.setVisible(isValidVolume);
        mGamePreference.setVisible(isValidVolume);
        mMoviesPreference.setVisible(isValidVolume);
        mAppPreference.setVisible(isValidVolume);
        mFilePreference.setVisible(isValidVolume);
        mSystemPreference.setVisible(isValidVolume);

        if (isValidVolume) {
            setFilesPreferenceVisibility();
        }
    }

    private void setFilesPreferenceVisibility() {
        if (mScreen != null) {
@@ -251,7 +271,7 @@ public class StorageItemPreferenceController extends AbstractPreferenceControlle
        mSystemPreference = screen.findPreference(SYSTEM_KEY);
        mFilePreference = screen.findPreference(FILES_KEY);

        setFilesPreferenceVisibility();
        updateCategoryPreferencesVisibility();
    }

    public void onLoadFinished(SparseArray<StorageAsyncLoader.AppsStorageResult> result,
@@ -296,17 +316,6 @@ public class StorageItemPreferenceController extends AbstractPreferenceControlle
        mTotalSize = totalSizeBytes;
    }

    /** Set storage size to 0 for each preference. */
    public void clearStorageSizeDisplay() {
        mPhotoPreference.setStorageSize(0L, 0L);
        mAudioPreference.setStorageSize(0L, 0L);
        mGamePreference.setStorageSize(0L, 0L);
        mMoviesPreference.setStorageSize(0L, 0L);
        mAppPreference.setStorageSize(0L, 0L);
        mFilePreference.setStorageSize(0L, 0L);
        mSystemPreference.setStorageSize(0L, 0L);
    }

    /**
     * Returns a list of keys used by this preference controller.
     */
+3 −15
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ import com.android.settingslib.deviceinfo.PrivateStorageInfo;
import com.android.settingslib.deviceinfo.StorageVolumeProvider;
import com.android.settingslib.utils.AsyncLoaderCompat;

import java.io.File;
import java.io.IOException;

public class VolumeSizesLoader extends AsyncLoaderCompat<PrivateStorageInfo> {
@@ -50,11 +49,6 @@ public class VolumeSizesLoader extends AsyncLoaderCompat<PrivateStorageInfo> {

    @Override
    public PrivateStorageInfo loadInBackground() {
        if (mVolume == null || (mVolume.getState() != VolumeInfo.STATE_MOUNTED
                && mVolume.getState() != VolumeInfo.STATE_MOUNTED_READ_ONLY)) {
            return new PrivateStorageInfo(0L /* freeBytes */, 0L /* totalBytes */);
        }

        PrivateStorageInfo volumeSizes;
        try {
            volumeSizes = getVolumeSize(mVolumeProvider, mStats, mVolume);
@@ -68,14 +62,8 @@ public class VolumeSizesLoader extends AsyncLoaderCompat<PrivateStorageInfo> {
    static PrivateStorageInfo getVolumeSize(
            StorageVolumeProvider storageVolumeProvider, StorageStatsManager stats, VolumeInfo info)
            throws IOException {
        if (info.getType() == VolumeInfo.TYPE_PRIVATE) {
            return new PrivateStorageInfo(storageVolumeProvider.getFreeBytes(stats, info),
                    storageVolumeProvider.getTotalBytes(stats, info));
        }
        // TODO(b/174964885): It's confusing to use PrivateStorageInfo for a public storage,
        //                    replace it with a new naming or a different object.
        final File rootFile = info.getPath();
        return rootFile == null ? new PrivateStorageInfo(0L /* freeBytes */, 0L /* totalBytes */)
                : new PrivateStorageInfo(rootFile.getFreeSpace(), rootFile.getTotalSpace());
        long privateTotalBytes = storageVolumeProvider.getTotalBytes(stats, info);
        long privateFreeBytes = storageVolumeProvider.getFreeBytes(stats, info);
        return new PrivateStorageInfo(privateFreeBytes, privateTotalBytes);
    }
}
+1 −18
Original line number Diff line number Diff line
@@ -34,10 +34,8 @@ import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class VolumeSizesLoaderTest {
    @Test
    public void getVolumeSize_privateMountedVolume_getsValidSizes() throws Exception {
    public void getVolumeSize_getsValidSizes() throws Exception {
        VolumeInfo info = mock(VolumeInfo.class);
        when(info.getType()).thenReturn(VolumeInfo.TYPE_PRIVATE);
        when(info.getState()).thenReturn(VolumeInfo.STATE_MOUNTED);
        StorageVolumeProvider storageVolumeProvider = mock(StorageVolumeProvider.class);
        when(storageVolumeProvider.getTotalBytes(any(), any())).thenReturn(10000L);
        when(storageVolumeProvider.getFreeBytes(any(), any())).thenReturn(1000L);
@@ -48,19 +46,4 @@ public class VolumeSizesLoaderTest {
        assertThat(storageInfo.freeBytes).isEqualTo(1000L);
        assertThat(storageInfo.totalBytes).isEqualTo(10000L);
    }

    @Test
    public void getVolumeSize_unmountedVolume_getsValidSizes() throws Exception {
        VolumeInfo info = mock(VolumeInfo.class);
        when(info.getState()).thenReturn(VolumeInfo.STATE_UNMOUNTED);
        StorageVolumeProvider storageVolumeProvider = mock(StorageVolumeProvider.class);
        when(storageVolumeProvider.getTotalBytes(any(), any())).thenReturn(10000L);
        when(storageVolumeProvider.getFreeBytes(any(), any())).thenReturn(1000L);

        PrivateStorageInfo storageInfo =
                VolumeSizesLoader.getVolumeSize(storageVolumeProvider, null, info);

        assertThat(storageInfo.freeBytes).isEqualTo(0L);
        assertThat(storageInfo.totalBytes).isEqualTo(0L);
    }
}