StorageManager.java revision bff405f91001237ec94ec64f86fa2370c4509559
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 17package android.os.storage; 18 19import android.os.Handler; 20import android.os.Looper; 21import android.os.Message; 22import android.os.RemoteException; 23import android.os.ServiceManager; 24import android.util.Log; 25 26import java.lang.ref.WeakReference; 27import java.util.ArrayList; 28import java.util.Iterator; 29import java.util.LinkedList; 30import java.util.List; 31 32/** 33 * StorageManager is the interface to the systems storage service. The storage 34 * manager handles storage-related items such as Opaque Binary Blobs (OBBs). 35 * <p> 36 * OBBs contain a filesystem that maybe be encrypted on disk and mounted 37 * on-demand from an application. OBBs are a good way of providing large amounts 38 * of binary assets without packaging them into APKs as they may be multiple 39 * gigabytes in size. However, due to their size, they're most likely stored in 40 * a shared storage pool accessible from all programs. The system does not 41 * guarantee the security of the OBB file itself: if any program modifies the 42 * OBB, there is no guarantee that a read from that OBB will produce the 43 * expected output. 44 * <p> 45 * Get an instance of this class by calling 46 * {@link android.content.Context#getSystemService(java.lang.String)} with an 47 * argument of {@link android.content.Context#STORAGE_SERVICE}. 48 */ 49 50public class StorageManager 51{ 52 private static final String TAG = "StorageManager"; 53 54 /* 55 * Our internal MountService binder reference 56 */ 57 private IMountService mMountService; 58 59 /* 60 * The looper target for callbacks 61 */ 62 Looper mTgtLooper; 63 64 /* 65 * Target listener for binder callbacks 66 */ 67 private MountServiceBinderListener mBinderListener; 68 69 /* 70 * List of our listeners 71 */ 72 private ArrayList<ListenerDelegate> mListeners = new ArrayList<ListenerDelegate>(); 73 74 private class MountServiceBinderListener extends IMountServiceListener.Stub { 75 public void onUsbMassStorageConnectionChanged(boolean available) { 76 final int size = mListeners.size(); 77 for (int i = 0; i < size; i++) { 78 mListeners.get(i).sendShareAvailabilityChanged(available); 79 } 80 } 81 82 public void onStorageStateChanged(String path, String oldState, String newState) { 83 final int size = mListeners.size(); 84 for (int i = 0; i < size; i++) { 85 mListeners.get(i).sendStorageStateChanged(path, oldState, newState); 86 } 87 } 88 } 89 90 /** 91 * Binder listener for OBB action results. 92 */ 93 private final ObbActionListener mObbActionListener = new ObbActionListener(); 94 95 private class ObbActionListener extends IObbActionListener.Stub { 96 private List<WeakReference<ObbListenerDelegate>> mListeners = new LinkedList<WeakReference<ObbListenerDelegate>>(); 97 98 @Override 99 public void onObbResult(String filename, String status) throws RemoteException { 100 synchronized (mListeners) { 101 final Iterator<WeakReference<ObbListenerDelegate>> iter = mListeners.iterator(); 102 while (iter.hasNext()) { 103 final WeakReference<ObbListenerDelegate> ref = iter.next(); 104 105 final ObbListenerDelegate delegate = (ref == null) ? null : ref.get(); 106 if (delegate == null) { 107 iter.remove(); 108 continue; 109 } 110 111 delegate.sendObbStateChanged(filename, status); 112 } 113 } 114 } 115 116 public void addListener(OnObbStateChangeListener listener) { 117 if (listener == null) { 118 return; 119 } 120 121 synchronized (mListeners) { 122 final Iterator<WeakReference<ObbListenerDelegate>> iter = mListeners.iterator(); 123 while (iter.hasNext()) { 124 final WeakReference<ObbListenerDelegate> ref = iter.next(); 125 126 final ObbListenerDelegate delegate = (ref == null) ? null : ref.get(); 127 if (delegate == null) { 128 iter.remove(); 129 continue; 130 } 131 132 /* 133 * If we're already in the listeners, we don't need to be in 134 * there again. 135 */ 136 if (listener.equals(delegate.getListener())) { 137 return; 138 } 139 } 140 141 final ObbListenerDelegate delegate = new ObbListenerDelegate(listener); 142 mListeners.add(new WeakReference<ObbListenerDelegate>(delegate)); 143 } 144 } 145 } 146 147 /** 148 * Private class containing sender and receiver code for StorageEvents. 149 */ 150 private class ObbListenerDelegate { 151 private final WeakReference<OnObbStateChangeListener> mObbEventListenerRef; 152 private final Handler mHandler; 153 154 ObbListenerDelegate(OnObbStateChangeListener listener) { 155 mObbEventListenerRef = new WeakReference<OnObbStateChangeListener>(listener); 156 mHandler = new Handler(mTgtLooper) { 157 @Override 158 public void handleMessage(Message msg) { 159 final OnObbStateChangeListener listener = getListener(); 160 if (listener == null) { 161 return; 162 } 163 164 StorageEvent e = (StorageEvent) msg.obj; 165 166 if (msg.what == StorageEvent.EVENT_OBB_STATE_CHANGED) { 167 ObbStateChangedStorageEvent ev = (ObbStateChangedStorageEvent) e; 168 listener.onObbStateChange(ev.path, ev.state); 169 } else { 170 Log.e(TAG, "Unsupported event " + msg.what); 171 } 172 } 173 }; 174 } 175 176 OnObbStateChangeListener getListener() { 177 if (mObbEventListenerRef == null) { 178 return null; 179 } 180 return mObbEventListenerRef.get(); 181 } 182 183 void sendObbStateChanged(String path, String state) { 184 ObbStateChangedStorageEvent e = new ObbStateChangedStorageEvent(path, state); 185 mHandler.sendMessage(e.getMessage()); 186 } 187 } 188 189 /** 190 * Message sent during an OBB status change event. 191 */ 192 private class ObbStateChangedStorageEvent extends StorageEvent { 193 public final String path; 194 public final String state; 195 196 public ObbStateChangedStorageEvent(String path, String state) { 197 super(EVENT_OBB_STATE_CHANGED); 198 this.path = path; 199 this.state = state; 200 } 201 } 202 203 /** 204 * Private base class for messages sent between the callback thread 205 * and the target looper handler. 206 */ 207 private class StorageEvent { 208 static final int EVENT_UMS_CONNECTION_CHANGED = 1; 209 static final int EVENT_STORAGE_STATE_CHANGED = 2; 210 static final int EVENT_OBB_STATE_CHANGED = 3; 211 212 private Message mMessage; 213 214 public StorageEvent(int what) { 215 mMessage = Message.obtain(); 216 mMessage.what = what; 217 mMessage.obj = this; 218 } 219 220 public Message getMessage() { 221 return mMessage; 222 } 223 } 224 225 /** 226 * Message sent on a USB mass storage connection change. 227 */ 228 private class UmsConnectionChangedStorageEvent extends StorageEvent { 229 public boolean available; 230 231 public UmsConnectionChangedStorageEvent(boolean a) { 232 super(EVENT_UMS_CONNECTION_CHANGED); 233 available = a; 234 } 235 } 236 237 /** 238 * Message sent on volume state change. 239 */ 240 private class StorageStateChangedStorageEvent extends StorageEvent { 241 public String path; 242 public String oldState; 243 public String newState; 244 245 public StorageStateChangedStorageEvent(String p, String oldS, String newS) { 246 super(EVENT_STORAGE_STATE_CHANGED); 247 path = p; 248 oldState = oldS; 249 newState = newS; 250 } 251 } 252 253 /** 254 * Private class containing sender and receiver code for StorageEvents. 255 */ 256 private class ListenerDelegate { 257 final StorageEventListener mStorageEventListener; 258 private final Handler mHandler; 259 260 ListenerDelegate(StorageEventListener listener) { 261 mStorageEventListener = listener; 262 mHandler = new Handler(mTgtLooper) { 263 @Override 264 public void handleMessage(Message msg) { 265 StorageEvent e = (StorageEvent) msg.obj; 266 267 if (msg.what == StorageEvent.EVENT_UMS_CONNECTION_CHANGED) { 268 UmsConnectionChangedStorageEvent ev = (UmsConnectionChangedStorageEvent) e; 269 mStorageEventListener.onUsbMassStorageConnectionChanged(ev.available); 270 } else if (msg.what == StorageEvent.EVENT_STORAGE_STATE_CHANGED) { 271 StorageStateChangedStorageEvent ev = (StorageStateChangedStorageEvent) e; 272 mStorageEventListener.onStorageStateChanged(ev.path, ev.oldState, ev.newState); 273 } else { 274 Log.e(TAG, "Unsupported event " + msg.what); 275 } 276 } 277 }; 278 } 279 280 StorageEventListener getListener() { 281 return mStorageEventListener; 282 } 283 284 void sendShareAvailabilityChanged(boolean available) { 285 UmsConnectionChangedStorageEvent e = new UmsConnectionChangedStorageEvent(available); 286 mHandler.sendMessage(e.getMessage()); 287 } 288 289 void sendStorageStateChanged(String path, String oldState, String newState) { 290 StorageStateChangedStorageEvent e = new StorageStateChangedStorageEvent(path, oldState, newState); 291 mHandler.sendMessage(e.getMessage()); 292 } 293 } 294 295 /** 296 * Constructs a StorageManager object through which an application can 297 * can communicate with the systems mount service. 298 * 299 * @param tgtLooper The {@android.os.Looper} which events will be received on. 300 * 301 * <p>Applications can get instance of this class by calling 302 * {@link android.content.Context#getSystemService(java.lang.String)} with an argument 303 * of {@link android.content.Context#STORAGE_SERVICE}. 304 * 305 * @hide 306 */ 307 public StorageManager(Looper tgtLooper) throws RemoteException { 308 mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount")); 309 if (mMountService == null) { 310 Log.e(TAG, "Unable to connect to mount service! - is it running yet?"); 311 return; 312 } 313 mTgtLooper = tgtLooper; 314 mBinderListener = new MountServiceBinderListener(); 315 mMountService.registerListener(mBinderListener); 316 } 317 318 319 /** 320 * Registers a {@link android.os.storage.StorageEventListener StorageEventListener}. 321 * 322 * @param listener A {@link android.os.storage.StorageEventListener StorageEventListener} object. 323 * 324 */ 325 public void registerListener(StorageEventListener listener) { 326 if (listener == null) { 327 return; 328 } 329 330 synchronized (mListeners) { 331 mListeners.add(new ListenerDelegate(listener)); 332 } 333 } 334 335 /** 336 * Unregisters a {@link android.os.storage.StorageEventListener StorageEventListener}. 337 * 338 * @param listener A {@link android.os.storage.StorageEventListener StorageEventListener} object. 339 * 340 */ 341 public void unregisterListener(StorageEventListener listener) { 342 if (listener == null) { 343 return; 344 } 345 346 synchronized (mListeners) { 347 final int size = mListeners.size(); 348 for (int i=0 ; i<size ; i++) { 349 ListenerDelegate l = mListeners.get(i); 350 if (l.getListener() == listener) { 351 mListeners.remove(i); 352 break; 353 } 354 } 355 } 356 } 357 358 /** 359 * Enables USB Mass Storage (UMS) on the device. 360 */ 361 public void enableUsbMassStorage() { 362 try { 363 mMountService.setUsbMassStorageEnabled(true); 364 } catch (Exception ex) { 365 Log.e(TAG, "Failed to enable UMS", ex); 366 } 367 } 368 369 /** 370 * Disables USB Mass Storage (UMS) on the device. 371 */ 372 public void disableUsbMassStorage() { 373 try { 374 mMountService.setUsbMassStorageEnabled(false); 375 } catch (Exception ex) { 376 Log.e(TAG, "Failed to disable UMS", ex); 377 } 378 } 379 380 /** 381 * Query if a USB Mass Storage (UMS) host is connected. 382 * @return true if UMS host is connected. 383 */ 384 public boolean isUsbMassStorageConnected() { 385 try { 386 return mMountService.isUsbMassStorageConnected(); 387 } catch (Exception ex) { 388 Log.e(TAG, "Failed to get UMS connection state", ex); 389 } 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. 396 */ 397 public boolean isUsbMassStorageEnabled() { 398 try { 399 return mMountService.isUsbMassStorageEnabled(); 400 } catch (RemoteException rex) { 401 Log.e(TAG, "Failed to get UMS enable state", rex); 402 } 403 return false; 404 } 405 406 /** 407 * Mount an Opaque Binary Blob (OBB) file. If a <code>key</code> is 408 * specified, it is supplied to the mounting process to be used in any 409 * encryption used in the OBB. 410 * <p> 411 * The OBB will remain mounted for as long as the StorageManager reference 412 * is held by the application. As soon as this reference is lost, the OBBs 413 * in use will be unmounted. The {@link OnObbStateChangeListener} registered with 414 * this call will receive all further OBB-related events until it goes out 415 * of scope. If the caller is not interested in whether the call succeeds, 416 * the <code>listener</code> may be specified as <code>null</code>. 417 * <p> 418 * <em>Note:</em> you can only mount OBB files for which the OBB tag on the 419 * file matches a package ID that is owned by the calling program's UID. 420 * That is, shared UID applications can attempt to mount any other 421 * application's OBB that shares its UID. 422 * 423 * @param filename the path to the OBB file 424 * @param key secret used to encrypt the OBB; may be <code>null</code> if no 425 * encryption was used on the OBB. 426 * @return whether the mount call was successfully queued or not 427 * @throws IllegalArgumentException when the OBB is already mounted 428 */ 429 public boolean mountObb(String filename, String key, OnObbStateChangeListener listener) { 430 try { 431 mObbActionListener.addListener(listener); 432 mMountService.mountObb(filename, key, mObbActionListener); 433 return true; 434 } catch (RemoteException e) { 435 Log.e(TAG, "Failed to mount OBB", e); 436 } 437 438 return false; 439 } 440 441 /** 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> 446 * The {@link OnObbStateChangeListener} registered with this call will receive all 447 * further OBB-related events until it goes out of scope. If the caller is 448 * not interested in whether the call succeeded, the listener may be 449 * specified as <code>null</code>. 450 * <p> 451 * <em>Note:</em> you can only mount OBB files for which the OBB tag on the 452 * file matches a package ID that is owned by the calling program's UID. 453 * That is, shared UID applications can obtain access to any other 454 * application's OBB that shares its UID. 455 * <p> 456 * 457 * @param filename path to the OBB file 458 * @param force whether to kill any programs using this in order to unmount 459 * it 460 * @return whether the unmount call was successfully queued or not 461 * @throws IllegalArgumentException when OBB is not already mounted 462 */ 463 public boolean unmountObb(String filename, boolean force, OnObbStateChangeListener listener) 464 throws IllegalArgumentException { 465 try { 466 mObbActionListener.addListener(listener); 467 mMountService.unmountObb(filename, force, mObbActionListener); 468 return true; 469 } catch (RemoteException e) { 470 Log.e(TAG, "Failed to mount OBB", e); 471 } 472 473 return false; 474 } 475 476 /** 477 * Check whether an Opaque Binary Blob (OBB) is mounted or not. 478 * 479 * @param filename path to OBB image 480 * @return true if OBB is mounted; false if not mounted or on error 481 */ 482 public boolean isObbMounted(String filename) throws IllegalArgumentException { 483 try { 484 return mMountService.isObbMounted(filename); 485 } catch (RemoteException e) { 486 Log.e(TAG, "Failed to check if OBB is mounted", e); 487 } 488 489 return false; 490 } 491 492 /** 493 * Check the mounted path of an Opaque Binary Blob (OBB) file. This will 494 * give you the path to where you can obtain access to the internals of the 495 * OBB. 496 * 497 * @param filename path to OBB image 498 * @return absolute path to mounted OBB image data or <code>null</code> if 499 * not mounted or exception encountered trying to read status 500 */ 501 public String getMountedObbPath(String filename) { 502 try { 503 return mMountService.getMountedObbPath(filename); 504 } catch (RemoteException e) { 505 Log.e(TAG, "Failed to find mounted path for OBB", e); 506 } catch (IllegalArgumentException e) { 507 Log.d(TAG, "Couldn't read OBB file", e); 508 } 509 510 return null; 511 } 512} 513