Search in sources :

Example 36 with VolumeInfo

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

the class ApplicationPackageManager method getPrimaryStorageCandidateVolumes.

@Override
@NonNull
public List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
    final StorageManager storage = mContext.getSystemService(StorageManager.class);
    final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
    final List<VolumeInfo> vols = storage.getVolumes();
    final List<VolumeInfo> candidates = new ArrayList<>();
    if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, storage.getPrimaryStorageUuid()) && currentVol != null) {
        // TODO: support moving primary physical to emulated volume
        candidates.add(currentVol);
    } else {
        for (VolumeInfo vol : vols) {
            if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
                candidates.add(vol);
            }
        }
    }
    return candidates;
}
Also used : StorageManager(android.os.storage.StorageManager) ArrayList(java.util.ArrayList) VolumeInfo(android.os.storage.VolumeInfo) NonNull(android.annotation.NonNull)

Example 37 with VolumeInfo

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

the class OpenExternalDirectoryActivity method showFragment.

/**
     * Validates the given path (volume + directory) and display the appropriate dialog asking the
     * user to grant access to it.
     */
private static boolean showFragment(OpenExternalDirectoryActivity activity, int userId, StorageVolume storageVolume, String directoryName) {
    if (DEBUG)
        Log.d(TAG, "showFragment() for volume " + storageVolume.dump() + ", directory " + directoryName + ", and user " + userId);
    final boolean isRoot = directoryName.equals(DIRECTORY_ROOT);
    final boolean isPrimary = storageVolume.isPrimary();
    if (isRoot && isPrimary) {
        if (DEBUG)
            Log.d(TAG, "root access requested on primary volume");
        return false;
    }
    final File volumeRoot = storageVolume.getPathFile();
    File file;
    try {
        file = isRoot ? volumeRoot : new File(volumeRoot, directoryName).getCanonicalFile();
    } catch (IOException e) {
        Log.e(TAG, "Could not get canonical file for volume " + storageVolume.dump() + " and directory " + directoryName);
        logInvalidScopedAccessRequest(activity, SCOPED_DIRECTORY_ACCESS_ERROR);
        return false;
    }
    final StorageManager sm = (StorageManager) activity.getSystemService(Context.STORAGE_SERVICE);
    final String root, directory;
    if (isRoot) {
        root = volumeRoot.getAbsolutePath();
        directory = ".";
    } else {
        root = file.getParent();
        directory = file.getName();
        // Verify directory is valid.
        if (TextUtils.isEmpty(directory) || !isStandardDirectory(directory)) {
            if (DEBUG)
                Log.d(TAG, "Directory '" + directory + "' is not standard (full path: '" + file.getAbsolutePath() + "')");
            logInvalidScopedAccessRequest(activity, SCOPED_DIRECTORY_ACCESS_INVALID_DIRECTORY);
            return false;
        }
    }
    // Gets volume label and converted path.
    String volumeLabel = null;
    String volumeUuid = null;
    final List<VolumeInfo> volumes = sm.getVolumes();
    if (DEBUG)
        Log.d(TAG, "Number of volumes: " + volumes.size());
    File internalRoot = null;
    boolean found = true;
    for (VolumeInfo volume : volumes) {
        if (isRightVolume(volume, root, userId)) {
            found = true;
            internalRoot = volume.getInternalPathForUser(userId);
            // Must convert path before calling getDocIdForFileCreateNewDir()
            if (DEBUG)
                Log.d(TAG, "Converting " + root + " to " + internalRoot);
            file = isRoot ? internalRoot : new File(internalRoot, directory);
            volumeUuid = storageVolume.getUuid();
            volumeLabel = sm.getBestVolumeDescription(volume);
            if (TextUtils.isEmpty(volumeLabel)) {
                volumeLabel = storageVolume.getDescription(activity);
            }
            if (TextUtils.isEmpty(volumeLabel)) {
                volumeLabel = activity.getString(android.R.string.unknownName);
                Log.w(TAG, "No volume description  for " + volume + "; using " + volumeLabel);
            }
            break;
        }
    }
    if (internalRoot == null) {
        // Should not happen on normal circumstances, unless app crafted an invalid volume
        // using reflection or the list of mounted volumes changed.
        Log.e(TAG, "Didn't find right volume for '" + storageVolume.dump() + "' on " + volumes);
        return false;
    }
    // Checks if the user has granted the permission already.
    final Intent intent = getIntentForExistingPermission(activity, isRoot, internalRoot, file);
    if (intent != null) {
        logValidScopedAccessRequest(activity, directory, SCOPED_DIRECTORY_ACCESS_ALREADY_GRANTED);
        activity.setResult(RESULT_OK, intent);
        activity.finish();
        return true;
    }
    if (!found) {
        Log.e(TAG, "Could not get volume for " + file);
        logInvalidScopedAccessRequest(activity, SCOPED_DIRECTORY_ACCESS_ERROR);
        return false;
    }
    // Gets the package label.
    final String appLabel = getAppLabel(activity);
    if (appLabel == null) {
        // Error already logged.
        return false;
    }
    // Sets args that will be retrieve on onCreate()
    final Bundle args = new Bundle();
    args.putString(EXTRA_FILE, file.getAbsolutePath());
    args.putString(EXTRA_VOLUME_LABEL, volumeLabel);
    args.putString(EXTRA_VOLUME_UUID, volumeUuid);
    args.putString(EXTRA_APP_LABEL, appLabel);
    args.putBoolean(EXTRA_IS_ROOT, isRoot);
    args.putBoolean(EXTRA_IS_PRIMARY, isPrimary);
    final FragmentManager fm = activity.getFragmentManager();
    final FragmentTransaction ft = fm.beginTransaction();
    final OpenExternalDirectoryDialogFragment fragment = new OpenExternalDirectoryDialogFragment();
    fragment.setArguments(args);
    ft.add(fragment, FM_TAG);
    ft.commitAllowingStateLoss();
    return true;
}
Also used : FragmentManager(android.app.FragmentManager) FragmentTransaction(android.app.FragmentTransaction) Bundle(android.os.Bundle) StorageManager(android.os.storage.StorageManager) VolumeInfo(android.os.storage.VolumeInfo) Intent(android.content.Intent) IOException(java.io.IOException) File(java.io.File)

Example 38 with VolumeInfo

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

the class Sm method runListVolumes.

public void runListVolumes() throws RemoteException {
    final String filter = nextArg();
    final int filterType;
    if ("public".equals(filter)) {
        filterType = VolumeInfo.TYPE_PUBLIC;
    } else if ("private".equals(filter)) {
        filterType = VolumeInfo.TYPE_PRIVATE;
    } else if ("emulated".equals(filter)) {
        filterType = VolumeInfo.TYPE_EMULATED;
    } else {
        filterType = -1;
    }
    final VolumeInfo[] vols = mSm.getVolumes(0);
    for (VolumeInfo vol : vols) {
        if (filterType == -1 || filterType == vol.getType()) {
            final String envState = VolumeInfo.getEnvironmentForState(vol.getState());
            System.out.println(vol.getId() + " " + envState + " " + vol.getFsUuid());
        }
    }
}
Also used : VolumeInfo(android.os.storage.VolumeInfo)

Example 39 with VolumeInfo

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

the class ApplicationPackageManager method getPackageCandidateVolumes.

@Override
@NonNull
public List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
    final StorageManager storage = mContext.getSystemService(StorageManager.class);
    final VolumeInfo currentVol = getPackageCurrentVolume(app);
    final List<VolumeInfo> vols = storage.getVolumes();
    final List<VolumeInfo> candidates = new ArrayList<>();
    for (VolumeInfo vol : vols) {
        if (Objects.equals(vol, currentVol) || isPackageCandidateVolume(mContext, app, vol)) {
            candidates.add(vol);
        }
    }
    return candidates;
}
Also used : StorageManager(android.os.storage.StorageManager) ArrayList(java.util.ArrayList) VolumeInfo(android.os.storage.VolumeInfo) NonNull(android.annotation.NonNull)

Example 40 with VolumeInfo

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

the class ApplicationPackageManager method getPrimaryStorageCandidateVolumes.

@Override
@NonNull
public List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
    final StorageManager storage = mContext.getSystemService(StorageManager.class);
    final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
    final List<VolumeInfo> vols = storage.getVolumes();
    final List<VolumeInfo> candidates = new ArrayList<>();
    if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, storage.getPrimaryStorageUuid()) && currentVol != null) {
        // TODO: support moving primary physical to emulated volume
        candidates.add(currentVol);
    } else {
        for (VolumeInfo vol : vols) {
            if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
                candidates.add(vol);
            }
        }
    }
    return candidates;
}
Also used : StorageManager(android.os.storage.StorageManager) ArrayList(java.util.ArrayList) VolumeInfo(android.os.storage.VolumeInfo) NonNull(android.annotation.NonNull)

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