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

Commit 144c5cde authored by Ari's avatar Ari Committed by Ricardo Cerqueira
Browse files

Fix for ambiguous and broken disk formatting dialog

This patch ensures that the path-string from StorageNotification is converted
to the appropriate StorageVolume so that it can be correctly passed to the
formatting class. It also displays this path to the user so that it is clear
what will be deleted.

While trying to use a USB flash drive over OTG, the drive's FS somehow got
corrupted, so Android gave me a notification to re-format it. Okay, so then
a dialog pops up that doesn't specify what volume/device it will format, and
I incorrectly assumed it would be the USb drive. However this activity defaults
to the main internal memory for some reason, and wiped my data. Thank God for
them internets... (Dropbox)

Change-Id: I5621dff3cfd12a173b0d793398cf729333f97263
parent 38fe41a5
Loading
Loading
Loading
Loading
+32 −4
Original line number Diff line number Diff line
@@ -26,6 +26,8 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;

/**
 * This activity is shown to the user to confirm formatting of external media.
@@ -34,6 +36,10 @@ import android.util.Log;
public class ExternalMediaFormatActivity extends AlertActivity implements DialogInterface.OnClickListener {

    private static final int POSITIVE_BUTTON = AlertDialog.BUTTON_POSITIVE;
    public static final String FORMAT_PATH = "format_path";

    private StorageManager mStorageManager;
    private StorageVolume mStorageVolume = null;

    /** Used to detect when the media state changes, in case we need to call finish() */
    private BroadcastReceiver mStorageReceiver = new BroadcastReceiver() {
@@ -55,12 +61,33 @@ public class ExternalMediaFormatActivity extends AlertActivity implements Dialog
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // This is necessary because this class's caller,
        // packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java,
        // supplies the path to be erased/formatted as a String, instead of a
        // StorageVolume. This for-loop gets the correct StorageVolume from the
        // given path.
        mStorageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
        String path = getIntent().getStringExtra(FORMAT_PATH);
        StorageVolume[] volumes = mStorageManager.getVolumeList();

        for (StorageVolume sv : volumes) {
            if (path.equals(sv.getPath())) {
                mStorageVolume = sv;
                break;
            }
        }

        Log.d("ExternalMediaFormatActivity", "onCreate!");
        Log.d("ExternalMediaFormatActivity", "The storage volume to be formatted is : "
                + mStorageVolume.getPath());

        // Set up the "dialog"
        final AlertController.AlertParams p = mAlertParams;
        p.mIconId = com.android.internal.R.drawable.stat_sys_warning;
        p.mTitle = getString(com.android.internal.R.string.extmedia_format_title);
        p.mMessage = getString(com.android.internal.R.string.extmedia_format_message);
        p.mMessage = String.format(
                getString(com.android.internal.R.string.extmedia_format_message),
                mStorageVolume.getPath());
        p.mPositiveButtonText = getString(com.android.internal.R.string.extmedia_format_button_format);
        p.mPositiveButtonListener = this;
        p.mNegativeButtonText = getString(com.android.internal.R.string.cancel);
@@ -95,6 +122,7 @@ public class ExternalMediaFormatActivity extends AlertActivity implements Dialog
        if (which == POSITIVE_BUTTON) {
            Intent intent = new Intent(ExternalStorageFormatter.FORMAT_ONLY);
            intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
            intent.putExtra(StorageVolume.EXTRA_STORAGE_VOLUME, mStorageVolume);
            startService(intent);
        }

+2 −2
Original line number Diff line number Diff line
@@ -3551,9 +3551,9 @@
    <!-- See EXTMEDIA_FORMAT.  EXTMEDIA_FORMAT_DIALOG:  After the user selects the notification, a dialog is shown asking if he wants to format the SD card.  This is the title. -->
    <string name="extmedia_format_title" product="default">Format SD card?</string>
    <!-- See EXTMEDIA_FORMAT.   This is the message. [CHAR LIMIT=NONE] -->
    <string name="extmedia_format_message" product="nosdcard">All files stored in your USB storage will be erased. This action can\'t be reversed!</string>
    <string name="extmedia_format_message" product="nosdcard">All files stored in your USB storage (under path \'%1$s\') will be erased. This action can\'t be reversed!</string>
    <!-- See EXTMEDIA_FORMAT.   This is the message. -->
    <string name="extmedia_format_message" product="default">All data on your card will be lost.</string>
    <string name="extmedia_format_message" product="default">All data on your card (under path \'%1$s\') will be erased. This action can\'t be reversed!</string>
    <!-- See EXTMEDIA_FORMAT.    This is the button text to format the sd card. -->
    <string name="extmedia_format_button_format">Format</string>

+4 −0
Original line number Diff line number Diff line
@@ -199,6 +199,8 @@ public class StorageNotification extends SystemUI {
             */
            Intent intent = new Intent();
            intent.setClass(mContext, com.android.internal.app.ExternalMediaFormatActivity.class);
            // send the volume's path through to the formatting activity
            intent.putExtra(com.android.internal.app.ExternalMediaFormatActivity.FORMAT_PATH, path);
            PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);

            setMediaStorageNotification(
@@ -213,6 +215,8 @@ public class StorageNotification extends SystemUI {
             */
            Intent intent = new Intent();
            intent.setClass(mContext, com.android.internal.app.ExternalMediaFormatActivity.class);
            // send the volume's path through to the formatting activity
            intent.putExtra(com.android.internal.app.ExternalMediaFormatActivity.FORMAT_PATH, path);
            PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);

            setMediaStorageNotification(