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

Commit a5250c93 authored by Mike Lockwood's avatar Mike Lockwood
Browse files

MountService: Add StorageVolume as extra in storage related broadcasts.



Change-Id: I8e1a21ae233ba9812e58b363b59a66b260a01cbf
Signed-off-by: default avatarMike Lockwood <lockwood@android.com>
parent 2c7906eb
Loading
Loading
Loading
Loading
+5 −0
Original line number Original line Diff line number Diff line
@@ -36,6 +36,11 @@ public class StorageVolume implements Parcelable {
    private final int mMtpReserveSpace;
    private final int mMtpReserveSpace;
    private int mStorageId;
    private int mStorageId;


    // StorageVolume extra for ACTION_MEDIA_REMOVED, ACTION_MEDIA_UNMOUNTED, ACTION_MEDIA_CHECKING,
    // ACTION_MEDIA_NOFS, ACTION_MEDIA_MOUNTED, ACTION_MEDIA_SHARED, ACTION_MEDIA_UNSHARED,
    // ACTION_MEDIA_BAD_REMOVAL, ACTION_MEDIA_UNMOUNTABLE and ACTION_MEDIA_EJECT broadcasts.
    public static final String EXTRA_STORAGE_VOLUME = "storage_volume";

    public StorageVolume(String path, String description,
    public StorageVolume(String path, String description,
            boolean removable, boolean emulated, int mtpReserveSpace) {
            boolean removable, boolean emulated, int mtpReserveSpace) {
        mPath = path;
        mPath = path;
+36 −30
Original line number Original line Diff line number Diff line
@@ -159,6 +159,7 @@ class MountService extends IMountService.Stub implements INativeDaemonConnectorC
    private final ArrayList<StorageVolume>        mVolumes = new ArrayList<StorageVolume>();
    private final ArrayList<StorageVolume>        mVolumes = new ArrayList<StorageVolume>();
    private StorageVolume                         mPrimaryVolume;
    private StorageVolume                         mPrimaryVolume;
    private final HashMap<String, String>         mVolumeStates = new HashMap<String, String>();
    private final HashMap<String, String>         mVolumeStates = new HashMap<String, String>();
    private final HashMap<String, StorageVolume>  mVolumeMap = new HashMap<String, StorageVolume>();
    private String                                mExternalStoragePath;
    private String                                mExternalStoragePath;
    private PackageManagerService                 mPms;
    private PackageManagerService                 mPms;
    private boolean                               mUmsEnabling;
    private boolean                               mUmsEnabling;
@@ -669,8 +670,6 @@ class MountService extends IMountService.Stub implements INativeDaemonConnectorC
     * Callback from NativeDaemonConnector
     * Callback from NativeDaemonConnector
     */
     */
    public boolean onEvent(int code, String raw, String[] cooked) {
    public boolean onEvent(int code, String raw, String[] cooked) {
        Intent in = null;

        if (DEBUG_EVENTS) {
        if (DEBUG_EVENTS) {
            StringBuilder builder = new StringBuilder();
            StringBuilder builder = new StringBuilder();
            builder.append("onEvent::");
            builder.append("onEvent::");
@@ -705,6 +704,7 @@ class MountService extends IMountService.Stub implements INativeDaemonConnectorC
            // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
            // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
            // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
            // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
            // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
            // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
            String action = null;
            final String label = cooked[2];
            final String label = cooked[2];
            final String path = cooked[3];
            final String path = cooked[3];
            int major = -1;
            int major = -1;
@@ -743,32 +743,31 @@ class MountService extends IMountService.Stub implements INativeDaemonConnectorC
                /* Send the media unmounted event first */
                /* Send the media unmounted event first */
                if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
                if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
                updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
                updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
                in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
                sendStorageIntent(Environment.MEDIA_UNMOUNTED, path);
                mContext.sendBroadcast(in);


                if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
                if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
                updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
                updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
                in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
                action = Intent.ACTION_MEDIA_REMOVED;
            } else if (code == VoldResponseCode.VolumeBadRemoval) {
            } else if (code == VoldResponseCode.VolumeBadRemoval) {
                if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
                if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
                /* Send the media unmounted event first */
                /* Send the media unmounted event first */
                updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
                updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
                in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
                action = Intent.ACTION_MEDIA_UNMOUNTED;
                mContext.sendBroadcast(in);


                if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
                if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
                updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
                updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
                in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
                action = Intent.ACTION_MEDIA_BAD_REMOVAL;
            } else {
            } else {
                Slog.e(TAG, String.format("Unknown code {%d}", code));
                Slog.e(TAG, String.format("Unknown code {%d}", code));
            }
            }

            if (action != null) {
                sendStorageIntent(action, path);
            }
        } else {
        } else {
            return false;
            return false;
        }
        }


        if (in != null) {
            mContext.sendBroadcast(in);
        }
        return true;
        return true;
    }
    }


@@ -776,12 +775,11 @@ class MountService extends IMountService.Stub implements INativeDaemonConnectorC
        String vs = getVolumeState(path);
        String vs = getVolumeState(path);
        if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
        if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);


        Intent in = null;
        String action = null;


        if (oldState == VolumeState.Shared && newState != oldState) {
        if (oldState == VolumeState.Shared && newState != oldState) {
            if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
            if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
            mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
            sendStorageIntent(Intent.ACTION_MEDIA_UNSHARED,  path);
                                                Uri.parse("file://" + path)));
        }
        }


        if (newState == VolumeState.Init) {
        if (newState == VolumeState.Init) {
@@ -798,31 +796,29 @@ class MountService extends IMountService.Stub implements INativeDaemonConnectorC
                                    Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
                                    Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
                if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
                if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
                updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
                updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
                in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
                action = Intent.ACTION_MEDIA_UNMOUNTED;
            }
            }
        } else if (newState == VolumeState.Pending) {
        } else if (newState == VolumeState.Pending) {
        } else if (newState == VolumeState.Checking) {
        } else if (newState == VolumeState.Checking) {
            if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
            if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
            updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
            updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
            in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
            action = Intent.ACTION_MEDIA_CHECKING;
        } else if (newState == VolumeState.Mounted) {
        } else if (newState == VolumeState.Mounted) {
            if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
            if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
            updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
            updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
            in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
            action = Intent.ACTION_MEDIA_MOUNTED;
            in.putExtra("read-only", false);
        } else if (newState == VolumeState.Unmounting) {
        } else if (newState == VolumeState.Unmounting) {
            in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
            action = Intent.ACTION_MEDIA_EJECT;
        } else if (newState == VolumeState.Formatting) {
        } else if (newState == VolumeState.Formatting) {
        } else if (newState == VolumeState.Shared) {
        } else if (newState == VolumeState.Shared) {
            if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
            if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
            /* Send the media unmounted event first */
            /* Send the media unmounted event first */
            updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
            updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
            in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
            sendStorageIntent(Intent.ACTION_MEDIA_UNMOUNTED, path);
            mContext.sendBroadcast(in);


            if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
            if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
            updatePublicVolumeState(path, Environment.MEDIA_SHARED);
            updatePublicVolumeState(path, Environment.MEDIA_SHARED);
            in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
            action = Intent.ACTION_MEDIA_SHARED;
            if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
            if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
        } else if (newState == VolumeState.SharedMnt) {
        } else if (newState == VolumeState.SharedMnt) {
            Slog.e(TAG, "Live shared mounts not supported yet!");
            Slog.e(TAG, "Live shared mounts not supported yet!");
@@ -831,8 +827,8 @@ class MountService extends IMountService.Stub implements INativeDaemonConnectorC
            Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
            Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
        }
        }


        if (in != null) {
        if (action != null) {
            mContext.sendBroadcast(in);
            sendStorageIntent(action, path);
        }
        }
    }
    }


@@ -882,7 +878,7 @@ class MountService extends IMountService.Stub implements INativeDaemonConnectorC
            /*
            /*
             * Mount failed for some reason
             * Mount failed for some reason
             */
             */
            Intent in = null;
            String action = null;
            int code = e.getCode();
            int code = e.getCode();
            if (code == VoldResponseCode.OpFailedNoMedia) {
            if (code == VoldResponseCode.OpFailedNoMedia) {
                /*
                /*
@@ -895,7 +891,7 @@ class MountService extends IMountService.Stub implements INativeDaemonConnectorC
                 * Media is blank or does not contain a supported filesystem
                 * Media is blank or does not contain a supported filesystem
                 */
                 */
                updatePublicVolumeState(path, Environment.MEDIA_NOFS);
                updatePublicVolumeState(path, Environment.MEDIA_NOFS);
                in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
                action = Intent.ACTION_MEDIA_NOFS;
                rc = StorageResultCode.OperationFailedMediaBlank;
                rc = StorageResultCode.OperationFailedMediaBlank;
            } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
            } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
                if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
                if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
@@ -903,7 +899,7 @@ class MountService extends IMountService.Stub implements INativeDaemonConnectorC
                 * Volume consistency check failed
                 * Volume consistency check failed
                 */
                 */
                updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
                updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
                in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
                action = Intent.ACTION_MEDIA_UNMOUNTABLE;
                rc = StorageResultCode.OperationFailedMediaCorrupt;
                rc = StorageResultCode.OperationFailedMediaCorrupt;
            } else {
            } else {
                rc = StorageResultCode.OperationFailedInternalError;
                rc = StorageResultCode.OperationFailedInternalError;
@@ -912,8 +908,8 @@ class MountService extends IMountService.Stub implements INativeDaemonConnectorC
            /*
            /*
             * Send broadcast intent (if required for the failure)
             * Send broadcast intent (if required for the failure)
             */
             */
            if (in != null) {
            if (action != null) {
                mContext.sendBroadcast(in);
                sendStorageIntent(action, path);
            }
            }
        }
        }


@@ -1070,6 +1066,14 @@ class MountService extends IMountService.Stub implements INativeDaemonConnectorC
        }
        }
    }
    }


    private void sendStorageIntent(String action, String path) {
        Intent intent = new Intent(action, Uri.parse("file://" + path));
        // add StorageVolume extra
        intent.putExtra(StorageVolume.EXTRA_STORAGE_VOLUME, mVolumeMap.get(path));
        Slog.d(TAG, "sendStorageIntent " + intent);
        mContext.sendBroadcast(intent);
    }

    private void sendUmsIntent(boolean c) {
    private void sendUmsIntent(boolean c) {
        mContext.sendBroadcast(
        mContext.sendBroadcast(
                new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
                new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
@@ -1121,7 +1125,8 @@ class MountService extends IMountService.Stub implements INativeDaemonConnectorC
                    if (path == null || description == null) {
                    if (path == null || description == null) {
                        Slog.e(TAG, "path or description is null in readStorageList");
                        Slog.e(TAG, "path or description is null in readStorageList");
                    } else {
                    } else {
                        StorageVolume volume = new StorageVolume(path.toString(),
                        String pathString = path.toString();
                        StorageVolume volume = new StorageVolume(pathString,
                                description.toString(), removable, emulated, mtpReserve);
                                description.toString(), removable, emulated, mtpReserve);
                        if (primary) {
                        if (primary) {
                            if (mPrimaryVolume == null) {
                            if (mPrimaryVolume == null) {
@@ -1136,6 +1141,7 @@ class MountService extends IMountService.Stub implements INativeDaemonConnectorC
                        } else {
                        } else {
                            mVolumes.add(volume);
                            mVolumes.add(volume);
                        }
                        }
                        mVolumeMap.put(pathString, volume);
                    }
                    }
                    a.recycle();
                    a.recycle();
                }
                }