Example usage for android.content Intent ACTION_MEDIA_BAD_REMOVAL

List of usage examples for android.content Intent ACTION_MEDIA_BAD_REMOVAL

Introduction

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

Prototype

String ACTION_MEDIA_BAD_REMOVAL

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

Click Source Link

Document

Broadcast Action: External media was removed from SD card slot, but mount point was not unmounted.

Usage

From source file:org.chromium.ChromeSystemStorage.java

private static BackgroundEventHandler<ChromeSystemStorage> createEventHandler() {

    return new BackgroundEventHandler<ChromeSystemStorage>() {

        @Override//from  w  ww.  j av  a2 s.c om
        public BackgroundEventInfo mapBroadcast(Context context, Intent intent) {

            String action = intent.getAction();
            if (!(Intent.ACTION_MEDIA_MOUNTED.equals(action) || Intent.ACTION_MEDIA_BAD_REMOVAL.equals(action)
                    || Intent.ACTION_MEDIA_REMOVED.equals(action) || Intent.ACTION_MEDIA_SHARED.equals(action)
                    || Intent.ACTION_MEDIA_UNMOUNTED.equals(action))) {
                // Ignore any other actions
                return null;
            }

            BackgroundEventInfo event = new BackgroundEventInfo(action);
            event.getData().putString(DATA_STORAGE_PATH, intent.getDataString());

            return event;
        }

        @Override
        public void mapEventToMessage(BackgroundEventInfo event, JSONObject message) throws JSONException {
            boolean attached = Intent.ACTION_MEDIA_MOUNTED.equals(event.action);

            // Sanitize the path provided with the event
            String storagePath = getBaseStoragePath(
                    Uri.parse(event.getData().getString(DATA_STORAGE_PATH)).getPath());

            ChromeSystemStorage plugin = getCurrentPlugin();

            // The attached/detached events may fire before the client has a chance to call getInfo().
            // Thus, must initialize the external storage here (if not already done), to ensure that
            // unit ids are consistent across calls to getInfo, and subsequent attach/detach events.
            StorageFile[] directories = plugin.initializeExternalStorageDirectories();

            String unitId = plugin.getExternalStorageId(storagePath);
            StorageFile attachedStorage = null;
            if (attached) {
                attachedStorage = plugin.getExternalStorageDirectoryByPath(storagePath, directories);
            } else {
                // If the detached event causes initialization, the unit id may not be found
                // as it won't be reported in the list of directories.  We can safely generate
                // a random id, as the client won't have called getInfo yet.
                if (unitId == null) {
                    unitId = UUID.randomUUID().toString();
                }
            }

            message.put("action", attached ? "attached" : "detached");
            message.put("id", unitId);
            if (attached) {
                JSONObject storageUnit = plugin.buildExternalStorageUnitInfo(attachedStorage);

                message.put("info", storageUnit);
            }
        }
    };
}

From source file:org.xwalk.runtime.extension.api.device_capabilities.DeviceCapabilitiesStorage.java

private void registerIntentFilter() {
    mIntentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
    mIntentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
    mIntentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
    mIntentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
    mIntentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
    mIntentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
    mIntentFilter.addDataScheme("file");
}

From source file:com.loloof64.android.chess_position_manager.MainActivity.java

@Override
protected void onResume() {
    super.onResume();

    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_MEDIA_REMOVED);
    filter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
    filter.addAction(Intent.ACTION_MEDIA_EJECT);
    filter.addAction(Intent.ACTION_MEDIA_NOFS);
    filter.addAction(Intent.ACTION_MEDIA_SHARED);
    filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
    registerReceiver(mStorageReceiver, filter);
}

From source file:net.kidlogger.kidlogger.KLService.java

public void setupLogging() {
    // Set up GPS / Network logging      
    if (Settings.loggingGps(this)) {
        startGpsUpdates();/*from   ww w.  jav  a  2 s . c om*/
        gpsOn = true;
    }

    // Set up WiFi logging
    if (Settings.loggingWifi(this)) {
        wifiReceiver = new WifiReceiver(this);
        registerReceiver(wifiReceiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));
        wifiOn = true;
    }

    // Set up SMS logging
    if (Settings.loggingSms(this)) {
        smsObserver = new SmsObserver(this, handlering);
        IntentFilter smsFilter = new IntentFilter(SMS_RECEIVED);
        registerReceiver(smsObserver.inSms, smsFilter);
        smsOn = true;
    }

    // Set up Calls logging
    if (Settings.loggingCalls(this)) {
        IntentFilter callsFilter = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED);

        callsReceiver = new CallsReceiver(this);
        registerReceiver(callsReceiver, callsFilter);
        callOn = true;
    }

    // Set up Idle logging
    IntentFilter idleFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
    idleFilter.addAction(Intent.ACTION_USER_PRESENT);

    idleReceiver = new IdleReceiver(this);
    registerReceiver(idleReceiver, idleFilter);
    idleOn = true;
    /*if(Settings.loggingIdle(this)){
       IntentFilter idleFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
       idleFilter.addAction(Intent.ACTION_USER_PRESENT);
               
       idleReceiver = new IdleReceiver(this);
       registerReceiver(idleReceiver, idleFilter);
       idleOn = true;
    }*/

    // Set up URL logging
    if (Settings.loggingUrl(this)) {
        urlObserver = new HistoryObserver(this, handlering);
        urlOn = true;
    }

    // Set up USB logging
    if (Settings.loggingUsb(this)) {
        IntentFilter usbFilter = new IntentFilter(Intent.ACTION_UMS_CONNECTED);
        usbFilter.addAction(Intent.ACTION_UMS_DISCONNECTED);

        usbReceiver = new UsbReceiver(this);
        registerReceiver(usbReceiver, usbFilter);
        usbOn = true;
    }

    // Set up Tasks logging
    if (logTask) {
        // Check if a new Application was started
        taskScan = new Runnable() {
            public void run() {
                new Thread(new Runnable() {
                    public void run() {
                        doScanTask();
                    }
                }).start();
                if (userPresent) {
                    handleTask.postDelayed(this, SCAN_TASK_TIME);
                    scanningTask = true;
                } else {
                    scanningTask = false;
                }
            }
        };
        handleTask.postDelayed(taskScan, SCAN_TASK_TIME);
        taskOn = true;
    }

    // Set up Clipboard logging
    if (logClip) {
        // Scan clipboard content, only first 30 characters
        clipboardScan = new Runnable() {
            public void run() {
                new Thread(new Runnable() {
                    public void run() {
                        doScanClipboard();
                    }
                }).start();
                if (userPresent) {
                    handleClipb.postDelayed(this, SCAN_CLIP_TIME);
                    scanningClip = true;
                } else {
                    scanningClip = false;
                }
            }
        };
        handleClipb.postDelayed(clipboardScan, SCAN_CLIP_TIME);
        clipOn = true;
    }

    // Set up Power logging
    if (Settings.loggingPower(this)) {
        IntentFilter powerFilter = new IntentFilter(Intent.ACTION_SHUTDOWN);

        powerReceiver = new ShutdownReceiver(this);
        registerReceiver(powerReceiver, powerFilter);
        powerOn = true;
    }

    // Set up Memory Card logging
    if (Settings.loggingMedia(this)) {
        IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_REMOVED);
        mediaFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
        mediaFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        mediaFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
        mediaFilter.addAction(Intent.ACTION_MEDIA_SHARED);
        mediaFilter.addDataScheme("file");

        mediaReceiver = new MediaReceiver(this);
        registerReceiver(mediaReceiver, mediaFilter);

        mediaOn = true;
    }

    // Set up GSM logging
    if (Settings.loggingGsm(this)) {
        gsmObserver = new GsmObserver(this);
        telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        telManager.listen(gsmObserver,
                PhoneStateListener.LISTEN_SERVICE_STATE | PhoneStateListener.LISTEN_CELL_LOCATION);
        gsmOn = true;
    }

    // Set up Airplane mode receiver
    if (Settings.loggingAir(this)) {
        IntentFilter airFilter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);

        airReceiver = new AirplaneReceiver(this);
        registerReceiver(airReceiver, airFilter);
        airOn = true;
    }

    // Set up Photos logging
    if (Settings.loggingPhotos(this)) {
        photoObserver = new PhotoObserver(this, this, handlering);
        photoOn = true;
    }

    // Set up SliceMultimediaFile
    if (Settings.uploadPhotos(this) || Settings.uploadRecords(this)) {
        mediaSlicer = new SliceMultimediaFile(this);
    }

    // Set up ConnectivityReceiver
    mConReceiver = new ConnectivityReceiver(this);
    registerReceiver(mConReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

    // Ser up CallIntentReceiver
    //outCallReceiver = new CallIntentReceiver();
    //registerReceiver(outCallReceiver, new IntentFilter(KLService.OUTGOING_CALL));
}

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

/**
 * Callback from NativeDaemonConnector//from w  w  w  . ja  v a 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;
}