blob: 4e80147d0e645135502b93014167403b75315d82 [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;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070021import com.android.server.pm.PackageManagerService;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080022
Jason parks8888c592011-01-20 22:46:41 -060023import android.Manifest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.BroadcastReceiver;
Kenny Roota02b8b02010-08-05 16:14:17 -070025import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
Kenny Roota02b8b02010-08-05 16:14:17 -070029import android.content.ServiceConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.pm.PackageManager;
Kenny Root02c87302010-07-01 08:10:18 -070031import android.content.res.ObbInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.net.Uri;
Kenny Root02c87302010-07-01 08:10:18 -070033import android.os.Binder;
Kenny Roota02b8b02010-08-05 16:14:17 -070034import android.os.Environment;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080035import android.os.Handler;
Daniel Sandler5f27ef42010-03-16 15:42:02 -040036import android.os.HandlerThread;
Kenny Roota02b8b02010-08-05 16:14:17 -070037import android.os.IBinder;
Daniel Sandler5f27ef42010-03-16 15:42:02 -040038import android.os.Looper;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080039import android.os.Message;
San Mehat4270e1e2010-01-29 05:32:19 -080040import android.os.RemoteException;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -080041import android.os.ServiceManager;
San Mehat207e5382010-02-04 20:46:54 -080042import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.SystemProperties;
Kenny Roota02b8b02010-08-05 16:14:17 -070044import android.os.storage.IMountService;
45import android.os.storage.IMountServiceListener;
46import android.os.storage.IMountShutdownObserver;
47import android.os.storage.IObbActionListener;
Kenny Rootaf9d6672010-10-08 09:21:39 -070048import android.os.storage.OnObbStateChangeListener;
Kenny Roota02b8b02010-08-05 16:14:17 -070049import android.os.storage.StorageResultCode;
Jason parksf7b3cd42011-01-27 09:28:25 -060050import android.text.TextUtils;
San Mehata5078592010-03-25 09:36:54 -070051import android.util.Slog;
Kenny Roota02b8b02010-08-05 16:14:17 -070052
Kenny Root38cf8862010-09-26 14:18:51 -070053import java.io.FileDescriptor;
Kenny Root05105f72010-09-22 17:29:43 -070054import java.io.IOException;
Kenny Root38cf8862010-09-26 14:18:51 -070055import java.io.PrintWriter;
Kenny Root3b1abba2010-10-13 15:00:07 -070056import java.math.BigInteger;
Kenny Root735de3b2010-09-30 14:11:39 -070057import java.security.NoSuchAlgorithmException;
Kenny Root3b1abba2010-10-13 15:00:07 -070058import java.security.spec.InvalidKeySpecException;
59import java.security.spec.KeySpec;
San Mehat22dd86e2010-01-12 12:21:18 -080060import java.util.ArrayList;
Kenny Roota02b8b02010-08-05 16:14:17 -070061import java.util.HashMap;
San Mehat6cdd9c02010-02-09 14:45:20 -080062import java.util.HashSet;
Kenny Root38cf8862010-09-26 14:18:51 -070063import java.util.Iterator;
Kenny Roota02b8b02010-08-05 16:14:17 -070064import java.util.LinkedList;
65import java.util.List;
66import java.util.Map;
Kenny Root38cf8862010-09-26 14:18:51 -070067import java.util.Map.Entry;
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
Kenny Rootcf0b38c2011-03-22 14:17:59 -070090 /** Maximum number of ASEC containers allowed to be mounted. */
91 private static final int MAX_CONTAINERS = 250;
92
San Mehat4270e1e2010-01-29 05:32:19 -080093 /*
94 * Internal vold volume state constants
95 */
San Mehat7fd0fee2009-12-17 07:12:23 -080096 class VolumeState {
97 public static final int Init = -1;
98 public static final int NoMedia = 0;
99 public static final int Idle = 1;
100 public static final int Pending = 2;
101 public static final int Checking = 3;
102 public static final int Mounted = 4;
103 public static final int Unmounting = 5;
104 public static final int Formatting = 6;
105 public static final int Shared = 7;
106 public static final int SharedMnt = 8;
107 }
108
San Mehat4270e1e2010-01-29 05:32:19 -0800109 /*
110 * Internal vold response code constants
111 */
San Mehat22dd86e2010-01-12 12:21:18 -0800112 class VoldResponseCode {
San Mehat4270e1e2010-01-29 05:32:19 -0800113 /*
114 * 100 series - Requestion action was initiated; expect another reply
115 * before proceeding with a new command.
116 */
San Mehat22dd86e2010-01-12 12:21:18 -0800117 public static final int VolumeListResult = 110;
118 public static final int AsecListResult = 111;
San Mehatc1b4ce92010-02-16 17:13:03 -0800119 public static final int StorageUsersListResult = 112;
San Mehat22dd86e2010-01-12 12:21:18 -0800120
San Mehat4270e1e2010-01-29 05:32:19 -0800121 /*
122 * 200 series - Requestion action has been successfully completed.
123 */
124 public static final int ShareStatusResult = 210;
San Mehat22dd86e2010-01-12 12:21:18 -0800125 public static final int AsecPathResult = 211;
San Mehat4270e1e2010-01-29 05:32:19 -0800126 public static final int ShareEnabledResult = 212;
San Mehat22dd86e2010-01-12 12:21:18 -0800127
San Mehat4270e1e2010-01-29 05:32:19 -0800128 /*
129 * 400 series - Command was accepted, but the requested action
130 * did not take place.
131 */
132 public static final int OpFailedNoMedia = 401;
133 public static final int OpFailedMediaBlank = 402;
134 public static final int OpFailedMediaCorrupt = 403;
135 public static final int OpFailedVolNotMounted = 404;
San Mehatd9709982010-02-18 11:43:03 -0800136 public static final int OpFailedStorageBusy = 405;
San Mehat2d66cef2010-03-23 11:12:52 -0700137 public static final int OpFailedStorageNotFound = 406;
San Mehat4270e1e2010-01-29 05:32:19 -0800138
139 /*
140 * 600 series - Unsolicited broadcasts.
141 */
San Mehat22dd86e2010-01-12 12:21:18 -0800142 public static final int VolumeStateChange = 605;
San Mehat22dd86e2010-01-12 12:21:18 -0800143 public static final int ShareAvailabilityChange = 620;
144 public static final int VolumeDiskInserted = 630;
145 public static final int VolumeDiskRemoved = 631;
146 public static final int VolumeBadRemoval = 632;
147 }
148
San Mehat4270e1e2010-01-29 05:32:19 -0800149 private Context mContext;
150 private NativeDaemonConnector mConnector;
151 private String mLegacyState = Environment.MEDIA_REMOVED;
152 private PackageManagerService mPms;
153 private boolean mUmsEnabling;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800154 // Used as a lock for methods that register/unregister listeners.
155 final private ArrayList<MountServiceBinderListener> mListeners =
156 new ArrayList<MountServiceBinderListener>();
San Mehat6a965af22010-02-24 17:47:30 -0800157 private boolean mBooted = false;
158 private boolean mReady = false;
159 private boolean mSendUmsConnectedOnBoot = false;
Mike Lockwood03559752010-07-19 18:25:03 -0400160 // true if we should fake MEDIA_MOUNTED state for external storage
161 private boolean mEmulateExternalStorage = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800162
San Mehat6cdd9c02010-02-09 14:45:20 -0800163 /**
164 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800165 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800166 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800167 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800168
Kenny Root02c87302010-07-01 08:10:18 -0700169 /**
Kenny Root3b1abba2010-10-13 15:00:07 -0700170 * The size of the crypto algorithm key in bits for OBB files. Currently
171 * Twofish is used which takes 128-bit keys.
172 */
173 private static final int CRYPTO_ALGORITHM_KEY_SIZE = 128;
174
175 /**
176 * The number of times to run SHA1 in the PBKDF2 function for OBB files.
177 * 1024 is reasonably secure and not too slow.
178 */
179 private static final int PBKDF2_HASH_ROUNDS = 1024;
180
181 /**
Kenny Roota02b8b02010-08-05 16:14:17 -0700182 * Mounted OBB tracking information. Used to track the current state of all
183 * OBBs.
Kenny Root02c87302010-07-01 08:10:18 -0700184 */
Kenny Root735de3b2010-09-30 14:11:39 -0700185 final private Map<IBinder, List<ObbState>> mObbMounts = new HashMap<IBinder, List<ObbState>>();
Kenny Roota02b8b02010-08-05 16:14:17 -0700186 final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>();
187
188 class ObbState implements IBinder.DeathRecipient {
Kenny Rootaf9d6672010-10-08 09:21:39 -0700189 public ObbState(String filename, int callerUid, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -0700190 throws RemoteException {
Kenny Roota02b8b02010-08-05 16:14:17 -0700191 this.filename = filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700192 this.callerUid = callerUid;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700193 this.token = token;
194 this.nonce = nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700195 }
196
197 // OBB source filename
Kenny Rootaf9d6672010-10-08 09:21:39 -0700198 String filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700199
200 // Binder.callingUid()
Kenny Root05105f72010-09-22 17:29:43 -0700201 final public int callerUid;
Kenny Roota02b8b02010-08-05 16:14:17 -0700202
Kenny Rootaf9d6672010-10-08 09:21:39 -0700203 // Token of remote Binder caller
204 final IObbActionListener token;
205
206 // Identifier to pass back to the token
207 final int nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700208
Kenny Root735de3b2010-09-30 14:11:39 -0700209 public IBinder getBinder() {
210 return token.asBinder();
211 }
212
Kenny Roota02b8b02010-08-05 16:14:17 -0700213 @Override
214 public void binderDied() {
215 ObbAction action = new UnmountObbAction(this, true);
216 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root735de3b2010-09-30 14:11:39 -0700217 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700218
Kenny Root5919ac62010-10-05 09:49:40 -0700219 public void link() throws RemoteException {
220 getBinder().linkToDeath(this, 0);
221 }
222
223 public void unlink() {
Kenny Root735de3b2010-09-30 14:11:39 -0700224 getBinder().unlinkToDeath(this, 0);
Kenny Roota02b8b02010-08-05 16:14:17 -0700225 }
Kenny Root38cf8862010-09-26 14:18:51 -0700226
227 @Override
228 public String toString() {
229 StringBuilder sb = new StringBuilder("ObbState{");
230 sb.append("filename=");
231 sb.append(filename);
232 sb.append(",token=");
233 sb.append(token.toString());
234 sb.append(",callerUid=");
235 sb.append(callerUid);
Kenny Root38cf8862010-09-26 14:18:51 -0700236 sb.append('}');
237 return sb.toString();
238 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700239 }
240
241 // OBB Action Handler
242 final private ObbActionHandler mObbActionHandler;
243
244 // OBB action handler messages
245 private static final int OBB_RUN_ACTION = 1;
246 private static final int OBB_MCS_BOUND = 2;
247 private static final int OBB_MCS_UNBIND = 3;
248 private static final int OBB_MCS_RECONNECT = 4;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700249 private static final int OBB_FLUSH_MOUNT_STATE = 5;
Kenny Roota02b8b02010-08-05 16:14:17 -0700250
251 /*
252 * Default Container Service information
253 */
254 static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
255 "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService");
256
257 final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection();
258
259 class DefaultContainerConnection implements ServiceConnection {
260 public void onServiceConnected(ComponentName name, IBinder service) {
261 if (DEBUG_OBB)
262 Slog.i(TAG, "onServiceConnected");
263 IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service);
264 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs));
265 }
266
267 public void onServiceDisconnected(ComponentName name) {
268 if (DEBUG_OBB)
269 Slog.i(TAG, "onServiceDisconnected");
270 }
271 };
272
273 // Used in the ObbActionHandler
274 private IMediaContainerService mContainerService = null;
Kenny Root02c87302010-07-01 08:10:18 -0700275
276 // Handler messages
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800277 private static final int H_UNMOUNT_PM_UPDATE = 1;
278 private static final int H_UNMOUNT_PM_DONE = 2;
279 private static final int H_UNMOUNT_MS = 3;
280 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
281 private static final int MAX_UNMOUNT_RETRIES = 4;
282
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800283 class UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700284 final String path;
285 final boolean force;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800286 int retries;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800287
288 UnmountCallBack(String path, boolean force) {
289 retries = 0;
290 this.path = path;
291 this.force = force;
292 }
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800293
294 void handleFinished() {
San Mehata5078592010-03-25 09:36:54 -0700295 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path);
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800296 doUnmountVolume(path, true);
297 }
298 }
299
300 class UmsEnableCallBack extends UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700301 final String method;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800302
303 UmsEnableCallBack(String path, String method, boolean force) {
304 super(path, force);
305 this.method = method;
306 }
307
308 @Override
309 void handleFinished() {
310 super.handleFinished();
311 doShareUnshareVolume(path, method, true);
312 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800313 }
314
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800315 class ShutdownCallBack extends UnmountCallBack {
316 IMountShutdownObserver observer;
317 ShutdownCallBack(String path, IMountShutdownObserver observer) {
318 super(path, true);
319 this.observer = observer;
320 }
321
322 @Override
323 void handleFinished() {
324 int ret = doUnmountVolume(path, true);
325 if (observer != null) {
326 try {
327 observer.onShutDownComplete(ret);
328 } catch (RemoteException e) {
San Mehata5078592010-03-25 09:36:54 -0700329 Slog.w(TAG, "RemoteException when shutting down");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800330 }
331 }
332 }
333 }
334
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400335 class MountServiceHandler extends Handler {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800336 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700337 boolean mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800338
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400339 MountServiceHandler(Looper l) {
340 super(l);
341 }
342
Jason parks5af0b912010-11-29 09:05:25 -0600343 @Override
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800344 public void handleMessage(Message msg) {
345 switch (msg.what) {
346 case H_UNMOUNT_PM_UPDATE: {
San Mehata5078592010-03-25 09:36:54 -0700347 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800348 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
349 mForceUnmounts.add(ucb);
San Mehata5078592010-03-25 09:36:54 -0700350 if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800351 // Register only if needed.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700352 if (!mUpdatingStatus) {
San Mehata5078592010-03-25 09:36:54 -0700353 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700354 mUpdatingStatus = true;
355 mPms.updateExternalMediaStatus(false, true);
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800356 }
357 break;
358 }
359 case H_UNMOUNT_PM_DONE: {
San Mehata5078592010-03-25 09:36:54 -0700360 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE");
San Mehata5078592010-03-25 09:36:54 -0700361 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700362 mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800363 int size = mForceUnmounts.size();
364 int sizeArr[] = new int[size];
365 int sizeArrN = 0;
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700366 // Kill processes holding references first
367 ActivityManagerService ams = (ActivityManagerService)
368 ServiceManager.getService("activity");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800369 for (int i = 0; i < size; i++) {
370 UnmountCallBack ucb = mForceUnmounts.get(i);
371 String path = ucb.path;
372 boolean done = false;
373 if (!ucb.force) {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800374 done = true;
375 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800376 int pids[] = getStorageUsers(path);
377 if (pids == null || pids.length == 0) {
378 done = true;
379 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800380 // Eliminate system process here?
Dianne Hackborn64825172011-03-02 21:32:58 -0800381 ams.killPids(pids, "unmount media", true);
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700382 // Confirm if file references have been freed.
383 pids = getStorageUsers(path);
384 if (pids == null || pids.length == 0) {
385 done = true;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800386 }
387 }
388 }
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700389 if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) {
390 // Retry again
391 Slog.i(TAG, "Retrying to kill storage users again");
392 mHandler.sendMessageDelayed(
393 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
394 ucb.retries++),
395 RETRY_UNMOUNT_DELAY);
396 } else {
397 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
398 Slog.i(TAG, "Failed to unmount media inspite of " +
399 MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now");
400 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800401 sizeArr[sizeArrN++] = i;
402 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
403 ucb));
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800404 }
405 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800406 // Remove already processed elements from list.
407 for (int i = (sizeArrN-1); i >= 0; i--) {
408 mForceUnmounts.remove(sizeArr[i]);
409 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800410 break;
411 }
412 case H_UNMOUNT_MS : {
San Mehata5078592010-03-25 09:36:54 -0700413 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800414 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800415 ucb.handleFinished();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800416 break;
417 }
418 }
419 }
420 };
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400421 final private HandlerThread mHandlerThread;
422 final private Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800423
San Mehat207e5382010-02-04 20:46:54 -0800424 private void waitForReady() {
425 while (mReady == false) {
426 for (int retries = 5; retries > 0; retries--) {
427 if (mReady) {
428 return;
429 }
430 SystemClock.sleep(1000);
431 }
San Mehata5078592010-03-25 09:36:54 -0700432 Slog.w(TAG, "Waiting too long for mReady!");
San Mehat207e5382010-02-04 20:46:54 -0800433 }
San Mehat1f6301e2010-01-07 22:40:27 -0800434 }
Kenny Root02c87302010-07-01 08:10:18 -0700435
San Mehat207e5382010-02-04 20:46:54 -0800436 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Jason parks5af0b912010-11-29 09:05:25 -0600437 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 public void onReceive(Context context, Intent intent) {
San Mehat91c77612010-01-07 10:39:41 -0800439 String action = intent.getAction();
440
441 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
San Mehat207e5382010-02-04 20:46:54 -0800442 mBooted = true;
San Mehat22dd86e2010-01-12 12:21:18 -0800443
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800444 /*
445 * In the simulator, we need to broadcast a volume mounted event
446 * to make the media scanner run.
447 */
448 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
449 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted);
450 return;
451 }
San Mehatfafb0412010-02-18 19:40:04 -0800452 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600453 @Override
San Mehatfafb0412010-02-18 19:40:04 -0800454 public void run() {
455 try {
456 String path = Environment.getExternalStorageDirectory().getPath();
San Mehat6a254402010-03-22 10:21:00 -0700457 String state = getVolumeState(path);
458
Mike Lockwood03559752010-07-19 18:25:03 -0400459 if (mEmulateExternalStorage) {
460 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Mounted);
461 } else if (state.equals(Environment.MEDIA_UNMOUNTED)) {
San Mehatfafb0412010-02-18 19:40:04 -0800462 int rc = doMountVolume(path);
463 if (rc != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700464 Slog.e(TAG, String.format("Boot-time mount failed (%d)", rc));
San Mehatfafb0412010-02-18 19:40:04 -0800465 }
San Mehat6a254402010-03-22 10:21:00 -0700466 } else if (state.equals(Environment.MEDIA_SHARED)) {
467 /*
468 * Bootstrap UMS enabled state since vold indicates
469 * the volume is shared (runtime restart while ums enabled)
470 */
471 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Shared);
San Mehatfafb0412010-02-18 19:40:04 -0800472 }
San Mehat6a254402010-03-22 10:21:00 -0700473
San Mehat6a965af22010-02-24 17:47:30 -0800474 /*
San Mehat6a254402010-03-22 10:21:00 -0700475 * If UMS was connected on boot, send the connected event
San Mehat6a965af22010-02-24 17:47:30 -0800476 * now that we're up.
477 */
478 if (mSendUmsConnectedOnBoot) {
479 sendUmsIntent(true);
480 mSendUmsConnectedOnBoot = false;
481 }
San Mehatfafb0412010-02-18 19:40:04 -0800482 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700483 Slog.e(TAG, "Boot-time mount exception", ex);
San Mehatfafb0412010-02-18 19:40:04 -0800484 }
San Mehat207e5382010-02-04 20:46:54 -0800485 }
San Mehatfafb0412010-02-18 19:40:04 -0800486 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 }
488 }
489 };
San Mehat4270e1e2010-01-29 05:32:19 -0800490 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
491 final IMountServiceListener mListener;
492
493 MountServiceBinderListener(IMountServiceListener listener) {
494 mListener = listener;
Kenny Root02c87302010-07-01 08:10:18 -0700495
San Mehat91c77612010-01-07 10:39:41 -0800496 }
497
San Mehat4270e1e2010-01-29 05:32:19 -0800498 public void binderDied() {
San Mehata5078592010-03-25 09:36:54 -0700499 if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!");
Kenny Roota02b8b02010-08-05 16:14:17 -0700500 synchronized (mListeners) {
San Mehat4270e1e2010-01-29 05:32:19 -0800501 mListeners.remove(this);
502 mListener.asBinder().unlinkToDeath(this, 0);
503 }
504 }
505 }
506
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800507 private void doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800508 // TODO: Add support for multiple share methods
509 if (!method.equals("ums")) {
510 throw new IllegalArgumentException(String.format("Method %s not supported", method));
511 }
512
San Mehat4270e1e2010-01-29 05:32:19 -0800513 try {
514 mConnector.doCommand(String.format(
515 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
516 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -0700517 Slog.e(TAG, "Failed to share/unshare", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800518 }
San Mehat4270e1e2010-01-29 05:32:19 -0800519 }
520
San Mehat207e5382010-02-04 20:46:54 -0800521 private void updatePublicVolumeState(String path, String state) {
San Mehat4270e1e2010-01-29 05:32:19 -0800522 if (!path.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -0700523 Slog.w(TAG, "Multiple volumes not currently supported");
San Mehat4270e1e2010-01-29 05:32:19 -0800524 return;
525 }
San Mehatb1043402010-02-05 08:26:50 -0800526
527 if (mLegacyState.equals(state)) {
San Mehata5078592010-03-25 09:36:54 -0700528 Slog.w(TAG, String.format("Duplicate state transition (%s -> %s)", mLegacyState, state));
San Mehatb1043402010-02-05 08:26:50 -0800529 return;
530 }
Mike Lockwood03559752010-07-19 18:25:03 -0400531 // Update state on PackageManager, but only of real events
532 if (!mEmulateExternalStorage) {
533 if (Environment.MEDIA_UNMOUNTED.equals(state)) {
534 mPms.updateExternalMediaStatus(false, false);
Kenny Rootaf9d6672010-10-08 09:21:39 -0700535
Kenny Root0a9b54e2010-10-13 15:43:35 -0700536 /*
537 * Some OBBs might have been unmounted when this volume was
538 * unmounted, so send a message to the handler to let it know to
539 * remove those from the list of mounted OBBS.
540 */
541 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_FLUSH_MOUNT_STATE,
542 path));
Mike Lockwood03559752010-07-19 18:25:03 -0400543 } else if (Environment.MEDIA_MOUNTED.equals(state)) {
544 mPms.updateExternalMediaStatus(true, false);
545 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800546 }
Kenny Root38cf8862010-09-26 14:18:51 -0700547
San Mehat4270e1e2010-01-29 05:32:19 -0800548 String oldState = mLegacyState;
549 mLegacyState = state;
550
551 synchronized (mListeners) {
552 for (int i = mListeners.size() -1; i >= 0; i--) {
553 MountServiceBinderListener bl = mListeners.get(i);
554 try {
San Mehatb1043402010-02-05 08:26:50 -0800555 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800556 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700557 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800558 mListeners.remove(i);
559 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700560 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800561 }
562 }
563 }
564 }
565
566 /**
567 *
568 * Callback from NativeDaemonConnector
569 */
570 public void onDaemonConnected() {
571 /*
572 * Since we'll be calling back into the NativeDaemonConnector,
573 * we need to do our work in a new thread.
574 */
575 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600576 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800577 public void run() {
578 /**
579 * Determine media state and UMS detection status
580 */
581 String path = Environment.getExternalStorageDirectory().getPath();
582 String state = Environment.MEDIA_REMOVED;
583
584 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>
590 if (!tok[1].equals(path)) {
San Mehata5078592010-03-25 09:36:54 -0700591 Slog.w(TAG, String.format(
San Mehat4270e1e2010-01-29 05:32:19 -0800592 "Skipping unknown volume '%s'",tok[1]));
593 continue;
594 }
595 int st = Integer.parseInt(tok[2]);
596 if (st == VolumeState.NoMedia) {
597 state = Environment.MEDIA_REMOVED;
598 } else if (st == VolumeState.Idle) {
San Mehat207e5382010-02-04 20:46:54 -0800599 state = Environment.MEDIA_UNMOUNTED;
San Mehat4270e1e2010-01-29 05:32:19 -0800600 } else if (st == VolumeState.Mounted) {
601 state = Environment.MEDIA_MOUNTED;
San Mehata5078592010-03-25 09:36:54 -0700602 Slog.i(TAG, "Media already mounted on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800603 } else if (st == VolumeState.Shared) {
604 state = Environment.MEDIA_SHARED;
San Mehata5078592010-03-25 09:36:54 -0700605 Slog.i(TAG, "Media shared on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800606 } else {
607 throw new Exception(String.format("Unexpected state %d", st));
608 }
609 }
610 if (state != null) {
San Mehata5078592010-03-25 09:36:54 -0700611 if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
San Mehat4270e1e2010-01-29 05:32:19 -0800612 updatePublicVolumeState(path, state);
613 }
614 } catch (Exception e) {
San Mehata5078592010-03-25 09:36:54 -0700615 Slog.e(TAG, "Error processing initial volume state", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800616 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
617 }
618
619 try {
San Mehat207e5382010-02-04 20:46:54 -0800620 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800621 notifyShareAvailabilityChange("ums", avail);
622 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700623 Slog.w(TAG, "Failed to get share availability");
San Mehat4270e1e2010-01-29 05:32:19 -0800624 }
San Mehat207e5382010-02-04 20:46:54 -0800625 /*
Jason parks9ed98bc2011-01-17 09:58:35 -0600626 * Now that we've done our initialization, release
San Mehat207e5382010-02-04 20:46:54 -0800627 * the hounds!
628 */
629 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800630 }
631 }.start();
632 }
633
634 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800635 * Callback from NativeDaemonConnector
636 */
637 public boolean onEvent(int code, String raw, String[] cooked) {
638 Intent in = null;
639
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800640 if (DEBUG_EVENTS) {
641 StringBuilder builder = new StringBuilder();
642 builder.append("onEvent::");
643 builder.append(" raw= " + raw);
644 if (cooked != null) {
645 builder.append(" cooked = " );
646 for (String str : cooked) {
647 builder.append(" " + str);
648 }
649 }
San Mehata5078592010-03-25 09:36:54 -0700650 Slog.i(TAG, builder.toString());
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800651 }
San Mehat4270e1e2010-01-29 05:32:19 -0800652 if (code == VoldResponseCode.VolumeStateChange) {
653 /*
654 * One of the volumes we're managing has changed state.
655 * Format: "NNN Volume <label> <path> state changed
656 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
657 */
658 notifyVolumeStateChange(
659 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
660 Integer.parseInt(cooked[10]));
661 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
662 // FMT: NNN Share method <method> now <available|unavailable>
663 boolean avail = false;
664 if (cooked[5].equals("available")) {
665 avail = true;
666 }
667 notifyShareAvailabilityChange(cooked[3], avail);
668 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
669 (code == VoldResponseCode.VolumeDiskRemoved) ||
670 (code == VoldResponseCode.VolumeBadRemoval)) {
671 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
672 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
673 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
674 final String label = cooked[2];
675 final String path = cooked[3];
676 int major = -1;
677 int minor = -1;
678
679 try {
680 String devComp = cooked[6].substring(1, cooked[6].length() -1);
681 String[] devTok = devComp.split(":");
682 major = Integer.parseInt(devTok[0]);
683 minor = Integer.parseInt(devTok[1]);
684 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700685 Slog.e(TAG, "Failed to parse major/minor", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800686 }
687
San Mehat4270e1e2010-01-29 05:32:19 -0800688 if (code == VoldResponseCode.VolumeDiskInserted) {
689 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600690 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800691 public void run() {
692 try {
693 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800694 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700695 Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
San Mehat4270e1e2010-01-29 05:32:19 -0800696 }
697 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700698 Slog.w(TAG, "Failed to mount media on insertion", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800699 }
700 }
701 }.start();
702 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
703 /*
704 * This event gets trumped if we're already in BAD_REMOVAL state
705 */
706 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
707 return true;
708 }
709 /* Send the media unmounted event first */
San Mehata5078592010-03-25 09:36:54 -0700710 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800711 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
712 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
713 mContext.sendBroadcast(in);
714
San Mehata5078592010-03-25 09:36:54 -0700715 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
San Mehat4270e1e2010-01-29 05:32:19 -0800716 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
717 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
718 } else if (code == VoldResponseCode.VolumeBadRemoval) {
San Mehata5078592010-03-25 09:36:54 -0700719 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800720 /* Send the media unmounted event first */
721 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
722 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
723 mContext.sendBroadcast(in);
724
San Mehata5078592010-03-25 09:36:54 -0700725 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
San Mehat4270e1e2010-01-29 05:32:19 -0800726 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
727 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
728 } else {
San Mehata5078592010-03-25 09:36:54 -0700729 Slog.e(TAG, String.format("Unknown code {%d}", code));
San Mehat4270e1e2010-01-29 05:32:19 -0800730 }
731 } else {
732 return false;
733 }
734
735 if (in != null) {
736 mContext.sendBroadcast(in);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400737 }
738 return true;
San Mehat4270e1e2010-01-29 05:32:19 -0800739 }
740
San Mehat207e5382010-02-04 20:46:54 -0800741 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800742 String vs = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -0700743 if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
San Mehat4270e1e2010-01-29 05:32:19 -0800744
745 Intent in = null;
746
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500747 if (oldState == VolumeState.Shared && newState != oldState) {
San Mehata5078592010-03-25 09:36:54 -0700748 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500749 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
750 Uri.parse("file://" + path)));
751 }
752
San Mehat4270e1e2010-01-29 05:32:19 -0800753 if (newState == VolumeState.Init) {
754 } else if (newState == VolumeState.NoMedia) {
755 // NoMedia is handled via Disk Remove events
756 } else if (newState == VolumeState.Idle) {
757 /*
758 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
759 * if we're in the process of enabling UMS
760 */
761 if (!vs.equals(
762 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
763 Environment.MEDIA_NOFS) && !vs.equals(
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800764 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
San Mehata5078592010-03-25 09:36:54 -0700765 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
San Mehat4270e1e2010-01-29 05:32:19 -0800766 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
767 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
768 }
769 } else if (newState == VolumeState.Pending) {
770 } else if (newState == VolumeState.Checking) {
San Mehata5078592010-03-25 09:36:54 -0700771 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
San Mehat4270e1e2010-01-29 05:32:19 -0800772 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
773 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
774 } else if (newState == VolumeState.Mounted) {
San Mehata5078592010-03-25 09:36:54 -0700775 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800776 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
San Mehat4270e1e2010-01-29 05:32:19 -0800777 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
778 in.putExtra("read-only", false);
779 } else if (newState == VolumeState.Unmounting) {
San Mehat4270e1e2010-01-29 05:32:19 -0800780 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
781 } else if (newState == VolumeState.Formatting) {
782 } else if (newState == VolumeState.Shared) {
San Mehata5078592010-03-25 09:36:54 -0700783 if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800784 /* Send the media unmounted event first */
785 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
786 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
787 mContext.sendBroadcast(in);
788
San Mehata5078592010-03-25 09:36:54 -0700789 if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
San Mehat4270e1e2010-01-29 05:32:19 -0800790 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
791 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
San Mehata5078592010-03-25 09:36:54 -0700792 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
San Mehat4270e1e2010-01-29 05:32:19 -0800793 } else if (newState == VolumeState.SharedMnt) {
San Mehata5078592010-03-25 09:36:54 -0700794 Slog.e(TAG, "Live shared mounts not supported yet!");
San Mehat4270e1e2010-01-29 05:32:19 -0800795 return;
796 } else {
San Mehata5078592010-03-25 09:36:54 -0700797 Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800798 }
799
800 if (in != null) {
801 mContext.sendBroadcast(in);
802 }
803 }
804
San Mehat207e5382010-02-04 20:46:54 -0800805 private boolean doGetShareMethodAvailable(String method) {
Kenny Root85fb2062010-06-01 20:50:21 -0700806 ArrayList<String> rsp;
Kenny Roota80ce062010-06-01 13:23:53 -0700807 try {
Kenny Root85fb2062010-06-01 20:50:21 -0700808 rsp = mConnector.doCommand("share status " + method);
Kenny Roota80ce062010-06-01 13:23:53 -0700809 } catch (NativeDaemonConnectorException ex) {
810 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
811 return false;
812 }
San Mehat207e5382010-02-04 20:46:54 -0800813
814 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700815 String[] tok = line.split(" ");
816 if (tok.length < 3) {
817 Slog.e(TAG, "Malformed response to share status " + method);
818 return false;
819 }
820
San Mehat207e5382010-02-04 20:46:54 -0800821 int code;
822 try {
823 code = Integer.parseInt(tok[0]);
824 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700825 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehat207e5382010-02-04 20:46:54 -0800826 return false;
827 }
828 if (code == VoldResponseCode.ShareStatusResult) {
829 if (tok[2].equals("available"))
830 return true;
831 return false;
832 } else {
San Mehata5078592010-03-25 09:36:54 -0700833 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehat207e5382010-02-04 20:46:54 -0800834 return false;
835 }
836 }
San Mehata5078592010-03-25 09:36:54 -0700837 Slog.e(TAG, "Got an empty response");
San Mehat207e5382010-02-04 20:46:54 -0800838 return false;
839 }
840
841 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800842 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800843
San Mehata5078592010-03-25 09:36:54 -0700844 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
San Mehat207e5382010-02-04 20:46:54 -0800845 try {
846 mConnector.doCommand(String.format("volume mount %s", path));
847 } catch (NativeDaemonConnectorException e) {
848 /*
849 * Mount failed for some reason
850 */
851 Intent in = null;
852 int code = e.getCode();
853 if (code == VoldResponseCode.OpFailedNoMedia) {
854 /*
855 * Attempt to mount but no media inserted
856 */
San Mehatb1043402010-02-05 08:26:50 -0800857 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800858 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
San Mehata5078592010-03-25 09:36:54 -0700859 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
San Mehat207e5382010-02-04 20:46:54 -0800860 /*
861 * Media is blank or does not contain a supported filesystem
862 */
863 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
864 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800865 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800866 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehata5078592010-03-25 09:36:54 -0700867 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
San Mehat207e5382010-02-04 20:46:54 -0800868 /*
869 * Volume consistency check failed
870 */
871 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
872 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800873 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800874 } else {
San Mehatb1043402010-02-05 08:26:50 -0800875 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800876 }
877
878 /*
879 * Send broadcast intent (if required for the failure)
880 */
881 if (in != null) {
882 mContext.sendBroadcast(in);
883 }
884 }
885
886 return rc;
887 }
888
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800889 /*
890 * If force is not set, we do not unmount if there are
891 * processes holding references to the volume about to be unmounted.
892 * If force is set, all the processes holding references need to be
893 * killed via the ActivityManager before actually unmounting the volume.
894 * This might even take a while and might be retried after timed delays
895 * to make sure we dont end up in an instable state and kill some core
896 * processes.
897 */
San Mehatd9709982010-02-18 11:43:03 -0800898 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800899 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800900 return VoldResponseCode.OpFailedVolNotMounted;
901 }
Kenny Rootaa485402010-09-14 14:49:41 -0700902
903 /*
904 * Force a GC to make sure AssetManagers in other threads of the
905 * system_server are cleaned up. We have to do this since AssetManager
906 * instances are kept as a WeakReference and it's possible we have files
907 * open on the external storage.
908 */
909 Runtime.getRuntime().gc();
910
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800911 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700912 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800913 try {
San Mehatd9709982010-02-18 11:43:03 -0800914 mConnector.doCommand(String.format(
915 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700916 // We unmounted the volume. None of the asec containers are available now.
917 synchronized (mAsecMountSet) {
918 mAsecMountSet.clear();
919 }
San Mehatb1043402010-02-05 08:26:50 -0800920 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800921 } catch (NativeDaemonConnectorException e) {
922 // Don't worry about mismatch in PackageManager since the
923 // call back will handle the status changes any way.
924 int code = e.getCode();
925 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800926 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800927 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
928 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800929 } else {
San Mehatb1043402010-02-05 08:26:50 -0800930 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800931 }
932 }
933 }
934
935 private int doFormatVolume(String path) {
936 try {
937 String cmd = String.format("volume format %s", path);
938 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800939 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800940 } catch (NativeDaemonConnectorException e) {
941 int code = e.getCode();
942 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800943 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800944 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800945 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800946 } else {
San Mehatb1043402010-02-05 08:26:50 -0800947 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800948 }
949 }
950 }
951
San Mehatb1043402010-02-05 08:26:50 -0800952 private boolean doGetVolumeShared(String path, String method) {
953 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700954 ArrayList<String> rsp;
955
956 try {
957 rsp = mConnector.doCommand(cmd);
958 } catch (NativeDaemonConnectorException ex) {
959 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
960 return false;
961 }
San Mehatb1043402010-02-05 08:26:50 -0800962
963 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700964 String[] tok = line.split(" ");
965 if (tok.length < 3) {
966 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
967 return false;
968 }
969
San Mehatb1043402010-02-05 08:26:50 -0800970 int code;
971 try {
972 code = Integer.parseInt(tok[0]);
973 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700974 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800975 return false;
976 }
977 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700978 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800979 } else {
San Mehata5078592010-03-25 09:36:54 -0700980 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800981 return false;
982 }
983 }
San Mehata5078592010-03-25 09:36:54 -0700984 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800985 return false;
986 }
987
San Mehat207e5382010-02-04 20:46:54 -0800988 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800989 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -0700990 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800991 return;
992 }
993
994 synchronized (mListeners) {
995 for (int i = mListeners.size() -1; i >= 0; i--) {
996 MountServiceBinderListener bl = mListeners.get(i);
997 try {
San Mehatb1043402010-02-05 08:26:50 -0800998 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -0800999 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001000 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -08001001 mListeners.remove(i);
1002 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -07001003 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -08001004 }
1005 }
1006 }
1007
San Mehat207e5382010-02-04 20:46:54 -08001008 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -08001009 sendUmsIntent(avail);
1010 } else {
1011 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -08001012 }
San Mehat2fe718a2010-03-11 12:01:49 -08001013
1014 final String path = Environment.getExternalStorageDirectory().getPath();
1015 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
1016 /*
1017 * USB mass storage disconnected while enabled
1018 */
1019 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -06001020 @Override
San Mehat2fe718a2010-03-11 12:01:49 -08001021 public void run() {
1022 try {
1023 int rc;
San Mehata5078592010-03-25 09:36:54 -07001024 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -08001025 doShareUnshareVolume(path, "ums", false);
1026 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001027 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -08001028 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
1029 path, rc));
1030 }
1031 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -07001032 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -08001033 }
1034 }
1035 }.start();
1036 }
San Mehat4270e1e2010-01-29 05:32:19 -08001037 }
1038
San Mehat6a965af22010-02-24 17:47:30 -08001039 private void sendUmsIntent(boolean c) {
1040 mContext.sendBroadcast(
1041 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
1042 }
1043
San Mehat207e5382010-02-04 20:46:54 -08001044 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -08001045 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
1046 throw new SecurityException(String.format("Requires %s permission", perm));
1047 }
1048 }
1049
1050 /**
San Mehat207e5382010-02-04 20:46:54 -08001051 * Constructs a new MountService instance
1052 *
1053 * @param context Binder context for this service
1054 */
1055 public MountService(Context context) {
1056 mContext = context;
1057
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");
1062 mLegacyState = Environment.MEDIA_MOUNTED;
1063 }
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 */
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001093 mConnector = new NativeDaemonConnector(this, "vold", MAX_CONTAINERS * 2, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001094 mReady = false;
Kenny Root305bcbf2010-09-03 07:56:38 -07001095 Thread thread = new Thread(mConnector, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001096 thread.start();
1097 }
1098
1099 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001100 * Exposed API calls below here
1101 */
1102
1103 public void registerListener(IMountServiceListener listener) {
1104 synchronized (mListeners) {
1105 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
1106 try {
1107 listener.asBinder().linkToDeath(bl, 0);
1108 mListeners.add(bl);
1109 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001110 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -08001111 }
1112 }
1113 }
1114
1115 public void unregisterListener(IMountServiceListener listener) {
1116 synchronized (mListeners) {
1117 for(MountServiceBinderListener bl : mListeners) {
1118 if (bl.mListener == listener) {
1119 mListeners.remove(mListeners.indexOf(bl));
1120 return;
1121 }
1122 }
1123 }
1124 }
1125
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001126 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -08001127 validatePermission(android.Manifest.permission.SHUTDOWN);
1128
San Mehata5078592010-03-25 09:36:54 -07001129 Slog.i(TAG, "Shutting down");
San Mehat4270e1e2010-01-29 05:32:19 -08001130
1131 String path = Environment.getExternalStorageDirectory().getPath();
1132 String state = getVolumeState(path);
San Mehat91c77612010-01-07 10:39:41 -08001133
1134 if (state.equals(Environment.MEDIA_SHARED)) {
1135 /*
1136 * If the media is currently shared, unshare it.
1137 * XXX: This is still dangerous!. We should not
1138 * be rebooting at *all* if UMS is enabled, since
1139 * the UMS host could have dirty FAT cache entries
1140 * yet to flush.
1141 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001142 setUsbMassStorageEnabled(false);
San Mehat91c77612010-01-07 10:39:41 -08001143 } else if (state.equals(Environment.MEDIA_CHECKING)) {
1144 /*
1145 * If the media is being checked, then we need to wait for
1146 * it to complete before being able to proceed.
1147 */
1148 // XXX: @hackbod - Should we disable the ANR timer here?
1149 int retries = 30;
1150 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
1151 try {
1152 Thread.sleep(1000);
1153 } catch (InterruptedException iex) {
San Mehata5078592010-03-25 09:36:54 -07001154 Slog.e(TAG, "Interrupted while waiting for media", iex);
San Mehat91c77612010-01-07 10:39:41 -08001155 break;
1156 }
1157 state = Environment.getExternalStorageState();
1158 }
1159 if (retries == 0) {
San Mehata5078592010-03-25 09:36:54 -07001160 Slog.e(TAG, "Timed out waiting for media to check");
San Mehat91c77612010-01-07 10:39:41 -08001161 }
1162 }
1163
1164 if (state.equals(Environment.MEDIA_MOUNTED)) {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001165 // Post a unmount message.
1166 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
1167 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
Johan Alfven5d0db4d2010-11-09 10:32:25 +01001168 } else if (observer != null) {
1169 /*
1170 * Observer is waiting for onShutDownComplete when we are done.
1171 * Since nothing will be done send notification directly so shutdown
1172 * sequence can continue.
1173 */
1174 try {
1175 observer.onShutDownComplete(StorageResultCode.OperationSucceeded);
1176 } catch (RemoteException e) {
1177 Slog.w(TAG, "RemoteException when shutting down");
1178 }
San Mehat4270e1e2010-01-29 05:32:19 -08001179 }
1180 }
1181
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001182 private boolean getUmsEnabling() {
1183 synchronized (mListeners) {
1184 return mUmsEnabling;
1185 }
1186 }
1187
1188 private void setUmsEnabling(boolean enable) {
1189 synchronized (mListeners) {
Tony Wufc711252010-08-09 16:49:19 +08001190 mUmsEnabling = enable;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001191 }
1192 }
1193
San Mehatb1043402010-02-05 08:26:50 -08001194 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -08001195 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -08001196
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001197 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001198 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001199 }
San Mehatb1043402010-02-05 08:26:50 -08001200 return doGetShareMethodAvailable("ums");
1201 }
1202
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001203 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001204 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001205 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001206
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001207 // TODO: Add support for multiple share methods
1208
1209 /*
1210 * If the volume is mounted and we're enabling then unmount it
1211 */
1212 String path = Environment.getExternalStorageDirectory().getPath();
1213 String vs = getVolumeState(path);
1214 String method = "ums";
1215 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1216 // Override for isUsbMassStorageEnabled()
1217 setUmsEnabling(enable);
1218 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1219 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1220 // Clear override
1221 setUmsEnabling(false);
1222 }
1223 /*
1224 * If we disabled UMS then mount the volume
1225 */
1226 if (!enable) {
1227 doShareUnshareVolume(path, method, enable);
1228 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001229 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001230 " after disabling share method " + method);
1231 /*
1232 * Even though the mount failed, the unshare didn't so don't indicate an error.
1233 * The mountVolume() call will have set the storage state and sent the necessary
1234 * broadcasts.
1235 */
1236 }
1237 }
San Mehatb1043402010-02-05 08:26:50 -08001238 }
1239
1240 public boolean isUsbMassStorageEnabled() {
1241 waitForReady();
1242 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001244
San Mehat7fd0fee2009-12-17 07:12:23 -08001245 /**
1246 * @return state of the volume at the specified mount point
1247 */
San Mehat4270e1e2010-01-29 05:32:19 -08001248 public String getVolumeState(String mountPoint) {
San Mehat7fd0fee2009-12-17 07:12:23 -08001249 /*
1250 * XXX: Until we have multiple volume discovery, just hardwire
1251 * this to /sdcard
1252 */
1253 if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -07001254 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
San Mehat7fd0fee2009-12-17 07:12:23 -08001255 throw new IllegalArgumentException();
1256 }
1257
1258 return mLegacyState;
1259 }
1260
Kenny Roote1ff2142010-10-12 11:20:01 -07001261 public boolean isExternalStorageEmulated() {
1262 return mEmulateExternalStorage;
1263 }
1264
San Mehat4270e1e2010-01-29 05:32:19 -08001265 public int mountVolume(String path) {
1266 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001267
San Mehat207e5382010-02-04 20:46:54 -08001268 waitForReady();
1269 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 }
1271
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001272 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001273 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001274 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001276 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001277 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001278 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1279 Environment.MEDIA_REMOVED.equals(volState) ||
1280 Environment.MEDIA_SHARED.equals(volState) ||
1281 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1282 // Media already unmounted or cannot be unmounted.
1283 // TODO return valid return code when adding observer call back.
1284 return;
1285 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001286 UnmountCallBack ucb = new UnmountCallBack(path, force);
1287 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 }
1289
San Mehat4270e1e2010-01-29 05:32:19 -08001290 public int formatVolume(String path) {
1291 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001292 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001293
San Mehat207e5382010-02-04 20:46:54 -08001294 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 }
1296
San Mehatc1b4ce92010-02-16 17:13:03 -08001297 public int []getStorageUsers(String path) {
1298 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1299 waitForReady();
1300 try {
1301 String[] r = mConnector.doListCommand(
1302 String.format("storage users %s", path),
1303 VoldResponseCode.StorageUsersListResult);
1304 // FMT: <pid> <process name>
1305 int[] data = new int[r.length];
1306 for (int i = 0; i < r.length; i++) {
1307 String []tok = r[i].split(" ");
1308 try {
1309 data[i] = Integer.parseInt(tok[0]);
1310 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001311 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001312 return new int[0];
1313 }
1314 }
1315 return data;
1316 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001317 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001318 return new int[0];
1319 }
1320 }
1321
San Mehatb1043402010-02-05 08:26:50 -08001322 private void warnOnNotMounted() {
1323 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001324 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001325 }
1326 }
1327
San Mehat4270e1e2010-01-29 05:32:19 -08001328 public String[] getSecureContainerList() {
1329 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001330 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001331 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001332
San Mehat4270e1e2010-01-29 05:32:19 -08001333 try {
1334 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1335 } catch (NativeDaemonConnectorException e) {
1336 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
1338 }
San Mehat36972292010-01-06 11:06:32 -08001339
San Mehat4270e1e2010-01-29 05:32:19 -08001340 public int createSecureContainer(String id, int sizeMb, String fstype,
1341 String key, int ownerUid) {
1342 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001343 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001344 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001345
San Mehatb1043402010-02-05 08:26:50 -08001346 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001347 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1348 try {
1349 mConnector.doCommand(cmd);
1350 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001351 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001352 }
San Mehata181b212010-02-11 06:50:20 -08001353
1354 if (rc == StorageResultCode.OperationSucceeded) {
1355 synchronized (mAsecMountSet) {
1356 mAsecMountSet.add(id);
1357 }
1358 }
San Mehat4270e1e2010-01-29 05:32:19 -08001359 return rc;
San Mehat36972292010-01-06 11:06:32 -08001360 }
1361
San Mehat4270e1e2010-01-29 05:32:19 -08001362 public int finalizeSecureContainer(String id) {
1363 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001364 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001365
San Mehatb1043402010-02-05 08:26:50 -08001366 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001367 try {
1368 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001369 /*
1370 * Finalization does a remount, so no need
1371 * to update mAsecMountSet
1372 */
San Mehat4270e1e2010-01-29 05:32:19 -08001373 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001374 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001375 }
San Mehat4270e1e2010-01-29 05:32:19 -08001376 return rc;
San Mehat36972292010-01-06 11:06:32 -08001377 }
1378
San Mehatd9709982010-02-18 11:43:03 -08001379 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001380 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001381 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001382 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001383
Kenny Rootaa485402010-09-14 14:49:41 -07001384 /*
1385 * Force a GC to make sure AssetManagers in other threads of the
1386 * system_server are cleaned up. We have to do this since AssetManager
1387 * instances are kept as a WeakReference and it's possible we have files
1388 * open on the external storage.
1389 */
1390 Runtime.getRuntime().gc();
1391
San Mehatb1043402010-02-05 08:26:50 -08001392 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001393 try {
San Mehatd9709982010-02-18 11:43:03 -08001394 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001395 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001396 int code = e.getCode();
1397 if (code == VoldResponseCode.OpFailedStorageBusy) {
1398 rc = StorageResultCode.OperationFailedStorageBusy;
1399 } else {
1400 rc = StorageResultCode.OperationFailedInternalError;
1401 }
San Mehat02735bc2010-01-26 15:18:08 -08001402 }
San Mehata181b212010-02-11 06:50:20 -08001403
1404 if (rc == StorageResultCode.OperationSucceeded) {
1405 synchronized (mAsecMountSet) {
1406 if (mAsecMountSet.contains(id)) {
1407 mAsecMountSet.remove(id);
1408 }
1409 }
1410 }
1411
San Mehat4270e1e2010-01-29 05:32:19 -08001412 return rc;
San Mehat36972292010-01-06 11:06:32 -08001413 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001414
San Mehat4270e1e2010-01-29 05:32:19 -08001415 public int mountSecureContainer(String id, String key, int ownerUid) {
1416 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001417 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001418 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001419
San Mehata181b212010-02-11 06:50:20 -08001420 synchronized (mAsecMountSet) {
1421 if (mAsecMountSet.contains(id)) {
1422 return StorageResultCode.OperationFailedStorageMounted;
1423 }
1424 }
1425
San Mehatb1043402010-02-05 08:26:50 -08001426 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001427 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1428 try {
1429 mConnector.doCommand(cmd);
1430 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001431 int code = e.getCode();
1432 if (code != VoldResponseCode.OpFailedStorageBusy) {
1433 rc = StorageResultCode.OperationFailedInternalError;
1434 }
San Mehat02735bc2010-01-26 15:18:08 -08001435 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001436
1437 if (rc == StorageResultCode.OperationSucceeded) {
1438 synchronized (mAsecMountSet) {
1439 mAsecMountSet.add(id);
1440 }
1441 }
San Mehat4270e1e2010-01-29 05:32:19 -08001442 return rc;
San Mehat36972292010-01-06 11:06:32 -08001443 }
1444
San Mehatd9709982010-02-18 11:43:03 -08001445 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001446 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001447 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001448 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001449
San Mehat6cdd9c02010-02-09 14:45:20 -08001450 synchronized (mAsecMountSet) {
1451 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001452 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001453 }
1454 }
1455
Kenny Rootaa485402010-09-14 14:49:41 -07001456 /*
1457 * Force a GC to make sure AssetManagers in other threads of the
1458 * system_server are cleaned up. We have to do this since AssetManager
1459 * instances are kept as a WeakReference and it's possible we have files
1460 * open on the external storage.
1461 */
1462 Runtime.getRuntime().gc();
1463
San Mehatb1043402010-02-05 08:26:50 -08001464 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001465 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001466 try {
1467 mConnector.doCommand(cmd);
1468 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001469 int code = e.getCode();
1470 if (code == VoldResponseCode.OpFailedStorageBusy) {
1471 rc = StorageResultCode.OperationFailedStorageBusy;
1472 } else {
1473 rc = StorageResultCode.OperationFailedInternalError;
1474 }
San Mehat02735bc2010-01-26 15:18:08 -08001475 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001476
1477 if (rc == StorageResultCode.OperationSucceeded) {
1478 synchronized (mAsecMountSet) {
1479 mAsecMountSet.remove(id);
1480 }
1481 }
San Mehat4270e1e2010-01-29 05:32:19 -08001482 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001483 }
1484
San Mehat6cdd9c02010-02-09 14:45:20 -08001485 public boolean isSecureContainerMounted(String id) {
1486 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1487 waitForReady();
1488 warnOnNotMounted();
1489
1490 synchronized (mAsecMountSet) {
1491 return mAsecMountSet.contains(id);
1492 }
1493 }
1494
San Mehat4270e1e2010-01-29 05:32:19 -08001495 public int renameSecureContainer(String oldId, String newId) {
1496 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001497 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001498 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001499
San Mehata181b212010-02-11 06:50:20 -08001500 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001501 /*
Jason parks9ed98bc2011-01-17 09:58:35 -06001502 * Because a mounted container has active internal state which cannot be
San Mehat85451ee2010-02-24 08:54:18 -08001503 * changed while active, we must ensure both ids are not currently mounted.
1504 */
1505 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001506 return StorageResultCode.OperationFailedStorageMounted;
1507 }
1508 }
1509
San Mehatb1043402010-02-05 08:26:50 -08001510 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001511 String cmd = String.format("asec rename %s %s", oldId, newId);
1512 try {
1513 mConnector.doCommand(cmd);
1514 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001515 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001516 }
San Mehata181b212010-02-11 06:50:20 -08001517
San Mehat4270e1e2010-01-29 05:32:19 -08001518 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001519 }
1520
San Mehat4270e1e2010-01-29 05:32:19 -08001521 public String getSecureContainerPath(String id) {
1522 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001523 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001524 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001525
San Mehat2d66cef2010-03-23 11:12:52 -07001526 try {
1527 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1528 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001529 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001530 if (code != VoldResponseCode.AsecPathResult) {
1531 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1532 }
1533 return tok[1];
1534 } catch (NativeDaemonConnectorException e) {
1535 int code = e.getCode();
1536 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1537 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001538 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001539 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001540 }
1541 }
San Mehat22dd86e2010-01-12 12:21:18 -08001542 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001543
1544 public void finishMediaUpdate() {
1545 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1546 }
Kenny Root02c87302010-07-01 08:10:18 -07001547
Kenny Roota02b8b02010-08-05 16:14:17 -07001548 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
1549 if (callerUid == android.os.Process.SYSTEM_UID) {
1550 return true;
1551 }
1552
Kenny Root02c87302010-07-01 08:10:18 -07001553 if (packageName == null) {
1554 return false;
1555 }
1556
1557 final int packageUid = mPms.getPackageUid(packageName);
1558
1559 if (DEBUG_OBB) {
1560 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
1561 packageUid + ", callerUid = " + callerUid);
1562 }
1563
1564 return callerUid == packageUid;
1565 }
1566
1567 public String getMountedObbPath(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001568 if (filename == null) {
1569 throw new IllegalArgumentException("filename cannot be null");
1570 }
1571
Kenny Root02c87302010-07-01 08:10:18 -07001572 waitForReady();
1573 warnOnNotMounted();
1574
Kenny Root02c87302010-07-01 08:10:18 -07001575 try {
1576 ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename));
1577 String []tok = rsp.get(0).split(" ");
1578 int code = Integer.parseInt(tok[0]);
1579 if (code != VoldResponseCode.AsecPathResult) {
1580 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1581 }
1582 return tok[1];
1583 } catch (NativeDaemonConnectorException e) {
1584 int code = e.getCode();
1585 if (code == VoldResponseCode.OpFailedStorageNotFound) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001586 return null;
Kenny Root02c87302010-07-01 08:10:18 -07001587 } else {
1588 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1589 }
1590 }
1591 }
1592
1593 public boolean isObbMounted(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001594 if (filename == null) {
1595 throw new IllegalArgumentException("filename cannot be null");
Kenny Root02c87302010-07-01 08:10:18 -07001596 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001597
1598 synchronized (mObbMounts) {
1599 return mObbPathToStateMap.containsKey(filename);
1600 }
Kenny Root02c87302010-07-01 08:10:18 -07001601 }
1602
Kenny Rootaf9d6672010-10-08 09:21:39 -07001603 public void mountObb(String filename, String key, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -07001604 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001605 if (filename == null) {
1606 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootaf9d6672010-10-08 09:21:39 -07001607 }
1608
1609 if (token == null) {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001610 throw new IllegalArgumentException("token cannot be null");
1611 }
1612
Kenny Rootaf9d6672010-10-08 09:21:39 -07001613 final int callerUid = Binder.getCallingUid();
1614 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1615 final ObbAction action = new MountObbAction(obbState, key);
Kenny Roota02b8b02010-08-05 16:14:17 -07001616 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
1617
1618 if (DEBUG_OBB)
1619 Slog.i(TAG, "Send to OBB handler: " + action.toString());
Kenny Root02c87302010-07-01 08:10:18 -07001620 }
1621
Kenny Rootaf9d6672010-10-08 09:21:39 -07001622 public void unmountObb(String filename, boolean force, IObbActionListener token, int nonce)
1623 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001624 if (filename == null) {
1625 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootf1121dc2010-09-29 07:30:53 -07001626 }
1627
Kenny Rootaf9d6672010-10-08 09:21:39 -07001628 final int callerUid = Binder.getCallingUid();
1629 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1630 final ObbAction action = new UnmountObbAction(obbState, force);
Kenny Roota02b8b02010-08-05 16:14:17 -07001631 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root02c87302010-07-01 08:10:18 -07001632
Kenny Roota02b8b02010-08-05 16:14:17 -07001633 if (DEBUG_OBB)
1634 Slog.i(TAG, "Send to OBB handler: " + action.toString());
1635 }
1636
Jason parks5af0b912010-11-29 09:05:25 -06001637 public int decryptStorage(String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06001638 if (TextUtils.isEmpty(password)) {
1639 throw new IllegalArgumentException("password cannot be empty");
Jason parks5af0b912010-11-29 09:05:25 -06001640 }
1641
Jason parks8888c592011-01-20 22:46:41 -06001642 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1643 "no permission to access the crypt keeper");
Jason parks5af0b912010-11-29 09:05:25 -06001644
1645 waitForReady();
1646
1647 if (DEBUG_EVENTS) {
1648 Slog.i(TAG, "decrypting storage...");
1649 }
1650
1651 try {
Jason parks9ed98bc2011-01-17 09:58:35 -06001652 ArrayList<String> rsp = mConnector.doCommand("cryptfs checkpw " + password);
Jason parksf7b3cd42011-01-27 09:28:25 -06001653 String[] tokens = rsp.get(0).split(" ");
Jason parks9ed98bc2011-01-17 09:58:35 -06001654
Jason parksf7b3cd42011-01-27 09:28:25 -06001655 if (tokens == null || tokens.length != 2) {
Jason parks9ed98bc2011-01-17 09:58:35 -06001656 return -1;
1657 }
1658
Jason parksf7b3cd42011-01-27 09:28:25 -06001659 int code = Integer.parseInt(tokens[1]);
Jason parks9ed98bc2011-01-17 09:58:35 -06001660
1661 if (code == 0) {
1662 // Decrypt was successful. Post a delayed message before restarting in order
1663 // to let the UI to clear itself
1664 mHandler.postDelayed(new Runnable() {
1665 public void run() {
1666 mConnector.doCommand(String.format("cryptfs restart"));
1667 }
Jason parksf7b3cd42011-01-27 09:28:25 -06001668 }, 1000); // 1 second
Jason parks9ed98bc2011-01-17 09:58:35 -06001669 }
1670
1671 return code;
Jason parks5af0b912010-11-29 09:05:25 -06001672 } catch (NativeDaemonConnectorException e) {
1673 // Decryption failed
1674 return e.getCode();
1675 }
Jason parks5af0b912010-11-29 09:05:25 -06001676 }
1677
Jason parks56aa5322011-01-07 09:01:15 -06001678 public int encryptStorage(String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06001679 if (TextUtils.isEmpty(password)) {
1680 throw new IllegalArgumentException("password cannot be empty");
Jason parks56aa5322011-01-07 09:01:15 -06001681 }
1682
Jason parks8888c592011-01-20 22:46:41 -06001683 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1684 "no permission to access the crypt keeper");
Jason parks56aa5322011-01-07 09:01:15 -06001685
1686 waitForReady();
1687
1688 if (DEBUG_EVENTS) {
Jason parks8888c592011-01-20 22:46:41 -06001689 Slog.i(TAG, "encrypting storage...");
Jason parks56aa5322011-01-07 09:01:15 -06001690 }
1691
1692 try {
Jason parks9ed98bc2011-01-17 09:58:35 -06001693 mConnector.doCommand(String.format("cryptfs enablecrypto inplace %s", password));
Jason parks56aa5322011-01-07 09:01:15 -06001694 } catch (NativeDaemonConnectorException e) {
1695 // Encryption failed
1696 return e.getCode();
1697 }
1698
1699 return 0;
1700 }
1701
Jason parksf7b3cd42011-01-27 09:28:25 -06001702 public int changeEncryptionPassword(String password) {
1703 if (TextUtils.isEmpty(password)) {
1704 throw new IllegalArgumentException("password cannot be empty");
1705 }
1706
1707 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1708 "no permission to access the crypt keeper");
1709
1710 waitForReady();
1711
1712 if (DEBUG_EVENTS) {
1713 Slog.i(TAG, "changing encryption password...");
1714 }
1715
1716 try {
1717 ArrayList<String> response = mConnector.doCommand("cryptfs changepw " + password);
1718
1719 String[] tokens = response.get(0).split(" ");
1720
1721 if (tokens == null || tokens.length != 2) {
1722 return -1;
1723 }
1724
1725 return Integer.parseInt(tokens[1]);
1726 } catch (NativeDaemonConnectorException e) {
1727 // Encryption failed
1728 return e.getCode();
1729 }
1730 }
1731
Kenny Rootaf9d6672010-10-08 09:21:39 -07001732 private void addObbStateLocked(ObbState obbState) throws RemoteException {
1733 final IBinder binder = obbState.getBinder();
1734 List<ObbState> obbStates = mObbMounts.get(binder);
Kenny Root5919ac62010-10-05 09:49:40 -07001735
Kenny Rootaf9d6672010-10-08 09:21:39 -07001736 if (obbStates == null) {
1737 obbStates = new ArrayList<ObbState>();
1738 mObbMounts.put(binder, obbStates);
1739 } else {
1740 for (final ObbState o : obbStates) {
1741 if (o.filename.equals(obbState.filename)) {
1742 throw new IllegalStateException("Attempt to add ObbState twice. "
1743 + "This indicates an error in the MountService logic.");
Kenny Root5919ac62010-10-05 09:49:40 -07001744 }
1745 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001746 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001747
1748 obbStates.add(obbState);
1749 try {
1750 obbState.link();
1751 } catch (RemoteException e) {
1752 /*
1753 * The binder died before we could link it, so clean up our state
1754 * and return failure.
1755 */
1756 obbStates.remove(obbState);
1757 if (obbStates.isEmpty()) {
1758 mObbMounts.remove(binder);
1759 }
1760
1761 // Rethrow the error so mountObb can get it
1762 throw e;
1763 }
1764
1765 mObbPathToStateMap.put(obbState.filename, obbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07001766 }
1767
Kenny Rootaf9d6672010-10-08 09:21:39 -07001768 private void removeObbStateLocked(ObbState obbState) {
1769 final IBinder binder = obbState.getBinder();
1770 final List<ObbState> obbStates = mObbMounts.get(binder);
1771 if (obbStates != null) {
1772 if (obbStates.remove(obbState)) {
1773 obbState.unlink();
Kenny Root05105f72010-09-22 17:29:43 -07001774 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001775 if (obbStates.isEmpty()) {
1776 mObbMounts.remove(binder);
1777 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001778 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001779
Kenny Rootaf9d6672010-10-08 09:21:39 -07001780 mObbPathToStateMap.remove(obbState.filename);
Kenny Root38cf8862010-09-26 14:18:51 -07001781 }
1782
Kenny Roota02b8b02010-08-05 16:14:17 -07001783 private class ObbActionHandler extends Handler {
1784 private boolean mBound = false;
Kenny Root480afe72010-10-07 10:17:50 -07001785 private final List<ObbAction> mActions = new LinkedList<ObbAction>();
Kenny Roota02b8b02010-08-05 16:14:17 -07001786
1787 ObbActionHandler(Looper l) {
1788 super(l);
1789 }
1790
1791 @Override
1792 public void handleMessage(Message msg) {
1793 switch (msg.what) {
1794 case OBB_RUN_ACTION: {
Kenny Root480afe72010-10-07 10:17:50 -07001795 final ObbAction action = (ObbAction) msg.obj;
Kenny Roota02b8b02010-08-05 16:14:17 -07001796
1797 if (DEBUG_OBB)
1798 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
1799
1800 // If a bind was already initiated we don't really
1801 // need to do anything. The pending install
1802 // will be processed later on.
1803 if (!mBound) {
1804 // If this is the only one pending we might
1805 // have to bind to the service again.
1806 if (!connectToService()) {
1807 Slog.e(TAG, "Failed to bind to media container service");
1808 action.handleError();
1809 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07001810 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001811 }
Kenny Root735de3b2010-09-30 14:11:39 -07001812
Kenny Root735de3b2010-09-30 14:11:39 -07001813 mActions.add(action);
Kenny Roota02b8b02010-08-05 16:14:17 -07001814 break;
1815 }
1816 case OBB_MCS_BOUND: {
1817 if (DEBUG_OBB)
1818 Slog.i(TAG, "OBB_MCS_BOUND");
1819 if (msg.obj != null) {
1820 mContainerService = (IMediaContainerService) msg.obj;
1821 }
1822 if (mContainerService == null) {
1823 // Something seriously wrong. Bail out
1824 Slog.e(TAG, "Cannot bind to media container service");
1825 for (ObbAction action : mActions) {
1826 // Indicate service bind error
1827 action.handleError();
1828 }
1829 mActions.clear();
1830 } else if (mActions.size() > 0) {
Kenny Root480afe72010-10-07 10:17:50 -07001831 final ObbAction action = mActions.get(0);
Kenny Roota02b8b02010-08-05 16:14:17 -07001832 if (action != null) {
1833 action.execute(this);
1834 }
1835 } else {
1836 // Should never happen ideally.
1837 Slog.w(TAG, "Empty queue");
1838 }
1839 break;
1840 }
1841 case OBB_MCS_RECONNECT: {
1842 if (DEBUG_OBB)
1843 Slog.i(TAG, "OBB_MCS_RECONNECT");
1844 if (mActions.size() > 0) {
1845 if (mBound) {
1846 disconnectService();
1847 }
1848 if (!connectToService()) {
1849 Slog.e(TAG, "Failed to bind to media container service");
1850 for (ObbAction action : mActions) {
1851 // Indicate service bind error
1852 action.handleError();
1853 }
1854 mActions.clear();
1855 }
1856 }
1857 break;
1858 }
1859 case OBB_MCS_UNBIND: {
1860 if (DEBUG_OBB)
1861 Slog.i(TAG, "OBB_MCS_UNBIND");
1862
1863 // Delete pending install
1864 if (mActions.size() > 0) {
1865 mActions.remove(0);
1866 }
1867 if (mActions.size() == 0) {
1868 if (mBound) {
1869 disconnectService();
1870 }
1871 } else {
1872 // There are more pending requests in queue.
1873 // Just post MCS_BOUND message to trigger processing
1874 // of next pending install.
1875 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1876 }
1877 break;
1878 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001879 case OBB_FLUSH_MOUNT_STATE: {
1880 final String path = (String) msg.obj;
1881
1882 if (DEBUG_OBB)
1883 Slog.i(TAG, "Flushing all OBB state for path " + path);
1884
1885 synchronized (mObbMounts) {
1886 final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>();
1887
1888 final Iterator<Entry<String, ObbState>> i =
1889 mObbPathToStateMap.entrySet().iterator();
1890 while (i.hasNext()) {
1891 final Entry<String, ObbState> obbEntry = i.next();
1892
1893 /*
1894 * If this entry's source file is in the volume path
1895 * that got unmounted, remove it because it's no
1896 * longer valid.
1897 */
1898 if (obbEntry.getKey().startsWith(path)) {
1899 obbStatesToRemove.add(obbEntry.getValue());
1900 }
1901 }
1902
1903 for (final ObbState obbState : obbStatesToRemove) {
1904 if (DEBUG_OBB)
1905 Slog.i(TAG, "Removing state for " + obbState.filename);
1906
1907 removeObbStateLocked(obbState);
1908
1909 try {
1910 obbState.token.onObbResult(obbState.filename, obbState.nonce,
1911 OnObbStateChangeListener.UNMOUNTED);
1912 } catch (RemoteException e) {
1913 Slog.i(TAG, "Couldn't send unmount notification for OBB: "
1914 + obbState.filename);
1915 }
1916 }
1917 }
1918 break;
1919 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001920 }
1921 }
1922
1923 private boolean connectToService() {
1924 if (DEBUG_OBB)
1925 Slog.i(TAG, "Trying to bind to DefaultContainerService");
1926
1927 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
1928 if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) {
1929 mBound = true;
1930 return true;
1931 }
1932 return false;
1933 }
1934
1935 private void disconnectService() {
1936 mContainerService = null;
1937 mBound = false;
1938 mContext.unbindService(mDefContainerConn);
1939 }
1940 }
1941
1942 abstract class ObbAction {
1943 private static final int MAX_RETRIES = 3;
1944 private int mRetries;
1945
1946 ObbState mObbState;
1947
1948 ObbAction(ObbState obbState) {
1949 mObbState = obbState;
1950 }
1951
1952 public void execute(ObbActionHandler handler) {
1953 try {
1954 if (DEBUG_OBB)
1955 Slog.i(TAG, "Starting to execute action: " + this.toString());
1956 mRetries++;
1957 if (mRetries > MAX_RETRIES) {
1958 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
Kenny Root480afe72010-10-07 10:17:50 -07001959 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001960 handleError();
1961 return;
1962 } else {
1963 handleExecute();
1964 if (DEBUG_OBB)
1965 Slog.i(TAG, "Posting install MCS_UNBIND");
1966 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1967 }
1968 } catch (RemoteException e) {
1969 if (DEBUG_OBB)
1970 Slog.i(TAG, "Posting install MCS_RECONNECT");
1971 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
1972 } catch (Exception e) {
1973 if (DEBUG_OBB)
1974 Slog.d(TAG, "Error handling OBB action", e);
1975 handleError();
Kenny Root17eb6fb2010-10-06 15:02:52 -07001976 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001977 }
1978 }
1979
Kenny Root05105f72010-09-22 17:29:43 -07001980 abstract void handleExecute() throws RemoteException, IOException;
Kenny Roota02b8b02010-08-05 16:14:17 -07001981 abstract void handleError();
Kenny Root38cf8862010-09-26 14:18:51 -07001982
1983 protected ObbInfo getObbInfo() throws IOException {
1984 ObbInfo obbInfo;
1985 try {
1986 obbInfo = mContainerService.getObbInfo(mObbState.filename);
1987 } catch (RemoteException e) {
1988 Slog.d(TAG, "Couldn't call DefaultContainerService to fetch OBB info for "
1989 + mObbState.filename);
1990 obbInfo = null;
1991 }
1992 if (obbInfo == null) {
1993 throw new IOException("Couldn't read OBB file: " + mObbState.filename);
1994 }
1995 return obbInfo;
1996 }
1997
Kenny Rootaf9d6672010-10-08 09:21:39 -07001998 protected void sendNewStatusOrIgnore(int status) {
1999 if (mObbState == null || mObbState.token == null) {
2000 return;
2001 }
2002
Kenny Root38cf8862010-09-26 14:18:51 -07002003 try {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002004 mObbState.token.onObbResult(mObbState.filename, mObbState.nonce, status);
Kenny Root38cf8862010-09-26 14:18:51 -07002005 } catch (RemoteException e) {
2006 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
2007 }
2008 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002009 }
2010
2011 class MountObbAction extends ObbAction {
2012 private String mKey;
2013
2014 MountObbAction(ObbState obbState, String key) {
2015 super(obbState);
2016 mKey = key;
2017 }
2018
Jason parks5af0b912010-11-29 09:05:25 -06002019 @Override
Kenny Root735de3b2010-09-30 14:11:39 -07002020 public void handleExecute() throws IOException, RemoteException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002021 waitForReady();
2022 warnOnNotMounted();
2023
Kenny Root38cf8862010-09-26 14:18:51 -07002024 final ObbInfo obbInfo = getObbInfo();
2025
Kenny Roota02b8b02010-08-05 16:14:17 -07002026 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002027 Slog.w(TAG, "Denied attempt to mount OBB " + obbInfo.filename
2028 + " which is owned by " + obbInfo.packageName);
2029 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2030 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07002031 }
2032
Kenny Rootaf9d6672010-10-08 09:21:39 -07002033 final boolean isMounted;
2034 synchronized (mObbMounts) {
2035 isMounted = mObbPathToStateMap.containsKey(obbInfo.filename);
2036 }
2037 if (isMounted) {
2038 Slog.w(TAG, "Attempt to mount OBB which is already mounted: " + obbInfo.filename);
2039 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_ALREADY_MOUNTED);
2040 return;
2041 }
2042
2043 /*
2044 * The filename passed in might not be the canonical name, so just
2045 * set the filename to the canonicalized version.
2046 */
2047 mObbState.filename = obbInfo.filename;
2048
2049 final String hashedKey;
2050 if (mKey == null) {
2051 hashedKey = "none";
2052 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002053 try {
Kenny Root3b1abba2010-10-13 15:00:07 -07002054 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
2055
2056 KeySpec ks = new PBEKeySpec(mKey.toCharArray(), obbInfo.salt,
2057 PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE);
2058 SecretKey key = factory.generateSecret(ks);
2059 BigInteger bi = new BigInteger(key.getEncoded());
2060 hashedKey = bi.toString(16);
Kenny Rootaf9d6672010-10-08 09:21:39 -07002061 } catch (NoSuchAlgorithmException e) {
Kenny Root3b1abba2010-10-13 15:00:07 -07002062 Slog.e(TAG, "Could not load PBKDF2 algorithm", e);
2063 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
2064 return;
2065 } catch (InvalidKeySpecException e) {
2066 Slog.e(TAG, "Invalid key spec when loading PBKDF2 algorithm", e);
2067 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root38cf8862010-09-26 14:18:51 -07002068 return;
2069 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002070 }
Kenny Root38cf8862010-09-26 14:18:51 -07002071
Kenny Rootaf9d6672010-10-08 09:21:39 -07002072 int rc = StorageResultCode.OperationSucceeded;
2073 String cmd = String.format("obb mount %s %s %d", mObbState.filename, hashedKey,
2074 mObbState.callerUid);
2075 try {
2076 mConnector.doCommand(cmd);
2077 } catch (NativeDaemonConnectorException e) {
2078 int code = e.getCode();
2079 if (code != VoldResponseCode.OpFailedStorageBusy) {
2080 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002081 }
2082 }
2083
Kenny Rootaf9d6672010-10-08 09:21:39 -07002084 if (rc == StorageResultCode.OperationSucceeded) {
2085 if (DEBUG_OBB)
2086 Slog.d(TAG, "Successfully mounted OBB " + mObbState.filename);
2087
2088 synchronized (mObbMounts) {
2089 addObbStateLocked(mObbState);
2090 }
2091
2092 sendNewStatusOrIgnore(OnObbStateChangeListener.MOUNTED);
Kenny Root02c87302010-07-01 08:10:18 -07002093 } else {
Kenny Root05105f72010-09-22 17:29:43 -07002094 Slog.e(TAG, "Couldn't mount OBB file: " + rc);
Kenny Roota02b8b02010-08-05 16:14:17 -07002095
Kenny Rootaf9d6672010-10-08 09:21:39 -07002096 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT);
Kenny Root02c87302010-07-01 08:10:18 -07002097 }
2098 }
2099
Jason parks5af0b912010-11-29 09:05:25 -06002100 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002101 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002102 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root02c87302010-07-01 08:10:18 -07002103 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002104
2105 @Override
2106 public String toString() {
2107 StringBuilder sb = new StringBuilder();
2108 sb.append("MountObbAction{");
2109 sb.append("filename=");
2110 sb.append(mObbState.filename);
2111 sb.append(",callerUid=");
2112 sb.append(mObbState.callerUid);
2113 sb.append(",token=");
2114 sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002115 sb.append(",binder=");
2116 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002117 sb.append('}');
2118 return sb.toString();
2119 }
2120 }
2121
2122 class UnmountObbAction extends ObbAction {
2123 private boolean mForceUnmount;
2124
2125 UnmountObbAction(ObbState obbState, boolean force) {
2126 super(obbState);
2127 mForceUnmount = force;
2128 }
2129
Jason parks5af0b912010-11-29 09:05:25 -06002130 @Override
Kenny Root38cf8862010-09-26 14:18:51 -07002131 public void handleExecute() throws IOException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002132 waitForReady();
2133 warnOnNotMounted();
2134
Kenny Root38cf8862010-09-26 14:18:51 -07002135 final ObbInfo obbInfo = getObbInfo();
Kenny Roota02b8b02010-08-05 16:14:17 -07002136
Kenny Rootaf9d6672010-10-08 09:21:39 -07002137 final ObbState obbState;
Kenny Root38cf8862010-09-26 14:18:51 -07002138 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002139 obbState = mObbPathToStateMap.get(obbInfo.filename);
2140 }
Kenny Root38cf8862010-09-26 14:18:51 -07002141
Kenny Rootaf9d6672010-10-08 09:21:39 -07002142 if (obbState == null) {
2143 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_NOT_MOUNTED);
2144 return;
2145 }
2146
2147 if (obbState.callerUid != mObbState.callerUid) {
2148 Slog.w(TAG, "Permission denied attempting to unmount OBB " + obbInfo.filename
2149 + " (owned by " + obbInfo.packageName + ")");
2150 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2151 return;
2152 }
2153
2154 mObbState.filename = obbInfo.filename;
2155
2156 int rc = StorageResultCode.OperationSucceeded;
2157 String cmd = String.format("obb unmount %s%s", mObbState.filename,
2158 (mForceUnmount ? " force" : ""));
2159 try {
2160 mConnector.doCommand(cmd);
2161 } catch (NativeDaemonConnectorException e) {
2162 int code = e.getCode();
2163 if (code == VoldResponseCode.OpFailedStorageBusy) {
2164 rc = StorageResultCode.OperationFailedStorageBusy;
2165 } else if (code == VoldResponseCode.OpFailedStorageNotFound) {
2166 // If it's not mounted then we've already won.
2167 rc = StorageResultCode.OperationSucceeded;
2168 } else {
2169 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002170 }
2171 }
2172
Kenny Rootaf9d6672010-10-08 09:21:39 -07002173 if (rc == StorageResultCode.OperationSucceeded) {
2174 synchronized (mObbMounts) {
2175 removeObbStateLocked(obbState);
Kenny Root38cf8862010-09-26 14:18:51 -07002176 }
2177
Kenny Rootaf9d6672010-10-08 09:21:39 -07002178 sendNewStatusOrIgnore(OnObbStateChangeListener.UNMOUNTED);
Kenny Roota02b8b02010-08-05 16:14:17 -07002179 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002180 Slog.w(TAG, "Could not mount OBB: " + mObbState.filename);
2181 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT);
Kenny Roota02b8b02010-08-05 16:14:17 -07002182 }
2183 }
2184
Jason parks5af0b912010-11-29 09:05:25 -06002185 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002186 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002187 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Roota02b8b02010-08-05 16:14:17 -07002188 }
2189
2190 @Override
2191 public String toString() {
2192 StringBuilder sb = new StringBuilder();
2193 sb.append("UnmountObbAction{");
2194 sb.append("filename=");
2195 sb.append(mObbState.filename != null ? mObbState.filename : "null");
2196 sb.append(",force=");
2197 sb.append(mForceUnmount);
2198 sb.append(",callerUid=");
2199 sb.append(mObbState.callerUid);
2200 sb.append(",token=");
2201 sb.append(mObbState.token != null ? mObbState.token.toString() : "null");
Kenny Root735de3b2010-09-30 14:11:39 -07002202 sb.append(",binder=");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002203 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002204 sb.append('}');
2205 return sb.toString();
2206 }
Kenny Root02c87302010-07-01 08:10:18 -07002207 }
Kenny Root38cf8862010-09-26 14:18:51 -07002208
2209 @Override
2210 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2211 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
2212 pw.println("Permission Denial: can't dump ActivityManager from from pid="
2213 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
2214 + " without permission " + android.Manifest.permission.DUMP);
2215 return;
2216 }
2217
Kenny Root38cf8862010-09-26 14:18:51 -07002218 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002219 pw.println(" mObbMounts:");
Kenny Root38cf8862010-09-26 14:18:51 -07002220
Kenny Rootaf9d6672010-10-08 09:21:39 -07002221 final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet().iterator();
2222 while (binders.hasNext()) {
2223 Entry<IBinder, List<ObbState>> e = binders.next();
2224 pw.print(" Key="); pw.println(e.getKey().toString());
2225 final List<ObbState> obbStates = e.getValue();
Kenny Root38cf8862010-09-26 14:18:51 -07002226 for (final ObbState obbState : obbStates) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002227 pw.print(" "); pw.println(obbState.toString());
Kenny Root38cf8862010-09-26 14:18:51 -07002228 }
2229 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002230
2231 pw.println("");
2232 pw.println(" mObbPathToStateMap:");
2233 final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator();
2234 while (maps.hasNext()) {
2235 final Entry<String, ObbState> e = maps.next();
2236 pw.print(" "); pw.print(e.getKey());
2237 pw.print(" -> "); pw.println(e.getValue().toString());
2238 }
Kenny Root38cf8862010-09-26 14:18:51 -07002239 }
2240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241}
2242