Example usage for android.content Intent ACTION_MEDIA_EJECT

List of usage examples for android.content Intent ACTION_MEDIA_EJECT

Introduction

In this page you can find the example usage for android.content Intent ACTION_MEDIA_EJECT.

Prototype

String ACTION_MEDIA_EJECT

To view the source code for android.content Intent ACTION_MEDIA_EJECT.

Click Source Link

Document

Broadcast Action: User has expressed the desire to remove the external storage media.

Usage

From source file:org.mariotaku.harmony.MusicPlaybackService.java

/**
 * Registers an intent to listen for ACTION_MEDIA_EJECT notifications. The
 * intent will call closeExternalStorageFiles() if the external media is
 * going to be ejected, so applications can clean up any files they have
 * open./*from  w  w  w  .  ja v a2s.  co  m*/
 */
public void registerExternalStorageListener() {
    if (mUnmountReceiver == null) {
        mUnmountReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {

                String action = intent.getAction();
                if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
                    saveQueue(true);
                    mQueueIsSaveable = false;
                    closeExternalStorageFiles(intent.getData().getPath());
                } else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
                    mCardId = MusicUtils.getCardId(context);
                    reloadQueue();
                    mQueueIsSaveable = true;
                    notifyChange(BROADCAST_QUEUE_CHANGED);
                    notifyChange(BROADCAST_MEDIA_CHANGED);
                }
            }
        };
        final IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_MEDIA_EJECT);
        filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        filter.addDataScheme(ContentResolver.SCHEME_FILE);
        registerReceiver(mUnmountReceiver, filter);
    }
}

From source file:com.android.server.MountService.java

private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
    final StorageVolume volume;
    final String state;
    synchronized (mVolumesLock) {
        volume = mVolumesByPath.get(path);
        state = getVolumeState(path);/*from   w w  w . j  a  v a  2s.  com*/
    }

    if (DEBUG_EVENTS)
        Slog.i(TAG, "notifyVolumeStateChange::" + state);

    String action = null;

    if (oldState == VolumeState.Shared && newState != oldState) {
        if (LOCAL_LOGD)
            Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
        sendStorageIntent(Intent.ACTION_MEDIA_UNSHARED, volume, UserHandle.ALL);
    }

    if (newState == VolumeState.Init) {
    } else if (newState == VolumeState.NoMedia) {
        // NoMedia is handled via Disk Remove events
    } else if (newState == VolumeState.Idle) {
        /*
         * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
         * if we're in the process of enabling UMS
         */
        if (!state.equals(Environment.MEDIA_BAD_REMOVAL) && !state.equals(Environment.MEDIA_NOFS)
                && !state.equals(Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
            if (DEBUG_EVENTS)
                Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
            updatePublicVolumeState(volume, Environment.MEDIA_UNMOUNTED);
            action = Intent.ACTION_MEDIA_UNMOUNTED;
        }
    } else if (newState == VolumeState.Pending) {
    } else if (newState == VolumeState.Checking) {
        if (DEBUG_EVENTS)
            Slog.i(TAG, "updating volume state checking");
        updatePublicVolumeState(volume, Environment.MEDIA_CHECKING);
        action = Intent.ACTION_MEDIA_CHECKING;
    } else if (newState == VolumeState.Mounted) {
        if (DEBUG_EVENTS)
            Slog.i(TAG, "updating volume state mounted");
        updatePublicVolumeState(volume, Environment.MEDIA_MOUNTED);
        action = Intent.ACTION_MEDIA_MOUNTED;
    } else if (newState == VolumeState.Unmounting) {
        action = Intent.ACTION_MEDIA_EJECT;
    } else if (newState == VolumeState.Formatting) {
    } else if (newState == VolumeState.Shared) {
        if (DEBUG_EVENTS)
            Slog.i(TAG, "Updating volume state media mounted");
        /* Send the media unmounted event first */
        updatePublicVolumeState(volume, Environment.MEDIA_UNMOUNTED);
        sendStorageIntent(Intent.ACTION_MEDIA_UNMOUNTED, volume, UserHandle.ALL);

        if (DEBUG_EVENTS)
            Slog.i(TAG, "Updating media shared");
        updatePublicVolumeState(volume, Environment.MEDIA_SHARED);
        action = Intent.ACTION_MEDIA_SHARED;
        if (LOCAL_LOGD)
            Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
    } else if (newState == VolumeState.SharedMnt) {
        Slog.e(TAG, "Live shared mounts not supported yet!");
        return;
    } else {
        Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
    }

    if (action != null) {
        sendStorageIntent(action, volume, UserHandle.ALL);
    }
}

From source file:org.yammp.MusicPlaybackService.java

/**
 * Registers an intent to listen for ACTION_MEDIA_EJECT notifications. The
 * intent will call closeExternalStorageFiles() if the external media is
 * going to be ejected, so applications can clean up any files they have
 * open./*from ww w  .ja  va 2 s  .  com*/
 */
public void registerExternalStorageListener() {

    if (mUnmountReceiver == null) {
        mUnmountReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {

                String action = intent.getAction();
                if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
                    saveQueue(true);
                    mQueueIsSaveable = false;
                    closeExternalStorageFiles(intent.getData().getPath());
                } else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
                    mMediaMountedCount++;
                    mCardId = mUtils.getCardId();
                    reloadQueue();
                    mQueueIsSaveable = true;
                    notifyChange(BROADCAST_QUEUE_CHANGED);
                    notifyChange(BROADCAST_META_CHANGED);
                }
            }
        };
        IntentFilter iFilter = new IntentFilter();
        iFilter.addAction(Intent.ACTION_MEDIA_EJECT);
        iFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        iFilter.addDataScheme("file");
        registerReceiver(mUnmountReceiver, iFilter);
    }
}

From source file:org.brandroid.openmanager.activities.OpenExplorer.java

public void handleMediaReceiver() {
    storageReceiver = new BroadcastReceiver() {
        @Override/*from   w  w  w  .  j a  va2 s .c  o m*/
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            String data = intent.getDataString();
            final String path = data.replace("file://", "");
            if (action.equals(Intent.ACTION_MEDIA_MOUNTED))
                handleRefreshMedia(path, true, 10);
            else
                refreshBookmarks();
        }
    };

    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
    filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
    filter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
    filter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
    filter.addAction(Intent.ACTION_MEDIA_EJECT);
    filter.addDataScheme("file");
    registerReceiver(storageReceiver, filter);

    ContentObserver mDbObserver = new ContentObserver(mHandler) {
        @Override
        public void onChange(boolean selfChange) {
            //rebake(false, ImageManager.isMediaScannerScanning(
            //      getContentResolver()));
        }
    };

    getContentResolver().registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true,
            mDbObserver);
}