use of android.os.storage.StorageManager 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);
}
}
use of android.os.storage.StorageManager in project android_frameworks_base by AOSPA.
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;
}
use of android.os.storage.StorageManager 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;
}
use of android.os.storage.StorageManager in project android_frameworks_base by crdroidandroid.
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);
}
}
use of android.os.storage.StorageManager 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;
}
Aggregations