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