StorageManager.java revision e82e89ef9033f02489503196df6da0654ae007be
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17package android.os.storage; 18 19import static android.net.TrafficStats.GB_IN_BYTES; 20import static android.net.TrafficStats.MB_IN_BYTES; 21 22import android.annotation.BytesLong; 23import android.annotation.IntDef; 24import android.annotation.NonNull; 25import android.annotation.Nullable; 26import android.annotation.RequiresPermission; 27import android.annotation.SdkConstant; 28import android.annotation.SuppressLint; 29import android.annotation.SystemApi; 30import android.annotation.SystemService; 31import android.annotation.WorkerThread; 32import android.app.Activity; 33import android.app.ActivityThread; 34import android.content.ContentResolver; 35import android.content.Context; 36import android.content.Intent; 37import android.content.pm.ApplicationInfo; 38import android.content.pm.IPackageMoveObserver; 39import android.content.pm.PackageManager; 40import android.os.Binder; 41import android.os.Environment; 42import android.os.FileUtils; 43import android.os.Handler; 44import android.os.Looper; 45import android.os.Message; 46import android.os.ParcelFileDescriptor; 47import android.os.ParcelableException; 48import android.os.ProxyFileDescriptorCallback; 49import android.os.RemoteException; 50import android.os.ServiceManager; 51import android.os.ServiceManager.ServiceNotFoundException; 52import android.os.SystemProperties; 53import android.os.UserHandle; 54import android.provider.Settings; 55import android.system.ErrnoException; 56import android.system.Os; 57import android.system.OsConstants; 58import android.text.TextUtils; 59import android.util.Log; 60import android.util.Pair; 61import android.util.Slog; 62import android.util.SparseArray; 63 64import com.android.internal.annotations.GuardedBy; 65import com.android.internal.annotations.VisibleForTesting; 66import com.android.internal.logging.MetricsLogger; 67import com.android.internal.os.AppFuseMount; 68import com.android.internal.os.FuseAppLoop; 69import com.android.internal.os.FuseUnavailableMountException; 70import com.android.internal.os.RoSystemProperties; 71import com.android.internal.os.SomeArgs; 72import com.android.internal.util.Preconditions; 73 74import java.io.File; 75import java.io.FileDescriptor; 76import java.io.FileNotFoundException; 77import java.io.IOException; 78import java.lang.annotation.Retention; 79import java.lang.annotation.RetentionPolicy; 80import java.lang.ref.WeakReference; 81import java.nio.charset.StandardCharsets; 82import java.util.ArrayList; 83import java.util.Arrays; 84import java.util.Collections; 85import java.util.Iterator; 86import java.util.List; 87import java.util.Objects; 88import java.util.UUID; 89import java.util.concurrent.ThreadFactory; 90import java.util.concurrent.atomic.AtomicInteger; 91 92/** 93 * StorageManager is the interface to the systems storage service. The storage 94 * manager handles storage-related items such as Opaque Binary Blobs (OBBs). 95 * <p> 96 * OBBs contain a filesystem that maybe be encrypted on disk and mounted 97 * on-demand from an application. OBBs are a good way of providing large amounts 98 * of binary assets without packaging them into APKs as they may be multiple 99 * gigabytes in size. However, due to their size, they're most likely stored in 100 * a shared storage pool accessible from all programs. The system does not 101 * guarantee the security of the OBB file itself: if any program modifies the 102 * OBB, there is no guarantee that a read from that OBB will produce the 103 * expected output. 104 */ 105@SystemService(Context.STORAGE_SERVICE) 106public class StorageManager { 107 private static final String TAG = "StorageManager"; 108 109 /** {@hide} */ 110 public static final String PROP_PRIMARY_PHYSICAL = "ro.vold.primary_physical"; 111 /** {@hide} */ 112 public static final String PROP_HAS_ADOPTABLE = "vold.has_adoptable"; 113 /** {@hide} */ 114 public static final String PROP_FORCE_ADOPTABLE = "persist.fw.force_adoptable"; 115 /** {@hide} */ 116 public static final String PROP_EMULATE_FBE = "persist.sys.emulate_fbe"; 117 /** {@hide} */ 118 public static final String PROP_SDCARDFS = "persist.sys.sdcardfs"; 119 /** {@hide} */ 120 public static final String PROP_VIRTUAL_DISK = "persist.sys.virtual_disk"; 121 /** {@hide} */ 122 public static final String PROP_ADOPTABLE_FBE = "persist.sys.adoptable_fbe"; 123 124 /** {@hide} */ 125 public static final String UUID_PRIVATE_INTERNAL = null; 126 /** {@hide} */ 127 public static final String UUID_PRIMARY_PHYSICAL = "primary_physical"; 128 /** {@hide} */ 129 public static final String UUID_SYSTEM = "system"; 130 131 // NOTE: UUID constants below are namespaced 132 // uuid -v5 ad99aa3d-308e-4191-a200-ebcab371c0ad default 133 // uuid -v5 ad99aa3d-308e-4191-a200-ebcab371c0ad primary_physical 134 // uuid -v5 ad99aa3d-308e-4191-a200-ebcab371c0ad system 135 136 /** 137 * UUID representing the default internal storage of this device which 138 * provides {@link Environment#getDataDirectory()}. 139 * <p> 140 * This value is constant across all devices and it will never change, and 141 * thus it cannot be used to uniquely identify a particular physical device. 142 * 143 * @see #getUuidForPath(File) 144 * @see ApplicationInfo#storageUuid 145 */ 146 public static final UUID UUID_DEFAULT = UUID 147 .fromString("41217664-9172-527a-b3d5-edabb50a7d69"); 148 149 /** {@hide} */ 150 public static final UUID UUID_PRIMARY_PHYSICAL_ = UUID 151 .fromString("0f95a519-dae7-5abf-9519-fbd6209e05fd"); 152 153 /** {@hide} */ 154 public static final UUID UUID_SYSTEM_ = UUID 155 .fromString("5d258386-e60d-59e3-826d-0089cdd42cc0"); 156 157 /** 158 * Activity Action: Allows the user to manage their storage. This activity 159 * provides the ability to free up space on the device by deleting data such 160 * as apps. 161 * <p> 162 * If the sending application has a specific storage device or allocation 163 * size in mind, they can optionally define {@link #EXTRA_UUID} or 164 * {@link #EXTRA_REQUESTED_BYTES}, respectively. 165 * <p> 166 * This intent should be launched using 167 * {@link Activity#startActivityForResult(Intent, int)} so that the user 168 * knows which app is requesting the storage space. The returned result will 169 * be {@link Activity#RESULT_OK} if the requested space was made available, 170 * or {@link Activity#RESULT_CANCELED} otherwise. 171 */ 172 @SdkConstant(SdkConstant.SdkConstantType.ACTIVITY_INTENT_ACTION) 173 public static final String ACTION_MANAGE_STORAGE = "android.os.storage.action.MANAGE_STORAGE"; 174 175 /** 176 * Extra {@link UUID} used to indicate the storage volume where an 177 * application is interested in allocating or managing disk space. 178 * 179 * @see #ACTION_MANAGE_STORAGE 180 * @see #UUID_DEFAULT 181 * @see #getUuidForPath(File) 182 * @see Intent#putExtra(String, java.io.Serializable) 183 */ 184 public static final String EXTRA_UUID = "android.os.storage.extra.UUID"; 185 186 /** 187 * Extra used to indicate the total size (in bytes) that an application is 188 * interested in allocating. 189 * <p> 190 * When defined, the management UI will help guide the user to free up 191 * enough disk space to reach this requested value. 192 * 193 * @see #ACTION_MANAGE_STORAGE 194 */ 195 public static final String EXTRA_REQUESTED_BYTES = "android.os.storage.extra.REQUESTED_BYTES"; 196 197 /** {@hide} */ 198 public static final int DEBUG_FORCE_ADOPTABLE = 1 << 0; 199 /** {@hide} */ 200 public static final int DEBUG_EMULATE_FBE = 1 << 1; 201 /** {@hide} */ 202 public static final int DEBUG_SDCARDFS_FORCE_ON = 1 << 2; 203 /** {@hide} */ 204 public static final int DEBUG_SDCARDFS_FORCE_OFF = 1 << 3; 205 /** {@hide} */ 206 public static final int DEBUG_VIRTUAL_DISK = 1 << 4; 207 208 // NOTE: keep in sync with installd 209 /** {@hide} */ 210 public static final int FLAG_STORAGE_DE = 1 << 0; 211 /** {@hide} */ 212 public static final int FLAG_STORAGE_CE = 1 << 1; 213 214 /** {@hide} */ 215 public static final int FLAG_FOR_WRITE = 1 << 8; 216 /** {@hide} */ 217 public static final int FLAG_REAL_STATE = 1 << 9; 218 /** {@hide} */ 219 public static final int FLAG_INCLUDE_INVISIBLE = 1 << 10; 220 221 /** {@hide} */ 222 public static final int FSTRIM_FLAG_DEEP = 1 << 0; 223 /** {@hide} */ 224 public static final int FSTRIM_FLAG_BENCHMARK = 1 << 1; 225 226 /** @hide The volume is not encrypted. */ 227 public static final int ENCRYPTION_STATE_NONE = 1; 228 229 /** @hide The volume has been encrypted succesfully. */ 230 public static final int ENCRYPTION_STATE_OK = 0; 231 232 /** @hide The volume is in a bad state.*/ 233 public static final int ENCRYPTION_STATE_ERROR_UNKNOWN = -1; 234 235 /** @hide Encryption is incomplete */ 236 public static final int ENCRYPTION_STATE_ERROR_INCOMPLETE = -2; 237 238 /** @hide Encryption is incomplete and irrecoverable */ 239 public static final int ENCRYPTION_STATE_ERROR_INCONSISTENT = -3; 240 241 /** @hide Underlying data is corrupt */ 242 public static final int ENCRYPTION_STATE_ERROR_CORRUPT = -4; 243 244 private static volatile IStorageManager sStorageManager = null; 245 246 private final Context mContext; 247 private final ContentResolver mResolver; 248 249 private final IStorageManager mStorageManager; 250 private final Looper mLooper; 251 private final AtomicInteger mNextNonce = new AtomicInteger(0); 252 253 private final ArrayList<StorageEventListenerDelegate> mDelegates = new ArrayList<>(); 254 255 private static class StorageEventListenerDelegate extends IStorageEventListener.Stub implements 256 Handler.Callback { 257 private static final int MSG_STORAGE_STATE_CHANGED = 1; 258 private static final int MSG_VOLUME_STATE_CHANGED = 2; 259 private static final int MSG_VOLUME_RECORD_CHANGED = 3; 260 private static final int MSG_VOLUME_FORGOTTEN = 4; 261 private static final int MSG_DISK_SCANNED = 5; 262 private static final int MSG_DISK_DESTROYED = 6; 263 264 final StorageEventListener mCallback; 265 final Handler mHandler; 266 267 public StorageEventListenerDelegate(StorageEventListener callback, Looper looper) { 268 mCallback = callback; 269 mHandler = new Handler(looper, this); 270 } 271 272 @Override 273 public boolean handleMessage(Message msg) { 274 final SomeArgs args = (SomeArgs) msg.obj; 275 switch (msg.what) { 276 case MSG_STORAGE_STATE_CHANGED: 277 mCallback.onStorageStateChanged((String) args.arg1, (String) args.arg2, 278 (String) args.arg3); 279 args.recycle(); 280 return true; 281 case MSG_VOLUME_STATE_CHANGED: 282 mCallback.onVolumeStateChanged((VolumeInfo) args.arg1, args.argi2, args.argi3); 283 args.recycle(); 284 return true; 285 case MSG_VOLUME_RECORD_CHANGED: 286 mCallback.onVolumeRecordChanged((VolumeRecord) args.arg1); 287 args.recycle(); 288 return true; 289 case MSG_VOLUME_FORGOTTEN: 290 mCallback.onVolumeForgotten((String) args.arg1); 291 args.recycle(); 292 return true; 293 case MSG_DISK_SCANNED: 294 mCallback.onDiskScanned((DiskInfo) args.arg1, args.argi2); 295 args.recycle(); 296 return true; 297 case MSG_DISK_DESTROYED: 298 mCallback.onDiskDestroyed((DiskInfo) args.arg1); 299 args.recycle(); 300 return true; 301 } 302 args.recycle(); 303 return false; 304 } 305 306 @Override 307 public void onUsbMassStorageConnectionChanged(boolean connected) throws RemoteException { 308 // Ignored 309 } 310 311 @Override 312 public void onStorageStateChanged(String path, String oldState, String newState) { 313 final SomeArgs args = SomeArgs.obtain(); 314 args.arg1 = path; 315 args.arg2 = oldState; 316 args.arg3 = newState; 317 mHandler.obtainMessage(MSG_STORAGE_STATE_CHANGED, args).sendToTarget(); 318 } 319 320 @Override 321 public void onVolumeStateChanged(VolumeInfo vol, int oldState, int newState) { 322 final SomeArgs args = SomeArgs.obtain(); 323 args.arg1 = vol; 324 args.argi2 = oldState; 325 args.argi3 = newState; 326 mHandler.obtainMessage(MSG_VOLUME_STATE_CHANGED, args).sendToTarget(); 327 } 328 329 @Override 330 public void onVolumeRecordChanged(VolumeRecord rec) { 331 final SomeArgs args = SomeArgs.obtain(); 332 args.arg1 = rec; 333 mHandler.obtainMessage(MSG_VOLUME_RECORD_CHANGED, args).sendToTarget(); 334 } 335 336 @Override 337 public void onVolumeForgotten(String fsUuid) { 338 final SomeArgs args = SomeArgs.obtain(); 339 args.arg1 = fsUuid; 340 mHandler.obtainMessage(MSG_VOLUME_FORGOTTEN, args).sendToTarget(); 341 } 342 343 @Override 344 public void onDiskScanned(DiskInfo disk, int volumeCount) { 345 final SomeArgs args = SomeArgs.obtain(); 346 args.arg1 = disk; 347 args.argi2 = volumeCount; 348 mHandler.obtainMessage(MSG_DISK_SCANNED, args).sendToTarget(); 349 } 350 351 @Override 352 public void onDiskDestroyed(DiskInfo disk) throws RemoteException { 353 final SomeArgs args = SomeArgs.obtain(); 354 args.arg1 = disk; 355 mHandler.obtainMessage(MSG_DISK_DESTROYED, args).sendToTarget(); 356 } 357 } 358 359 /** 360 * Binder listener for OBB action results. 361 */ 362 private final ObbActionListener mObbActionListener = new ObbActionListener(); 363 364 private class ObbActionListener extends IObbActionListener.Stub { 365 @SuppressWarnings("hiding") 366 private SparseArray<ObbListenerDelegate> mListeners = new SparseArray<ObbListenerDelegate>(); 367 368 @Override 369 public void onObbResult(String filename, int nonce, int status) { 370 final ObbListenerDelegate delegate; 371 synchronized (mListeners) { 372 delegate = mListeners.get(nonce); 373 if (delegate != null) { 374 mListeners.remove(nonce); 375 } 376 } 377 378 if (delegate != null) { 379 delegate.sendObbStateChanged(filename, status); 380 } 381 } 382 383 public int addListener(OnObbStateChangeListener listener) { 384 final ObbListenerDelegate delegate = new ObbListenerDelegate(listener); 385 386 synchronized (mListeners) { 387 mListeners.put(delegate.nonce, delegate); 388 } 389 390 return delegate.nonce; 391 } 392 } 393 394 private int getNextNonce() { 395 return mNextNonce.getAndIncrement(); 396 } 397 398 /** 399 * Private class containing sender and receiver code for StorageEvents. 400 */ 401 private class ObbListenerDelegate { 402 private final WeakReference<OnObbStateChangeListener> mObbEventListenerRef; 403 private final Handler mHandler; 404 405 private final int nonce; 406 407 ObbListenerDelegate(OnObbStateChangeListener listener) { 408 nonce = getNextNonce(); 409 mObbEventListenerRef = new WeakReference<OnObbStateChangeListener>(listener); 410 mHandler = new Handler(mLooper) { 411 @Override 412 public void handleMessage(Message msg) { 413 final OnObbStateChangeListener changeListener = getListener(); 414 if (changeListener == null) { 415 return; 416 } 417 418 changeListener.onObbStateChange((String) msg.obj, msg.arg1); 419 } 420 }; 421 } 422 423 OnObbStateChangeListener getListener() { 424 if (mObbEventListenerRef == null) { 425 return null; 426 } 427 return mObbEventListenerRef.get(); 428 } 429 430 void sendObbStateChanged(String path, int state) { 431 mHandler.obtainMessage(0, state, 0, path).sendToTarget(); 432 } 433 } 434 435 /** {@hide} */ 436 @Deprecated 437 public static StorageManager from(Context context) { 438 return context.getSystemService(StorageManager.class); 439 } 440 441 /** 442 * Constructs a StorageManager object through which an application can 443 * can communicate with the systems mount service. 444 * 445 * @param looper The {@link android.os.Looper} which events will be received on. 446 * 447 * <p>Applications can get instance of this class by calling 448 * {@link android.content.Context#getSystemService(java.lang.String)} with an argument 449 * of {@link android.content.Context#STORAGE_SERVICE}. 450 * 451 * @hide 452 */ 453 public StorageManager(Context context, Looper looper) throws ServiceNotFoundException { 454 mContext = context; 455 mResolver = context.getContentResolver(); 456 mLooper = looper; 457 mStorageManager = IStorageManager.Stub.asInterface(ServiceManager.getServiceOrThrow("mount")); 458 } 459 460 /** 461 * Registers a {@link android.os.storage.StorageEventListener StorageEventListener}. 462 * 463 * @param listener A {@link android.os.storage.StorageEventListener StorageEventListener} object. 464 * 465 * @hide 466 */ 467 public void registerListener(StorageEventListener listener) { 468 synchronized (mDelegates) { 469 final StorageEventListenerDelegate delegate = new StorageEventListenerDelegate(listener, 470 mLooper); 471 try { 472 mStorageManager.registerListener(delegate); 473 } catch (RemoteException e) { 474 throw e.rethrowFromSystemServer(); 475 } 476 mDelegates.add(delegate); 477 } 478 } 479 480 /** 481 * Unregisters a {@link android.os.storage.StorageEventListener StorageEventListener}. 482 * 483 * @param listener A {@link android.os.storage.StorageEventListener StorageEventListener} object. 484 * 485 * @hide 486 */ 487 public void unregisterListener(StorageEventListener listener) { 488 synchronized (mDelegates) { 489 for (Iterator<StorageEventListenerDelegate> i = mDelegates.iterator(); i.hasNext();) { 490 final StorageEventListenerDelegate delegate = i.next(); 491 if (delegate.mCallback == listener) { 492 try { 493 mStorageManager.unregisterListener(delegate); 494 } catch (RemoteException e) { 495 throw e.rethrowFromSystemServer(); 496 } 497 i.remove(); 498 } 499 } 500 } 501 } 502 503 /** 504 * Enables USB Mass Storage (UMS) on the device. 505 * 506 * @hide 507 */ 508 @Deprecated 509 public void enableUsbMassStorage() { 510 } 511 512 /** 513 * Disables USB Mass Storage (UMS) on the device. 514 * 515 * @hide 516 */ 517 @Deprecated 518 public void disableUsbMassStorage() { 519 } 520 521 /** 522 * Query if a USB Mass Storage (UMS) host is connected. 523 * @return true if UMS host is connected. 524 * 525 * @hide 526 */ 527 @Deprecated 528 public boolean isUsbMassStorageConnected() { 529 return false; 530 } 531 532 /** 533 * Query if a USB Mass Storage (UMS) is enabled on the device. 534 * @return true if UMS host is enabled. 535 * 536 * @hide 537 */ 538 @Deprecated 539 public boolean isUsbMassStorageEnabled() { 540 return false; 541 } 542 543 /** 544 * Mount an Opaque Binary Blob (OBB) file. If a <code>key</code> is 545 * specified, it is supplied to the mounting process to be used in any 546 * encryption used in the OBB. 547 * <p> 548 * The OBB will remain mounted for as long as the StorageManager reference 549 * is held by the application. As soon as this reference is lost, the OBBs 550 * in use will be unmounted. The {@link OnObbStateChangeListener} registered 551 * with this call will receive the success or failure of this operation. 552 * <p> 553 * <em>Note:</em> you can only mount OBB files for which the OBB tag on the 554 * file matches a package ID that is owned by the calling program's UID. 555 * That is, shared UID applications can attempt to mount any other 556 * application's OBB that shares its UID. 557 * 558 * @param rawPath the path to the OBB file 559 * @param key secret used to encrypt the OBB; may be <code>null</code> if no 560 * encryption was used on the OBB. 561 * @param listener will receive the success or failure of the operation 562 * @return whether the mount call was successfully queued or not 563 */ 564 public boolean mountObb(String rawPath, String key, OnObbStateChangeListener listener) { 565 Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); 566 Preconditions.checkNotNull(listener, "listener cannot be null"); 567 568 try { 569 final String canonicalPath = new File(rawPath).getCanonicalPath(); 570 final int nonce = mObbActionListener.addListener(listener); 571 mStorageManager.mountObb(rawPath, canonicalPath, key, mObbActionListener, nonce); 572 return true; 573 } catch (IOException e) { 574 throw new IllegalArgumentException("Failed to resolve path: " + rawPath, e); 575 } catch (RemoteException e) { 576 throw e.rethrowFromSystemServer(); 577 } 578 } 579 580 /** 581 * Unmount an Opaque Binary Blob (OBB) file asynchronously. If the 582 * <code>force</code> flag is true, it will kill any application needed to 583 * unmount the given OBB (even the calling application). 584 * <p> 585 * The {@link OnObbStateChangeListener} registered with this call will 586 * receive the success or failure of this operation. 587 * <p> 588 * <em>Note:</em> you can only mount OBB files for which the OBB tag on the 589 * file matches a package ID that is owned by the calling program's UID. 590 * That is, shared UID applications can obtain access to any other 591 * application's OBB that shares its UID. 592 * <p> 593 * 594 * @param rawPath path to the OBB file 595 * @param force whether to kill any programs using this in order to unmount 596 * it 597 * @param listener will receive the success or failure of the operation 598 * @return whether the unmount call was successfully queued or not 599 */ 600 public boolean unmountObb(String rawPath, boolean force, OnObbStateChangeListener listener) { 601 Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); 602 Preconditions.checkNotNull(listener, "listener cannot be null"); 603 604 try { 605 final int nonce = mObbActionListener.addListener(listener); 606 mStorageManager.unmountObb(rawPath, force, mObbActionListener, nonce); 607 return true; 608 } catch (RemoteException e) { 609 throw e.rethrowFromSystemServer(); 610 } 611 } 612 613 /** 614 * Check whether an Opaque Binary Blob (OBB) is mounted or not. 615 * 616 * @param rawPath path to OBB image 617 * @return true if OBB is mounted; false if not mounted or on error 618 */ 619 public boolean isObbMounted(String rawPath) { 620 Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); 621 622 try { 623 return mStorageManager.isObbMounted(rawPath); 624 } catch (RemoteException e) { 625 throw e.rethrowFromSystemServer(); 626 } 627 } 628 629 /** 630 * Check the mounted path of an Opaque Binary Blob (OBB) file. This will 631 * give you the path to where you can obtain access to the internals of the 632 * OBB. 633 * 634 * @param rawPath path to OBB image 635 * @return absolute path to mounted OBB image data or <code>null</code> if 636 * not mounted or exception encountered trying to read status 637 */ 638 public String getMountedObbPath(String rawPath) { 639 Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); 640 641 try { 642 return mStorageManager.getMountedObbPath(rawPath); 643 } catch (RemoteException e) { 644 throw e.rethrowFromSystemServer(); 645 } 646 } 647 648 /** {@hide} */ 649 public @NonNull List<DiskInfo> getDisks() { 650 try { 651 return Arrays.asList(mStorageManager.getDisks()); 652 } catch (RemoteException e) { 653 throw e.rethrowFromSystemServer(); 654 } 655 } 656 657 /** {@hide} */ 658 public @Nullable DiskInfo findDiskById(String id) { 659 Preconditions.checkNotNull(id); 660 // TODO; go directly to service to make this faster 661 for (DiskInfo disk : getDisks()) { 662 if (Objects.equals(disk.id, id)) { 663 return disk; 664 } 665 } 666 return null; 667 } 668 669 /** {@hide} */ 670 public @Nullable VolumeInfo findVolumeById(String id) { 671 Preconditions.checkNotNull(id); 672 // TODO; go directly to service to make this faster 673 for (VolumeInfo vol : getVolumes()) { 674 if (Objects.equals(vol.id, id)) { 675 return vol; 676 } 677 } 678 return null; 679 } 680 681 /** {@hide} */ 682 public @Nullable VolumeInfo findVolumeByUuid(String fsUuid) { 683 Preconditions.checkNotNull(fsUuid); 684 // TODO; go directly to service to make this faster 685 for (VolumeInfo vol : getVolumes()) { 686 if (Objects.equals(vol.fsUuid, fsUuid)) { 687 return vol; 688 } 689 } 690 return null; 691 } 692 693 /** {@hide} */ 694 public @Nullable VolumeRecord findRecordByUuid(String fsUuid) { 695 Preconditions.checkNotNull(fsUuid); 696 // TODO; go directly to service to make this faster 697 for (VolumeRecord rec : getVolumeRecords()) { 698 if (Objects.equals(rec.fsUuid, fsUuid)) { 699 return rec; 700 } 701 } 702 return null; 703 } 704 705 /** {@hide} */ 706 public @Nullable VolumeInfo findPrivateForEmulated(VolumeInfo emulatedVol) { 707 if (emulatedVol != null) { 708 return findVolumeById(emulatedVol.getId().replace("emulated", "private")); 709 } else { 710 return null; 711 } 712 } 713 714 /** {@hide} */ 715 public @Nullable VolumeInfo findEmulatedForPrivate(VolumeInfo privateVol) { 716 if (privateVol != null) { 717 return findVolumeById(privateVol.getId().replace("private", "emulated")); 718 } else { 719 return null; 720 } 721 } 722 723 /** {@hide} */ 724 public @Nullable VolumeInfo findVolumeByQualifiedUuid(String volumeUuid) { 725 if (Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, volumeUuid)) { 726 return findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL); 727 } else if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, volumeUuid)) { 728 return getPrimaryPhysicalVolume(); 729 } else { 730 return findVolumeByUuid(volumeUuid); 731 } 732 } 733 734 /** 735 * Return a UUID identifying the storage volume that hosts the given 736 * filesystem path. 737 * <p> 738 * If this path is hosted by the default internal storage of the device at 739 * {@link Environment#getDataDirectory()}, the returned value will be 740 * {@link #UUID_DEFAULT}. 741 * 742 * @throws IOException when the storage device hosting the given path isn't 743 * present, or when it doesn't have a valid UUID. 744 */ 745 public @NonNull UUID getUuidForPath(@NonNull File path) throws IOException { 746 Preconditions.checkNotNull(path); 747 final String pathString = path.getCanonicalPath(); 748 if (FileUtils.contains(Environment.getDataDirectory().getAbsolutePath(), pathString)) { 749 return UUID_DEFAULT; 750 } 751 try { 752 for (VolumeInfo vol : mStorageManager.getVolumes(0)) { 753 if (vol.path != null && FileUtils.contains(vol.path, pathString)) { 754 // TODO: verify that emulated adopted devices have UUID of 755 // underlying volume 756 return convert(vol.fsUuid); 757 } 758 } 759 } catch (RemoteException e) { 760 throw e.rethrowFromSystemServer(); 761 } 762 throw new FileNotFoundException("Failed to find a storage device for " + path); 763 } 764 765 /** {@hide} */ 766 public @NonNull File findPathForUuid(String volumeUuid) throws FileNotFoundException { 767 final VolumeInfo vol = findVolumeByQualifiedUuid(volumeUuid); 768 if (vol != null) { 769 return vol.getPath(); 770 } 771 throw new FileNotFoundException("Failed to find a storage device for " + volumeUuid); 772 } 773 774 /** 775 * Test if the given file descriptor supports allocation of disk space using 776 * {@link #allocateBytes(FileDescriptor, long)}. 777 */ 778 public boolean isAllocationSupported(@NonNull FileDescriptor fd) { 779 try { 780 getUuidForPath(ParcelFileDescriptor.getFile(fd)); 781 return true; 782 } catch (IOException e) { 783 return false; 784 } 785 } 786 787 /** {@hide} */ 788 public @NonNull List<VolumeInfo> getVolumes() { 789 try { 790 return Arrays.asList(mStorageManager.getVolumes(0)); 791 } catch (RemoteException e) { 792 throw e.rethrowFromSystemServer(); 793 } 794 } 795 796 /** {@hide} */ 797 public @NonNull List<VolumeInfo> getWritablePrivateVolumes() { 798 try { 799 final ArrayList<VolumeInfo> res = new ArrayList<>(); 800 for (VolumeInfo vol : mStorageManager.getVolumes(0)) { 801 if (vol.getType() == VolumeInfo.TYPE_PRIVATE && vol.isMountedWritable()) { 802 res.add(vol); 803 } 804 } 805 return res; 806 } catch (RemoteException e) { 807 throw e.rethrowFromSystemServer(); 808 } 809 } 810 811 /** {@hide} */ 812 public @NonNull List<VolumeRecord> getVolumeRecords() { 813 try { 814 return Arrays.asList(mStorageManager.getVolumeRecords(0)); 815 } catch (RemoteException e) { 816 throw e.rethrowFromSystemServer(); 817 } 818 } 819 820 /** {@hide} */ 821 public @Nullable String getBestVolumeDescription(VolumeInfo vol) { 822 if (vol == null) return null; 823 824 // Nickname always takes precedence when defined 825 if (!TextUtils.isEmpty(vol.fsUuid)) { 826 final VolumeRecord rec = findRecordByUuid(vol.fsUuid); 827 if (rec != null && !TextUtils.isEmpty(rec.nickname)) { 828 return rec.nickname; 829 } 830 } 831 832 if (!TextUtils.isEmpty(vol.getDescription())) { 833 return vol.getDescription(); 834 } 835 836 if (vol.disk != null) { 837 return vol.disk.getDescription(); 838 } 839 840 return null; 841 } 842 843 /** {@hide} */ 844 public @Nullable VolumeInfo getPrimaryPhysicalVolume() { 845 final List<VolumeInfo> vols = getVolumes(); 846 for (VolumeInfo vol : vols) { 847 if (vol.isPrimaryPhysical()) { 848 return vol; 849 } 850 } 851 return null; 852 } 853 854 /** {@hide} */ 855 public void mount(String volId) { 856 try { 857 mStorageManager.mount(volId); 858 } catch (RemoteException e) { 859 throw e.rethrowFromSystemServer(); 860 } 861 } 862 863 /** {@hide} */ 864 public void unmount(String volId) { 865 try { 866 mStorageManager.unmount(volId); 867 } catch (RemoteException e) { 868 throw e.rethrowFromSystemServer(); 869 } 870 } 871 872 /** {@hide} */ 873 public void format(String volId) { 874 try { 875 mStorageManager.format(volId); 876 } catch (RemoteException e) { 877 throw e.rethrowFromSystemServer(); 878 } 879 } 880 881 /** {@hide} */ 882 public long benchmark(String volId) { 883 try { 884 return mStorageManager.benchmark(volId); 885 } catch (RemoteException e) { 886 throw e.rethrowFromSystemServer(); 887 } 888 } 889 890 /** {@hide} */ 891 public void partitionPublic(String diskId) { 892 try { 893 mStorageManager.partitionPublic(diskId); 894 } catch (RemoteException e) { 895 throw e.rethrowFromSystemServer(); 896 } 897 } 898 899 /** {@hide} */ 900 public void partitionPrivate(String diskId) { 901 try { 902 mStorageManager.partitionPrivate(diskId); 903 } catch (RemoteException e) { 904 throw e.rethrowFromSystemServer(); 905 } 906 } 907 908 /** {@hide} */ 909 public void partitionMixed(String diskId, int ratio) { 910 try { 911 mStorageManager.partitionMixed(diskId, ratio); 912 } catch (RemoteException e) { 913 throw e.rethrowFromSystemServer(); 914 } 915 } 916 917 /** {@hide} */ 918 public void wipeAdoptableDisks() { 919 // We only wipe devices in "adoptable" locations, which are in a 920 // long-term stable slot/location on the device, where apps have a 921 // reasonable chance of storing sensitive data. (Apps need to go through 922 // SAF to write to transient volumes.) 923 final List<DiskInfo> disks = getDisks(); 924 for (DiskInfo disk : disks) { 925 final String diskId = disk.getId(); 926 if (disk.isAdoptable()) { 927 Slog.d(TAG, "Found adoptable " + diskId + "; wiping"); 928 try { 929 // TODO: switch to explicit wipe command when we have it, 930 // for now rely on the fact that vfat format does a wipe 931 mStorageManager.partitionPublic(diskId); 932 } catch (Exception e) { 933 Slog.w(TAG, "Failed to wipe " + diskId + ", but soldiering onward", e); 934 } 935 } else { 936 Slog.d(TAG, "Ignorning non-adoptable disk " + disk.getId()); 937 } 938 } 939 } 940 941 /** {@hide} */ 942 public void setVolumeNickname(String fsUuid, String nickname) { 943 try { 944 mStorageManager.setVolumeNickname(fsUuid, nickname); 945 } catch (RemoteException e) { 946 throw e.rethrowFromSystemServer(); 947 } 948 } 949 950 /** {@hide} */ 951 public void setVolumeInited(String fsUuid, boolean inited) { 952 try { 953 mStorageManager.setVolumeUserFlags(fsUuid, inited ? VolumeRecord.USER_FLAG_INITED : 0, 954 VolumeRecord.USER_FLAG_INITED); 955 } catch (RemoteException e) { 956 throw e.rethrowFromSystemServer(); 957 } 958 } 959 960 /** {@hide} */ 961 public void setVolumeSnoozed(String fsUuid, boolean snoozed) { 962 try { 963 mStorageManager.setVolumeUserFlags(fsUuid, snoozed ? VolumeRecord.USER_FLAG_SNOOZED : 0, 964 VolumeRecord.USER_FLAG_SNOOZED); 965 } catch (RemoteException e) { 966 throw e.rethrowFromSystemServer(); 967 } 968 } 969 970 /** {@hide} */ 971 public void forgetVolume(String fsUuid) { 972 try { 973 mStorageManager.forgetVolume(fsUuid); 974 } catch (RemoteException e) { 975 throw e.rethrowFromSystemServer(); 976 } 977 } 978 979 /** 980 * This is not the API you're looking for. 981 * 982 * @see PackageManager#getPrimaryStorageCurrentVolume() 983 * @hide 984 */ 985 public String getPrimaryStorageUuid() { 986 try { 987 return mStorageManager.getPrimaryStorageUuid(); 988 } catch (RemoteException e) { 989 throw e.rethrowFromSystemServer(); 990 } 991 } 992 993 /** 994 * This is not the API you're looking for. 995 * 996 * @see PackageManager#movePrimaryStorage(VolumeInfo) 997 * @hide 998 */ 999 public void setPrimaryStorageUuid(String volumeUuid, IPackageMoveObserver callback) { 1000 try { 1001 mStorageManager.setPrimaryStorageUuid(volumeUuid, callback); 1002 } catch (RemoteException e) { 1003 throw e.rethrowFromSystemServer(); 1004 } 1005 } 1006 1007 /** 1008 * Return the {@link StorageVolume} that contains the given file, or {@code null} if none. 1009 */ 1010 public @Nullable StorageVolume getStorageVolume(File file) { 1011 return getStorageVolume(getVolumeList(), file); 1012 } 1013 1014 /** {@hide} */ 1015 public static @Nullable StorageVolume getStorageVolume(File file, int userId) { 1016 return getStorageVolume(getVolumeList(userId, 0), file); 1017 } 1018 1019 /** {@hide} */ 1020 private static @Nullable StorageVolume getStorageVolume(StorageVolume[] volumes, File file) { 1021 if (file == null) { 1022 return null; 1023 } 1024 try { 1025 file = file.getCanonicalFile(); 1026 } catch (IOException ignored) { 1027 Slog.d(TAG, "Could not get canonical path for " + file); 1028 return null; 1029 } 1030 for (StorageVolume volume : volumes) { 1031 File volumeFile = volume.getPathFile(); 1032 try { 1033 volumeFile = volumeFile.getCanonicalFile(); 1034 } catch (IOException ignored) { 1035 continue; 1036 } 1037 if (FileUtils.contains(volumeFile, file)) { 1038 return volume; 1039 } 1040 } 1041 return null; 1042 } 1043 1044 /** 1045 * Gets the state of a volume via its mountpoint. 1046 * @hide 1047 */ 1048 @Deprecated 1049 public @NonNull String getVolumeState(String mountPoint) { 1050 final StorageVolume vol = getStorageVolume(new File(mountPoint)); 1051 if (vol != null) { 1052 return vol.getState(); 1053 } else { 1054 return Environment.MEDIA_UNKNOWN; 1055 } 1056 } 1057 1058 /** 1059 * Return the list of shared/external storage volumes available to the 1060 * current user. This includes both the primary shared storage device and 1061 * any attached external volumes including SD cards and USB drives. 1062 * 1063 * @see Environment#getExternalStorageDirectory() 1064 * @see StorageVolume#createAccessIntent(String) 1065 */ 1066 public @NonNull List<StorageVolume> getStorageVolumes() { 1067 final ArrayList<StorageVolume> res = new ArrayList<>(); 1068 Collections.addAll(res, 1069 getVolumeList(UserHandle.myUserId(), FLAG_REAL_STATE | FLAG_INCLUDE_INVISIBLE)); 1070 return res; 1071 } 1072 1073 /** 1074 * Return the primary shared/external storage volume available to the 1075 * current user. This volume is the same storage device returned by 1076 * {@link Environment#getExternalStorageDirectory()} and 1077 * {@link Context#getExternalFilesDir(String)}. 1078 */ 1079 public @NonNull StorageVolume getPrimaryStorageVolume() { 1080 return getVolumeList(UserHandle.myUserId(), FLAG_REAL_STATE | FLAG_INCLUDE_INVISIBLE)[0]; 1081 } 1082 1083 /** {@hide} */ 1084 public static Pair<String, Long> getPrimaryStoragePathAndSize() { 1085 return Pair.create(null, 1086 FileUtils.roundStorageSize(Environment.getDataDirectory().getTotalSpace() 1087 + Environment.getRootDirectory().getTotalSpace())); 1088 } 1089 1090 /** {@hide} */ 1091 public long getPrimaryStorageSize() { 1092 return FileUtils.roundStorageSize(Environment.getDataDirectory().getTotalSpace() 1093 + Environment.getRootDirectory().getTotalSpace()); 1094 } 1095 1096 /** @removed */ 1097 public @NonNull StorageVolume[] getVolumeList() { 1098 return getVolumeList(mContext.getUserId(), 0); 1099 } 1100 1101 /** {@hide} */ 1102 public static @NonNull StorageVolume[] getVolumeList(int userId, int flags) { 1103 final IStorageManager storageManager = IStorageManager.Stub.asInterface( 1104 ServiceManager.getService("mount")); 1105 try { 1106 String packageName = ActivityThread.currentOpPackageName(); 1107 if (packageName == null) { 1108 // Package name can be null if the activity thread is running but the app 1109 // hasn't bound yet. In this case we fall back to the first package in the 1110 // current UID. This works for runtime permissions as permission state is 1111 // per UID and permission realted app ops are updated for all UID packages. 1112 String[] packageNames = ActivityThread.getPackageManager().getPackagesForUid( 1113 android.os.Process.myUid()); 1114 if (packageNames == null || packageNames.length <= 0) { 1115 return new StorageVolume[0]; 1116 } 1117 packageName = packageNames[0]; 1118 } 1119 final int uid = ActivityThread.getPackageManager().getPackageUid(packageName, 1120 PackageManager.MATCH_DEBUG_TRIAGED_MISSING, userId); 1121 if (uid <= 0) { 1122 return new StorageVolume[0]; 1123 } 1124 return storageManager.getVolumeList(uid, packageName, flags); 1125 } catch (RemoteException e) { 1126 throw e.rethrowFromSystemServer(); 1127 } 1128 } 1129 1130 /** 1131 * Returns list of paths for all mountable volumes. 1132 * @hide 1133 */ 1134 @Deprecated 1135 public @NonNull String[] getVolumePaths() { 1136 StorageVolume[] volumes = getVolumeList(); 1137 int count = volumes.length; 1138 String[] paths = new String[count]; 1139 for (int i = 0; i < count; i++) { 1140 paths[i] = volumes[i].getPath(); 1141 } 1142 return paths; 1143 } 1144 1145 /** @removed */ 1146 public @NonNull StorageVolume getPrimaryVolume() { 1147 return getPrimaryVolume(getVolumeList()); 1148 } 1149 1150 /** {@hide} */ 1151 public static @NonNull StorageVolume getPrimaryVolume(StorageVolume[] volumes) { 1152 for (StorageVolume volume : volumes) { 1153 if (volume.isPrimary()) { 1154 return volume; 1155 } 1156 } 1157 throw new IllegalStateException("Missing primary storage"); 1158 } 1159 1160 private static final int DEFAULT_THRESHOLD_PERCENTAGE = 5; 1161 private static final long DEFAULT_THRESHOLD_MAX_BYTES = 500 * MB_IN_BYTES; 1162 1163 private static final int DEFAULT_CACHE_PERCENTAGE = 10; 1164 private static final long DEFAULT_CACHE_MAX_BYTES = 5 * GB_IN_BYTES; 1165 1166 private static final long DEFAULT_FULL_THRESHOLD_BYTES = MB_IN_BYTES; 1167 1168 /** 1169 * Return the number of available bytes until the given path is considered 1170 * running low on storage. 1171 * 1172 * @hide 1173 */ 1174 public long getStorageBytesUntilLow(File path) { 1175 return path.getUsableSpace() - getStorageFullBytes(path); 1176 } 1177 1178 /** 1179 * Return the number of available bytes at which the given path is 1180 * considered running low on storage. 1181 * 1182 * @hide 1183 */ 1184 public long getStorageLowBytes(File path) { 1185 final long lowPercent = Settings.Global.getInt(mResolver, 1186 Settings.Global.SYS_STORAGE_THRESHOLD_PERCENTAGE, DEFAULT_THRESHOLD_PERCENTAGE); 1187 final long lowBytes = (path.getTotalSpace() * lowPercent) / 100; 1188 1189 final long maxLowBytes = Settings.Global.getLong(mResolver, 1190 Settings.Global.SYS_STORAGE_THRESHOLD_MAX_BYTES, DEFAULT_THRESHOLD_MAX_BYTES); 1191 1192 return Math.min(lowBytes, maxLowBytes); 1193 } 1194 1195 /** 1196 * Return the minimum number of bytes of storage on the device that should 1197 * be reserved for cached data. 1198 * 1199 * @hide 1200 */ 1201 public long getStorageCacheBytes(File path, @AllocateFlags int flags) { 1202 final long cachePercent = Settings.Global.getInt(mResolver, 1203 Settings.Global.SYS_STORAGE_CACHE_PERCENTAGE, DEFAULT_CACHE_PERCENTAGE); 1204 final long cacheBytes = (path.getTotalSpace() * cachePercent) / 100; 1205 1206 final long maxCacheBytes = Settings.Global.getLong(mResolver, 1207 Settings.Global.SYS_STORAGE_CACHE_MAX_BYTES, DEFAULT_CACHE_MAX_BYTES); 1208 1209 final long result = Math.min(cacheBytes, maxCacheBytes); 1210 if ((flags & StorageManager.FLAG_ALLOCATE_AGGRESSIVE) != 0) { 1211 return 0; 1212 } else if ((flags & StorageManager.FLAG_ALLOCATE_DEFY_ALL_RESERVED) != 0) { 1213 return 0; 1214 } else if ((flags & StorageManager.FLAG_ALLOCATE_DEFY_HALF_RESERVED) != 0) { 1215 return result / 2; 1216 } else { 1217 return result; 1218 } 1219 } 1220 1221 /** 1222 * Return the number of available bytes at which the given path is 1223 * considered full. 1224 * 1225 * @hide 1226 */ 1227 public long getStorageFullBytes(File path) { 1228 return Settings.Global.getLong(mResolver, Settings.Global.SYS_STORAGE_FULL_THRESHOLD_BYTES, 1229 DEFAULT_FULL_THRESHOLD_BYTES); 1230 } 1231 1232 /** {@hide} */ 1233 public void createUserKey(int userId, int serialNumber, boolean ephemeral) { 1234 try { 1235 mStorageManager.createUserKey(userId, serialNumber, ephemeral); 1236 } catch (RemoteException e) { 1237 throw e.rethrowFromSystemServer(); 1238 } 1239 } 1240 1241 /** {@hide} */ 1242 public void destroyUserKey(int userId) { 1243 try { 1244 mStorageManager.destroyUserKey(userId); 1245 } catch (RemoteException e) { 1246 throw e.rethrowFromSystemServer(); 1247 } 1248 } 1249 1250 /** {@hide} */ 1251 public void unlockUserKey(int userId, int serialNumber, byte[] token, byte[] secret) { 1252 try { 1253 mStorageManager.unlockUserKey(userId, serialNumber, token, secret); 1254 } catch (RemoteException e) { 1255 throw e.rethrowFromSystemServer(); 1256 } 1257 } 1258 1259 /** {@hide} */ 1260 public void lockUserKey(int userId) { 1261 try { 1262 mStorageManager.lockUserKey(userId); 1263 } catch (RemoteException e) { 1264 throw e.rethrowFromSystemServer(); 1265 } 1266 } 1267 1268 /** {@hide} */ 1269 public void prepareUserStorage(String volumeUuid, int userId, int serialNumber, int flags) { 1270 try { 1271 mStorageManager.prepareUserStorage(volumeUuid, userId, serialNumber, flags); 1272 } catch (RemoteException e) { 1273 throw e.rethrowFromSystemServer(); 1274 } 1275 } 1276 1277 /** {@hide} */ 1278 public void destroyUserStorage(String volumeUuid, int userId, int flags) { 1279 try { 1280 mStorageManager.destroyUserStorage(volumeUuid, userId, flags); 1281 } catch (RemoteException e) { 1282 throw e.rethrowFromSystemServer(); 1283 } 1284 } 1285 1286 /** {@hide} */ 1287 public void secdiscard(String path) { 1288 try { 1289 mStorageManager.secdiscard(path); 1290 } catch (RemoteException e) { 1291 throw e.rethrowFromSystemServer(); 1292 } 1293 } 1294 1295 /** {@hide} */ 1296 public static boolean isUserKeyUnlocked(int userId) { 1297 if (sStorageManager == null) { 1298 sStorageManager = IStorageManager.Stub 1299 .asInterface(ServiceManager.getService("mount")); 1300 } 1301 if (sStorageManager == null) { 1302 Slog.w(TAG, "Early during boot, assuming locked"); 1303 return false; 1304 } 1305 final long token = Binder.clearCallingIdentity(); 1306 try { 1307 return sStorageManager.isUserKeyUnlocked(userId); 1308 } catch (RemoteException e) { 1309 throw e.rethrowAsRuntimeException(); 1310 } finally { 1311 Binder.restoreCallingIdentity(token); 1312 } 1313 } 1314 1315 /** 1316 * Return if data stored at or under the given path will be encrypted while 1317 * at rest. This can help apps avoid the overhead of double-encrypting data. 1318 */ 1319 public boolean isEncrypted(File file) { 1320 if (FileUtils.contains(Environment.getDataDirectory(), file)) { 1321 return isEncrypted(); 1322 } else if (FileUtils.contains(Environment.getExpandDirectory(), file)) { 1323 return true; 1324 } 1325 // TODO: extend to support shared storage 1326 return false; 1327 } 1328 1329 /** {@hide} 1330 * Is this device encryptable or already encrypted? 1331 * @return true for encryptable or encrypted 1332 * false not encrypted and not encryptable 1333 */ 1334 public static boolean isEncryptable() { 1335 return RoSystemProperties.CRYPTO_ENCRYPTABLE; 1336 } 1337 1338 /** {@hide} 1339 * Is this device already encrypted? 1340 * @return true for encrypted. (Implies isEncryptable() == true) 1341 * false not encrypted 1342 */ 1343 public static boolean isEncrypted() { 1344 return RoSystemProperties.CRYPTO_ENCRYPTED; 1345 } 1346 1347 /** {@hide} 1348 * Is this device file encrypted? 1349 * @return true for file encrypted. (Implies isEncrypted() == true) 1350 * false not encrypted or block encrypted 1351 */ 1352 public static boolean isFileEncryptedNativeOnly() { 1353 if (!isEncrypted()) { 1354 return false; 1355 } 1356 return RoSystemProperties.CRYPTO_FILE_ENCRYPTED; 1357 } 1358 1359 /** {@hide} 1360 * Is this device block encrypted? 1361 * @return true for block encrypted. (Implies isEncrypted() == true) 1362 * false not encrypted or file encrypted 1363 */ 1364 public static boolean isBlockEncrypted() { 1365 if (!isEncrypted()) { 1366 return false; 1367 } 1368 return RoSystemProperties.CRYPTO_BLOCK_ENCRYPTED; 1369 } 1370 1371 /** {@hide} 1372 * Is this device block encrypted with credentials? 1373 * @return true for crediential block encrypted. 1374 * (Implies isBlockEncrypted() == true) 1375 * false not encrypted, file encrypted or default block encrypted 1376 */ 1377 public static boolean isNonDefaultBlockEncrypted() { 1378 if (!isBlockEncrypted()) { 1379 return false; 1380 } 1381 1382 try { 1383 IStorageManager storageManager = IStorageManager.Stub.asInterface( 1384 ServiceManager.getService("mount")); 1385 return storageManager.getPasswordType() != CRYPT_TYPE_DEFAULT; 1386 } catch (RemoteException e) { 1387 Log.e(TAG, "Error getting encryption type"); 1388 return false; 1389 } 1390 } 1391 1392 /** {@hide} 1393 * Is this device in the process of being block encrypted? 1394 * @return true for encrypting. 1395 * false otherwise 1396 * Whether device isEncrypted at this point is undefined 1397 * Note that only system services and CryptKeeper will ever see this return 1398 * true - no app will ever be launched in this state. 1399 * Also note that this state will not change without a teardown of the 1400 * framework, so no service needs to check for changes during their lifespan 1401 */ 1402 public static boolean isBlockEncrypting() { 1403 final String state = SystemProperties.get("vold.encrypt_progress", ""); 1404 return !"".equalsIgnoreCase(state); 1405 } 1406 1407 /** {@hide} 1408 * Is this device non default block encrypted and in the process of 1409 * prompting for credentials? 1410 * @return true for prompting for credentials. 1411 * (Implies isNonDefaultBlockEncrypted() == true) 1412 * false otherwise 1413 * Note that only system services and CryptKeeper will ever see this return 1414 * true - no app will ever be launched in this state. 1415 * Also note that this state will not change without a teardown of the 1416 * framework, so no service needs to check for changes during their lifespan 1417 */ 1418 public static boolean inCryptKeeperBounce() { 1419 final String status = SystemProperties.get("vold.decrypt"); 1420 return "trigger_restart_min_framework".equals(status); 1421 } 1422 1423 /** {@hide} */ 1424 public static boolean isFileEncryptedEmulatedOnly() { 1425 return SystemProperties.getBoolean(StorageManager.PROP_EMULATE_FBE, false); 1426 } 1427 1428 /** {@hide} 1429 * Is this device running in a file encrypted mode, either native or emulated? 1430 * @return true for file encrypted, false otherwise 1431 */ 1432 public static boolean isFileEncryptedNativeOrEmulated() { 1433 return isFileEncryptedNativeOnly() 1434 || isFileEncryptedEmulatedOnly(); 1435 } 1436 1437 /** {@hide} */ 1438 public static File maybeTranslateEmulatedPathToInternal(File path) { 1439 // Disabled now that FUSE has been replaced by sdcardfs 1440 return path; 1441 } 1442 1443 /** {@hide} */ 1444 @VisibleForTesting 1445 public @NonNull ParcelFileDescriptor openProxyFileDescriptor( 1446 int mode, ProxyFileDescriptorCallback callback, Handler handler, ThreadFactory factory) 1447 throws IOException { 1448 Preconditions.checkNotNull(callback); 1449 MetricsLogger.count(mContext, "storage_open_proxy_file_descriptor", 1); 1450 // Retry is needed because the mount point mFuseAppLoop is using may be unmounted before 1451 // invoking StorageManagerService#openProxyFileDescriptor. In this case, we need to re-mount 1452 // the bridge by calling mountProxyFileDescriptorBridge. 1453 while (true) { 1454 try { 1455 synchronized (mFuseAppLoopLock) { 1456 boolean newlyCreated = false; 1457 if (mFuseAppLoop == null) { 1458 final AppFuseMount mount = mStorageManager.mountProxyFileDescriptorBridge(); 1459 if (mount == null) { 1460 throw new IOException("Failed to mount proxy bridge"); 1461 } 1462 mFuseAppLoop = new FuseAppLoop(mount.mountPointId, mount.fd, factory); 1463 newlyCreated = true; 1464 } 1465 if (handler == null) { 1466 handler = new Handler(Looper.getMainLooper()); 1467 } 1468 try { 1469 final int fileId = mFuseAppLoop.registerCallback(callback, handler); 1470 final ParcelFileDescriptor pfd = mStorageManager.openProxyFileDescriptor( 1471 mFuseAppLoop.getMountPointId(), fileId, mode); 1472 if (pfd == null) { 1473 mFuseAppLoop.unregisterCallback(fileId); 1474 throw new FuseUnavailableMountException( 1475 mFuseAppLoop.getMountPointId()); 1476 } 1477 return pfd; 1478 } catch (FuseUnavailableMountException exception) { 1479 // The bridge is being unmounted. Tried to recreate it unless the bridge was 1480 // just created. 1481 if (newlyCreated) { 1482 throw new IOException(exception); 1483 } 1484 mFuseAppLoop = null; 1485 continue; 1486 } 1487 } 1488 } catch (RemoteException e) { 1489 // Cannot recover from remote exception. 1490 throw new IOException(e); 1491 } 1492 } 1493 } 1494 1495 /** {@hide} */ 1496 public @NonNull ParcelFileDescriptor openProxyFileDescriptor( 1497 int mode, ProxyFileDescriptorCallback callback) 1498 throws IOException { 1499 return openProxyFileDescriptor(mode, callback, null, null); 1500 } 1501 1502 /** 1503 * Opens a seekable {@link ParcelFileDescriptor} that proxies all low-level 1504 * I/O requests back to the given {@link ProxyFileDescriptorCallback}. 1505 * <p> 1506 * This can be useful when you want to provide quick access to a large file 1507 * that isn't backed by a real file on disk, such as a file on a network 1508 * share, cloud storage service, etc. As an example, you could respond to a 1509 * {@link ContentResolver#openFileDescriptor(android.net.Uri, String)} 1510 * request by returning a {@link ParcelFileDescriptor} created with this 1511 * method, and then stream the content on-demand as requested. 1512 * <p> 1513 * Another useful example might be where you have an encrypted file that 1514 * you're willing to decrypt on-demand, but where you want to avoid 1515 * persisting the cleartext version. 1516 * 1517 * @param mode The desired access mode, must be one of 1518 * {@link ParcelFileDescriptor#MODE_READ_ONLY}, 1519 * {@link ParcelFileDescriptor#MODE_WRITE_ONLY}, or 1520 * {@link ParcelFileDescriptor#MODE_READ_WRITE} 1521 * @param callback Callback to process file operation requests issued on 1522 * returned file descriptor. 1523 * @param handler Handler that invokes callback methods. 1524 * @return Seekable ParcelFileDescriptor. 1525 * @throws IOException 1526 */ 1527 public @NonNull ParcelFileDescriptor openProxyFileDescriptor( 1528 int mode, ProxyFileDescriptorCallback callback, Handler handler) 1529 throws IOException { 1530 Preconditions.checkNotNull(handler); 1531 return openProxyFileDescriptor(mode, callback, handler, null); 1532 } 1533 1534 /** {@hide} */ 1535 @VisibleForTesting 1536 public int getProxyFileDescriptorMountPointId() { 1537 synchronized (mFuseAppLoopLock) { 1538 return mFuseAppLoop != null ? mFuseAppLoop.getMountPointId() : -1; 1539 } 1540 } 1541 1542 /** 1543 * Return quota size in bytes for all cached data belonging to the calling 1544 * app on the given storage volume. 1545 * <p> 1546 * If your app goes above this quota, your cached files will be some of the 1547 * first to be deleted when additional disk space is needed. Conversely, if 1548 * your app stays under this quota, your cached files will be some of the 1549 * last to be deleted when additional disk space is needed. 1550 * <p> 1551 * This quota will change over time depending on how frequently the user 1552 * interacts with your app, and depending on how much system-wide disk space 1553 * is used. 1554 * <p class="note"> 1555 * Note: if your app uses the {@code android:sharedUserId} manifest feature, 1556 * then cached data for all packages in your shared UID is tracked together 1557 * as a single unit. 1558 * </p> 1559 * 1560 * @param storageUuid the UUID of the storage volume that you're interested 1561 * in. The UUID for a specific path can be obtained using 1562 * {@link #getUuidForPath(File)}. 1563 * @throws IOException when the storage device isn't present, or when it 1564 * doesn't support cache quotas. 1565 * @see #getCacheSizeBytes(UUID) 1566 */ 1567 @WorkerThread 1568 public @BytesLong long getCacheQuotaBytes(@NonNull UUID storageUuid) throws IOException { 1569 try { 1570 final ApplicationInfo app = mContext.getApplicationInfo(); 1571 return mStorageManager.getCacheQuotaBytes(convert(storageUuid), app.uid); 1572 } catch (ParcelableException e) { 1573 e.maybeRethrow(IOException.class); 1574 throw new RuntimeException(e); 1575 } catch (RemoteException e) { 1576 throw e.rethrowFromSystemServer(); 1577 } 1578 } 1579 1580 /** 1581 * Return total size in bytes of all cached data belonging to the calling 1582 * app on the given storage volume. 1583 * <p> 1584 * Cached data tracked by this method always includes 1585 * {@link Context#getCacheDir()} and {@link Context#getCodeCacheDir()}, and 1586 * it also includes {@link Context#getExternalCacheDir()} if the primary 1587 * shared/external storage is hosted on the same storage device as your 1588 * private data. 1589 * <p class="note"> 1590 * Note: if your app uses the {@code android:sharedUserId} manifest feature, 1591 * then cached data for all packages in your shared UID is tracked together 1592 * as a single unit. 1593 * </p> 1594 * 1595 * @param storageUuid the UUID of the storage volume that you're interested 1596 * in. The UUID for a specific path can be obtained using 1597 * {@link #getUuidForPath(File)}. 1598 * @throws IOException when the storage device isn't present, or when it 1599 * doesn't support cache quotas. 1600 * @see #getCacheQuotaBytes(UUID) 1601 */ 1602 @WorkerThread 1603 public @BytesLong long getCacheSizeBytes(@NonNull UUID storageUuid) throws IOException { 1604 try { 1605 final ApplicationInfo app = mContext.getApplicationInfo(); 1606 return mStorageManager.getCacheSizeBytes(convert(storageUuid), app.uid); 1607 } catch (ParcelableException e) { 1608 e.maybeRethrow(IOException.class); 1609 throw new RuntimeException(e); 1610 } catch (RemoteException e) { 1611 throw e.rethrowFromSystemServer(); 1612 } 1613 } 1614 1615 /** 1616 * Flag indicating that a disk space allocation request should operate in an 1617 * aggressive mode. This flag should only be rarely used in situations that 1618 * are critical to system health or security. 1619 * <p> 1620 * When set, the system is more aggressive about the data that it considers 1621 * for possible deletion when allocating disk space. 1622 * <p class="note"> 1623 * Note: your app must hold the 1624 * {@link android.Manifest.permission#ALLOCATE_AGGRESSIVE} permission for 1625 * this flag to take effect. 1626 * </p> 1627 * 1628 * @see #getAllocatableBytes(UUID, int) 1629 * @see #allocateBytes(UUID, long, int) 1630 * @see #allocateBytes(FileDescriptor, long, int) 1631 * @hide 1632 */ 1633 @RequiresPermission(android.Manifest.permission.ALLOCATE_AGGRESSIVE) 1634 @SystemApi 1635 public static final int FLAG_ALLOCATE_AGGRESSIVE = 1 << 0; 1636 1637 /** 1638 * Flag indicating that a disk space allocation request should be allowed to 1639 * clear up to all reserved disk space. 1640 * 1641 * @hide 1642 */ 1643 public static final int FLAG_ALLOCATE_DEFY_ALL_RESERVED = 1 << 1; 1644 1645 /** 1646 * Flag indicating that a disk space allocation request should be allowed to 1647 * clear up to half of all reserved disk space. 1648 * 1649 * @hide 1650 */ 1651 public static final int FLAG_ALLOCATE_DEFY_HALF_RESERVED = 1 << 2; 1652 1653 /** @hide */ 1654 @IntDef(flag = true, value = { 1655 FLAG_ALLOCATE_AGGRESSIVE, 1656 FLAG_ALLOCATE_DEFY_ALL_RESERVED, 1657 FLAG_ALLOCATE_DEFY_HALF_RESERVED, 1658 }) 1659 @Retention(RetentionPolicy.SOURCE) 1660 public @interface AllocateFlags {} 1661 1662 /** 1663 * Return the maximum number of new bytes that your app can allocate for 1664 * itself on the given storage volume. This value is typically larger than 1665 * {@link File#getUsableSpace()}, since the system may be willing to delete 1666 * cached files to satisfy an allocation request. You can then allocate 1667 * space for yourself using {@link #allocateBytes(UUID, long)} or 1668 * {@link #allocateBytes(FileDescriptor, long)}. 1669 * <p> 1670 * This method is best used as a pre-flight check, such as deciding if there 1671 * is enough space to store an entire music album before you allocate space 1672 * for each audio file in the album. Attempts to allocate disk space beyond 1673 * the returned value will fail. 1674 * <p> 1675 * If the returned value is not large enough for the data you'd like to 1676 * persist, you can launch {@link #ACTION_MANAGE_STORAGE} with the 1677 * {@link #EXTRA_UUID} and {@link #EXTRA_REQUESTED_BYTES} options to help 1678 * involve the user in freeing up disk space. 1679 * <p> 1680 * If you're progressively allocating an unbounded amount of storage space 1681 * (such as when recording a video) you should avoid calling this method 1682 * more than once every 30 seconds. 1683 * <p class="note"> 1684 * Note: if your app uses the {@code android:sharedUserId} manifest feature, 1685 * then allocatable space for all packages in your shared UID is tracked 1686 * together as a single unit. 1687 * </p> 1688 * 1689 * @param storageUuid the UUID of the storage volume where you're 1690 * considering allocating disk space, since allocatable space can 1691 * vary widely depending on the underlying storage device. The 1692 * UUID for a specific path can be obtained using 1693 * {@link #getUuidForPath(File)}. 1694 * @return the maximum number of new bytes that the calling app can allocate 1695 * using {@link #allocateBytes(UUID, long)} or 1696 * {@link #allocateBytes(FileDescriptor, long)}. 1697 * @throws IOException when the storage device isn't present, or when it 1698 * doesn't support allocating space. 1699 */ 1700 @WorkerThread 1701 public @BytesLong long getAllocatableBytes(@NonNull UUID storageUuid) 1702 throws IOException { 1703 return getAllocatableBytes(storageUuid, 0); 1704 } 1705 1706 /** @hide */ 1707 @SystemApi 1708 @WorkerThread 1709 @SuppressLint("Doclava125") 1710 public long getAllocatableBytes(@NonNull UUID storageUuid, 1711 @RequiresPermission @AllocateFlags int flags) throws IOException { 1712 try { 1713 return mStorageManager.getAllocatableBytes(convert(storageUuid), flags, 1714 mContext.getOpPackageName()); 1715 } catch (ParcelableException e) { 1716 e.maybeRethrow(IOException.class); 1717 throw new RuntimeException(e); 1718 } catch (RemoteException e) { 1719 throw e.rethrowFromSystemServer(); 1720 } 1721 } 1722 1723 /** 1724 * Allocate the requested number of bytes for your application to use on the 1725 * given storage volume. This will cause the system to delete any cached 1726 * files necessary to satisfy your request. 1727 * <p> 1728 * Attempts to allocate disk space beyond the value returned by 1729 * {@link #getAllocatableBytes(UUID)} will fail. 1730 * <p> 1731 * Since multiple apps can be running simultaneously, this method may be 1732 * subject to race conditions. If possible, consider using 1733 * {@link #allocateBytes(FileDescriptor, long)} which will guarantee 1734 * that bytes are allocated to an opened file. 1735 * <p> 1736 * If you're progressively allocating an unbounded amount of storage space 1737 * (such as when recording a video) you should avoid calling this method 1738 * more than once every 60 seconds. 1739 * 1740 * @param storageUuid the UUID of the storage volume where you'd like to 1741 * allocate disk space. The UUID for a specific path can be 1742 * obtained using {@link #getUuidForPath(File)}. 1743 * @param bytes the number of bytes to allocate. 1744 * @throws IOException when the storage device isn't present, or when it 1745 * doesn't support allocating space, or if the device had 1746 * trouble allocating the requested space. 1747 * @see #getAllocatableBytes(UUID) 1748 */ 1749 @WorkerThread 1750 public void allocateBytes(@NonNull UUID storageUuid, @BytesLong long bytes) 1751 throws IOException { 1752 allocateBytes(storageUuid, bytes, 0); 1753 } 1754 1755 /** @hide */ 1756 @SystemApi 1757 @WorkerThread 1758 @SuppressLint("Doclava125") 1759 public void allocateBytes(@NonNull UUID storageUuid, @BytesLong long bytes, 1760 @RequiresPermission @AllocateFlags int flags) throws IOException { 1761 try { 1762 mStorageManager.allocateBytes(convert(storageUuid), bytes, flags, 1763 mContext.getOpPackageName()); 1764 } catch (ParcelableException e) { 1765 e.maybeRethrow(IOException.class); 1766 } catch (RemoteException e) { 1767 throw e.rethrowFromSystemServer(); 1768 } 1769 } 1770 1771 /** 1772 * Allocate the requested number of bytes for your application to use in the 1773 * given open file. This will cause the system to delete any cached files 1774 * necessary to satisfy your request. 1775 * <p> 1776 * Attempts to allocate disk space beyond the value returned by 1777 * {@link #getAllocatableBytes(UUID)} will fail. 1778 * <p> 1779 * This method guarantees that bytes have been allocated to the opened file, 1780 * otherwise it will throw if fast allocation is not possible. Fast 1781 * allocation is typically only supported in private app data directories, 1782 * and on shared/external storage devices which are emulated. 1783 * <p> 1784 * If you're progressively allocating an unbounded amount of storage space 1785 * (such as when recording a video) you should avoid calling this method 1786 * more than once every 60 seconds. 1787 * 1788 * @param fd the open file that you'd like to allocate disk space for. 1789 * @param bytes the number of bytes to allocate. This is the desired final 1790 * size of the open file. If the open file is smaller than this 1791 * requested size, it will be extended without modifying any 1792 * existing contents. If the open file is larger than this 1793 * requested size, it will be truncated. 1794 * @throws IOException when the storage device isn't present, or when it 1795 * doesn't support allocating space, or if the device had 1796 * trouble allocating the requested space. 1797 * @see #getAllocatableBytes(UUID, int) 1798 * @see #isAllocationSupported(FileDescriptor) 1799 * @see Environment#isExternalStorageEmulated(File) 1800 */ 1801 @WorkerThread 1802 public void allocateBytes(FileDescriptor fd, @BytesLong long bytes) throws IOException { 1803 allocateBytes(fd, bytes, 0); 1804 } 1805 1806 /** @hide */ 1807 @SystemApi 1808 @WorkerThread 1809 @SuppressLint("Doclava125") 1810 public void allocateBytes(FileDescriptor fd, @BytesLong long bytes, 1811 @RequiresPermission @AllocateFlags int flags) throws IOException { 1812 final File file = ParcelFileDescriptor.getFile(fd); 1813 final UUID uuid = getUuidForPath(file); 1814 for (int i = 0; i < 3; i++) { 1815 try { 1816 final long haveBytes = Os.fstat(fd).st_blocks * 512; 1817 final long needBytes = bytes - haveBytes; 1818 1819 if (needBytes > 0) { 1820 allocateBytes(uuid, needBytes, flags); 1821 } 1822 1823 try { 1824 Os.posix_fallocate(fd, 0, bytes); 1825 return; 1826 } catch (ErrnoException e) { 1827 if (e.errno == OsConstants.ENOSYS || e.errno == OsConstants.ENOTSUP) { 1828 Log.w(TAG, "fallocate() not supported; falling back to ftruncate()"); 1829 Os.ftruncate(fd, bytes); 1830 return; 1831 } else { 1832 throw e; 1833 } 1834 } 1835 } catch (ErrnoException e) { 1836 if (e.errno == OsConstants.ENOSPC) { 1837 Log.w(TAG, "Odd, not enough space; let's try again?"); 1838 continue; 1839 } 1840 throw e.rethrowAsIOException(); 1841 } 1842 } 1843 throw new IOException( 1844 "Well this is embarassing; we can't allocate " + bytes + " for " + file); 1845 } 1846 1847 private static final String XATTR_CACHE_GROUP = "user.cache_group"; 1848 private static final String XATTR_CACHE_TOMBSTONE = "user.cache_tombstone"; 1849 1850 /** {@hide} */ 1851 private static void setCacheBehavior(File path, String name, boolean enabled) 1852 throws IOException { 1853 if (!path.isDirectory()) { 1854 throw new IOException("Cache behavior can only be set on directories"); 1855 } 1856 if (enabled) { 1857 try { 1858 Os.setxattr(path.getAbsolutePath(), name, 1859 "1".getBytes(StandardCharsets.UTF_8), 0); 1860 } catch (ErrnoException e) { 1861 throw e.rethrowAsIOException(); 1862 } 1863 } else { 1864 try { 1865 Os.removexattr(path.getAbsolutePath(), name); 1866 } catch (ErrnoException e) { 1867 if (e.errno != OsConstants.ENODATA) { 1868 throw e.rethrowAsIOException(); 1869 } 1870 } 1871 } 1872 } 1873 1874 /** {@hide} */ 1875 private static boolean isCacheBehavior(File path, String name) throws IOException { 1876 try { 1877 Os.getxattr(path.getAbsolutePath(), name); 1878 return true; 1879 } catch (ErrnoException e) { 1880 if (e.errno != OsConstants.ENODATA) { 1881 throw e.rethrowAsIOException(); 1882 } else { 1883 return false; 1884 } 1885 } 1886 } 1887 1888 /** 1889 * Enable or disable special cache behavior that treats this directory and 1890 * its contents as an entire group. 1891 * <p> 1892 * When enabled and this directory is considered for automatic deletion by 1893 * the OS, all contained files will either be deleted together, or not at 1894 * all. This is useful when you have a directory that contains several 1895 * related metadata files that depend on each other, such as movie file and 1896 * a subtitle file. 1897 * <p> 1898 * When enabled, the <em>newest</em> {@link File#lastModified()} value of 1899 * any contained files is considered the modified time of the entire 1900 * directory. 1901 * <p> 1902 * This behavior can only be set on a directory, and it applies recursively 1903 * to all contained files and directories. 1904 */ 1905 public void setCacheBehaviorGroup(File path, boolean group) throws IOException { 1906 setCacheBehavior(path, XATTR_CACHE_GROUP, group); 1907 } 1908 1909 /** 1910 * Read the current value set by 1911 * {@link #setCacheBehaviorGroup(File, boolean)}. 1912 */ 1913 public boolean isCacheBehaviorGroup(File path) throws IOException { 1914 return isCacheBehavior(path, XATTR_CACHE_GROUP); 1915 } 1916 1917 /** 1918 * Enable or disable special cache behavior that leaves deleted cache files 1919 * intact as tombstones. 1920 * <p> 1921 * When enabled and a file contained in this directory is automatically 1922 * deleted by the OS, the file will be truncated to have a length of 0 bytes 1923 * instead of being fully deleted. This is useful if you need to distinguish 1924 * between a file that was deleted versus one that never existed. 1925 * <p> 1926 * This behavior can only be set on a directory, and it applies recursively 1927 * to all contained files and directories. 1928 * <p class="note"> 1929 * Note: this behavior is ignored completely if the user explicitly requests 1930 * that all cached data be cleared. 1931 * </p> 1932 */ 1933 public void setCacheBehaviorTombstone(File path, boolean tombstone) throws IOException { 1934 setCacheBehavior(path, XATTR_CACHE_TOMBSTONE, tombstone); 1935 } 1936 1937 /** 1938 * Read the current value set by 1939 * {@link #setCacheBehaviorTombstone(File, boolean)}. 1940 */ 1941 public boolean isCacheBehaviorTombstone(File path) throws IOException { 1942 return isCacheBehavior(path, XATTR_CACHE_TOMBSTONE); 1943 } 1944 1945 /** {@hide} */ 1946 public static UUID convert(String uuid) { 1947 if (Objects.equals(uuid, UUID_PRIVATE_INTERNAL)) { 1948 return UUID_DEFAULT; 1949 } else if (Objects.equals(uuid, UUID_PRIMARY_PHYSICAL)) { 1950 return UUID_PRIMARY_PHYSICAL_; 1951 } else if (Objects.equals(uuid, UUID_SYSTEM)) { 1952 return UUID_SYSTEM_; 1953 } else { 1954 return UUID.fromString(uuid); 1955 } 1956 } 1957 1958 /** {@hide} */ 1959 public static String convert(UUID storageUuid) { 1960 if (UUID_DEFAULT.equals(storageUuid)) { 1961 return UUID_PRIVATE_INTERNAL; 1962 } else if (UUID_PRIMARY_PHYSICAL_.equals(storageUuid)) { 1963 return UUID_PRIMARY_PHYSICAL; 1964 } else if (UUID_SYSTEM_.equals(storageUuid)) { 1965 return UUID_SYSTEM; 1966 } else { 1967 return storageUuid.toString(); 1968 } 1969 } 1970 1971 private final Object mFuseAppLoopLock = new Object(); 1972 1973 @GuardedBy("mFuseAppLoopLock") 1974 private @Nullable FuseAppLoop mFuseAppLoop = null; 1975 1976 /// Consts to match the password types in cryptfs.h 1977 /** @hide */ 1978 public static final int CRYPT_TYPE_PASSWORD = 0; 1979 /** @hide */ 1980 public static final int CRYPT_TYPE_DEFAULT = 1; 1981 /** @hide */ 1982 public static final int CRYPT_TYPE_PATTERN = 2; 1983 /** @hide */ 1984 public static final int CRYPT_TYPE_PIN = 3; 1985 1986 // Constants for the data available via StorageManagerService.getField. 1987 /** @hide */ 1988 public static final String SYSTEM_LOCALE_KEY = "SystemLocale"; 1989 /** @hide */ 1990 public static final String OWNER_INFO_KEY = "OwnerInfo"; 1991 /** @hide */ 1992 public static final String PATTERN_VISIBLE_KEY = "PatternVisible"; 1993 /** @hide */ 1994 public static final String PASSWORD_VISIBLE_KEY = "PasswordVisible"; 1995} 1996