blob: a91b6906822f420ea4875a7bf51b00daf90a8f31 [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
Jason parks8888c592011-01-20 22:46:41 -060022import android.Manifest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.BroadcastReceiver;
Kenny Roota02b8b02010-08-05 16:14:17 -070024import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
Kenny Roota02b8b02010-08-05 16:14:17 -070028import android.content.ServiceConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.pm.PackageManager;
Kenny Root02c87302010-07-01 08:10:18 -070030import android.content.res.ObbInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.net.Uri;
Kenny Root02c87302010-07-01 08:10:18 -070032import android.os.Binder;
Kenny Roota02b8b02010-08-05 16:14:17 -070033import android.os.Environment;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080034import android.os.Handler;
Daniel Sandler5f27ef42010-03-16 15:42:02 -040035import android.os.HandlerThread;
Kenny Roota02b8b02010-08-05 16:14:17 -070036import android.os.IBinder;
Daniel Sandler5f27ef42010-03-16 15:42:02 -040037import android.os.Looper;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080038import android.os.Message;
San Mehat4270e1e2010-01-29 05:32:19 -080039import android.os.RemoteException;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -080040import android.os.ServiceManager;
San Mehat207e5382010-02-04 20:46:54 -080041import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.SystemProperties;
Kenny Roota02b8b02010-08-05 16:14:17 -070043import android.os.storage.IMountService;
44import android.os.storage.IMountServiceListener;
45import android.os.storage.IMountShutdownObserver;
46import android.os.storage.IObbActionListener;
Kenny Rootaf9d6672010-10-08 09:21:39 -070047import android.os.storage.OnObbStateChangeListener;
Kenny Roota02b8b02010-08-05 16:14:17 -070048import android.os.storage.StorageResultCode;
Jason parksf7b3cd42011-01-27 09:28:25 -060049import android.text.TextUtils;
San Mehata5078592010-03-25 09:36:54 -070050import android.util.Slog;
Kenny Roota02b8b02010-08-05 16:14:17 -070051
Kenny Root38cf8862010-09-26 14:18:51 -070052import java.io.FileDescriptor;
Kenny Root05105f72010-09-22 17:29:43 -070053import java.io.IOException;
Kenny Root38cf8862010-09-26 14:18:51 -070054import java.io.PrintWriter;
Kenny Root3b1abba2010-10-13 15:00:07 -070055import java.math.BigInteger;
Kenny Root735de3b2010-09-30 14:11:39 -070056import java.security.NoSuchAlgorithmException;
Kenny Root3b1abba2010-10-13 15:00:07 -070057import java.security.spec.InvalidKeySpecException;
58import java.security.spec.KeySpec;
San Mehat22dd86e2010-01-12 12:21:18 -080059import java.util.ArrayList;
Kenny Roota02b8b02010-08-05 16:14:17 -070060import java.util.HashMap;
San Mehat6cdd9c02010-02-09 14:45:20 -080061import java.util.HashSet;
Kenny Root38cf8862010-09-26 14:18:51 -070062import java.util.Iterator;
Kenny Roota02b8b02010-08-05 16:14:17 -070063import java.util.LinkedList;
64import java.util.List;
65import java.util.Map;
Kenny Root38cf8862010-09-26 14:18:51 -070066import java.util.Map.Entry;
Mike Lockwoodd967f462011-03-24 08:12:30 -070067import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
Kenny Root3b1abba2010-10-13 15:00:07 -070069import javax.crypto.SecretKey;
70import javax.crypto.SecretKeyFactory;
71import javax.crypto.spec.PBEKeySpec;
72
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073/**
San Mehatb1043402010-02-05 08:26:50 -080074 * MountService implements back-end services for platform storage
75 * management.
76 * @hide - Applications should use android.os.storage.StorageManager
77 * to access the MountService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 */
Jason parks5af0b912010-11-29 09:05:25 -060079class MountService extends IMountService.Stub implements INativeDaemonConnectorCallbacks {
80
San Mehatb1043402010-02-05 08:26:50 -080081 private static final boolean LOCAL_LOGD = false;
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -080082 private static final boolean DEBUG_UNMOUNT = false;
83 private static final boolean DEBUG_EVENTS = false;
Kenny Rootb7db2722011-01-25 16:39:35 -080084 private static final boolean DEBUG_OBB = false;
Kenny Root02c87302010-07-01 08:10:18 -070085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 private static final String TAG = "MountService";
87
Kenny Root305bcbf2010-09-03 07:56:38 -070088 private static final String VOLD_TAG = "VoldConnector";
89
San Mehat4270e1e2010-01-29 05:32:19 -080090 /*
91 * Internal vold volume state constants
92 */
San Mehat7fd0fee2009-12-17 07:12:23 -080093 class VolumeState {
94 public static final int Init = -1;
95 public static final int NoMedia = 0;
96 public static final int Idle = 1;
97 public static final int Pending = 2;
98 public static final int Checking = 3;
99 public static final int Mounted = 4;
100 public static final int Unmounting = 5;
101 public static final int Formatting = 6;
102 public static final int Shared = 7;
103 public static final int SharedMnt = 8;
104 }
105
San Mehat4270e1e2010-01-29 05:32:19 -0800106 /*
107 * Internal vold response code constants
108 */
San Mehat22dd86e2010-01-12 12:21:18 -0800109 class VoldResponseCode {
San Mehat4270e1e2010-01-29 05:32:19 -0800110 /*
111 * 100 series - Requestion action was initiated; expect another reply
112 * before proceeding with a new command.
113 */
San Mehat22dd86e2010-01-12 12:21:18 -0800114 public static final int VolumeListResult = 110;
115 public static final int AsecListResult = 111;
San Mehatc1b4ce92010-02-16 17:13:03 -0800116 public static final int StorageUsersListResult = 112;
San Mehat22dd86e2010-01-12 12:21:18 -0800117
San Mehat4270e1e2010-01-29 05:32:19 -0800118 /*
119 * 200 series - Requestion action has been successfully completed.
120 */
121 public static final int ShareStatusResult = 210;
San Mehat22dd86e2010-01-12 12:21:18 -0800122 public static final int AsecPathResult = 211;
San Mehat4270e1e2010-01-29 05:32:19 -0800123 public static final int ShareEnabledResult = 212;
San Mehat22dd86e2010-01-12 12:21:18 -0800124
San Mehat4270e1e2010-01-29 05:32:19 -0800125 /*
126 * 400 series - Command was accepted, but the requested action
127 * did not take place.
128 */
129 public static final int OpFailedNoMedia = 401;
130 public static final int OpFailedMediaBlank = 402;
131 public static final int OpFailedMediaCorrupt = 403;
132 public static final int OpFailedVolNotMounted = 404;
San Mehatd9709982010-02-18 11:43:03 -0800133 public static final int OpFailedStorageBusy = 405;
San Mehat2d66cef2010-03-23 11:12:52 -0700134 public static final int OpFailedStorageNotFound = 406;
San Mehat4270e1e2010-01-29 05:32:19 -0800135
136 /*
137 * 600 series - Unsolicited broadcasts.
138 */
San Mehat22dd86e2010-01-12 12:21:18 -0800139 public static final int VolumeStateChange = 605;
San Mehat22dd86e2010-01-12 12:21:18 -0800140 public static final int ShareAvailabilityChange = 620;
141 public static final int VolumeDiskInserted = 630;
142 public static final int VolumeDiskRemoved = 631;
143 public static final int VolumeBadRemoval = 632;
144 }
145
San Mehat4270e1e2010-01-29 05:32:19 -0800146 private Context mContext;
147 private NativeDaemonConnector mConnector;
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400148 private final HashMap<String, String> mVolumeStates = new HashMap<String, String>();
149 private String mExternalStoragePath;
San Mehat4270e1e2010-01-29 05:32:19 -0800150 private PackageManagerService mPms;
151 private boolean mUmsEnabling;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800152 // Used as a lock for methods that register/unregister listeners.
153 final private ArrayList<MountServiceBinderListener> mListeners =
154 new ArrayList<MountServiceBinderListener>();
San Mehat6a965af22010-02-24 17:47:30 -0800155 private boolean mBooted = false;
156 private boolean mReady = false;
157 private boolean mSendUmsConnectedOnBoot = false;
Mike Lockwood03559752010-07-19 18:25:03 -0400158 // true if we should fake MEDIA_MOUNTED state for external storage
159 private boolean mEmulateExternalStorage = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800160
San Mehat6cdd9c02010-02-09 14:45:20 -0800161 /**
162 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800163 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800164 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800165 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800166
Kenny Root02c87302010-07-01 08:10:18 -0700167 /**
Kenny Root3b1abba2010-10-13 15:00:07 -0700168 * The size of the crypto algorithm key in bits for OBB files. Currently
169 * Twofish is used which takes 128-bit keys.
170 */
171 private static final int CRYPTO_ALGORITHM_KEY_SIZE = 128;
172
173 /**
174 * The number of times to run SHA1 in the PBKDF2 function for OBB files.
175 * 1024 is reasonably secure and not too slow.
176 */
177 private static final int PBKDF2_HASH_ROUNDS = 1024;
178
179 /**
Kenny Roota02b8b02010-08-05 16:14:17 -0700180 * Mounted OBB tracking information. Used to track the current state of all
181 * OBBs.
Kenny Root02c87302010-07-01 08:10:18 -0700182 */
Kenny Root735de3b2010-09-30 14:11:39 -0700183 final private Map<IBinder, List<ObbState>> mObbMounts = new HashMap<IBinder, List<ObbState>>();
Kenny Roota02b8b02010-08-05 16:14:17 -0700184 final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>();
185
186 class ObbState implements IBinder.DeathRecipient {
Kenny Rootaf9d6672010-10-08 09:21:39 -0700187 public ObbState(String filename, int callerUid, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -0700188 throws RemoteException {
Kenny Roota02b8b02010-08-05 16:14:17 -0700189 this.filename = filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700190 this.callerUid = callerUid;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700191 this.token = token;
192 this.nonce = nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700193 }
194
195 // OBB source filename
Kenny Rootaf9d6672010-10-08 09:21:39 -0700196 String filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700197
198 // Binder.callingUid()
Kenny Root05105f72010-09-22 17:29:43 -0700199 final public int callerUid;
Kenny Roota02b8b02010-08-05 16:14:17 -0700200
Kenny Rootaf9d6672010-10-08 09:21:39 -0700201 // Token of remote Binder caller
202 final IObbActionListener token;
203
204 // Identifier to pass back to the token
205 final int nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700206
Kenny Root735de3b2010-09-30 14:11:39 -0700207 public IBinder getBinder() {
208 return token.asBinder();
209 }
210
Kenny Roota02b8b02010-08-05 16:14:17 -0700211 @Override
212 public void binderDied() {
213 ObbAction action = new UnmountObbAction(this, true);
214 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root735de3b2010-09-30 14:11:39 -0700215 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700216
Kenny Root5919ac62010-10-05 09:49:40 -0700217 public void link() throws RemoteException {
218 getBinder().linkToDeath(this, 0);
219 }
220
221 public void unlink() {
Kenny Root735de3b2010-09-30 14:11:39 -0700222 getBinder().unlinkToDeath(this, 0);
Kenny Roota02b8b02010-08-05 16:14:17 -0700223 }
Kenny Root38cf8862010-09-26 14:18:51 -0700224
225 @Override
226 public String toString() {
227 StringBuilder sb = new StringBuilder("ObbState{");
228 sb.append("filename=");
229 sb.append(filename);
230 sb.append(",token=");
231 sb.append(token.toString());
232 sb.append(",callerUid=");
233 sb.append(callerUid);
Kenny Root38cf8862010-09-26 14:18:51 -0700234 sb.append('}');
235 return sb.toString();
236 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700237 }
238
239 // OBB Action Handler
240 final private ObbActionHandler mObbActionHandler;
241
242 // OBB action handler messages
243 private static final int OBB_RUN_ACTION = 1;
244 private static final int OBB_MCS_BOUND = 2;
245 private static final int OBB_MCS_UNBIND = 3;
246 private static final int OBB_MCS_RECONNECT = 4;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700247 private static final int OBB_FLUSH_MOUNT_STATE = 5;
Kenny Roota02b8b02010-08-05 16:14:17 -0700248
249 /*
250 * Default Container Service information
251 */
252 static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
253 "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService");
254
255 final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection();
256
257 class DefaultContainerConnection implements ServiceConnection {
258 public void onServiceConnected(ComponentName name, IBinder service) {
259 if (DEBUG_OBB)
260 Slog.i(TAG, "onServiceConnected");
261 IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service);
262 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs));
263 }
264
265 public void onServiceDisconnected(ComponentName name) {
266 if (DEBUG_OBB)
267 Slog.i(TAG, "onServiceDisconnected");
268 }
269 };
270
271 // Used in the ObbActionHandler
272 private IMediaContainerService mContainerService = null;
Kenny Root02c87302010-07-01 08:10:18 -0700273
274 // Handler messages
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800275 private static final int H_UNMOUNT_PM_UPDATE = 1;
276 private static final int H_UNMOUNT_PM_DONE = 2;
277 private static final int H_UNMOUNT_MS = 3;
278 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
279 private static final int MAX_UNMOUNT_RETRIES = 4;
280
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800281 class UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700282 final String path;
283 final boolean force;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800284 int retries;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800285
286 UnmountCallBack(String path, boolean force) {
287 retries = 0;
288 this.path = path;
289 this.force = force;
290 }
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800291
292 void handleFinished() {
San Mehata5078592010-03-25 09:36:54 -0700293 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path);
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800294 doUnmountVolume(path, true);
295 }
296 }
297
298 class UmsEnableCallBack extends UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700299 final String method;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800300
301 UmsEnableCallBack(String path, String method, boolean force) {
302 super(path, force);
303 this.method = method;
304 }
305
306 @Override
307 void handleFinished() {
308 super.handleFinished();
309 doShareUnshareVolume(path, method, true);
310 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800311 }
312
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800313 class ShutdownCallBack extends UnmountCallBack {
314 IMountShutdownObserver observer;
315 ShutdownCallBack(String path, IMountShutdownObserver observer) {
316 super(path, true);
317 this.observer = observer;
318 }
319
320 @Override
321 void handleFinished() {
322 int ret = doUnmountVolume(path, true);
323 if (observer != null) {
324 try {
325 observer.onShutDownComplete(ret);
326 } catch (RemoteException e) {
San Mehata5078592010-03-25 09:36:54 -0700327 Slog.w(TAG, "RemoteException when shutting down");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800328 }
329 }
330 }
331 }
332
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400333 class MountServiceHandler extends Handler {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800334 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700335 boolean mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800336
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400337 MountServiceHandler(Looper l) {
338 super(l);
339 }
340
Jason parks5af0b912010-11-29 09:05:25 -0600341 @Override
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800342 public void handleMessage(Message msg) {
343 switch (msg.what) {
344 case H_UNMOUNT_PM_UPDATE: {
San Mehata5078592010-03-25 09:36:54 -0700345 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800346 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
347 mForceUnmounts.add(ucb);
San Mehata5078592010-03-25 09:36:54 -0700348 if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800349 // Register only if needed.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700350 if (!mUpdatingStatus) {
San Mehata5078592010-03-25 09:36:54 -0700351 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700352 mUpdatingStatus = true;
353 mPms.updateExternalMediaStatus(false, true);
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800354 }
355 break;
356 }
357 case H_UNMOUNT_PM_DONE: {
San Mehata5078592010-03-25 09:36:54 -0700358 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE");
San Mehata5078592010-03-25 09:36:54 -0700359 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700360 mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800361 int size = mForceUnmounts.size();
362 int sizeArr[] = new int[size];
363 int sizeArrN = 0;
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700364 // Kill processes holding references first
365 ActivityManagerService ams = (ActivityManagerService)
366 ServiceManager.getService("activity");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800367 for (int i = 0; i < size; i++) {
368 UnmountCallBack ucb = mForceUnmounts.get(i);
369 String path = ucb.path;
370 boolean done = false;
371 if (!ucb.force) {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800372 done = true;
373 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800374 int pids[] = getStorageUsers(path);
375 if (pids == null || pids.length == 0) {
376 done = true;
377 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800378 // Eliminate system process here?
Dianne Hackborn64825172011-03-02 21:32:58 -0800379 ams.killPids(pids, "unmount media", true);
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700380 // Confirm if file references have been freed.
381 pids = getStorageUsers(path);
382 if (pids == null || pids.length == 0) {
383 done = true;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800384 }
385 }
386 }
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700387 if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) {
388 // Retry again
389 Slog.i(TAG, "Retrying to kill storage users again");
390 mHandler.sendMessageDelayed(
391 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
392 ucb.retries++),
393 RETRY_UNMOUNT_DELAY);
394 } else {
395 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
396 Slog.i(TAG, "Failed to unmount media inspite of " +
397 MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now");
398 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800399 sizeArr[sizeArrN++] = i;
400 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
401 ucb));
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800402 }
403 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800404 // Remove already processed elements from list.
405 for (int i = (sizeArrN-1); i >= 0; i--) {
406 mForceUnmounts.remove(sizeArr[i]);
407 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800408 break;
409 }
410 case H_UNMOUNT_MS : {
San Mehata5078592010-03-25 09:36:54 -0700411 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800412 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800413 ucb.handleFinished();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800414 break;
415 }
416 }
417 }
418 };
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400419 final private HandlerThread mHandlerThread;
420 final private Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800421
San Mehat207e5382010-02-04 20:46:54 -0800422 private void waitForReady() {
423 while (mReady == false) {
424 for (int retries = 5; retries > 0; retries--) {
425 if (mReady) {
426 return;
427 }
428 SystemClock.sleep(1000);
429 }
San Mehata5078592010-03-25 09:36:54 -0700430 Slog.w(TAG, "Waiting too long for mReady!");
San Mehat207e5382010-02-04 20:46:54 -0800431 }
San Mehat1f6301e2010-01-07 22:40:27 -0800432 }
Kenny Root02c87302010-07-01 08:10:18 -0700433
San Mehat207e5382010-02-04 20:46:54 -0800434 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Jason parks5af0b912010-11-29 09:05:25 -0600435 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 public void onReceive(Context context, Intent intent) {
San Mehat91c77612010-01-07 10:39:41 -0800437 String action = intent.getAction();
438
439 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
San Mehat207e5382010-02-04 20:46:54 -0800440 mBooted = true;
San Mehat22dd86e2010-01-12 12:21:18 -0800441
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800442 /*
443 * In the simulator, we need to broadcast a volume mounted event
444 * to make the media scanner run.
445 */
446 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
447 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted);
448 return;
449 }
San Mehatfafb0412010-02-18 19:40:04 -0800450 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600451 @Override
San Mehatfafb0412010-02-18 19:40:04 -0800452 public void run() {
453 try {
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400454 synchronized (mVolumeStates) {
455 for (String path : mVolumeStates.keySet()) {
456 String state = mVolumeStates.get(path);
San Mehat6a254402010-03-22 10:21:00 -0700457
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400458 if (state.equals(Environment.MEDIA_UNMOUNTED)) {
459 int rc = doMountVolume(path);
460 if (rc != StorageResultCode.OperationSucceeded) {
461 Slog.e(TAG, String.format("Boot-time mount failed (%d)",
462 rc));
463 }
464 } else if (state.equals(Environment.MEDIA_SHARED)) {
465 /*
466 * Bootstrap UMS enabled state since vold indicates
467 * the volume is shared (runtime restart while ums enabled)
468 */
469 notifyVolumeStateChange(null, path, VolumeState.NoMedia,
470 VolumeState.Shared);
471 }
San Mehatfafb0412010-02-18 19:40:04 -0800472 }
473 }
San Mehat6a254402010-03-22 10:21:00 -0700474
San Mehat6a965af22010-02-24 17:47:30 -0800475 /*
San Mehat6a254402010-03-22 10:21:00 -0700476 * If UMS was connected on boot, send the connected event
San Mehat6a965af22010-02-24 17:47:30 -0800477 * now that we're up.
478 */
479 if (mSendUmsConnectedOnBoot) {
480 sendUmsIntent(true);
481 mSendUmsConnectedOnBoot = false;
482 }
San Mehatfafb0412010-02-18 19:40:04 -0800483 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700484 Slog.e(TAG, "Boot-time mount exception", ex);
San Mehatfafb0412010-02-18 19:40:04 -0800485 }
San Mehat207e5382010-02-04 20:46:54 -0800486 }
San Mehatfafb0412010-02-18 19:40:04 -0800487 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 }
489 }
490 };
491
San Mehat4270e1e2010-01-29 05:32:19 -0800492 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
493 final IMountServiceListener mListener;
494
495 MountServiceBinderListener(IMountServiceListener listener) {
496 mListener = listener;
Kenny Root02c87302010-07-01 08:10:18 -0700497
San Mehat91c77612010-01-07 10:39:41 -0800498 }
499
San Mehat4270e1e2010-01-29 05:32:19 -0800500 public void binderDied() {
San Mehata5078592010-03-25 09:36:54 -0700501 if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!");
Kenny Roota02b8b02010-08-05 16:14:17 -0700502 synchronized (mListeners) {
San Mehat4270e1e2010-01-29 05:32:19 -0800503 mListeners.remove(this);
504 mListener.asBinder().unlinkToDeath(this, 0);
505 }
506 }
507 }
508
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800509 private void doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800510 // TODO: Add support for multiple share methods
511 if (!method.equals("ums")) {
512 throw new IllegalArgumentException(String.format("Method %s not supported", method));
513 }
514
San Mehat4270e1e2010-01-29 05:32:19 -0800515 try {
516 mConnector.doCommand(String.format(
517 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
518 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -0700519 Slog.e(TAG, "Failed to share/unshare", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800520 }
San Mehat4270e1e2010-01-29 05:32:19 -0800521 }
522
San Mehat207e5382010-02-04 20:46:54 -0800523 private void updatePublicVolumeState(String path, String state) {
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400524 String oldState;
525 synchronized(mVolumeStates) {
526 oldState = mVolumeStates.put(path, state);
527 }
528 if (state.equals(oldState)) {
529 Slog.w(TAG, String.format("Duplicate state transition (%s -> %s) for %s",
530 state, state, path));
San Mehat4270e1e2010-01-29 05:32:19 -0800531 return;
532 }
San Mehatb1043402010-02-05 08:26:50 -0800533
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400534 Slog.d(TAG, "volume state changed for " + path + " (" + oldState + " -> " + state + ")");
Kenny Rootaf9d6672010-10-08 09:21:39 -0700535
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400536 if (path.equals(mExternalStoragePath)) {
537 // Update state on PackageManager, but only of real events
538 if (!mEmulateExternalStorage) {
539 if (Environment.MEDIA_UNMOUNTED.equals(state)) {
540 mPms.updateExternalMediaStatus(false, false);
541
542 /*
543 * Some OBBs might have been unmounted when this volume was
544 * unmounted, so send a message to the handler to let it know to
545 * remove those from the list of mounted OBBS.
546 */
547 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(
548 OBB_FLUSH_MOUNT_STATE, path));
549 } else if (Environment.MEDIA_MOUNTED.equals(state)) {
550 mPms.updateExternalMediaStatus(true, false);
551 }
Mike Lockwood03559752010-07-19 18:25:03 -0400552 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800553 }
San Mehat4270e1e2010-01-29 05:32:19 -0800554 synchronized (mListeners) {
555 for (int i = mListeners.size() -1; i >= 0; i--) {
556 MountServiceBinderListener bl = mListeners.get(i);
557 try {
San Mehatb1043402010-02-05 08:26:50 -0800558 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800559 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700560 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800561 mListeners.remove(i);
562 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700563 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800564 }
565 }
566 }
567 }
568
569 /**
570 *
571 * Callback from NativeDaemonConnector
572 */
573 public void onDaemonConnected() {
574 /*
575 * Since we'll be calling back into the NativeDaemonConnector,
576 * we need to do our work in a new thread.
577 */
578 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600579 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800580 public void run() {
581 /**
582 * Determine media state and UMS detection status
583 */
San Mehat4270e1e2010-01-29 05:32:19 -0800584 try {
585 String[] vols = mConnector.doListCommand(
586 "volume list", VoldResponseCode.VolumeListResult);
587 for (String volstr : vols) {
588 String[] tok = volstr.split(" ");
589 // FMT: <label> <mountpoint> <state>
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400590 String path = tok[1];
591 String state = Environment.MEDIA_REMOVED;
592
San Mehat4270e1e2010-01-29 05:32:19 -0800593 int st = Integer.parseInt(tok[2]);
594 if (st == VolumeState.NoMedia) {
595 state = Environment.MEDIA_REMOVED;
596 } else if (st == VolumeState.Idle) {
San Mehat207e5382010-02-04 20:46:54 -0800597 state = Environment.MEDIA_UNMOUNTED;
San Mehat4270e1e2010-01-29 05:32:19 -0800598 } else if (st == VolumeState.Mounted) {
599 state = Environment.MEDIA_MOUNTED;
San Mehata5078592010-03-25 09:36:54 -0700600 Slog.i(TAG, "Media already mounted on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800601 } else if (st == VolumeState.Shared) {
602 state = Environment.MEDIA_SHARED;
San Mehata5078592010-03-25 09:36:54 -0700603 Slog.i(TAG, "Media shared on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800604 } else {
605 throw new Exception(String.format("Unexpected state %d", st));
606 }
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400607
608 if (state != null) {
609 if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
610 updatePublicVolumeState(path, state);
611 }
San Mehat4270e1e2010-01-29 05:32:19 -0800612 }
613 } catch (Exception e) {
San Mehata5078592010-03-25 09:36:54 -0700614 Slog.e(TAG, "Error processing initial volume state", e);
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400615 updatePublicVolumeState(mExternalStoragePath, Environment.MEDIA_REMOVED);
San Mehat4270e1e2010-01-29 05:32:19 -0800616 }
617
618 try {
San Mehat207e5382010-02-04 20:46:54 -0800619 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800620 notifyShareAvailabilityChange("ums", avail);
621 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700622 Slog.w(TAG, "Failed to get share availability");
San Mehat4270e1e2010-01-29 05:32:19 -0800623 }
San Mehat207e5382010-02-04 20:46:54 -0800624 /*
Jason parks9ed98bc2011-01-17 09:58:35 -0600625 * Now that we've done our initialization, release
San Mehat207e5382010-02-04 20:46:54 -0800626 * the hounds!
627 */
628 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800629 }
630 }.start();
631 }
632
633 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800634 * Callback from NativeDaemonConnector
635 */
636 public boolean onEvent(int code, String raw, String[] cooked) {
637 Intent in = null;
638
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800639 if (DEBUG_EVENTS) {
640 StringBuilder builder = new StringBuilder();
641 builder.append("onEvent::");
642 builder.append(" raw= " + raw);
643 if (cooked != null) {
644 builder.append(" cooked = " );
645 for (String str : cooked) {
646 builder.append(" " + str);
647 }
648 }
San Mehata5078592010-03-25 09:36:54 -0700649 Slog.i(TAG, builder.toString());
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800650 }
San Mehat4270e1e2010-01-29 05:32:19 -0800651 if (code == VoldResponseCode.VolumeStateChange) {
652 /*
653 * One of the volumes we're managing has changed state.
654 * Format: "NNN Volume <label> <path> state changed
655 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
656 */
657 notifyVolumeStateChange(
658 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
659 Integer.parseInt(cooked[10]));
660 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
661 // FMT: NNN Share method <method> now <available|unavailable>
662 boolean avail = false;
663 if (cooked[5].equals("available")) {
664 avail = true;
665 }
666 notifyShareAvailabilityChange(cooked[3], avail);
667 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
668 (code == VoldResponseCode.VolumeDiskRemoved) ||
669 (code == VoldResponseCode.VolumeBadRemoval)) {
670 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
671 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
672 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
673 final String label = cooked[2];
674 final String path = cooked[3];
675 int major = -1;
676 int minor = -1;
677
678 try {
679 String devComp = cooked[6].substring(1, cooked[6].length() -1);
680 String[] devTok = devComp.split(":");
681 major = Integer.parseInt(devTok[0]);
682 minor = Integer.parseInt(devTok[1]);
683 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700684 Slog.e(TAG, "Failed to parse major/minor", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800685 }
686
San Mehat4270e1e2010-01-29 05:32:19 -0800687 if (code == VoldResponseCode.VolumeDiskInserted) {
688 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600689 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800690 public void run() {
691 try {
692 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800693 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700694 Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
San Mehat4270e1e2010-01-29 05:32:19 -0800695 }
696 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700697 Slog.w(TAG, "Failed to mount media on insertion", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800698 }
699 }
700 }.start();
701 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
702 /*
703 * This event gets trumped if we're already in BAD_REMOVAL state
704 */
705 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
706 return true;
707 }
708 /* Send the media unmounted event first */
San Mehata5078592010-03-25 09:36:54 -0700709 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800710 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
711 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
712 mContext.sendBroadcast(in);
713
San Mehata5078592010-03-25 09:36:54 -0700714 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
San Mehat4270e1e2010-01-29 05:32:19 -0800715 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
716 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
717 } else if (code == VoldResponseCode.VolumeBadRemoval) {
San Mehata5078592010-03-25 09:36:54 -0700718 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800719 /* Send the media unmounted event first */
720 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
721 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
722 mContext.sendBroadcast(in);
723
San Mehata5078592010-03-25 09:36:54 -0700724 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
San Mehat4270e1e2010-01-29 05:32:19 -0800725 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
726 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
727 } else {
San Mehata5078592010-03-25 09:36:54 -0700728 Slog.e(TAG, String.format("Unknown code {%d}", code));
San Mehat4270e1e2010-01-29 05:32:19 -0800729 }
730 } else {
731 return false;
732 }
733
734 if (in != null) {
735 mContext.sendBroadcast(in);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400736 }
737 return true;
San Mehat4270e1e2010-01-29 05:32:19 -0800738 }
739
San Mehat207e5382010-02-04 20:46:54 -0800740 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800741 String vs = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -0700742 if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
San Mehat4270e1e2010-01-29 05:32:19 -0800743
744 Intent in = null;
745
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500746 if (oldState == VolumeState.Shared && newState != oldState) {
San Mehata5078592010-03-25 09:36:54 -0700747 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500748 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
749 Uri.parse("file://" + path)));
750 }
751
San Mehat4270e1e2010-01-29 05:32:19 -0800752 if (newState == VolumeState.Init) {
753 } else if (newState == VolumeState.NoMedia) {
754 // NoMedia is handled via Disk Remove events
755 } else if (newState == VolumeState.Idle) {
756 /*
757 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
758 * if we're in the process of enabling UMS
759 */
760 if (!vs.equals(
761 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
762 Environment.MEDIA_NOFS) && !vs.equals(
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800763 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
San Mehata5078592010-03-25 09:36:54 -0700764 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
San Mehat4270e1e2010-01-29 05:32:19 -0800765 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
766 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
767 }
768 } else if (newState == VolumeState.Pending) {
769 } else if (newState == VolumeState.Checking) {
San Mehata5078592010-03-25 09:36:54 -0700770 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
San Mehat4270e1e2010-01-29 05:32:19 -0800771 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
772 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
773 } else if (newState == VolumeState.Mounted) {
San Mehata5078592010-03-25 09:36:54 -0700774 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800775 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
San Mehat4270e1e2010-01-29 05:32:19 -0800776 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
777 in.putExtra("read-only", false);
778 } else if (newState == VolumeState.Unmounting) {
San Mehat4270e1e2010-01-29 05:32:19 -0800779 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
780 } else if (newState == VolumeState.Formatting) {
781 } else if (newState == VolumeState.Shared) {
San Mehata5078592010-03-25 09:36:54 -0700782 if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800783 /* Send the media unmounted event first */
784 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
785 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
786 mContext.sendBroadcast(in);
787
San Mehata5078592010-03-25 09:36:54 -0700788 if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
San Mehat4270e1e2010-01-29 05:32:19 -0800789 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
790 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
San Mehata5078592010-03-25 09:36:54 -0700791 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
San Mehat4270e1e2010-01-29 05:32:19 -0800792 } else if (newState == VolumeState.SharedMnt) {
San Mehata5078592010-03-25 09:36:54 -0700793 Slog.e(TAG, "Live shared mounts not supported yet!");
San Mehat4270e1e2010-01-29 05:32:19 -0800794 return;
795 } else {
San Mehata5078592010-03-25 09:36:54 -0700796 Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800797 }
798
799 if (in != null) {
800 mContext.sendBroadcast(in);
801 }
802 }
803
San Mehat207e5382010-02-04 20:46:54 -0800804 private boolean doGetShareMethodAvailable(String method) {
Kenny Root85fb2062010-06-01 20:50:21 -0700805 ArrayList<String> rsp;
Kenny Roota80ce062010-06-01 13:23:53 -0700806 try {
Kenny Root85fb2062010-06-01 20:50:21 -0700807 rsp = mConnector.doCommand("share status " + method);
Kenny Roota80ce062010-06-01 13:23:53 -0700808 } catch (NativeDaemonConnectorException ex) {
809 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
810 return false;
811 }
San Mehat207e5382010-02-04 20:46:54 -0800812
813 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700814 String[] tok = line.split(" ");
815 if (tok.length < 3) {
816 Slog.e(TAG, "Malformed response to share status " + method);
817 return false;
818 }
819
San Mehat207e5382010-02-04 20:46:54 -0800820 int code;
821 try {
822 code = Integer.parseInt(tok[0]);
823 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700824 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehat207e5382010-02-04 20:46:54 -0800825 return false;
826 }
827 if (code == VoldResponseCode.ShareStatusResult) {
828 if (tok[2].equals("available"))
829 return true;
830 return false;
831 } else {
San Mehata5078592010-03-25 09:36:54 -0700832 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehat207e5382010-02-04 20:46:54 -0800833 return false;
834 }
835 }
San Mehata5078592010-03-25 09:36:54 -0700836 Slog.e(TAG, "Got an empty response");
San Mehat207e5382010-02-04 20:46:54 -0800837 return false;
838 }
839
840 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800841 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800842
San Mehata5078592010-03-25 09:36:54 -0700843 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
San Mehat207e5382010-02-04 20:46:54 -0800844 try {
845 mConnector.doCommand(String.format("volume mount %s", path));
846 } catch (NativeDaemonConnectorException e) {
847 /*
848 * Mount failed for some reason
849 */
850 Intent in = null;
851 int code = e.getCode();
852 if (code == VoldResponseCode.OpFailedNoMedia) {
853 /*
854 * Attempt to mount but no media inserted
855 */
San Mehatb1043402010-02-05 08:26:50 -0800856 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800857 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
San Mehata5078592010-03-25 09:36:54 -0700858 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
San Mehat207e5382010-02-04 20:46:54 -0800859 /*
860 * Media is blank or does not contain a supported filesystem
861 */
862 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
863 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800864 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800865 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehata5078592010-03-25 09:36:54 -0700866 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
San Mehat207e5382010-02-04 20:46:54 -0800867 /*
868 * Volume consistency check failed
869 */
870 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
871 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800872 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800873 } else {
San Mehatb1043402010-02-05 08:26:50 -0800874 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800875 }
876
877 /*
878 * Send broadcast intent (if required for the failure)
879 */
880 if (in != null) {
881 mContext.sendBroadcast(in);
882 }
883 }
884
885 return rc;
886 }
887
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800888 /*
889 * If force is not set, we do not unmount if there are
890 * processes holding references to the volume about to be unmounted.
891 * If force is set, all the processes holding references need to be
892 * killed via the ActivityManager before actually unmounting the volume.
893 * This might even take a while and might be retried after timed delays
894 * to make sure we dont end up in an instable state and kill some core
895 * processes.
896 */
San Mehatd9709982010-02-18 11:43:03 -0800897 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800898 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800899 return VoldResponseCode.OpFailedVolNotMounted;
900 }
Kenny Rootaa485402010-09-14 14:49:41 -0700901
902 /*
903 * Force a GC to make sure AssetManagers in other threads of the
904 * system_server are cleaned up. We have to do this since AssetManager
905 * instances are kept as a WeakReference and it's possible we have files
906 * open on the external storage.
907 */
908 Runtime.getRuntime().gc();
909
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800910 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700911 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800912 try {
San Mehatd9709982010-02-18 11:43:03 -0800913 mConnector.doCommand(String.format(
914 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700915 // We unmounted the volume. None of the asec containers are available now.
916 synchronized (mAsecMountSet) {
917 mAsecMountSet.clear();
918 }
San Mehatb1043402010-02-05 08:26:50 -0800919 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800920 } catch (NativeDaemonConnectorException e) {
921 // Don't worry about mismatch in PackageManager since the
922 // call back will handle the status changes any way.
923 int code = e.getCode();
924 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800925 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800926 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
927 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800928 } else {
San Mehatb1043402010-02-05 08:26:50 -0800929 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800930 }
931 }
932 }
933
934 private int doFormatVolume(String path) {
935 try {
936 String cmd = String.format("volume format %s", path);
937 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800938 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800939 } catch (NativeDaemonConnectorException e) {
940 int code = e.getCode();
941 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800942 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800943 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800944 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800945 } else {
San Mehatb1043402010-02-05 08:26:50 -0800946 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800947 }
948 }
949 }
950
San Mehatb1043402010-02-05 08:26:50 -0800951 private boolean doGetVolumeShared(String path, String method) {
952 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700953 ArrayList<String> rsp;
954
955 try {
956 rsp = mConnector.doCommand(cmd);
957 } catch (NativeDaemonConnectorException ex) {
958 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
959 return false;
960 }
San Mehatb1043402010-02-05 08:26:50 -0800961
962 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700963 String[] tok = line.split(" ");
964 if (tok.length < 3) {
965 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
966 return false;
967 }
968
San Mehatb1043402010-02-05 08:26:50 -0800969 int code;
970 try {
971 code = Integer.parseInt(tok[0]);
972 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700973 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800974 return false;
975 }
976 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700977 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800978 } else {
San Mehata5078592010-03-25 09:36:54 -0700979 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800980 return false;
981 }
982 }
San Mehata5078592010-03-25 09:36:54 -0700983 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800984 return false;
985 }
986
San Mehat207e5382010-02-04 20:46:54 -0800987 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800988 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -0700989 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800990 return;
991 }
992
993 synchronized (mListeners) {
994 for (int i = mListeners.size() -1; i >= 0; i--) {
995 MountServiceBinderListener bl = mListeners.get(i);
996 try {
San Mehatb1043402010-02-05 08:26:50 -0800997 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -0800998 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700999 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -08001000 mListeners.remove(i);
1001 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -07001002 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -08001003 }
1004 }
1005 }
1006
San Mehat207e5382010-02-04 20:46:54 -08001007 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -08001008 sendUmsIntent(avail);
1009 } else {
1010 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -08001011 }
San Mehat2fe718a2010-03-11 12:01:49 -08001012
1013 final String path = Environment.getExternalStorageDirectory().getPath();
1014 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
1015 /*
1016 * USB mass storage disconnected while enabled
1017 */
1018 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -06001019 @Override
San Mehat2fe718a2010-03-11 12:01:49 -08001020 public void run() {
1021 try {
1022 int rc;
San Mehata5078592010-03-25 09:36:54 -07001023 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -08001024 doShareUnshareVolume(path, "ums", false);
1025 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001026 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -08001027 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
1028 path, rc));
1029 }
1030 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -07001031 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -08001032 }
1033 }
1034 }.start();
1035 }
San Mehat4270e1e2010-01-29 05:32:19 -08001036 }
1037
San Mehat6a965af22010-02-24 17:47:30 -08001038 private void sendUmsIntent(boolean c) {
1039 mContext.sendBroadcast(
1040 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
1041 }
1042
San Mehat207e5382010-02-04 20:46:54 -08001043 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -08001044 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
1045 throw new SecurityException(String.format("Requires %s permission", perm));
1046 }
1047 }
1048
1049 /**
San Mehat207e5382010-02-04 20:46:54 -08001050 * Constructs a new MountService instance
1051 *
1052 * @param context Binder context for this service
1053 */
1054 public MountService(Context context) {
1055 mContext = context;
1056
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001057 mExternalStoragePath = Environment.getExternalStorageDirectory().getPath();
Mike Lockwood03559752010-07-19 18:25:03 -04001058 mEmulateExternalStorage = context.getResources().getBoolean(
1059 com.android.internal.R.bool.config_emulateExternalStorage);
1060 if (mEmulateExternalStorage) {
1061 Slog.d(TAG, "using emulated external storage");
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001062 mVolumeStates.put(mExternalStoragePath, Environment.MEDIA_MOUNTED);
Mike Lockwood03559752010-07-19 18:25:03 -04001063 }
1064
San Mehat207e5382010-02-04 20:46:54 -08001065 // XXX: This will go away soon in favor of IMountServiceObserver
1066 mPms = (PackageManagerService) ServiceManager.getService("package");
1067
1068 mContext.registerReceiver(mBroadcastReceiver,
1069 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
1070
Daniel Sandler5f27ef42010-03-16 15:42:02 -04001071 mHandlerThread = new HandlerThread("MountService");
1072 mHandlerThread.start();
1073 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
1074
Kenny Roota02b8b02010-08-05 16:14:17 -07001075 // Add OBB Action Handler to MountService thread.
1076 mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper());
1077
Marco Nelissenc34ebce2010-02-18 13:39:41 -08001078 /*
1079 * Vold does not run in the simulator, so pretend the connector thread
1080 * ran and did its thing.
1081 */
1082 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
1083 mReady = true;
1084 mUmsEnabling = true;
1085 return;
1086 }
1087
Kenny Root305bcbf2010-09-03 07:56:38 -07001088 /*
1089 * Create the connection to vold with a maximum queue of twice the
1090 * amount of containers we'd ever expect to have. This keeps an
1091 * "asec list" from blocking a thread repeatedly.
1092 */
1093 mConnector = new NativeDaemonConnector(this, "vold",
1094 PackageManagerService.MAX_CONTAINERS * 2, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001095 mReady = false;
Kenny Root305bcbf2010-09-03 07:56:38 -07001096 Thread thread = new Thread(mConnector, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001097 thread.start();
1098 }
1099
1100 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001101 * Exposed API calls below here
1102 */
1103
1104 public void registerListener(IMountServiceListener listener) {
1105 synchronized (mListeners) {
1106 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
1107 try {
1108 listener.asBinder().linkToDeath(bl, 0);
1109 mListeners.add(bl);
1110 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001111 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -08001112 }
1113 }
1114 }
1115
1116 public void unregisterListener(IMountServiceListener listener) {
1117 synchronized (mListeners) {
1118 for(MountServiceBinderListener bl : mListeners) {
1119 if (bl.mListener == listener) {
1120 mListeners.remove(mListeners.indexOf(bl));
1121 return;
1122 }
1123 }
1124 }
1125 }
1126
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001127 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -08001128 validatePermission(android.Manifest.permission.SHUTDOWN);
1129
San Mehata5078592010-03-25 09:36:54 -07001130 Slog.i(TAG, "Shutting down");
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001131 synchronized (mVolumeStates) {
1132 for (String path : mVolumeStates.keySet()) {
1133 String state = mVolumeStates.get(path);
San Mehat4270e1e2010-01-29 05:32:19 -08001134
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001135 if (state.equals(Environment.MEDIA_SHARED)) {
1136 /*
1137 * If the media is currently shared, unshare it.
1138 * XXX: This is still dangerous!. We should not
1139 * be rebooting at *all* if UMS is enabled, since
1140 * the UMS host could have dirty FAT cache entries
1141 * yet to flush.
1142 */
1143 setUsbMassStorageEnabled(false);
1144 } else if (state.equals(Environment.MEDIA_CHECKING)) {
1145 /*
1146 * If the media is being checked, then we need to wait for
1147 * it to complete before being able to proceed.
1148 */
1149 // XXX: @hackbod - Should we disable the ANR timer here?
1150 int retries = 30;
1151 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
1152 try {
1153 Thread.sleep(1000);
1154 } catch (InterruptedException iex) {
1155 Slog.e(TAG, "Interrupted while waiting for media", iex);
1156 break;
1157 }
1158 state = Environment.getExternalStorageState();
1159 }
1160 if (retries == 0) {
1161 Slog.e(TAG, "Timed out waiting for media to check");
1162 }
San Mehat91c77612010-01-07 10:39:41 -08001163 }
San Mehat91c77612010-01-07 10:39:41 -08001164
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001165 if (state.equals(Environment.MEDIA_MOUNTED)) {
1166 // Post a unmount message.
1167 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
1168 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
1169 } else if (observer != null) {
1170 /*
1171 * Observer is waiting for onShutDownComplete when we are done.
1172 * Since nothing will be done send notification directly so shutdown
1173 * sequence can continue.
1174 */
1175 try {
1176 observer.onShutDownComplete(StorageResultCode.OperationSucceeded);
1177 } catch (RemoteException e) {
1178 Slog.w(TAG, "RemoteException when shutting down");
1179 }
1180 }
Johan Alfven5d0db4d2010-11-09 10:32:25 +01001181 }
San Mehat4270e1e2010-01-29 05:32:19 -08001182 }
1183 }
1184
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001185 private boolean getUmsEnabling() {
1186 synchronized (mListeners) {
1187 return mUmsEnabling;
1188 }
1189 }
1190
1191 private void setUmsEnabling(boolean enable) {
1192 synchronized (mListeners) {
Tony Wufc711252010-08-09 16:49:19 +08001193 mUmsEnabling = enable;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001194 }
1195 }
1196
San Mehatb1043402010-02-05 08:26:50 -08001197 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -08001198 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -08001199
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001200 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001201 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001202 }
San Mehatb1043402010-02-05 08:26:50 -08001203 return doGetShareMethodAvailable("ums");
1204 }
1205
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001206 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001207 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001208 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001209
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001210 // TODO: Add support for multiple share methods
1211
1212 /*
1213 * If the volume is mounted and we're enabling then unmount it
1214 */
1215 String path = Environment.getExternalStorageDirectory().getPath();
1216 String vs = getVolumeState(path);
1217 String method = "ums";
1218 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1219 // Override for isUsbMassStorageEnabled()
1220 setUmsEnabling(enable);
1221 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1222 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1223 // Clear override
1224 setUmsEnabling(false);
1225 }
1226 /*
1227 * If we disabled UMS then mount the volume
1228 */
1229 if (!enable) {
1230 doShareUnshareVolume(path, method, enable);
1231 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001232 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001233 " after disabling share method " + method);
1234 /*
1235 * Even though the mount failed, the unshare didn't so don't indicate an error.
1236 * The mountVolume() call will have set the storage state and sent the necessary
1237 * broadcasts.
1238 */
1239 }
1240 }
San Mehatb1043402010-02-05 08:26:50 -08001241 }
1242
1243 public boolean isUsbMassStorageEnabled() {
1244 waitForReady();
1245 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001247
San Mehat7fd0fee2009-12-17 07:12:23 -08001248 /**
1249 * @return state of the volume at the specified mount point
1250 */
San Mehat4270e1e2010-01-29 05:32:19 -08001251 public String getVolumeState(String mountPoint) {
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001252 synchronized (mVolumeStates) {
1253 String state = mVolumeStates.get(mountPoint);
1254 if (state == null) {
1255 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
1256 throw new IllegalArgumentException();
1257 }
San Mehat7fd0fee2009-12-17 07:12:23 -08001258
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001259 return state;
1260 }
San Mehat7fd0fee2009-12-17 07:12:23 -08001261 }
1262
Kenny Roote1ff2142010-10-12 11:20:01 -07001263 public boolean isExternalStorageEmulated() {
1264 return mEmulateExternalStorage;
1265 }
1266
San Mehat4270e1e2010-01-29 05:32:19 -08001267 public int mountVolume(String path) {
1268 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001269
San Mehat207e5382010-02-04 20:46:54 -08001270 waitForReady();
1271 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 }
1273
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001274 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001275 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001276 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001278 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001279 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001280 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1281 Environment.MEDIA_REMOVED.equals(volState) ||
1282 Environment.MEDIA_SHARED.equals(volState) ||
1283 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1284 // Media already unmounted or cannot be unmounted.
1285 // TODO return valid return code when adding observer call back.
1286 return;
1287 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001288 UnmountCallBack ucb = new UnmountCallBack(path, force);
1289 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 }
1291
San Mehat4270e1e2010-01-29 05:32:19 -08001292 public int formatVolume(String path) {
1293 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001294 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001295
San Mehat207e5382010-02-04 20:46:54 -08001296 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 }
1298
San Mehatc1b4ce92010-02-16 17:13:03 -08001299 public int []getStorageUsers(String path) {
1300 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1301 waitForReady();
1302 try {
1303 String[] r = mConnector.doListCommand(
1304 String.format("storage users %s", path),
1305 VoldResponseCode.StorageUsersListResult);
1306 // FMT: <pid> <process name>
1307 int[] data = new int[r.length];
1308 for (int i = 0; i < r.length; i++) {
1309 String []tok = r[i].split(" ");
1310 try {
1311 data[i] = Integer.parseInt(tok[0]);
1312 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001313 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001314 return new int[0];
1315 }
1316 }
1317 return data;
1318 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001319 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001320 return new int[0];
1321 }
1322 }
1323
San Mehatb1043402010-02-05 08:26:50 -08001324 private void warnOnNotMounted() {
1325 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001326 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001327 }
1328 }
1329
San Mehat4270e1e2010-01-29 05:32:19 -08001330 public String[] getSecureContainerList() {
1331 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001332 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001333 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001334
San Mehat4270e1e2010-01-29 05:32:19 -08001335 try {
1336 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1337 } catch (NativeDaemonConnectorException e) {
1338 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 }
1340 }
San Mehat36972292010-01-06 11:06:32 -08001341
San Mehat4270e1e2010-01-29 05:32:19 -08001342 public int createSecureContainer(String id, int sizeMb, String fstype,
1343 String key, int ownerUid) {
1344 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001345 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001346 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001347
San Mehatb1043402010-02-05 08:26:50 -08001348 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001349 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1350 try {
1351 mConnector.doCommand(cmd);
1352 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001353 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001354 }
San Mehata181b212010-02-11 06:50:20 -08001355
1356 if (rc == StorageResultCode.OperationSucceeded) {
1357 synchronized (mAsecMountSet) {
1358 mAsecMountSet.add(id);
1359 }
1360 }
San Mehat4270e1e2010-01-29 05:32:19 -08001361 return rc;
San Mehat36972292010-01-06 11:06:32 -08001362 }
1363
San Mehat4270e1e2010-01-29 05:32:19 -08001364 public int finalizeSecureContainer(String id) {
1365 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001366 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001367
San Mehatb1043402010-02-05 08:26:50 -08001368 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001369 try {
1370 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001371 /*
1372 * Finalization does a remount, so no need
1373 * to update mAsecMountSet
1374 */
San Mehat4270e1e2010-01-29 05:32:19 -08001375 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001376 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001377 }
San Mehat4270e1e2010-01-29 05:32:19 -08001378 return rc;
San Mehat36972292010-01-06 11:06:32 -08001379 }
1380
San Mehatd9709982010-02-18 11:43:03 -08001381 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001382 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001383 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001384 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001385
Kenny Rootaa485402010-09-14 14:49:41 -07001386 /*
1387 * Force a GC to make sure AssetManagers in other threads of the
1388 * system_server are cleaned up. We have to do this since AssetManager
1389 * instances are kept as a WeakReference and it's possible we have files
1390 * open on the external storage.
1391 */
1392 Runtime.getRuntime().gc();
1393
San Mehatb1043402010-02-05 08:26:50 -08001394 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001395 try {
San Mehatd9709982010-02-18 11:43:03 -08001396 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001397 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001398 int code = e.getCode();
1399 if (code == VoldResponseCode.OpFailedStorageBusy) {
1400 rc = StorageResultCode.OperationFailedStorageBusy;
1401 } else {
1402 rc = StorageResultCode.OperationFailedInternalError;
1403 }
San Mehat02735bc2010-01-26 15:18:08 -08001404 }
San Mehata181b212010-02-11 06:50:20 -08001405
1406 if (rc == StorageResultCode.OperationSucceeded) {
1407 synchronized (mAsecMountSet) {
1408 if (mAsecMountSet.contains(id)) {
1409 mAsecMountSet.remove(id);
1410 }
1411 }
1412 }
1413
San Mehat4270e1e2010-01-29 05:32:19 -08001414 return rc;
San Mehat36972292010-01-06 11:06:32 -08001415 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001416
San Mehat4270e1e2010-01-29 05:32:19 -08001417 public int mountSecureContainer(String id, String key, int ownerUid) {
1418 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001419 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001420 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001421
San Mehata181b212010-02-11 06:50:20 -08001422 synchronized (mAsecMountSet) {
1423 if (mAsecMountSet.contains(id)) {
1424 return StorageResultCode.OperationFailedStorageMounted;
1425 }
1426 }
1427
San Mehatb1043402010-02-05 08:26:50 -08001428 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001429 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1430 try {
1431 mConnector.doCommand(cmd);
1432 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001433 int code = e.getCode();
1434 if (code != VoldResponseCode.OpFailedStorageBusy) {
1435 rc = StorageResultCode.OperationFailedInternalError;
1436 }
San Mehat02735bc2010-01-26 15:18:08 -08001437 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001438
1439 if (rc == StorageResultCode.OperationSucceeded) {
1440 synchronized (mAsecMountSet) {
1441 mAsecMountSet.add(id);
1442 }
1443 }
San Mehat4270e1e2010-01-29 05:32:19 -08001444 return rc;
San Mehat36972292010-01-06 11:06:32 -08001445 }
1446
San Mehatd9709982010-02-18 11:43:03 -08001447 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001448 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001449 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001450 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001451
San Mehat6cdd9c02010-02-09 14:45:20 -08001452 synchronized (mAsecMountSet) {
1453 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001454 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001455 }
1456 }
1457
Kenny Rootaa485402010-09-14 14:49:41 -07001458 /*
1459 * Force a GC to make sure AssetManagers in other threads of the
1460 * system_server are cleaned up. We have to do this since AssetManager
1461 * instances are kept as a WeakReference and it's possible we have files
1462 * open on the external storage.
1463 */
1464 Runtime.getRuntime().gc();
1465
San Mehatb1043402010-02-05 08:26:50 -08001466 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001467 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001468 try {
1469 mConnector.doCommand(cmd);
1470 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001471 int code = e.getCode();
1472 if (code == VoldResponseCode.OpFailedStorageBusy) {
1473 rc = StorageResultCode.OperationFailedStorageBusy;
1474 } else {
1475 rc = StorageResultCode.OperationFailedInternalError;
1476 }
San Mehat02735bc2010-01-26 15:18:08 -08001477 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001478
1479 if (rc == StorageResultCode.OperationSucceeded) {
1480 synchronized (mAsecMountSet) {
1481 mAsecMountSet.remove(id);
1482 }
1483 }
San Mehat4270e1e2010-01-29 05:32:19 -08001484 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001485 }
1486
San Mehat6cdd9c02010-02-09 14:45:20 -08001487 public boolean isSecureContainerMounted(String id) {
1488 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1489 waitForReady();
1490 warnOnNotMounted();
1491
1492 synchronized (mAsecMountSet) {
1493 return mAsecMountSet.contains(id);
1494 }
1495 }
1496
San Mehat4270e1e2010-01-29 05:32:19 -08001497 public int renameSecureContainer(String oldId, String newId) {
1498 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001499 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001500 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001501
San Mehata181b212010-02-11 06:50:20 -08001502 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001503 /*
Jason parks9ed98bc2011-01-17 09:58:35 -06001504 * Because a mounted container has active internal state which cannot be
San Mehat85451ee2010-02-24 08:54:18 -08001505 * changed while active, we must ensure both ids are not currently mounted.
1506 */
1507 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001508 return StorageResultCode.OperationFailedStorageMounted;
1509 }
1510 }
1511
San Mehatb1043402010-02-05 08:26:50 -08001512 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001513 String cmd = String.format("asec rename %s %s", oldId, newId);
1514 try {
1515 mConnector.doCommand(cmd);
1516 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001517 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001518 }
San Mehata181b212010-02-11 06:50:20 -08001519
San Mehat4270e1e2010-01-29 05:32:19 -08001520 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001521 }
1522
San Mehat4270e1e2010-01-29 05:32:19 -08001523 public String getSecureContainerPath(String id) {
1524 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001525 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001526 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001527
San Mehat2d66cef2010-03-23 11:12:52 -07001528 try {
1529 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1530 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001531 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001532 if (code != VoldResponseCode.AsecPathResult) {
1533 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1534 }
1535 return tok[1];
1536 } catch (NativeDaemonConnectorException e) {
1537 int code = e.getCode();
1538 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1539 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001540 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001541 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001542 }
1543 }
San Mehat22dd86e2010-01-12 12:21:18 -08001544 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001545
1546 public void finishMediaUpdate() {
1547 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1548 }
Kenny Root02c87302010-07-01 08:10:18 -07001549
Kenny Roota02b8b02010-08-05 16:14:17 -07001550 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
1551 if (callerUid == android.os.Process.SYSTEM_UID) {
1552 return true;
1553 }
1554
Kenny Root02c87302010-07-01 08:10:18 -07001555 if (packageName == null) {
1556 return false;
1557 }
1558
1559 final int packageUid = mPms.getPackageUid(packageName);
1560
1561 if (DEBUG_OBB) {
1562 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
1563 packageUid + ", callerUid = " + callerUid);
1564 }
1565
1566 return callerUid == packageUid;
1567 }
1568
1569 public String getMountedObbPath(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001570 if (filename == null) {
1571 throw new IllegalArgumentException("filename cannot be null");
1572 }
1573
Kenny Root02c87302010-07-01 08:10:18 -07001574 waitForReady();
1575 warnOnNotMounted();
1576
Kenny Root02c87302010-07-01 08:10:18 -07001577 try {
1578 ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename));
1579 String []tok = rsp.get(0).split(" ");
1580 int code = Integer.parseInt(tok[0]);
1581 if (code != VoldResponseCode.AsecPathResult) {
1582 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1583 }
1584 return tok[1];
1585 } catch (NativeDaemonConnectorException e) {
1586 int code = e.getCode();
1587 if (code == VoldResponseCode.OpFailedStorageNotFound) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001588 return null;
Kenny Root02c87302010-07-01 08:10:18 -07001589 } else {
1590 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1591 }
1592 }
1593 }
1594
1595 public boolean isObbMounted(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001596 if (filename == null) {
1597 throw new IllegalArgumentException("filename cannot be null");
Kenny Root02c87302010-07-01 08:10:18 -07001598 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001599
1600 synchronized (mObbMounts) {
1601 return mObbPathToStateMap.containsKey(filename);
1602 }
Kenny Root02c87302010-07-01 08:10:18 -07001603 }
1604
Kenny Rootaf9d6672010-10-08 09:21:39 -07001605 public void mountObb(String filename, String key, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -07001606 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001607 if (filename == null) {
1608 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootaf9d6672010-10-08 09:21:39 -07001609 }
1610
1611 if (token == null) {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001612 throw new IllegalArgumentException("token cannot be null");
1613 }
1614
Kenny Rootaf9d6672010-10-08 09:21:39 -07001615 final int callerUid = Binder.getCallingUid();
1616 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1617 final ObbAction action = new MountObbAction(obbState, key);
Kenny Roota02b8b02010-08-05 16:14:17 -07001618 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
1619
1620 if (DEBUG_OBB)
1621 Slog.i(TAG, "Send to OBB handler: " + action.toString());
Kenny Root02c87302010-07-01 08:10:18 -07001622 }
1623
Kenny Rootaf9d6672010-10-08 09:21:39 -07001624 public void unmountObb(String filename, boolean force, IObbActionListener token, int nonce)
1625 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001626 if (filename == null) {
1627 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootf1121dc2010-09-29 07:30:53 -07001628 }
1629
Kenny Rootaf9d6672010-10-08 09:21:39 -07001630 final int callerUid = Binder.getCallingUid();
1631 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1632 final ObbAction action = new UnmountObbAction(obbState, force);
Kenny Roota02b8b02010-08-05 16:14:17 -07001633 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root02c87302010-07-01 08:10:18 -07001634
Kenny Roota02b8b02010-08-05 16:14:17 -07001635 if (DEBUG_OBB)
1636 Slog.i(TAG, "Send to OBB handler: " + action.toString());
1637 }
1638
Jason parks5af0b912010-11-29 09:05:25 -06001639 public int decryptStorage(String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06001640 if (TextUtils.isEmpty(password)) {
1641 throw new IllegalArgumentException("password cannot be empty");
Jason parks5af0b912010-11-29 09:05:25 -06001642 }
1643
Jason parks8888c592011-01-20 22:46:41 -06001644 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1645 "no permission to access the crypt keeper");
Jason parks5af0b912010-11-29 09:05:25 -06001646
1647 waitForReady();
1648
1649 if (DEBUG_EVENTS) {
1650 Slog.i(TAG, "decrypting storage...");
1651 }
1652
1653 try {
Jason parks9ed98bc2011-01-17 09:58:35 -06001654 ArrayList<String> rsp = mConnector.doCommand("cryptfs checkpw " + password);
Jason parksf7b3cd42011-01-27 09:28:25 -06001655 String[] tokens = rsp.get(0).split(" ");
Jason parks9ed98bc2011-01-17 09:58:35 -06001656
Jason parksf7b3cd42011-01-27 09:28:25 -06001657 if (tokens == null || tokens.length != 2) {
Jason parks9ed98bc2011-01-17 09:58:35 -06001658 return -1;
1659 }
1660
Jason parksf7b3cd42011-01-27 09:28:25 -06001661 int code = Integer.parseInt(tokens[1]);
Jason parks9ed98bc2011-01-17 09:58:35 -06001662
1663 if (code == 0) {
1664 // Decrypt was successful. Post a delayed message before restarting in order
1665 // to let the UI to clear itself
1666 mHandler.postDelayed(new Runnable() {
1667 public void run() {
1668 mConnector.doCommand(String.format("cryptfs restart"));
1669 }
Jason parksf7b3cd42011-01-27 09:28:25 -06001670 }, 1000); // 1 second
Jason parks9ed98bc2011-01-17 09:58:35 -06001671 }
1672
1673 return code;
Jason parks5af0b912010-11-29 09:05:25 -06001674 } catch (NativeDaemonConnectorException e) {
1675 // Decryption failed
1676 return e.getCode();
1677 }
Jason parks5af0b912010-11-29 09:05:25 -06001678 }
1679
Jason parks56aa5322011-01-07 09:01:15 -06001680 public int encryptStorage(String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06001681 if (TextUtils.isEmpty(password)) {
1682 throw new IllegalArgumentException("password cannot be empty");
Jason parks56aa5322011-01-07 09:01:15 -06001683 }
1684
Jason parks8888c592011-01-20 22:46:41 -06001685 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1686 "no permission to access the crypt keeper");
Jason parks56aa5322011-01-07 09:01:15 -06001687
1688 waitForReady();
1689
1690 if (DEBUG_EVENTS) {
Jason parks8888c592011-01-20 22:46:41 -06001691 Slog.i(TAG, "encrypting storage...");
Jason parks56aa5322011-01-07 09:01:15 -06001692 }
1693
1694 try {
Jason parks9ed98bc2011-01-17 09:58:35 -06001695 mConnector.doCommand(String.format("cryptfs enablecrypto inplace %s", password));
Jason parks56aa5322011-01-07 09:01:15 -06001696 } catch (NativeDaemonConnectorException e) {
1697 // Encryption failed
1698 return e.getCode();
1699 }
1700
1701 return 0;
1702 }
1703
Jason parksf7b3cd42011-01-27 09:28:25 -06001704 public int changeEncryptionPassword(String password) {
1705 if (TextUtils.isEmpty(password)) {
1706 throw new IllegalArgumentException("password cannot be empty");
1707 }
1708
1709 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1710 "no permission to access the crypt keeper");
1711
1712 waitForReady();
1713
1714 if (DEBUG_EVENTS) {
1715 Slog.i(TAG, "changing encryption password...");
1716 }
1717
1718 try {
1719 ArrayList<String> response = mConnector.doCommand("cryptfs changepw " + password);
1720
1721 String[] tokens = response.get(0).split(" ");
1722
1723 if (tokens == null || tokens.length != 2) {
1724 return -1;
1725 }
1726
1727 return Integer.parseInt(tokens[1]);
1728 } catch (NativeDaemonConnectorException e) {
1729 // Encryption failed
1730 return e.getCode();
1731 }
1732 }
1733
Mike Lockwoodd967f462011-03-24 08:12:30 -07001734 public String[] getVolumeList() {
1735 synchronized(mVolumeStates) {
1736 Set<String> volumes = mVolumeStates.keySet();
1737 String[] result = new String[volumes.size()];
1738 int i = 0;
1739 for (String volume : volumes) {
1740 result[i++] = volume;
1741 }
1742 return result;
1743 }
1744 }
1745
Kenny Rootaf9d6672010-10-08 09:21:39 -07001746 private void addObbStateLocked(ObbState obbState) throws RemoteException {
1747 final IBinder binder = obbState.getBinder();
1748 List<ObbState> obbStates = mObbMounts.get(binder);
Kenny Root5919ac62010-10-05 09:49:40 -07001749
Kenny Rootaf9d6672010-10-08 09:21:39 -07001750 if (obbStates == null) {
1751 obbStates = new ArrayList<ObbState>();
1752 mObbMounts.put(binder, obbStates);
1753 } else {
1754 for (final ObbState o : obbStates) {
1755 if (o.filename.equals(obbState.filename)) {
1756 throw new IllegalStateException("Attempt to add ObbState twice. "
1757 + "This indicates an error in the MountService logic.");
Kenny Root5919ac62010-10-05 09:49:40 -07001758 }
1759 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001760 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001761
1762 obbStates.add(obbState);
1763 try {
1764 obbState.link();
1765 } catch (RemoteException e) {
1766 /*
1767 * The binder died before we could link it, so clean up our state
1768 * and return failure.
1769 */
1770 obbStates.remove(obbState);
1771 if (obbStates.isEmpty()) {
1772 mObbMounts.remove(binder);
1773 }
1774
1775 // Rethrow the error so mountObb can get it
1776 throw e;
1777 }
1778
1779 mObbPathToStateMap.put(obbState.filename, obbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07001780 }
1781
Kenny Rootaf9d6672010-10-08 09:21:39 -07001782 private void removeObbStateLocked(ObbState obbState) {
1783 final IBinder binder = obbState.getBinder();
1784 final List<ObbState> obbStates = mObbMounts.get(binder);
1785 if (obbStates != null) {
1786 if (obbStates.remove(obbState)) {
1787 obbState.unlink();
Kenny Root05105f72010-09-22 17:29:43 -07001788 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001789 if (obbStates.isEmpty()) {
1790 mObbMounts.remove(binder);
1791 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001792 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001793
Kenny Rootaf9d6672010-10-08 09:21:39 -07001794 mObbPathToStateMap.remove(obbState.filename);
Kenny Root38cf8862010-09-26 14:18:51 -07001795 }
1796
Kenny Roota02b8b02010-08-05 16:14:17 -07001797 private class ObbActionHandler extends Handler {
1798 private boolean mBound = false;
Kenny Root480afe72010-10-07 10:17:50 -07001799 private final List<ObbAction> mActions = new LinkedList<ObbAction>();
Kenny Roota02b8b02010-08-05 16:14:17 -07001800
1801 ObbActionHandler(Looper l) {
1802 super(l);
1803 }
1804
1805 @Override
1806 public void handleMessage(Message msg) {
1807 switch (msg.what) {
1808 case OBB_RUN_ACTION: {
Kenny Root480afe72010-10-07 10:17:50 -07001809 final ObbAction action = (ObbAction) msg.obj;
Kenny Roota02b8b02010-08-05 16:14:17 -07001810
1811 if (DEBUG_OBB)
1812 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
1813
1814 // If a bind was already initiated we don't really
1815 // need to do anything. The pending install
1816 // will be processed later on.
1817 if (!mBound) {
1818 // If this is the only one pending we might
1819 // have to bind to the service again.
1820 if (!connectToService()) {
1821 Slog.e(TAG, "Failed to bind to media container service");
1822 action.handleError();
1823 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07001824 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001825 }
Kenny Root735de3b2010-09-30 14:11:39 -07001826
Kenny Root735de3b2010-09-30 14:11:39 -07001827 mActions.add(action);
Kenny Roota02b8b02010-08-05 16:14:17 -07001828 break;
1829 }
1830 case OBB_MCS_BOUND: {
1831 if (DEBUG_OBB)
1832 Slog.i(TAG, "OBB_MCS_BOUND");
1833 if (msg.obj != null) {
1834 mContainerService = (IMediaContainerService) msg.obj;
1835 }
1836 if (mContainerService == null) {
1837 // Something seriously wrong. Bail out
1838 Slog.e(TAG, "Cannot bind to media container service");
1839 for (ObbAction action : mActions) {
1840 // Indicate service bind error
1841 action.handleError();
1842 }
1843 mActions.clear();
1844 } else if (mActions.size() > 0) {
Kenny Root480afe72010-10-07 10:17:50 -07001845 final ObbAction action = mActions.get(0);
Kenny Roota02b8b02010-08-05 16:14:17 -07001846 if (action != null) {
1847 action.execute(this);
1848 }
1849 } else {
1850 // Should never happen ideally.
1851 Slog.w(TAG, "Empty queue");
1852 }
1853 break;
1854 }
1855 case OBB_MCS_RECONNECT: {
1856 if (DEBUG_OBB)
1857 Slog.i(TAG, "OBB_MCS_RECONNECT");
1858 if (mActions.size() > 0) {
1859 if (mBound) {
1860 disconnectService();
1861 }
1862 if (!connectToService()) {
1863 Slog.e(TAG, "Failed to bind to media container service");
1864 for (ObbAction action : mActions) {
1865 // Indicate service bind error
1866 action.handleError();
1867 }
1868 mActions.clear();
1869 }
1870 }
1871 break;
1872 }
1873 case OBB_MCS_UNBIND: {
1874 if (DEBUG_OBB)
1875 Slog.i(TAG, "OBB_MCS_UNBIND");
1876
1877 // Delete pending install
1878 if (mActions.size() > 0) {
1879 mActions.remove(0);
1880 }
1881 if (mActions.size() == 0) {
1882 if (mBound) {
1883 disconnectService();
1884 }
1885 } else {
1886 // There are more pending requests in queue.
1887 // Just post MCS_BOUND message to trigger processing
1888 // of next pending install.
1889 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1890 }
1891 break;
1892 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001893 case OBB_FLUSH_MOUNT_STATE: {
1894 final String path = (String) msg.obj;
1895
1896 if (DEBUG_OBB)
1897 Slog.i(TAG, "Flushing all OBB state for path " + path);
1898
1899 synchronized (mObbMounts) {
1900 final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>();
1901
1902 final Iterator<Entry<String, ObbState>> i =
1903 mObbPathToStateMap.entrySet().iterator();
1904 while (i.hasNext()) {
1905 final Entry<String, ObbState> obbEntry = i.next();
1906
1907 /*
1908 * If this entry's source file is in the volume path
1909 * that got unmounted, remove it because it's no
1910 * longer valid.
1911 */
1912 if (obbEntry.getKey().startsWith(path)) {
1913 obbStatesToRemove.add(obbEntry.getValue());
1914 }
1915 }
1916
1917 for (final ObbState obbState : obbStatesToRemove) {
1918 if (DEBUG_OBB)
1919 Slog.i(TAG, "Removing state for " + obbState.filename);
1920
1921 removeObbStateLocked(obbState);
1922
1923 try {
1924 obbState.token.onObbResult(obbState.filename, obbState.nonce,
1925 OnObbStateChangeListener.UNMOUNTED);
1926 } catch (RemoteException e) {
1927 Slog.i(TAG, "Couldn't send unmount notification for OBB: "
1928 + obbState.filename);
1929 }
1930 }
1931 }
1932 break;
1933 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001934 }
1935 }
1936
1937 private boolean connectToService() {
1938 if (DEBUG_OBB)
1939 Slog.i(TAG, "Trying to bind to DefaultContainerService");
1940
1941 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
1942 if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) {
1943 mBound = true;
1944 return true;
1945 }
1946 return false;
1947 }
1948
1949 private void disconnectService() {
1950 mContainerService = null;
1951 mBound = false;
1952 mContext.unbindService(mDefContainerConn);
1953 }
1954 }
1955
1956 abstract class ObbAction {
1957 private static final int MAX_RETRIES = 3;
1958 private int mRetries;
1959
1960 ObbState mObbState;
1961
1962 ObbAction(ObbState obbState) {
1963 mObbState = obbState;
1964 }
1965
1966 public void execute(ObbActionHandler handler) {
1967 try {
1968 if (DEBUG_OBB)
1969 Slog.i(TAG, "Starting to execute action: " + this.toString());
1970 mRetries++;
1971 if (mRetries > MAX_RETRIES) {
1972 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
Kenny Root480afe72010-10-07 10:17:50 -07001973 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001974 handleError();
1975 return;
1976 } else {
1977 handleExecute();
1978 if (DEBUG_OBB)
1979 Slog.i(TAG, "Posting install MCS_UNBIND");
1980 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1981 }
1982 } catch (RemoteException e) {
1983 if (DEBUG_OBB)
1984 Slog.i(TAG, "Posting install MCS_RECONNECT");
1985 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
1986 } catch (Exception e) {
1987 if (DEBUG_OBB)
1988 Slog.d(TAG, "Error handling OBB action", e);
1989 handleError();
Kenny Root17eb6fb2010-10-06 15:02:52 -07001990 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001991 }
1992 }
1993
Kenny Root05105f72010-09-22 17:29:43 -07001994 abstract void handleExecute() throws RemoteException, IOException;
Kenny Roota02b8b02010-08-05 16:14:17 -07001995 abstract void handleError();
Kenny Root38cf8862010-09-26 14:18:51 -07001996
1997 protected ObbInfo getObbInfo() throws IOException {
1998 ObbInfo obbInfo;
1999 try {
2000 obbInfo = mContainerService.getObbInfo(mObbState.filename);
2001 } catch (RemoteException e) {
2002 Slog.d(TAG, "Couldn't call DefaultContainerService to fetch OBB info for "
2003 + mObbState.filename);
2004 obbInfo = null;
2005 }
2006 if (obbInfo == null) {
2007 throw new IOException("Couldn't read OBB file: " + mObbState.filename);
2008 }
2009 return obbInfo;
2010 }
2011
Kenny Rootaf9d6672010-10-08 09:21:39 -07002012 protected void sendNewStatusOrIgnore(int status) {
2013 if (mObbState == null || mObbState.token == null) {
2014 return;
2015 }
2016
Kenny Root38cf8862010-09-26 14:18:51 -07002017 try {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002018 mObbState.token.onObbResult(mObbState.filename, mObbState.nonce, status);
Kenny Root38cf8862010-09-26 14:18:51 -07002019 } catch (RemoteException e) {
2020 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
2021 }
2022 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002023 }
2024
2025 class MountObbAction extends ObbAction {
2026 private String mKey;
2027
2028 MountObbAction(ObbState obbState, String key) {
2029 super(obbState);
2030 mKey = key;
2031 }
2032
Jason parks5af0b912010-11-29 09:05:25 -06002033 @Override
Kenny Root735de3b2010-09-30 14:11:39 -07002034 public void handleExecute() throws IOException, RemoteException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002035 waitForReady();
2036 warnOnNotMounted();
2037
Kenny Root38cf8862010-09-26 14:18:51 -07002038 final ObbInfo obbInfo = getObbInfo();
2039
Kenny Roota02b8b02010-08-05 16:14:17 -07002040 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002041 Slog.w(TAG, "Denied attempt to mount OBB " + obbInfo.filename
2042 + " which is owned by " + obbInfo.packageName);
2043 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2044 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07002045 }
2046
Kenny Rootaf9d6672010-10-08 09:21:39 -07002047 final boolean isMounted;
2048 synchronized (mObbMounts) {
2049 isMounted = mObbPathToStateMap.containsKey(obbInfo.filename);
2050 }
2051 if (isMounted) {
2052 Slog.w(TAG, "Attempt to mount OBB which is already mounted: " + obbInfo.filename);
2053 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_ALREADY_MOUNTED);
2054 return;
2055 }
2056
2057 /*
2058 * The filename passed in might not be the canonical name, so just
2059 * set the filename to the canonicalized version.
2060 */
2061 mObbState.filename = obbInfo.filename;
2062
2063 final String hashedKey;
2064 if (mKey == null) {
2065 hashedKey = "none";
2066 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002067 try {
Kenny Root3b1abba2010-10-13 15:00:07 -07002068 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
2069
2070 KeySpec ks = new PBEKeySpec(mKey.toCharArray(), obbInfo.salt,
2071 PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE);
2072 SecretKey key = factory.generateSecret(ks);
2073 BigInteger bi = new BigInteger(key.getEncoded());
2074 hashedKey = bi.toString(16);
Kenny Rootaf9d6672010-10-08 09:21:39 -07002075 } catch (NoSuchAlgorithmException e) {
Kenny Root3b1abba2010-10-13 15:00:07 -07002076 Slog.e(TAG, "Could not load PBKDF2 algorithm", e);
2077 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
2078 return;
2079 } catch (InvalidKeySpecException e) {
2080 Slog.e(TAG, "Invalid key spec when loading PBKDF2 algorithm", e);
2081 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root38cf8862010-09-26 14:18:51 -07002082 return;
2083 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002084 }
Kenny Root38cf8862010-09-26 14:18:51 -07002085
Kenny Rootaf9d6672010-10-08 09:21:39 -07002086 int rc = StorageResultCode.OperationSucceeded;
2087 String cmd = String.format("obb mount %s %s %d", mObbState.filename, hashedKey,
2088 mObbState.callerUid);
2089 try {
2090 mConnector.doCommand(cmd);
2091 } catch (NativeDaemonConnectorException e) {
2092 int code = e.getCode();
2093 if (code != VoldResponseCode.OpFailedStorageBusy) {
2094 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002095 }
2096 }
2097
Kenny Rootaf9d6672010-10-08 09:21:39 -07002098 if (rc == StorageResultCode.OperationSucceeded) {
2099 if (DEBUG_OBB)
2100 Slog.d(TAG, "Successfully mounted OBB " + mObbState.filename);
2101
2102 synchronized (mObbMounts) {
2103 addObbStateLocked(mObbState);
2104 }
2105
2106 sendNewStatusOrIgnore(OnObbStateChangeListener.MOUNTED);
Kenny Root02c87302010-07-01 08:10:18 -07002107 } else {
Kenny Root05105f72010-09-22 17:29:43 -07002108 Slog.e(TAG, "Couldn't mount OBB file: " + rc);
Kenny Roota02b8b02010-08-05 16:14:17 -07002109
Kenny Rootaf9d6672010-10-08 09:21:39 -07002110 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT);
Kenny Root02c87302010-07-01 08:10:18 -07002111 }
2112 }
2113
Jason parks5af0b912010-11-29 09:05:25 -06002114 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002115 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002116 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root02c87302010-07-01 08:10:18 -07002117 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002118
2119 @Override
2120 public String toString() {
2121 StringBuilder sb = new StringBuilder();
2122 sb.append("MountObbAction{");
2123 sb.append("filename=");
2124 sb.append(mObbState.filename);
2125 sb.append(",callerUid=");
2126 sb.append(mObbState.callerUid);
2127 sb.append(",token=");
2128 sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002129 sb.append(",binder=");
2130 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002131 sb.append('}');
2132 return sb.toString();
2133 }
2134 }
2135
2136 class UnmountObbAction extends ObbAction {
2137 private boolean mForceUnmount;
2138
2139 UnmountObbAction(ObbState obbState, boolean force) {
2140 super(obbState);
2141 mForceUnmount = force;
2142 }
2143
Jason parks5af0b912010-11-29 09:05:25 -06002144 @Override
Kenny Root38cf8862010-09-26 14:18:51 -07002145 public void handleExecute() throws IOException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002146 waitForReady();
2147 warnOnNotMounted();
2148
Kenny Root38cf8862010-09-26 14:18:51 -07002149 final ObbInfo obbInfo = getObbInfo();
Kenny Roota02b8b02010-08-05 16:14:17 -07002150
Kenny Rootaf9d6672010-10-08 09:21:39 -07002151 final ObbState obbState;
Kenny Root38cf8862010-09-26 14:18:51 -07002152 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002153 obbState = mObbPathToStateMap.get(obbInfo.filename);
2154 }
Kenny Root38cf8862010-09-26 14:18:51 -07002155
Kenny Rootaf9d6672010-10-08 09:21:39 -07002156 if (obbState == null) {
2157 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_NOT_MOUNTED);
2158 return;
2159 }
2160
2161 if (obbState.callerUid != mObbState.callerUid) {
2162 Slog.w(TAG, "Permission denied attempting to unmount OBB " + obbInfo.filename
2163 + " (owned by " + obbInfo.packageName + ")");
2164 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2165 return;
2166 }
2167
2168 mObbState.filename = obbInfo.filename;
2169
2170 int rc = StorageResultCode.OperationSucceeded;
2171 String cmd = String.format("obb unmount %s%s", mObbState.filename,
2172 (mForceUnmount ? " force" : ""));
2173 try {
2174 mConnector.doCommand(cmd);
2175 } catch (NativeDaemonConnectorException e) {
2176 int code = e.getCode();
2177 if (code == VoldResponseCode.OpFailedStorageBusy) {
2178 rc = StorageResultCode.OperationFailedStorageBusy;
2179 } else if (code == VoldResponseCode.OpFailedStorageNotFound) {
2180 // If it's not mounted then we've already won.
2181 rc = StorageResultCode.OperationSucceeded;
2182 } else {
2183 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002184 }
2185 }
2186
Kenny Rootaf9d6672010-10-08 09:21:39 -07002187 if (rc == StorageResultCode.OperationSucceeded) {
2188 synchronized (mObbMounts) {
2189 removeObbStateLocked(obbState);
Kenny Root38cf8862010-09-26 14:18:51 -07002190 }
2191
Kenny Rootaf9d6672010-10-08 09:21:39 -07002192 sendNewStatusOrIgnore(OnObbStateChangeListener.UNMOUNTED);
Kenny Roota02b8b02010-08-05 16:14:17 -07002193 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002194 Slog.w(TAG, "Could not mount OBB: " + mObbState.filename);
2195 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT);
Kenny Roota02b8b02010-08-05 16:14:17 -07002196 }
2197 }
2198
Jason parks5af0b912010-11-29 09:05:25 -06002199 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002200 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002201 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Roota02b8b02010-08-05 16:14:17 -07002202 }
2203
2204 @Override
2205 public String toString() {
2206 StringBuilder sb = new StringBuilder();
2207 sb.append("UnmountObbAction{");
2208 sb.append("filename=");
2209 sb.append(mObbState.filename != null ? mObbState.filename : "null");
2210 sb.append(",force=");
2211 sb.append(mForceUnmount);
2212 sb.append(",callerUid=");
2213 sb.append(mObbState.callerUid);
2214 sb.append(",token=");
2215 sb.append(mObbState.token != null ? mObbState.token.toString() : "null");
Kenny Root735de3b2010-09-30 14:11:39 -07002216 sb.append(",binder=");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002217 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002218 sb.append('}');
2219 return sb.toString();
2220 }
Kenny Root02c87302010-07-01 08:10:18 -07002221 }
Kenny Root38cf8862010-09-26 14:18:51 -07002222
2223 @Override
2224 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2225 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
2226 pw.println("Permission Denial: can't dump ActivityManager from from pid="
2227 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
2228 + " without permission " + android.Manifest.permission.DUMP);
2229 return;
2230 }
2231
Kenny Root38cf8862010-09-26 14:18:51 -07002232 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002233 pw.println(" mObbMounts:");
Kenny Root38cf8862010-09-26 14:18:51 -07002234
Kenny Rootaf9d6672010-10-08 09:21:39 -07002235 final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet().iterator();
2236 while (binders.hasNext()) {
2237 Entry<IBinder, List<ObbState>> e = binders.next();
2238 pw.print(" Key="); pw.println(e.getKey().toString());
2239 final List<ObbState> obbStates = e.getValue();
Kenny Root38cf8862010-09-26 14:18:51 -07002240 for (final ObbState obbState : obbStates) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002241 pw.print(" "); pw.println(obbState.toString());
Kenny Root38cf8862010-09-26 14:18:51 -07002242 }
2243 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002244
2245 pw.println("");
2246 pw.println(" mObbPathToStateMap:");
2247 final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator();
2248 while (maps.hasNext()) {
2249 final Entry<String, ObbState> e = maps.next();
2250 pw.print(" "); pw.print(e.getKey());
2251 pw.print(" -> "); pw.println(e.getValue().toString());
2252 }
Kenny Root38cf8862010-09-26 14:18:51 -07002253 }
2254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255}
2256