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 | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 19 | import static android.net.TrafficStats.MB_IN_BYTES; |
| 20 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
Daniel Nishi | 690346b | 2016-06-17 10:21:48 -0700 | [diff] [blame] | 23 | import android.annotation.SdkConstant; |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 24 | import android.app.ActivityThread; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 25 | import android.content.ContentResolver; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 27 | import android.content.pm.IPackageMoveObserver; |
| 28 | import android.content.pm.PackageManager; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 29 | import android.os.Binder; |
Mike Lockwood | cba928c | 2011-08-17 15:58:52 -0700 | [diff] [blame] | 30 | import android.os.Environment; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 31 | import android.os.FileUtils; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 32 | import android.os.Handler; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 33 | import android.os.Looper; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 34 | import android.os.Message; |
Daichi Hirono | 9e8d9e2 | 2015-11-13 14:37:00 +0900 | [diff] [blame] | 35 | import android.os.ParcelFileDescriptor; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 36 | import android.os.RemoteException; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 37 | import android.os.ServiceManager; |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 38 | import android.os.SystemProperties; |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 39 | import android.os.UserHandle; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 40 | import android.provider.Settings; |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 41 | import android.text.TextUtils; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 42 | import android.util.Log; |
Jeff Sharkey | b42d694 | 2015-04-28 22:25:26 -0700 | [diff] [blame] | 43 | import android.util.Slog; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 44 | import android.util.SparseArray; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 45 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 46 | import com.android.internal.os.SomeArgs; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 47 | import com.android.internal.util.Preconditions; |
| 48 | |
Felipe Leme | 18202e00b | 2016-05-12 12:56:28 -0700 | [diff] [blame] | 49 | import java.io.BufferedReader; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 50 | import java.io.File; |
Felipe Leme | 18202e00b | 2016-05-12 12:56:28 -0700 | [diff] [blame] | 51 | import java.io.FileInputStream; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 52 | import java.io.IOException; |
Felipe Leme | 18202e00b | 2016-05-12 12:56:28 -0700 | [diff] [blame] | 53 | import java.io.InputStreamReader; |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 54 | import java.lang.ref.WeakReference; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 55 | import java.util.ArrayList; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 56 | import java.util.Arrays; |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 57 | import java.util.Collections; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 58 | import java.util.Iterator; |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 59 | import java.util.List; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 60 | import java.util.Objects; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 61 | import java.util.concurrent.atomic.AtomicInteger; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 62 | |
| 63 | /** |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 64 | * StorageManager is the interface to the systems storage service. The storage |
| 65 | * manager handles storage-related items such as Opaque Binary Blobs (OBBs). |
| 66 | * <p> |
| 67 | * OBBs contain a filesystem that maybe be encrypted on disk and mounted |
| 68 | * on-demand from an application. OBBs are a good way of providing large amounts |
| 69 | * of binary assets without packaging them into APKs as they may be multiple |
| 70 | * gigabytes in size. However, due to their size, they're most likely stored in |
| 71 | * a shared storage pool accessible from all programs. The system does not |
| 72 | * guarantee the security of the OBB file itself: if any program modifies the |
| 73 | * OBB, there is no guarantee that a read from that OBB will produce the |
| 74 | * expected output. |
| 75 | * <p> |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 76 | * Get an instance of this class by calling |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 77 | * {@link android.content.Context#getSystemService(java.lang.String)} with an |
| 78 | * argument of {@link android.content.Context#STORAGE_SERVICE}. |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 79 | */ |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 80 | public class StorageManager { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 81 | private static final String TAG = "StorageManager"; |
| 82 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 83 | /** {@hide} */ |
| 84 | public static final String PROP_PRIMARY_PHYSICAL = "ro.vold.primary_physical"; |
Jeff Sharkey | 74acbbb | 2015-04-21 12:14:03 -0700 | [diff] [blame] | 85 | /** {@hide} */ |
Jeff Sharkey | 0d838a0 | 2015-05-13 13:54:30 -0700 | [diff] [blame] | 86 | public static final String PROP_HAS_ADOPTABLE = "vold.has_adoptable"; |
| 87 | /** {@hide} */ |
Jeff Sharkey | 74acbbb | 2015-04-21 12:14:03 -0700 | [diff] [blame] | 88 | public static final String PROP_FORCE_ADOPTABLE = "persist.fw.force_adoptable"; |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 89 | /** {@hide} */ |
Jeff Sharkey | ba51235 | 2015-11-12 20:17:45 -0800 | [diff] [blame] | 90 | public static final String PROP_EMULATE_FBE = "persist.sys.emulate_fbe"; |
Jeff Sharkey | 33dd156 | 2016-04-07 11:05:33 -0600 | [diff] [blame] | 91 | /** {@hide} */ |
| 92 | public static final String PROP_SDCARDFS = "persist.sys.sdcardfs"; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 93 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 94 | /** {@hide} */ |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 95 | public static final String UUID_PRIVATE_INTERNAL = null; |
| 96 | /** {@hide} */ |
| 97 | public static final String UUID_PRIMARY_PHYSICAL = "primary_physical"; |
| 98 | |
Daniel Nishi | 690346b | 2016-06-17 10:21:48 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Activity Action: Allows the user to manage their storage. This activity provides the ability |
| 102 | * to free up space on the device by deleting data such as apps. |
| 103 | * <p> |
| 104 | * Input: Nothing. |
| 105 | * <p> |
| 106 | * Output: Nothing. |
| 107 | */ |
| 108 | @SdkConstant(SdkConstant.SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 109 | public static final String ACTION_MANAGE_STORAGE |
| 110 | = "android.os.storage.action.MANAGE_STORAGE"; |
| 111 | |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 112 | /** {@hide} */ |
| 113 | public static final int DEBUG_FORCE_ADOPTABLE = 1 << 0; |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 114 | /** {@hide} */ |
| 115 | public static final int DEBUG_EMULATE_FBE = 1 << 1; |
Jeff Sharkey | 33dd156 | 2016-04-07 11:05:33 -0600 | [diff] [blame] | 116 | /** {@hide} */ |
| 117 | public static final int DEBUG_SDCARDFS_FORCE_ON = 1 << 2; |
| 118 | /** {@hide} */ |
| 119 | public static final int DEBUG_SDCARDFS_FORCE_OFF = 1 << 3; |
Jeff Sharkey | 4c099d0 | 2015-05-15 13:45:00 -0700 | [diff] [blame] | 120 | |
Jeff Sharkey | 47f7108 | 2016-02-01 17:03:54 -0700 | [diff] [blame] | 121 | // NOTE: keep in sync with installd |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 122 | /** {@hide} */ |
Jeff Sharkey | 47f7108 | 2016-02-01 17:03:54 -0700 | [diff] [blame] | 123 | public static final int FLAG_STORAGE_DE = 1 << 0; |
| 124 | /** {@hide} */ |
| 125 | public static final int FLAG_STORAGE_CE = 1 << 1; |
| 126 | |
| 127 | /** {@hide} */ |
| 128 | public static final int FLAG_FOR_WRITE = 1 << 8; |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 129 | /** {@hide} */ |
| 130 | public static final int FLAG_REAL_STATE = 1 << 9; |
| 131 | /** {@hide} */ |
| 132 | public static final int FLAG_INCLUDE_INVISIBLE = 1 << 10; |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 133 | |
Jeff Sharkey | 4815ed4 | 2016-05-26 09:31:04 -0600 | [diff] [blame] | 134 | private static volatile IMountService sMountService = null; |
Felipe Leme | 179923a | 2016-07-19 09:33:31 -0700 | [diff] [blame] | 135 | |
| 136 | // TODO: the location of the primary storage block varies from device to device, so we need to |
| 137 | // try the most likely candidates - a long-term solution would be a device-specific vold |
| 138 | // function that returns the calculated size. |
| 139 | private static final String[] INTERNAL_STORAGE_SIZE_PATHS = { |
| 140 | "/sys/block/mmcblk0/size", |
| 141 | "/sys/block/sda/size" |
| 142 | }; |
| 143 | private static final int INTERNAL_STORAGE_SECTOR_SIZE = 512; |
Jeff Sharkey | 4815ed4 | 2016-05-26 09:31:04 -0600 | [diff] [blame] | 144 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 145 | private final Context mContext; |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 146 | private final ContentResolver mResolver; |
| 147 | |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 148 | private final IMountService mMountService; |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 149 | private final Looper mLooper; |
| 150 | private final AtomicInteger mNextNonce = new AtomicInteger(0); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 151 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 152 | private final ArrayList<StorageEventListenerDelegate> mDelegates = new ArrayList<>(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 153 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 154 | private static class StorageEventListenerDelegate extends IMountServiceListener.Stub implements |
| 155 | Handler.Callback { |
| 156 | private static final int MSG_STORAGE_STATE_CHANGED = 1; |
| 157 | private static final int MSG_VOLUME_STATE_CHANGED = 2; |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 158 | private static final int MSG_VOLUME_RECORD_CHANGED = 3; |
| 159 | private static final int MSG_VOLUME_FORGOTTEN = 4; |
| 160 | private static final int MSG_DISK_SCANNED = 5; |
Makoto Onuki | 9dc575d | 2015-06-12 16:10:25 -0700 | [diff] [blame] | 161 | private static final int MSG_DISK_DESTROYED = 6; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 162 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 163 | final StorageEventListener mCallback; |
| 164 | final Handler mHandler; |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 165 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 166 | public StorageEventListenerDelegate(StorageEventListener callback, Looper looper) { |
| 167 | mCallback = callback; |
| 168 | mHandler = new Handler(looper, this); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 171 | @Override |
| 172 | public boolean handleMessage(Message msg) { |
| 173 | final SomeArgs args = (SomeArgs) msg.obj; |
| 174 | switch (msg.what) { |
| 175 | case MSG_STORAGE_STATE_CHANGED: |
| 176 | mCallback.onStorageStateChanged((String) args.arg1, (String) args.arg2, |
| 177 | (String) args.arg3); |
| 178 | args.recycle(); |
| 179 | return true; |
| 180 | case MSG_VOLUME_STATE_CHANGED: |
| 181 | mCallback.onVolumeStateChanged((VolumeInfo) args.arg1, args.argi2, args.argi3); |
| 182 | args.recycle(); |
| 183 | return true; |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 184 | case MSG_VOLUME_RECORD_CHANGED: |
| 185 | mCallback.onVolumeRecordChanged((VolumeRecord) args.arg1); |
| 186 | args.recycle(); |
| 187 | return true; |
| 188 | case MSG_VOLUME_FORGOTTEN: |
| 189 | mCallback.onVolumeForgotten((String) args.arg1); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 190 | args.recycle(); |
| 191 | return true; |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 192 | case MSG_DISK_SCANNED: |
| 193 | mCallback.onDiskScanned((DiskInfo) args.arg1, args.argi2); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 194 | args.recycle(); |
| 195 | return true; |
Makoto Onuki | 9dc575d | 2015-06-12 16:10:25 -0700 | [diff] [blame] | 196 | case MSG_DISK_DESTROYED: |
| 197 | mCallback.onDiskDestroyed((DiskInfo) args.arg1); |
| 198 | args.recycle(); |
| 199 | return true; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 200 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 201 | args.recycle(); |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void onUsbMassStorageConnectionChanged(boolean connected) throws RemoteException { |
| 207 | // Ignored |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public void onStorageStateChanged(String path, String oldState, String newState) { |
| 212 | final SomeArgs args = SomeArgs.obtain(); |
| 213 | args.arg1 = path; |
| 214 | args.arg2 = oldState; |
| 215 | args.arg3 = newState; |
| 216 | mHandler.obtainMessage(MSG_STORAGE_STATE_CHANGED, args).sendToTarget(); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void onVolumeStateChanged(VolumeInfo vol, int oldState, int newState) { |
| 221 | final SomeArgs args = SomeArgs.obtain(); |
| 222 | args.arg1 = vol; |
| 223 | args.argi2 = oldState; |
| 224 | args.argi3 = newState; |
| 225 | mHandler.obtainMessage(MSG_VOLUME_STATE_CHANGED, args).sendToTarget(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 226 | } |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 227 | |
| 228 | @Override |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 229 | public void onVolumeRecordChanged(VolumeRecord rec) { |
| 230 | final SomeArgs args = SomeArgs.obtain(); |
| 231 | args.arg1 = rec; |
| 232 | mHandler.obtainMessage(MSG_VOLUME_RECORD_CHANGED, args).sendToTarget(); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public void onVolumeForgotten(String fsUuid) { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 237 | final SomeArgs args = SomeArgs.obtain(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 238 | args.arg1 = fsUuid; |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 239 | mHandler.obtainMessage(MSG_VOLUME_FORGOTTEN, args).sendToTarget(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 240 | } |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 241 | |
| 242 | @Override |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 243 | public void onDiskScanned(DiskInfo disk, int volumeCount) { |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 244 | final SomeArgs args = SomeArgs.obtain(); |
| 245 | args.arg1 = disk; |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 246 | args.argi2 = volumeCount; |
| 247 | mHandler.obtainMessage(MSG_DISK_SCANNED, args).sendToTarget(); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 248 | } |
Makoto Onuki | 9dc575d | 2015-06-12 16:10:25 -0700 | [diff] [blame] | 249 | |
| 250 | @Override |
| 251 | public void onDiskDestroyed(DiskInfo disk) throws RemoteException { |
| 252 | final SomeArgs args = SomeArgs.obtain(); |
| 253 | args.arg1 = disk; |
| 254 | mHandler.obtainMessage(MSG_DISK_DESTROYED, args).sendToTarget(); |
| 255 | } |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 259 | * Binder listener for OBB action results. |
| 260 | */ |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 261 | private final ObbActionListener mObbActionListener = new ObbActionListener(); |
| 262 | |
| 263 | private class ObbActionListener extends IObbActionListener.Stub { |
Gilles Debunne | 37051cd | 2011-05-25 16:27:13 -0700 | [diff] [blame] | 264 | @SuppressWarnings("hiding") |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 265 | private SparseArray<ObbListenerDelegate> mListeners = new SparseArray<ObbListenerDelegate>(); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 266 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 267 | @Override |
Gilles Debunne | 37051cd | 2011-05-25 16:27:13 -0700 | [diff] [blame] | 268 | public void onObbResult(String filename, int nonce, int status) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 269 | final ObbListenerDelegate delegate; |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 270 | synchronized (mListeners) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 271 | delegate = mListeners.get(nonce); |
| 272 | if (delegate != null) { |
| 273 | mListeners.remove(nonce); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 276 | |
| 277 | if (delegate != null) { |
| 278 | delegate.sendObbStateChanged(filename, status); |
| 279 | } |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 282 | public int addListener(OnObbStateChangeListener listener) { |
| 283 | final ObbListenerDelegate delegate = new ObbListenerDelegate(listener); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 284 | |
| 285 | synchronized (mListeners) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 286 | mListeners.put(delegate.nonce, delegate); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 287 | } |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 288 | |
| 289 | return delegate.nonce; |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 293 | private int getNextNonce() { |
| 294 | return mNextNonce.getAndIncrement(); |
| 295 | } |
| 296 | |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 297 | /** |
| 298 | * Private class containing sender and receiver code for StorageEvents. |
| 299 | */ |
| 300 | private class ObbListenerDelegate { |
| 301 | private final WeakReference<OnObbStateChangeListener> mObbEventListenerRef; |
| 302 | private final Handler mHandler; |
| 303 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 304 | private final int nonce; |
| 305 | |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 306 | ObbListenerDelegate(OnObbStateChangeListener listener) { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 307 | nonce = getNextNonce(); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 308 | mObbEventListenerRef = new WeakReference<OnObbStateChangeListener>(listener); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 309 | mHandler = new Handler(mLooper) { |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 310 | @Override |
| 311 | public void handleMessage(Message msg) { |
Gilles Debunne | 37051cd | 2011-05-25 16:27:13 -0700 | [diff] [blame] | 312 | final OnObbStateChangeListener changeListener = getListener(); |
| 313 | if (changeListener == null) { |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 314 | return; |
| 315 | } |
| 316 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 317 | changeListener.onObbStateChange((String) msg.obj, msg.arg1); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 318 | } |
| 319 | }; |
| 320 | } |
| 321 | |
| 322 | OnObbStateChangeListener getListener() { |
| 323 | if (mObbEventListenerRef == null) { |
| 324 | return null; |
| 325 | } |
| 326 | return mObbEventListenerRef.get(); |
| 327 | } |
| 328 | |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 329 | void sendObbStateChanged(String path, int state) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 330 | mHandler.obtainMessage(0, state, 0, path).sendToTarget(); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 334 | /** {@hide} */ |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 335 | @Deprecated |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 336 | public static StorageManager from(Context context) { |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 337 | return context.getSystemService(StorageManager.class); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 338 | } |
| 339 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 340 | /** |
| 341 | * Constructs a StorageManager object through which an application can |
| 342 | * can communicate with the systems mount service. |
Daichi Hirono | 9e8d9e2 | 2015-11-13 14:37:00 +0900 | [diff] [blame] | 343 | * |
Jeff Smith | a45746e | 2012-07-19 14:19:24 -0500 | [diff] [blame] | 344 | * @param tgtLooper The {@link android.os.Looper} which events will be received on. |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 345 | * |
| 346 | * <p>Applications can get instance of this class by calling |
| 347 | * {@link android.content.Context#getSystemService(java.lang.String)} with an argument |
| 348 | * of {@link android.content.Context#STORAGE_SERVICE}. |
| 349 | * |
| 350 | * @hide |
| 351 | */ |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 352 | public StorageManager(Context context, Looper looper) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 353 | mContext = context; |
| 354 | mResolver = context.getContentResolver(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 355 | mLooper = looper; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 356 | mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount")); |
| 357 | if (mMountService == null) { |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 358 | throw new IllegalStateException("Failed to find running mount service"); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 359 | } |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 360 | } |
| 361 | |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 362 | /** |
| 363 | * Registers a {@link android.os.storage.StorageEventListener StorageEventListener}. |
| 364 | * |
| 365 | * @param listener A {@link android.os.storage.StorageEventListener StorageEventListener} object. |
| 366 | * |
Kenny Root | ec7c9ff | 2011-01-17 09:11:21 -0800 | [diff] [blame] | 367 | * @hide |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 368 | */ |
| 369 | public void registerListener(StorageEventListener listener) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 370 | synchronized (mDelegates) { |
| 371 | final StorageEventListenerDelegate delegate = new StorageEventListenerDelegate(listener, |
| 372 | mLooper); |
| 373 | try { |
| 374 | mMountService.registerListener(delegate); |
| 375 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 376 | throw e.rethrowFromSystemServer(); |
Chuanxia Dong | 6614bb6 | 2012-05-29 12:28:24 +0800 | [diff] [blame] | 377 | } |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 378 | mDelegates.add(delegate); |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Unregisters a {@link android.os.storage.StorageEventListener StorageEventListener}. |
| 384 | * |
| 385 | * @param listener A {@link android.os.storage.StorageEventListener StorageEventListener} object. |
| 386 | * |
Kenny Root | ec7c9ff | 2011-01-17 09:11:21 -0800 | [diff] [blame] | 387 | * @hide |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 388 | */ |
| 389 | public void unregisterListener(StorageEventListener listener) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 390 | synchronized (mDelegates) { |
| 391 | for (Iterator<StorageEventListenerDelegate> i = mDelegates.iterator(); i.hasNext();) { |
| 392 | final StorageEventListenerDelegate delegate = i.next(); |
| 393 | if (delegate.mCallback == listener) { |
| 394 | try { |
| 395 | mMountService.unregisterListener(delegate); |
| 396 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 397 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 398 | } |
| 399 | i.remove(); |
| 400 | } |
| 401 | } |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 402 | } |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Enables USB Mass Storage (UMS) on the device. |
Kenny Root | ec7c9ff | 2011-01-17 09:11:21 -0800 | [diff] [blame] | 407 | * |
| 408 | * @hide |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 409 | */ |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 410 | @Deprecated |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 411 | public void enableUsbMassStorage() { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Disables USB Mass Storage (UMS) on the device. |
Kenny Root | ec7c9ff | 2011-01-17 09:11:21 -0800 | [diff] [blame] | 416 | * |
| 417 | * @hide |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 418 | */ |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 419 | @Deprecated |
Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 420 | public void disableUsbMassStorage() { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Query if a USB Mass Storage (UMS) host is connected. |
| 425 | * @return true if UMS host is connected. |
Kenny Root | ec7c9ff | 2011-01-17 09:11:21 -0800 | [diff] [blame] | 426 | * |
| 427 | * @hide |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 428 | */ |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 429 | @Deprecated |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 430 | public boolean isUsbMassStorageConnected() { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 431 | return false; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Query if a USB Mass Storage (UMS) is enabled on the device. |
| 436 | * @return true if UMS host is enabled. |
Kenny Root | ec7c9ff | 2011-01-17 09:11:21 -0800 | [diff] [blame] | 437 | * |
| 438 | * @hide |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 439 | */ |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 440 | @Deprecated |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 441 | public boolean isUsbMassStorageEnabled() { |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 442 | return false; |
| 443 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 444 | |
| 445 | /** |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 446 | * Mount an Opaque Binary Blob (OBB) file. If a <code>key</code> is |
| 447 | * specified, it is supplied to the mounting process to be used in any |
| 448 | * encryption used in the OBB. |
| 449 | * <p> |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 450 | * The OBB will remain mounted for as long as the StorageManager reference |
| 451 | * is held by the application. As soon as this reference is lost, the OBBs |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 452 | * in use will be unmounted. The {@link OnObbStateChangeListener} registered |
| 453 | * with this call will receive the success or failure of this operation. |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 454 | * <p> |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 455 | * <em>Note:</em> you can only mount OBB files for which the OBB tag on the |
| 456 | * file matches a package ID that is owned by the calling program's UID. |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 457 | * That is, shared UID applications can attempt to mount any other |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 458 | * application's OBB that shares its UID. |
Daichi Hirono | 9e8d9e2 | 2015-11-13 14:37:00 +0900 | [diff] [blame] | 459 | * |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 460 | * @param rawPath the path to the OBB file |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 461 | * @param key secret used to encrypt the OBB; may be <code>null</code> if no |
| 462 | * encryption was used on the OBB. |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 463 | * @param listener will receive the success or failure of the operation |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 464 | * @return whether the mount call was successfully queued or not |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 465 | */ |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 466 | public boolean mountObb(String rawPath, String key, OnObbStateChangeListener listener) { |
| 467 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
| 468 | Preconditions.checkNotNull(listener, "listener cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 469 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 470 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 471 | final String canonicalPath = new File(rawPath).getCanonicalPath(); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 472 | final int nonce = mObbActionListener.addListener(listener); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 473 | mMountService.mountObb(rawPath, canonicalPath, key, mObbActionListener, nonce); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 474 | return true; |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 475 | } catch (IOException e) { |
| 476 | throw new IllegalArgumentException("Failed to resolve path: " + rawPath, e); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 477 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 478 | throw e.rethrowFromSystemServer(); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 479 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /** |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 483 | * Unmount an Opaque Binary Blob (OBB) file asynchronously. If the |
| 484 | * <code>force</code> flag is true, it will kill any application needed to |
| 485 | * unmount the given OBB (even the calling application). |
| 486 | * <p> |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 487 | * The {@link OnObbStateChangeListener} registered with this call will |
| 488 | * receive the success or failure of this operation. |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 489 | * <p> |
| 490 | * <em>Note:</em> you can only mount OBB files for which the OBB tag on the |
| 491 | * file matches a package ID that is owned by the calling program's UID. |
| 492 | * That is, shared UID applications can obtain access to any other |
| 493 | * application's OBB that shares its UID. |
Kenny Root | 02ca31f | 2010-08-12 07:36:02 -0700 | [diff] [blame] | 494 | * <p> |
Daichi Hirono | 9e8d9e2 | 2015-11-13 14:37:00 +0900 | [diff] [blame] | 495 | * |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 496 | * @param rawPath path to the OBB file |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 497 | * @param force whether to kill any programs using this in order to unmount |
| 498 | * it |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 499 | * @param listener will receive the success or failure of the operation |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 500 | * @return whether the unmount call was successfully queued or not |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 501 | */ |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 502 | public boolean unmountObb(String rawPath, boolean force, OnObbStateChangeListener listener) { |
| 503 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
| 504 | Preconditions.checkNotNull(listener, "listener cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 505 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 506 | try { |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 507 | final int nonce = mObbActionListener.addListener(listener); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 508 | mMountService.unmountObb(rawPath, force, mObbActionListener, nonce); |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 509 | return true; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 510 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 511 | throw e.rethrowFromSystemServer(); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 512 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 515 | /** |
| 516 | * Check whether an Opaque Binary Blob (OBB) is mounted or not. |
Daichi Hirono | 9e8d9e2 | 2015-11-13 14:37:00 +0900 | [diff] [blame] | 517 | * |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 518 | * @param rawPath path to OBB image |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 519 | * @return true if OBB is mounted; false if not mounted or on error |
| 520 | */ |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 521 | public boolean isObbMounted(String rawPath) { |
| 522 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 523 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 524 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 525 | return mMountService.isObbMounted(rawPath); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 526 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 527 | throw e.rethrowFromSystemServer(); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 528 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /** |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 532 | * Check the mounted path of an Opaque Binary Blob (OBB) file. This will |
| 533 | * give you the path to where you can obtain access to the internals of the |
| 534 | * OBB. |
Daichi Hirono | 9e8d9e2 | 2015-11-13 14:37:00 +0900 | [diff] [blame] | 535 | * |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 536 | * @param rawPath path to OBB image |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 537 | * @return absolute path to mounted OBB image data or <code>null</code> if |
| 538 | * not mounted or exception encountered trying to read status |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 539 | */ |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 540 | public String getMountedObbPath(String rawPath) { |
| 541 | Preconditions.checkNotNull(rawPath, "rawPath cannot be null"); |
Kenny Root | af9d667 | 2010-10-08 09:21:39 -0700 | [diff] [blame] | 542 | |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 543 | try { |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 544 | return mMountService.getMountedObbPath(rawPath); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 545 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 546 | throw e.rethrowFromSystemServer(); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 547 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 548 | } |
Mike Lockwood | d967f46 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 549 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 550 | /** {@hide} */ |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 551 | public @NonNull List<DiskInfo> getDisks() { |
| 552 | try { |
| 553 | return Arrays.asList(mMountService.getDisks()); |
| 554 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 555 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
| 559 | /** {@hide} */ |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 560 | public @Nullable DiskInfo findDiskById(String id) { |
| 561 | Preconditions.checkNotNull(id); |
| 562 | // TODO; go directly to service to make this faster |
| 563 | for (DiskInfo disk : getDisks()) { |
| 564 | if (Objects.equals(disk.id, id)) { |
| 565 | return disk; |
| 566 | } |
| 567 | } |
| 568 | return null; |
| 569 | } |
| 570 | |
| 571 | /** {@hide} */ |
| 572 | public @Nullable VolumeInfo findVolumeById(String id) { |
| 573 | Preconditions.checkNotNull(id); |
| 574 | // TODO; go directly to service to make this faster |
| 575 | for (VolumeInfo vol : getVolumes()) { |
| 576 | if (Objects.equals(vol.id, id)) { |
| 577 | return vol; |
| 578 | } |
| 579 | } |
| 580 | return null; |
| 581 | } |
| 582 | |
| 583 | /** {@hide} */ |
| 584 | public @Nullable VolumeInfo findVolumeByUuid(String fsUuid) { |
| 585 | Preconditions.checkNotNull(fsUuid); |
| 586 | // TODO; go directly to service to make this faster |
| 587 | for (VolumeInfo vol : getVolumes()) { |
| 588 | if (Objects.equals(vol.fsUuid, fsUuid)) { |
| 589 | return vol; |
| 590 | } |
| 591 | } |
| 592 | return null; |
| 593 | } |
| 594 | |
| 595 | /** {@hide} */ |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 596 | public @Nullable VolumeRecord findRecordByUuid(String fsUuid) { |
| 597 | Preconditions.checkNotNull(fsUuid); |
| 598 | // TODO; go directly to service to make this faster |
| 599 | for (VolumeRecord rec : getVolumeRecords()) { |
| 600 | if (Objects.equals(rec.fsUuid, fsUuid)) { |
| 601 | return rec; |
| 602 | } |
| 603 | } |
| 604 | return null; |
| 605 | } |
| 606 | |
| 607 | /** {@hide} */ |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 608 | public @Nullable VolumeInfo findPrivateForEmulated(VolumeInfo emulatedVol) { |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 609 | if (emulatedVol != null) { |
| 610 | return findVolumeById(emulatedVol.getId().replace("emulated", "private")); |
| 611 | } else { |
| 612 | return null; |
| 613 | } |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /** {@hide} */ |
| 617 | public @Nullable VolumeInfo findEmulatedForPrivate(VolumeInfo privateVol) { |
Jeff Sharkey | ef10ee0 | 2015-07-05 14:17:27 -0700 | [diff] [blame] | 618 | if (privateVol != null) { |
| 619 | return findVolumeById(privateVol.getId().replace("private", "emulated")); |
| 620 | } else { |
| 621 | return null; |
| 622 | } |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /** {@hide} */ |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 626 | public @Nullable VolumeInfo findVolumeByQualifiedUuid(String volumeUuid) { |
| 627 | if (Objects.equals(StorageManager.UUID_PRIVATE_INTERNAL, volumeUuid)) { |
| 628 | return findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL); |
| 629 | } else if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL, volumeUuid)) { |
| 630 | return getPrimaryPhysicalVolume(); |
| 631 | } else { |
| 632 | return findVolumeByUuid(volumeUuid); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | /** {@hide} */ |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 637 | public @NonNull List<VolumeInfo> getVolumes() { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 638 | try { |
| 639 | return Arrays.asList(mMountService.getVolumes(0)); |
| 640 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 641 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 642 | } |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /** {@hide} */ |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 646 | public @NonNull List<VolumeInfo> getWritablePrivateVolumes() { |
| 647 | try { |
| 648 | final ArrayList<VolumeInfo> res = new ArrayList<>(); |
| 649 | for (VolumeInfo vol : mMountService.getVolumes(0)) { |
| 650 | if (vol.getType() == VolumeInfo.TYPE_PRIVATE && vol.isMountedWritable()) { |
| 651 | res.add(vol); |
| 652 | } |
| 653 | } |
| 654 | return res; |
| 655 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 656 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | /** {@hide} */ |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 661 | public @NonNull List<VolumeRecord> getVolumeRecords() { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 662 | try { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 663 | return Arrays.asList(mMountService.getVolumeRecords(0)); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 664 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 665 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
| 669 | /** {@hide} */ |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 670 | public @Nullable String getBestVolumeDescription(VolumeInfo vol) { |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 671 | if (vol == null) return null; |
| 672 | |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 673 | // Nickname always takes precedence when defined |
| 674 | if (!TextUtils.isEmpty(vol.fsUuid)) { |
| 675 | final VolumeRecord rec = findRecordByUuid(vol.fsUuid); |
Jeff Sharkey | c840681 | 2015-05-04 12:04:09 -0700 | [diff] [blame] | 676 | if (rec != null && !TextUtils.isEmpty(rec.nickname)) { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 677 | return rec.nickname; |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 678 | } |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 679 | } |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 680 | |
| 681 | if (!TextUtils.isEmpty(vol.getDescription())) { |
| 682 | return vol.getDescription(); |
| 683 | } |
| 684 | |
| 685 | if (vol.disk != null) { |
| 686 | return vol.disk.getDescription(); |
| 687 | } |
| 688 | |
| 689 | return null; |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /** {@hide} */ |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 693 | public @Nullable VolumeInfo getPrimaryPhysicalVolume() { |
| 694 | final List<VolumeInfo> vols = getVolumes(); |
| 695 | for (VolumeInfo vol : vols) { |
| 696 | if (vol.isPrimaryPhysical()) { |
| 697 | return vol; |
| 698 | } |
| 699 | } |
| 700 | return null; |
| 701 | } |
| 702 | |
| 703 | /** {@hide} */ |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 704 | public void mount(String volId) { |
| 705 | try { |
| 706 | mMountService.mount(volId); |
| 707 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 708 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
| 712 | /** {@hide} */ |
| 713 | public void unmount(String volId) { |
| 714 | try { |
| 715 | mMountService.unmount(volId); |
| 716 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 717 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
| 721 | /** {@hide} */ |
| 722 | public void format(String volId) { |
| 723 | try { |
| 724 | mMountService.format(volId); |
| 725 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 726 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
| 730 | /** {@hide} */ |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 731 | public long benchmark(String volId) { |
| 732 | try { |
| 733 | return mMountService.benchmark(volId); |
| 734 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 735 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 9756d75 | 2015-05-14 21:07:42 -0700 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
| 739 | /** {@hide} */ |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 740 | public void partitionPublic(String diskId) { |
| 741 | try { |
| 742 | mMountService.partitionPublic(diskId); |
| 743 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 744 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
| 748 | /** {@hide} */ |
| 749 | public void partitionPrivate(String diskId) { |
| 750 | try { |
| 751 | mMountService.partitionPrivate(diskId); |
| 752 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 753 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
| 757 | /** {@hide} */ |
| 758 | public void partitionMixed(String diskId, int ratio) { |
| 759 | try { |
| 760 | mMountService.partitionMixed(diskId, ratio); |
| 761 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 762 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | |
| 766 | /** {@hide} */ |
Jeff Sharkey | b42d694 | 2015-04-28 22:25:26 -0700 | [diff] [blame] | 767 | public void wipeAdoptableDisks() { |
| 768 | // We only wipe devices in "adoptable" locations, which are in a |
| 769 | // long-term stable slot/location on the device, where apps have a |
| 770 | // reasonable chance of storing sensitive data. (Apps need to go through |
| 771 | // SAF to write to transient volumes.) |
| 772 | final List<DiskInfo> disks = getDisks(); |
| 773 | for (DiskInfo disk : disks) { |
| 774 | final String diskId = disk.getId(); |
| 775 | if (disk.isAdoptable()) { |
| 776 | Slog.d(TAG, "Found adoptable " + diskId + "; wiping"); |
| 777 | try { |
| 778 | // TODO: switch to explicit wipe command when we have it, |
| 779 | // for now rely on the fact that vfat format does a wipe |
| 780 | mMountService.partitionPublic(diskId); |
| 781 | } catch (Exception e) { |
| 782 | Slog.w(TAG, "Failed to wipe " + diskId + ", but soldiering onward", e); |
| 783 | } |
| 784 | } else { |
| 785 | Slog.d(TAG, "Ignorning non-adoptable disk " + disk.getId()); |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | /** {@hide} */ |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 791 | public void setVolumeNickname(String fsUuid, String nickname) { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 792 | try { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 793 | mMountService.setVolumeNickname(fsUuid, nickname); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 794 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 795 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
| 799 | /** {@hide} */ |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 800 | public void setVolumeInited(String fsUuid, boolean inited) { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 801 | try { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 802 | mMountService.setVolumeUserFlags(fsUuid, inited ? VolumeRecord.USER_FLAG_INITED : 0, |
| 803 | VolumeRecord.USER_FLAG_INITED); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 804 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 805 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
| 809 | /** {@hide} */ |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 810 | public void setVolumeSnoozed(String fsUuid, boolean snoozed) { |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 811 | try { |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 812 | mMountService.setVolumeUserFlags(fsUuid, snoozed ? VolumeRecord.USER_FLAG_SNOOZED : 0, |
| 813 | VolumeRecord.USER_FLAG_SNOOZED); |
| 814 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 815 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | b36586a | 2015-04-27 08:42:28 -0700 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
| 819 | /** {@hide} */ |
| 820 | public void forgetVolume(String fsUuid) { |
| 821 | try { |
| 822 | mMountService.forgetVolume(fsUuid); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 823 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 824 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 828 | /** |
| 829 | * This is not the API you're looking for. |
| 830 | * |
| 831 | * @see PackageManager#getPrimaryStorageCurrentVolume() |
| 832 | * @hide |
| 833 | */ |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 834 | public String getPrimaryStorageUuid() { |
| 835 | try { |
| 836 | return mMountService.getPrimaryStorageUuid(); |
| 837 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 838 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 842 | /** |
| 843 | * This is not the API you're looking for. |
| 844 | * |
| 845 | * @see PackageManager#movePrimaryStorage(VolumeInfo) |
| 846 | * @hide |
| 847 | */ |
| 848 | public void setPrimaryStorageUuid(String volumeUuid, IPackageMoveObserver callback) { |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 849 | try { |
Jeff Sharkey | 275e3e4 | 2015-04-24 16:10:32 -0700 | [diff] [blame] | 850 | mMountService.setPrimaryStorageUuid(volumeUuid, callback); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 851 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 852 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
Felipe Leme | c250e45 | 2016-04-11 18:44:33 -0700 | [diff] [blame] | 856 | /** |
| 857 | * Return the {@link StorageVolume} that contains the given file, or {@code null} if none. |
| 858 | */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 859 | public @Nullable StorageVolume getStorageVolume(File file) { |
| 860 | return getStorageVolume(getVolumeList(), file); |
| 861 | } |
| 862 | |
| 863 | /** {@hide} */ |
| 864 | public static @Nullable StorageVolume getStorageVolume(File file, int userId) { |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 865 | return getStorageVolume(getVolumeList(userId, 0), file); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /** {@hide} */ |
| 869 | private static @Nullable StorageVolume getStorageVolume(StorageVolume[] volumes, File file) { |
Felipe Leme | c250e45 | 2016-04-11 18:44:33 -0700 | [diff] [blame] | 870 | if (file == null) { |
| 871 | return null; |
| 872 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 873 | try { |
Jeff Sharkey | 98329459 | 2015-07-13 10:25:31 -0700 | [diff] [blame] | 874 | file = file.getCanonicalFile(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 875 | } catch (IOException ignored) { |
Felipe Leme | c250e45 | 2016-04-11 18:44:33 -0700 | [diff] [blame] | 876 | Slog.d(TAG, "Could not get canonical path for " + file); |
Jeff Sharkey | 98329459 | 2015-07-13 10:25:31 -0700 | [diff] [blame] | 877 | return null; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 878 | } |
| 879 | for (StorageVolume volume : volumes) { |
Jeff Sharkey | 98329459 | 2015-07-13 10:25:31 -0700 | [diff] [blame] | 880 | File volumeFile = volume.getPathFile(); |
| 881 | try { |
| 882 | volumeFile = volumeFile.getCanonicalFile(); |
| 883 | } catch (IOException ignored) { |
| 884 | continue; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 885 | } |
Jeff Sharkey | 98329459 | 2015-07-13 10:25:31 -0700 | [diff] [blame] | 886 | if (FileUtils.contains(volumeFile, file)) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 887 | return volume; |
| 888 | } |
| 889 | } |
| 890 | return null; |
| 891 | } |
| 892 | |
Mike Lockwood | d967f46 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 893 | /** |
| 894 | * Gets the state of a volume via its mountpoint. |
| 895 | * @hide |
| 896 | */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 897 | @Deprecated |
| 898 | public @NonNull String getVolumeState(String mountPoint) { |
| 899 | final StorageVolume vol = getStorageVolume(new File(mountPoint)); |
| 900 | if (vol != null) { |
| 901 | return vol.getState(); |
| 902 | } else { |
| 903 | return Environment.MEDIA_UNKNOWN; |
Mike Lockwood | d967f46 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 907 | /** |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 908 | * Return the list of shared/external storage volumes available to the |
| 909 | * current user. This includes both the primary shared storage device and |
| 910 | * any attached external volumes including SD cards and USB drives. |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 911 | * |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 912 | * @see Environment#getExternalStorageDirectory() |
| 913 | * @see StorageVolume#createAccessIntent(String) |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 914 | */ |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 915 | public @NonNull List<StorageVolume> getStorageVolumes() { |
| 916 | final ArrayList<StorageVolume> res = new ArrayList<>(); |
| 917 | Collections.addAll(res, |
| 918 | getVolumeList(UserHandle.myUserId(), FLAG_REAL_STATE | FLAG_INCLUDE_INVISIBLE)); |
| 919 | return res; |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * Return the primary shared/external storage volume available to the |
| 924 | * current user. This volume is the same storage device returned by |
| 925 | * {@link Environment#getExternalStorageDirectory()} and |
| 926 | * {@link Context#getExternalFilesDir(String)}. |
| 927 | */ |
| 928 | public @NonNull StorageVolume getPrimaryStorageVolume() { |
| 929 | return getVolumeList(UserHandle.myUserId(), FLAG_REAL_STATE | FLAG_INCLUDE_INVISIBLE)[0]; |
| 930 | } |
| 931 | |
Felipe Leme | 18202e00b | 2016-05-12 12:56:28 -0700 | [diff] [blame] | 932 | /** {@hide} */ |
| 933 | public long getPrimaryStorageSize() { |
Felipe Leme | 179923a | 2016-07-19 09:33:31 -0700 | [diff] [blame] | 934 | for (String path : INTERNAL_STORAGE_SIZE_PATHS) { |
| 935 | final long numberBlocks = readLong(path); |
| 936 | if (numberBlocks > 0) { |
| 937 | return numberBlocks * INTERNAL_STORAGE_SECTOR_SIZE; |
| 938 | } |
| 939 | } |
| 940 | return 0; |
Felipe Leme | 18202e00b | 2016-05-12 12:56:28 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | private long readLong(String path) { |
| 944 | try (final FileInputStream fis = new FileInputStream(path); |
| 945 | final BufferedReader reader = new BufferedReader(new InputStreamReader(fis));) { |
| 946 | return Long.parseLong(reader.readLine()); |
| 947 | } catch (Exception e) { |
Felipe Leme | 179923a | 2016-07-19 09:33:31 -0700 | [diff] [blame] | 948 | Slog.w(TAG, "Could not read " + path, e); |
Felipe Leme | 18202e00b | 2016-05-12 12:56:28 -0700 | [diff] [blame] | 949 | return 0; |
| 950 | } |
| 951 | } |
| 952 | |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 953 | /** @removed */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 954 | public @NonNull StorageVolume[] getVolumeList() { |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 955 | return getVolumeList(mContext.getUserId(), 0); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | /** {@hide} */ |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 959 | public static @NonNull StorageVolume[] getVolumeList(int userId, int flags) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 960 | final IMountService mountService = IMountService.Stub.asInterface( |
| 961 | ServiceManager.getService("mount")); |
| 962 | try { |
Svetoslav | 7395cbf | 2015-07-15 15:58:01 -0700 | [diff] [blame] | 963 | String packageName = ActivityThread.currentOpPackageName(); |
| 964 | if (packageName == null) { |
| 965 | // Package name can be null if the activity thread is running but the app |
| 966 | // hasn't bound yet. In this case we fall back to the first package in the |
| 967 | // current UID. This works for runtime permissions as permission state is |
| 968 | // per UID and permission realted app ops are updated for all UID packages. |
| 969 | String[] packageNames = ActivityThread.getPackageManager().getPackagesForUid( |
| 970 | android.os.Process.myUid()); |
| 971 | if (packageNames == null || packageNames.length <= 0) { |
| 972 | return new StorageVolume[0]; |
| 973 | } |
| 974 | packageName = packageNames[0]; |
| 975 | } |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 976 | final int uid = ActivityThread.getPackageManager().getPackageUid(packageName, |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 977 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, userId); |
Svetoslav | 7395cbf | 2015-07-15 15:58:01 -0700 | [diff] [blame] | 978 | if (uid <= 0) { |
| 979 | return new StorageVolume[0]; |
| 980 | } |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 981 | return mountService.getVolumeList(uid, packageName, flags); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 982 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 983 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | d967f46 | 2011-03-24 08:12:30 -0700 | [diff] [blame] | 984 | } |
| 985 | } |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 986 | |
| 987 | /** |
| 988 | * Returns list of paths for all mountable volumes. |
| 989 | * @hide |
| 990 | */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 991 | @Deprecated |
| 992 | public @NonNull String[] getVolumePaths() { |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 993 | StorageVolume[] volumes = getVolumeList(); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 994 | int count = volumes.length; |
| 995 | String[] paths = new String[count]; |
| 996 | for (int i = 0; i < count; i++) { |
| 997 | paths[i] = volumes[i].getPath(); |
| 998 | } |
| 999 | return paths; |
| 1000 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1001 | |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 1002 | /** @removed */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1003 | public @NonNull StorageVolume getPrimaryVolume() { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1004 | return getPrimaryVolume(getVolumeList()); |
| 1005 | } |
| 1006 | |
| 1007 | /** {@hide} */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1008 | public static @NonNull StorageVolume getPrimaryVolume(StorageVolume[] volumes) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1009 | for (StorageVolume volume : volumes) { |
| 1010 | if (volume.isPrimary()) { |
| 1011 | return volume; |
| 1012 | } |
| 1013 | } |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1014 | throw new IllegalStateException("Missing primary storage"); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1015 | } |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 1016 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1017 | /** {@hide} */ |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 1018 | private static final int DEFAULT_THRESHOLD_PERCENTAGE = 10; |
| 1019 | private static final long DEFAULT_THRESHOLD_MAX_BYTES = 500 * MB_IN_BYTES; |
| 1020 | private static final long DEFAULT_FULL_THRESHOLD_BYTES = MB_IN_BYTES; |
| 1021 | |
| 1022 | /** |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 1023 | * Return the number of available bytes until the given path is considered |
| 1024 | * running low on storage. |
| 1025 | * |
| 1026 | * @hide |
| 1027 | */ |
| 1028 | public long getStorageBytesUntilLow(File path) { |
| 1029 | return path.getUsableSpace() - getStorageFullBytes(path); |
| 1030 | } |
| 1031 | |
| 1032 | /** |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 1033 | * Return the number of available bytes at which the given path is |
| 1034 | * considered running low on storage. |
| 1035 | * |
| 1036 | * @hide |
| 1037 | */ |
| 1038 | public long getStorageLowBytes(File path) { |
| 1039 | final long lowPercent = Settings.Global.getInt(mResolver, |
| 1040 | Settings.Global.SYS_STORAGE_THRESHOLD_PERCENTAGE, DEFAULT_THRESHOLD_PERCENTAGE); |
| 1041 | final long lowBytes = (path.getTotalSpace() * lowPercent) / 100; |
| 1042 | |
| 1043 | final long maxLowBytes = Settings.Global.getLong(mResolver, |
| 1044 | Settings.Global.SYS_STORAGE_THRESHOLD_MAX_BYTES, DEFAULT_THRESHOLD_MAX_BYTES); |
| 1045 | |
| 1046 | return Math.min(lowBytes, maxLowBytes); |
| 1047 | } |
| 1048 | |
| 1049 | /** |
| 1050 | * Return the number of available bytes at which the given path is |
| 1051 | * considered full. |
| 1052 | * |
| 1053 | * @hide |
| 1054 | */ |
| 1055 | public long getStorageFullBytes(File path) { |
| 1056 | return Settings.Global.getLong(mResolver, Settings.Global.SYS_STORAGE_FULL_THRESHOLD_BYTES, |
| 1057 | DEFAULT_FULL_THRESHOLD_BYTES); |
| 1058 | } |
Paul Lawrence | e8fdc54 | 2014-05-28 07:14:17 -0700 | [diff] [blame] | 1059 | |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1060 | /** {@hide} */ |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 1061 | public void createUserKey(int userId, int serialNumber, boolean ephemeral) { |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 1062 | try { |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 1063 | mMountService.createUserKey(userId, serialNumber, ephemeral); |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 1064 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1065 | throw e.rethrowFromSystemServer(); |
Paul Crowley | bcf48ed | 2015-04-22 13:36:59 +0100 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | /** {@hide} */ |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 1070 | public void destroyUserKey(int userId) { |
Paul Crowley | 7ec733f | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 1071 | try { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 1072 | mMountService.destroyUserKey(userId); |
| 1073 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1074 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | /** {@hide} */ |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1079 | public void unlockUserKey(int userId, int serialNumber, byte[] token, byte[] secret) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 1080 | try { |
Paul Crowley | faeb3eb | 2016-02-08 15:58:29 +0000 | [diff] [blame] | 1081 | mMountService.unlockUserKey(userId, serialNumber, token, secret); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 1082 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1083 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | /** {@hide} */ |
| 1088 | public void lockUserKey(int userId) { |
| 1089 | try { |
| 1090 | mMountService.lockUserKey(userId); |
| 1091 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1092 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | /** {@hide} */ |
Jeff Sharkey | 47f7108 | 2016-02-01 17:03:54 -0700 | [diff] [blame] | 1097 | public void prepareUserStorage(String volumeUuid, int userId, int serialNumber, int flags) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 1098 | try { |
Jeff Sharkey | 47f7108 | 2016-02-01 17:03:54 -0700 | [diff] [blame] | 1099 | mMountService.prepareUserStorage(volumeUuid, userId, serialNumber, flags); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 1100 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1101 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | /** {@hide} */ |
Jeff Sharkey | fcf1e55 | 2016-04-14 20:44:58 -0600 | [diff] [blame] | 1106 | public void destroyUserStorage(String volumeUuid, int userId, int flags) { |
| 1107 | try { |
| 1108 | mMountService.destroyUserStorage(volumeUuid, userId, flags); |
| 1109 | } catch (RemoteException e) { |
| 1110 | throw e.rethrowFromSystemServer(); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | /** {@hide} */ |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1115 | public static boolean isUserKeyUnlocked(int userId) { |
Jeff Sharkey | 4815ed4 | 2016-05-26 09:31:04 -0600 | [diff] [blame] | 1116 | if (sMountService == null) { |
| 1117 | sMountService = IMountService.Stub |
| 1118 | .asInterface(ServiceManager.getService("mount")); |
| 1119 | } |
| 1120 | if (sMountService == null) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1121 | Slog.w(TAG, "Early during boot, assuming locked"); |
| 1122 | return false; |
| 1123 | } |
| 1124 | final long token = Binder.clearCallingIdentity(); |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 1125 | try { |
Jeff Sharkey | 4815ed4 | 2016-05-26 09:31:04 -0600 | [diff] [blame] | 1126 | return sMountService.isUserKeyUnlocked(userId); |
Paul Crowley | 7ec733f | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 1127 | } catch (RemoteException e) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1128 | throw e.rethrowAsRuntimeException(); |
| 1129 | } finally { |
| 1130 | Binder.restoreCallingIdentity(token); |
Paul Crowley | 7ec733f | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | |
Jeff Sharkey | cf3f0a1 | 2016-03-17 19:57:58 -0600 | [diff] [blame] | 1134 | /** |
Jeff Sharkey | f82c2f0 | 2016-04-12 15:19:10 -0600 | [diff] [blame] | 1135 | * Return if data stored at or under the given path will be encrypted while |
| 1136 | * at rest. This can help apps avoid the overhead of double-encrypting data. |
Jeff Sharkey | cf3f0a1 | 2016-03-17 19:57:58 -0600 | [diff] [blame] | 1137 | */ |
| 1138 | public boolean isEncrypted(File file) { |
| 1139 | if (FileUtils.contains(Environment.getDataDirectory(), file)) { |
| 1140 | return isEncrypted(); |
| 1141 | } else if (FileUtils.contains(Environment.getExpandDirectory(), file)) { |
| 1142 | return true; |
| 1143 | } |
| 1144 | // TODO: extend to support shared storage |
| 1145 | return false; |
| 1146 | } |
| 1147 | |
Paul Lawrence | 20be5d6 | 2016-02-26 13:51:17 -0800 | [diff] [blame] | 1148 | /** {@hide} |
| 1149 | * Is this device encryptable or already encrypted? |
| 1150 | * @return true for encryptable or encrypted |
| 1151 | * false not encrypted and not encryptable |
| 1152 | */ |
| 1153 | public static boolean isEncryptable() { |
| 1154 | final String state = SystemProperties.get("ro.crypto.state", "unsupported"); |
| 1155 | return !"unsupported".equalsIgnoreCase(state); |
| 1156 | } |
| 1157 | |
| 1158 | /** {@hide} |
| 1159 | * Is this device already encrypted? |
| 1160 | * @return true for encrypted. (Implies isEncryptable() == true) |
| 1161 | * false not encrypted |
| 1162 | */ |
| 1163 | public static boolean isEncrypted() { |
| 1164 | final String state = SystemProperties.get("ro.crypto.state", ""); |
| 1165 | return "encrypted".equalsIgnoreCase(state); |
| 1166 | } |
| 1167 | |
| 1168 | /** {@hide} |
| 1169 | * Is this device file encrypted? |
| 1170 | * @return true for file encrypted. (Implies isEncrypted() == true) |
| 1171 | * false not encrypted or block encrypted |
| 1172 | */ |
| 1173 | public static boolean isFileEncryptedNativeOnly() { |
| 1174 | if (!isEncrypted()) { |
| 1175 | return false; |
| 1176 | } |
| 1177 | |
| 1178 | final String status = SystemProperties.get("ro.crypto.type", ""); |
| 1179 | return "file".equalsIgnoreCase(status); |
| 1180 | } |
| 1181 | |
| 1182 | /** {@hide} |
| 1183 | * Is this device block encrypted? |
| 1184 | * @return true for block encrypted. (Implies isEncrypted() == true) |
| 1185 | * false not encrypted or file encrypted |
| 1186 | */ |
| 1187 | public static boolean isBlockEncrypted() { |
| 1188 | if (!isEncrypted()) { |
| 1189 | return false; |
| 1190 | } |
| 1191 | final String status = SystemProperties.get("ro.crypto.type", ""); |
| 1192 | return "block".equalsIgnoreCase(status); |
| 1193 | } |
| 1194 | |
| 1195 | /** {@hide} |
| 1196 | * Is this device block encrypted with credentials? |
| 1197 | * @return true for crediential block encrypted. |
| 1198 | * (Implies isBlockEncrypted() == true) |
| 1199 | * false not encrypted, file encrypted or default block encrypted |
| 1200 | */ |
| 1201 | public static boolean isNonDefaultBlockEncrypted() { |
| 1202 | if (!isBlockEncrypted()) { |
| 1203 | return false; |
| 1204 | } |
| 1205 | |
| 1206 | try { |
| 1207 | IMountService mountService = IMountService.Stub.asInterface( |
| 1208 | ServiceManager.getService("mount")); |
| 1209 | return mountService.getPasswordType() != CRYPT_TYPE_DEFAULT; |
| 1210 | } catch (RemoteException e) { |
| 1211 | Log.e(TAG, "Error getting encryption type"); |
| 1212 | return false; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | /** {@hide} |
| 1217 | * Is this device in the process of being block encrypted? |
| 1218 | * @return true for encrypting. |
| 1219 | * false otherwise |
| 1220 | * Whether device isEncrypted at this point is undefined |
| 1221 | * Note that only system services and CryptKeeper will ever see this return |
| 1222 | * true - no app will ever be launched in this state. |
| 1223 | * Also note that this state will not change without a teardown of the |
| 1224 | * framework, so no service needs to check for changes during their lifespan |
| 1225 | */ |
| 1226 | public static boolean isBlockEncrypting() { |
| 1227 | final String state = SystemProperties.get("vold.encrypt_progress", ""); |
| 1228 | return !"".equalsIgnoreCase(state); |
| 1229 | } |
| 1230 | |
| 1231 | /** {@hide} |
| 1232 | * Is this device non default block encrypted and in the process of |
| 1233 | * prompting for credentials? |
| 1234 | * @return true for prompting for credentials. |
| 1235 | * (Implies isNonDefaultBlockEncrypted() == true) |
| 1236 | * false otherwise |
| 1237 | * Note that only system services and CryptKeeper will ever see this return |
| 1238 | * true - no app will ever be launched in this state. |
| 1239 | * Also note that this state will not change without a teardown of the |
| 1240 | * framework, so no service needs to check for changes during their lifespan |
| 1241 | */ |
| 1242 | public static boolean inCryptKeeperBounce() { |
| 1243 | final String status = SystemProperties.get("vold.decrypt"); |
| 1244 | return "trigger_restart_min_framework".equals(status); |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | /** {@hide} */ |
Paul Lawrence | 20be5d6 | 2016-02-26 13:51:17 -0800 | [diff] [blame] | 1248 | public static boolean isFileEncryptedEmulatedOnly() { |
Jeff Sharkey | ce14cd0 | 2015-12-07 15:35:42 -0700 | [diff] [blame] | 1249 | return SystemProperties.getBoolean(StorageManager.PROP_EMULATE_FBE, false); |
Clara Bayarri | 965da39 | 2015-10-28 17:53:53 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Paul Lawrence | 20be5d6 | 2016-02-26 13:51:17 -0800 | [diff] [blame] | 1252 | /** {@hide} |
| 1253 | * Is this device running in a file encrypted mode, either native or emulated? |
| 1254 | * @return true for file encrypted, false otherwise |
| 1255 | */ |
| 1256 | public static boolean isFileEncryptedNativeOrEmulated() { |
| 1257 | return isFileEncryptedNativeOnly() |
| 1258 | || isFileEncryptedEmulatedOnly(); |
| 1259 | } |
| 1260 | |
Clara Bayarri | 965da39 | 2015-10-28 17:53:53 +0000 | [diff] [blame] | 1261 | /** {@hide} */ |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1262 | public static File maybeTranslateEmulatedPathToInternal(File path) { |
Jeff Sharkey | 8575da1 | 2016-04-07 17:56:06 +0000 | [diff] [blame] | 1263 | final IMountService mountService = IMountService.Stub.asInterface( |
| 1264 | ServiceManager.getService("mount")); |
| 1265 | try { |
| 1266 | final VolumeInfo[] vols = mountService.getVolumes(0); |
| 1267 | for (VolumeInfo vol : vols) { |
| 1268 | if ((vol.getType() == VolumeInfo.TYPE_EMULATED |
| 1269 | || vol.getType() == VolumeInfo.TYPE_PUBLIC) && vol.isMountedReadable()) { |
| 1270 | final File internalPath = FileUtils.rewriteAfterRename(vol.getPath(), |
| 1271 | vol.getInternalPath(), path); |
| 1272 | if (internalPath != null && internalPath.exists()) { |
| 1273 | return internalPath; |
| 1274 | } |
| 1275 | } |
| 1276 | } |
| 1277 | } catch (RemoteException e) { |
| 1278 | throw e.rethrowFromSystemServer(); |
| 1279 | } |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1280 | return path; |
| 1281 | } |
| 1282 | |
Daichi Hirono | 9e8d9e2 | 2015-11-13 14:37:00 +0900 | [diff] [blame] | 1283 | /** {@hide} */ |
| 1284 | public ParcelFileDescriptor mountAppFuse(String name) { |
| 1285 | try { |
| 1286 | return mMountService.mountAppFuse(name); |
| 1287 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1288 | throw e.rethrowFromSystemServer(); |
Daichi Hirono | 9e8d9e2 | 2015-11-13 14:37:00 +0900 | [diff] [blame] | 1289 | } |
| 1290 | } |
| 1291 | |
Paul Lawrence | e8fdc54 | 2014-05-28 07:14:17 -0700 | [diff] [blame] | 1292 | /// Consts to match the password types in cryptfs.h |
| 1293 | /** @hide */ |
| 1294 | public static final int CRYPT_TYPE_PASSWORD = 0; |
| 1295 | /** @hide */ |
| 1296 | public static final int CRYPT_TYPE_DEFAULT = 1; |
| 1297 | /** @hide */ |
| 1298 | public static final int CRYPT_TYPE_PATTERN = 2; |
| 1299 | /** @hide */ |
| 1300 | public static final int CRYPT_TYPE_PIN = 3; |
Elliott Hughes | f839b4f | 2014-09-26 12:30:47 -0700 | [diff] [blame] | 1301 | |
| 1302 | // Constants for the data available via MountService.getField. |
| 1303 | /** @hide */ |
| 1304 | public static final String SYSTEM_LOCALE_KEY = "SystemLocale"; |
| 1305 | /** @hide */ |
| 1306 | public static final String OWNER_INFO_KEY = "OwnerInfo"; |
| 1307 | /** @hide */ |
| 1308 | public static final String PATTERN_VISIBLE_KEY = "PatternVisible"; |
Paul Lawrence | d8fdb33 | 2015-05-18 13:26:11 -0700 | [diff] [blame] | 1309 | /** @hide */ |
| 1310 | public static final String PASSWORD_VISIBLE_KEY = "PasswordVisible"; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1311 | } |