blob: d862585b09c5036ba903a32772b888f771e7203b [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 /*
Jason parks9ed98bc2011-01-17 09:58:35 -0600621 * Now that we've done our initialization, release
San Mehat207e5382010-02-04 20:46:54 -0800622 * 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 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001240
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 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001410
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 /*
Jason parks9ed98bc2011-01-17 09:58:35 -06001498 * Because a mounted container has active internal state which cannot be
San Mehat85451ee2010-02-24 08:54:18 -08001499 * 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 {
Jason parks9ed98bc2011-01-17 09:58:35 -06001647 ArrayList<String> rsp = mConnector.doCommand("cryptfs checkpw " + password);
1648 String []tok = rsp.get(0).split(" ");
1649
1650 if (tok == null || tok.length != 2) {
1651 return -1;
1652 }
1653
1654 int code = Integer.parseInt(tok[1]);
1655
1656 if (code == 0) {
1657 // Decrypt was successful. Post a delayed message before restarting in order
1658 // to let the UI to clear itself
1659 mHandler.postDelayed(new Runnable() {
1660 public void run() {
1661 mConnector.doCommand(String.format("cryptfs restart"));
1662 }
1663 }, 2000); // 2 seconds
1664 }
1665
1666 return code;
Jason parks5af0b912010-11-29 09:05:25 -06001667 } catch (NativeDaemonConnectorException e) {
1668 // Decryption failed
1669 return e.getCode();
1670 }
Jason parks5af0b912010-11-29 09:05:25 -06001671 }
1672
Jason parks56aa5322011-01-07 09:01:15 -06001673 public int encryptStorage(String password) {
1674 if (password == null) {
1675 throw new IllegalArgumentException("password cannot be null");
1676 }
1677
1678 // TODO: Enforce a permission
1679
1680 waitForReady();
1681
1682 if (DEBUG_EVENTS) {
1683 Slog.i(TAG, "decrypting storage...");
1684 }
1685
1686 try {
Jason parks9ed98bc2011-01-17 09:58:35 -06001687 mConnector.doCommand(String.format("cryptfs enablecrypto inplace %s", password));
Jason parks56aa5322011-01-07 09:01:15 -06001688 } catch (NativeDaemonConnectorException e) {
1689 // Encryption failed
1690 return e.getCode();
1691 }
1692
1693 return 0;
1694 }
1695
Kenny Rootaf9d6672010-10-08 09:21:39 -07001696 private void addObbStateLocked(ObbState obbState) throws RemoteException {
1697 final IBinder binder = obbState.getBinder();
1698 List<ObbState> obbStates = mObbMounts.get(binder);
Kenny Root5919ac62010-10-05 09:49:40 -07001699
Kenny Rootaf9d6672010-10-08 09:21:39 -07001700 if (obbStates == null) {
1701 obbStates = new ArrayList<ObbState>();
1702 mObbMounts.put(binder, obbStates);
1703 } else {
1704 for (final ObbState o : obbStates) {
1705 if (o.filename.equals(obbState.filename)) {
1706 throw new IllegalStateException("Attempt to add ObbState twice. "
1707 + "This indicates an error in the MountService logic.");
Kenny Root5919ac62010-10-05 09:49:40 -07001708 }
1709 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001710 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001711
1712 obbStates.add(obbState);
1713 try {
1714 obbState.link();
1715 } catch (RemoteException e) {
1716 /*
1717 * The binder died before we could link it, so clean up our state
1718 * and return failure.
1719 */
1720 obbStates.remove(obbState);
1721 if (obbStates.isEmpty()) {
1722 mObbMounts.remove(binder);
1723 }
1724
1725 // Rethrow the error so mountObb can get it
1726 throw e;
1727 }
1728
1729 mObbPathToStateMap.put(obbState.filename, obbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07001730 }
1731
Kenny Rootaf9d6672010-10-08 09:21:39 -07001732 private void removeObbStateLocked(ObbState obbState) {
1733 final IBinder binder = obbState.getBinder();
1734 final List<ObbState> obbStates = mObbMounts.get(binder);
1735 if (obbStates != null) {
1736 if (obbStates.remove(obbState)) {
1737 obbState.unlink();
Kenny Root05105f72010-09-22 17:29:43 -07001738 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001739 if (obbStates.isEmpty()) {
1740 mObbMounts.remove(binder);
1741 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001742 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001743
Kenny Rootaf9d6672010-10-08 09:21:39 -07001744 mObbPathToStateMap.remove(obbState.filename);
Kenny Root38cf8862010-09-26 14:18:51 -07001745 }
1746
Kenny Roota02b8b02010-08-05 16:14:17 -07001747 private class ObbActionHandler extends Handler {
1748 private boolean mBound = false;
Kenny Root480afe72010-10-07 10:17:50 -07001749 private final List<ObbAction> mActions = new LinkedList<ObbAction>();
Kenny Roota02b8b02010-08-05 16:14:17 -07001750
1751 ObbActionHandler(Looper l) {
1752 super(l);
1753 }
1754
1755 @Override
1756 public void handleMessage(Message msg) {
1757 switch (msg.what) {
1758 case OBB_RUN_ACTION: {
Kenny Root480afe72010-10-07 10:17:50 -07001759 final ObbAction action = (ObbAction) msg.obj;
Kenny Roota02b8b02010-08-05 16:14:17 -07001760
1761 if (DEBUG_OBB)
1762 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
1763
1764 // If a bind was already initiated we don't really
1765 // need to do anything. The pending install
1766 // will be processed later on.
1767 if (!mBound) {
1768 // If this is the only one pending we might
1769 // have to bind to the service again.
1770 if (!connectToService()) {
1771 Slog.e(TAG, "Failed to bind to media container service");
1772 action.handleError();
1773 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07001774 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001775 }
Kenny Root735de3b2010-09-30 14:11:39 -07001776
Kenny Root735de3b2010-09-30 14:11:39 -07001777 mActions.add(action);
Kenny Roota02b8b02010-08-05 16:14:17 -07001778 break;
1779 }
1780 case OBB_MCS_BOUND: {
1781 if (DEBUG_OBB)
1782 Slog.i(TAG, "OBB_MCS_BOUND");
1783 if (msg.obj != null) {
1784 mContainerService = (IMediaContainerService) msg.obj;
1785 }
1786 if (mContainerService == null) {
1787 // Something seriously wrong. Bail out
1788 Slog.e(TAG, "Cannot bind to media container service");
1789 for (ObbAction action : mActions) {
1790 // Indicate service bind error
1791 action.handleError();
1792 }
1793 mActions.clear();
1794 } else if (mActions.size() > 0) {
Kenny Root480afe72010-10-07 10:17:50 -07001795 final ObbAction action = mActions.get(0);
Kenny Roota02b8b02010-08-05 16:14:17 -07001796 if (action != null) {
1797 action.execute(this);
1798 }
1799 } else {
1800 // Should never happen ideally.
1801 Slog.w(TAG, "Empty queue");
1802 }
1803 break;
1804 }
1805 case OBB_MCS_RECONNECT: {
1806 if (DEBUG_OBB)
1807 Slog.i(TAG, "OBB_MCS_RECONNECT");
1808 if (mActions.size() > 0) {
1809 if (mBound) {
1810 disconnectService();
1811 }
1812 if (!connectToService()) {
1813 Slog.e(TAG, "Failed to bind to media container service");
1814 for (ObbAction action : mActions) {
1815 // Indicate service bind error
1816 action.handleError();
1817 }
1818 mActions.clear();
1819 }
1820 }
1821 break;
1822 }
1823 case OBB_MCS_UNBIND: {
1824 if (DEBUG_OBB)
1825 Slog.i(TAG, "OBB_MCS_UNBIND");
1826
1827 // Delete pending install
1828 if (mActions.size() > 0) {
1829 mActions.remove(0);
1830 }
1831 if (mActions.size() == 0) {
1832 if (mBound) {
1833 disconnectService();
1834 }
1835 } else {
1836 // There are more pending requests in queue.
1837 // Just post MCS_BOUND message to trigger processing
1838 // of next pending install.
1839 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1840 }
1841 break;
1842 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001843 case OBB_FLUSH_MOUNT_STATE: {
1844 final String path = (String) msg.obj;
1845
1846 if (DEBUG_OBB)
1847 Slog.i(TAG, "Flushing all OBB state for path " + path);
1848
1849 synchronized (mObbMounts) {
1850 final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>();
1851
1852 final Iterator<Entry<String, ObbState>> i =
1853 mObbPathToStateMap.entrySet().iterator();
1854 while (i.hasNext()) {
1855 final Entry<String, ObbState> obbEntry = i.next();
1856
1857 /*
1858 * If this entry's source file is in the volume path
1859 * that got unmounted, remove it because it's no
1860 * longer valid.
1861 */
1862 if (obbEntry.getKey().startsWith(path)) {
1863 obbStatesToRemove.add(obbEntry.getValue());
1864 }
1865 }
1866
1867 for (final ObbState obbState : obbStatesToRemove) {
1868 if (DEBUG_OBB)
1869 Slog.i(TAG, "Removing state for " + obbState.filename);
1870
1871 removeObbStateLocked(obbState);
1872
1873 try {
1874 obbState.token.onObbResult(obbState.filename, obbState.nonce,
1875 OnObbStateChangeListener.UNMOUNTED);
1876 } catch (RemoteException e) {
1877 Slog.i(TAG, "Couldn't send unmount notification for OBB: "
1878 + obbState.filename);
1879 }
1880 }
1881 }
1882 break;
1883 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001884 }
1885 }
1886
1887 private boolean connectToService() {
1888 if (DEBUG_OBB)
1889 Slog.i(TAG, "Trying to bind to DefaultContainerService");
1890
1891 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
1892 if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) {
1893 mBound = true;
1894 return true;
1895 }
1896 return false;
1897 }
1898
1899 private void disconnectService() {
1900 mContainerService = null;
1901 mBound = false;
1902 mContext.unbindService(mDefContainerConn);
1903 }
1904 }
1905
1906 abstract class ObbAction {
1907 private static final int MAX_RETRIES = 3;
1908 private int mRetries;
1909
1910 ObbState mObbState;
1911
1912 ObbAction(ObbState obbState) {
1913 mObbState = obbState;
1914 }
1915
1916 public void execute(ObbActionHandler handler) {
1917 try {
1918 if (DEBUG_OBB)
1919 Slog.i(TAG, "Starting to execute action: " + this.toString());
1920 mRetries++;
1921 if (mRetries > MAX_RETRIES) {
1922 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
Kenny Root480afe72010-10-07 10:17:50 -07001923 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001924 handleError();
1925 return;
1926 } else {
1927 handleExecute();
1928 if (DEBUG_OBB)
1929 Slog.i(TAG, "Posting install MCS_UNBIND");
1930 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1931 }
1932 } catch (RemoteException e) {
1933 if (DEBUG_OBB)
1934 Slog.i(TAG, "Posting install MCS_RECONNECT");
1935 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
1936 } catch (Exception e) {
1937 if (DEBUG_OBB)
1938 Slog.d(TAG, "Error handling OBB action", e);
1939 handleError();
Kenny Root17eb6fb2010-10-06 15:02:52 -07001940 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001941 }
1942 }
1943
Kenny Root05105f72010-09-22 17:29:43 -07001944 abstract void handleExecute() throws RemoteException, IOException;
Kenny Roota02b8b02010-08-05 16:14:17 -07001945 abstract void handleError();
Kenny Root38cf8862010-09-26 14:18:51 -07001946
1947 protected ObbInfo getObbInfo() throws IOException {
1948 ObbInfo obbInfo;
1949 try {
1950 obbInfo = mContainerService.getObbInfo(mObbState.filename);
1951 } catch (RemoteException e) {
1952 Slog.d(TAG, "Couldn't call DefaultContainerService to fetch OBB info for "
1953 + mObbState.filename);
1954 obbInfo = null;
1955 }
1956 if (obbInfo == null) {
1957 throw new IOException("Couldn't read OBB file: " + mObbState.filename);
1958 }
1959 return obbInfo;
1960 }
1961
Kenny Rootaf9d6672010-10-08 09:21:39 -07001962 protected void sendNewStatusOrIgnore(int status) {
1963 if (mObbState == null || mObbState.token == null) {
1964 return;
1965 }
1966
Kenny Root38cf8862010-09-26 14:18:51 -07001967 try {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001968 mObbState.token.onObbResult(mObbState.filename, mObbState.nonce, status);
Kenny Root38cf8862010-09-26 14:18:51 -07001969 } catch (RemoteException e) {
1970 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1971 }
1972 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001973 }
1974
1975 class MountObbAction extends ObbAction {
1976 private String mKey;
1977
1978 MountObbAction(ObbState obbState, String key) {
1979 super(obbState);
1980 mKey = key;
1981 }
1982
Jason parks5af0b912010-11-29 09:05:25 -06001983 @Override
Kenny Root735de3b2010-09-30 14:11:39 -07001984 public void handleExecute() throws IOException, RemoteException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001985 waitForReady();
1986 warnOnNotMounted();
1987
Kenny Root38cf8862010-09-26 14:18:51 -07001988 final ObbInfo obbInfo = getObbInfo();
1989
Kenny Roota02b8b02010-08-05 16:14:17 -07001990 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001991 Slog.w(TAG, "Denied attempt to mount OBB " + obbInfo.filename
1992 + " which is owned by " + obbInfo.packageName);
1993 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
1994 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07001995 }
1996
Kenny Rootaf9d6672010-10-08 09:21:39 -07001997 final boolean isMounted;
1998 synchronized (mObbMounts) {
1999 isMounted = mObbPathToStateMap.containsKey(obbInfo.filename);
2000 }
2001 if (isMounted) {
2002 Slog.w(TAG, "Attempt to mount OBB which is already mounted: " + obbInfo.filename);
2003 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_ALREADY_MOUNTED);
2004 return;
2005 }
2006
2007 /*
2008 * The filename passed in might not be the canonical name, so just
2009 * set the filename to the canonicalized version.
2010 */
2011 mObbState.filename = obbInfo.filename;
2012
2013 final String hashedKey;
2014 if (mKey == null) {
2015 hashedKey = "none";
2016 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002017 try {
Kenny Root3b1abba2010-10-13 15:00:07 -07002018 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
2019
2020 KeySpec ks = new PBEKeySpec(mKey.toCharArray(), obbInfo.salt,
2021 PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE);
2022 SecretKey key = factory.generateSecret(ks);
2023 BigInteger bi = new BigInteger(key.getEncoded());
2024 hashedKey = bi.toString(16);
Kenny Rootaf9d6672010-10-08 09:21:39 -07002025 } catch (NoSuchAlgorithmException e) {
Kenny Root3b1abba2010-10-13 15:00:07 -07002026 Slog.e(TAG, "Could not load PBKDF2 algorithm", e);
2027 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
2028 return;
2029 } catch (InvalidKeySpecException e) {
2030 Slog.e(TAG, "Invalid key spec when loading PBKDF2 algorithm", e);
2031 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root38cf8862010-09-26 14:18:51 -07002032 return;
2033 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002034 }
Kenny Root38cf8862010-09-26 14:18:51 -07002035
Kenny Rootaf9d6672010-10-08 09:21:39 -07002036 int rc = StorageResultCode.OperationSucceeded;
2037 String cmd = String.format("obb mount %s %s %d", mObbState.filename, hashedKey,
2038 mObbState.callerUid);
2039 try {
2040 mConnector.doCommand(cmd);
2041 } catch (NativeDaemonConnectorException e) {
2042 int code = e.getCode();
2043 if (code != VoldResponseCode.OpFailedStorageBusy) {
2044 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002045 }
2046 }
2047
Kenny Rootaf9d6672010-10-08 09:21:39 -07002048 if (rc == StorageResultCode.OperationSucceeded) {
2049 if (DEBUG_OBB)
2050 Slog.d(TAG, "Successfully mounted OBB " + mObbState.filename);
2051
2052 synchronized (mObbMounts) {
2053 addObbStateLocked(mObbState);
2054 }
2055
2056 sendNewStatusOrIgnore(OnObbStateChangeListener.MOUNTED);
Kenny Root02c87302010-07-01 08:10:18 -07002057 } else {
Kenny Root05105f72010-09-22 17:29:43 -07002058 Slog.e(TAG, "Couldn't mount OBB file: " + rc);
Kenny Roota02b8b02010-08-05 16:14:17 -07002059
Kenny Rootaf9d6672010-10-08 09:21:39 -07002060 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT);
Kenny Root02c87302010-07-01 08:10:18 -07002061 }
2062 }
2063
Jason parks5af0b912010-11-29 09:05:25 -06002064 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002065 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002066 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root02c87302010-07-01 08:10:18 -07002067 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002068
2069 @Override
2070 public String toString() {
2071 StringBuilder sb = new StringBuilder();
2072 sb.append("MountObbAction{");
2073 sb.append("filename=");
2074 sb.append(mObbState.filename);
2075 sb.append(",callerUid=");
2076 sb.append(mObbState.callerUid);
2077 sb.append(",token=");
2078 sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002079 sb.append(",binder=");
2080 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002081 sb.append('}');
2082 return sb.toString();
2083 }
2084 }
2085
2086 class UnmountObbAction extends ObbAction {
2087 private boolean mForceUnmount;
2088
2089 UnmountObbAction(ObbState obbState, boolean force) {
2090 super(obbState);
2091 mForceUnmount = force;
2092 }
2093
Jason parks5af0b912010-11-29 09:05:25 -06002094 @Override
Kenny Root38cf8862010-09-26 14:18:51 -07002095 public void handleExecute() throws IOException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002096 waitForReady();
2097 warnOnNotMounted();
2098
Kenny Root38cf8862010-09-26 14:18:51 -07002099 final ObbInfo obbInfo = getObbInfo();
Kenny Roota02b8b02010-08-05 16:14:17 -07002100
Kenny Rootaf9d6672010-10-08 09:21:39 -07002101 final ObbState obbState;
Kenny Root38cf8862010-09-26 14:18:51 -07002102 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002103 obbState = mObbPathToStateMap.get(obbInfo.filename);
2104 }
Kenny Root38cf8862010-09-26 14:18:51 -07002105
Kenny Rootaf9d6672010-10-08 09:21:39 -07002106 if (obbState == null) {
2107 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_NOT_MOUNTED);
2108 return;
2109 }
2110
2111 if (obbState.callerUid != mObbState.callerUid) {
2112 Slog.w(TAG, "Permission denied attempting to unmount OBB " + obbInfo.filename
2113 + " (owned by " + obbInfo.packageName + ")");
2114 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2115 return;
2116 }
2117
2118 mObbState.filename = obbInfo.filename;
2119
2120 int rc = StorageResultCode.OperationSucceeded;
2121 String cmd = String.format("obb unmount %s%s", mObbState.filename,
2122 (mForceUnmount ? " force" : ""));
2123 try {
2124 mConnector.doCommand(cmd);
2125 } catch (NativeDaemonConnectorException e) {
2126 int code = e.getCode();
2127 if (code == VoldResponseCode.OpFailedStorageBusy) {
2128 rc = StorageResultCode.OperationFailedStorageBusy;
2129 } else if (code == VoldResponseCode.OpFailedStorageNotFound) {
2130 // If it's not mounted then we've already won.
2131 rc = StorageResultCode.OperationSucceeded;
2132 } else {
2133 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002134 }
2135 }
2136
Kenny Rootaf9d6672010-10-08 09:21:39 -07002137 if (rc == StorageResultCode.OperationSucceeded) {
2138 synchronized (mObbMounts) {
2139 removeObbStateLocked(obbState);
Kenny Root38cf8862010-09-26 14:18:51 -07002140 }
2141
Kenny Rootaf9d6672010-10-08 09:21:39 -07002142 sendNewStatusOrIgnore(OnObbStateChangeListener.UNMOUNTED);
Kenny Roota02b8b02010-08-05 16:14:17 -07002143 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002144 Slog.w(TAG, "Could not mount OBB: " + mObbState.filename);
2145 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT);
Kenny Roota02b8b02010-08-05 16:14:17 -07002146 }
2147 }
2148
Jason parks5af0b912010-11-29 09:05:25 -06002149 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002150 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002151 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Roota02b8b02010-08-05 16:14:17 -07002152 }
2153
2154 @Override
2155 public String toString() {
2156 StringBuilder sb = new StringBuilder();
2157 sb.append("UnmountObbAction{");
2158 sb.append("filename=");
2159 sb.append(mObbState.filename != null ? mObbState.filename : "null");
2160 sb.append(",force=");
2161 sb.append(mForceUnmount);
2162 sb.append(",callerUid=");
2163 sb.append(mObbState.callerUid);
2164 sb.append(",token=");
2165 sb.append(mObbState.token != null ? mObbState.token.toString() : "null");
Kenny Root735de3b2010-09-30 14:11:39 -07002166 sb.append(",binder=");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002167 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002168 sb.append('}');
2169 return sb.toString();
2170 }
Kenny Root02c87302010-07-01 08:10:18 -07002171 }
Kenny Root38cf8862010-09-26 14:18:51 -07002172
2173 @Override
2174 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2175 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
2176 pw.println("Permission Denial: can't dump ActivityManager from from pid="
2177 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
2178 + " without permission " + android.Manifest.permission.DUMP);
2179 return;
2180 }
2181
Kenny Root38cf8862010-09-26 14:18:51 -07002182 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002183 pw.println(" mObbMounts:");
Kenny Root38cf8862010-09-26 14:18:51 -07002184
Kenny Rootaf9d6672010-10-08 09:21:39 -07002185 final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet().iterator();
2186 while (binders.hasNext()) {
2187 Entry<IBinder, List<ObbState>> e = binders.next();
2188 pw.print(" Key="); pw.println(e.getKey().toString());
2189 final List<ObbState> obbStates = e.getValue();
Kenny Root38cf8862010-09-26 14:18:51 -07002190 for (final ObbState obbState : obbStates) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002191 pw.print(" "); pw.println(obbState.toString());
Kenny Root38cf8862010-09-26 14:18:51 -07002192 }
2193 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002194
2195 pw.println("");
2196 pw.println(" mObbPathToStateMap:");
2197 final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator();
2198 while (maps.hasNext()) {
2199 final Entry<String, ObbState> e = maps.next();
2200 pw.print(" "); pw.print(e.getKey());
2201 pw.print(" -> "); pw.println(e.getValue().toString());
2202 }
Kenny Root38cf8862010-09-26 14:18:51 -07002203 }
2204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205}
2206