use of java.io.FileNotFoundException in project platform_frameworks_base by android.
the class ExternalStorageProvider method updateVolumesLocked.
private void updateVolumesLocked() {
mRoots.clear();
VolumeInfo primaryVolume = null;
final int userId = UserHandle.myUserId();
final List<VolumeInfo> volumes = mStorageManager.getVolumes();
for (VolumeInfo volume : volumes) {
if (!volume.isMountedReadable())
continue;
final String rootId;
final String title;
if (volume.getType() == VolumeInfo.TYPE_EMULATED) {
// a time, and it's always considered the primary
if (DEBUG)
Log.d(TAG, "Found primary volume: " + volume);
rootId = ROOT_ID_PRIMARY_EMULATED;
if (VolumeInfo.ID_EMULATED_INTERNAL.equals(volume.getId())) {
// This is basically the user's primary device storage.
// Use device name for the volume since this is likely same thing
// the user sees when they mount their phone on another device.
String deviceName = Settings.Global.getString(getContext().getContentResolver(), Settings.Global.DEVICE_NAME);
// Device name should always be set. In case it isn't, though,
// fall back to a localized "Internal Storage" string.
title = !TextUtils.isEmpty(deviceName) ? deviceName : getContext().getString(R.string.root_internal_storage);
} else {
// This should cover all other storage devices, like an SD card
// or USB OTG drive plugged in. Using getBestVolumeDescription()
// will give us a nice string like "Samsung SD card" or "SanDisk USB drive"
final VolumeInfo privateVol = mStorageManager.findPrivateForEmulated(volume);
title = mStorageManager.getBestVolumeDescription(privateVol);
}
} else if (volume.getType() == VolumeInfo.TYPE_PUBLIC) {
rootId = volume.getFsUuid();
title = mStorageManager.getBestVolumeDescription(volume);
} else {
// Unsupported volume; ignore
continue;
}
if (TextUtils.isEmpty(rootId)) {
Log.d(TAG, "Missing UUID for " + volume.getId() + "; skipping");
continue;
}
if (mRoots.containsKey(rootId)) {
Log.w(TAG, "Duplicate UUID " + rootId + " for " + volume.getId() + "; skipping");
continue;
}
final RootInfo root = new RootInfo();
mRoots.put(rootId, root);
root.rootId = rootId;
root.flags = Root.FLAG_LOCAL_ONLY | Root.FLAG_SUPPORTS_SEARCH | Root.FLAG_SUPPORTS_IS_CHILD;
final DiskInfo disk = volume.getDisk();
if (DEBUG)
Log.d(TAG, "Disk for root " + rootId + " is " + disk);
if (disk != null && disk.isSd()) {
root.flags |= Root.FLAG_REMOVABLE_SD;
} else if (disk != null && disk.isUsb()) {
root.flags |= Root.FLAG_REMOVABLE_USB;
}
if (volume.isPrimary()) {
// save off the primary volume for subsequent "Home" dir initialization.
primaryVolume = volume;
root.flags |= Root.FLAG_ADVANCED;
}
// Dunno when this would NOT be the case, but never hurts to be correct.
if (volume.isMountedWritable()) {
root.flags |= Root.FLAG_SUPPORTS_CREATE;
}
root.title = title;
if (volume.getType() == VolumeInfo.TYPE_PUBLIC) {
root.flags |= Root.FLAG_HAS_SETTINGS;
}
if (volume.isVisibleForRead(userId)) {
root.visiblePath = volume.getPathForUser(userId);
} else {
root.visiblePath = null;
}
root.path = volume.getInternalPathForUser(userId);
try {
root.docId = getDocIdForFile(root.path);
} catch (FileNotFoundException e) {
throw new IllegalStateException(e);
}
}
// by calling either getPathForUser, or getInternalPathForUser.
if (primaryVolume != null && primaryVolume.isVisible()) {
final RootInfo root = new RootInfo();
root.rootId = ROOT_ID_HOME;
mRoots.put(root.rootId, root);
root.title = getContext().getString(R.string.root_documents);
// Only report bytes on *volumes*...as a matter of policy.
root.reportAvailableBytes = false;
root.flags = Root.FLAG_LOCAL_ONLY | Root.FLAG_SUPPORTS_SEARCH | Root.FLAG_SUPPORTS_IS_CHILD;
// Dunno when this would NOT be the case, but never hurts to be correct.
if (primaryVolume.isMountedWritable()) {
root.flags |= Root.FLAG_SUPPORTS_CREATE;
}
// Create the "Documents" directory on disk (don't use the localized title).
root.visiblePath = new File(primaryVolume.getPathForUser(userId), Environment.DIRECTORY_DOCUMENTS);
root.path = new File(primaryVolume.getInternalPathForUser(userId), Environment.DIRECTORY_DOCUMENTS);
try {
root.docId = getDocIdForFile(root.path);
} catch (FileNotFoundException e) {
throw new IllegalStateException(e);
}
}
Log.d(TAG, "After updating volumes, found " + mRoots.size() + " active roots");
// Note this affects content://com.android.externalstorage.documents/root/39BD-07C5
// as well as content://com.android.externalstorage.documents/document/*/children,
// so just notify on content://com.android.externalstorage.documents/.
getContext().getContentResolver().notifyChange(BASE_URI, null, false);
}
use of java.io.FileNotFoundException in project platform_frameworks_base by android.
the class TestDocumentsProvider method openDocumentThumbnail.
@Override
public AssetFileDescriptor openDocumentThumbnail(String docId, Point sizeHint, CancellationSignal signal) throws FileNotFoundException {
if (LAG)
lagUntilCanceled(signal);
if (THUMB_WEDGE)
wedgeUntilCanceled(signal);
if (THUMB_CRASH)
System.exit(12);
final Bitmap bitmap = Bitmap.createBitmap(32, 32, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
final Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawColor(Color.RED);
canvas.drawLine(0, 0, 32, 32, paint);
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 50, bos);
final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
try {
final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createReliablePipe();
new AsyncTask<Object, Object, Object>() {
@Override
protected Object doInBackground(Object... params) {
final FileOutputStream fos = new FileOutputStream(fds[1].getFileDescriptor());
try {
Streams.copy(bis, fos);
} catch (IOException e) {
throw new RuntimeException(e);
}
IoUtils.closeQuietly(fds[1]);
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return new AssetFileDescriptor(fds[0], 0, AssetFileDescriptor.UNKNOWN_LENGTH);
} catch (IOException e) {
throw new FileNotFoundException(e.getMessage());
}
}
use of java.io.FileNotFoundException in project platform_frameworks_base by android.
the class ExternalStorageProvider method openDocument.
@Override
public ParcelFileDescriptor openDocument(String documentId, String mode, CancellationSignal signal) throws FileNotFoundException {
if (mArchiveHelper.isArchivedDocument(documentId)) {
return mArchiveHelper.openDocument(documentId, mode, signal);
}
final File file = getFileForDocId(documentId);
final File visibleFile = getFileForDocId(documentId, true);
final int pfdMode = ParcelFileDescriptor.parseMode(mode);
if (pfdMode == ParcelFileDescriptor.MODE_READ_ONLY || visibleFile == null) {
return ParcelFileDescriptor.open(file, pfdMode);
} else {
try {
// When finished writing, kick off media scanner
return ParcelFileDescriptor.open(file, pfdMode, mHandler, new OnCloseListener() {
@Override
public void onClose(IOException e) {
final Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(visibleFile));
getContext().sendBroadcast(intent);
}
});
} catch (IOException e) {
throw new FileNotFoundException("Failed to open for writing: " + e);
}
}
}
use of java.io.FileNotFoundException in project platform_frameworks_base by android.
the class ExternalStorageProvider method getFileForDocId.
private File getFileForDocId(String docId, boolean visible) throws FileNotFoundException {
final int splitIndex = docId.indexOf(':', 1);
final String tag = docId.substring(0, splitIndex);
final String path = docId.substring(splitIndex + 1);
RootInfo root;
synchronized (mRootsLock) {
root = mRoots.get(tag);
}
if (root == null) {
throw new FileNotFoundException("No root for " + tag);
}
File target = visible ? root.visiblePath : root.path;
if (target == null) {
return null;
}
if (!target.exists()) {
target.mkdirs();
}
target = new File(target, path);
if (!target.exists()) {
throw new FileNotFoundException("Missing file for " + docId + " at " + target);
}
return target;
}
use of java.io.FileNotFoundException in project platform_frameworks_base by android.
the class LockSettingsService method setLockPasswordInternal.
private void setLockPasswordInternal(String password, String savedCredential, int userId) throws RemoteException {
byte[] currentHandle = getCurrentHandle(userId);
if (password == null) {
clearUserKeyProtection(userId);
getGateKeeperService().clearSecureUserId(userId);
mStorage.writePasswordHash(null, userId);
setKeystorePassword(null, userId);
fixateNewestUserKeyAuth(userId);
onUserLockChanged(userId);
return;
}
if (isManagedProfileWithUnifiedLock(userId)) {
// get credential from keystore when managed profile has unified lock
try {
savedCredential = getDecryptedPasswordForTiedProfile(userId);
} catch (FileNotFoundException e) {
Slog.i(TAG, "Child profile key not found");
} catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | CertificateException | IOException e) {
Slog.e(TAG, "Failed to decrypt child profile key", e);
}
} else {
if (currentHandle == null) {
if (savedCredential != null) {
Slog.w(TAG, "Saved credential provided, but none stored");
}
savedCredential = null;
}
}
byte[] enrolledHandle = enrollCredential(currentHandle, savedCredential, password, userId);
if (enrolledHandle != null) {
CredentialHash willStore = new CredentialHash(enrolledHandle, CredentialHash.VERSION_GATEKEEPER);
setUserKeyProtection(userId, password, doVerifyPassword(password, willStore, true, 0, userId, null));
mStorage.writePasswordHash(enrolledHandle, userId);
fixateNewestUserKeyAuth(userId);
onUserLockChanged(userId);
} else {
throw new RemoteException("Failed to enroll password");
}
}
Aggregations