blob: 59f08523ec0fd4e6177f2d1c9f6427d74a5ef19e [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;
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400151 private final HashMap<String, String> mVolumeStates = new HashMap<String, String>();
152 private String mExternalStoragePath;
San Mehat4270e1e2010-01-29 05:32:19 -0800153 private PackageManagerService mPms;
154 private boolean mUmsEnabling;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800155 // Used as a lock for methods that register/unregister listeners.
156 final private ArrayList<MountServiceBinderListener> mListeners =
157 new ArrayList<MountServiceBinderListener>();
San Mehat6a965af22010-02-24 17:47:30 -0800158 private boolean mBooted = false;
159 private boolean mReady = false;
160 private boolean mSendUmsConnectedOnBoot = false;
Mike Lockwood03559752010-07-19 18:25:03 -0400161 // true if we should fake MEDIA_MOUNTED state for external storage
162 private boolean mEmulateExternalStorage = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800163
San Mehat6cdd9c02010-02-09 14:45:20 -0800164 /**
165 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800166 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800167 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800168 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800169
Kenny Root02c87302010-07-01 08:10:18 -0700170 /**
Kenny Root3b1abba2010-10-13 15:00:07 -0700171 * The size of the crypto algorithm key in bits for OBB files. Currently
172 * Twofish is used which takes 128-bit keys.
173 */
174 private static final int CRYPTO_ALGORITHM_KEY_SIZE = 128;
175
176 /**
177 * The number of times to run SHA1 in the PBKDF2 function for OBB files.
178 * 1024 is reasonably secure and not too slow.
179 */
180 private static final int PBKDF2_HASH_ROUNDS = 1024;
181
182 /**
Kenny Roota02b8b02010-08-05 16:14:17 -0700183 * Mounted OBB tracking information. Used to track the current state of all
184 * OBBs.
Kenny Root02c87302010-07-01 08:10:18 -0700185 */
Kenny Root735de3b2010-09-30 14:11:39 -0700186 final private Map<IBinder, List<ObbState>> mObbMounts = new HashMap<IBinder, List<ObbState>>();
Kenny Roota02b8b02010-08-05 16:14:17 -0700187 final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>();
188
189 class ObbState implements IBinder.DeathRecipient {
Kenny Rootaf9d6672010-10-08 09:21:39 -0700190 public ObbState(String filename, int callerUid, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -0700191 throws RemoteException {
Kenny Roota02b8b02010-08-05 16:14:17 -0700192 this.filename = filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700193 this.callerUid = callerUid;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700194 this.token = token;
195 this.nonce = nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700196 }
197
198 // OBB source filename
Kenny Rootaf9d6672010-10-08 09:21:39 -0700199 String filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700200
201 // Binder.callingUid()
Kenny Root05105f72010-09-22 17:29:43 -0700202 final public int callerUid;
Kenny Roota02b8b02010-08-05 16:14:17 -0700203
Kenny Rootaf9d6672010-10-08 09:21:39 -0700204 // Token of remote Binder caller
205 final IObbActionListener token;
206
207 // Identifier to pass back to the token
208 final int nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700209
Kenny Root735de3b2010-09-30 14:11:39 -0700210 public IBinder getBinder() {
211 return token.asBinder();
212 }
213
Kenny Roota02b8b02010-08-05 16:14:17 -0700214 @Override
215 public void binderDied() {
216 ObbAction action = new UnmountObbAction(this, true);
217 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root735de3b2010-09-30 14:11:39 -0700218 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700219
Kenny Root5919ac62010-10-05 09:49:40 -0700220 public void link() throws RemoteException {
221 getBinder().linkToDeath(this, 0);
222 }
223
224 public void unlink() {
Kenny Root735de3b2010-09-30 14:11:39 -0700225 getBinder().unlinkToDeath(this, 0);
Kenny Roota02b8b02010-08-05 16:14:17 -0700226 }
Kenny Root38cf8862010-09-26 14:18:51 -0700227
228 @Override
229 public String toString() {
230 StringBuilder sb = new StringBuilder("ObbState{");
231 sb.append("filename=");
232 sb.append(filename);
233 sb.append(",token=");
234 sb.append(token.toString());
235 sb.append(",callerUid=");
236 sb.append(callerUid);
Kenny Root38cf8862010-09-26 14:18:51 -0700237 sb.append('}');
238 return sb.toString();
239 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700240 }
241
242 // OBB Action Handler
243 final private ObbActionHandler mObbActionHandler;
244
245 // OBB action handler messages
246 private static final int OBB_RUN_ACTION = 1;
247 private static final int OBB_MCS_BOUND = 2;
248 private static final int OBB_MCS_UNBIND = 3;
249 private static final int OBB_MCS_RECONNECT = 4;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700250 private static final int OBB_FLUSH_MOUNT_STATE = 5;
Kenny Roota02b8b02010-08-05 16:14:17 -0700251
252 /*
253 * Default Container Service information
254 */
255 static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
256 "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService");
257
258 final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection();
259
260 class DefaultContainerConnection implements ServiceConnection {
261 public void onServiceConnected(ComponentName name, IBinder service) {
262 if (DEBUG_OBB)
263 Slog.i(TAG, "onServiceConnected");
264 IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service);
265 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs));
266 }
267
268 public void onServiceDisconnected(ComponentName name) {
269 if (DEBUG_OBB)
270 Slog.i(TAG, "onServiceDisconnected");
271 }
272 };
273
274 // Used in the ObbActionHandler
275 private IMediaContainerService mContainerService = null;
Kenny Root02c87302010-07-01 08:10:18 -0700276
277 // Handler messages
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800278 private static final int H_UNMOUNT_PM_UPDATE = 1;
279 private static final int H_UNMOUNT_PM_DONE = 2;
280 private static final int H_UNMOUNT_MS = 3;
281 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
282 private static final int MAX_UNMOUNT_RETRIES = 4;
283
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800284 class UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700285 final String path;
286 final boolean force;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800287 int retries;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800288
289 UnmountCallBack(String path, boolean force) {
290 retries = 0;
291 this.path = path;
292 this.force = force;
293 }
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800294
295 void handleFinished() {
San Mehata5078592010-03-25 09:36:54 -0700296 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path);
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800297 doUnmountVolume(path, true);
298 }
299 }
300
301 class UmsEnableCallBack extends UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700302 final String method;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800303
304 UmsEnableCallBack(String path, String method, boolean force) {
305 super(path, force);
306 this.method = method;
307 }
308
309 @Override
310 void handleFinished() {
311 super.handleFinished();
312 doShareUnshareVolume(path, method, true);
313 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800314 }
315
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800316 class ShutdownCallBack extends UnmountCallBack {
317 IMountShutdownObserver observer;
318 ShutdownCallBack(String path, IMountShutdownObserver observer) {
319 super(path, true);
320 this.observer = observer;
321 }
322
323 @Override
324 void handleFinished() {
325 int ret = doUnmountVolume(path, true);
326 if (observer != null) {
327 try {
328 observer.onShutDownComplete(ret);
329 } catch (RemoteException e) {
San Mehata5078592010-03-25 09:36:54 -0700330 Slog.w(TAG, "RemoteException when shutting down");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800331 }
332 }
333 }
334 }
335
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400336 class MountServiceHandler extends Handler {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800337 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700338 boolean mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800339
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400340 MountServiceHandler(Looper l) {
341 super(l);
342 }
343
Jason parks5af0b912010-11-29 09:05:25 -0600344 @Override
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800345 public void handleMessage(Message msg) {
346 switch (msg.what) {
347 case H_UNMOUNT_PM_UPDATE: {
San Mehata5078592010-03-25 09:36:54 -0700348 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800349 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
350 mForceUnmounts.add(ucb);
San Mehata5078592010-03-25 09:36:54 -0700351 if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800352 // Register only if needed.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700353 if (!mUpdatingStatus) {
San Mehata5078592010-03-25 09:36:54 -0700354 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700355 mUpdatingStatus = true;
356 mPms.updateExternalMediaStatus(false, true);
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800357 }
358 break;
359 }
360 case H_UNMOUNT_PM_DONE: {
San Mehata5078592010-03-25 09:36:54 -0700361 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE");
San Mehata5078592010-03-25 09:36:54 -0700362 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700363 mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800364 int size = mForceUnmounts.size();
365 int sizeArr[] = new int[size];
366 int sizeArrN = 0;
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700367 // Kill processes holding references first
368 ActivityManagerService ams = (ActivityManagerService)
369 ServiceManager.getService("activity");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800370 for (int i = 0; i < size; i++) {
371 UnmountCallBack ucb = mForceUnmounts.get(i);
372 String path = ucb.path;
373 boolean done = false;
374 if (!ucb.force) {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800375 done = true;
376 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800377 int pids[] = getStorageUsers(path);
378 if (pids == null || pids.length == 0) {
379 done = true;
380 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800381 // Eliminate system process here?
Dianne Hackborn64825172011-03-02 21:32:58 -0800382 ams.killPids(pids, "unmount media", true);
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700383 // Confirm if file references have been freed.
384 pids = getStorageUsers(path);
385 if (pids == null || pids.length == 0) {
386 done = true;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800387 }
388 }
389 }
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700390 if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) {
391 // Retry again
392 Slog.i(TAG, "Retrying to kill storage users again");
393 mHandler.sendMessageDelayed(
394 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
395 ucb.retries++),
396 RETRY_UNMOUNT_DELAY);
397 } else {
398 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
399 Slog.i(TAG, "Failed to unmount media inspite of " +
400 MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now");
401 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800402 sizeArr[sizeArrN++] = i;
403 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
404 ucb));
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800405 }
406 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800407 // Remove already processed elements from list.
408 for (int i = (sizeArrN-1); i >= 0; i--) {
409 mForceUnmounts.remove(sizeArr[i]);
410 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800411 break;
412 }
413 case H_UNMOUNT_MS : {
San Mehata5078592010-03-25 09:36:54 -0700414 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800415 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800416 ucb.handleFinished();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800417 break;
418 }
419 }
420 }
421 };
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400422 final private HandlerThread mHandlerThread;
423 final private Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800424
San Mehat207e5382010-02-04 20:46:54 -0800425 private void waitForReady() {
426 while (mReady == false) {
427 for (int retries = 5; retries > 0; retries--) {
428 if (mReady) {
429 return;
430 }
431 SystemClock.sleep(1000);
432 }
San Mehata5078592010-03-25 09:36:54 -0700433 Slog.w(TAG, "Waiting too long for mReady!");
San Mehat207e5382010-02-04 20:46:54 -0800434 }
San Mehat1f6301e2010-01-07 22:40:27 -0800435 }
Kenny Root02c87302010-07-01 08:10:18 -0700436
San Mehat207e5382010-02-04 20:46:54 -0800437 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Jason parks5af0b912010-11-29 09:05:25 -0600438 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 public void onReceive(Context context, Intent intent) {
San Mehat91c77612010-01-07 10:39:41 -0800440 String action = intent.getAction();
441
442 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
San Mehat207e5382010-02-04 20:46:54 -0800443 mBooted = true;
San Mehat22dd86e2010-01-12 12:21:18 -0800444
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800445 /*
446 * In the simulator, we need to broadcast a volume mounted event
447 * to make the media scanner run.
448 */
449 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
450 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted);
451 return;
452 }
San Mehatfafb0412010-02-18 19:40:04 -0800453 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600454 @Override
San Mehatfafb0412010-02-18 19:40:04 -0800455 public void run() {
456 try {
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400457 synchronized (mVolumeStates) {
458 for (String path : mVolumeStates.keySet()) {
459 String state = mVolumeStates.get(path);
San Mehat6a254402010-03-22 10:21:00 -0700460
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400461 if (state.equals(Environment.MEDIA_UNMOUNTED)) {
462 int rc = doMountVolume(path);
463 if (rc != StorageResultCode.OperationSucceeded) {
464 Slog.e(TAG, String.format("Boot-time mount failed (%d)",
465 rc));
466 }
467 } else if (state.equals(Environment.MEDIA_SHARED)) {
468 /*
469 * Bootstrap UMS enabled state since vold indicates
470 * the volume is shared (runtime restart while ums enabled)
471 */
472 notifyVolumeStateChange(null, path, VolumeState.NoMedia,
473 VolumeState.Shared);
474 }
San Mehatfafb0412010-02-18 19:40:04 -0800475 }
476 }
San Mehat6a254402010-03-22 10:21:00 -0700477
San Mehat6a965af22010-02-24 17:47:30 -0800478 /*
San Mehat6a254402010-03-22 10:21:00 -0700479 * If UMS was connected on boot, send the connected event
San Mehat6a965af22010-02-24 17:47:30 -0800480 * now that we're up.
481 */
482 if (mSendUmsConnectedOnBoot) {
483 sendUmsIntent(true);
484 mSendUmsConnectedOnBoot = false;
485 }
San Mehatfafb0412010-02-18 19:40:04 -0800486 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700487 Slog.e(TAG, "Boot-time mount exception", ex);
San Mehatfafb0412010-02-18 19:40:04 -0800488 }
San Mehat207e5382010-02-04 20:46:54 -0800489 }
San Mehatfafb0412010-02-18 19:40:04 -0800490 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
492 }
493 };
San Mehat4270e1e2010-01-29 05:32:19 -0800494 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
495 final IMountServiceListener mListener;
496
497 MountServiceBinderListener(IMountServiceListener listener) {
498 mListener = listener;
Kenny Root02c87302010-07-01 08:10:18 -0700499
San Mehat91c77612010-01-07 10:39:41 -0800500 }
501
San Mehat4270e1e2010-01-29 05:32:19 -0800502 public void binderDied() {
San Mehata5078592010-03-25 09:36:54 -0700503 if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!");
Kenny Roota02b8b02010-08-05 16:14:17 -0700504 synchronized (mListeners) {
San Mehat4270e1e2010-01-29 05:32:19 -0800505 mListeners.remove(this);
506 mListener.asBinder().unlinkToDeath(this, 0);
507 }
508 }
509 }
510
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800511 private void doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800512 // TODO: Add support for multiple share methods
513 if (!method.equals("ums")) {
514 throw new IllegalArgumentException(String.format("Method %s not supported", method));
515 }
516
San Mehat4270e1e2010-01-29 05:32:19 -0800517 try {
518 mConnector.doCommand(String.format(
519 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
520 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -0700521 Slog.e(TAG, "Failed to share/unshare", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800522 }
San Mehat4270e1e2010-01-29 05:32:19 -0800523 }
524
San Mehat207e5382010-02-04 20:46:54 -0800525 private void updatePublicVolumeState(String path, String state) {
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400526 String oldState;
527 synchronized(mVolumeStates) {
528 oldState = mVolumeStates.put(path, state);
529 }
530 if (state.equals(oldState)) {
531 Slog.w(TAG, String.format("Duplicate state transition (%s -> %s) for %s",
532 state, state, path));
San Mehat4270e1e2010-01-29 05:32:19 -0800533 return;
534 }
San Mehatb1043402010-02-05 08:26:50 -0800535
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400536 Slog.d(TAG, "volume state changed for " + path + " (" + oldState + " -> " + state + ")");
Kenny Rootaf9d6672010-10-08 09:21:39 -0700537
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400538 if (path.equals(mExternalStoragePath)) {
539 // Update state on PackageManager, but only of real events
540 if (!mEmulateExternalStorage) {
541 if (Environment.MEDIA_UNMOUNTED.equals(state)) {
542 mPms.updateExternalMediaStatus(false, false);
543
544 /*
545 * Some OBBs might have been unmounted when this volume was
546 * unmounted, so send a message to the handler to let it know to
547 * remove those from the list of mounted OBBS.
548 */
549 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(
550 OBB_FLUSH_MOUNT_STATE, path));
551 } else if (Environment.MEDIA_MOUNTED.equals(state)) {
552 mPms.updateExternalMediaStatus(true, false);
553 }
Mike Lockwood03559752010-07-19 18:25:03 -0400554 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800555 }
San Mehat4270e1e2010-01-29 05:32:19 -0800556 synchronized (mListeners) {
557 for (int i = mListeners.size() -1; i >= 0; i--) {
558 MountServiceBinderListener bl = mListeners.get(i);
559 try {
San Mehatb1043402010-02-05 08:26:50 -0800560 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800561 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700562 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800563 mListeners.remove(i);
564 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700565 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800566 }
567 }
568 }
569 }
570
571 /**
572 *
573 * Callback from NativeDaemonConnector
574 */
575 public void onDaemonConnected() {
576 /*
577 * Since we'll be calling back into the NativeDaemonConnector,
578 * we need to do our work in a new thread.
579 */
580 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600581 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800582 public void run() {
583 /**
584 * Determine media state and UMS detection status
585 */
San Mehat4270e1e2010-01-29 05:32:19 -0800586 try {
587 String[] vols = mConnector.doListCommand(
588 "volume list", VoldResponseCode.VolumeListResult);
589 for (String volstr : vols) {
590 String[] tok = volstr.split(" ");
591 // FMT: <label> <mountpoint> <state>
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400592 String path = tok[1];
593 String state = Environment.MEDIA_REMOVED;
594
San Mehat4270e1e2010-01-29 05:32:19 -0800595 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 }
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400609
610 if (state != null) {
611 if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
612 updatePublicVolumeState(path, state);
613 }
San Mehat4270e1e2010-01-29 05:32:19 -0800614 }
615 } catch (Exception e) {
San Mehata5078592010-03-25 09:36:54 -0700616 Slog.e(TAG, "Error processing initial volume state", e);
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400617 updatePublicVolumeState(mExternalStoragePath, Environment.MEDIA_REMOVED);
San Mehat4270e1e2010-01-29 05:32:19 -0800618 }
619
620 try {
San Mehat207e5382010-02-04 20:46:54 -0800621 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800622 notifyShareAvailabilityChange("ums", avail);
623 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700624 Slog.w(TAG, "Failed to get share availability");
San Mehat4270e1e2010-01-29 05:32:19 -0800625 }
San Mehat207e5382010-02-04 20:46:54 -0800626 /*
Jason parks9ed98bc2011-01-17 09:58:35 -0600627 * Now that we've done our initialization, release
San Mehat207e5382010-02-04 20:46:54 -0800628 * the hounds!
629 */
630 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800631 }
632 }.start();
633 }
634
635 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800636 * Callback from NativeDaemonConnector
637 */
638 public boolean onEvent(int code, String raw, String[] cooked) {
639 Intent in = null;
640
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800641 if (DEBUG_EVENTS) {
642 StringBuilder builder = new StringBuilder();
643 builder.append("onEvent::");
644 builder.append(" raw= " + raw);
645 if (cooked != null) {
646 builder.append(" cooked = " );
647 for (String str : cooked) {
648 builder.append(" " + str);
649 }
650 }
San Mehata5078592010-03-25 09:36:54 -0700651 Slog.i(TAG, builder.toString());
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800652 }
San Mehat4270e1e2010-01-29 05:32:19 -0800653 if (code == VoldResponseCode.VolumeStateChange) {
654 /*
655 * One of the volumes we're managing has changed state.
656 * Format: "NNN Volume <label> <path> state changed
657 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
658 */
659 notifyVolumeStateChange(
660 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
661 Integer.parseInt(cooked[10]));
662 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
663 // FMT: NNN Share method <method> now <available|unavailable>
664 boolean avail = false;
665 if (cooked[5].equals("available")) {
666 avail = true;
667 }
668 notifyShareAvailabilityChange(cooked[3], avail);
669 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
670 (code == VoldResponseCode.VolumeDiskRemoved) ||
671 (code == VoldResponseCode.VolumeBadRemoval)) {
672 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
673 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
674 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
675 final String label = cooked[2];
676 final String path = cooked[3];
677 int major = -1;
678 int minor = -1;
679
680 try {
681 String devComp = cooked[6].substring(1, cooked[6].length() -1);
682 String[] devTok = devComp.split(":");
683 major = Integer.parseInt(devTok[0]);
684 minor = Integer.parseInt(devTok[1]);
685 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700686 Slog.e(TAG, "Failed to parse major/minor", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800687 }
688
San Mehat4270e1e2010-01-29 05:32:19 -0800689 if (code == VoldResponseCode.VolumeDiskInserted) {
690 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600691 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800692 public void run() {
693 try {
694 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800695 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700696 Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
San Mehat4270e1e2010-01-29 05:32:19 -0800697 }
698 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700699 Slog.w(TAG, "Failed to mount media on insertion", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800700 }
701 }
702 }.start();
703 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
704 /*
705 * This event gets trumped if we're already in BAD_REMOVAL state
706 */
707 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
708 return true;
709 }
710 /* Send the media unmounted event first */
San Mehata5078592010-03-25 09:36:54 -0700711 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800712 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
713 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
714 mContext.sendBroadcast(in);
715
San Mehata5078592010-03-25 09:36:54 -0700716 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
San Mehat4270e1e2010-01-29 05:32:19 -0800717 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
718 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
719 } else if (code == VoldResponseCode.VolumeBadRemoval) {
San Mehata5078592010-03-25 09:36:54 -0700720 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800721 /* Send the media unmounted event first */
722 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
723 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
724 mContext.sendBroadcast(in);
725
San Mehata5078592010-03-25 09:36:54 -0700726 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
San Mehat4270e1e2010-01-29 05:32:19 -0800727 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
728 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
729 } else {
San Mehata5078592010-03-25 09:36:54 -0700730 Slog.e(TAG, String.format("Unknown code {%d}", code));
San Mehat4270e1e2010-01-29 05:32:19 -0800731 }
732 } else {
733 return false;
734 }
735
736 if (in != null) {
737 mContext.sendBroadcast(in);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400738 }
739 return true;
San Mehat4270e1e2010-01-29 05:32:19 -0800740 }
741
San Mehat207e5382010-02-04 20:46:54 -0800742 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800743 String vs = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -0700744 if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
San Mehat4270e1e2010-01-29 05:32:19 -0800745
746 Intent in = null;
747
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500748 if (oldState == VolumeState.Shared && newState != oldState) {
San Mehata5078592010-03-25 09:36:54 -0700749 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500750 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
751 Uri.parse("file://" + path)));
752 }
753
San Mehat4270e1e2010-01-29 05:32:19 -0800754 if (newState == VolumeState.Init) {
755 } else if (newState == VolumeState.NoMedia) {
756 // NoMedia is handled via Disk Remove events
757 } else if (newState == VolumeState.Idle) {
758 /*
759 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
760 * if we're in the process of enabling UMS
761 */
762 if (!vs.equals(
763 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
764 Environment.MEDIA_NOFS) && !vs.equals(
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800765 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
San Mehata5078592010-03-25 09:36:54 -0700766 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
San Mehat4270e1e2010-01-29 05:32:19 -0800767 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
768 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
769 }
770 } else if (newState == VolumeState.Pending) {
771 } else if (newState == VolumeState.Checking) {
San Mehata5078592010-03-25 09:36:54 -0700772 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
San Mehat4270e1e2010-01-29 05:32:19 -0800773 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
774 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
775 } else if (newState == VolumeState.Mounted) {
San Mehata5078592010-03-25 09:36:54 -0700776 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800777 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
San Mehat4270e1e2010-01-29 05:32:19 -0800778 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
779 in.putExtra("read-only", false);
780 } else if (newState == VolumeState.Unmounting) {
San Mehat4270e1e2010-01-29 05:32:19 -0800781 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
782 } else if (newState == VolumeState.Formatting) {
783 } else if (newState == VolumeState.Shared) {
San Mehata5078592010-03-25 09:36:54 -0700784 if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800785 /* Send the media unmounted event first */
786 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
787 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
788 mContext.sendBroadcast(in);
789
San Mehata5078592010-03-25 09:36:54 -0700790 if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
San Mehat4270e1e2010-01-29 05:32:19 -0800791 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
792 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
San Mehata5078592010-03-25 09:36:54 -0700793 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
San Mehat4270e1e2010-01-29 05:32:19 -0800794 } else if (newState == VolumeState.SharedMnt) {
San Mehata5078592010-03-25 09:36:54 -0700795 Slog.e(TAG, "Live shared mounts not supported yet!");
San Mehat4270e1e2010-01-29 05:32:19 -0800796 return;
797 } else {
San Mehata5078592010-03-25 09:36:54 -0700798 Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800799 }
800
801 if (in != null) {
802 mContext.sendBroadcast(in);
803 }
804 }
805
San Mehat207e5382010-02-04 20:46:54 -0800806 private boolean doGetShareMethodAvailable(String method) {
Kenny Root85fb2062010-06-01 20:50:21 -0700807 ArrayList<String> rsp;
Kenny Roota80ce062010-06-01 13:23:53 -0700808 try {
Kenny Root85fb2062010-06-01 20:50:21 -0700809 rsp = mConnector.doCommand("share status " + method);
Kenny Roota80ce062010-06-01 13:23:53 -0700810 } catch (NativeDaemonConnectorException ex) {
811 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
812 return false;
813 }
San Mehat207e5382010-02-04 20:46:54 -0800814
815 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700816 String[] tok = line.split(" ");
817 if (tok.length < 3) {
818 Slog.e(TAG, "Malformed response to share status " + method);
819 return false;
820 }
821
San Mehat207e5382010-02-04 20:46:54 -0800822 int code;
823 try {
824 code = Integer.parseInt(tok[0]);
825 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700826 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehat207e5382010-02-04 20:46:54 -0800827 return false;
828 }
829 if (code == VoldResponseCode.ShareStatusResult) {
830 if (tok[2].equals("available"))
831 return true;
832 return false;
833 } else {
San Mehata5078592010-03-25 09:36:54 -0700834 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehat207e5382010-02-04 20:46:54 -0800835 return false;
836 }
837 }
San Mehata5078592010-03-25 09:36:54 -0700838 Slog.e(TAG, "Got an empty response");
San Mehat207e5382010-02-04 20:46:54 -0800839 return false;
840 }
841
842 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800843 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800844
San Mehata5078592010-03-25 09:36:54 -0700845 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
San Mehat207e5382010-02-04 20:46:54 -0800846 try {
847 mConnector.doCommand(String.format("volume mount %s", path));
848 } catch (NativeDaemonConnectorException e) {
849 /*
850 * Mount failed for some reason
851 */
852 Intent in = null;
853 int code = e.getCode();
854 if (code == VoldResponseCode.OpFailedNoMedia) {
855 /*
856 * Attempt to mount but no media inserted
857 */
San Mehatb1043402010-02-05 08:26:50 -0800858 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800859 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
San Mehata5078592010-03-25 09:36:54 -0700860 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
San Mehat207e5382010-02-04 20:46:54 -0800861 /*
862 * Media is blank or does not contain a supported filesystem
863 */
864 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
865 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800866 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800867 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehata5078592010-03-25 09:36:54 -0700868 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
San Mehat207e5382010-02-04 20:46:54 -0800869 /*
870 * Volume consistency check failed
871 */
872 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
873 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800874 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800875 } else {
San Mehatb1043402010-02-05 08:26:50 -0800876 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800877 }
878
879 /*
880 * Send broadcast intent (if required for the failure)
881 */
882 if (in != null) {
883 mContext.sendBroadcast(in);
884 }
885 }
886
887 return rc;
888 }
889
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800890 /*
891 * If force is not set, we do not unmount if there are
892 * processes holding references to the volume about to be unmounted.
893 * If force is set, all the processes holding references need to be
894 * killed via the ActivityManager before actually unmounting the volume.
895 * This might even take a while and might be retried after timed delays
896 * to make sure we dont end up in an instable state and kill some core
897 * processes.
898 */
San Mehatd9709982010-02-18 11:43:03 -0800899 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800900 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800901 return VoldResponseCode.OpFailedVolNotMounted;
902 }
Kenny Rootaa485402010-09-14 14:49:41 -0700903
904 /*
905 * Force a GC to make sure AssetManagers in other threads of the
906 * system_server are cleaned up. We have to do this since AssetManager
907 * instances are kept as a WeakReference and it's possible we have files
908 * open on the external storage.
909 */
910 Runtime.getRuntime().gc();
911
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800912 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700913 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800914 try {
San Mehatd9709982010-02-18 11:43:03 -0800915 mConnector.doCommand(String.format(
916 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700917 // We unmounted the volume. None of the asec containers are available now.
918 synchronized (mAsecMountSet) {
919 mAsecMountSet.clear();
920 }
San Mehatb1043402010-02-05 08:26:50 -0800921 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800922 } catch (NativeDaemonConnectorException e) {
923 // Don't worry about mismatch in PackageManager since the
924 // call back will handle the status changes any way.
925 int code = e.getCode();
926 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800927 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800928 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
929 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800930 } else {
San Mehatb1043402010-02-05 08:26:50 -0800931 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800932 }
933 }
934 }
935
936 private int doFormatVolume(String path) {
937 try {
938 String cmd = String.format("volume format %s", path);
939 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800940 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800941 } catch (NativeDaemonConnectorException e) {
942 int code = e.getCode();
943 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800944 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800945 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800946 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800947 } else {
San Mehatb1043402010-02-05 08:26:50 -0800948 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800949 }
950 }
951 }
952
San Mehatb1043402010-02-05 08:26:50 -0800953 private boolean doGetVolumeShared(String path, String method) {
954 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700955 ArrayList<String> rsp;
956
957 try {
958 rsp = mConnector.doCommand(cmd);
959 } catch (NativeDaemonConnectorException ex) {
960 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
961 return false;
962 }
San Mehatb1043402010-02-05 08:26:50 -0800963
964 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700965 String[] tok = line.split(" ");
966 if (tok.length < 3) {
967 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
968 return false;
969 }
970
San Mehatb1043402010-02-05 08:26:50 -0800971 int code;
972 try {
973 code = Integer.parseInt(tok[0]);
974 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700975 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800976 return false;
977 }
978 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700979 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800980 } else {
San Mehata5078592010-03-25 09:36:54 -0700981 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800982 return false;
983 }
984 }
San Mehata5078592010-03-25 09:36:54 -0700985 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800986 return false;
987 }
988
San Mehat207e5382010-02-04 20:46:54 -0800989 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800990 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -0700991 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800992 return;
993 }
994
995 synchronized (mListeners) {
996 for (int i = mListeners.size() -1; i >= 0; i--) {
997 MountServiceBinderListener bl = mListeners.get(i);
998 try {
San Mehatb1043402010-02-05 08:26:50 -0800999 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -08001000 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001001 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -08001002 mListeners.remove(i);
1003 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -07001004 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -08001005 }
1006 }
1007 }
1008
San Mehat207e5382010-02-04 20:46:54 -08001009 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -08001010 sendUmsIntent(avail);
1011 } else {
1012 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -08001013 }
San Mehat2fe718a2010-03-11 12:01:49 -08001014
1015 final String path = Environment.getExternalStorageDirectory().getPath();
1016 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
1017 /*
1018 * USB mass storage disconnected while enabled
1019 */
1020 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -06001021 @Override
San Mehat2fe718a2010-03-11 12:01:49 -08001022 public void run() {
1023 try {
1024 int rc;
San Mehata5078592010-03-25 09:36:54 -07001025 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -08001026 doShareUnshareVolume(path, "ums", false);
1027 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001028 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -08001029 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
1030 path, rc));
1031 }
1032 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -07001033 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -08001034 }
1035 }
1036 }.start();
1037 }
San Mehat4270e1e2010-01-29 05:32:19 -08001038 }
1039
San Mehat6a965af22010-02-24 17:47:30 -08001040 private void sendUmsIntent(boolean c) {
1041 mContext.sendBroadcast(
1042 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
1043 }
1044
San Mehat207e5382010-02-04 20:46:54 -08001045 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -08001046 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
1047 throw new SecurityException(String.format("Requires %s permission", perm));
1048 }
1049 }
1050
1051 /**
San Mehat207e5382010-02-04 20:46:54 -08001052 * Constructs a new MountService instance
1053 *
1054 * @param context Binder context for this service
1055 */
1056 public MountService(Context context) {
1057 mContext = context;
1058
Mike Lockwood7fa24aa2011-03-23 14:52:34 -04001059 mExternalStoragePath = Environment.getExternalStorageDirectory().getPath();
Mike Lockwood03559752010-07-19 18:25:03 -04001060 mEmulateExternalStorage = context.getResources().getBoolean(
1061 com.android.internal.R.bool.config_emulateExternalStorage);
1062 if (mEmulateExternalStorage) {
1063 Slog.d(TAG, "using emulated external storage");
Mike Lockwood7fa24aa2011-03-23 14:52:34 -04001064 mVolumeStates.put(mExternalStoragePath, Environment.MEDIA_MOUNTED);
Mike Lockwood03559752010-07-19 18:25:03 -04001065 }
1066
San Mehat207e5382010-02-04 20:46:54 -08001067 // XXX: This will go away soon in favor of IMountServiceObserver
1068 mPms = (PackageManagerService) ServiceManager.getService("package");
1069
1070 mContext.registerReceiver(mBroadcastReceiver,
1071 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
1072
Daniel Sandler5f27ef42010-03-16 15:42:02 -04001073 mHandlerThread = new HandlerThread("MountService");
1074 mHandlerThread.start();
1075 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
1076
Kenny Roota02b8b02010-08-05 16:14:17 -07001077 // Add OBB Action Handler to MountService thread.
1078 mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper());
1079
Marco Nelissenc34ebce2010-02-18 13:39:41 -08001080 /*
1081 * Vold does not run in the simulator, so pretend the connector thread
1082 * ran and did its thing.
1083 */
1084 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
1085 mReady = true;
1086 mUmsEnabling = true;
1087 return;
1088 }
1089
Kenny Root305bcbf2010-09-03 07:56:38 -07001090 /*
1091 * Create the connection to vold with a maximum queue of twice the
1092 * amount of containers we'd ever expect to have. This keeps an
1093 * "asec list" from blocking a thread repeatedly.
1094 */
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001095 mConnector = new NativeDaemonConnector(this, "vold", MAX_CONTAINERS * 2, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001096 mReady = false;
Kenny Root305bcbf2010-09-03 07:56:38 -07001097 Thread thread = new Thread(mConnector, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001098 thread.start();
1099 }
1100
1101 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001102 * Exposed API calls below here
1103 */
1104
1105 public void registerListener(IMountServiceListener listener) {
1106 synchronized (mListeners) {
1107 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
1108 try {
1109 listener.asBinder().linkToDeath(bl, 0);
1110 mListeners.add(bl);
1111 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001112 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -08001113 }
1114 }
1115 }
1116
1117 public void unregisterListener(IMountServiceListener listener) {
1118 synchronized (mListeners) {
1119 for(MountServiceBinderListener bl : mListeners) {
1120 if (bl.mListener == listener) {
1121 mListeners.remove(mListeners.indexOf(bl));
1122 return;
1123 }
1124 }
1125 }
1126 }
1127
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001128 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -08001129 validatePermission(android.Manifest.permission.SHUTDOWN);
1130
San Mehata5078592010-03-25 09:36:54 -07001131 Slog.i(TAG, "Shutting down");
Mike Lockwood7fa24aa2011-03-23 14:52:34 -04001132 synchronized (mVolumeStates) {
1133 for (String path : mVolumeStates.keySet()) {
1134 String state = mVolumeStates.get(path);
San Mehat4270e1e2010-01-29 05:32:19 -08001135
Mike Lockwood7fa24aa2011-03-23 14:52:34 -04001136 if (state.equals(Environment.MEDIA_SHARED)) {
1137 /*
1138 * If the media is currently shared, unshare it.
1139 * XXX: This is still dangerous!. We should not
1140 * be rebooting at *all* if UMS is enabled, since
1141 * the UMS host could have dirty FAT cache entries
1142 * yet to flush.
1143 */
1144 setUsbMassStorageEnabled(false);
1145 } else if (state.equals(Environment.MEDIA_CHECKING)) {
1146 /*
1147 * If the media is being checked, then we need to wait for
1148 * it to complete before being able to proceed.
1149 */
1150 // XXX: @hackbod - Should we disable the ANR timer here?
1151 int retries = 30;
1152 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
1153 try {
1154 Thread.sleep(1000);
1155 } catch (InterruptedException iex) {
1156 Slog.e(TAG, "Interrupted while waiting for media", iex);
1157 break;
1158 }
1159 state = Environment.getExternalStorageState();
1160 }
1161 if (retries == 0) {
1162 Slog.e(TAG, "Timed out waiting for media to check");
1163 }
San Mehat91c77612010-01-07 10:39:41 -08001164 }
San Mehat91c77612010-01-07 10:39:41 -08001165
Mike Lockwood7fa24aa2011-03-23 14:52:34 -04001166 if (state.equals(Environment.MEDIA_MOUNTED)) {
1167 // Post a unmount message.
1168 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
1169 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
1170 } else if (observer != null) {
1171 /*
1172 * Observer is waiting for onShutDownComplete when we are done.
1173 * Since nothing will be done send notification directly so shutdown
1174 * sequence can continue.
1175 */
1176 try {
1177 observer.onShutDownComplete(StorageResultCode.OperationSucceeded);
1178 } catch (RemoteException e) {
1179 Slog.w(TAG, "RemoteException when shutting down");
1180 }
1181 }
Johan Alfven5d0db4d2010-11-09 10:32:25 +01001182 }
San Mehat4270e1e2010-01-29 05:32:19 -08001183 }
1184 }
1185
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001186 private boolean getUmsEnabling() {
1187 synchronized (mListeners) {
1188 return mUmsEnabling;
1189 }
1190 }
1191
1192 private void setUmsEnabling(boolean enable) {
1193 synchronized (mListeners) {
Tony Wufc711252010-08-09 16:49:19 +08001194 mUmsEnabling = enable;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001195 }
1196 }
1197
San Mehatb1043402010-02-05 08:26:50 -08001198 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -08001199 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -08001200
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001201 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001202 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001203 }
San Mehatb1043402010-02-05 08:26:50 -08001204 return doGetShareMethodAvailable("ums");
1205 }
1206
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001207 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001208 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001209 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001210
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001211 // TODO: Add support for multiple share methods
1212
1213 /*
1214 * If the volume is mounted and we're enabling then unmount it
1215 */
1216 String path = Environment.getExternalStorageDirectory().getPath();
1217 String vs = getVolumeState(path);
1218 String method = "ums";
1219 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1220 // Override for isUsbMassStorageEnabled()
1221 setUmsEnabling(enable);
1222 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1223 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1224 // Clear override
1225 setUmsEnabling(false);
1226 }
1227 /*
1228 * If we disabled UMS then mount the volume
1229 */
1230 if (!enable) {
1231 doShareUnshareVolume(path, method, enable);
1232 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001233 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001234 " after disabling share method " + method);
1235 /*
1236 * Even though the mount failed, the unshare didn't so don't indicate an error.
1237 * The mountVolume() call will have set the storage state and sent the necessary
1238 * broadcasts.
1239 */
1240 }
1241 }
San Mehatb1043402010-02-05 08:26:50 -08001242 }
1243
1244 public boolean isUsbMassStorageEnabled() {
1245 waitForReady();
1246 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001248
San Mehat7fd0fee2009-12-17 07:12:23 -08001249 /**
1250 * @return state of the volume at the specified mount point
1251 */
San Mehat4270e1e2010-01-29 05:32:19 -08001252 public String getVolumeState(String mountPoint) {
Mike Lockwood7fa24aa2011-03-23 14:52:34 -04001253 synchronized (mVolumeStates) {
1254 String state = mVolumeStates.get(mountPoint);
1255 if (state == null) {
1256 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
1257 throw new IllegalArgumentException();
1258 }
San Mehat7fd0fee2009-12-17 07:12:23 -08001259
Mike Lockwood7fa24aa2011-03-23 14:52:34 -04001260 return state;
1261 }
San Mehat7fd0fee2009-12-17 07:12:23 -08001262 }
1263
Kenny Roote1ff2142010-10-12 11:20:01 -07001264 public boolean isExternalStorageEmulated() {
1265 return mEmulateExternalStorage;
1266 }
1267
San Mehat4270e1e2010-01-29 05:32:19 -08001268 public int mountVolume(String path) {
1269 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001270
San Mehat207e5382010-02-04 20:46:54 -08001271 waitForReady();
1272 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 }
1274
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001275 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001276 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001277 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001279 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001280 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001281 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1282 Environment.MEDIA_REMOVED.equals(volState) ||
1283 Environment.MEDIA_SHARED.equals(volState) ||
1284 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1285 // Media already unmounted or cannot be unmounted.
1286 // TODO return valid return code when adding observer call back.
1287 return;
1288 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001289 UnmountCallBack ucb = new UnmountCallBack(path, force);
1290 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 }
1292
San Mehat4270e1e2010-01-29 05:32:19 -08001293 public int formatVolume(String path) {
1294 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001295 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001296
San Mehat207e5382010-02-04 20:46:54 -08001297 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 }
1299
San Mehatc1b4ce92010-02-16 17:13:03 -08001300 public int []getStorageUsers(String path) {
1301 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1302 waitForReady();
1303 try {
1304 String[] r = mConnector.doListCommand(
1305 String.format("storage users %s", path),
1306 VoldResponseCode.StorageUsersListResult);
1307 // FMT: <pid> <process name>
1308 int[] data = new int[r.length];
1309 for (int i = 0; i < r.length; i++) {
1310 String []tok = r[i].split(" ");
1311 try {
1312 data[i] = Integer.parseInt(tok[0]);
1313 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001314 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001315 return new int[0];
1316 }
1317 }
1318 return data;
1319 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001320 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001321 return new int[0];
1322 }
1323 }
1324
San Mehatb1043402010-02-05 08:26:50 -08001325 private void warnOnNotMounted() {
1326 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001327 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001328 }
1329 }
1330
San Mehat4270e1e2010-01-29 05:32:19 -08001331 public String[] getSecureContainerList() {
1332 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001333 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001334 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001335
San Mehat4270e1e2010-01-29 05:32:19 -08001336 try {
1337 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1338 } catch (NativeDaemonConnectorException e) {
1339 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 }
1341 }
San Mehat36972292010-01-06 11:06:32 -08001342
San Mehat4270e1e2010-01-29 05:32:19 -08001343 public int createSecureContainer(String id, int sizeMb, String fstype,
1344 String key, int ownerUid) {
1345 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001346 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001347 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001348
San Mehatb1043402010-02-05 08:26:50 -08001349 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001350 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1351 try {
1352 mConnector.doCommand(cmd);
1353 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001354 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001355 }
San Mehata181b212010-02-11 06:50:20 -08001356
1357 if (rc == StorageResultCode.OperationSucceeded) {
1358 synchronized (mAsecMountSet) {
1359 mAsecMountSet.add(id);
1360 }
1361 }
San Mehat4270e1e2010-01-29 05:32:19 -08001362 return rc;
San Mehat36972292010-01-06 11:06:32 -08001363 }
1364
San Mehat4270e1e2010-01-29 05:32:19 -08001365 public int finalizeSecureContainer(String id) {
1366 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001367 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001368
San Mehatb1043402010-02-05 08:26:50 -08001369 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001370 try {
1371 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001372 /*
1373 * Finalization does a remount, so no need
1374 * to update mAsecMountSet
1375 */
San Mehat4270e1e2010-01-29 05:32:19 -08001376 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001377 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001378 }
San Mehat4270e1e2010-01-29 05:32:19 -08001379 return rc;
San Mehat36972292010-01-06 11:06:32 -08001380 }
1381
San Mehatd9709982010-02-18 11:43:03 -08001382 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001383 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001384 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001385 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001386
Kenny Rootaa485402010-09-14 14:49:41 -07001387 /*
1388 * Force a GC to make sure AssetManagers in other threads of the
1389 * system_server are cleaned up. We have to do this since AssetManager
1390 * instances are kept as a WeakReference and it's possible we have files
1391 * open on the external storage.
1392 */
1393 Runtime.getRuntime().gc();
1394
San Mehatb1043402010-02-05 08:26:50 -08001395 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001396 try {
San Mehatd9709982010-02-18 11:43:03 -08001397 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001398 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001399 int code = e.getCode();
1400 if (code == VoldResponseCode.OpFailedStorageBusy) {
1401 rc = StorageResultCode.OperationFailedStorageBusy;
1402 } else {
1403 rc = StorageResultCode.OperationFailedInternalError;
1404 }
San Mehat02735bc2010-01-26 15:18:08 -08001405 }
San Mehata181b212010-02-11 06:50:20 -08001406
1407 if (rc == StorageResultCode.OperationSucceeded) {
1408 synchronized (mAsecMountSet) {
1409 if (mAsecMountSet.contains(id)) {
1410 mAsecMountSet.remove(id);
1411 }
1412 }
1413 }
1414
San Mehat4270e1e2010-01-29 05:32:19 -08001415 return rc;
San Mehat36972292010-01-06 11:06:32 -08001416 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001417
San Mehat4270e1e2010-01-29 05:32:19 -08001418 public int mountSecureContainer(String id, String key, int ownerUid) {
1419 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001420 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001421 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001422
San Mehata181b212010-02-11 06:50:20 -08001423 synchronized (mAsecMountSet) {
1424 if (mAsecMountSet.contains(id)) {
1425 return StorageResultCode.OperationFailedStorageMounted;
1426 }
1427 }
1428
San Mehatb1043402010-02-05 08:26:50 -08001429 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001430 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1431 try {
1432 mConnector.doCommand(cmd);
1433 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001434 int code = e.getCode();
1435 if (code != VoldResponseCode.OpFailedStorageBusy) {
1436 rc = StorageResultCode.OperationFailedInternalError;
1437 }
San Mehat02735bc2010-01-26 15:18:08 -08001438 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001439
1440 if (rc == StorageResultCode.OperationSucceeded) {
1441 synchronized (mAsecMountSet) {
1442 mAsecMountSet.add(id);
1443 }
1444 }
San Mehat4270e1e2010-01-29 05:32:19 -08001445 return rc;
San Mehat36972292010-01-06 11:06:32 -08001446 }
1447
San Mehatd9709982010-02-18 11:43:03 -08001448 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001449 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001450 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001451 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001452
San Mehat6cdd9c02010-02-09 14:45:20 -08001453 synchronized (mAsecMountSet) {
1454 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001455 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001456 }
1457 }
1458
Kenny Rootaa485402010-09-14 14:49:41 -07001459 /*
1460 * Force a GC to make sure AssetManagers in other threads of the
1461 * system_server are cleaned up. We have to do this since AssetManager
1462 * instances are kept as a WeakReference and it's possible we have files
1463 * open on the external storage.
1464 */
1465 Runtime.getRuntime().gc();
1466
San Mehatb1043402010-02-05 08:26:50 -08001467 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001468 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001469 try {
1470 mConnector.doCommand(cmd);
1471 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001472 int code = e.getCode();
1473 if (code == VoldResponseCode.OpFailedStorageBusy) {
1474 rc = StorageResultCode.OperationFailedStorageBusy;
1475 } else {
1476 rc = StorageResultCode.OperationFailedInternalError;
1477 }
San Mehat02735bc2010-01-26 15:18:08 -08001478 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001479
1480 if (rc == StorageResultCode.OperationSucceeded) {
1481 synchronized (mAsecMountSet) {
1482 mAsecMountSet.remove(id);
1483 }
1484 }
San Mehat4270e1e2010-01-29 05:32:19 -08001485 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001486 }
1487
San Mehat6cdd9c02010-02-09 14:45:20 -08001488 public boolean isSecureContainerMounted(String id) {
1489 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1490 waitForReady();
1491 warnOnNotMounted();
1492
1493 synchronized (mAsecMountSet) {
1494 return mAsecMountSet.contains(id);
1495 }
1496 }
1497
San Mehat4270e1e2010-01-29 05:32:19 -08001498 public int renameSecureContainer(String oldId, String newId) {
1499 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001500 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001501 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001502
San Mehata181b212010-02-11 06:50:20 -08001503 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001504 /*
Jason parks9ed98bc2011-01-17 09:58:35 -06001505 * Because a mounted container has active internal state which cannot be
San Mehat85451ee2010-02-24 08:54:18 -08001506 * changed while active, we must ensure both ids are not currently mounted.
1507 */
1508 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001509 return StorageResultCode.OperationFailedStorageMounted;
1510 }
1511 }
1512
San Mehatb1043402010-02-05 08:26:50 -08001513 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001514 String cmd = String.format("asec rename %s %s", oldId, newId);
1515 try {
1516 mConnector.doCommand(cmd);
1517 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001518 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001519 }
San Mehata181b212010-02-11 06:50:20 -08001520
San Mehat4270e1e2010-01-29 05:32:19 -08001521 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001522 }
1523
San Mehat4270e1e2010-01-29 05:32:19 -08001524 public String getSecureContainerPath(String id) {
1525 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001526 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001527 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001528
San Mehat2d66cef2010-03-23 11:12:52 -07001529 try {
1530 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1531 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001532 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001533 if (code != VoldResponseCode.AsecPathResult) {
1534 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1535 }
1536 return tok[1];
1537 } catch (NativeDaemonConnectorException e) {
1538 int code = e.getCode();
1539 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1540 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001541 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001542 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001543 }
1544 }
San Mehat22dd86e2010-01-12 12:21:18 -08001545 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001546
1547 public void finishMediaUpdate() {
1548 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1549 }
Kenny Root02c87302010-07-01 08:10:18 -07001550
Kenny Roota02b8b02010-08-05 16:14:17 -07001551 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
1552 if (callerUid == android.os.Process.SYSTEM_UID) {
1553 return true;
1554 }
1555
Kenny Root02c87302010-07-01 08:10:18 -07001556 if (packageName == null) {
1557 return false;
1558 }
1559
1560 final int packageUid = mPms.getPackageUid(packageName);
1561
1562 if (DEBUG_OBB) {
1563 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
1564 packageUid + ", callerUid = " + callerUid);
1565 }
1566
1567 return callerUid == packageUid;
1568 }
1569
1570 public String getMountedObbPath(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001571 if (filename == null) {
1572 throw new IllegalArgumentException("filename cannot be null");
1573 }
1574
Kenny Root02c87302010-07-01 08:10:18 -07001575 waitForReady();
1576 warnOnNotMounted();
1577
Kenny Root02c87302010-07-01 08:10:18 -07001578 try {
1579 ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename));
1580 String []tok = rsp.get(0).split(" ");
1581 int code = Integer.parseInt(tok[0]);
1582 if (code != VoldResponseCode.AsecPathResult) {
1583 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1584 }
1585 return tok[1];
1586 } catch (NativeDaemonConnectorException e) {
1587 int code = e.getCode();
1588 if (code == VoldResponseCode.OpFailedStorageNotFound) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001589 return null;
Kenny Root02c87302010-07-01 08:10:18 -07001590 } else {
1591 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1592 }
1593 }
1594 }
1595
1596 public boolean isObbMounted(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001597 if (filename == null) {
1598 throw new IllegalArgumentException("filename cannot be null");
Kenny Root02c87302010-07-01 08:10:18 -07001599 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001600
1601 synchronized (mObbMounts) {
1602 return mObbPathToStateMap.containsKey(filename);
1603 }
Kenny Root02c87302010-07-01 08:10:18 -07001604 }
1605
Kenny Rootaf9d6672010-10-08 09:21:39 -07001606 public void mountObb(String filename, String key, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -07001607 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001608 if (filename == null) {
1609 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootaf9d6672010-10-08 09:21:39 -07001610 }
1611
1612 if (token == null) {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001613 throw new IllegalArgumentException("token cannot be null");
1614 }
1615
Kenny Rootaf9d6672010-10-08 09:21:39 -07001616 final int callerUid = Binder.getCallingUid();
1617 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1618 final ObbAction action = new MountObbAction(obbState, key);
Kenny Roota02b8b02010-08-05 16:14:17 -07001619 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
1620
1621 if (DEBUG_OBB)
1622 Slog.i(TAG, "Send to OBB handler: " + action.toString());
Kenny Root02c87302010-07-01 08:10:18 -07001623 }
1624
Kenny Rootaf9d6672010-10-08 09:21:39 -07001625 public void unmountObb(String filename, boolean force, IObbActionListener token, int nonce)
1626 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001627 if (filename == null) {
1628 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootf1121dc2010-09-29 07:30:53 -07001629 }
1630
Kenny Rootaf9d6672010-10-08 09:21:39 -07001631 final int callerUid = Binder.getCallingUid();
1632 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1633 final ObbAction action = new UnmountObbAction(obbState, force);
Kenny Roota02b8b02010-08-05 16:14:17 -07001634 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root02c87302010-07-01 08:10:18 -07001635
Kenny Roota02b8b02010-08-05 16:14:17 -07001636 if (DEBUG_OBB)
1637 Slog.i(TAG, "Send to OBB handler: " + action.toString());
1638 }
1639
Jason parks5af0b912010-11-29 09:05:25 -06001640 public int decryptStorage(String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06001641 if (TextUtils.isEmpty(password)) {
1642 throw new IllegalArgumentException("password cannot be empty");
Jason parks5af0b912010-11-29 09:05:25 -06001643 }
1644
Jason parks8888c592011-01-20 22:46:41 -06001645 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1646 "no permission to access the crypt keeper");
Jason parks5af0b912010-11-29 09:05:25 -06001647
1648 waitForReady();
1649
1650 if (DEBUG_EVENTS) {
1651 Slog.i(TAG, "decrypting storage...");
1652 }
1653
1654 try {
Jason parks9ed98bc2011-01-17 09:58:35 -06001655 ArrayList<String> rsp = mConnector.doCommand("cryptfs checkpw " + password);
Jason parksf7b3cd42011-01-27 09:28:25 -06001656 String[] tokens = rsp.get(0).split(" ");
Jason parks9ed98bc2011-01-17 09:58:35 -06001657
Jason parksf7b3cd42011-01-27 09:28:25 -06001658 if (tokens == null || tokens.length != 2) {
Jason parks9ed98bc2011-01-17 09:58:35 -06001659 return -1;
1660 }
1661
Jason parksf7b3cd42011-01-27 09:28:25 -06001662 int code = Integer.parseInt(tokens[1]);
Jason parks9ed98bc2011-01-17 09:58:35 -06001663
1664 if (code == 0) {
1665 // Decrypt was successful. Post a delayed message before restarting in order
1666 // to let the UI to clear itself
1667 mHandler.postDelayed(new Runnable() {
1668 public void run() {
1669 mConnector.doCommand(String.format("cryptfs restart"));
1670 }
Jason parksf7b3cd42011-01-27 09:28:25 -06001671 }, 1000); // 1 second
Jason parks9ed98bc2011-01-17 09:58:35 -06001672 }
1673
1674 return code;
Jason parks5af0b912010-11-29 09:05:25 -06001675 } catch (NativeDaemonConnectorException e) {
1676 // Decryption failed
1677 return e.getCode();
1678 }
Jason parks5af0b912010-11-29 09:05:25 -06001679 }
1680
Jason parks56aa5322011-01-07 09:01:15 -06001681 public int encryptStorage(String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06001682 if (TextUtils.isEmpty(password)) {
1683 throw new IllegalArgumentException("password cannot be empty");
Jason parks56aa5322011-01-07 09:01:15 -06001684 }
1685
Jason parks8888c592011-01-20 22:46:41 -06001686 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1687 "no permission to access the crypt keeper");
Jason parks56aa5322011-01-07 09:01:15 -06001688
1689 waitForReady();
1690
1691 if (DEBUG_EVENTS) {
Jason parks8888c592011-01-20 22:46:41 -06001692 Slog.i(TAG, "encrypting storage...");
Jason parks56aa5322011-01-07 09:01:15 -06001693 }
1694
1695 try {
Jason parks9ed98bc2011-01-17 09:58:35 -06001696 mConnector.doCommand(String.format("cryptfs enablecrypto inplace %s", password));
Jason parks56aa5322011-01-07 09:01:15 -06001697 } catch (NativeDaemonConnectorException e) {
1698 // Encryption failed
1699 return e.getCode();
1700 }
1701
1702 return 0;
1703 }
1704
Jason parksf7b3cd42011-01-27 09:28:25 -06001705 public int changeEncryptionPassword(String password) {
1706 if (TextUtils.isEmpty(password)) {
1707 throw new IllegalArgumentException("password cannot be empty");
1708 }
1709
1710 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1711 "no permission to access the crypt keeper");
1712
1713 waitForReady();
1714
1715 if (DEBUG_EVENTS) {
1716 Slog.i(TAG, "changing encryption password...");
1717 }
1718
1719 try {
1720 ArrayList<String> response = mConnector.doCommand("cryptfs changepw " + password);
1721
1722 String[] tokens = response.get(0).split(" ");
1723
1724 if (tokens == null || tokens.length != 2) {
1725 return -1;
1726 }
1727
1728 return Integer.parseInt(tokens[1]);
1729 } catch (NativeDaemonConnectorException e) {
1730 // Encryption failed
1731 return e.getCode();
1732 }
1733 }
1734
Kenny Rootaf9d6672010-10-08 09:21:39 -07001735 private void addObbStateLocked(ObbState obbState) throws RemoteException {
1736 final IBinder binder = obbState.getBinder();
1737 List<ObbState> obbStates = mObbMounts.get(binder);
Kenny Root5919ac62010-10-05 09:49:40 -07001738
Kenny Rootaf9d6672010-10-08 09:21:39 -07001739 if (obbStates == null) {
1740 obbStates = new ArrayList<ObbState>();
1741 mObbMounts.put(binder, obbStates);
1742 } else {
1743 for (final ObbState o : obbStates) {
1744 if (o.filename.equals(obbState.filename)) {
1745 throw new IllegalStateException("Attempt to add ObbState twice. "
1746 + "This indicates an error in the MountService logic.");
Kenny Root5919ac62010-10-05 09:49:40 -07001747 }
1748 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001749 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001750
1751 obbStates.add(obbState);
1752 try {
1753 obbState.link();
1754 } catch (RemoteException e) {
1755 /*
1756 * The binder died before we could link it, so clean up our state
1757 * and return failure.
1758 */
1759 obbStates.remove(obbState);
1760 if (obbStates.isEmpty()) {
1761 mObbMounts.remove(binder);
1762 }
1763
1764 // Rethrow the error so mountObb can get it
1765 throw e;
1766 }
1767
1768 mObbPathToStateMap.put(obbState.filename, obbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07001769 }
1770
Kenny Rootaf9d6672010-10-08 09:21:39 -07001771 private void removeObbStateLocked(ObbState obbState) {
1772 final IBinder binder = obbState.getBinder();
1773 final List<ObbState> obbStates = mObbMounts.get(binder);
1774 if (obbStates != null) {
1775 if (obbStates.remove(obbState)) {
1776 obbState.unlink();
Kenny Root05105f72010-09-22 17:29:43 -07001777 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001778 if (obbStates.isEmpty()) {
1779 mObbMounts.remove(binder);
1780 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001781 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001782
Kenny Rootaf9d6672010-10-08 09:21:39 -07001783 mObbPathToStateMap.remove(obbState.filename);
Kenny Root38cf8862010-09-26 14:18:51 -07001784 }
1785
Kenny Roota02b8b02010-08-05 16:14:17 -07001786 private class ObbActionHandler extends Handler {
1787 private boolean mBound = false;
Kenny Root480afe72010-10-07 10:17:50 -07001788 private final List<ObbAction> mActions = new LinkedList<ObbAction>();
Kenny Roota02b8b02010-08-05 16:14:17 -07001789
1790 ObbActionHandler(Looper l) {
1791 super(l);
1792 }
1793
1794 @Override
1795 public void handleMessage(Message msg) {
1796 switch (msg.what) {
1797 case OBB_RUN_ACTION: {
Kenny Root480afe72010-10-07 10:17:50 -07001798 final ObbAction action = (ObbAction) msg.obj;
Kenny Roota02b8b02010-08-05 16:14:17 -07001799
1800 if (DEBUG_OBB)
1801 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
1802
1803 // If a bind was already initiated we don't really
1804 // need to do anything. The pending install
1805 // will be processed later on.
1806 if (!mBound) {
1807 // If this is the only one pending we might
1808 // have to bind to the service again.
1809 if (!connectToService()) {
1810 Slog.e(TAG, "Failed to bind to media container service");
1811 action.handleError();
1812 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07001813 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001814 }
Kenny Root735de3b2010-09-30 14:11:39 -07001815
Kenny Root735de3b2010-09-30 14:11:39 -07001816 mActions.add(action);
Kenny Roota02b8b02010-08-05 16:14:17 -07001817 break;
1818 }
1819 case OBB_MCS_BOUND: {
1820 if (DEBUG_OBB)
1821 Slog.i(TAG, "OBB_MCS_BOUND");
1822 if (msg.obj != null) {
1823 mContainerService = (IMediaContainerService) msg.obj;
1824 }
1825 if (mContainerService == null) {
1826 // Something seriously wrong. Bail out
1827 Slog.e(TAG, "Cannot bind to media container service");
1828 for (ObbAction action : mActions) {
1829 // Indicate service bind error
1830 action.handleError();
1831 }
1832 mActions.clear();
1833 } else if (mActions.size() > 0) {
Kenny Root480afe72010-10-07 10:17:50 -07001834 final ObbAction action = mActions.get(0);
Kenny Roota02b8b02010-08-05 16:14:17 -07001835 if (action != null) {
1836 action.execute(this);
1837 }
1838 } else {
1839 // Should never happen ideally.
1840 Slog.w(TAG, "Empty queue");
1841 }
1842 break;
1843 }
1844 case OBB_MCS_RECONNECT: {
1845 if (DEBUG_OBB)
1846 Slog.i(TAG, "OBB_MCS_RECONNECT");
1847 if (mActions.size() > 0) {
1848 if (mBound) {
1849 disconnectService();
1850 }
1851 if (!connectToService()) {
1852 Slog.e(TAG, "Failed to bind to media container service");
1853 for (ObbAction action : mActions) {
1854 // Indicate service bind error
1855 action.handleError();
1856 }
1857 mActions.clear();
1858 }
1859 }
1860 break;
1861 }
1862 case OBB_MCS_UNBIND: {
1863 if (DEBUG_OBB)
1864 Slog.i(TAG, "OBB_MCS_UNBIND");
1865
1866 // Delete pending install
1867 if (mActions.size() > 0) {
1868 mActions.remove(0);
1869 }
1870 if (mActions.size() == 0) {
1871 if (mBound) {
1872 disconnectService();
1873 }
1874 } else {
1875 // There are more pending requests in queue.
1876 // Just post MCS_BOUND message to trigger processing
1877 // of next pending install.
1878 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1879 }
1880 break;
1881 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001882 case OBB_FLUSH_MOUNT_STATE: {
1883 final String path = (String) msg.obj;
1884
1885 if (DEBUG_OBB)
1886 Slog.i(TAG, "Flushing all OBB state for path " + path);
1887
1888 synchronized (mObbMounts) {
1889 final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>();
1890
1891 final Iterator<Entry<String, ObbState>> i =
1892 mObbPathToStateMap.entrySet().iterator();
1893 while (i.hasNext()) {
1894 final Entry<String, ObbState> obbEntry = i.next();
1895
1896 /*
1897 * If this entry's source file is in the volume path
1898 * that got unmounted, remove it because it's no
1899 * longer valid.
1900 */
1901 if (obbEntry.getKey().startsWith(path)) {
1902 obbStatesToRemove.add(obbEntry.getValue());
1903 }
1904 }
1905
1906 for (final ObbState obbState : obbStatesToRemove) {
1907 if (DEBUG_OBB)
1908 Slog.i(TAG, "Removing state for " + obbState.filename);
1909
1910 removeObbStateLocked(obbState);
1911
1912 try {
1913 obbState.token.onObbResult(obbState.filename, obbState.nonce,
1914 OnObbStateChangeListener.UNMOUNTED);
1915 } catch (RemoteException e) {
1916 Slog.i(TAG, "Couldn't send unmount notification for OBB: "
1917 + obbState.filename);
1918 }
1919 }
1920 }
1921 break;
1922 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001923 }
1924 }
1925
1926 private boolean connectToService() {
1927 if (DEBUG_OBB)
1928 Slog.i(TAG, "Trying to bind to DefaultContainerService");
1929
1930 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
1931 if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) {
1932 mBound = true;
1933 return true;
1934 }
1935 return false;
1936 }
1937
1938 private void disconnectService() {
1939 mContainerService = null;
1940 mBound = false;
1941 mContext.unbindService(mDefContainerConn);
1942 }
1943 }
1944
1945 abstract class ObbAction {
1946 private static final int MAX_RETRIES = 3;
1947 private int mRetries;
1948
1949 ObbState mObbState;
1950
1951 ObbAction(ObbState obbState) {
1952 mObbState = obbState;
1953 }
1954
1955 public void execute(ObbActionHandler handler) {
1956 try {
1957 if (DEBUG_OBB)
1958 Slog.i(TAG, "Starting to execute action: " + this.toString());
1959 mRetries++;
1960 if (mRetries > MAX_RETRIES) {
1961 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
Kenny Root480afe72010-10-07 10:17:50 -07001962 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001963 handleError();
1964 return;
1965 } else {
1966 handleExecute();
1967 if (DEBUG_OBB)
1968 Slog.i(TAG, "Posting install MCS_UNBIND");
1969 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1970 }
1971 } catch (RemoteException e) {
1972 if (DEBUG_OBB)
1973 Slog.i(TAG, "Posting install MCS_RECONNECT");
1974 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
1975 } catch (Exception e) {
1976 if (DEBUG_OBB)
1977 Slog.d(TAG, "Error handling OBB action", e);
1978 handleError();
Kenny Root17eb6fb2010-10-06 15:02:52 -07001979 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001980 }
1981 }
1982
Kenny Root05105f72010-09-22 17:29:43 -07001983 abstract void handleExecute() throws RemoteException, IOException;
Kenny Roota02b8b02010-08-05 16:14:17 -07001984 abstract void handleError();
Kenny Root38cf8862010-09-26 14:18:51 -07001985
1986 protected ObbInfo getObbInfo() throws IOException {
1987 ObbInfo obbInfo;
1988 try {
1989 obbInfo = mContainerService.getObbInfo(mObbState.filename);
1990 } catch (RemoteException e) {
1991 Slog.d(TAG, "Couldn't call DefaultContainerService to fetch OBB info for "
1992 + mObbState.filename);
1993 obbInfo = null;
1994 }
1995 if (obbInfo == null) {
1996 throw new IOException("Couldn't read OBB file: " + mObbState.filename);
1997 }
1998 return obbInfo;
1999 }
2000
Kenny Rootaf9d6672010-10-08 09:21:39 -07002001 protected void sendNewStatusOrIgnore(int status) {
2002 if (mObbState == null || mObbState.token == null) {
2003 return;
2004 }
2005
Kenny Root38cf8862010-09-26 14:18:51 -07002006 try {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002007 mObbState.token.onObbResult(mObbState.filename, mObbState.nonce, status);
Kenny Root38cf8862010-09-26 14:18:51 -07002008 } catch (RemoteException e) {
2009 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
2010 }
2011 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002012 }
2013
2014 class MountObbAction extends ObbAction {
2015 private String mKey;
2016
2017 MountObbAction(ObbState obbState, String key) {
2018 super(obbState);
2019 mKey = key;
2020 }
2021
Jason parks5af0b912010-11-29 09:05:25 -06002022 @Override
Kenny Root735de3b2010-09-30 14:11:39 -07002023 public void handleExecute() throws IOException, RemoteException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002024 waitForReady();
2025 warnOnNotMounted();
2026
Kenny Root38cf8862010-09-26 14:18:51 -07002027 final ObbInfo obbInfo = getObbInfo();
2028
Kenny Roota02b8b02010-08-05 16:14:17 -07002029 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002030 Slog.w(TAG, "Denied attempt to mount OBB " + obbInfo.filename
2031 + " which is owned by " + obbInfo.packageName);
2032 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2033 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07002034 }
2035
Kenny Rootaf9d6672010-10-08 09:21:39 -07002036 final boolean isMounted;
2037 synchronized (mObbMounts) {
2038 isMounted = mObbPathToStateMap.containsKey(obbInfo.filename);
2039 }
2040 if (isMounted) {
2041 Slog.w(TAG, "Attempt to mount OBB which is already mounted: " + obbInfo.filename);
2042 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_ALREADY_MOUNTED);
2043 return;
2044 }
2045
2046 /*
2047 * The filename passed in might not be the canonical name, so just
2048 * set the filename to the canonicalized version.
2049 */
2050 mObbState.filename = obbInfo.filename;
2051
2052 final String hashedKey;
2053 if (mKey == null) {
2054 hashedKey = "none";
2055 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002056 try {
Kenny Root3b1abba2010-10-13 15:00:07 -07002057 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
2058
2059 KeySpec ks = new PBEKeySpec(mKey.toCharArray(), obbInfo.salt,
2060 PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE);
2061 SecretKey key = factory.generateSecret(ks);
2062 BigInteger bi = new BigInteger(key.getEncoded());
2063 hashedKey = bi.toString(16);
Kenny Rootaf9d6672010-10-08 09:21:39 -07002064 } catch (NoSuchAlgorithmException e) {
Kenny Root3b1abba2010-10-13 15:00:07 -07002065 Slog.e(TAG, "Could not load PBKDF2 algorithm", e);
2066 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
2067 return;
2068 } catch (InvalidKeySpecException e) {
2069 Slog.e(TAG, "Invalid key spec when loading PBKDF2 algorithm", e);
2070 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root38cf8862010-09-26 14:18:51 -07002071 return;
2072 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002073 }
Kenny Root38cf8862010-09-26 14:18:51 -07002074
Kenny Rootaf9d6672010-10-08 09:21:39 -07002075 int rc = StorageResultCode.OperationSucceeded;
2076 String cmd = String.format("obb mount %s %s %d", mObbState.filename, hashedKey,
2077 mObbState.callerUid);
2078 try {
2079 mConnector.doCommand(cmd);
2080 } catch (NativeDaemonConnectorException e) {
2081 int code = e.getCode();
2082 if (code != VoldResponseCode.OpFailedStorageBusy) {
2083 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002084 }
2085 }
2086
Kenny Rootaf9d6672010-10-08 09:21:39 -07002087 if (rc == StorageResultCode.OperationSucceeded) {
2088 if (DEBUG_OBB)
2089 Slog.d(TAG, "Successfully mounted OBB " + mObbState.filename);
2090
2091 synchronized (mObbMounts) {
2092 addObbStateLocked(mObbState);
2093 }
2094
2095 sendNewStatusOrIgnore(OnObbStateChangeListener.MOUNTED);
Kenny Root02c87302010-07-01 08:10:18 -07002096 } else {
Kenny Root05105f72010-09-22 17:29:43 -07002097 Slog.e(TAG, "Couldn't mount OBB file: " + rc);
Kenny Roota02b8b02010-08-05 16:14:17 -07002098
Kenny Rootaf9d6672010-10-08 09:21:39 -07002099 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT);
Kenny Root02c87302010-07-01 08:10:18 -07002100 }
2101 }
2102
Jason parks5af0b912010-11-29 09:05:25 -06002103 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002104 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002105 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root02c87302010-07-01 08:10:18 -07002106 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002107
2108 @Override
2109 public String toString() {
2110 StringBuilder sb = new StringBuilder();
2111 sb.append("MountObbAction{");
2112 sb.append("filename=");
2113 sb.append(mObbState.filename);
2114 sb.append(",callerUid=");
2115 sb.append(mObbState.callerUid);
2116 sb.append(",token=");
2117 sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002118 sb.append(",binder=");
2119 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002120 sb.append('}');
2121 return sb.toString();
2122 }
2123 }
2124
2125 class UnmountObbAction extends ObbAction {
2126 private boolean mForceUnmount;
2127
2128 UnmountObbAction(ObbState obbState, boolean force) {
2129 super(obbState);
2130 mForceUnmount = force;
2131 }
2132
Jason parks5af0b912010-11-29 09:05:25 -06002133 @Override
Kenny Root38cf8862010-09-26 14:18:51 -07002134 public void handleExecute() throws IOException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002135 waitForReady();
2136 warnOnNotMounted();
2137
Kenny Root38cf8862010-09-26 14:18:51 -07002138 final ObbInfo obbInfo = getObbInfo();
Kenny Roota02b8b02010-08-05 16:14:17 -07002139
Kenny Rootaf9d6672010-10-08 09:21:39 -07002140 final ObbState obbState;
Kenny Root38cf8862010-09-26 14:18:51 -07002141 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002142 obbState = mObbPathToStateMap.get(obbInfo.filename);
2143 }
Kenny Root38cf8862010-09-26 14:18:51 -07002144
Kenny Rootaf9d6672010-10-08 09:21:39 -07002145 if (obbState == null) {
2146 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_NOT_MOUNTED);
2147 return;
2148 }
2149
2150 if (obbState.callerUid != mObbState.callerUid) {
2151 Slog.w(TAG, "Permission denied attempting to unmount OBB " + obbInfo.filename
2152 + " (owned by " + obbInfo.packageName + ")");
2153 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2154 return;
2155 }
2156
2157 mObbState.filename = obbInfo.filename;
2158
2159 int rc = StorageResultCode.OperationSucceeded;
2160 String cmd = String.format("obb unmount %s%s", mObbState.filename,
2161 (mForceUnmount ? " force" : ""));
2162 try {
2163 mConnector.doCommand(cmd);
2164 } catch (NativeDaemonConnectorException e) {
2165 int code = e.getCode();
2166 if (code == VoldResponseCode.OpFailedStorageBusy) {
2167 rc = StorageResultCode.OperationFailedStorageBusy;
2168 } else if (code == VoldResponseCode.OpFailedStorageNotFound) {
2169 // If it's not mounted then we've already won.
2170 rc = StorageResultCode.OperationSucceeded;
2171 } else {
2172 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002173 }
2174 }
2175
Kenny Rootaf9d6672010-10-08 09:21:39 -07002176 if (rc == StorageResultCode.OperationSucceeded) {
2177 synchronized (mObbMounts) {
2178 removeObbStateLocked(obbState);
Kenny Root38cf8862010-09-26 14:18:51 -07002179 }
2180
Kenny Rootaf9d6672010-10-08 09:21:39 -07002181 sendNewStatusOrIgnore(OnObbStateChangeListener.UNMOUNTED);
Kenny Roota02b8b02010-08-05 16:14:17 -07002182 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002183 Slog.w(TAG, "Could not mount OBB: " + mObbState.filename);
2184 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT);
Kenny Roota02b8b02010-08-05 16:14:17 -07002185 }
2186 }
2187
Jason parks5af0b912010-11-29 09:05:25 -06002188 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002189 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002190 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Roota02b8b02010-08-05 16:14:17 -07002191 }
2192
2193 @Override
2194 public String toString() {
2195 StringBuilder sb = new StringBuilder();
2196 sb.append("UnmountObbAction{");
2197 sb.append("filename=");
2198 sb.append(mObbState.filename != null ? mObbState.filename : "null");
2199 sb.append(",force=");
2200 sb.append(mForceUnmount);
2201 sb.append(",callerUid=");
2202 sb.append(mObbState.callerUid);
2203 sb.append(",token=");
2204 sb.append(mObbState.token != null ? mObbState.token.toString() : "null");
Kenny Root735de3b2010-09-30 14:11:39 -07002205 sb.append(",binder=");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002206 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002207 sb.append('}');
2208 return sb.toString();
2209 }
Kenny Root02c87302010-07-01 08:10:18 -07002210 }
Kenny Root38cf8862010-09-26 14:18:51 -07002211
2212 @Override
2213 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2214 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
2215 pw.println("Permission Denial: can't dump ActivityManager from from pid="
2216 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
2217 + " without permission " + android.Manifest.permission.DUMP);
2218 return;
2219 }
2220
Kenny Root38cf8862010-09-26 14:18:51 -07002221 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002222 pw.println(" mObbMounts:");
Kenny Root38cf8862010-09-26 14:18:51 -07002223
Kenny Rootaf9d6672010-10-08 09:21:39 -07002224 final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet().iterator();
2225 while (binders.hasNext()) {
2226 Entry<IBinder, List<ObbState>> e = binders.next();
2227 pw.print(" Key="); pw.println(e.getKey().toString());
2228 final List<ObbState> obbStates = e.getValue();
Kenny Root38cf8862010-09-26 14:18:51 -07002229 for (final ObbState obbState : obbStates) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002230 pw.print(" "); pw.println(obbState.toString());
Kenny Root38cf8862010-09-26 14:18:51 -07002231 }
2232 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002233
2234 pw.println("");
2235 pw.println(" mObbPathToStateMap:");
2236 final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator();
2237 while (maps.hasNext()) {
2238 final Entry<String, ObbState> e = maps.next();
2239 pw.print(" "); pw.print(e.getKey());
2240 pw.print(" -> "); pw.println(e.getValue().toString());
2241 }
Kenny Root38cf8862010-09-26 14:18:51 -07002242 }
2243 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244}
2245