Example usage for android.os Environment MEDIA_REMOVED

List of usage examples for android.os Environment MEDIA_REMOVED

Introduction

In this page you can find the example usage for android.os Environment MEDIA_REMOVED.

Prototype

String MEDIA_REMOVED

To view the source code for android.os Environment MEDIA_REMOVED.

Click Source Link

Document

Storage state if the media is not present.

Usage

From source file:org.opendatakit.utilities.ODKFileUtils.java

public static void verifyExternalStorageAvailability() {
    String cardstatus = Environment.getExternalStorageState();
    if (cardstatus.equals(Environment.MEDIA_REMOVED) || cardstatus.equals(Environment.MEDIA_UNMOUNTABLE)
            || cardstatus.equals(Environment.MEDIA_UNMOUNTED)
            || cardstatus.equals(Environment.MEDIA_MOUNTED_READ_ONLY)
            || cardstatus.equals(Environment.MEDIA_SHARED)) {
        throw new RuntimeException("ODK reports :: SDCard error: " + Environment.getExternalStorageState());
    }/*from   www  .  j  av a2s .co m*/
}

From source file:com.example.demo_highlights.slidingmenu.fragment.LeftFragment.java

public void getSDSize() {
    textView1.setText("");
    progressBar1.setProgress(0);//from  w  w  w  .  j  a v  a2 s . c  o m
    //
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        File path = Environment.getExternalStorageDirectory();
        //sdcard
        StatFs statfs = new StatFs(path.getPath());
        //blockSIZE
        long blocSize = statfs.getBlockSize();
        //BLOCK
        long totalBlocks = statfs.getBlockCount();
        //Block
        long availaBlock = statfs.getAvailableBlocks();

        String[] total = filesize(totalBlocks * blocSize);
        String[] availale = filesize(availaBlock * blocSize);
        // 
        int maxValue = Integer.parseInt(availale[0].replaceAll(",", "")) * progressBar1.getMax()
                / Integer.parseInt(total[0].replaceAll(",", ""));
        progressBar1.setProgress(100 - maxValue);
        String Text = "SD:"
                + (Integer.parseInt(total[0].replaceAll(",", ""))
                        - Integer.parseInt(availale[0].replaceAll(",", "")))
                + availale[1]/*availale[0].replaceAll(",", "")+availale[1]*/ + " / "
                + total[0].replaceAll(",", "") + total[1];
        textView1.setText(Text);

    } else if (Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)) {
        //          Toast.makeText(getActivity(), "sdCard", 1000).show();
        textView1.setText("SD??");
    }
}

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

/**
 * Callback from NativeDaemonConnector//from  w w  w .  j  av a  2 s  . c o  m
 */
public void onDaemonConnected() {
    /*
     * Since we'll be calling back into the NativeDaemonConnector,
     * we need to do our work in a new thread.
     */
    new Thread("MountService#onDaemonConnected") {
        @Override
        public void run() {
            /**
             * Determine media state and UMS detection status
             */
            try {
                final String[] vols = NativeDaemonEvent.filterMessageList(
                        mConnector.executeForList("volume", "list", "broadcast"),
                        VoldResponseCode.VolumeListResult);
                for (String volstr : vols) {
                    String[] tok = volstr.split(" ");
                    // FMT: <label> <mountpoint> <state>
                    String path = tok[1];
                    String state = Environment.MEDIA_REMOVED;

                    final StorageVolume volume;
                    synchronized (mVolumesLock) {
                        volume = mVolumesByPath.get(path);
                    }

                    int st = Integer.parseInt(tok[2]);
                    if (st == VolumeState.NoMedia) {
                        state = Environment.MEDIA_REMOVED;
                    } else if (st == VolumeState.Idle) {
                        state = Environment.MEDIA_UNMOUNTED;
                    } else if (st == VolumeState.Mounted) {
                        state = Environment.MEDIA_MOUNTED;
                        Slog.i(TAG, "Media already mounted on daemon connection");
                    } else if (st == VolumeState.Shared) {
                        state = Environment.MEDIA_SHARED;
                        Slog.i(TAG, "Media shared on daemon connection");
                    } else {
                        throw new Exception(String.format("Unexpected state %d", st));
                    }

                    if (state != null) {
                        if (DEBUG_EVENTS)
                            Slog.i(TAG, "Updating valid state " + state);
                        updatePublicVolumeState(volume, state);
                    }
                }
            } catch (Exception e) {
                Slog.e(TAG, "Error processing initial volume state", e);
                final StorageVolume primary = getPrimaryPhysicalVolume();
                if (primary != null) {
                    updatePublicVolumeState(primary, Environment.MEDIA_REMOVED);
                }
            }

            /*
             * Now that we've done our initialization, release
             * the hounds!
             */
            mConnectedSignal.countDown();

            // On an encrypted device we can't see system properties yet, so pull
            // the system locale out of the mount service.
            if ("".equals(SystemProperties.get("vold.encrypt_progress"))) {
                copyLocaleFromMountService();
            }

            // Let package manager load internal ASECs.
            mPms.scanAvailableAsecs();

            // Notify people waiting for ASECs to be scanned that it's done.
            mAsecsScanned.countDown();
        }
    }.start();
}

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

/**
 * Callback from NativeDaemonConnector/*from   w w  w.  j a  va 2 s . c  o m*/
 */
public boolean onEvent(int code, String raw, String[] cooked) {
    if (DEBUG_EVENTS) {
        StringBuilder builder = new StringBuilder();
        builder.append("onEvent::");
        builder.append(" raw= " + raw);
        if (cooked != null) {
            builder.append(" cooked = ");
            for (String str : cooked) {
                builder.append(" " + str);
            }
        }
        Slog.i(TAG, builder.toString());
    }
    if (code == VoldResponseCode.VolumeStateChange) {
        /*
         * One of the volumes we're managing has changed state.
         * Format: "NNN Volume <label> <path> state changed
         * from <old_#> (<old_str>) to <new_#> (<new_str>)"
         */
        notifyVolumeStateChange(cooked[2], cooked[3], Integer.parseInt(cooked[7]),
                Integer.parseInt(cooked[10]));
    } else if (code == VoldResponseCode.VolumeUuidChange) {
        // Format: nnn <label> <path> <uuid>
        final String path = cooked[2];
        final String uuid = (cooked.length > 3) ? cooked[3] : null;

        final StorageVolume vol = mVolumesByPath.get(path);
        if (vol != null) {
            vol.setUuid(uuid);
        }

    } else if (code == VoldResponseCode.VolumeUserLabelChange) {
        // Format: nnn <label> <path> <label>
        final String path = cooked[2];
        final String userLabel = (cooked.length > 3) ? cooked[3] : null;

        final StorageVolume vol = mVolumesByPath.get(path);
        if (vol != null) {
            vol.setUserLabel(userLabel);
        }

    } else if ((code == VoldResponseCode.VolumeDiskInserted) || (code == VoldResponseCode.VolumeDiskRemoved)
            || (code == VoldResponseCode.VolumeBadRemoval)) {
        // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
        // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
        // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
        String action = null;
        final String label = cooked[2];
        final String path = cooked[3];
        int major = -1;
        int minor = -1;

        try {
            String devComp = cooked[6].substring(1, cooked[6].length() - 1);
            String[] devTok = devComp.split(":");
            major = Integer.parseInt(devTok[0]);
            minor = Integer.parseInt(devTok[1]);
        } catch (Exception ex) {
            Slog.e(TAG, "Failed to parse major/minor", ex);
        }

        final StorageVolume volume;
        final String state;
        synchronized (mVolumesLock) {
            volume = mVolumesByPath.get(path);
            state = mVolumeStates.get(path);
        }

        if (code == VoldResponseCode.VolumeDiskInserted) {
            new Thread("MountService#VolumeDiskInserted") {
                @Override
                public void run() {
                    try {
                        int rc;
                        if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
                            Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
                        }
                    } catch (Exception ex) {
                        Slog.w(TAG, "Failed to mount media on insertion", ex);
                    }
                }
            }.start();
        } else if (code == VoldResponseCode.VolumeDiskRemoved) {
            /*
             * This event gets trumped if we're already in BAD_REMOVAL state
             */
            if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
                return true;
            }
            /* Send the media unmounted event first */
            if (DEBUG_EVENTS)
                Slog.i(TAG, "Sending unmounted event first");
            updatePublicVolumeState(volume, Environment.MEDIA_UNMOUNTED);
            sendStorageIntent(Intent.ACTION_MEDIA_UNMOUNTED, volume, UserHandle.ALL);

            if (DEBUG_EVENTS)
                Slog.i(TAG, "Sending media removed");
            updatePublicVolumeState(volume, Environment.MEDIA_REMOVED);
            action = Intent.ACTION_MEDIA_REMOVED;
        } else if (code == VoldResponseCode.VolumeBadRemoval) {
            if (DEBUG_EVENTS)
                Slog.i(TAG, "Sending unmounted event first");
            /* 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, "Sending media bad removal");
            updatePublicVolumeState(volume, Environment.MEDIA_BAD_REMOVAL);
            action = Intent.ACTION_MEDIA_BAD_REMOVAL;
        } else if (code == VoldResponseCode.FstrimCompleted) {
            EventLogTags.writeFstrimFinish(SystemClock.elapsedRealtime());
        } else {
            Slog.e(TAG, String.format("Unknown code {%d}", code));
        }

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

    return true;
}

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

/**
 * @return state of the volume at the specified mount point
 *//* w  ww . j a v  a  2  s.  c  o m*/
public String getVolumeState(String mountPoint) {
    synchronized (mVolumesLock) {
        String state = mVolumeStates.get(mountPoint);
        if (state == null) {
            Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
            if (SystemProperties.get("vold.encrypt_progress").length() != 0) {
                state = Environment.MEDIA_REMOVED;
            } else {
                throw new IllegalArgumentException();
            }
        }

        return state;
    }
}

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

public void unmountVolume(String path, boolean force, boolean removeEncryption) {
    validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
    waitForReady();/*  w ww  . j a v a 2s.  c  om*/

    String volState = getVolumeState(path);
    if (DEBUG_UNMOUNT) {
        Slog.i(TAG, "Unmounting " + path + " force = " + force + " removeEncryption = " + removeEncryption);
    }
    if (Environment.MEDIA_UNMOUNTED.equals(volState) || Environment.MEDIA_REMOVED.equals(volState)
            || Environment.MEDIA_SHARED.equals(volState) || Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
        // Media already unmounted or cannot be unmounted.
        // TODO return valid return code when adding observer call back.
        return;
    }
    UnmountCallBack ucb = new UnmountCallBack(path, force, removeEncryption);
    mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
}