Search in sources :

Example 6 with VolumeInfo

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

the class AppCollectorTest method testMultipleUsersOneApp.

@Test
public void testMultipleUsersOneApp() throws Exception {
    addApplication("com.test.app", "testuuid");
    ApplicationInfo otherUsersApp = new ApplicationInfo();
    otherUsersApp.packageName = "com.test.app";
    otherUsersApp.volumeUuid = "testuuid";
    otherUsersApp.uid = 1;
    mUsers.add(new UserInfo(1, "", 0));
    VolumeInfo volume = new VolumeInfo("testuuid", 0, null, null);
    volume.fsUuid = "testuuid";
    AppCollector collector = new AppCollector(mContext, volume);
    PackageStats stats = new PackageStats("com.test.app");
    PackageStats otherStats = new PackageStats("com.test.app");
    otherStats.userHandle = 1;
    // Set up this to handle the asynchronous call to the PackageManager. This returns the
    // package info for our packages.
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            try {
                ((IPackageStatsObserver.Stub) invocation.getArguments()[2]).onGetStatsCompleted(stats, true);
                // Now callback for the other uid.
                ((IPackageStatsObserver.Stub) invocation.getArguments()[2]).onGetStatsCompleted(otherStats, true);
            } catch (Exception e) {
                // We fail instead of just letting the exception fly because throwing
                // out of the callback like this on the background thread causes the test
                // runner to crash, rather than reporting the failure.
                fail();
            }
            return null;
        }
    }).when(mPm).getPackageSizeInfoAsUser(eq("com.test.app"), eq(0), any());
    // Because getPackageStats is a blocking call, we block execution of the test until the
    // call finishes. In order to finish the call, we need the above answer to execute.
    List<PackageStats> myStats = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(1);
    new Thread(new Runnable() {

        @Override
        public void run() {
            myStats.addAll(collector.getPackageStats(TIMEOUT));
            latch.countDown();
        }
    }).start();
    latch.await();
    assertThat(myStats).containsAllOf(stats, otherStats);
}
Also used : ApplicationInfo(android.content.pm.ApplicationInfo) ArrayList(java.util.ArrayList) UserInfo(android.content.pm.UserInfo) VolumeInfo(android.os.storage.VolumeInfo) CountDownLatch(java.util.concurrent.CountDownLatch) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PackageStats(android.content.pm.PackageStats) IPackageStatsObserver(android.content.pm.IPackageStatsObserver) Test(org.junit.Test)

Example 7 with VolumeInfo

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

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 8 with VolumeInfo

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

the class AppCollectorTest method testNoApps.

@Test
public void testNoApps() throws Exception {
    VolumeInfo volume = new VolumeInfo("testuuid", 0, null, null);
    volume.fsUuid = "testuuid";
    AppCollector collector = new AppCollector(mContext, volume);
    assertThat(collector.getPackageStats(TIMEOUT)).isEmpty();
}
Also used : VolumeInfo(android.os.storage.VolumeInfo) Test(org.junit.Test)

Example 9 with VolumeInfo

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

the class AppCollectorTest method testOneValidApp.

@Test
public void testOneValidApp() throws Exception {
    addApplication("com.test.app", "testuuid");
    VolumeInfo volume = new VolumeInfo("testuuid", 0, null, null);
    volume.fsUuid = "testuuid";
    AppCollector collector = new AppCollector(mContext, volume);
    PackageStats stats = new PackageStats("com.test.app");
    // Set up this to handle the asynchronous call to the PackageManager. This returns the
    // package info for the specified package.
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            try {
                ((IPackageStatsObserver.Stub) invocation.getArguments()[2]).onGetStatsCompleted(stats, true);
            } catch (Exception e) {
                // We fail instead of just letting the exception fly because throwing
                // out of the callback like this on the background thread causes the test
                // runner to crash, rather than reporting the failure.
                fail();
            }
            return null;
        }
    }).when(mPm).getPackageSizeInfoAsUser(eq("com.test.app"), eq(0), any());
    // Because getPackageStats is a blocking call, we block execution of the test until the
    // call finishes. In order to finish the call, we need the above answer to execute.
    List<PackageStats> myStats = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(1);
    new Thread(new Runnable() {

        @Override
        public void run() {
            myStats.addAll(collector.getPackageStats(TIMEOUT));
            latch.countDown();
        }
    }).start();
    latch.await();
    assertThat(myStats).containsExactly(stats);
}
Also used : ArrayList(java.util.ArrayList) VolumeInfo(android.os.storage.VolumeInfo) CountDownLatch(java.util.concurrent.CountDownLatch) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PackageStats(android.content.pm.PackageStats) IPackageStatsObserver(android.content.pm.IPackageStatsObserver) Test(org.junit.Test)

Example 10 with VolumeInfo

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

the class AppCollectorTest method testMultipleUsersOneApp.

@Test
public void testMultipleUsersOneApp() throws Exception {
    addApplication("com.test.app", "testuuid");
    ApplicationInfo otherUsersApp = new ApplicationInfo();
    otherUsersApp.packageName = "com.test.app";
    otherUsersApp.volumeUuid = "testuuid";
    otherUsersApp.uid = 1;
    mUsers.add(new UserInfo(1, "", 0));
    VolumeInfo volume = new VolumeInfo("testuuid", 0, null, null);
    volume.fsUuid = "testuuid";
    AppCollector collector = new AppCollector(mContext, volume);
    PackageStats stats = new PackageStats("com.test.app");
    PackageStats otherStats = new PackageStats("com.test.app");
    otherStats.userHandle = 1;
    // Set up this to handle the asynchronous call to the PackageManager. This returns the
    // package info for our packages.
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            try {
                ((IPackageStatsObserver.Stub) invocation.getArguments()[2]).onGetStatsCompleted(stats, true);
                // Now callback for the other uid.
                ((IPackageStatsObserver.Stub) invocation.getArguments()[2]).onGetStatsCompleted(otherStats, true);
            } catch (Exception e) {
                // We fail instead of just letting the exception fly because throwing
                // out of the callback like this on the background thread causes the test
                // runner to crash, rather than reporting the failure.
                fail();
            }
            return null;
        }
    }).when(mPm).getPackageSizeInfoAsUser(eq("com.test.app"), eq(0), any());
    // Because getPackageStats is a blocking call, we block execution of the test until the
    // call finishes. In order to finish the call, we need the above answer to execute.
    List<PackageStats> myStats = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(1);
    new Thread(new Runnable() {

        @Override
        public void run() {
            myStats.addAll(collector.getPackageStats(TIMEOUT));
            latch.countDown();
        }
    }).start();
    latch.await();
    assertThat(myStats).containsAllOf(stats, otherStats);
}
Also used : ApplicationInfo(android.content.pm.ApplicationInfo) ArrayList(java.util.ArrayList) UserInfo(android.content.pm.UserInfo) VolumeInfo(android.os.storage.VolumeInfo) CountDownLatch(java.util.concurrent.CountDownLatch) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PackageStats(android.content.pm.PackageStats) IPackageStatsObserver(android.content.pm.IPackageStatsObserver) Test(org.junit.Test)

Aggregations

VolumeInfo (android.os.storage.VolumeInfo)290 StorageManager (android.os.storage.StorageManager)81 File (java.io.File)49 Test (org.junit.Test)42 Intent (android.content.Intent)39 DiskInfo (android.os.storage.DiskInfo)29 Bundle (android.os.Bundle)28 ArrayList (java.util.ArrayList)26 VolumeRecord (android.os.storage.VolumeRecord)22 Context (android.content.Context)21 Before (org.junit.Before)21 UserHandle (android.os.UserHandle)19 LayoutInflater (android.view.LayoutInflater)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 IOException (java.io.IOException)17 StorageStatsManager (android.app.usage.StorageStatsManager)14 MenuItem (android.view.MenuItem)14 StorageVolumeProvider (com.android.settingslib.deviceinfo.StorageVolumeProvider)14 NonNull (android.annotation.NonNull)10 Notification (android.app.Notification)10