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