Search in sources :

Example 1 with StorageListener

use of android.os.storage.StorageListener in project platform_frameworks_base by android.

the class PackageManagerTests method unmountMedia.

private boolean unmountMedia() {
    // We can't unmount emulated storage.
    if (Environment.isExternalStorageEmulated()) {
        return true;
    }
    if (checkMediaState(Environment.MEDIA_UNMOUNTED)) {
        return true;
    }
    final String path = Environment.getExternalStorageDirectory().getPath();
    StorageListener observer = new StorageListener(Environment.MEDIA_UNMOUNTED);
    StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
    sm.registerListener(observer);
    try {
        // Wait on observer
        synchronized (observer) {
            getMs().unmountVolume(path, true, false);
            long waitTime = 0;
            while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
                observer.wait(WAIT_TIME_INCR);
                waitTime += WAIT_TIME_INCR;
            }
            if (!observer.isDone()) {
                throw new Exception("Timed out waiting for unmount media notification");
            }
            return true;
        }
    } catch (Exception e) {
        Log.e(TAG, "Exception : " + e);
        return false;
    } finally {
        sm.unregisterListener(observer);
    }
}
Also used : StorageListener(android.os.storage.StorageListener) StorageManager(android.os.storage.StorageManager) SettingNotFoundException(android.provider.Settings.SettingNotFoundException) ErrnoException(android.system.ErrnoException) NotFoundException(android.content.res.Resources.NotFoundException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) PackageParserException(android.content.pm.PackageParser.PackageParserException)

Example 2 with StorageListener

use of android.os.storage.StorageListener in project android_frameworks_base by AOSPA.

the class PackageManagerTests method mountMedia.

boolean mountMedia() {
    // We can't mount emulated storage.
    if (Environment.isExternalStorageEmulated()) {
        return true;
    }
    if (checkMediaState(Environment.MEDIA_MOUNTED)) {
        return true;
    }
    final String path = Environment.getExternalStorageDirectory().toString();
    StorageListener observer = new StorageListener(Environment.MEDIA_MOUNTED);
    StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
    sm.registerListener(observer);
    try {
        // Wait on observer
        synchronized (observer) {
            int ret = getMs().mountVolume(path);
            if (ret != StorageResultCode.OperationSucceeded) {
                throw new Exception("Could not mount the media");
            }
            long waitTime = 0;
            while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
                observer.wait(WAIT_TIME_INCR);
                waitTime += WAIT_TIME_INCR;
            }
            if (!observer.isDone()) {
                throw new Exception("Timed out waiting for unmount media notification");
            }
            return true;
        }
    } catch (Exception e) {
        Log.e(TAG, "Exception : " + e);
        return false;
    } finally {
        sm.unregisterListener(observer);
    }
}
Also used : StorageListener(android.os.storage.StorageListener) StorageManager(android.os.storage.StorageManager) SettingNotFoundException(android.provider.Settings.SettingNotFoundException) ErrnoException(android.system.ErrnoException) NotFoundException(android.content.res.Resources.NotFoundException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) PackageParserException(android.content.pm.PackageParser.PackageParserException)

Example 3 with StorageListener

use of android.os.storage.StorageListener in project android_frameworks_base by AOSPA.

the class PackageManagerTests method unmountMedia.

private boolean unmountMedia() {
    // We can't unmount emulated storage.
    if (Environment.isExternalStorageEmulated()) {
        return true;
    }
    if (checkMediaState(Environment.MEDIA_UNMOUNTED)) {
        return true;
    }
    final String path = Environment.getExternalStorageDirectory().getPath();
    StorageListener observer = new StorageListener(Environment.MEDIA_UNMOUNTED);
    StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
    sm.registerListener(observer);
    try {
        // Wait on observer
        synchronized (observer) {
            getMs().unmountVolume(path, true, false);
            long waitTime = 0;
            while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
                observer.wait(WAIT_TIME_INCR);
                waitTime += WAIT_TIME_INCR;
            }
            if (!observer.isDone()) {
                throw new Exception("Timed out waiting for unmount media notification");
            }
            return true;
        }
    } catch (Exception e) {
        Log.e(TAG, "Exception : " + e);
        return false;
    } finally {
        sm.unregisterListener(observer);
    }
}
Also used : StorageListener(android.os.storage.StorageListener) StorageManager(android.os.storage.StorageManager) SettingNotFoundException(android.provider.Settings.SettingNotFoundException) ErrnoException(android.system.ErrnoException) NotFoundException(android.content.res.Resources.NotFoundException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) PackageParserException(android.content.pm.PackageParser.PackageParserException)

Example 4 with StorageListener

use of android.os.storage.StorageListener in project android_frameworks_base by ResurrectionRemix.

the class PackageManagerTests method mountMedia.

boolean mountMedia() {
    // We can't mount emulated storage.
    if (Environment.isExternalStorageEmulated()) {
        return true;
    }
    if (checkMediaState(Environment.MEDIA_MOUNTED)) {
        return true;
    }
    final String path = Environment.getExternalStorageDirectory().toString();
    StorageListener observer = new StorageListener(Environment.MEDIA_MOUNTED);
    StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
    sm.registerListener(observer);
    try {
        // Wait on observer
        synchronized (observer) {
            int ret = getMs().mountVolume(path);
            if (ret != StorageResultCode.OperationSucceeded) {
                throw new Exception("Could not mount the media");
            }
            long waitTime = 0;
            while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
                observer.wait(WAIT_TIME_INCR);
                waitTime += WAIT_TIME_INCR;
            }
            if (!observer.isDone()) {
                throw new Exception("Timed out waiting for unmount media notification");
            }
            return true;
        }
    } catch (Exception e) {
        Log.e(TAG, "Exception : " + e);
        return false;
    } finally {
        sm.unregisterListener(observer);
    }
}
Also used : StorageListener(android.os.storage.StorageListener) StorageManager(android.os.storage.StorageManager) SettingNotFoundException(android.provider.Settings.SettingNotFoundException) ErrnoException(android.system.ErrnoException) NotFoundException(android.content.res.Resources.NotFoundException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) PackageParserException(android.content.pm.PackageParser.PackageParserException)

Example 5 with StorageListener

use of android.os.storage.StorageListener in project android_frameworks_base by ResurrectionRemix.

the class PackageManagerTests method unmountMedia.

private boolean unmountMedia() {
    // We can't unmount emulated storage.
    if (Environment.isExternalStorageEmulated()) {
        return true;
    }
    if (checkMediaState(Environment.MEDIA_UNMOUNTED)) {
        return true;
    }
    final String path = Environment.getExternalStorageDirectory().getPath();
    StorageListener observer = new StorageListener(Environment.MEDIA_UNMOUNTED);
    StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
    sm.registerListener(observer);
    try {
        // Wait on observer
        synchronized (observer) {
            getMs().unmountVolume(path, true, false);
            long waitTime = 0;
            while ((!observer.isDone()) && (waitTime < MAX_WAIT_TIME)) {
                observer.wait(WAIT_TIME_INCR);
                waitTime += WAIT_TIME_INCR;
            }
            if (!observer.isDone()) {
                throw new Exception("Timed out waiting for unmount media notification");
            }
            return true;
        }
    } catch (Exception e) {
        Log.e(TAG, "Exception : " + e);
        return false;
    } finally {
        sm.unregisterListener(observer);
    }
}
Also used : StorageListener(android.os.storage.StorageListener) StorageManager(android.os.storage.StorageManager) SettingNotFoundException(android.provider.Settings.SettingNotFoundException) ErrnoException(android.system.ErrnoException) NotFoundException(android.content.res.Resources.NotFoundException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) PackageParserException(android.content.pm.PackageParser.PackageParserException)

Aggregations

NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)12 NotFoundException (android.content.res.Resources.NotFoundException)12 RemoteException (android.os.RemoteException)12 StorageListener (android.os.storage.StorageListener)12 StorageManager (android.os.storage.StorageManager)12 SettingNotFoundException (android.provider.Settings.SettingNotFoundException)12 IOException (java.io.IOException)12 PackageParserException (android.content.pm.PackageParser.PackageParserException)10 ErrnoException (android.system.ErrnoException)10 ErrnoException (libcore.io.ErrnoException)2