2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package android.os.storage;
19 import static android.net.TrafficStats.MB_IN_BYTES;
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.annotation.SdkConstant;
24 import android.app.ActivityThread;
25 import android.content.ContentResolver;
26 import android.content.Context;
27 import android.content.pm.IPackageMoveObserver;
28 import android.content.pm.PackageManager;
29 import android.os.Binder;
30 import android.os.Environment;
31 import android.os.FileUtils;
32 import android.os.Handler;
33 import android.os.Looper;
34 import android.os.Message;
35 import android.os.ParcelFileDescriptor;
36 import android.os.RemoteException;
37 import android.os.ServiceManager;
38 import android.os.SystemProperties;
39 import android.os.UserHandle;
40 import android.provider.Settings;
41 import android.text.TextUtils;
42 import android.util.Log;
43 import android.util.Slog;
44 import android.util.SparseArray;
46 import com.android.internal.os.SomeArgs;
47 import com.android.internal.util.Preconditions;
49 import java.io.BufferedReader;
51 import java.io.FileInputStream;
52 import java.io.IOException;
53 import java.io.InputStreamReader;
54 import java.lang.ref.WeakReference;
55 import java.util.ArrayList;
56 import java.util.Arrays;
57 import java.util.Collections;
58 import java.util.Iterator;
59 import java.util.List;
60 import java.util.Objects;
61 import java.util.concurrent.atomic.AtomicInteger;
64 * StorageManager is the interface to the systems storage service. The storage
65 * manager handles storage-related items such as Opaque Binary Blobs (OBBs).
67 * OBBs contain a filesystem that maybe be encrypted on disk and mounted
68 * on-demand from an application. OBBs are a good way of providing large amounts
69 * of binary assets without packaging them into APKs as they may be multiple
70 * gigabytes in size. However, due to their size, they're most likely stored in
71 * a shared storage pool accessible from all programs. The system does not
72 * guarantee the security of the OBB file itself: if any program modifies the
73 * OBB, there is no guarantee that a read from that OBB will produce the
76 * Get an instance of this class by calling
77 * {@link android.content.Context#getSystemService(java.lang.String)} with an
78 * argument of {@link android.content.Context#STORAGE_SERVICE}.
80 public class StorageManager {
81 private static final String TAG = "StorageManager";
84 public static final String PROP_PRIMARY_PHYSICAL = "ro.vold.primary_physical";
86 public static final String PROP_HAS_ADOPTABLE = "vold.has_adoptable";
88 public static final String PROP_FORCE_ADOPTABLE = "persist.fw.force_adoptable";
90 public static final String PROP_EMULATE_FBE = "persist.sys.emulate_fbe";
92 public static final String PROP_SDCARDFS = "persist.sys.sdcardfs";
95 public static final String UUID_PRIVATE_INTERNAL = null;
97 public static final String UUID_PRIMARY_PHYSICAL = "primary_physical";
101 * Activity Action: Allows the user to manage their storage. This activity provides the ability
102 * to free up space on the device by deleting data such as apps.
108 @SdkConstant(SdkConstant.SdkConstantType.ACTIVITY_INTENT_ACTION)
109 public static final String ACTION_MANAGE_STORAGE
110 = "android.os.storage.action.MANAGE_STORAGE";
113 public static final int DEBUG_FORCE_ADOPTABLE = 1 << 0;
115 public static final int DEBUG_EMULATE_FBE = 1 << 1;
117 public static final int DEBUG_SDCARDFS_FORCE_ON = 1 << 2;
119 public static final int DEBUG_SDCARDFS_FORCE_OFF = 1 << 3;
121 // NOTE: keep in sync with installd
123 public static final int FLAG_STORAGE_DE = 1 << 0;
125 public static final int FLAG_STORAGE_CE = 1 << 1;
128 public static final int FLAG_FOR_WRITE = 1 << 8;
130 public static final int FLAG_REAL_STATE = 1 << 9;
132 public static final int FLAG_INCLUDE_INVISIBLE = 1 << 10;
134 private static volatile IMountService sMountService = null;
136 // TODO: the location of the primary storage block varies from device to device, so we need to
137 // try the most likely candidates - a long-term solution would be a device-specific vold
138 // function that returns the calculated size.
139 private static final String[] INTERNAL_STORAGE_SIZE_PATHS = {
140 "/sys/block/mmcblk0/size",
141 "/sys/block/sda/size"
143 private static final int INTERNAL_STORAGE_SECTOR_SIZE = 512;
145 private final Context mContext;
146 private final ContentResolver mResolver;
148 private final IMountService mMountService;
149 private final Looper mLooper;
150 private final AtomicInteger mNextNonce = new AtomicInteger(0);
152 private final ArrayList<StorageEventListenerDelegate> mDelegates = new ArrayList<>();
154 private static class StorageEventListenerDelegate extends IMountServiceListener.Stub implements
156 private static final int MSG_STORAGE_STATE_CHANGED = 1;
157 private static final int MSG_VOLUME_STATE_CHANGED = 2;
158 private static final int MSG_VOLUME_RECORD_CHANGED = 3;
159 private static final int MSG_VOLUME_FORGOTTEN = 4;
160 private static final int MSG_DISK_SCANNED = 5;
161 private static final int MSG_DISK_DESTROYED = 6;
163 final StorageEventListener mCallback;
164 final Handler mHandler;
166 public StorageEventListenerDelegate(StorageEventListener callback, Looper looper) {
167 mCallback = callback;
168 mHandler = new Handler(looper, this);
172 public boolean handleMessage(Message msg) {
173 final SomeArgs args = (SomeArgs) msg.obj;
175 case MSG_STORAGE_STATE_CHANGED:
176 mCallback.onStorageStateChanged((String) args.arg1, (String) args.arg2,
180 case MSG_VOLUME_STATE_CHANGED:
181 mCallback.onVolumeStateChanged((VolumeInfo) args.arg1, args.argi2, args.argi3);
184 case MSG_VOLUME_RECORD_CHANGED:
185 mCallback.onVolumeRecordChanged((VolumeRecord) args.arg1);
188 case MSG_VOLUME_FORGOTTEN:
189 mCallback.onVolumeForgotten((String) args.arg1);
192 case MSG_DISK_SCANNED:
193 mCallback.onDiskScanned((DiskInfo) args.arg1, args.argi2);
196 case MSG_DISK_DESTROYED:
197 mCallback.onDiskDestroyed((DiskInfo) args.arg1);
206 public void onUsbMassStorageConnectionChanged(boolean connected) throws RemoteException {
211 public void onStorageStateChanged(String path, String oldState, String newState) {
212 final SomeArgs args = SomeArgs.obtain();
214 args.arg2 = oldState;
215 args.arg3 = newState;
216 mHandler.obtainMessage(MSG_STORAGE_STATE_CHANGED, args).sendToTarget();
220 public void onVolumeStateChanged(VolumeInfo vol, int oldState, int newState) {
221 final SomeArgs args = SomeArgs.obtain();
223 args.argi2 = oldState;
224 args.argi3 = newState;
225 mHandler.obtainMessage(MSG_VOLUME_STATE_CHANGED, args).sendToTarget();
229 public void onVolumeRecordChanged(VolumeRecord rec) {
230 final SomeArgs args = SomeArgs.obtain();
232 mHandler.obtainMessage(MSG_VOLUME_RECORD_CHANGED, args).sendToTarget();
236 public void onVolumeForgotten(String fsUuid) {
237 final SomeArgs args = SomeArgs.obtain();
239 mHandler.obtainMessage(MSG_VOLUME_FORGOTTEN, args).sendToTarget();
243 public void onDiskScanned(DiskInfo disk, int volumeCount) {
244 final SomeArgs args = SomeArgs.obtain();
246 args.argi2 = volumeCount;
247 mHandler.obtainMessage(MSG_DISK_SCANNED, args).sendToTarget();
251 public void onDiskDestroyed(DiskInfo disk) throws RemoteException {
252 final SomeArgs args = SomeArgs.obtain();
254 mHandler.obtainMessage(MSG_DISK_DESTROYED, args).sendToTarget();
259 * Binder listener for OBB action results.
261 private final ObbActionListener mObbActionListener = new ObbActionListener();
263 private class ObbActionListener extends IObbActionListener.Stub {
264 @SuppressWarnings("hiding")
265 private SparseArray<ObbListenerDelegate> mListeners = new SparseArray<ObbListenerDelegate>();
268 public void onObbResult(String filename, int nonce, int status) {
269 final ObbListenerDelegate delegate;
270 synchronized (mListeners) {
271 delegate = mListeners.get(nonce);
272 if (delegate != null) {
273 mListeners.remove(nonce);
277 if (delegate != null) {
278 delegate.sendObbStateChanged(filename, status);
282 public int addListener(OnObbStateChangeListener listener) {
283 final ObbListenerDelegate delegate = new ObbListenerDelegate(listener);
285 synchronized (mListeners) {
286 mListeners.put(delegate.nonce, delegate);
289 return delegate.nonce;
293 private int getNextNonce() {
294 return mNextNonce.getAndIncrement();
298 * Private class containing sender and receiver code for StorageEvents.
300 private class ObbListenerDelegate {
301 private final WeakReference<OnObbStateChangeListener> mObbEventListenerRef;
302 private final Handler mHandler;
304 private final int nonce;
306 ObbListenerDelegate(OnObbStateChangeListener listener) {
307 nonce = getNextNonce();
308 mObbEventListenerRef = new WeakReference<OnObbStateChangeListener>(listener);
309 mHandler = new Handler(mLooper) {
311 public void handleMessage(Message msg) {
312 final OnObbStateChangeListener changeListener = getListener();
313 if (changeListener == null) {
317 changeListener.onObbStateChange((String) msg.obj, msg.arg1);
322 OnObbStateChangeListener getListener() {
323 if (mObbEventListenerRef == null) {
326 return mObbEventListenerRef.get();
329 void sendObbStateChanged(String path, int state) {
330 mHandler.obtainMessage(0, state, 0, path).sendToTarget();
336 public static StorageManager from(Context context) {
337 return context.getSystemService(StorageManager.class);
341 * Constructs a StorageManager object through which an application can
342 * can communicate with the systems mount service.
344 * @param tgtLooper The {@link android.os.Looper} which events will be received on.
346 * <p>Applications can get instance of this class by calling
347 * {@link android.content.Context#getSystemService(java.lang.String)} with an argument
348 * of {@link android.content.Context#STORAGE_SERVICE}.
352 public StorageManager(Context context, Looper looper) {
354 mResolver = context.getContentResolver();
356 mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
357 if (mMountService == null) {
358 throw new IllegalStateException("Failed to find running mount service");
363 * Registers a {@link android.os.storage.StorageEventListener StorageEventListener}.
365 * @param listener A {@link android.os.storage.StorageEventListener StorageEventListener} object.
369 public void registerListener(StorageEventListener listener) {
370 synchronized (mDelegates) {
371 final StorageEventListenerDelegate delegate = new StorageEventListenerDelegate(listener,
374 mMountService.registerListener(delegate);
375 } catch (RemoteException e) {
376 throw e.rethrowFromSystemServer();
378 mDelegates.add(delegate);
383 * Unregisters a {@link android.os.storage.StorageEventListener StorageEventListener}.
385 * @param listener A {@link android.os.storage.StorageEventListener StorageEventListener} object.
389 public void unregisterListener(StorageEventListener listener) {
390 synchronized (mDelegates) {
391 for (Iterator<StorageEventListenerDelegate> i = mDelegates.iterator(); i.hasNext();) {
392 final StorageEventListenerDelegate delegate = i.next();
393 if (delegate.mCallback == listener) {
395 mMountService.unregisterListener(delegate);
396 } catch (RemoteException e) {
397 throw e.rethrowFromSystemServer();
406 * Enables USB Mass Storage (UMS) on the device.
411 public void enableUsbMassStorage() {
415 * Disables USB Mass Storage (UMS) on the device.
420 public void disableUsbMassStorage() {
424 * Query if a USB Mass Storage (UMS) host is connected.
425 * @return true if UMS host is connected.
430 public boolean isUsbMassStorageConnected() {
435 * Query if a USB Mass Storage (UMS) is enabled on the device.
436 * @return true if UMS host is enabled.
441 public boolean isUsbMassStorageEnabled() {
446 * Mount an Opaque Binary Blob (OBB) file. If a <code>key</code> is
447 * specified, it is supplied to the mounting process to be used in any
448 * encryption used in the OBB.
450 * The OBB will remain mounted for as long as the StorageManager reference
451 * is held by the application. As soon as this reference is lost, the OBBs
452 * in use will be unmounted. The {@link OnObbStateChangeListener} registered
453 * with this call will receive the success or failure of this operation.
455 * <em>Note:</em> you can only mount OBB files for which the OBB tag on the
456 * file matches a package ID that is owned by the calling program's UID.
457 * That is, shared UID applications can attempt to mount any other
458 * application's OBB that shares its UID.
460 * @param rawPath the path to the OBB file
461 * @param key secret used to encrypt the OBB; may be <code>null</code> if no
462 * encryption was used on the OBB.
463 * @param listener will receive the success or failure of the operation
464 * @return whether the mount call was successfully queued or not
466 public boolean mountObb(String rawPath, String key, OnObbStateChangeListener listener) {
467 Preconditions.checkNotNull(rawPath, "rawPath cannot be null");
468 Preconditions.checkNotNull(listener, "listener cannot be null");
471 final String canonicalPath = new File(rawPath).getCanonicalPath();
472 final int nonce = mObbActionListener.addListener(listener);
473 mMountService.mountObb(rawPath, canonicalPath, key, mObbActionListener, nonce);
475 } catch (IOException e) {
476 throw new IllegalArgumentException("Failed to resolve path: " + rawPath, e);
477 } catch (RemoteException e) {
478 throw e.rethrowFromSystemServer();
483 * Unmount an Opaque Binary Blob (OBB) file asynchronously. If the
484 * <code>force</code> flag is true, it will kill any application needed to
485 * unmount the given OBB (even the calling application).
487 * The {@link OnObbStateChangeListener} registered with this call will
488 * receive the success or failure of this operation.
490 * <em>Note:</em> you can only mount OBB files for which the OBB tag on the
491 * file matches a package ID that is owned by the calling program's UID.
492 * That is, shared UID applications can obtain access to any other
493 * application's OBB that shares its UID.
496 * @param rawPath path to the OBB file
497 * @param force whether to kill any programs using this in order to unmount
499 * @param listener will receive the success or failure of the operation
500 * @return whether the unmount call was successfully queued or not
502 public boolean unmountObb(String rawPath, boolean force, OnObbStateChangeListener listener) {
503 Preconditions.checkNotNull(rawPath, "rawPath cannot be null");
504 Preconditions.checkNotNull(listener, "listener cannot be null");
507 final int nonce = mObbActionListener.addListener(listener);
508 mMountService.unmountObb(rawPath, force, mObbActionListener, nonce);
510 } catch (RemoteException e) {
511 throw e.rethrowFromSystemServer();
516 * Check whether an Opaque Binary Blob (OBB) is mounted or not.
518 * @param rawPath path to OBB image
519 * @return true if OBB is mounted; false if not mounted or on error
521 public boolean isObbMounted(String rawPath) {
522 Preconditions.checkNotNull(rawPath, "rawPath cannot be null");
525 return mMountService.isObbMounted(rawPath);
526 } catch (RemoteException e) {
527 throw e.rethrowFromSystemServer();
532 * Check the mounted path of an Opaque Binary Blob (OBB) file. This will
533 * give you the path to where you can obtain access to the internals of the
536 * @param rawPath path to OBB image
537 * @return absolute path to mounted OBB image data or <code>null</code> if
538 * not mounted or exception encountered trying to read status
540 public String getMountedObbPath(String rawPath) {
541 Preconditions.checkNotNull(rawPath, "rawPath cannot be null");
544 return mMountService.getMountedObbPath(rawPath);
545 } catch (RemoteException e) {
546 throw e.rethrowFromSystemServer();
551 public @NonNull List<DiskInfo> getDisks() {
553 return Arrays.asList(mMountService.getDisks());
554 } catch (RemoteException e) {
555 throw e.rethrowFromSystemServer();
560 public @Nullable DiskInfo findDiskById(String id) {
561 Preconditions.checkNotNull(id);
562 // TODO; go directly to service to make this faster
563 for (DiskInfo disk : getDisks()) {
564 if (Objects.equals(disk.id, id)) {
572 public @Nullable VolumeInfo findVolumeById(String id) {
573 Preconditions.checkNotNull(id);
574 // TODO; go directly to service to make this faster
575 for (VolumeInfo vol : getVolumes()) {
576 if (Objects.equals(vol.id, id)) {
584 public @Nullable VolumeInfo findVolumeByUuid(String fsUuid) {
585 Preconditions.checkNotNull(fsUuid);
586 // TODO; go directly to service to make this faster
587 for (VolumeInfo vol : getVolumes()) {
588 if (Objects.equals(vol.fsUuid, fsUuid)) {
596 public @Nullable VolumeRecord findRecordByUuid(String fsUuid) {
597 Preconditions.checkNotNull(fsUuid);
598 // TODO; go directly to service to make this faster
599 for (VolumeRecord rec : getVolumeRecords()) {
600 if (Objects.equals(rec.fsUuid, fsUuid)) {
608 public @Nullable VolumeInfo findPrivateForEmulated(VolumeInfo emulatedVol) {
609 if (emulatedVol != null) {
610 return findVolumeById(emulatedVol.getId().replace("emulated", "private"));
617 public @Nullable VolumeInfo findEmulatedForPrivate(VolumeInfo privateVol) {
618 if (privateVol != null) {
619 return findVolumeById(privateVol.getId().replace("private", "emulated"));
626 public @Nullable VolumeInfo findVolumeByQualifiedUuid(String volumeUuid) {
627 if (Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, volumeUuid)) {
628 return findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
629 } else if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, volumeUuid)) {
630 return getPrimaryPhysicalVolume();
632 return findVolumeByUuid(volumeUuid);
637 public @NonNull List<VolumeInfo> getVolumes() {
639 return Arrays.asList(mMountService.getVolumes(0));
640 } catch (RemoteException e) {
641 throw e.rethrowFromSystemServer();
646 public @NonNull List<VolumeInfo> getWritablePrivateVolumes() {
648 final ArrayList<VolumeInfo> res = new ArrayList<>();
649 for (VolumeInfo vol : mMountService.getVolumes(0)) {
650 if (vol.getType() == VolumeInfo.TYPE_PRIVATE && vol.isMountedWritable()) {
655 } catch (RemoteException e) {
656 throw e.rethrowFromSystemServer();
661 public @NonNull List<VolumeRecord> getVolumeRecords() {
663 return Arrays.asList(mMountService.getVolumeRecords(0));
664 } catch (RemoteException e) {
665 throw e.rethrowFromSystemServer();
670 public @Nullable String getBestVolumeDescription(VolumeInfo vol) {
671 if (vol == null) return null;
673 // Nickname always takes precedence when defined
674 if (!TextUtils.isEmpty(vol.fsUuid)) {
675 final VolumeRecord rec = findRecordByUuid(vol.fsUuid);
676 if (rec != null && !TextUtils.isEmpty(rec.nickname)) {
681 if (!TextUtils.isEmpty(vol.getDescription())) {
682 return vol.getDescription();
685 if (vol.disk != null) {
686 return vol.disk.getDescription();
693 public @Nullable VolumeInfo getPrimaryPhysicalVolume() {
694 final List<VolumeInfo> vols = getVolumes();
695 for (VolumeInfo vol : vols) {
696 if (vol.isPrimaryPhysical()) {
704 public void mount(String volId) {
706 mMountService.mount(volId);
707 } catch (RemoteException e) {
708 throw e.rethrowFromSystemServer();
713 public void unmount(String volId) {
715 mMountService.unmount(volId);
716 } catch (RemoteException e) {
717 throw e.rethrowFromSystemServer();
722 public void format(String volId) {
724 mMountService.format(volId);
725 } catch (RemoteException e) {
726 throw e.rethrowFromSystemServer();
731 public long benchmark(String volId) {
733 return mMountService.benchmark(volId);
734 } catch (RemoteException e) {
735 throw e.rethrowFromSystemServer();
740 public void partitionPublic(String diskId) {
742 mMountService.partitionPublic(diskId);
743 } catch (RemoteException e) {
744 throw e.rethrowFromSystemServer();
749 public void partitionPrivate(String diskId) {
751 mMountService.partitionPrivate(diskId);
752 } catch (RemoteException e) {
753 throw e.rethrowFromSystemServer();
758 public void partitionMixed(String diskId, int ratio) {
760 mMountService.partitionMixed(diskId, ratio);
761 } catch (RemoteException e) {
762 throw e.rethrowFromSystemServer();
767 public void wipeAdoptableDisks() {
768 // We only wipe devices in "adoptable" locations, which are in a
769 // long-term stable slot/location on the device, where apps have a
770 // reasonable chance of storing sensitive data. (Apps need to go through
771 // SAF to write to transient volumes.)
772 final List<DiskInfo> disks = getDisks();
773 for (DiskInfo disk : disks) {
774 final String diskId = disk.getId();
775 if (disk.isAdoptable()) {
776 Slog.d(TAG, "Found adoptable " + diskId + "; wiping");
778 // TODO: switch to explicit wipe command when we have it,
779 // for now rely on the fact that vfat format does a wipe
780 mMountService.partitionPublic(diskId);
781 } catch (Exception e) {
782 Slog.w(TAG, "Failed to wipe " + diskId + ", but soldiering onward", e);
785 Slog.d(TAG, "Ignorning non-adoptable disk " + disk.getId());
791 public void setVolumeNickname(String fsUuid, String nickname) {
793 mMountService.setVolumeNickname(fsUuid, nickname);
794 } catch (RemoteException e) {
795 throw e.rethrowFromSystemServer();
800 public void setVolumeInited(String fsUuid, boolean inited) {
802 mMountService.setVolumeUserFlags(fsUuid, inited ? VolumeRecord.USER_FLAG_INITED : 0,
803 VolumeRecord.USER_FLAG_INITED);
804 } catch (RemoteException e) {
805 throw e.rethrowFromSystemServer();
810 public void setVolumeSnoozed(String fsUuid, boolean snoozed) {
812 mMountService.setVolumeUserFlags(fsUuid, snoozed ? VolumeRecord.USER_FLAG_SNOOZED : 0,
813 VolumeRecord.USER_FLAG_SNOOZED);
814 } catch (RemoteException e) {
815 throw e.rethrowFromSystemServer();
820 public void forgetVolume(String fsUuid) {
822 mMountService.forgetVolume(fsUuid);
823 } catch (RemoteException e) {
824 throw e.rethrowFromSystemServer();
829 * This is not the API you're looking for.
831 * @see PackageManager#getPrimaryStorageCurrentVolume()
834 public String getPrimaryStorageUuid() {
836 return mMountService.getPrimaryStorageUuid();
837 } catch (RemoteException e) {
838 throw e.rethrowFromSystemServer();
843 * This is not the API you're looking for.
845 * @see PackageManager#movePrimaryStorage(VolumeInfo)
848 public void setPrimaryStorageUuid(String volumeUuid, IPackageMoveObserver callback) {
850 mMountService.setPrimaryStorageUuid(volumeUuid, callback);
851 } catch (RemoteException e) {
852 throw e.rethrowFromSystemServer();
857 * Return the {@link StorageVolume} that contains the given file, or {@code null} if none.
859 public @Nullable StorageVolume getStorageVolume(File file) {
860 return getStorageVolume(getVolumeList(), file);
864 public static @Nullable StorageVolume getStorageVolume(File file, int userId) {
865 return getStorageVolume(getVolumeList(userId, 0), file);
869 private static @Nullable StorageVolume getStorageVolume(StorageVolume[] volumes, File file) {
874 file = file.getCanonicalFile();
875 } catch (IOException ignored) {
876 Slog.d(TAG, "Could not get canonical path for " + file);
879 for (StorageVolume volume : volumes) {
880 File volumeFile = volume.getPathFile();
882 volumeFile = volumeFile.getCanonicalFile();
883 } catch (IOException ignored) {
886 if (FileUtils.contains(volumeFile, file)) {
894 * Gets the state of a volume via its mountpoint.
898 public @NonNull String getVolumeState(String mountPoint) {
899 final StorageVolume vol = getStorageVolume(new File(mountPoint));
901 return vol.getState();
903 return Environment.MEDIA_UNKNOWN;
908 * Return the list of shared/external storage volumes available to the
909 * current user. This includes both the primary shared storage device and
910 * any attached external volumes including SD cards and USB drives.
912 * @see Environment#getExternalStorageDirectory()
913 * @see StorageVolume#createAccessIntent(String)
915 public @NonNull List<StorageVolume> getStorageVolumes() {
916 final ArrayList<StorageVolume> res = new ArrayList<>();
917 Collections.addAll(res,
918 getVolumeList(UserHandle.myUserId(), FLAG_REAL_STATE | FLAG_INCLUDE_INVISIBLE));
923 * Return the primary shared/external storage volume available to the
924 * current user. This volume is the same storage device returned by
925 * {@link Environment#getExternalStorageDirectory()} and
926 * {@link Context#getExternalFilesDir(String)}.
928 public @NonNull StorageVolume getPrimaryStorageVolume() {
929 return getVolumeList(UserHandle.myUserId(), FLAG_REAL_STATE | FLAG_INCLUDE_INVISIBLE)[0];
933 public long getPrimaryStorageSize() {
934 for (String path : INTERNAL_STORAGE_SIZE_PATHS) {
935 final long numberBlocks = readLong(path);
936 if (numberBlocks > 0) {
937 return numberBlocks * INTERNAL_STORAGE_SECTOR_SIZE;
943 private long readLong(String path) {
944 try (final FileInputStream fis = new FileInputStream(path);
945 final BufferedReader reader = new BufferedReader(new InputStreamReader(fis));) {
946 return Long.parseLong(reader.readLine());
947 } catch (Exception e) {
948 Slog.w(TAG, "Could not read " + path, e);
954 public @NonNull StorageVolume[] getVolumeList() {
955 return getVolumeList(mContext.getUserId(), 0);
959 public static @NonNull StorageVolume[] getVolumeList(int userId, int flags) {
960 final IMountService mountService = IMountService.Stub.asInterface(
961 ServiceManager.getService("mount"));
963 String packageName = ActivityThread.currentOpPackageName();
964 if (packageName == null) {
965 // Package name can be null if the activity thread is running but the app
966 // hasn't bound yet. In this case we fall back to the first package in the
967 // current UID. This works for runtime permissions as permission state is
968 // per UID and permission realted app ops are updated for all UID packages.
969 String[] packageNames = ActivityThread.getPackageManager().getPackagesForUid(
970 android.os.Process.myUid());
971 if (packageNames == null || packageNames.length <= 0) {
972 return new StorageVolume[0];
974 packageName = packageNames[0];
976 final int uid = ActivityThread.getPackageManager().getPackageUid(packageName,
977 PackageManager.MATCH_DEBUG_TRIAGED_MISSING, userId);
979 return new StorageVolume[0];
981 return mountService.getVolumeList(uid, packageName, flags);
982 } catch (RemoteException e) {
983 throw e.rethrowFromSystemServer();
988 * Returns list of paths for all mountable volumes.
992 public @NonNull String[] getVolumePaths() {
993 StorageVolume[] volumes = getVolumeList();
994 int count = volumes.length;
995 String[] paths = new String[count];
996 for (int i = 0; i < count; i++) {
997 paths[i] = volumes[i].getPath();
1003 public @NonNull StorageVolume getPrimaryVolume() {
1004 return getPrimaryVolume(getVolumeList());
1008 public static @NonNull StorageVolume getPrimaryVolume(StorageVolume[] volumes) {
1009 for (StorageVolume volume : volumes) {
1010 if (volume.isPrimary()) {
1014 throw new IllegalStateException("Missing primary storage");
1018 private static final int DEFAULT_THRESHOLD_PERCENTAGE = 10;
1019 private static final long DEFAULT_THRESHOLD_MAX_BYTES = 500 * MB_IN_BYTES;
1020 private static final long DEFAULT_FULL_THRESHOLD_BYTES = MB_IN_BYTES;
1023 * Return the number of available bytes until the given path is considered
1024 * running low on storage.
1028 public long getStorageBytesUntilLow(File path) {
1029 return path.getUsableSpace() - getStorageFullBytes(path);
1033 * Return the number of available bytes at which the given path is
1034 * considered running low on storage.
1038 public long getStorageLowBytes(File path) {
1039 final long lowPercent = Settings.Global.getInt(mResolver,
1040 Settings.Global.SYS_STORAGE_THRESHOLD_PERCENTAGE, DEFAULT_THRESHOLD_PERCENTAGE);
1041 final long lowBytes = (path.getTotalSpace() * lowPercent) / 100;
1043 final long maxLowBytes = Settings.Global.getLong(mResolver,
1044 Settings.Global.SYS_STORAGE_THRESHOLD_MAX_BYTES, DEFAULT_THRESHOLD_MAX_BYTES);
1046 return Math.min(lowBytes, maxLowBytes);
1050 * Return the number of available bytes at which the given path is
1055 public long getStorageFullBytes(File path) {
1056 return Settings.Global.getLong(mResolver, Settings.Global.SYS_STORAGE_FULL_THRESHOLD_BYTES,
1057 DEFAULT_FULL_THRESHOLD_BYTES);
1061 public void createUserKey(int userId, int serialNumber, boolean ephemeral) {
1063 mMountService.createUserKey(userId, serialNumber, ephemeral);
1064 } catch (RemoteException e) {
1065 throw e.rethrowFromSystemServer();
1070 public void destroyUserKey(int userId) {
1072 mMountService.destroyUserKey(userId);
1073 } catch (RemoteException e) {
1074 throw e.rethrowFromSystemServer();
1079 public void unlockUserKey(int userId, int serialNumber, byte[] token, byte[] secret) {
1081 mMountService.unlockUserKey(userId, serialNumber, token, secret);
1082 } catch (RemoteException e) {
1083 throw e.rethrowFromSystemServer();
1088 public void lockUserKey(int userId) {
1090 mMountService.lockUserKey(userId);
1091 } catch (RemoteException e) {
1092 throw e.rethrowFromSystemServer();
1097 public void prepareUserStorage(String volumeUuid, int userId, int serialNumber, int flags) {
1099 mMountService.prepareUserStorage(volumeUuid, userId, serialNumber, flags);
1100 } catch (RemoteException e) {
1101 throw e.rethrowFromSystemServer();
1106 public void destroyUserStorage(String volumeUuid, int userId, int flags) {
1108 mMountService.destroyUserStorage(volumeUuid, userId, flags);
1109 } catch (RemoteException e) {
1110 throw e.rethrowFromSystemServer();
1115 public static boolean isUserKeyUnlocked(int userId) {
1116 if (sMountService == null) {
1117 sMountService = IMountService.Stub
1118 .asInterface(ServiceManager.getService("mount"));
1120 if (sMountService == null) {
1121 Slog.w(TAG, "Early during boot, assuming locked");
1124 final long token = Binder.clearCallingIdentity();
1126 return sMountService.isUserKeyUnlocked(userId);
1127 } catch (RemoteException e) {
1128 throw e.rethrowAsRuntimeException();
1130 Binder.restoreCallingIdentity(token);
1135 * Return if data stored at or under the given path will be encrypted while
1136 * at rest. This can help apps avoid the overhead of double-encrypting data.
1138 public boolean isEncrypted(File file) {
1139 if (FileUtils.contains(Environment.getDataDirectory(), file)) {
1140 return isEncrypted();
1141 } else if (FileUtils.contains(Environment.getExpandDirectory(), file)) {
1144 // TODO: extend to support shared storage
1149 * Is this device encryptable or already encrypted?
1150 * @return true for encryptable or encrypted
1151 * false not encrypted and not encryptable
1153 public static boolean isEncryptable() {
1154 final String state = SystemProperties.get("ro.crypto.state", "unsupported");
1155 return !"unsupported".equalsIgnoreCase(state);
1159 * Is this device already encrypted?
1160 * @return true for encrypted. (Implies isEncryptable() == true)
1161 * false not encrypted
1163 public static boolean isEncrypted() {
1164 final String state = SystemProperties.get("ro.crypto.state", "");
1165 return "encrypted".equalsIgnoreCase(state);
1169 * Is this device file encrypted?
1170 * @return true for file encrypted. (Implies isEncrypted() == true)
1171 * false not encrypted or block encrypted
1173 public static boolean isFileEncryptedNativeOnly() {
1174 if (!isEncrypted()) {
1178 final String status = SystemProperties.get("ro.crypto.type", "");
1179 return "file".equalsIgnoreCase(status);
1183 * Is this device block encrypted?
1184 * @return true for block encrypted. (Implies isEncrypted() == true)
1185 * false not encrypted or file encrypted
1187 public static boolean isBlockEncrypted() {
1188 if (!isEncrypted()) {
1191 final String status = SystemProperties.get("ro.crypto.type", "");
1192 return "block".equalsIgnoreCase(status);
1196 * Is this device block encrypted with credentials?
1197 * @return true for crediential block encrypted.
1198 * (Implies isBlockEncrypted() == true)
1199 * false not encrypted, file encrypted or default block encrypted
1201 public static boolean isNonDefaultBlockEncrypted() {
1202 if (!isBlockEncrypted()) {
1207 IMountService mountService = IMountService.Stub.asInterface(
1208 ServiceManager.getService("mount"));
1209 return mountService.getPasswordType() != CRYPT_TYPE_DEFAULT;
1210 } catch (RemoteException e) {
1211 Log.e(TAG, "Error getting encryption type");
1217 * Is this device in the process of being block encrypted?
1218 * @return true for encrypting.
1220 * Whether device isEncrypted at this point is undefined
1221 * Note that only system services and CryptKeeper will ever see this return
1222 * true - no app will ever be launched in this state.
1223 * Also note that this state will not change without a teardown of the
1224 * framework, so no service needs to check for changes during their lifespan
1226 public static boolean isBlockEncrypting() {
1227 final String state = SystemProperties.get("vold.encrypt_progress", "");
1228 return !"".equalsIgnoreCase(state);
1232 * Is this device non default block encrypted and in the process of
1233 * prompting for credentials?
1234 * @return true for prompting for credentials.
1235 * (Implies isNonDefaultBlockEncrypted() == true)
1237 * Note that only system services and CryptKeeper will ever see this return
1238 * true - no app will ever be launched in this state.
1239 * Also note that this state will not change without a teardown of the
1240 * framework, so no service needs to check for changes during their lifespan
1242 public static boolean inCryptKeeperBounce() {
1243 final String status = SystemProperties.get("vold.decrypt");
1244 return "trigger_restart_min_framework".equals(status);
1248 public static boolean isFileEncryptedEmulatedOnly() {
1249 return SystemProperties.getBoolean(StorageManager.PROP_EMULATE_FBE, false);
1253 * Is this device running in a file encrypted mode, either native or emulated?
1254 * @return true for file encrypted, false otherwise
1256 public static boolean isFileEncryptedNativeOrEmulated() {
1257 return isFileEncryptedNativeOnly()
1258 || isFileEncryptedEmulatedOnly();
1262 public static File maybeTranslateEmulatedPathToInternal(File path) {
1263 final IMountService mountService = IMountService.Stub.asInterface(
1264 ServiceManager.getService("mount"));
1266 final VolumeInfo[] vols = mountService.getVolumes(0);
1267 for (VolumeInfo vol : vols) {
1268 if ((vol.getType() == VolumeInfo.TYPE_EMULATED
1269 || vol.getType() == VolumeInfo.TYPE_PUBLIC) && vol.isMountedReadable()) {
1270 final File internalPath = FileUtils.rewriteAfterRename(vol.getPath(),
1271 vol.getInternalPath(), path);
1272 if (internalPath != null && internalPath.exists()) {
1273 return internalPath;
1277 } catch (RemoteException e) {
1278 throw e.rethrowFromSystemServer();
1284 public ParcelFileDescriptor mountAppFuse(String name) {
1286 return mMountService.mountAppFuse(name);
1287 } catch (RemoteException e) {
1288 throw e.rethrowFromSystemServer();
1292 /// Consts to match the password types in cryptfs.h
1294 public static final int CRYPT_TYPE_PASSWORD = 0;
1296 public static final int CRYPT_TYPE_DEFAULT = 1;
1298 public static final int CRYPT_TYPE_PATTERN = 2;
1300 public static final int CRYPT_TYPE_PIN = 3;
1302 // Constants for the data available via MountService.getField.
1304 public static final String SYSTEM_LOCALE_KEY = "SystemLocale";
1306 public static final String OWNER_INFO_KEY = "OwnerInfo";
1308 public static final String PATTERN_VISIBLE_KEY = "PatternVisible";
1310 public static final String PASSWORD_VISIBLE_KEY = "PasswordVisible";