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

Commit df229d26 authored by Uday Kumar Sundar's avatar Uday Kumar Sundar Committed by Steve Kondik
Browse files

Fix for framework reboot on format of SD card

MountService is not aware of /storage/emulated/legacy path which is
causing the issue on ExternalStorageFormatter service. We are getting
the a list of non-emulated SD cards and format the first one .

Change-Id: Iba1a9f6e40499f7f6fb095a5ee4e711b02bed439
CRs-fixed: 489037
parent 336f4148
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import com.google.android.collect.Lists;

/**
 * StorageManager is the interface to the systems storage service. The storage
@@ -645,4 +646,16 @@ public class StorageManager {
        return Settings.Global.getLong(mResolver, Settings.Global.SYS_STORAGE_FULL_THRESHOLD_BYTES,
                DEFAULT_FULL_THRESHOLD_BYTES);
    }

    /** {@hide} */
    public static ArrayList<StorageVolume> getPhysicalExternalVolume(StorageVolume[] volumesphy) {
        int count = volumesphy.length;
        ArrayList<StorageVolume> volumes = Lists.newArrayList();
        for (int i=0; i < count ; i++) {
            if (!volumesphy[i].isEmulated()) {
                volumes.add(volumesphy[i]);
            }
        }
        return volumes;
    }
}
+106 −20
Original line number Diff line number Diff line
@@ -20,6 +20,14 @@ import android.view.WindowManager;
import android.widget.Toast;

import com.android.internal.R;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * Takes care of unmounting and formatting external storage.
@@ -117,15 +125,29 @@ public class ExternalStorageFormatter extends Service
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCancel(DialogInterface dialog) {

        IMountService mountService = getMountService();
        String extStoragePath = mStorageVolume == null ?
                Environment.getLegacyExternalStorageDirectory().toString() :
                mStorageVolume.getPath();
        try {
            final StorageVolume[] volumes = mountService.getVolumeList();
            final ArrayList<StorageVolume> physicalVols = StorageManager.getPhysicalExternalVolume(volumes);
            String extStoragePath = null;
            // find external storage path if storage volume not specified
            if (mStorageVolume == null) {
                if (physicalVols.size() == 0) {
                        updateProgressDialog(R.string.progress_nomediapresent);
                } else {
                        final StorageVolume physicalVol = physicalVols.get(0);
                        extStoragePath = physicalVol.toString();
                        mountService.mountVolume(extStoragePath);
                }
            }
            //else use the specified storage volume
            else {
                extStoragePath = mStorageVolume.getPath();
                mountService.mountVolume(extStoragePath);
            }
        } catch (RemoteException e) {
            Log.w(TAG, "Failed talking with mount service", e);
        }
@@ -140,37 +162,93 @@ public class ExternalStorageFormatter extends Service
        stopSelf();
    }



    void updateProgressState() {
        String status = mStorageVolume == null ?
                Environment.getExternalStorageState() :
                mStorageManager.getVolumeState(mStorageVolume.getPath());
        String status = null;
        String extStoragePath = null;
        StorageVolume physicalVol;
        try {
            final IMountService mountService = getMountService();
            final StorageVolume[] volumes = mountService.getVolumeList();
            final ArrayList<StorageVolume> physicalVols = StorageManager.getPhysicalExternalVolume(volumes);
            // find external storage path if storage volume not specified
            if (mStorageVolume == null) {
                if (physicalVols.size() == 0) {
                    updateProgressDialog(R.string.progress_nomediapresent);
                    return;
                    } else {
                        physicalVol = physicalVols.get(0);
                        status = mStorageManager.getVolumeState(physicalVol.getPath()) ;
                    }
                }
                //else use the specified storage volume
                else {
                        status = mStorageManager.getVolumeState(mStorageVolume.getPath());
                }
        }
        catch (RemoteException e) {
                Log.w(TAG, "Failed talking with mount service", e);
        }
        if (Environment.MEDIA_MOUNTED.equals(status)
                || Environment.MEDIA_MOUNTED_READ_ONLY.equals(status)) {
            updateProgressDialog(R.string.progress_unmounting);
            IMountService mountService = getMountService();
            final String extStoragePath = mStorageVolume == null ?
                    Environment.getLegacyExternalStorageDirectory().toString() :
                    mStorageVolume.getPath();
            try {
                // Remove encryption mapping if this is an unmount for a factory reset.
                final IMountService mountService = getMountService();
                final StorageVolume[] volumes = mountService.getVolumeList();
                final ArrayList<StorageVolume> physicalVols = StorageManager.getPhysicalExternalVolume(volumes);
                // find external storage path if storage volume not specified
                if (mStorageVolume == null) {
                    if (physicalVols.size() == 0) {
                        updateProgressDialog(R.string.progress_nomediapresent);
                        return;
                    } else {
                        physicalVol = physicalVols.get(0);
                        extStoragePath = physicalVol.getPath();
                        Log.e(TAG, "physicalVol : " + physicalVol.toString());
                        mountService.unmountVolume(extStoragePath, true, mFactoryReset);
            } catch (RemoteException e) {
                    }
                }
                //else use the specified storage volume
                else {
                        extStoragePath = mStorageVolume.getPath();
                        mountService.unmountVolume(extStoragePath, true, mFactoryReset);
                }
            }
            catch (RemoteException e) {
                Log.w(TAG, "Failed talking with mount service", e);
            }
        } else if (Environment.MEDIA_NOFS.equals(status)
        }
        else if (Environment.MEDIA_NOFS.equals(status)
                || Environment.MEDIA_UNMOUNTED.equals(status)
                || Environment.MEDIA_UNMOUNTABLE.equals(status)) {
            updateProgressDialog(R.string.progress_erasing);
            final IMountService mountService = getMountService();
            final String extStoragePath = mStorageVolume == null ?
                    Environment.getLegacyExternalStorageDirectory().toString() :
                    mStorageVolume.getPath();
            if (mountService != null) {
                new Thread() {
                    @Override
                    public void run() {
                        boolean success = false;
                        StorageVolume physicalVol = null;
                        ArrayList<StorageVolume> physicalVols = null;
                        String extStoragePath = null;
                        try {
                            final StorageVolume[] volumes = mountService.getVolumeList();
                            physicalVols = StorageManager.getPhysicalExternalVolume(volumes);
                            // find external storage path if storage volume not specified
                            if (mStorageVolume == null) {
                                if (physicalVols.size() == 0) {
                                    updateProgressDialog(R.string.progress_nomediapresent);
                                    return;
                                } else {
                                    physicalVol = physicalVols.get(0);
                                    extStoragePath = physicalVol.getPath();
                                }
                            }
                            //else use the specified storage volume
                            else {
                                extStoragePath = mStorageVolume.getPath();
                            }
                            mountService.formatVolume(extStoragePath);
                            success = true;
                        } catch (Exception e) {
@@ -191,7 +269,15 @@ public class ExternalStorageFormatter extends Service
                            sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
                        } else {
                            try {
                                if(physicalVols.size() == 0) {
                                    updateProgressDialog(R.string.progress_nomediapresent);
                                    return;
                                } else {
                                    physicalVol = physicalVols.get(0);
                                    extStoragePath = mStorageVolume == null ?
                                        physicalVol.getPath() : mStorageVolume.getPath();
                                    mountService.mountVolume(extStoragePath);
                                }
                            } catch (RemoteException e) {
                                Log.w(TAG, "Failed talking with mount service", e);
                            }
+2 −0
Original line number Diff line number Diff line
@@ -3618,6 +3618,8 @@
    <string name="progress_unmounting" product="nosdcard">Unmounting USB storage\u2026</string>
    <!-- Text for progress dialog while unmounting SD card [CHAR LIMIT=NONE] -->
    <string name="progress_unmounting" product="default">Unmounting SD card\u2026</string>
    <!-- Text for progress dialog while formatting SD card [CHAR LIMIT=NONE] -->
    <string name="progress_nomediapresent" product="default">No removable media present\u2026</string>
    <!-- Text for progress dialog while erasing USB storage volume [CHAR LIMIT=NONE] -->
    <string name="progress_erasing" product="nosdcard">Erasing USB storage\u2026</string>
    <!-- Text for progress dialog while erasing SD card [CHAR LIMIT=NONE] -->
+1 −0
Original line number Diff line number Diff line
@@ -707,6 +707,7 @@
  <java-symbol type="string" name="progress_unmounting" />
  <java-symbol type="string" name="mobile_provisioning_url" />
  <java-symbol type="string" name="mobile_redirected_provisioning_url" />
  <java-symbol type="string" name="progress_nomediapresent" />
  <java-symbol type="string" name="reboot_safemode_confirm" />
  <java-symbol type="string" name="reboot_safemode_title" />
  <java-symbol type="string" name="relationTypeAssistant" />