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