Search in sources :

Example 6 with StorageManager

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

the class AppFuseTest method testWriteFile.

public void testWriteFile() throws IOException {
    final StorageManager storageManager = getContext().getSystemService(StorageManager.class);
    final int INODE = 10;
    final byte[] resultBytes = new byte[5];
    final AppFuse appFuse = new AppFuse("test", new TestCallback() {

        @Override
        public long getFileSize(int inode) throws FileNotFoundException {
            if (inode != INODE) {
                throw new FileNotFoundException();
            }
            return resultBytes.length;
        }

        @Override
        public int writeObjectBytes(long fileHandle, int inode, long offset, int size, byte[] bytes) {
            for (int i = 0; i < size; i++) {
                resultBytes[(int) (offset + i)] = bytes[i];
            }
            return size;
        }
    });
    appFuse.mount(storageManager);
    final ParcelFileDescriptor fd = appFuse.openFile(INODE, ParcelFileDescriptor.MODE_WRITE_ONLY | ParcelFileDescriptor.MODE_TRUNCATE);
    try (final ParcelFileDescriptor.AutoCloseOutputStream stream = new ParcelFileDescriptor.AutoCloseOutputStream(fd)) {
        stream.write('a');
        stream.write('b');
        stream.write('c');
        stream.write('d');
        stream.write('e');
    }
    final byte[] BYTES = new byte[] { 'a', 'b', 'c', 'd', 'e' };
    assertTrue(Arrays.equals(BYTES, resultBytes));
    appFuse.close();
}
Also used : StorageManager(android.os.storage.StorageManager) FileNotFoundException(java.io.FileNotFoundException) ParcelFileDescriptor(android.os.ParcelFileDescriptor)

Example 7 with StorageManager

use of android.os.storage.StorageManager 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 8 with StorageManager

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

the class PackageHelper method fitsOnInternal.

public static boolean fitsOnInternal(Context context, long sizeBytes) {
    final StorageManager storage = context.getSystemService(StorageManager.class);
    final File target = Environment.getDataDirectory();
    return (sizeBytes <= storage.getStorageBytesUntilLow(target));
}
Also used : StorageManager(android.os.storage.StorageManager) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 9 with StorageManager

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

the class PackageHelper method fitsOnExternal.

public static boolean fitsOnExternal(Context context, long sizeBytes) {
    final StorageManager storage = context.getSystemService(StorageManager.class);
    final StorageVolume primary = storage.getPrimaryVolume();
    return (sizeBytes > 0) && !primary.isEmulated() && Environment.MEDIA_MOUNTED.equals(primary.getState()) && sizeBytes <= storage.getStorageBytesUntilLow(primary.getPathFile());
}
Also used : StorageVolume(android.os.storage.StorageVolume) StorageManager(android.os.storage.StorageManager)

Example 10 with StorageManager

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

the class MountService method onVolumeCreatedLocked.

private void onVolumeCreatedLocked(VolumeInfo vol) {
    if (mPms.isOnlyCoreApps()) {
        Slog.d(TAG, "System booted in core-only mode; ignoring volume " + vol.getId());
        return;
    }
    if (vol.type == VolumeInfo.TYPE_EMULATED) {
        final StorageManager storage = mContext.getSystemService(StorageManager.class);
        final VolumeInfo privateVol = storage.findPrivateForEmulated(vol);
        if (Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, mPrimaryStorageUuid) && VolumeInfo.ID_PRIVATE_INTERNAL.equals(privateVol.id)) {
            Slog.v(TAG, "Found primary storage at " + vol);
            vol.mountFlags |= VolumeInfo.MOUNT_FLAG_PRIMARY;
            vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE;
            mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget();
        } else if (Objects.equals(privateVol.fsUuid, mPrimaryStorageUuid)) {
            Slog.v(TAG, "Found primary storage at " + vol);
            vol.mountFlags |= VolumeInfo.MOUNT_FLAG_PRIMARY;
            vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE;
            mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget();
        }
    } else if (vol.type == VolumeInfo.TYPE_PUBLIC) {
        // TODO: only look at first public partition
        if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, mPrimaryStorageUuid) && vol.disk.isDefaultPrimary()) {
            Slog.v(TAG, "Found primary storage at " + vol);
            vol.mountFlags |= VolumeInfo.MOUNT_FLAG_PRIMARY;
            vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE;
        }
        // public API requirement of being in a stable location.
        if (vol.disk.isAdoptable()) {
            vol.mountFlags |= VolumeInfo.MOUNT_FLAG_VISIBLE;
        }
        vol.mountUserId = mCurrentUserId;
        mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget();
    } else if (vol.type == VolumeInfo.TYPE_PRIVATE) {
        mHandler.obtainMessage(H_VOLUME_MOUNT, vol).sendToTarget();
    } else {
        Slog.d(TAG, "Skipping automatic mounting of " + vol);
    }
}
Also used : StorageManager(android.os.storage.StorageManager) VolumeInfo(android.os.storage.VolumeInfo)

Aggregations

StorageManager (android.os.storage.StorageManager)161 File (java.io.File)43 IOException (java.io.IOException)42 VolumeInfo (android.os.storage.VolumeInfo)38 FileNotFoundException (java.io.FileNotFoundException)32 ParcelFileDescriptor (android.os.ParcelFileDescriptor)25 LargeTest (android.test.suitebuilder.annotation.LargeTest)20 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)17 RemoteException (android.os.RemoteException)14 ArrayList (java.util.ArrayList)14 Intent (android.content.Intent)13 Bundle (android.os.Bundle)13 StorageVolume (android.os.storage.StorageVolume)13 PackageParserException (android.content.pm.PackageParser.PackageParserException)12 NotFoundException (android.content.res.Resources.NotFoundException)12 StorageListener (android.os.storage.StorageListener)12 SettingNotFoundException (android.provider.Settings.SettingNotFoundException)12 ErrnoException (android.system.ErrnoException)12 NonNull (android.annotation.NonNull)10 ZipFile (java.util.zip.ZipFile)10