Search in sources :

Example 21 with StorageManager

use of android.os.storage.StorageManager in project android_frameworks_base by DirtyUnicorns.

the class MountServiceTests method testAttemptMountObbWrongPackage.

@LargeTest
public void testAttemptMountObbWrongPackage() {
    StorageManager sm = getStorageManager();
    final File outFile = getFilePath("test1_wrongpackage.obb");
    mountObb(sm, R.raw.test1_wrongpackage, outFile, OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
    assertFalse("OBB should not be mounted", sm.isObbMounted(outFile.getPath()));
    assertNull("OBB's mounted path should be null", sm.getMountedObbPath(outFile.getPath()));
}
Also used : StorageManager(android.os.storage.StorageManager) File(java.io.File) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 22 with StorageManager

use of android.os.storage.StorageManager in project android_frameworks_base by DirtyUnicorns.

the class PackageHelper method resolveInstallVolume.

/**
     * Given a requested {@link PackageInfo#installLocation} and calculated
     * install size, pick the actual volume to install the app. Only considers
     * internal and private volumes, and prefers to keep an existing package on
     * its current volume.
     *
     * @return the {@link VolumeInfo#fsUuid} to install onto, or {@code null}
     *         for internal storage.
     */
public static String resolveInstallVolume(Context context, String packageName, int installLocation, long sizeBytes) throws IOException {
    final boolean forceAllowOnExternal = Settings.Global.getInt(context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
    // TODO: handle existing apps installed in ASEC; currently assumes
    // they'll end up back on internal storage
    ApplicationInfo existingInfo = null;
    try {
        existingInfo = context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
    } catch (NameNotFoundException ignored) {
    }
    final StorageManager storageManager = context.getSystemService(StorageManager.class);
    final boolean fitsOnInternal = fitsOnInternal(context, sizeBytes);
    final ArraySet<String> allCandidates = new ArraySet<>();
    VolumeInfo bestCandidate = null;
    long bestCandidateAvailBytes = Long.MIN_VALUE;
    for (VolumeInfo vol : storageManager.getVolumes()) {
        if (vol.type == VolumeInfo.TYPE_PRIVATE && vol.isMountedWritable()) {
            final long availBytes = storageManager.getStorageBytesUntilLow(new File(vol.path));
            if (availBytes >= sizeBytes) {
                allCandidates.add(vol.fsUuid);
            }
            if (availBytes >= bestCandidateAvailBytes) {
                bestCandidate = vol;
                bestCandidateAvailBytes = availBytes;
            }
        }
    }
    // System apps always forced to internal storage
    if (existingInfo != null && existingInfo.isSystemApp()) {
        installLocation = PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY;
    }
    // If app expresses strong desire for internal storage, honor it
    if (!forceAllowOnExternal && installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) {
        if (existingInfo != null && !Objects.equals(existingInfo.volumeUuid, StorageManager.UUID_PRIVATE_INTERNAL)) {
            throw new IOException("Cannot automatically move " + packageName + " from " + existingInfo.volumeUuid + " to internal storage");
        }
        if (fitsOnInternal) {
            return StorageManager.UUID_PRIVATE_INTERNAL;
        } else {
            throw new IOException("Requested internal only, but not enough space");
        }
    }
    // If app already exists somewhere, we must stay on that volume
    if (existingInfo != null) {
        if (Objects.equals(existingInfo.volumeUuid, StorageManager.UUID_PRIVATE_INTERNAL) && fitsOnInternal) {
            return StorageManager.UUID_PRIVATE_INTERNAL;
        } else if (allCandidates.contains(existingInfo.volumeUuid)) {
            return existingInfo.volumeUuid;
        } else {
            throw new IOException("Not enough space on existing volume " + existingInfo.volumeUuid + " for " + packageName + " upgrade");
        }
    }
    // volume with most space
    if (bestCandidate != null) {
        return bestCandidate.fsUuid;
    } else if (fitsOnInternal) {
        return StorageManager.UUID_PRIVATE_INTERNAL;
    } else {
        throw new IOException("No special requests, but no room anywhere");
    }
}
Also used : ArraySet(android.util.ArraySet) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ApplicationInfo(android.content.pm.ApplicationInfo) StorageManager(android.os.storage.StorageManager) VolumeInfo(android.os.storage.VolumeInfo) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 23 with StorageManager

use of android.os.storage.StorageManager in project android_frameworks_base by DirtyUnicorns.

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 24 with StorageManager

use of android.os.storage.StorageManager in project android_frameworks_base by DirtyUnicorns.

the class AppFuseTest method testOpenFile_fileNotFound.

public void testOpenFile_fileNotFound() throws IOException {
    final StorageManager storageManager = getContext().getSystemService(StorageManager.class);
    final int INODE = 10;
    final AppFuse appFuse = new AppFuse("test", new TestCallback());
    appFuse.mount(storageManager);
    try {
        appFuse.openFile(INODE, ParcelFileDescriptor.MODE_READ_ONLY);
        fail();
    } catch (FileNotFoundException exp) {
    }
    appFuse.close();
}
Also used : StorageManager(android.os.storage.StorageManager) FileNotFoundException(java.io.FileNotFoundException)

Example 25 with StorageManager

use of android.os.storage.StorageManager in project android_frameworks_base by DirtyUnicorns.

the class AppFuseTest method testReadFile.

public void testReadFile() throws IOException {
    final StorageManager storageManager = getContext().getSystemService(StorageManager.class);
    final int fileInode = 10;
    final byte[] fileBytes = new byte[] { 'a', 'b', 'c', 'd', 'e' };
    final AppFuse appFuse = new AppFuse("test", new TestCallback() {

        @Override
        public long getFileSize(int inode) throws FileNotFoundException {
            if (inode == fileInode) {
                return fileBytes.length;
            }
            return super.getFileSize(inode);
        }

        @Override
        public long readObjectBytes(int inode, long offset, long size, byte[] bytes) throws IOException {
            if (inode == fileInode) {
                int i = 0;
                while (i < size && i + offset < fileBytes.length) {
                    bytes[i] = fileBytes[(int) (i + offset)];
                    i++;
                }
                return i;
            }
            return super.readObjectBytes(inode, offset, size, bytes);
        }
    });
    appFuse.mount(storageManager);
    final ParcelFileDescriptor fd = appFuse.openFile(fileInode, ParcelFileDescriptor.MODE_READ_ONLY);
    try (final ParcelFileDescriptor.AutoCloseInputStream stream = new ParcelFileDescriptor.AutoCloseInputStream(fd)) {
        final byte[] buffer = new byte[1024];
        final int size = stream.read(buffer, 0, buffer.length);
        assertEquals(5, size);
    }
    appFuse.close();
}
Also used : StorageManager(android.os.storage.StorageManager) FileNotFoundException(java.io.FileNotFoundException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException)

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