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

Commit 1715fd6a authored by Jorge Ruesga's avatar Jorge Ruesga Committed by Gerrit Code Review
Browse files

Storage Settings: Allow user to trigger a volume rescan

This change allows to trigger a rescan of a volume with the next restrictions:
- Internal volumes if they are emulated
- Primary volumes
- Non-Removable volumes

Ported from: http://review.cyanogenmod.org/44586

Change-Id: I7e75a324938d8081fd715f702d50f1fdceabd96d
parent f6286935
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -25,12 +25,21 @@

    <com.android.settings.deviceinfo.PercentageBarChart
        android:id="@+id/percentage_bar_chart"
        android:layout_width="match_parent"
        android:layout_width="0dp"
        android:layout_height="32dip"
        android:layout_weight="1"
        android:minHeight="?android:attr/listPreferredItemHeight"
        android:gravity="center_vertical"
        settings:minTickWidth="6dip"
        settings:emptyColor="@color/material_empty_color_light">
    </com.android.settings.deviceinfo.PercentageBarChart>

    <view class="com.android.settings.deviceinfo.UsageBarPreference$RefreshAnimationView"
        android:id="@+id/memory_usage_rescan_media"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:padding="12dp"
        android:background="?android:attr/selectableItemBackgroundBorderless"
        android:contentDescription="@string/storage_rescan_media"/>

</LinearLayout>
+3 −0
Original line number Diff line number Diff line
@@ -834,6 +834,9 @@
    <string name="usb_media_format_gesture_explanation" product="default">You need to draw your unlock pattern to confirm that you want to erase the USB storage.</string>
    <string name="uicc_media_format_gesture_explanation" product="default">You need to draw your unlock pattern to confirm that you want to erase the UICC storage.</string>

    <!-- Rescan storage -->
    <string name="storage_rescan_media">Rescan media for the storage volume</string>

    <!-- [CHAR LIMIT=130] Location mode screen, sub category for IZat Location -->
    <string name="location_category_enhanced_location">Enhanced location (IZat)</string>

+55 −2
Original line number Diff line number Diff line
@@ -25,6 +25,9 @@ import android.content.pm.IPackageManager;
import android.content.pm.UserInfo;
import android.content.res.Resources;
import android.hardware.usb.UsbManager;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.OnScanCompletedListener;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
@@ -36,19 +39,26 @@ import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.provider.MediaStore;
import android.text.format.Formatter;
import android.util.Log;

import com.android.settings.MediaFormat;
import com.android.settings.R;
import com.android.settings.Settings;
import com.android.settings.deviceinfo.StorageMeasurement.MeasurementDetails;
import com.android.settings.deviceinfo.StorageMeasurement.MeasurementReceiver;
import com.android.settings.deviceinfo.UsageBarPreference.OnRequestMediaRescanListener;
import com.google.android.collect.Lists;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class StorageVolumePreferenceCategory extends PreferenceCategory {
public class StorageVolumePreferenceCategory extends PreferenceCategory
    implements OnRequestMediaRescanListener, OnScanCompletedListener {

    public static final String TAG = "StorageVolumePreferenceCategory";

    public static final String KEY_CACHE = "cache";

    private static final int ORDER_USAGE_BAR = -2;
@@ -58,6 +68,9 @@ public class StorageVolumePreferenceCategory extends PreferenceCategory {
    private final StorageVolume mVolume;
    private final StorageMeasurement mMeasure;

    private final boolean mIsInternal;
    private final boolean mIsPrimary;

    private final Resources mResources;
    private final StorageManager mStorageManager;
    private final UserManager mUserManager;
@@ -84,6 +97,7 @@ public class StorageVolumePreferenceCategory extends PreferenceCategory {

    private static final int MSG_UI_UPDATE_APPROXIMATE = 1;
    private static final int MSG_UI_UPDATE_DETAILS = 2;
    private static final int MSG_UI_RESCAN_COMPLETE = 3;

    private Handler mUpdateHandler = new Handler() {
        @Override
@@ -99,6 +113,10 @@ public class StorageVolumePreferenceCategory extends PreferenceCategory {
                    updateDetails(details);
                    break;
                }
                case MSG_UI_RESCAN_COMPLETE: {
                    mUsageBarPreference.notifyScanCompleted();
                    break;
                }
            }
        }
    };
@@ -124,6 +142,8 @@ public class StorageVolumePreferenceCategory extends PreferenceCategory {

        mVolume = volume;
        mMeasure = StorageMeasurement.getInstance(context, volume);
        mIsInternal = mVolume == null;
        mIsPrimary = mVolume != null ? mVolume.isPrimary() : false;

        mResources = context.getResources();
        mStorageManager = StorageManager.from(context);
@@ -152,8 +172,17 @@ public class StorageVolumePreferenceCategory extends PreferenceCategory {
        final List<UserInfo> otherUsers = getUsersExcluding(currentUser);
        final boolean showUsers = mVolume == null && otherUsers.size() > 0;

        boolean allowMediaScan = false;
        if ((mIsInternal && Environment.isExternalStorageEmulated()) || mIsPrimary) {
            allowMediaScan = true;
        } else if (mVolume != null && !mVolume.isRemovable()) {
            allowMediaScan = true;
        }

        mUsageBarPreference = new UsageBarPreference(context);
        mUsageBarPreference.setOrder(ORDER_USAGE_BAR);
        mUsageBarPreference.setOnRequestMediaRescanListener(this);
        mUsageBarPreference.setAllowMediaScan(allowMediaScan);
        addPreference(mUsageBarPreference);

        mItemTotal = buildItem(R.string.memory_size, 0);
@@ -455,6 +484,7 @@ public class StorageVolumePreferenceCategory extends PreferenceCategory {
        }

        mUsageBarPreference.commit();
        mUpdateHandler.obtainMessage(MSG_UI_RESCAN_COMPLETE).sendToTarget();
    }

    private void updatePreference(StorageItemPreference pref, long size) {
@@ -468,6 +498,7 @@ public class StorageVolumePreferenceCategory extends PreferenceCategory {
    }

    private void measure() {
        mUsageBarPreference.onMeasure();
        mMeasure.invalidate();
        mMeasure.measure();
    }
@@ -527,7 +558,6 @@ public class StorageVolumePreferenceCategory extends PreferenceCategory {
        // TODO The current "delete" story is not fully handled by the respective applications.
        // When it is done, make sure the intent types below are correct.
        // If that cannot be done, remove these intents.
        final String key = pref.getKey();
        if (pref == mFormatPreference) {
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setClass(getContext(), com.android.settings.MediaFormat.class);
@@ -585,4 +615,27 @@ public class StorageVolumePreferenceCategory extends PreferenceCategory {
        }
        return users;
    }

    @Override
    public void onRequestMediaRescan() {
        File path = null;
        if ((mIsInternal && Environment.isExternalStorageEmulated()) || mIsPrimary) {
            path = Environment.getExternalStorageDirectory();
        } else {
            path = mVolume.getPathFile();
        }

        Log.d(TAG, "Request scan of " + path.getAbsolutePath());
        MediaScannerConnection.scanFile(
                getContext(), new String[]{path.getAbsolutePath()}, null, this);
    }

    @Override
    public void onScanCompleted(String path, final Uri uri) {
        if (uri != null) {
            measure();
        } else {
            mUpdateHandler.obtainMessage(MSG_UI_RESCAN_COMPLETE).sendToTarget();
        }
    }
}
+524 −0

File changed.

Preview size limit exceeded, changes collapsed.