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