blob: d6804f9ad5651d0ca9d1e0756f69b80647bccab7 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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 com.android.server;
18
Kenny Roota02b8b02010-08-05 16:14:17 -070019import com.android.internal.app.IMediaContainerService;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080020import com.android.server.am.ActivityManagerService;
21
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.BroadcastReceiver;
Kenny Roota02b8b02010-08-05 16:14:17 -070023import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
Kenny Roota02b8b02010-08-05 16:14:17 -070027import android.content.ServiceConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.pm.PackageManager;
Kenny Root02c87302010-07-01 08:10:18 -070029import android.content.res.ObbInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.net.Uri;
Kenny Root02c87302010-07-01 08:10:18 -070031import android.os.Binder;
Kenny Roota02b8b02010-08-05 16:14:17 -070032import android.os.Environment;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080033import android.os.Handler;
Daniel Sandler5f27ef42010-03-16 15:42:02 -040034import android.os.HandlerThread;
Kenny Roota02b8b02010-08-05 16:14:17 -070035import android.os.IBinder;
Daniel Sandler5f27ef42010-03-16 15:42:02 -040036import android.os.Looper;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080037import android.os.Message;
San Mehat4270e1e2010-01-29 05:32:19 -080038import android.os.RemoteException;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -080039import android.os.ServiceManager;
San Mehat207e5382010-02-04 20:46:54 -080040import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.SystemProperties;
Kenny Roota02b8b02010-08-05 16:14:17 -070042import android.os.storage.IMountService;
43import android.os.storage.IMountServiceListener;
44import android.os.storage.IMountShutdownObserver;
45import android.os.storage.IObbActionListener;
Kenny Rootaf9d6672010-10-08 09:21:39 -070046import android.os.storage.OnObbStateChangeListener;
Kenny Roota02b8b02010-08-05 16:14:17 -070047import android.os.storage.StorageResultCode;
San Mehata5078592010-03-25 09:36:54 -070048import android.util.Slog;
Kenny Roota02b8b02010-08-05 16:14:17 -070049
Kenny Root38cf8862010-09-26 14:18:51 -070050import java.io.FileDescriptor;
Kenny Root05105f72010-09-22 17:29:43 -070051import java.io.IOException;
Kenny Root38cf8862010-09-26 14:18:51 -070052import java.io.PrintWriter;
Kenny Root3b1abba2010-10-13 15:00:07 -070053import java.math.BigInteger;
Kenny Root735de3b2010-09-30 14:11:39 -070054import java.security.NoSuchAlgorithmException;
Kenny Root3b1abba2010-10-13 15:00:07 -070055import java.security.spec.InvalidKeySpecException;
56import java.security.spec.KeySpec;
San Mehat22dd86e2010-01-12 12:21:18 -080057import java.util.ArrayList;
Kenny Roota02b8b02010-08-05 16:14:17 -070058import java.util.HashMap;
San Mehat6cdd9c02010-02-09 14:45:20 -080059import java.util.HashSet;
Kenny Root38cf8862010-09-26 14:18:51 -070060import java.util.Iterator;
Kenny Roota02b8b02010-08-05 16:14:17 -070061import java.util.LinkedList;
62import java.util.List;
63import java.util.Map;
Kenny Root38cf8862010-09-26 14:18:51 -070064import java.util.Map.Entry;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
Kenny Root3b1abba2010-10-13 15:00:07 -070066import javax.crypto.SecretKey;
67import javax.crypto.SecretKeyFactory;
68import javax.crypto.spec.PBEKeySpec;
69
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070/**
San Mehatb1043402010-02-05 08:26:50 -080071 * MountService implements back-end services for platform storage
72 * management.
73 * @hide - Applications should use android.os.storage.StorageManager
74 * to access the MountService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 */
Jason parks5af0b912010-11-29 09:05:25 -060076class MountService extends IMountService.Stub implements INativeDaemonConnectorCallbacks {
77
San Mehatb1043402010-02-05 08:26:50 -080078 private static final boolean LOCAL_LOGD = false;
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -080079 private static final boolean DEBUG_UNMOUNT = false;
80 private static final boolean DEBUG_EVENTS = false;
Kenny Root02c87302010-07-01 08:10:18 -070081 private static final boolean DEBUG_OBB = true;
82
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 private static final String TAG = "MountService";
84
Kenny Root305bcbf2010-09-03 07:56:38 -070085 private static final String VOLD_TAG = "VoldConnector";
86
San Mehat4270e1e2010-01-29 05:32:19 -080087 /*
88 * Internal vold volume state constants
89 */
San Mehat7fd0fee2009-12-17 07:12:23 -080090 class VolumeState {
91 public static final int Init = -1;
92 public static final int NoMedia = 0;
93 public static final int Idle = 1;
94 public static final int Pending = 2;
95 public static final int Checking = 3;
96 public static final int Mounted = 4;
97 public static final int Unmounting = 5;
98 public static final int Formatting = 6;
99 public static final int Shared = 7;
100 public static final int SharedMnt = 8;
101 }
102
San Mehat4270e1e2010-01-29 05:32:19 -0800103 /*
104 * Internal vold response code constants
105 */
San Mehat22dd86e2010-01-12 12:21:18 -0800106 class VoldResponseCode {
San Mehat4270e1e2010-01-29 05:32:19 -0800107 /*
108 * 100 series - Requestion action was initiated; expect another reply
109 * before proceeding with a new command.
110 */
San Mehat22dd86e2010-01-12 12:21:18 -0800111 public static final int VolumeListResult = 110;
112 public static final int AsecListResult = 111;
San Mehatc1b4ce92010-02-16 17:13:03 -0800113 public static final int StorageUsersListResult = 112;
San Mehat22dd86e2010-01-12 12:21:18 -0800114
San Mehat4270e1e2010-01-29 05:32:19 -0800115 /*
116 * 200 series - Requestion action has been successfully completed.
117 */
118 public static final int ShareStatusResult = 210;
San Mehat22dd86e2010-01-12 12:21:18 -0800119 public static final int AsecPathResult = 211;
San Mehat4270e1e2010-01-29 05:32:19 -0800120 public static final int ShareEnabledResult = 212;
San Mehat22dd86e2010-01-12 12:21:18 -0800121
San Mehat4270e1e2010-01-29 05:32:19 -0800122 /*
123 * 400 series - Command was accepted, but the requested action
124 * did not take place.
125 */
126 public static final int OpFailedNoMedia = 401;
127 public static final int OpFailedMediaBlank = 402;
128 public static final int OpFailedMediaCorrupt = 403;
129 public static final int OpFailedVolNotMounted = 404;
San Mehatd9709982010-02-18 11:43:03 -0800130 public static final int OpFailedStorageBusy = 405;
San Mehat2d66cef2010-03-23 11:12:52 -0700131 public static final int OpFailedStorageNotFound = 406;
San Mehat4270e1e2010-01-29 05:32:19 -0800132
133 /*
134 * 600 series - Unsolicited broadcasts.
135 */
San Mehat22dd86e2010-01-12 12:21:18 -0800136 public static final int VolumeStateChange = 605;
San Mehat22dd86e2010-01-12 12:21:18 -0800137 public static final int ShareAvailabilityChange = 620;
138 public static final int VolumeDiskInserted = 630;
139 public static final int VolumeDiskRemoved = 631;
140 public static final int VolumeBadRemoval = 632;
141 }
142
San Mehat4270e1e2010-01-29 05:32:19 -0800143 private Context mContext;
144 private NativeDaemonConnector mConnector;
145 private String mLegacyState = Environment.MEDIA_REMOVED;
146 private PackageManagerService mPms;
147 private boolean mUmsEnabling;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800148 // Used as a lock for methods that register/unregister listeners.
149 final private ArrayList<MountServiceBinderListener> mListeners =
150 new ArrayList<MountServiceBinderListener>();
San Mehat6a965af22010-02-24 17:47:30 -0800151 private boolean mBooted = false;
152 private boolean mReady = false;
153 private boolean mSendUmsConnectedOnBoot = false;
Mike Lockwood03559752010-07-19 18:25:03 -0400154 // true if we should fake MEDIA_MOUNTED state for external storage
155 private boolean mEmulateExternalStorage = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800156
San Mehat6cdd9c02010-02-09 14:45:20 -0800157 /**
158 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800159 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800160 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800161 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800162
Kenny Root02c87302010-07-01 08:10:18 -0700163 /**
Kenny Root3b1abba2010-10-13 15:00:07 -0700164 * The size of the crypto algorithm key in bits for OBB files. Currently
165 * Twofish is used which takes 128-bit keys.
166 */
167 private static final int CRYPTO_ALGORITHM_KEY_SIZE = 128;
168
169 /**
170 * The number of times to run SHA1 in the PBKDF2 function for OBB files.
171 * 1024 is reasonably secure and not too slow.
172 */
173 private static final int PBKDF2_HASH_ROUNDS = 1024;
174
175 /**
Kenny Roota02b8b02010-08-05 16:14:17 -0700176 * Mounted OBB tracking information. Used to track the current state of all
177 * OBBs.
Kenny Root02c87302010-07-01 08:10:18 -0700178 */
Kenny Root735de3b2010-09-30 14:11:39 -0700179 final private Map<IBinder, List<ObbState>> mObbMounts = new HashMap<IBinder, List<ObbState>>();
Kenny Roota02b8b02010-08-05 16:14:17 -0700180 final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>();
181
182 class ObbState implements IBinder.DeathRecipient {
Kenny Rootaf9d6672010-10-08 09:21:39 -0700183 public ObbState(String filename, int callerUid, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -0700184 throws RemoteException {
Kenny Roota02b8b02010-08-05 16:14:17 -0700185 this.filename = filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700186 this.callerUid = callerUid;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700187 this.token = token;
188 this.nonce = nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700189 }
190
191 // OBB source filename
Kenny Rootaf9d6672010-10-08 09:21:39 -0700192 String filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700193
194 // Binder.callingUid()
Kenny Root05105f72010-09-22 17:29:43 -0700195 final public int callerUid;
Kenny Roota02b8b02010-08-05 16:14:17 -0700196
Kenny Rootaf9d6672010-10-08 09:21:39 -0700197 // Token of remote Binder caller
198 final IObbActionListener token;
199
200 // Identifier to pass back to the token
201 final int nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700202
Kenny Root735de3b2010-09-30 14:11:39 -0700203 public IBinder getBinder() {
204 return token.asBinder();
205 }
206
Kenny Roota02b8b02010-08-05 16:14:17 -0700207 @Override
208 public void binderDied() {
209 ObbAction action = new UnmountObbAction(this, true);
210 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root735de3b2010-09-30 14:11:39 -0700211 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700212
Kenny Root5919ac62010-10-05 09:49:40 -0700213 public void link() throws RemoteException {
214 getBinder().linkToDeath(this, 0);
215 }
216
217 public void unlink() {
Kenny Root735de3b2010-09-30 14:11:39 -0700218 getBinder().unlinkToDeath(this, 0);
Kenny Roota02b8b02010-08-05 16:14:17 -0700219 }
Kenny Root38cf8862010-09-26 14:18:51 -0700220
221 @Override
222 public String toString() {
223 StringBuilder sb = new StringBuilder("ObbState{");
224 sb.append("filename=");
225 sb.append(filename);
226 sb.append(",token=");
227 sb.append(token.toString());
228 sb.append(",callerUid=");
229 sb.append(callerUid);
Kenny Root38cf8862010-09-26 14:18:51 -0700230 sb.append('}');
231 return sb.toString();
232 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700233 }
234
235 // OBB Action Handler
236 final private ObbActionHandler mObbActionHandler;
237
238 // OBB action handler messages
239 private static final int OBB_RUN_ACTION = 1;
240 private static final int OBB_MCS_BOUND = 2;
241 private static final int OBB_MCS_UNBIND = 3;
242 private static final int OBB_MCS_RECONNECT = 4;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700243 private static final int OBB_FLUSH_MOUNT_STATE = 5;
Kenny Roota02b8b02010-08-05 16:14:17 -0700244
245 /*
246 * Default Container Service information
247 */
248 static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
249 "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService");
250
251 final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection();
252
253 class DefaultContainerConnection implements ServiceConnection {
254 public void onServiceConnected(ComponentName name, IBinder service) {
255 if (DEBUG_OBB)
256 Slog.i(TAG, "onServiceConnected");
257 IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service);
258 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs));
259 }
260
261 public void onServiceDisconnected(ComponentName name) {
262 if (DEBUG_OBB)
263 Slog.i(TAG, "onServiceDisconnected");
264 }
265 };
266
267 // Used in the ObbActionHandler
268 private IMediaContainerService mContainerService = null;
Kenny Root02c87302010-07-01 08:10:18 -0700269
270 // Handler messages
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800271 private static final int H_UNMOUNT_PM_UPDATE = 1;
272 private static final int H_UNMOUNT_PM_DONE = 2;
273 private static final int H_UNMOUNT_MS = 3;
274 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
275 private static final int MAX_UNMOUNT_RETRIES = 4;
276
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800277 class UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700278 final String path;
279 final boolean force;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800280 int retries;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800281
282 UnmountCallBack(String path, boolean force) {
283 retries = 0;
284 this.path = path;
285 this.force = force;
286 }
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800287
288 void handleFinished() {
San Mehata5078592010-03-25 09:36:54 -0700289 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path);
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800290 doUnmountVolume(path, true);
291 }
292 }
293
294 class UmsEnableCallBack extends UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700295 final String method;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800296
297 UmsEnableCallBack(String path, String method, boolean force) {
298 super(path, force);
299 this.method = method;
300 }
301
302 @Override
303 void handleFinished() {
304 super.handleFinished();
305 doShareUnshareVolume(path, method, true);
306 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800307 }
308
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800309 class ShutdownCallBack extends UnmountCallBack {
310 IMountShutdownObserver observer;
311 ShutdownCallBack(String path, IMountShutdownObserver observer) {
312 super(path, true);
313 this.observer = observer;
314 }
315
316 @Override
317 void handleFinished() {
318 int ret = doUnmountVolume(path, true);
319 if (observer != null) {
320 try {
321 observer.onShutDownComplete(ret);
322 } catch (RemoteException e) {
San Mehata5078592010-03-25 09:36:54 -0700323 Slog.w(TAG, "RemoteException when shutting down");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800324 }
325 }
326 }
327 }
328
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400329 class MountServiceHandler extends Handler {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800330 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700331 boolean mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800332
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400333 MountServiceHandler(Looper l) {
334 super(l);
335 }
336
Jason parks5af0b912010-11-29 09:05:25 -0600337 @Override
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800338 public void handleMessage(Message msg) {
339 switch (msg.what) {
340 case H_UNMOUNT_PM_UPDATE: {
San Mehata5078592010-03-25 09:36:54 -0700341 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800342 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
343 mForceUnmounts.add(ucb);
San Mehata5078592010-03-25 09:36:54 -0700344 if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800345 // Register only if needed.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700346 if (!mUpdatingStatus) {
San Mehata5078592010-03-25 09:36:54 -0700347 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700348 mUpdatingStatus = true;
349 mPms.updateExternalMediaStatus(false, true);
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800350 }
351 break;
352 }
353 case H_UNMOUNT_PM_DONE: {
San Mehata5078592010-03-25 09:36:54 -0700354 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE");
San Mehata5078592010-03-25 09:36:54 -0700355 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700356 mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800357 int size = mForceUnmounts.size();
358 int sizeArr[] = new int[size];
359 int sizeArrN = 0;
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700360 // Kill processes holding references first
361 ActivityManagerService ams = (ActivityManagerService)
362 ServiceManager.getService("activity");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800363 for (int i = 0; i < size; i++) {
364 UnmountCallBack ucb = mForceUnmounts.get(i);
365 String path = ucb.path;
366 boolean done = false;
367 if (!ucb.force) {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800368 done = true;
369 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800370 int pids[] = getStorageUsers(path);
371 if (pids == null || pids.length == 0) {
372 done = true;
373 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800374 // Eliminate system process here?
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700375 ams.killPids(pids, "unmount media");
376 // Confirm if file references have been freed.
377 pids = getStorageUsers(path);
378 if (pids == null || pids.length == 0) {
379 done = true;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800380 }
381 }
382 }
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700383 if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) {
384 // Retry again
385 Slog.i(TAG, "Retrying to kill storage users again");
386 mHandler.sendMessageDelayed(
387 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
388 ucb.retries++),
389 RETRY_UNMOUNT_DELAY);
390 } else {
391 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
392 Slog.i(TAG, "Failed to unmount media inspite of " +
393 MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now");
394 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800395 sizeArr[sizeArrN++] = i;
396 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
397 ucb));
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800398 }
399 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800400 // Remove already processed elements from list.
401 for (int i = (sizeArrN-1); i >= 0; i--) {
402 mForceUnmounts.remove(sizeArr[i]);
403 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800404 break;
405 }
406 case H_UNMOUNT_MS : {
San Mehata5078592010-03-25 09:36:54 -0700407 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800408 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800409 ucb.handleFinished();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800410 break;
411 }
412 }
413 }
414 };
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400415 final private HandlerThread mHandlerThread;
416 final private Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800417
San Mehat207e5382010-02-04 20:46:54 -0800418 private void waitForReady() {
419 while (mReady == false) {
420 for (int retries = 5; retries > 0; retries--) {
421 if (mReady) {
422 return;
423 }
424 SystemClock.sleep(1000);
425 }
San Mehata5078592010-03-25 09:36:54 -0700426 Slog.w(TAG, "Waiting too long for mReady!");
San Mehat207e5382010-02-04 20:46:54 -0800427 }
San Mehat1f6301e2010-01-07 22:40:27 -0800428 }
Kenny Root02c87302010-07-01 08:10:18 -0700429
San Mehat207e5382010-02-04 20:46:54 -0800430 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Jason parks5af0b912010-11-29 09:05:25 -0600431 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 public void onReceive(Context context, Intent intent) {
San Mehat91c77612010-01-07 10:39:41 -0800433 String action = intent.getAction();
434
435 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
San Mehat207e5382010-02-04 20:46:54 -0800436 mBooted = true;
San Mehat22dd86e2010-01-12 12:21:18 -0800437
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800438 /*
439 * In the simulator, we need to broadcast a volume mounted event
440 * to make the media scanner run.
441 */
442 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
443 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted);
444 return;
445 }
San Mehatfafb0412010-02-18 19:40:04 -0800446 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600447 @Override
San Mehatfafb0412010-02-18 19:40:04 -0800448 public void run() {
449 try {
450 String path = Environment.getExternalStorageDirectory().getPath();
San Mehat6a254402010-03-22 10:21:00 -0700451 String state = getVolumeState(path);
452
Mike Lockwood03559752010-07-19 18:25:03 -0400453 if (mEmulateExternalStorage) {
454 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Mounted);
455 } else if (state.equals(Environment.MEDIA_UNMOUNTED)) {
San Mehatfafb0412010-02-18 19:40:04 -0800456 int rc = doMountVolume(path);
457 if (rc != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700458 Slog.e(TAG, String.format("Boot-time mount failed (%d)", rc));
San Mehatfafb0412010-02-18 19:40:04 -0800459 }
San Mehat6a254402010-03-22 10:21:00 -0700460 } else if (state.equals(Environment.MEDIA_SHARED)) {
461 /*
462 * Bootstrap UMS enabled state since vold indicates
463 * the volume is shared (runtime restart while ums enabled)
464 */
465 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Shared);
San Mehatfafb0412010-02-18 19:40:04 -0800466 }
San Mehat6a254402010-03-22 10:21:00 -0700467
San Mehat6a965af22010-02-24 17:47:30 -0800468 /*
San Mehat6a254402010-03-22 10:21:00 -0700469 * If UMS was connected on boot, send the connected event
San Mehat6a965af22010-02-24 17:47:30 -0800470 * now that we're up.
471 */
472 if (mSendUmsConnectedOnBoot) {
473 sendUmsIntent(true);
474 mSendUmsConnectedOnBoot = false;
475 }
San Mehatfafb0412010-02-18 19:40:04 -0800476 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700477 Slog.e(TAG, "Boot-time mount exception", ex);
San Mehatfafb0412010-02-18 19:40:04 -0800478 }
San Mehat207e5382010-02-04 20:46:54 -0800479 }
San Mehatfafb0412010-02-18 19:40:04 -0800480 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 }
482 }
483 };
484
San Mehat4270e1e2010-01-29 05:32:19 -0800485 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
486 final IMountServiceListener mListener;
487
488 MountServiceBinderListener(IMountServiceListener listener) {
489 mListener = listener;
Kenny Root02c87302010-07-01 08:10:18 -0700490
San Mehat91c77612010-01-07 10:39:41 -0800491 }
492
San Mehat4270e1e2010-01-29 05:32:19 -0800493 public void binderDied() {
San Mehata5078592010-03-25 09:36:54 -0700494 if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!");
Kenny Roota02b8b02010-08-05 16:14:17 -0700495 synchronized (mListeners) {
San Mehat4270e1e2010-01-29 05:32:19 -0800496 mListeners.remove(this);
497 mListener.asBinder().unlinkToDeath(this, 0);
498 }
499 }
500 }
501
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800502 private void doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800503 // TODO: Add support for multiple share methods
504 if (!method.equals("ums")) {
505 throw new IllegalArgumentException(String.format("Method %s not supported", method));
506 }
507
San Mehat4270e1e2010-01-29 05:32:19 -0800508 try {
509 mConnector.doCommand(String.format(
510 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
511 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -0700512 Slog.e(TAG, "Failed to share/unshare", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800513 }
San Mehat4270e1e2010-01-29 05:32:19 -0800514 }
515
San Mehat207e5382010-02-04 20:46:54 -0800516 private void updatePublicVolumeState(String path, String state) {
San Mehat4270e1e2010-01-29 05:32:19 -0800517 if (!path.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -0700518 Slog.w(TAG, "Multiple volumes not currently supported");
San Mehat4270e1e2010-01-29 05:32:19 -0800519 return;
520 }
San Mehatb1043402010-02-05 08:26:50 -0800521
522 if (mLegacyState.equals(state)) {
San Mehata5078592010-03-25 09:36:54 -0700523 Slog.w(TAG, String.format("Duplicate state transition (%s -> %s)", mLegacyState, state));
San Mehatb1043402010-02-05 08:26:50 -0800524 return;
525 }
Mike Lockwood03559752010-07-19 18:25:03 -0400526 // Update state on PackageManager, but only of real events
527 if (!mEmulateExternalStorage) {
528 if (Environment.MEDIA_UNMOUNTED.equals(state)) {
529 mPms.updateExternalMediaStatus(false, false);
Kenny Rootaf9d6672010-10-08 09:21:39 -0700530
Kenny Root0a9b54e2010-10-13 15:43:35 -0700531 /*
532 * Some OBBs might have been unmounted when this volume was
533 * unmounted, so send a message to the handler to let it know to
534 * remove those from the list of mounted OBBS.
535 */
536 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_FLUSH_MOUNT_STATE,
537 path));
Mike Lockwood03559752010-07-19 18:25:03 -0400538 } else if (Environment.MEDIA_MOUNTED.equals(state)) {
539 mPms.updateExternalMediaStatus(true, false);
540 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800541 }
Kenny Root38cf8862010-09-26 14:18:51 -0700542
San Mehat4270e1e2010-01-29 05:32:19 -0800543 String oldState = mLegacyState;
544 mLegacyState = state;
545
546 synchronized (mListeners) {
547 for (int i = mListeners.size() -1; i >= 0; i--) {
548 MountServiceBinderListener bl = mListeners.get(i);
549 try {
San Mehatb1043402010-02-05 08:26:50 -0800550 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800551 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700552 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800553 mListeners.remove(i);
554 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700555 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800556 }
557 }
558 }
559 }
560
561 /**
562 *
563 * Callback from NativeDaemonConnector
564 */
565 public void onDaemonConnected() {
566 /*
567 * Since we'll be calling back into the NativeDaemonConnector,
568 * we need to do our work in a new thread.
569 */
570 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600571 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800572 public void run() {
573 /**
574 * Determine media state and UMS detection status
575 */
576 String path = Environment.getExternalStorageDirectory().getPath();
577 String state = Environment.MEDIA_REMOVED;
578
579 try {
580 String[] vols = mConnector.doListCommand(
581 "volume list", VoldResponseCode.VolumeListResult);
582 for (String volstr : vols) {
583 String[] tok = volstr.split(" ");
584 // FMT: <label> <mountpoint> <state>
585 if (!tok[1].equals(path)) {
San Mehata5078592010-03-25 09:36:54 -0700586 Slog.w(TAG, String.format(
San Mehat4270e1e2010-01-29 05:32:19 -0800587 "Skipping unknown volume '%s'",tok[1]));
588 continue;
589 }
590 int st = Integer.parseInt(tok[2]);
591 if (st == VolumeState.NoMedia) {
592 state = Environment.MEDIA_REMOVED;
593 } else if (st == VolumeState.Idle) {
San Mehat207e5382010-02-04 20:46:54 -0800594 state = Environment.MEDIA_UNMOUNTED;
San Mehat4270e1e2010-01-29 05:32:19 -0800595 } else if (st == VolumeState.Mounted) {
596 state = Environment.MEDIA_MOUNTED;
San Mehata5078592010-03-25 09:36:54 -0700597 Slog.i(TAG, "Media already mounted on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800598 } else if (st == VolumeState.Shared) {
599 state = Environment.MEDIA_SHARED;
San Mehata5078592010-03-25 09:36:54 -0700600 Slog.i(TAG, "Media shared on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800601 } else {
602 throw new Exception(String.format("Unexpected state %d", st));
603 }
604 }
605 if (state != null) {
San Mehata5078592010-03-25 09:36:54 -0700606 if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
San Mehat4270e1e2010-01-29 05:32:19 -0800607 updatePublicVolumeState(path, state);
608 }
609 } catch (Exception e) {
San Mehata5078592010-03-25 09:36:54 -0700610 Slog.e(TAG, "Error processing initial volume state", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800611 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
612 }
613
614 try {
San Mehat207e5382010-02-04 20:46:54 -0800615 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800616 notifyShareAvailabilityChange("ums", avail);
617 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700618 Slog.w(TAG, "Failed to get share availability");
San Mehat4270e1e2010-01-29 05:32:19 -0800619 }
San Mehat207e5382010-02-04 20:46:54 -0800620 /*
621 * Now that we've done our initialization, release
622 * the hounds!
623 */
624 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800625 }
626 }.start();
627 }
628
629 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800630 * Callback from NativeDaemonConnector
631 */
632 public boolean onEvent(int code, String raw, String[] cooked) {
633 Intent in = null;
634
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800635 if (DEBUG_EVENTS) {
636 StringBuilder builder = new StringBuilder();
637 builder.append("onEvent::");
638 builder.append(" raw= " + raw);
639 if (cooked != null) {
640 builder.append(" cooked = " );
641 for (String str : cooked) {
642 builder.append(" " + str);
643 }
644 }
San Mehata5078592010-03-25 09:36:54 -0700645 Slog.i(TAG, builder.toString());
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800646 }
San Mehat4270e1e2010-01-29 05:32:19 -0800647 if (code == VoldResponseCode.VolumeStateChange) {
648 /*
649 * One of the volumes we're managing has changed state.
650 * Format: "NNN Volume <label> <path> state changed
651 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
652 */
653 notifyVolumeStateChange(
654 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
655 Integer.parseInt(cooked[10]));
656 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
657 // FMT: NNN Share method <method> now <available|unavailable>
658 boolean avail = false;
659 if (cooked[5].equals("available")) {
660 avail = true;
661 }
662 notifyShareAvailabilityChange(cooked[3], avail);
663 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
664 (code == VoldResponseCode.VolumeDiskRemoved) ||
665 (code == VoldResponseCode.VolumeBadRemoval)) {
666 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
667 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
668 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
669 final String label = cooked[2];
670 final String path = cooked[3];
671 int major = -1;
672 int minor = -1;
673
674 try {
675 String devComp = cooked[6].substring(1, cooked[6].length() -1);
676 String[] devTok = devComp.split(":");
677 major = Integer.parseInt(devTok[0]);
678 minor = Integer.parseInt(devTok[1]);
679 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700680 Slog.e(TAG, "Failed to parse major/minor", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800681 }
682
San Mehat4270e1e2010-01-29 05:32:19 -0800683 if (code == VoldResponseCode.VolumeDiskInserted) {
684 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600685 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800686 public void run() {
687 try {
688 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800689 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700690 Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
San Mehat4270e1e2010-01-29 05:32:19 -0800691 }
692 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700693 Slog.w(TAG, "Failed to mount media on insertion", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800694 }
695 }
696 }.start();
697 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
698 /*
699 * This event gets trumped if we're already in BAD_REMOVAL state
700 */
701 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
702 return true;
703 }
704 /* Send the media unmounted event first */
San Mehata5078592010-03-25 09:36:54 -0700705 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800706 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
707 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
708 mContext.sendBroadcast(in);
709
San Mehata5078592010-03-25 09:36:54 -0700710 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
San Mehat4270e1e2010-01-29 05:32:19 -0800711 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
712 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
713 } else if (code == VoldResponseCode.VolumeBadRemoval) {
San Mehata5078592010-03-25 09:36:54 -0700714 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800715 /* Send the media unmounted event first */
716 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
717 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
718 mContext.sendBroadcast(in);
719
San Mehata5078592010-03-25 09:36:54 -0700720 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
San Mehat4270e1e2010-01-29 05:32:19 -0800721 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
722 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
723 } else {
San Mehata5078592010-03-25 09:36:54 -0700724 Slog.e(TAG, String.format("Unknown code {%d}", code));
San Mehat4270e1e2010-01-29 05:32:19 -0800725 }
726 } else {
727 return false;
728 }
729
730 if (in != null) {
731 mContext.sendBroadcast(in);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400732 }
733 return true;
San Mehat4270e1e2010-01-29 05:32:19 -0800734 }
735
San Mehat207e5382010-02-04 20:46:54 -0800736 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800737 String vs = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -0700738 if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
San Mehat4270e1e2010-01-29 05:32:19 -0800739
740 Intent in = null;
741
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500742 if (oldState == VolumeState.Shared && newState != oldState) {
San Mehata5078592010-03-25 09:36:54 -0700743 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500744 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
745 Uri.parse("file://" + path)));
746 }
747
San Mehat4270e1e2010-01-29 05:32:19 -0800748 if (newState == VolumeState.Init) {
749 } else if (newState == VolumeState.NoMedia) {
750 // NoMedia is handled via Disk Remove events
751 } else if (newState == VolumeState.Idle) {
752 /*
753 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
754 * if we're in the process of enabling UMS
755 */
756 if (!vs.equals(
757 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
758 Environment.MEDIA_NOFS) && !vs.equals(
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800759 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
San Mehata5078592010-03-25 09:36:54 -0700760 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
San Mehat4270e1e2010-01-29 05:32:19 -0800761 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
762 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
763 }
764 } else if (newState == VolumeState.Pending) {
765 } else if (newState == VolumeState.Checking) {
San Mehata5078592010-03-25 09:36:54 -0700766 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
San Mehat4270e1e2010-01-29 05:32:19 -0800767 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
768 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
769 } else if (newState == VolumeState.Mounted) {
San Mehata5078592010-03-25 09:36:54 -0700770 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800771 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
San Mehat4270e1e2010-01-29 05:32:19 -0800772 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
773 in.putExtra("read-only", false);
774 } else if (newState == VolumeState.Unmounting) {
San Mehat4270e1e2010-01-29 05:32:19 -0800775 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
776 } else if (newState == VolumeState.Formatting) {
777 } else if (newState == VolumeState.Shared) {
San Mehata5078592010-03-25 09:36:54 -0700778 if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800779 /* Send the media unmounted event first */
780 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
781 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
782 mContext.sendBroadcast(in);
783
San Mehata5078592010-03-25 09:36:54 -0700784 if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
San Mehat4270e1e2010-01-29 05:32:19 -0800785 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
786 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
San Mehata5078592010-03-25 09:36:54 -0700787 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
San Mehat4270e1e2010-01-29 05:32:19 -0800788 } else if (newState == VolumeState.SharedMnt) {
San Mehata5078592010-03-25 09:36:54 -0700789 Slog.e(TAG, "Live shared mounts not supported yet!");
San Mehat4270e1e2010-01-29 05:32:19 -0800790 return;
791 } else {
San Mehata5078592010-03-25 09:36:54 -0700792 Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800793 }
794
795 if (in != null) {
796 mContext.sendBroadcast(in);
797 }
798 }
799
San Mehat207e5382010-02-04 20:46:54 -0800800 private boolean doGetShareMethodAvailable(String method) {
Kenny Root85fb2062010-06-01 20:50:21 -0700801 ArrayList<String> rsp;
Kenny Roota80ce062010-06-01 13:23:53 -0700802 try {
Kenny Root85fb2062010-06-01 20:50:21 -0700803 rsp = mConnector.doCommand("share status " + method);
Kenny Roota80ce062010-06-01 13:23:53 -0700804 } catch (NativeDaemonConnectorException ex) {
805 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
806 return false;
807 }
San Mehat207e5382010-02-04 20:46:54 -0800808
809 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700810 String[] tok = line.split(" ");
811 if (tok.length < 3) {
812 Slog.e(TAG, "Malformed response to share status " + method);
813 return false;
814 }
815
San Mehat207e5382010-02-04 20:46:54 -0800816 int code;
817 try {
818 code = Integer.parseInt(tok[0]);
819 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700820 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehat207e5382010-02-04 20:46:54 -0800821 return false;
822 }
823 if (code == VoldResponseCode.ShareStatusResult) {
824 if (tok[2].equals("available"))
825 return true;
826 return false;
827 } else {
San Mehata5078592010-03-25 09:36:54 -0700828 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehat207e5382010-02-04 20:46:54 -0800829 return false;
830 }
831 }
San Mehata5078592010-03-25 09:36:54 -0700832 Slog.e(TAG, "Got an empty response");
San Mehat207e5382010-02-04 20:46:54 -0800833 return false;
834 }
835
836 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800837 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800838
San Mehata5078592010-03-25 09:36:54 -0700839 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
San Mehat207e5382010-02-04 20:46:54 -0800840 try {
841 mConnector.doCommand(String.format("volume mount %s", path));
842 } catch (NativeDaemonConnectorException e) {
843 /*
844 * Mount failed for some reason
845 */
846 Intent in = null;
847 int code = e.getCode();
848 if (code == VoldResponseCode.OpFailedNoMedia) {
849 /*
850 * Attempt to mount but no media inserted
851 */
San Mehatb1043402010-02-05 08:26:50 -0800852 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800853 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
San Mehata5078592010-03-25 09:36:54 -0700854 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
San Mehat207e5382010-02-04 20:46:54 -0800855 /*
856 * Media is blank or does not contain a supported filesystem
857 */
858 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
859 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800860 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800861 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehata5078592010-03-25 09:36:54 -0700862 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
San Mehat207e5382010-02-04 20:46:54 -0800863 /*
864 * Volume consistency check failed
865 */
866 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
867 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800868 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800869 } else {
San Mehatb1043402010-02-05 08:26:50 -0800870 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800871 }
872
873 /*
874 * Send broadcast intent (if required for the failure)
875 */
876 if (in != null) {
877 mContext.sendBroadcast(in);
878 }
879 }
880
881 return rc;
882 }
883
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800884 /*
885 * If force is not set, we do not unmount if there are
886 * processes holding references to the volume about to be unmounted.
887 * If force is set, all the processes holding references need to be
888 * killed via the ActivityManager before actually unmounting the volume.
889 * This might even take a while and might be retried after timed delays
890 * to make sure we dont end up in an instable state and kill some core
891 * processes.
892 */
San Mehatd9709982010-02-18 11:43:03 -0800893 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800894 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800895 return VoldResponseCode.OpFailedVolNotMounted;
896 }
Kenny Rootaa485402010-09-14 14:49:41 -0700897
898 /*
899 * Force a GC to make sure AssetManagers in other threads of the
900 * system_server are cleaned up. We have to do this since AssetManager
901 * instances are kept as a WeakReference and it's possible we have files
902 * open on the external storage.
903 */
904 Runtime.getRuntime().gc();
905
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800906 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700907 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800908 try {
San Mehatd9709982010-02-18 11:43:03 -0800909 mConnector.doCommand(String.format(
910 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700911 // We unmounted the volume. None of the asec containers are available now.
912 synchronized (mAsecMountSet) {
913 mAsecMountSet.clear();
914 }
San Mehatb1043402010-02-05 08:26:50 -0800915 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800916 } catch (NativeDaemonConnectorException e) {
917 // Don't worry about mismatch in PackageManager since the
918 // call back will handle the status changes any way.
919 int code = e.getCode();
920 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800921 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800922 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
923 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800924 } else {
San Mehatb1043402010-02-05 08:26:50 -0800925 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800926 }
927 }
928 }
929
930 private int doFormatVolume(String path) {
931 try {
932 String cmd = String.format("volume format %s", path);
933 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800934 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800935 } catch (NativeDaemonConnectorException e) {
936 int code = e.getCode();
937 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800938 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800939 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800940 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800941 } else {
San Mehatb1043402010-02-05 08:26:50 -0800942 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800943 }
944 }
945 }
946
San Mehatb1043402010-02-05 08:26:50 -0800947 private boolean doGetVolumeShared(String path, String method) {
948 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700949 ArrayList<String> rsp;
950
951 try {
952 rsp = mConnector.doCommand(cmd);
953 } catch (NativeDaemonConnectorException ex) {
954 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
955 return false;
956 }
San Mehatb1043402010-02-05 08:26:50 -0800957
958 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700959 String[] tok = line.split(" ");
960 if (tok.length < 3) {
961 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
962 return false;
963 }
964
San Mehatb1043402010-02-05 08:26:50 -0800965 int code;
966 try {
967 code = Integer.parseInt(tok[0]);
968 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700969 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800970 return false;
971 }
972 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700973 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800974 } else {
San Mehata5078592010-03-25 09:36:54 -0700975 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800976 return false;
977 }
978 }
San Mehata5078592010-03-25 09:36:54 -0700979 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800980 return false;
981 }
982
San Mehat207e5382010-02-04 20:46:54 -0800983 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800984 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -0700985 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800986 return;
987 }
988
989 synchronized (mListeners) {
990 for (int i = mListeners.size() -1; i >= 0; i--) {
991 MountServiceBinderListener bl = mListeners.get(i);
992 try {
San Mehatb1043402010-02-05 08:26:50 -0800993 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -0800994 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700995 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800996 mListeners.remove(i);
997 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700998 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800999 }
1000 }
1001 }
1002
San Mehat207e5382010-02-04 20:46:54 -08001003 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -08001004 sendUmsIntent(avail);
1005 } else {
1006 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -08001007 }
San Mehat2fe718a2010-03-11 12:01:49 -08001008
1009 final String path = Environment.getExternalStorageDirectory().getPath();
1010 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
1011 /*
1012 * USB mass storage disconnected while enabled
1013 */
1014 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -06001015 @Override
San Mehat2fe718a2010-03-11 12:01:49 -08001016 public void run() {
1017 try {
1018 int rc;
San Mehata5078592010-03-25 09:36:54 -07001019 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -08001020 doShareUnshareVolume(path, "ums", false);
1021 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001022 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -08001023 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
1024 path, rc));
1025 }
1026 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -07001027 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -08001028 }
1029 }
1030 }.start();
1031 }
San Mehat4270e1e2010-01-29 05:32:19 -08001032 }
1033
San Mehat6a965af22010-02-24 17:47:30 -08001034 private void sendUmsIntent(boolean c) {
1035 mContext.sendBroadcast(
1036 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
1037 }
1038
San Mehat207e5382010-02-04 20:46:54 -08001039 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -08001040 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
1041 throw new SecurityException(String.format("Requires %s permission", perm));
1042 }
1043 }
1044
1045 /**
San Mehat207e5382010-02-04 20:46:54 -08001046 * Constructs a new MountService instance
1047 *
1048 * @param context Binder context for this service
1049 */
1050 public MountService(Context context) {
1051 mContext = context;
1052
Mike Lockwood03559752010-07-19 18:25:03 -04001053 mEmulateExternalStorage = context.getResources().getBoolean(
1054 com.android.internal.R.bool.config_emulateExternalStorage);
1055 if (mEmulateExternalStorage) {
1056 Slog.d(TAG, "using emulated external storage");
1057 mLegacyState = Environment.MEDIA_MOUNTED;
1058 }
1059
San Mehat207e5382010-02-04 20:46:54 -08001060 // XXX: This will go away soon in favor of IMountServiceObserver
1061 mPms = (PackageManagerService) ServiceManager.getService("package");
1062
1063 mContext.registerReceiver(mBroadcastReceiver,
1064 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
1065
Daniel Sandler5f27ef42010-03-16 15:42:02 -04001066 mHandlerThread = new HandlerThread("MountService");
1067 mHandlerThread.start();
1068 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
1069
Kenny Roota02b8b02010-08-05 16:14:17 -07001070 // Add OBB Action Handler to MountService thread.
1071 mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper());
1072
Marco Nelissenc34ebce2010-02-18 13:39:41 -08001073 /*
1074 * Vold does not run in the simulator, so pretend the connector thread
1075 * ran and did its thing.
1076 */
1077 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
1078 mReady = true;
1079 mUmsEnabling = true;
1080 return;
1081 }
1082
Kenny Root305bcbf2010-09-03 07:56:38 -07001083 /*
1084 * Create the connection to vold with a maximum queue of twice the
1085 * amount of containers we'd ever expect to have. This keeps an
1086 * "asec list" from blocking a thread repeatedly.
1087 */
1088 mConnector = new NativeDaemonConnector(this, "vold",
1089 PackageManagerService.MAX_CONTAINERS * 2, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001090 mReady = false;
Kenny Root305bcbf2010-09-03 07:56:38 -07001091 Thread thread = new Thread(mConnector, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001092 thread.start();
1093 }
1094
1095 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001096 * Exposed API calls below here
1097 */
1098
1099 public void registerListener(IMountServiceListener listener) {
1100 synchronized (mListeners) {
1101 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
1102 try {
1103 listener.asBinder().linkToDeath(bl, 0);
1104 mListeners.add(bl);
1105 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001106 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -08001107 }
1108 }
1109 }
1110
1111 public void unregisterListener(IMountServiceListener listener) {
1112 synchronized (mListeners) {
1113 for(MountServiceBinderListener bl : mListeners) {
1114 if (bl.mListener == listener) {
1115 mListeners.remove(mListeners.indexOf(bl));
1116 return;
1117 }
1118 }
1119 }
1120 }
1121
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001122 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -08001123 validatePermission(android.Manifest.permission.SHUTDOWN);
1124
San Mehata5078592010-03-25 09:36:54 -07001125 Slog.i(TAG, "Shutting down");
San Mehat4270e1e2010-01-29 05:32:19 -08001126
1127 String path = Environment.getExternalStorageDirectory().getPath();
1128 String state = getVolumeState(path);
San Mehat91c77612010-01-07 10:39:41 -08001129
1130 if (state.equals(Environment.MEDIA_SHARED)) {
1131 /*
1132 * If the media is currently shared, unshare it.
1133 * XXX: This is still dangerous!. We should not
1134 * be rebooting at *all* if UMS is enabled, since
1135 * the UMS host could have dirty FAT cache entries
1136 * yet to flush.
1137 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001138 setUsbMassStorageEnabled(false);
San Mehat91c77612010-01-07 10:39:41 -08001139 } else if (state.equals(Environment.MEDIA_CHECKING)) {
1140 /*
1141 * If the media is being checked, then we need to wait for
1142 * it to complete before being able to proceed.
1143 */
1144 // XXX: @hackbod - Should we disable the ANR timer here?
1145 int retries = 30;
1146 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
1147 try {
1148 Thread.sleep(1000);
1149 } catch (InterruptedException iex) {
San Mehata5078592010-03-25 09:36:54 -07001150 Slog.e(TAG, "Interrupted while waiting for media", iex);
San Mehat91c77612010-01-07 10:39:41 -08001151 break;
1152 }
1153 state = Environment.getExternalStorageState();
1154 }
1155 if (retries == 0) {
San Mehata5078592010-03-25 09:36:54 -07001156 Slog.e(TAG, "Timed out waiting for media to check");
San Mehat91c77612010-01-07 10:39:41 -08001157 }
1158 }
1159
1160 if (state.equals(Environment.MEDIA_MOUNTED)) {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001161 // Post a unmount message.
1162 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
1163 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
Johan Alfven5d0db4d2010-11-09 10:32:25 +01001164 } else if (observer != null) {
1165 /*
1166 * Observer is waiting for onShutDownComplete when we are done.
1167 * Since nothing will be done send notification directly so shutdown
1168 * sequence can continue.
1169 */
1170 try {
1171 observer.onShutDownComplete(StorageResultCode.OperationSucceeded);
1172 } catch (RemoteException e) {
1173 Slog.w(TAG, "RemoteException when shutting down");
1174 }
San Mehat4270e1e2010-01-29 05:32:19 -08001175 }
1176 }
1177
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001178 private boolean getUmsEnabling() {
1179 synchronized (mListeners) {
1180 return mUmsEnabling;
1181 }
1182 }
1183
1184 private void setUmsEnabling(boolean enable) {
1185 synchronized (mListeners) {
Tony Wufc711252010-08-09 16:49:19 +08001186 mUmsEnabling = enable;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001187 }
1188 }
1189
San Mehatb1043402010-02-05 08:26:50 -08001190 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -08001191 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -08001192
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001193 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001194 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001195 }
San Mehatb1043402010-02-05 08:26:50 -08001196 return doGetShareMethodAvailable("ums");
1197 }
1198
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001199 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001200 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001201 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001202
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001203 // TODO: Add support for multiple share methods
1204
1205 /*
1206 * If the volume is mounted and we're enabling then unmount it
1207 */
1208 String path = Environment.getExternalStorageDirectory().getPath();
1209 String vs = getVolumeState(path);
1210 String method = "ums";
1211 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1212 // Override for isUsbMassStorageEnabled()
1213 setUmsEnabling(enable);
1214 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1215 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1216 // Clear override
1217 setUmsEnabling(false);
1218 }
1219 /*
1220 * If we disabled UMS then mount the volume
1221 */
1222 if (!enable) {
1223 doShareUnshareVolume(path, method, enable);
1224 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001225 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001226 " after disabling share method " + method);
1227 /*
1228 * Even though the mount failed, the unshare didn't so don't indicate an error.
1229 * The mountVolume() call will have set the storage state and sent the necessary
1230 * broadcasts.
1231 */
1232 }
1233 }
San Mehatb1043402010-02-05 08:26:50 -08001234 }
1235
1236 public boolean isUsbMassStorageEnabled() {
1237 waitForReady();
1238 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 }
San Mehat4270e1e2010-01-29 05:32:19 -08001240
San Mehat7fd0fee2009-12-17 07:12:23 -08001241 /**
1242 * @return state of the volume at the specified mount point
1243 */
San Mehat4270e1e2010-01-29 05:32:19 -08001244 public String getVolumeState(String mountPoint) {
San Mehat7fd0fee2009-12-17 07:12:23 -08001245 /*
1246 * XXX: Until we have multiple volume discovery, just hardwire
1247 * this to /sdcard
1248 */
1249 if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -07001250 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
San Mehat7fd0fee2009-12-17 07:12:23 -08001251 throw new IllegalArgumentException();
1252 }
1253
1254 return mLegacyState;
1255 }
1256
Kenny Roote1ff2142010-10-12 11:20:01 -07001257 public boolean isExternalStorageEmulated() {
1258 return mEmulateExternalStorage;
1259 }
1260
San Mehat4270e1e2010-01-29 05:32:19 -08001261 public int mountVolume(String path) {
1262 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001263
San Mehat207e5382010-02-04 20:46:54 -08001264 waitForReady();
1265 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 }
1267
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001268 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001269 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001270 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001272 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001273 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001274 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1275 Environment.MEDIA_REMOVED.equals(volState) ||
1276 Environment.MEDIA_SHARED.equals(volState) ||
1277 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1278 // Media already unmounted or cannot be unmounted.
1279 // TODO return valid return code when adding observer call back.
1280 return;
1281 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001282 UnmountCallBack ucb = new UnmountCallBack(path, force);
1283 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 }
1285
San Mehat4270e1e2010-01-29 05:32:19 -08001286 public int formatVolume(String path) {
1287 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001288 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001289
San Mehat207e5382010-02-04 20:46:54 -08001290 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 }
1292
San Mehatc1b4ce92010-02-16 17:13:03 -08001293 public int []getStorageUsers(String path) {
1294 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1295 waitForReady();
1296 try {
1297 String[] r = mConnector.doListCommand(
1298 String.format("storage users %s", path),
1299 VoldResponseCode.StorageUsersListResult);
1300 // FMT: <pid> <process name>
1301 int[] data = new int[r.length];
1302 for (int i = 0; i < r.length; i++) {
1303 String []tok = r[i].split(" ");
1304 try {
1305 data[i] = Integer.parseInt(tok[0]);
1306 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001307 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001308 return new int[0];
1309 }
1310 }
1311 return data;
1312 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001313 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001314 return new int[0];
1315 }
1316 }
1317
San Mehatb1043402010-02-05 08:26:50 -08001318 private void warnOnNotMounted() {
1319 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001320 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001321 }
1322 }
1323
San Mehat4270e1e2010-01-29 05:32:19 -08001324 public String[] getSecureContainerList() {
1325 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001326 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001327 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001328
San Mehat4270e1e2010-01-29 05:32:19 -08001329 try {
1330 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1331 } catch (NativeDaemonConnectorException e) {
1332 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 }
1334 }
San Mehat36972292010-01-06 11:06:32 -08001335
San Mehat4270e1e2010-01-29 05:32:19 -08001336 public int createSecureContainer(String id, int sizeMb, String fstype,
1337 String key, int ownerUid) {
1338 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001339 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001340 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001341
San Mehatb1043402010-02-05 08:26:50 -08001342 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001343 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1344 try {
1345 mConnector.doCommand(cmd);
1346 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001347 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001348 }
San Mehata181b212010-02-11 06:50:20 -08001349
1350 if (rc == StorageResultCode.OperationSucceeded) {
1351 synchronized (mAsecMountSet) {
1352 mAsecMountSet.add(id);
1353 }
1354 }
San Mehat4270e1e2010-01-29 05:32:19 -08001355 return rc;
San Mehat36972292010-01-06 11:06:32 -08001356 }
1357
San Mehat4270e1e2010-01-29 05:32:19 -08001358 public int finalizeSecureContainer(String id) {
1359 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001360 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001361
San Mehatb1043402010-02-05 08:26:50 -08001362 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001363 try {
1364 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001365 /*
1366 * Finalization does a remount, so no need
1367 * to update mAsecMountSet
1368 */
San Mehat4270e1e2010-01-29 05:32:19 -08001369 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001370 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001371 }
San Mehat4270e1e2010-01-29 05:32:19 -08001372 return rc;
San Mehat36972292010-01-06 11:06:32 -08001373 }
1374
San Mehatd9709982010-02-18 11:43:03 -08001375 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001376 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001377 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001378 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001379
Kenny Rootaa485402010-09-14 14:49:41 -07001380 /*
1381 * Force a GC to make sure AssetManagers in other threads of the
1382 * system_server are cleaned up. We have to do this since AssetManager
1383 * instances are kept as a WeakReference and it's possible we have files
1384 * open on the external storage.
1385 */
1386 Runtime.getRuntime().gc();
1387
San Mehatb1043402010-02-05 08:26:50 -08001388 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001389 try {
San Mehatd9709982010-02-18 11:43:03 -08001390 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001391 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001392 int code = e.getCode();
1393 if (code == VoldResponseCode.OpFailedStorageBusy) {
1394 rc = StorageResultCode.OperationFailedStorageBusy;
1395 } else {
1396 rc = StorageResultCode.OperationFailedInternalError;
1397 }
San Mehat02735bc2010-01-26 15:18:08 -08001398 }
San Mehata181b212010-02-11 06:50:20 -08001399
1400 if (rc == StorageResultCode.OperationSucceeded) {
1401 synchronized (mAsecMountSet) {
1402 if (mAsecMountSet.contains(id)) {
1403 mAsecMountSet.remove(id);
1404 }
1405 }
1406 }
1407
San Mehat4270e1e2010-01-29 05:32:19 -08001408 return rc;
San Mehat36972292010-01-06 11:06:32 -08001409 }
1410
San Mehat4270e1e2010-01-29 05:32:19 -08001411 public int mountSecureContainer(String id, String key, int ownerUid) {
1412 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001413 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001414 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001415
San Mehata181b212010-02-11 06:50:20 -08001416 synchronized (mAsecMountSet) {
1417 if (mAsecMountSet.contains(id)) {
1418 return StorageResultCode.OperationFailedStorageMounted;
1419 }
1420 }
1421
San Mehatb1043402010-02-05 08:26:50 -08001422 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001423 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1424 try {
1425 mConnector.doCommand(cmd);
1426 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001427 int code = e.getCode();
1428 if (code != VoldResponseCode.OpFailedStorageBusy) {
1429 rc = StorageResultCode.OperationFailedInternalError;
1430 }
San Mehat02735bc2010-01-26 15:18:08 -08001431 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001432
1433 if (rc == StorageResultCode.OperationSucceeded) {
1434 synchronized (mAsecMountSet) {
1435 mAsecMountSet.add(id);
1436 }
1437 }
San Mehat4270e1e2010-01-29 05:32:19 -08001438 return rc;
San Mehat36972292010-01-06 11:06:32 -08001439 }
1440
San Mehatd9709982010-02-18 11:43:03 -08001441 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001442 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001443 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001444 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001445
San Mehat6cdd9c02010-02-09 14:45:20 -08001446 synchronized (mAsecMountSet) {
1447 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001448 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001449 }
1450 }
1451
Kenny Rootaa485402010-09-14 14:49:41 -07001452 /*
1453 * Force a GC to make sure AssetManagers in other threads of the
1454 * system_server are cleaned up. We have to do this since AssetManager
1455 * instances are kept as a WeakReference and it's possible we have files
1456 * open on the external storage.
1457 */
1458 Runtime.getRuntime().gc();
1459
San Mehatb1043402010-02-05 08:26:50 -08001460 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001461 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001462 try {
1463 mConnector.doCommand(cmd);
1464 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001465 int code = e.getCode();
1466 if (code == VoldResponseCode.OpFailedStorageBusy) {
1467 rc = StorageResultCode.OperationFailedStorageBusy;
1468 } else {
1469 rc = StorageResultCode.OperationFailedInternalError;
1470 }
San Mehat02735bc2010-01-26 15:18:08 -08001471 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001472
1473 if (rc == StorageResultCode.OperationSucceeded) {
1474 synchronized (mAsecMountSet) {
1475 mAsecMountSet.remove(id);
1476 }
1477 }
San Mehat4270e1e2010-01-29 05:32:19 -08001478 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001479 }
1480
San Mehat6cdd9c02010-02-09 14:45:20 -08001481 public boolean isSecureContainerMounted(String id) {
1482 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1483 waitForReady();
1484 warnOnNotMounted();
1485
1486 synchronized (mAsecMountSet) {
1487 return mAsecMountSet.contains(id);
1488 }
1489 }
1490
San Mehat4270e1e2010-01-29 05:32:19 -08001491 public int renameSecureContainer(String oldId, String newId) {
1492 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001493 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001494 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001495
San Mehata181b212010-02-11 06:50:20 -08001496 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001497 /*
1498 * Because a mounted container has active internal state which cannot be
1499 * changed while active, we must ensure both ids are not currently mounted.
1500 */
1501 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001502 return StorageResultCode.OperationFailedStorageMounted;
1503 }
1504 }
1505
San Mehatb1043402010-02-05 08:26:50 -08001506 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001507 String cmd = String.format("asec rename %s %s", oldId, newId);
1508 try {
1509 mConnector.doCommand(cmd);
1510 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001511 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001512 }
San Mehata181b212010-02-11 06:50:20 -08001513
San Mehat4270e1e2010-01-29 05:32:19 -08001514 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001515 }
1516
San Mehat4270e1e2010-01-29 05:32:19 -08001517 public String getSecureContainerPath(String id) {
1518 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001519 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001520 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001521
San Mehat2d66cef2010-03-23 11:12:52 -07001522 try {
1523 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1524 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001525 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001526 if (code != VoldResponseCode.AsecPathResult) {
1527 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1528 }
1529 return tok[1];
1530 } catch (NativeDaemonConnectorException e) {
1531 int code = e.getCode();
1532 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1533 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001534 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001535 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001536 }
1537 }
San Mehat22dd86e2010-01-12 12:21:18 -08001538 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001539
1540 public void finishMediaUpdate() {
1541 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1542 }
Kenny Root02c87302010-07-01 08:10:18 -07001543
Kenny Roota02b8b02010-08-05 16:14:17 -07001544 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
1545 if (callerUid == android.os.Process.SYSTEM_UID) {
1546 return true;
1547 }
1548
Kenny Root02c87302010-07-01 08:10:18 -07001549 if (packageName == null) {
1550 return false;
1551 }
1552
1553 final int packageUid = mPms.getPackageUid(packageName);
1554
1555 if (DEBUG_OBB) {
1556 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
1557 packageUid + ", callerUid = " + callerUid);
1558 }
1559
1560 return callerUid == packageUid;
1561 }
1562
1563 public String getMountedObbPath(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001564 if (filename == null) {
1565 throw new IllegalArgumentException("filename cannot be null");
1566 }
1567
Kenny Root02c87302010-07-01 08:10:18 -07001568 waitForReady();
1569 warnOnNotMounted();
1570
Kenny Root02c87302010-07-01 08:10:18 -07001571 try {
1572 ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename));
1573 String []tok = rsp.get(0).split(" ");
1574 int code = Integer.parseInt(tok[0]);
1575 if (code != VoldResponseCode.AsecPathResult) {
1576 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1577 }
1578 return tok[1];
1579 } catch (NativeDaemonConnectorException e) {
1580 int code = e.getCode();
1581 if (code == VoldResponseCode.OpFailedStorageNotFound) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001582 return null;
Kenny Root02c87302010-07-01 08:10:18 -07001583 } else {
1584 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1585 }
1586 }
1587 }
1588
1589 public boolean isObbMounted(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001590 if (filename == null) {
1591 throw new IllegalArgumentException("filename cannot be null");
Kenny Root02c87302010-07-01 08:10:18 -07001592 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001593
1594 synchronized (mObbMounts) {
1595 return mObbPathToStateMap.containsKey(filename);
1596 }
Kenny Root02c87302010-07-01 08:10:18 -07001597 }
1598
Kenny Rootaf9d6672010-10-08 09:21:39 -07001599 public void mountObb(String filename, String key, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -07001600 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001601 if (filename == null) {
1602 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootaf9d6672010-10-08 09:21:39 -07001603 }
1604
1605 if (token == null) {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001606 throw new IllegalArgumentException("token cannot be null");
1607 }
1608
Kenny Rootaf9d6672010-10-08 09:21:39 -07001609 final int callerUid = Binder.getCallingUid();
1610 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1611 final ObbAction action = new MountObbAction(obbState, key);
Kenny Roota02b8b02010-08-05 16:14:17 -07001612 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
1613
1614 if (DEBUG_OBB)
1615 Slog.i(TAG, "Send to OBB handler: " + action.toString());
Kenny Root02c87302010-07-01 08:10:18 -07001616 }
1617
Kenny Rootaf9d6672010-10-08 09:21:39 -07001618 public void unmountObb(String filename, boolean force, IObbActionListener token, int nonce)
1619 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001620 if (filename == null) {
1621 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootf1121dc2010-09-29 07:30:53 -07001622 }
1623
Kenny Rootaf9d6672010-10-08 09:21:39 -07001624 final int callerUid = Binder.getCallingUid();
1625 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1626 final ObbAction action = new UnmountObbAction(obbState, force);
Kenny Roota02b8b02010-08-05 16:14:17 -07001627 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root02c87302010-07-01 08:10:18 -07001628
Kenny Roota02b8b02010-08-05 16:14:17 -07001629 if (DEBUG_OBB)
1630 Slog.i(TAG, "Send to OBB handler: " + action.toString());
1631 }
1632
Jason parks5af0b912010-11-29 09:05:25 -06001633 public int decryptStorage(String password) {
1634 if (password == null) {
1635 throw new IllegalArgumentException("password cannot be null");
1636 }
1637
1638 // TODO: Enforce a permission
1639
1640 waitForReady();
1641
1642 if (DEBUG_EVENTS) {
1643 Slog.i(TAG, "decrypting storage...");
1644 }
1645
1646 try {
1647 mConnector.doCommand(String.format("cryptfs checkpw %s", password));
1648 } catch (NativeDaemonConnectorException e) {
1649 // Decryption failed
1650 return e.getCode();
1651 }
1652
1653 return 0;
1654 }
1655
Jason parks56aa5322011-01-07 09:01:15 -06001656 public int encryptStorage(String password) {
1657 if (password == null) {
1658 throw new IllegalArgumentException("password cannot be null");
1659 }
1660
1661 // TODO: Enforce a permission
1662
1663 waitForReady();
1664
1665 if (DEBUG_EVENTS) {
1666 Slog.i(TAG, "decrypting storage...");
1667 }
1668
1669 try {
1670 mConnector.doCommand(String.format("cryptfs enablecrypto wipe %s", password));
1671 } catch (NativeDaemonConnectorException e) {
1672 // Encryption failed
1673 return e.getCode();
1674 }
1675
1676 return 0;
1677 }
1678
Kenny Rootaf9d6672010-10-08 09:21:39 -07001679 private void addObbStateLocked(ObbState obbState) throws RemoteException {
1680 final IBinder binder = obbState.getBinder();
1681 List<ObbState> obbStates = mObbMounts.get(binder);
Kenny Root5919ac62010-10-05 09:49:40 -07001682
Kenny Rootaf9d6672010-10-08 09:21:39 -07001683 if (obbStates == null) {
1684 obbStates = new ArrayList<ObbState>();
1685 mObbMounts.put(binder, obbStates);
1686 } else {
1687 for (final ObbState o : obbStates) {
1688 if (o.filename.equals(obbState.filename)) {
1689 throw new IllegalStateException("Attempt to add ObbState twice. "
1690 + "This indicates an error in the MountService logic.");
Kenny Root5919ac62010-10-05 09:49:40 -07001691 }
1692 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001693 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001694
1695 obbStates.add(obbState);
1696 try {
1697 obbState.link();
1698 } catch (RemoteException e) {
1699 /*
1700 * The binder died before we could link it, so clean up our state
1701 * and return failure.
1702 */
1703 obbStates.remove(obbState);
1704 if (obbStates.isEmpty()) {
1705 mObbMounts.remove(binder);
1706 }
1707
1708 // Rethrow the error so mountObb can get it
1709 throw e;
1710 }
1711
1712 mObbPathToStateMap.put(obbState.filename, obbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07001713 }
1714
Kenny Rootaf9d6672010-10-08 09:21:39 -07001715 private void removeObbStateLocked(ObbState obbState) {
1716 final IBinder binder = obbState.getBinder();
1717 final List<ObbState> obbStates = mObbMounts.get(binder);
1718 if (obbStates != null) {
1719 if (obbStates.remove(obbState)) {
1720 obbState.unlink();
Kenny Root05105f72010-09-22 17:29:43 -07001721 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001722 if (obbStates.isEmpty()) {
1723 mObbMounts.remove(binder);
1724 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001725 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001726
Kenny Rootaf9d6672010-10-08 09:21:39 -07001727 mObbPathToStateMap.remove(obbState.filename);
Kenny Root38cf8862010-09-26 14:18:51 -07001728 }
1729
Kenny Roota02b8b02010-08-05 16:14:17 -07001730 private class ObbActionHandler extends Handler {
1731 private boolean mBound = false;
Kenny Root480afe72010-10-07 10:17:50 -07001732 private final List<ObbAction> mActions = new LinkedList<ObbAction>();
Kenny Roota02b8b02010-08-05 16:14:17 -07001733
1734 ObbActionHandler(Looper l) {
1735 super(l);
1736 }
1737
1738 @Override
1739 public void handleMessage(Message msg) {
1740 switch (msg.what) {
1741 case OBB_RUN_ACTION: {
Kenny Root480afe72010-10-07 10:17:50 -07001742 final ObbAction action = (ObbAction) msg.obj;
Kenny Roota02b8b02010-08-05 16:14:17 -07001743
1744 if (DEBUG_OBB)
1745 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
1746
1747 // If a bind was already initiated we don't really
1748 // need to do anything. The pending install
1749 // will be processed later on.
1750 if (!mBound) {
1751 // If this is the only one pending we might
1752 // have to bind to the service again.
1753 if (!connectToService()) {
1754 Slog.e(TAG, "Failed to bind to media container service");
1755 action.handleError();
1756 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07001757 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001758 }
Kenny Root735de3b2010-09-30 14:11:39 -07001759
Kenny Root735de3b2010-09-30 14:11:39 -07001760 mActions.add(action);
Kenny Roota02b8b02010-08-05 16:14:17 -07001761 break;
1762 }
1763 case OBB_MCS_BOUND: {
1764 if (DEBUG_OBB)
1765 Slog.i(TAG, "OBB_MCS_BOUND");
1766 if (msg.obj != null) {
1767 mContainerService = (IMediaContainerService) msg.obj;
1768 }
1769 if (mContainerService == null) {
1770 // Something seriously wrong. Bail out
1771 Slog.e(TAG, "Cannot bind to media container service");
1772 for (ObbAction action : mActions) {
1773 // Indicate service bind error
1774 action.handleError();
1775 }
1776 mActions.clear();
1777 } else if (mActions.size() > 0) {
Kenny Root480afe72010-10-07 10:17:50 -07001778 final ObbAction action = mActions.get(0);
Kenny Roota02b8b02010-08-05 16:14:17 -07001779 if (action != null) {
1780 action.execute(this);
1781 }
1782 } else {
1783 // Should never happen ideally.
1784 Slog.w(TAG, "Empty queue");
1785 }
1786 break;
1787 }
1788 case OBB_MCS_RECONNECT: {
1789 if (DEBUG_OBB)
1790 Slog.i(TAG, "OBB_MCS_RECONNECT");
1791 if (mActions.size() > 0) {
1792 if (mBound) {
1793 disconnectService();
1794 }
1795 if (!connectToService()) {
1796 Slog.e(TAG, "Failed to bind to media container service");
1797 for (ObbAction action : mActions) {
1798 // Indicate service bind error
1799 action.handleError();
1800 }
1801 mActions.clear();
1802 }
1803 }
1804 break;
1805 }
1806 case OBB_MCS_UNBIND: {
1807 if (DEBUG_OBB)
1808 Slog.i(TAG, "OBB_MCS_UNBIND");
1809
1810 // Delete pending install
1811 if (mActions.size() > 0) {
1812 mActions.remove(0);
1813 }
1814 if (mActions.size() == 0) {
1815 if (mBound) {
1816 disconnectService();
1817 }
1818 } else {
1819 // There are more pending requests in queue.
1820 // Just post MCS_BOUND message to trigger processing
1821 // of next pending install.
1822 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1823 }
1824 break;
1825 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001826 case OBB_FLUSH_MOUNT_STATE: {
1827 final String path = (String) msg.obj;
1828
1829 if (DEBUG_OBB)
1830 Slog.i(TAG, "Flushing all OBB state for path " + path);
1831
1832 synchronized (mObbMounts) {
1833 final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>();
1834
1835 final Iterator<Entry<String, ObbState>> i =
1836 mObbPathToStateMap.entrySet().iterator();
1837 while (i.hasNext()) {
1838 final Entry<String, ObbState> obbEntry = i.next();
1839
1840 /*
1841 * If this entry's source file is in the volume path
1842 * that got unmounted, remove it because it's no
1843 * longer valid.
1844 */
1845 if (obbEntry.getKey().startsWith(path)) {
1846 obbStatesToRemove.add(obbEntry.getValue());
1847 }
1848 }
1849
1850 for (final ObbState obbState : obbStatesToRemove) {
1851 if (DEBUG_OBB)
1852 Slog.i(TAG, "Removing state for " + obbState.filename);
1853
1854 removeObbStateLocked(obbState);
1855
1856 try {
1857 obbState.token.onObbResult(obbState.filename, obbState.nonce,
1858 OnObbStateChangeListener.UNMOUNTED);
1859 } catch (RemoteException e) {
1860 Slog.i(TAG, "Couldn't send unmount notification for OBB: "
1861 + obbState.filename);
1862 }
1863 }
1864 }
1865 break;
1866 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001867 }
1868 }
1869
1870 private boolean connectToService() {
1871 if (DEBUG_OBB)
1872 Slog.i(TAG, "Trying to bind to DefaultContainerService");
1873
1874 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
1875 if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) {
1876 mBound = true;
1877 return true;
1878 }
1879 return false;
1880 }
1881
1882 private void disconnectService() {
1883 mContainerService = null;
1884 mBound = false;
1885 mContext.unbindService(mDefContainerConn);
1886 }
1887 }
1888
1889 abstract class ObbAction {
1890 private static final int MAX_RETRIES = 3;
1891 private int mRetries;
1892
1893 ObbState mObbState;
1894
1895 ObbAction(ObbState obbState) {
1896 mObbState = obbState;
1897 }
1898
1899 public void execute(ObbActionHandler handler) {
1900 try {
1901 if (DEBUG_OBB)
1902 Slog.i(TAG, "Starting to execute action: " + this.toString());
1903 mRetries++;
1904 if (mRetries > MAX_RETRIES) {
1905 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
Kenny Root480afe72010-10-07 10:17:50 -07001906 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001907 handleError();
1908 return;
1909 } else {
1910 handleExecute();
1911 if (DEBUG_OBB)
1912 Slog.i(TAG, "Posting install MCS_UNBIND");
1913 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1914 }
1915 } catch (RemoteException e) {
1916 if (DEBUG_OBB)
1917 Slog.i(TAG, "Posting install MCS_RECONNECT");
1918 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
1919 } catch (Exception e) {
1920 if (DEBUG_OBB)
1921 Slog.d(TAG, "Error handling OBB action", e);
1922 handleError();
Kenny Root17eb6fb2010-10-06 15:02:52 -07001923 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001924 }
1925 }
1926
Kenny Root05105f72010-09-22 17:29:43 -07001927 abstract void handleExecute() throws RemoteException, IOException;
Kenny Roota02b8b02010-08-05 16:14:17 -07001928 abstract void handleError();
Kenny Root38cf8862010-09-26 14:18:51 -07001929
1930 protected ObbInfo getObbInfo() throws IOException {
1931 ObbInfo obbInfo;
1932 try {
1933 obbInfo = mContainerService.getObbInfo(mObbState.filename);
1934 } catch (RemoteException e) {
1935 Slog.d(TAG, "Couldn't call DefaultContainerService to fetch OBB info for "
1936 + mObbState.filename);
1937 obbInfo = null;
1938 }
1939 if (obbInfo == null) {
1940 throw new IOException("Couldn't read OBB file: " + mObbState.filename);
1941 }
1942 return obbInfo;
1943 }
1944
Kenny Rootaf9d6672010-10-08 09:21:39 -07001945 protected void sendNewStatusOrIgnore(int status) {
1946 if (mObbState == null || mObbState.token == null) {
1947 return;
1948 }
1949
Kenny Root38cf8862010-09-26 14:18:51 -07001950 try {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001951 mObbState.token.onObbResult(mObbState.filename, mObbState.nonce, status);
Kenny Root38cf8862010-09-26 14:18:51 -07001952 } catch (RemoteException e) {
1953 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1954 }
1955 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001956 }
1957
1958 class MountObbAction extends ObbAction {
1959 private String mKey;
1960
1961 MountObbAction(ObbState obbState, String key) {
1962 super(obbState);
1963 mKey = key;
1964 }
1965
Jason parks5af0b912010-11-29 09:05:25 -06001966 @Override
Kenny Root735de3b2010-09-30 14:11:39 -07001967 public void handleExecute() throws IOException, RemoteException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001968 waitForReady();
1969 warnOnNotMounted();
1970
Kenny Root38cf8862010-09-26 14:18:51 -07001971 final ObbInfo obbInfo = getObbInfo();
1972
Kenny Roota02b8b02010-08-05 16:14:17 -07001973 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001974 Slog.w(TAG, "Denied attempt to mount OBB " + obbInfo.filename
1975 + " which is owned by " + obbInfo.packageName);
1976 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
1977 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07001978 }
1979
Kenny Rootaf9d6672010-10-08 09:21:39 -07001980 final boolean isMounted;
1981 synchronized (mObbMounts) {
1982 isMounted = mObbPathToStateMap.containsKey(obbInfo.filename);
1983 }
1984 if (isMounted) {
1985 Slog.w(TAG, "Attempt to mount OBB which is already mounted: " + obbInfo.filename);
1986 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_ALREADY_MOUNTED);
1987 return;
1988 }
1989
1990 /*
1991 * The filename passed in might not be the canonical name, so just
1992 * set the filename to the canonicalized version.
1993 */
1994 mObbState.filename = obbInfo.filename;
1995
1996 final String hashedKey;
1997 if (mKey == null) {
1998 hashedKey = "none";
1999 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002000 try {
Kenny Root3b1abba2010-10-13 15:00:07 -07002001 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
2002
2003 KeySpec ks = new PBEKeySpec(mKey.toCharArray(), obbInfo.salt,
2004 PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE);
2005 SecretKey key = factory.generateSecret(ks);
2006 BigInteger bi = new BigInteger(key.getEncoded());
2007 hashedKey = bi.toString(16);
Kenny Rootaf9d6672010-10-08 09:21:39 -07002008 } catch (NoSuchAlgorithmException e) {
Kenny Root3b1abba2010-10-13 15:00:07 -07002009 Slog.e(TAG, "Could not load PBKDF2 algorithm", e);
2010 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
2011 return;
2012 } catch (InvalidKeySpecException e) {
2013 Slog.e(TAG, "Invalid key spec when loading PBKDF2 algorithm", e);
2014 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root38cf8862010-09-26 14:18:51 -07002015 return;
2016 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002017 }
Kenny Root38cf8862010-09-26 14:18:51 -07002018
Kenny Rootaf9d6672010-10-08 09:21:39 -07002019 int rc = StorageResultCode.OperationSucceeded;
2020 String cmd = String.format("obb mount %s %s %d", mObbState.filename, hashedKey,
2021 mObbState.callerUid);
2022 try {
2023 mConnector.doCommand(cmd);
2024 } catch (NativeDaemonConnectorException e) {
2025 int code = e.getCode();
2026 if (code != VoldResponseCode.OpFailedStorageBusy) {
2027 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002028 }
2029 }
2030
Kenny Rootaf9d6672010-10-08 09:21:39 -07002031 if (rc == StorageResultCode.OperationSucceeded) {
2032 if (DEBUG_OBB)
2033 Slog.d(TAG, "Successfully mounted OBB " + mObbState.filename);
2034
2035 synchronized (mObbMounts) {
2036 addObbStateLocked(mObbState);
2037 }
2038
2039 sendNewStatusOrIgnore(OnObbStateChangeListener.MOUNTED);
Kenny Root02c87302010-07-01 08:10:18 -07002040 } else {
Kenny Root05105f72010-09-22 17:29:43 -07002041 Slog.e(TAG, "Couldn't mount OBB file: " + rc);
Kenny Roota02b8b02010-08-05 16:14:17 -07002042
Kenny Rootaf9d6672010-10-08 09:21:39 -07002043 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT);
Kenny Root02c87302010-07-01 08:10:18 -07002044 }
2045 }
2046
Jason parks5af0b912010-11-29 09:05:25 -06002047 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002048 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002049 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root02c87302010-07-01 08:10:18 -07002050 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002051
2052 @Override
2053 public String toString() {
2054 StringBuilder sb = new StringBuilder();
2055 sb.append("MountObbAction{");
2056 sb.append("filename=");
2057 sb.append(mObbState.filename);
2058 sb.append(",callerUid=");
2059 sb.append(mObbState.callerUid);
2060 sb.append(",token=");
2061 sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002062 sb.append(",binder=");
2063 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002064 sb.append('}');
2065 return sb.toString();
2066 }
2067 }
2068
2069 class UnmountObbAction extends ObbAction {
2070 private boolean mForceUnmount;
2071
2072 UnmountObbAction(ObbState obbState, boolean force) {
2073 super(obbState);
2074 mForceUnmount = force;
2075 }
2076
Jason parks5af0b912010-11-29 09:05:25 -06002077 @Override
Kenny Root38cf8862010-09-26 14:18:51 -07002078 public void handleExecute() throws IOException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002079 waitForReady();
2080 warnOnNotMounted();
2081
Kenny Root38cf8862010-09-26 14:18:51 -07002082 final ObbInfo obbInfo = getObbInfo();
Kenny Roota02b8b02010-08-05 16:14:17 -07002083
Kenny Rootaf9d6672010-10-08 09:21:39 -07002084 final ObbState obbState;
Kenny Root38cf8862010-09-26 14:18:51 -07002085 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002086 obbState = mObbPathToStateMap.get(obbInfo.filename);
2087 }
Kenny Root38cf8862010-09-26 14:18:51 -07002088
Kenny Rootaf9d6672010-10-08 09:21:39 -07002089 if (obbState == null) {
2090 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_NOT_MOUNTED);
2091 return;
2092 }
2093
2094 if (obbState.callerUid != mObbState.callerUid) {
2095 Slog.w(TAG, "Permission denied attempting to unmount OBB " + obbInfo.filename
2096 + " (owned by " + obbInfo.packageName + ")");
2097 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2098 return;
2099 }
2100
2101 mObbState.filename = obbInfo.filename;
2102
2103 int rc = StorageResultCode.OperationSucceeded;
2104 String cmd = String.format("obb unmount %s%s", mObbState.filename,
2105 (mForceUnmount ? " force" : ""));
2106 try {
2107 mConnector.doCommand(cmd);
2108 } catch (NativeDaemonConnectorException e) {
2109 int code = e.getCode();
2110 if (code == VoldResponseCode.OpFailedStorageBusy) {
2111 rc = StorageResultCode.OperationFailedStorageBusy;
2112 } else if (code == VoldResponseCode.OpFailedStorageNotFound) {
2113 // If it's not mounted then we've already won.
2114 rc = StorageResultCode.OperationSucceeded;
2115 } else {
2116 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002117 }
2118 }
2119
Kenny Rootaf9d6672010-10-08 09:21:39 -07002120 if (rc == StorageResultCode.OperationSucceeded) {
2121 synchronized (mObbMounts) {
2122 removeObbStateLocked(obbState);
Kenny Root38cf8862010-09-26 14:18:51 -07002123 }
2124
Kenny Rootaf9d6672010-10-08 09:21:39 -07002125 sendNewStatusOrIgnore(OnObbStateChangeListener.UNMOUNTED);
Kenny Roota02b8b02010-08-05 16:14:17 -07002126 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002127 Slog.w(TAG, "Could not mount OBB: " + mObbState.filename);
2128 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT);
Kenny Roota02b8b02010-08-05 16:14:17 -07002129 }
2130 }
2131
Jason parks5af0b912010-11-29 09:05:25 -06002132 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002133 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002134 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Roota02b8b02010-08-05 16:14:17 -07002135 }
2136
2137 @Override
2138 public String toString() {
2139 StringBuilder sb = new StringBuilder();
2140 sb.append("UnmountObbAction{");
2141 sb.append("filename=");
2142 sb.append(mObbState.filename != null ? mObbState.filename : "null");
2143 sb.append(",force=");
2144 sb.append(mForceUnmount);
2145 sb.append(",callerUid=");
2146 sb.append(mObbState.callerUid);
2147 sb.append(",token=");
2148 sb.append(mObbState.token != null ? mObbState.token.toString() : "null");
Kenny Root735de3b2010-09-30 14:11:39 -07002149 sb.append(",binder=");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002150 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002151 sb.append('}');
2152 return sb.toString();
2153 }
Kenny Root02c87302010-07-01 08:10:18 -07002154 }
Kenny Root38cf8862010-09-26 14:18:51 -07002155
2156 @Override
2157 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2158 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
2159 pw.println("Permission Denial: can't dump ActivityManager from from pid="
2160 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
2161 + " without permission " + android.Manifest.permission.DUMP);
2162 return;
2163 }
2164
Kenny Root38cf8862010-09-26 14:18:51 -07002165 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002166 pw.println(" mObbMounts:");
Kenny Root38cf8862010-09-26 14:18:51 -07002167
Kenny Rootaf9d6672010-10-08 09:21:39 -07002168 final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet().iterator();
2169 while (binders.hasNext()) {
2170 Entry<IBinder, List<ObbState>> e = binders.next();
2171 pw.print(" Key="); pw.println(e.getKey().toString());
2172 final List<ObbState> obbStates = e.getValue();
Kenny Root38cf8862010-09-26 14:18:51 -07002173 for (final ObbState obbState : obbStates) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002174 pw.print(" "); pw.println(obbState.toString());
Kenny Root38cf8862010-09-26 14:18:51 -07002175 }
2176 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002177
2178 pw.println("");
2179 pw.println(" mObbPathToStateMap:");
2180 final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator();
2181 while (maps.hasNext()) {
2182 final Entry<String, ObbState> e = maps.next();
2183 pw.print(" "); pw.print(e.getKey());
2184 pw.print(" -> "); pw.println(e.getValue().toString());
2185 }
Kenny Root38cf8862010-09-26 14:18:51 -07002186 }
2187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188}
2189