blob: 154712779b52797227d97858c8162327bd6e2083 [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;
Mike Lockwood8fa5f802011-03-24 08:12:30 -070068import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
Kenny Root3b1abba2010-10-13 15:00:07 -070070import javax.crypto.SecretKey;
71import javax.crypto.SecretKeyFactory;
72import javax.crypto.spec.PBEKeySpec;
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074/**
San Mehatb1043402010-02-05 08:26:50 -080075 * MountService implements back-end services for platform storage
76 * management.
77 * @hide - Applications should use android.os.storage.StorageManager
78 * to access the MountService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 */
Jason parks5af0b912010-11-29 09:05:25 -060080class MountService extends IMountService.Stub implements INativeDaemonConnectorCallbacks {
81
San Mehatb1043402010-02-05 08:26:50 -080082 private static final boolean LOCAL_LOGD = false;
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -080083 private static final boolean DEBUG_UNMOUNT = false;
84 private static final boolean DEBUG_EVENTS = false;
Kenny Rootb7db2722011-01-25 16:39:35 -080085 private static final boolean DEBUG_OBB = false;
Kenny Root02c87302010-07-01 08:10:18 -070086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 private static final String TAG = "MountService";
88
Kenny Root305bcbf2010-09-03 07:56:38 -070089 private static final String VOLD_TAG = "VoldConnector";
90
Kenny Rootcf0b38c2011-03-22 14:17:59 -070091 /** Maximum number of ASEC containers allowed to be mounted. */
92 private static final int MAX_CONTAINERS = 250;
93
San Mehat4270e1e2010-01-29 05:32:19 -080094 /*
95 * Internal vold volume state constants
96 */
San Mehat7fd0fee2009-12-17 07:12:23 -080097 class VolumeState {
98 public static final int Init = -1;
99 public static final int NoMedia = 0;
100 public static final int Idle = 1;
101 public static final int Pending = 2;
102 public static final int Checking = 3;
103 public static final int Mounted = 4;
104 public static final int Unmounting = 5;
105 public static final int Formatting = 6;
106 public static final int Shared = 7;
107 public static final int SharedMnt = 8;
108 }
109
San Mehat4270e1e2010-01-29 05:32:19 -0800110 /*
111 * Internal vold response code constants
112 */
San Mehat22dd86e2010-01-12 12:21:18 -0800113 class VoldResponseCode {
San Mehat4270e1e2010-01-29 05:32:19 -0800114 /*
115 * 100 series - Requestion action was initiated; expect another reply
116 * before proceeding with a new command.
117 */
San Mehat22dd86e2010-01-12 12:21:18 -0800118 public static final int VolumeListResult = 110;
119 public static final int AsecListResult = 111;
San Mehatc1b4ce92010-02-16 17:13:03 -0800120 public static final int StorageUsersListResult = 112;
San Mehat22dd86e2010-01-12 12:21:18 -0800121
San Mehat4270e1e2010-01-29 05:32:19 -0800122 /*
123 * 200 series - Requestion action has been successfully completed.
124 */
125 public static final int ShareStatusResult = 210;
San Mehat22dd86e2010-01-12 12:21:18 -0800126 public static final int AsecPathResult = 211;
San Mehat4270e1e2010-01-29 05:32:19 -0800127 public static final int ShareEnabledResult = 212;
San Mehat22dd86e2010-01-12 12:21:18 -0800128
San Mehat4270e1e2010-01-29 05:32:19 -0800129 /*
130 * 400 series - Command was accepted, but the requested action
131 * did not take place.
132 */
133 public static final int OpFailedNoMedia = 401;
134 public static final int OpFailedMediaBlank = 402;
135 public static final int OpFailedMediaCorrupt = 403;
136 public static final int OpFailedVolNotMounted = 404;
San Mehatd9709982010-02-18 11:43:03 -0800137 public static final int OpFailedStorageBusy = 405;
San Mehat2d66cef2010-03-23 11:12:52 -0700138 public static final int OpFailedStorageNotFound = 406;
San Mehat4270e1e2010-01-29 05:32:19 -0800139
140 /*
141 * 600 series - Unsolicited broadcasts.
142 */
San Mehat22dd86e2010-01-12 12:21:18 -0800143 public static final int VolumeStateChange = 605;
San Mehat22dd86e2010-01-12 12:21:18 -0800144 public static final int ShareAvailabilityChange = 620;
145 public static final int VolumeDiskInserted = 630;
146 public static final int VolumeDiskRemoved = 631;
147 public static final int VolumeBadRemoval = 632;
148 }
149
San Mehat4270e1e2010-01-29 05:32:19 -0800150 private Context mContext;
151 private NativeDaemonConnector mConnector;
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400152 private final HashMap<String, String> mVolumeStates = new HashMap<String, String>();
153 private String mExternalStoragePath;
San Mehat4270e1e2010-01-29 05:32:19 -0800154 private PackageManagerService mPms;
155 private boolean mUmsEnabling;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800156 // Used as a lock for methods that register/unregister listeners.
157 final private ArrayList<MountServiceBinderListener> mListeners =
158 new ArrayList<MountServiceBinderListener>();
San Mehat6a965af22010-02-24 17:47:30 -0800159 private boolean mBooted = false;
160 private boolean mReady = false;
161 private boolean mSendUmsConnectedOnBoot = false;
Mike Lockwood03559752010-07-19 18:25:03 -0400162 // true if we should fake MEDIA_MOUNTED state for external storage
163 private boolean mEmulateExternalStorage = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800164
San Mehat6cdd9c02010-02-09 14:45:20 -0800165 /**
166 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800167 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800168 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800169 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800170
Kenny Root02c87302010-07-01 08:10:18 -0700171 /**
Kenny Root3b1abba2010-10-13 15:00:07 -0700172 * The size of the crypto algorithm key in bits for OBB files. Currently
173 * Twofish is used which takes 128-bit keys.
174 */
175 private static final int CRYPTO_ALGORITHM_KEY_SIZE = 128;
176
177 /**
178 * The number of times to run SHA1 in the PBKDF2 function for OBB files.
179 * 1024 is reasonably secure and not too slow.
180 */
181 private static final int PBKDF2_HASH_ROUNDS = 1024;
182
183 /**
Kenny Roota02b8b02010-08-05 16:14:17 -0700184 * Mounted OBB tracking information. Used to track the current state of all
185 * OBBs.
Kenny Root02c87302010-07-01 08:10:18 -0700186 */
Kenny Root735de3b2010-09-30 14:11:39 -0700187 final private Map<IBinder, List<ObbState>> mObbMounts = new HashMap<IBinder, List<ObbState>>();
Kenny Roota02b8b02010-08-05 16:14:17 -0700188 final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>();
189
190 class ObbState implements IBinder.DeathRecipient {
Kenny Rootaf9d6672010-10-08 09:21:39 -0700191 public ObbState(String filename, int callerUid, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -0700192 throws RemoteException {
Kenny Roota02b8b02010-08-05 16:14:17 -0700193 this.filename = filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700194 this.callerUid = callerUid;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700195 this.token = token;
196 this.nonce = nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700197 }
198
199 // OBB source filename
Kenny Rootaf9d6672010-10-08 09:21:39 -0700200 String filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700201
202 // Binder.callingUid()
Kenny Root05105f72010-09-22 17:29:43 -0700203 final public int callerUid;
Kenny Roota02b8b02010-08-05 16:14:17 -0700204
Kenny Rootaf9d6672010-10-08 09:21:39 -0700205 // Token of remote Binder caller
206 final IObbActionListener token;
207
208 // Identifier to pass back to the token
209 final int nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700210
Kenny Root735de3b2010-09-30 14:11:39 -0700211 public IBinder getBinder() {
212 return token.asBinder();
213 }
214
Kenny Roota02b8b02010-08-05 16:14:17 -0700215 @Override
216 public void binderDied() {
217 ObbAction action = new UnmountObbAction(this, true);
218 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root735de3b2010-09-30 14:11:39 -0700219 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700220
Kenny Root5919ac62010-10-05 09:49:40 -0700221 public void link() throws RemoteException {
222 getBinder().linkToDeath(this, 0);
223 }
224
225 public void unlink() {
Kenny Root735de3b2010-09-30 14:11:39 -0700226 getBinder().unlinkToDeath(this, 0);
Kenny Roota02b8b02010-08-05 16:14:17 -0700227 }
Kenny Root38cf8862010-09-26 14:18:51 -0700228
229 @Override
230 public String toString() {
231 StringBuilder sb = new StringBuilder("ObbState{");
232 sb.append("filename=");
233 sb.append(filename);
234 sb.append(",token=");
235 sb.append(token.toString());
236 sb.append(",callerUid=");
237 sb.append(callerUid);
Kenny Root38cf8862010-09-26 14:18:51 -0700238 sb.append('}');
239 return sb.toString();
240 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700241 }
242
243 // OBB Action Handler
244 final private ObbActionHandler mObbActionHandler;
245
246 // OBB action handler messages
247 private static final int OBB_RUN_ACTION = 1;
248 private static final int OBB_MCS_BOUND = 2;
249 private static final int OBB_MCS_UNBIND = 3;
250 private static final int OBB_MCS_RECONNECT = 4;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700251 private static final int OBB_FLUSH_MOUNT_STATE = 5;
Kenny Roota02b8b02010-08-05 16:14:17 -0700252
253 /*
254 * Default Container Service information
255 */
256 static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
257 "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService");
258
259 final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection();
260
261 class DefaultContainerConnection implements ServiceConnection {
262 public void onServiceConnected(ComponentName name, IBinder service) {
263 if (DEBUG_OBB)
264 Slog.i(TAG, "onServiceConnected");
265 IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service);
266 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs));
267 }
268
269 public void onServiceDisconnected(ComponentName name) {
270 if (DEBUG_OBB)
271 Slog.i(TAG, "onServiceDisconnected");
272 }
273 };
274
275 // Used in the ObbActionHandler
276 private IMediaContainerService mContainerService = null;
Kenny Root02c87302010-07-01 08:10:18 -0700277
278 // Handler messages
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800279 private static final int H_UNMOUNT_PM_UPDATE = 1;
280 private static final int H_UNMOUNT_PM_DONE = 2;
281 private static final int H_UNMOUNT_MS = 3;
282 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
283 private static final int MAX_UNMOUNT_RETRIES = 4;
284
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800285 class UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700286 final String path;
287 final boolean force;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800288 int retries;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800289
290 UnmountCallBack(String path, boolean force) {
291 retries = 0;
292 this.path = path;
293 this.force = force;
294 }
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800295
296 void handleFinished() {
San Mehata5078592010-03-25 09:36:54 -0700297 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path);
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800298 doUnmountVolume(path, true);
299 }
300 }
301
302 class UmsEnableCallBack extends UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700303 final String method;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800304
305 UmsEnableCallBack(String path, String method, boolean force) {
306 super(path, force);
307 this.method = method;
308 }
309
310 @Override
311 void handleFinished() {
312 super.handleFinished();
313 doShareUnshareVolume(path, method, true);
314 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800315 }
316
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800317 class ShutdownCallBack extends UnmountCallBack {
318 IMountShutdownObserver observer;
319 ShutdownCallBack(String path, IMountShutdownObserver observer) {
320 super(path, true);
321 this.observer = observer;
322 }
323
324 @Override
325 void handleFinished() {
326 int ret = doUnmountVolume(path, true);
327 if (observer != null) {
328 try {
329 observer.onShutDownComplete(ret);
330 } catch (RemoteException e) {
San Mehata5078592010-03-25 09:36:54 -0700331 Slog.w(TAG, "RemoteException when shutting down");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800332 }
333 }
334 }
335 }
336
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400337 class MountServiceHandler extends Handler {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800338 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700339 boolean mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800340
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400341 MountServiceHandler(Looper l) {
342 super(l);
343 }
344
Jason parks5af0b912010-11-29 09:05:25 -0600345 @Override
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800346 public void handleMessage(Message msg) {
347 switch (msg.what) {
348 case H_UNMOUNT_PM_UPDATE: {
San Mehata5078592010-03-25 09:36:54 -0700349 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800350 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
351 mForceUnmounts.add(ucb);
San Mehata5078592010-03-25 09:36:54 -0700352 if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800353 // Register only if needed.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700354 if (!mUpdatingStatus) {
San Mehata5078592010-03-25 09:36:54 -0700355 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700356 mUpdatingStatus = true;
357 mPms.updateExternalMediaStatus(false, true);
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800358 }
359 break;
360 }
361 case H_UNMOUNT_PM_DONE: {
San Mehata5078592010-03-25 09:36:54 -0700362 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE");
San Mehata5078592010-03-25 09:36:54 -0700363 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700364 mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800365 int size = mForceUnmounts.size();
366 int sizeArr[] = new int[size];
367 int sizeArrN = 0;
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700368 // Kill processes holding references first
369 ActivityManagerService ams = (ActivityManagerService)
370 ServiceManager.getService("activity");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800371 for (int i = 0; i < size; i++) {
372 UnmountCallBack ucb = mForceUnmounts.get(i);
373 String path = ucb.path;
374 boolean done = false;
375 if (!ucb.force) {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800376 done = true;
377 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800378 int pids[] = getStorageUsers(path);
379 if (pids == null || pids.length == 0) {
380 done = true;
381 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800382 // Eliminate system process here?
Dianne Hackborn64825172011-03-02 21:32:58 -0800383 ams.killPids(pids, "unmount media", true);
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700384 // Confirm if file references have been freed.
385 pids = getStorageUsers(path);
386 if (pids == null || pids.length == 0) {
387 done = true;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800388 }
389 }
390 }
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700391 if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) {
392 // Retry again
393 Slog.i(TAG, "Retrying to kill storage users again");
394 mHandler.sendMessageDelayed(
395 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
396 ucb.retries++),
397 RETRY_UNMOUNT_DELAY);
398 } else {
399 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
400 Slog.i(TAG, "Failed to unmount media inspite of " +
401 MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now");
402 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800403 sizeArr[sizeArrN++] = i;
404 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
405 ucb));
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800406 }
407 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800408 // Remove already processed elements from list.
409 for (int i = (sizeArrN-1); i >= 0; i--) {
410 mForceUnmounts.remove(sizeArr[i]);
411 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800412 break;
413 }
414 case H_UNMOUNT_MS : {
San Mehata5078592010-03-25 09:36:54 -0700415 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800416 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800417 ucb.handleFinished();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800418 break;
419 }
420 }
421 }
422 };
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400423 final private HandlerThread mHandlerThread;
424 final private Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800425
San Mehat207e5382010-02-04 20:46:54 -0800426 private void waitForReady() {
427 while (mReady == false) {
428 for (int retries = 5; retries > 0; retries--) {
429 if (mReady) {
430 return;
431 }
432 SystemClock.sleep(1000);
433 }
San Mehata5078592010-03-25 09:36:54 -0700434 Slog.w(TAG, "Waiting too long for mReady!");
San Mehat207e5382010-02-04 20:46:54 -0800435 }
San Mehat1f6301e2010-01-07 22:40:27 -0800436 }
Kenny Root02c87302010-07-01 08:10:18 -0700437
San Mehat207e5382010-02-04 20:46:54 -0800438 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Jason parks5af0b912010-11-29 09:05:25 -0600439 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 public void onReceive(Context context, Intent intent) {
San Mehat91c77612010-01-07 10:39:41 -0800441 String action = intent.getAction();
442
443 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
San Mehat207e5382010-02-04 20:46:54 -0800444 mBooted = true;
San Mehat22dd86e2010-01-12 12:21:18 -0800445
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800446 /*
447 * In the simulator, we need to broadcast a volume mounted event
448 * to make the media scanner run.
449 */
450 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
451 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted);
452 return;
453 }
San Mehatfafb0412010-02-18 19:40:04 -0800454 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600455 @Override
San Mehatfafb0412010-02-18 19:40:04 -0800456 public void run() {
457 try {
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400458 synchronized (mVolumeStates) {
459 for (String path : mVolumeStates.keySet()) {
460 String state = mVolumeStates.get(path);
San Mehat6a254402010-03-22 10:21:00 -0700461
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400462 if (state.equals(Environment.MEDIA_UNMOUNTED)) {
463 int rc = doMountVolume(path);
464 if (rc != StorageResultCode.OperationSucceeded) {
465 Slog.e(TAG, String.format("Boot-time mount failed (%d)",
466 rc));
467 }
468 } else if (state.equals(Environment.MEDIA_SHARED)) {
469 /*
470 * Bootstrap UMS enabled state since vold indicates
471 * the volume is shared (runtime restart while ums enabled)
472 */
473 notifyVolumeStateChange(null, path, VolumeState.NoMedia,
474 VolumeState.Shared);
475 }
San Mehatfafb0412010-02-18 19:40:04 -0800476 }
477 }
San Mehat6a254402010-03-22 10:21:00 -0700478
San Mehat6a965af22010-02-24 17:47:30 -0800479 /*
San Mehat6a254402010-03-22 10:21:00 -0700480 * If UMS was connected on boot, send the connected event
San Mehat6a965af22010-02-24 17:47:30 -0800481 * now that we're up.
482 */
483 if (mSendUmsConnectedOnBoot) {
484 sendUmsIntent(true);
485 mSendUmsConnectedOnBoot = false;
486 }
San Mehatfafb0412010-02-18 19:40:04 -0800487 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700488 Slog.e(TAG, "Boot-time mount exception", ex);
San Mehatfafb0412010-02-18 19:40:04 -0800489 }
San Mehat207e5382010-02-04 20:46:54 -0800490 }
San Mehatfafb0412010-02-18 19:40:04 -0800491 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 }
493 }
494 };
San Mehat4270e1e2010-01-29 05:32:19 -0800495 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
496 final IMountServiceListener mListener;
497
498 MountServiceBinderListener(IMountServiceListener listener) {
499 mListener = listener;
Kenny Root02c87302010-07-01 08:10:18 -0700500
San Mehat91c77612010-01-07 10:39:41 -0800501 }
502
San Mehat4270e1e2010-01-29 05:32:19 -0800503 public void binderDied() {
San Mehata5078592010-03-25 09:36:54 -0700504 if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!");
Kenny Roota02b8b02010-08-05 16:14:17 -0700505 synchronized (mListeners) {
San Mehat4270e1e2010-01-29 05:32:19 -0800506 mListeners.remove(this);
507 mListener.asBinder().unlinkToDeath(this, 0);
508 }
509 }
510 }
511
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800512 private void doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800513 // TODO: Add support for multiple share methods
514 if (!method.equals("ums")) {
515 throw new IllegalArgumentException(String.format("Method %s not supported", method));
516 }
517
San Mehat4270e1e2010-01-29 05:32:19 -0800518 try {
519 mConnector.doCommand(String.format(
520 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
521 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -0700522 Slog.e(TAG, "Failed to share/unshare", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800523 }
San Mehat4270e1e2010-01-29 05:32:19 -0800524 }
525
San Mehat207e5382010-02-04 20:46:54 -0800526 private void updatePublicVolumeState(String path, String state) {
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400527 String oldState;
528 synchronized(mVolumeStates) {
529 oldState = mVolumeStates.put(path, state);
530 }
531 if (state.equals(oldState)) {
532 Slog.w(TAG, String.format("Duplicate state transition (%s -> %s) for %s",
533 state, state, path));
San Mehat4270e1e2010-01-29 05:32:19 -0800534 return;
535 }
San Mehatb1043402010-02-05 08:26:50 -0800536
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400537 Slog.d(TAG, "volume state changed for " + path + " (" + oldState + " -> " + state + ")");
Kenny Rootaf9d6672010-10-08 09:21:39 -0700538
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400539 if (path.equals(mExternalStoragePath)) {
540 // Update state on PackageManager, but only of real events
541 if (!mEmulateExternalStorage) {
542 if (Environment.MEDIA_UNMOUNTED.equals(state)) {
543 mPms.updateExternalMediaStatus(false, false);
544
545 /*
546 * Some OBBs might have been unmounted when this volume was
547 * unmounted, so send a message to the handler to let it know to
548 * remove those from the list of mounted OBBS.
549 */
550 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(
551 OBB_FLUSH_MOUNT_STATE, path));
552 } else if (Environment.MEDIA_MOUNTED.equals(state)) {
553 mPms.updateExternalMediaStatus(true, false);
554 }
Mike Lockwood03559752010-07-19 18:25:03 -0400555 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800556 }
San Mehat4270e1e2010-01-29 05:32:19 -0800557 synchronized (mListeners) {
558 for (int i = mListeners.size() -1; i >= 0; i--) {
559 MountServiceBinderListener bl = mListeners.get(i);
560 try {
San Mehatb1043402010-02-05 08:26:50 -0800561 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800562 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700563 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800564 mListeners.remove(i);
565 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700566 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800567 }
568 }
569 }
570 }
571
572 /**
573 *
574 * Callback from NativeDaemonConnector
575 */
576 public void onDaemonConnected() {
577 /*
578 * Since we'll be calling back into the NativeDaemonConnector,
579 * we need to do our work in a new thread.
580 */
581 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600582 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800583 public void run() {
584 /**
585 * Determine media state and UMS detection status
586 */
San Mehat4270e1e2010-01-29 05:32:19 -0800587 try {
588 String[] vols = mConnector.doListCommand(
589 "volume list", VoldResponseCode.VolumeListResult);
590 for (String volstr : vols) {
591 String[] tok = volstr.split(" ");
592 // FMT: <label> <mountpoint> <state>
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400593 String path = tok[1];
594 String state = Environment.MEDIA_REMOVED;
595
San Mehat4270e1e2010-01-29 05:32:19 -0800596 int st = Integer.parseInt(tok[2]);
597 if (st == VolumeState.NoMedia) {
598 state = Environment.MEDIA_REMOVED;
599 } else if (st == VolumeState.Idle) {
San Mehat207e5382010-02-04 20:46:54 -0800600 state = Environment.MEDIA_UNMOUNTED;
San Mehat4270e1e2010-01-29 05:32:19 -0800601 } else if (st == VolumeState.Mounted) {
602 state = Environment.MEDIA_MOUNTED;
San Mehata5078592010-03-25 09:36:54 -0700603 Slog.i(TAG, "Media already mounted on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800604 } else if (st == VolumeState.Shared) {
605 state = Environment.MEDIA_SHARED;
San Mehata5078592010-03-25 09:36:54 -0700606 Slog.i(TAG, "Media shared on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800607 } else {
608 throw new Exception(String.format("Unexpected state %d", st));
609 }
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400610
611 if (state != null) {
612 if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
613 updatePublicVolumeState(path, state);
614 }
San Mehat4270e1e2010-01-29 05:32:19 -0800615 }
616 } catch (Exception e) {
San Mehata5078592010-03-25 09:36:54 -0700617 Slog.e(TAG, "Error processing initial volume state", e);
Mike Lockwood7fa24aa2011-03-23 14:52:34 -0400618 updatePublicVolumeState(mExternalStoragePath, Environment.MEDIA_REMOVED);
San Mehat4270e1e2010-01-29 05:32:19 -0800619 }
620
621 try {
San Mehat207e5382010-02-04 20:46:54 -0800622 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800623 notifyShareAvailabilityChange("ums", avail);
624 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700625 Slog.w(TAG, "Failed to get share availability");
San Mehat4270e1e2010-01-29 05:32:19 -0800626 }
San Mehat207e5382010-02-04 20:46:54 -0800627 /*
Jason parks9ed98bc2011-01-17 09:58:35 -0600628 * Now that we've done our initialization, release
San Mehat207e5382010-02-04 20:46:54 -0800629 * the hounds!
630 */
631 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800632 }
633 }.start();
634 }
635
636 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800637 * Callback from NativeDaemonConnector
638 */
639 public boolean onEvent(int code, String raw, String[] cooked) {
640 Intent in = null;
641
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800642 if (DEBUG_EVENTS) {
643 StringBuilder builder = new StringBuilder();
644 builder.append("onEvent::");
645 builder.append(" raw= " + raw);
646 if (cooked != null) {
647 builder.append(" cooked = " );
648 for (String str : cooked) {
649 builder.append(" " + str);
650 }
651 }
San Mehata5078592010-03-25 09:36:54 -0700652 Slog.i(TAG, builder.toString());
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800653 }
San Mehat4270e1e2010-01-29 05:32:19 -0800654 if (code == VoldResponseCode.VolumeStateChange) {
655 /*
656 * One of the volumes we're managing has changed state.
657 * Format: "NNN Volume <label> <path> state changed
658 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
659 */
660 notifyVolumeStateChange(
661 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
662 Integer.parseInt(cooked[10]));
663 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
664 // FMT: NNN Share method <method> now <available|unavailable>
665 boolean avail = false;
666 if (cooked[5].equals("available")) {
667 avail = true;
668 }
669 notifyShareAvailabilityChange(cooked[3], avail);
670 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
671 (code == VoldResponseCode.VolumeDiskRemoved) ||
672 (code == VoldResponseCode.VolumeBadRemoval)) {
673 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
674 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
675 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
676 final String label = cooked[2];
677 final String path = cooked[3];
678 int major = -1;
679 int minor = -1;
680
681 try {
682 String devComp = cooked[6].substring(1, cooked[6].length() -1);
683 String[] devTok = devComp.split(":");
684 major = Integer.parseInt(devTok[0]);
685 minor = Integer.parseInt(devTok[1]);
686 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700687 Slog.e(TAG, "Failed to parse major/minor", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800688 }
689
San Mehat4270e1e2010-01-29 05:32:19 -0800690 if (code == VoldResponseCode.VolumeDiskInserted) {
691 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600692 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800693 public void run() {
694 try {
695 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800696 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700697 Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
San Mehat4270e1e2010-01-29 05:32:19 -0800698 }
699 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700700 Slog.w(TAG, "Failed to mount media on insertion", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800701 }
702 }
703 }.start();
704 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
705 /*
706 * This event gets trumped if we're already in BAD_REMOVAL state
707 */
708 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
709 return true;
710 }
711 /* Send the media unmounted event first */
San Mehata5078592010-03-25 09:36:54 -0700712 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800713 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
714 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
715 mContext.sendBroadcast(in);
716
San Mehata5078592010-03-25 09:36:54 -0700717 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
San Mehat4270e1e2010-01-29 05:32:19 -0800718 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
719 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
720 } else if (code == VoldResponseCode.VolumeBadRemoval) {
San Mehata5078592010-03-25 09:36:54 -0700721 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800722 /* Send the media unmounted event first */
723 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
724 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
725 mContext.sendBroadcast(in);
726
San Mehata5078592010-03-25 09:36:54 -0700727 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
San Mehat4270e1e2010-01-29 05:32:19 -0800728 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
729 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
730 } else {
San Mehata5078592010-03-25 09:36:54 -0700731 Slog.e(TAG, String.format("Unknown code {%d}", code));
San Mehat4270e1e2010-01-29 05:32:19 -0800732 }
733 } else {
734 return false;
735 }
736
737 if (in != null) {
738 mContext.sendBroadcast(in);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400739 }
740 return true;
San Mehat4270e1e2010-01-29 05:32:19 -0800741 }
742
San Mehat207e5382010-02-04 20:46:54 -0800743 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800744 String vs = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -0700745 if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
San Mehat4270e1e2010-01-29 05:32:19 -0800746
747 Intent in = null;
748
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500749 if (oldState == VolumeState.Shared && newState != oldState) {
San Mehata5078592010-03-25 09:36:54 -0700750 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500751 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
752 Uri.parse("file://" + path)));
753 }
754
San Mehat4270e1e2010-01-29 05:32:19 -0800755 if (newState == VolumeState.Init) {
756 } else if (newState == VolumeState.NoMedia) {
757 // NoMedia is handled via Disk Remove events
758 } else if (newState == VolumeState.Idle) {
759 /*
760 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
761 * if we're in the process of enabling UMS
762 */
763 if (!vs.equals(
764 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
765 Environment.MEDIA_NOFS) && !vs.equals(
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800766 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
San Mehata5078592010-03-25 09:36:54 -0700767 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
San Mehat4270e1e2010-01-29 05:32:19 -0800768 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
769 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
770 }
771 } else if (newState == VolumeState.Pending) {
772 } else if (newState == VolumeState.Checking) {
San Mehata5078592010-03-25 09:36:54 -0700773 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
San Mehat4270e1e2010-01-29 05:32:19 -0800774 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
775 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
776 } else if (newState == VolumeState.Mounted) {
San Mehata5078592010-03-25 09:36:54 -0700777 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800778 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
San Mehat4270e1e2010-01-29 05:32:19 -0800779 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
780 in.putExtra("read-only", false);
781 } else if (newState == VolumeState.Unmounting) {
San Mehat4270e1e2010-01-29 05:32:19 -0800782 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
783 } else if (newState == VolumeState.Formatting) {
784 } else if (newState == VolumeState.Shared) {
San Mehata5078592010-03-25 09:36:54 -0700785 if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800786 /* Send the media unmounted event first */
787 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
788 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
789 mContext.sendBroadcast(in);
790
San Mehata5078592010-03-25 09:36:54 -0700791 if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
San Mehat4270e1e2010-01-29 05:32:19 -0800792 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
793 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
San Mehata5078592010-03-25 09:36:54 -0700794 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
San Mehat4270e1e2010-01-29 05:32:19 -0800795 } else if (newState == VolumeState.SharedMnt) {
San Mehata5078592010-03-25 09:36:54 -0700796 Slog.e(TAG, "Live shared mounts not supported yet!");
San Mehat4270e1e2010-01-29 05:32:19 -0800797 return;
798 } else {
San Mehata5078592010-03-25 09:36:54 -0700799 Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800800 }
801
802 if (in != null) {
803 mContext.sendBroadcast(in);
804 }
805 }
806
San Mehat207e5382010-02-04 20:46:54 -0800807 private boolean doGetShareMethodAvailable(String method) {
Kenny Root85fb2062010-06-01 20:50:21 -0700808 ArrayList<String> rsp;
Kenny Roota80ce062010-06-01 13:23:53 -0700809 try {
Kenny Root85fb2062010-06-01 20:50:21 -0700810 rsp = mConnector.doCommand("share status " + method);
Kenny Roota80ce062010-06-01 13:23:53 -0700811 } catch (NativeDaemonConnectorException ex) {
812 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
813 return false;
814 }
San Mehat207e5382010-02-04 20:46:54 -0800815
816 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700817 String[] tok = line.split(" ");
818 if (tok.length < 3) {
819 Slog.e(TAG, "Malformed response to share status " + method);
820 return false;
821 }
822
San Mehat207e5382010-02-04 20:46:54 -0800823 int code;
824 try {
825 code = Integer.parseInt(tok[0]);
826 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700827 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehat207e5382010-02-04 20:46:54 -0800828 return false;
829 }
830 if (code == VoldResponseCode.ShareStatusResult) {
831 if (tok[2].equals("available"))
832 return true;
833 return false;
834 } else {
San Mehata5078592010-03-25 09:36:54 -0700835 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehat207e5382010-02-04 20:46:54 -0800836 return false;
837 }
838 }
San Mehata5078592010-03-25 09:36:54 -0700839 Slog.e(TAG, "Got an empty response");
San Mehat207e5382010-02-04 20:46:54 -0800840 return false;
841 }
842
843 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800844 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800845
San Mehata5078592010-03-25 09:36:54 -0700846 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
San Mehat207e5382010-02-04 20:46:54 -0800847 try {
848 mConnector.doCommand(String.format("volume mount %s", path));
849 } catch (NativeDaemonConnectorException e) {
850 /*
851 * Mount failed for some reason
852 */
853 Intent in = null;
854 int code = e.getCode();
855 if (code == VoldResponseCode.OpFailedNoMedia) {
856 /*
857 * Attempt to mount but no media inserted
858 */
San Mehatb1043402010-02-05 08:26:50 -0800859 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800860 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
San Mehata5078592010-03-25 09:36:54 -0700861 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
San Mehat207e5382010-02-04 20:46:54 -0800862 /*
863 * Media is blank or does not contain a supported filesystem
864 */
865 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
866 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800867 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800868 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehata5078592010-03-25 09:36:54 -0700869 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
San Mehat207e5382010-02-04 20:46:54 -0800870 /*
871 * Volume consistency check failed
872 */
873 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
874 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800875 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800876 } else {
San Mehatb1043402010-02-05 08:26:50 -0800877 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800878 }
879
880 /*
881 * Send broadcast intent (if required for the failure)
882 */
883 if (in != null) {
884 mContext.sendBroadcast(in);
885 }
886 }
887
888 return rc;
889 }
890
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800891 /*
892 * If force is not set, we do not unmount if there are
893 * processes holding references to the volume about to be unmounted.
894 * If force is set, all the processes holding references need to be
895 * killed via the ActivityManager before actually unmounting the volume.
896 * This might even take a while and might be retried after timed delays
897 * to make sure we dont end up in an instable state and kill some core
898 * processes.
899 */
San Mehatd9709982010-02-18 11:43:03 -0800900 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800901 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800902 return VoldResponseCode.OpFailedVolNotMounted;
903 }
Kenny Rootaa485402010-09-14 14:49:41 -0700904
905 /*
906 * Force a GC to make sure AssetManagers in other threads of the
907 * system_server are cleaned up. We have to do this since AssetManager
908 * instances are kept as a WeakReference and it's possible we have files
909 * open on the external storage.
910 */
911 Runtime.getRuntime().gc();
912
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800913 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700914 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800915 try {
San Mehatd9709982010-02-18 11:43:03 -0800916 mConnector.doCommand(String.format(
917 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700918 // We unmounted the volume. None of the asec containers are available now.
919 synchronized (mAsecMountSet) {
920 mAsecMountSet.clear();
921 }
San Mehatb1043402010-02-05 08:26:50 -0800922 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800923 } catch (NativeDaemonConnectorException e) {
924 // Don't worry about mismatch in PackageManager since the
925 // call back will handle the status changes any way.
926 int code = e.getCode();
927 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800928 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800929 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
930 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800931 } else {
San Mehatb1043402010-02-05 08:26:50 -0800932 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800933 }
934 }
935 }
936
937 private int doFormatVolume(String path) {
938 try {
939 String cmd = String.format("volume format %s", path);
940 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800941 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800942 } catch (NativeDaemonConnectorException e) {
943 int code = e.getCode();
944 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800945 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800946 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800947 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800948 } else {
San Mehatb1043402010-02-05 08:26:50 -0800949 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800950 }
951 }
952 }
953
San Mehatb1043402010-02-05 08:26:50 -0800954 private boolean doGetVolumeShared(String path, String method) {
955 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700956 ArrayList<String> rsp;
957
958 try {
959 rsp = mConnector.doCommand(cmd);
960 } catch (NativeDaemonConnectorException ex) {
961 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
962 return false;
963 }
San Mehatb1043402010-02-05 08:26:50 -0800964
965 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700966 String[] tok = line.split(" ");
967 if (tok.length < 3) {
968 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
969 return false;
970 }
971
San Mehatb1043402010-02-05 08:26:50 -0800972 int code;
973 try {
974 code = Integer.parseInt(tok[0]);
975 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700976 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800977 return false;
978 }
979 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700980 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800981 } else {
San Mehata5078592010-03-25 09:36:54 -0700982 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800983 return false;
984 }
985 }
San Mehata5078592010-03-25 09:36:54 -0700986 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800987 return false;
988 }
989
San Mehat207e5382010-02-04 20:46:54 -0800990 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800991 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -0700992 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800993 return;
994 }
995
996 synchronized (mListeners) {
997 for (int i = mListeners.size() -1; i >= 0; i--) {
998 MountServiceBinderListener bl = mListeners.get(i);
999 try {
San Mehatb1043402010-02-05 08:26:50 -08001000 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -08001001 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001002 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -08001003 mListeners.remove(i);
1004 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -07001005 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -08001006 }
1007 }
1008 }
1009
San Mehat207e5382010-02-04 20:46:54 -08001010 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -08001011 sendUmsIntent(avail);
1012 } else {
1013 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -08001014 }
San Mehat2fe718a2010-03-11 12:01:49 -08001015
1016 final String path = Environment.getExternalStorageDirectory().getPath();
1017 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
1018 /*
1019 * USB mass storage disconnected while enabled
1020 */
1021 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -06001022 @Override
San Mehat2fe718a2010-03-11 12:01:49 -08001023 public void run() {
1024 try {
1025 int rc;
San Mehata5078592010-03-25 09:36:54 -07001026 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -08001027 doShareUnshareVolume(path, "ums", false);
1028 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001029 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -08001030 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
1031 path, rc));
1032 }
1033 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -07001034 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -08001035 }
1036 }
1037 }.start();
1038 }
San Mehat4270e1e2010-01-29 05:32:19 -08001039 }
1040
San Mehat6a965af22010-02-24 17:47:30 -08001041 private void sendUmsIntent(boolean c) {
1042 mContext.sendBroadcast(
1043 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
1044 }
1045
San Mehat207e5382010-02-04 20:46:54 -08001046 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -08001047 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
1048 throw new SecurityException(String.format("Requires %s permission", perm));
1049 }
1050 }
1051
1052 /**
San Mehat207e5382010-02-04 20:46:54 -08001053 * Constructs a new MountService instance
1054 *
1055 * @param context Binder context for this service
1056 */
1057 public MountService(Context context) {
1058 mContext = context;
1059
Mike Lockwood7fa24aa2011-03-23 14:52:34 -04001060 mExternalStoragePath = Environment.getExternalStorageDirectory().getPath();
Mike Lockwood03559752010-07-19 18:25:03 -04001061 mEmulateExternalStorage = context.getResources().getBoolean(
1062 com.android.internal.R.bool.config_emulateExternalStorage);
1063 if (mEmulateExternalStorage) {
1064 Slog.d(TAG, "using emulated external storage");
Mike Lockwood7fa24aa2011-03-23 14:52:34 -04001065 mVolumeStates.put(mExternalStoragePath, Environment.MEDIA_MOUNTED);
Mike Lockwood03559752010-07-19 18:25:03 -04001066 }
1067
San Mehat207e5382010-02-04 20:46:54 -08001068 // XXX: This will go away soon in favor of IMountServiceObserver
1069 mPms = (PackageManagerService) ServiceManager.getService("package");
1070
1071 mContext.registerReceiver(mBroadcastReceiver,
1072 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
1073
Daniel Sandler5f27ef42010-03-16 15:42:02 -04001074 mHandlerThread = new HandlerThread("MountService");
1075 mHandlerThread.start();
1076 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
1077
Kenny Roota02b8b02010-08-05 16:14:17 -07001078 // Add OBB Action Handler to MountService thread.
1079 mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper());
1080
Marco Nelissenc34ebce2010-02-18 13:39:41 -08001081 /*
1082 * Vold does not run in the simulator, so pretend the connector thread
1083 * ran and did its thing.
1084 */
1085 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
1086 mReady = true;
1087 mUmsEnabling = true;
1088 return;
1089 }
1090
Kenny Root305bcbf2010-09-03 07:56:38 -07001091 /*
1092 * Create the connection to vold with a maximum queue of twice the
1093 * amount of containers we'd ever expect to have. This keeps an
1094 * "asec list" from blocking a thread repeatedly.
1095 */
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001096 mConnector = new NativeDaemonConnector(this, "vold", MAX_CONTAINERS * 2, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001097 mReady = false;
Kenny Root305bcbf2010-09-03 07:56:38 -07001098 Thread thread = new Thread(mConnector, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001099 thread.start();
1100 }
1101
1102 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001103 * Exposed API calls below here
1104 */
1105
1106 public void registerListener(IMountServiceListener listener) {
1107 synchronized (mListeners) {
1108 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
1109 try {
1110 listener.asBinder().linkToDeath(bl, 0);
1111 mListeners.add(bl);
1112 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001113 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -08001114 }
1115 }
1116 }
1117
1118 public void unregisterListener(IMountServiceListener listener) {
1119 synchronized (mListeners) {
1120 for(MountServiceBinderListener bl : mListeners) {
1121 if (bl.mListener == listener) {
1122 mListeners.remove(mListeners.indexOf(bl));
1123 return;
1124 }
1125 }
1126 }
1127 }
1128
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001129 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -08001130 validatePermission(android.Manifest.permission.SHUTDOWN);
1131
San Mehata5078592010-03-25 09:36:54 -07001132 Slog.i(TAG, "Shutting down");
Mike Lockwood7fa24aa2011-03-23 14:52:34 -04001133 synchronized (mVolumeStates) {
1134 for (String path : mVolumeStates.keySet()) {
1135 String state = mVolumeStates.get(path);
San Mehat4270e1e2010-01-29 05:32:19 -08001136
Mike Lockwood7fa24aa2011-03-23 14:52:34 -04001137 if (state.equals(Environment.MEDIA_SHARED)) {
1138 /*
1139 * If the media is currently shared, unshare it.
1140 * XXX: This is still dangerous!. We should not
1141 * be rebooting at *all* if UMS is enabled, since
1142 * the UMS host could have dirty FAT cache entries
1143 * yet to flush.
1144 */
1145 setUsbMassStorageEnabled(false);
1146 } else if (state.equals(Environment.MEDIA_CHECKING)) {
1147 /*
1148 * If the media is being checked, then we need to wait for
1149 * it to complete before being able to proceed.
1150 */
1151 // XXX: @hackbod - Should we disable the ANR timer here?
1152 int retries = 30;
1153 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
1154 try {
1155 Thread.sleep(1000);
1156 } catch (InterruptedException iex) {
1157 Slog.e(TAG, "Interrupted while waiting for media", iex);
1158 break;
1159 }
1160 state = Environment.getExternalStorageState();
1161 }
1162 if (retries == 0) {
1163 Slog.e(TAG, "Timed out waiting for media to check");
1164 }
San Mehat91c77612010-01-07 10:39:41 -08001165 }
San Mehat91c77612010-01-07 10:39:41 -08001166
Mike Lockwood7fa24aa2011-03-23 14:52:34 -04001167 if (state.equals(Environment.MEDIA_MOUNTED)) {
1168 // Post a unmount message.
1169 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
1170 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
1171 } else if (observer != null) {
1172 /*
1173 * Observer is waiting for onShutDownComplete when we are done.
1174 * Since nothing will be done send notification directly so shutdown
1175 * sequence can continue.
1176 */
1177 try {
1178 observer.onShutDownComplete(StorageResultCode.OperationSucceeded);
1179 } catch (RemoteException e) {
1180 Slog.w(TAG, "RemoteException when shutting down");
1181 }
1182 }
Johan Alfven5d0db4d2010-11-09 10:32:25 +01001183 }
San Mehat4270e1e2010-01-29 05:32:19 -08001184 }
1185 }
1186
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001187 private boolean getUmsEnabling() {
1188 synchronized (mListeners) {
1189 return mUmsEnabling;
1190 }
1191 }
1192
1193 private void setUmsEnabling(boolean enable) {
1194 synchronized (mListeners) {
Tony Wufc711252010-08-09 16:49:19 +08001195 mUmsEnabling = enable;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001196 }
1197 }
1198
San Mehatb1043402010-02-05 08:26:50 -08001199 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -08001200 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -08001201
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001202 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001203 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001204 }
San Mehatb1043402010-02-05 08:26:50 -08001205 return doGetShareMethodAvailable("ums");
1206 }
1207
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001208 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001209 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001210 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001211
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001212 // TODO: Add support for multiple share methods
1213
1214 /*
1215 * If the volume is mounted and we're enabling then unmount it
1216 */
1217 String path = Environment.getExternalStorageDirectory().getPath();
1218 String vs = getVolumeState(path);
1219 String method = "ums";
1220 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1221 // Override for isUsbMassStorageEnabled()
1222 setUmsEnabling(enable);
1223 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1224 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1225 // Clear override
1226 setUmsEnabling(false);
1227 }
1228 /*
1229 * If we disabled UMS then mount the volume
1230 */
1231 if (!enable) {
1232 doShareUnshareVolume(path, method, enable);
1233 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001234 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001235 " after disabling share method " + method);
1236 /*
1237 * Even though the mount failed, the unshare didn't so don't indicate an error.
1238 * The mountVolume() call will have set the storage state and sent the necessary
1239 * broadcasts.
1240 */
1241 }
1242 }
San Mehatb1043402010-02-05 08:26:50 -08001243 }
1244
1245 public boolean isUsbMassStorageEnabled() {
1246 waitForReady();
1247 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001249
San Mehat7fd0fee2009-12-17 07:12:23 -08001250 /**
1251 * @return state of the volume at the specified mount point
1252 */
San Mehat4270e1e2010-01-29 05:32:19 -08001253 public String getVolumeState(String mountPoint) {
Mike Lockwood7fa24aa2011-03-23 14:52:34 -04001254 synchronized (mVolumeStates) {
1255 String state = mVolumeStates.get(mountPoint);
1256 if (state == null) {
1257 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
1258 throw new IllegalArgumentException();
1259 }
San Mehat7fd0fee2009-12-17 07:12:23 -08001260
Mike Lockwood7fa24aa2011-03-23 14:52:34 -04001261 return state;
1262 }
San Mehat7fd0fee2009-12-17 07:12:23 -08001263 }
1264
Kenny Roote1ff2142010-10-12 11:20:01 -07001265 public boolean isExternalStorageEmulated() {
1266 return mEmulateExternalStorage;
1267 }
1268
San Mehat4270e1e2010-01-29 05:32:19 -08001269 public int mountVolume(String path) {
1270 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001271
San Mehat207e5382010-02-04 20:46:54 -08001272 waitForReady();
1273 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 }
1275
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001276 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001277 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001278 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001280 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001281 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001282 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1283 Environment.MEDIA_REMOVED.equals(volState) ||
1284 Environment.MEDIA_SHARED.equals(volState) ||
1285 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1286 // Media already unmounted or cannot be unmounted.
1287 // TODO return valid return code when adding observer call back.
1288 return;
1289 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001290 UnmountCallBack ucb = new UnmountCallBack(path, force);
1291 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 }
1293
San Mehat4270e1e2010-01-29 05:32:19 -08001294 public int formatVolume(String path) {
1295 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001296 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001297
San Mehat207e5382010-02-04 20:46:54 -08001298 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 }
1300
San Mehatc1b4ce92010-02-16 17:13:03 -08001301 public int []getStorageUsers(String path) {
1302 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1303 waitForReady();
1304 try {
1305 String[] r = mConnector.doListCommand(
1306 String.format("storage users %s", path),
1307 VoldResponseCode.StorageUsersListResult);
1308 // FMT: <pid> <process name>
1309 int[] data = new int[r.length];
1310 for (int i = 0; i < r.length; i++) {
1311 String []tok = r[i].split(" ");
1312 try {
1313 data[i] = Integer.parseInt(tok[0]);
1314 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001315 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001316 return new int[0];
1317 }
1318 }
1319 return data;
1320 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001321 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001322 return new int[0];
1323 }
1324 }
1325
San Mehatb1043402010-02-05 08:26:50 -08001326 private void warnOnNotMounted() {
1327 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001328 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001329 }
1330 }
1331
San Mehat4270e1e2010-01-29 05:32:19 -08001332 public String[] getSecureContainerList() {
1333 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001334 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001335 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001336
San Mehat4270e1e2010-01-29 05:32:19 -08001337 try {
1338 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1339 } catch (NativeDaemonConnectorException e) {
1340 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 }
1342 }
San Mehat36972292010-01-06 11:06:32 -08001343
San Mehat4270e1e2010-01-29 05:32:19 -08001344 public int createSecureContainer(String id, int sizeMb, String fstype,
1345 String key, int ownerUid) {
1346 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001347 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001348 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001349
San Mehatb1043402010-02-05 08:26:50 -08001350 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001351 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1352 try {
1353 mConnector.doCommand(cmd);
1354 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001355 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001356 }
San Mehata181b212010-02-11 06:50:20 -08001357
1358 if (rc == StorageResultCode.OperationSucceeded) {
1359 synchronized (mAsecMountSet) {
1360 mAsecMountSet.add(id);
1361 }
1362 }
San Mehat4270e1e2010-01-29 05:32:19 -08001363 return rc;
San Mehat36972292010-01-06 11:06:32 -08001364 }
1365
San Mehat4270e1e2010-01-29 05:32:19 -08001366 public int finalizeSecureContainer(String id) {
1367 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001368 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001369
San Mehatb1043402010-02-05 08:26:50 -08001370 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001371 try {
1372 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001373 /*
1374 * Finalization does a remount, so no need
1375 * to update mAsecMountSet
1376 */
San Mehat4270e1e2010-01-29 05:32:19 -08001377 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001378 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001379 }
San Mehat4270e1e2010-01-29 05:32:19 -08001380 return rc;
San Mehat36972292010-01-06 11:06:32 -08001381 }
1382
San Mehatd9709982010-02-18 11:43:03 -08001383 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001384 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001385 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001386 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001387
Kenny Rootaa485402010-09-14 14:49:41 -07001388 /*
1389 * Force a GC to make sure AssetManagers in other threads of the
1390 * system_server are cleaned up. We have to do this since AssetManager
1391 * instances are kept as a WeakReference and it's possible we have files
1392 * open on the external storage.
1393 */
1394 Runtime.getRuntime().gc();
1395
San Mehatb1043402010-02-05 08:26:50 -08001396 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001397 try {
San Mehatd9709982010-02-18 11:43:03 -08001398 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001399 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001400 int code = e.getCode();
1401 if (code == VoldResponseCode.OpFailedStorageBusy) {
1402 rc = StorageResultCode.OperationFailedStorageBusy;
1403 } else {
1404 rc = StorageResultCode.OperationFailedInternalError;
1405 }
San Mehat02735bc2010-01-26 15:18:08 -08001406 }
San Mehata181b212010-02-11 06:50:20 -08001407
1408 if (rc == StorageResultCode.OperationSucceeded) {
1409 synchronized (mAsecMountSet) {
1410 if (mAsecMountSet.contains(id)) {
1411 mAsecMountSet.remove(id);
1412 }
1413 }
1414 }
1415
San Mehat4270e1e2010-01-29 05:32:19 -08001416 return rc;
San Mehat36972292010-01-06 11:06:32 -08001417 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001418
San Mehat4270e1e2010-01-29 05:32:19 -08001419 public int mountSecureContainer(String id, String key, int ownerUid) {
1420 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001421 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001422 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001423
San Mehata181b212010-02-11 06:50:20 -08001424 synchronized (mAsecMountSet) {
1425 if (mAsecMountSet.contains(id)) {
1426 return StorageResultCode.OperationFailedStorageMounted;
1427 }
1428 }
1429
San Mehatb1043402010-02-05 08:26:50 -08001430 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001431 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1432 try {
1433 mConnector.doCommand(cmd);
1434 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001435 int code = e.getCode();
1436 if (code != VoldResponseCode.OpFailedStorageBusy) {
1437 rc = StorageResultCode.OperationFailedInternalError;
1438 }
San Mehat02735bc2010-01-26 15:18:08 -08001439 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001440
1441 if (rc == StorageResultCode.OperationSucceeded) {
1442 synchronized (mAsecMountSet) {
1443 mAsecMountSet.add(id);
1444 }
1445 }
San Mehat4270e1e2010-01-29 05:32:19 -08001446 return rc;
San Mehat36972292010-01-06 11:06:32 -08001447 }
1448
San Mehatd9709982010-02-18 11:43:03 -08001449 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001450 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001451 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001452 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001453
San Mehat6cdd9c02010-02-09 14:45:20 -08001454 synchronized (mAsecMountSet) {
1455 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001456 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001457 }
1458 }
1459
Kenny Rootaa485402010-09-14 14:49:41 -07001460 /*
1461 * Force a GC to make sure AssetManagers in other threads of the
1462 * system_server are cleaned up. We have to do this since AssetManager
1463 * instances are kept as a WeakReference and it's possible we have files
1464 * open on the external storage.
1465 */
1466 Runtime.getRuntime().gc();
1467
San Mehatb1043402010-02-05 08:26:50 -08001468 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001469 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001470 try {
1471 mConnector.doCommand(cmd);
1472 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001473 int code = e.getCode();
1474 if (code == VoldResponseCode.OpFailedStorageBusy) {
1475 rc = StorageResultCode.OperationFailedStorageBusy;
1476 } else {
1477 rc = StorageResultCode.OperationFailedInternalError;
1478 }
San Mehat02735bc2010-01-26 15:18:08 -08001479 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001480
1481 if (rc == StorageResultCode.OperationSucceeded) {
1482 synchronized (mAsecMountSet) {
1483 mAsecMountSet.remove(id);
1484 }
1485 }
San Mehat4270e1e2010-01-29 05:32:19 -08001486 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001487 }
1488
San Mehat6cdd9c02010-02-09 14:45:20 -08001489 public boolean isSecureContainerMounted(String id) {
1490 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1491 waitForReady();
1492 warnOnNotMounted();
1493
1494 synchronized (mAsecMountSet) {
1495 return mAsecMountSet.contains(id);
1496 }
1497 }
1498
San Mehat4270e1e2010-01-29 05:32:19 -08001499 public int renameSecureContainer(String oldId, String newId) {
1500 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001501 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001502 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001503
San Mehata181b212010-02-11 06:50:20 -08001504 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001505 /*
Jason parks9ed98bc2011-01-17 09:58:35 -06001506 * Because a mounted container has active internal state which cannot be
San Mehat85451ee2010-02-24 08:54:18 -08001507 * changed while active, we must ensure both ids are not currently mounted.
1508 */
1509 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001510 return StorageResultCode.OperationFailedStorageMounted;
1511 }
1512 }
1513
San Mehatb1043402010-02-05 08:26:50 -08001514 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001515 String cmd = String.format("asec rename %s %s", oldId, newId);
1516 try {
1517 mConnector.doCommand(cmd);
1518 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001519 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001520 }
San Mehata181b212010-02-11 06:50:20 -08001521
San Mehat4270e1e2010-01-29 05:32:19 -08001522 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001523 }
1524
San Mehat4270e1e2010-01-29 05:32:19 -08001525 public String getSecureContainerPath(String id) {
1526 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001527 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001528 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001529
San Mehat2d66cef2010-03-23 11:12:52 -07001530 try {
1531 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1532 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001533 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001534 if (code != VoldResponseCode.AsecPathResult) {
1535 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1536 }
1537 return tok[1];
1538 } catch (NativeDaemonConnectorException e) {
1539 int code = e.getCode();
1540 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1541 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001542 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001543 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001544 }
1545 }
San Mehat22dd86e2010-01-12 12:21:18 -08001546 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001547
1548 public void finishMediaUpdate() {
1549 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1550 }
Kenny Root02c87302010-07-01 08:10:18 -07001551
Kenny Roota02b8b02010-08-05 16:14:17 -07001552 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
1553 if (callerUid == android.os.Process.SYSTEM_UID) {
1554 return true;
1555 }
1556
Kenny Root02c87302010-07-01 08:10:18 -07001557 if (packageName == null) {
1558 return false;
1559 }
1560
1561 final int packageUid = mPms.getPackageUid(packageName);
1562
1563 if (DEBUG_OBB) {
1564 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
1565 packageUid + ", callerUid = " + callerUid);
1566 }
1567
1568 return callerUid == packageUid;
1569 }
1570
1571 public String getMountedObbPath(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001572 if (filename == null) {
1573 throw new IllegalArgumentException("filename cannot be null");
1574 }
1575
Kenny Root02c87302010-07-01 08:10:18 -07001576 waitForReady();
1577 warnOnNotMounted();
1578
Kenny Root02c87302010-07-01 08:10:18 -07001579 try {
1580 ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename));
1581 String []tok = rsp.get(0).split(" ");
1582 int code = Integer.parseInt(tok[0]);
1583 if (code != VoldResponseCode.AsecPathResult) {
1584 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1585 }
1586 return tok[1];
1587 } catch (NativeDaemonConnectorException e) {
1588 int code = e.getCode();
1589 if (code == VoldResponseCode.OpFailedStorageNotFound) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001590 return null;
Kenny Root02c87302010-07-01 08:10:18 -07001591 } else {
1592 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1593 }
1594 }
1595 }
1596
1597 public boolean isObbMounted(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001598 if (filename == null) {
1599 throw new IllegalArgumentException("filename cannot be null");
Kenny Root02c87302010-07-01 08:10:18 -07001600 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001601
1602 synchronized (mObbMounts) {
1603 return mObbPathToStateMap.containsKey(filename);
1604 }
Kenny Root02c87302010-07-01 08:10:18 -07001605 }
1606
Kenny Rootaf9d6672010-10-08 09:21:39 -07001607 public void mountObb(String filename, String key, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -07001608 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001609 if (filename == null) {
1610 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootaf9d6672010-10-08 09:21:39 -07001611 }
1612
1613 if (token == null) {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001614 throw new IllegalArgumentException("token cannot be null");
1615 }
1616
Kenny Rootaf9d6672010-10-08 09:21:39 -07001617 final int callerUid = Binder.getCallingUid();
1618 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1619 final ObbAction action = new MountObbAction(obbState, key);
Kenny Roota02b8b02010-08-05 16:14:17 -07001620 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
1621
1622 if (DEBUG_OBB)
1623 Slog.i(TAG, "Send to OBB handler: " + action.toString());
Kenny Root02c87302010-07-01 08:10:18 -07001624 }
1625
Kenny Rootaf9d6672010-10-08 09:21:39 -07001626 public void unmountObb(String filename, boolean force, IObbActionListener token, int nonce)
1627 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001628 if (filename == null) {
1629 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootf1121dc2010-09-29 07:30:53 -07001630 }
1631
Kenny Rootaf9d6672010-10-08 09:21:39 -07001632 final int callerUid = Binder.getCallingUid();
1633 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1634 final ObbAction action = new UnmountObbAction(obbState, force);
Kenny Roota02b8b02010-08-05 16:14:17 -07001635 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root02c87302010-07-01 08:10:18 -07001636
Kenny Roota02b8b02010-08-05 16:14:17 -07001637 if (DEBUG_OBB)
1638 Slog.i(TAG, "Send to OBB handler: " + action.toString());
1639 }
1640
Jason parks5af0b912010-11-29 09:05:25 -06001641 public int decryptStorage(String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06001642 if (TextUtils.isEmpty(password)) {
1643 throw new IllegalArgumentException("password cannot be empty");
Jason parks5af0b912010-11-29 09:05:25 -06001644 }
1645
Jason parks8888c592011-01-20 22:46:41 -06001646 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1647 "no permission to access the crypt keeper");
Jason parks5af0b912010-11-29 09:05:25 -06001648
1649 waitForReady();
1650
1651 if (DEBUG_EVENTS) {
1652 Slog.i(TAG, "decrypting storage...");
1653 }
1654
1655 try {
Jason parks9ed98bc2011-01-17 09:58:35 -06001656 ArrayList<String> rsp = mConnector.doCommand("cryptfs checkpw " + password);
Jason parksf7b3cd42011-01-27 09:28:25 -06001657 String[] tokens = rsp.get(0).split(" ");
Jason parks9ed98bc2011-01-17 09:58:35 -06001658
Jason parksf7b3cd42011-01-27 09:28:25 -06001659 if (tokens == null || tokens.length != 2) {
Jason parks9ed98bc2011-01-17 09:58:35 -06001660 return -1;
1661 }
1662
Jason parksf7b3cd42011-01-27 09:28:25 -06001663 int code = Integer.parseInt(tokens[1]);
Jason parks9ed98bc2011-01-17 09:58:35 -06001664
1665 if (code == 0) {
1666 // Decrypt was successful. Post a delayed message before restarting in order
1667 // to let the UI to clear itself
1668 mHandler.postDelayed(new Runnable() {
1669 public void run() {
1670 mConnector.doCommand(String.format("cryptfs restart"));
1671 }
Jason parksf7b3cd42011-01-27 09:28:25 -06001672 }, 1000); // 1 second
Jason parks9ed98bc2011-01-17 09:58:35 -06001673 }
1674
1675 return code;
Jason parks5af0b912010-11-29 09:05:25 -06001676 } catch (NativeDaemonConnectorException e) {
1677 // Decryption failed
1678 return e.getCode();
1679 }
Jason parks5af0b912010-11-29 09:05:25 -06001680 }
1681
Jason parks56aa5322011-01-07 09:01:15 -06001682 public int encryptStorage(String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06001683 if (TextUtils.isEmpty(password)) {
1684 throw new IllegalArgumentException("password cannot be empty");
Jason parks56aa5322011-01-07 09:01:15 -06001685 }
1686
Jason parks8888c592011-01-20 22:46:41 -06001687 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1688 "no permission to access the crypt keeper");
Jason parks56aa5322011-01-07 09:01:15 -06001689
1690 waitForReady();
1691
1692 if (DEBUG_EVENTS) {
Jason parks8888c592011-01-20 22:46:41 -06001693 Slog.i(TAG, "encrypting storage...");
Jason parks56aa5322011-01-07 09:01:15 -06001694 }
1695
1696 try {
Jason parks9ed98bc2011-01-17 09:58:35 -06001697 mConnector.doCommand(String.format("cryptfs enablecrypto inplace %s", password));
Jason parks56aa5322011-01-07 09:01:15 -06001698 } catch (NativeDaemonConnectorException e) {
1699 // Encryption failed
1700 return e.getCode();
1701 }
1702
1703 return 0;
1704 }
1705
Jason parksf7b3cd42011-01-27 09:28:25 -06001706 public int changeEncryptionPassword(String password) {
1707 if (TextUtils.isEmpty(password)) {
1708 throw new IllegalArgumentException("password cannot be empty");
1709 }
1710
1711 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1712 "no permission to access the crypt keeper");
1713
1714 waitForReady();
1715
1716 if (DEBUG_EVENTS) {
1717 Slog.i(TAG, "changing encryption password...");
1718 }
1719
1720 try {
1721 ArrayList<String> response = mConnector.doCommand("cryptfs changepw " + password);
1722
1723 String[] tokens = response.get(0).split(" ");
1724
1725 if (tokens == null || tokens.length != 2) {
1726 return -1;
1727 }
1728
1729 return Integer.parseInt(tokens[1]);
1730 } catch (NativeDaemonConnectorException e) {
1731 // Encryption failed
1732 return e.getCode();
1733 }
1734 }
1735
Mike Lockwood8fa5f802011-03-24 08:12:30 -07001736 public String[] getVolumeList() {
1737 synchronized(mVolumeStates) {
1738 Set<String> volumes = mVolumeStates.keySet();
1739 String[] result = new String[volumes.size()];
1740 int i = 0;
1741 for (String volume : volumes) {
1742 result[i++] = volume;
1743 }
1744 return result;
1745 }
1746 }
1747
Kenny Rootaf9d6672010-10-08 09:21:39 -07001748 private void addObbStateLocked(ObbState obbState) throws RemoteException {
1749 final IBinder binder = obbState.getBinder();
1750 List<ObbState> obbStates = mObbMounts.get(binder);
Kenny Root5919ac62010-10-05 09:49:40 -07001751
Kenny Rootaf9d6672010-10-08 09:21:39 -07001752 if (obbStates == null) {
1753 obbStates = new ArrayList<ObbState>();
1754 mObbMounts.put(binder, obbStates);
1755 } else {
1756 for (final ObbState o : obbStates) {
1757 if (o.filename.equals(obbState.filename)) {
1758 throw new IllegalStateException("Attempt to add ObbState twice. "
1759 + "This indicates an error in the MountService logic.");
Kenny Root5919ac62010-10-05 09:49:40 -07001760 }
1761 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001762 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001763
1764 obbStates.add(obbState);
1765 try {
1766 obbState.link();
1767 } catch (RemoteException e) {
1768 /*
1769 * The binder died before we could link it, so clean up our state
1770 * and return failure.
1771 */
1772 obbStates.remove(obbState);
1773 if (obbStates.isEmpty()) {
1774 mObbMounts.remove(binder);
1775 }
1776
1777 // Rethrow the error so mountObb can get it
1778 throw e;
1779 }
1780
1781 mObbPathToStateMap.put(obbState.filename, obbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07001782 }
1783
Kenny Rootaf9d6672010-10-08 09:21:39 -07001784 private void removeObbStateLocked(ObbState obbState) {
1785 final IBinder binder = obbState.getBinder();
1786 final List<ObbState> obbStates = mObbMounts.get(binder);
1787 if (obbStates != null) {
1788 if (obbStates.remove(obbState)) {
1789 obbState.unlink();
Kenny Root05105f72010-09-22 17:29:43 -07001790 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001791 if (obbStates.isEmpty()) {
1792 mObbMounts.remove(binder);
1793 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001794 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001795
Kenny Rootaf9d6672010-10-08 09:21:39 -07001796 mObbPathToStateMap.remove(obbState.filename);
Kenny Root38cf8862010-09-26 14:18:51 -07001797 }
1798
Kenny Roota02b8b02010-08-05 16:14:17 -07001799 private class ObbActionHandler extends Handler {
1800 private boolean mBound = false;
Kenny Root480afe72010-10-07 10:17:50 -07001801 private final List<ObbAction> mActions = new LinkedList<ObbAction>();
Kenny Roota02b8b02010-08-05 16:14:17 -07001802
1803 ObbActionHandler(Looper l) {
1804 super(l);
1805 }
1806
1807 @Override
1808 public void handleMessage(Message msg) {
1809 switch (msg.what) {
1810 case OBB_RUN_ACTION: {
Kenny Root480afe72010-10-07 10:17:50 -07001811 final ObbAction action = (ObbAction) msg.obj;
Kenny Roota02b8b02010-08-05 16:14:17 -07001812
1813 if (DEBUG_OBB)
1814 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
1815
1816 // If a bind was already initiated we don't really
1817 // need to do anything. The pending install
1818 // will be processed later on.
1819 if (!mBound) {
1820 // If this is the only one pending we might
1821 // have to bind to the service again.
1822 if (!connectToService()) {
1823 Slog.e(TAG, "Failed to bind to media container service");
1824 action.handleError();
1825 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07001826 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001827 }
Kenny Root735de3b2010-09-30 14:11:39 -07001828
Kenny Root735de3b2010-09-30 14:11:39 -07001829 mActions.add(action);
Kenny Roota02b8b02010-08-05 16:14:17 -07001830 break;
1831 }
1832 case OBB_MCS_BOUND: {
1833 if (DEBUG_OBB)
1834 Slog.i(TAG, "OBB_MCS_BOUND");
1835 if (msg.obj != null) {
1836 mContainerService = (IMediaContainerService) msg.obj;
1837 }
1838 if (mContainerService == null) {
1839 // Something seriously wrong. Bail out
1840 Slog.e(TAG, "Cannot bind to media container service");
1841 for (ObbAction action : mActions) {
1842 // Indicate service bind error
1843 action.handleError();
1844 }
1845 mActions.clear();
1846 } else if (mActions.size() > 0) {
Kenny Root480afe72010-10-07 10:17:50 -07001847 final ObbAction action = mActions.get(0);
Kenny Roota02b8b02010-08-05 16:14:17 -07001848 if (action != null) {
1849 action.execute(this);
1850 }
1851 } else {
1852 // Should never happen ideally.
1853 Slog.w(TAG, "Empty queue");
1854 }
1855 break;
1856 }
1857 case OBB_MCS_RECONNECT: {
1858 if (DEBUG_OBB)
1859 Slog.i(TAG, "OBB_MCS_RECONNECT");
1860 if (mActions.size() > 0) {
1861 if (mBound) {
1862 disconnectService();
1863 }
1864 if (!connectToService()) {
1865 Slog.e(TAG, "Failed to bind to media container service");
1866 for (ObbAction action : mActions) {
1867 // Indicate service bind error
1868 action.handleError();
1869 }
1870 mActions.clear();
1871 }
1872 }
1873 break;
1874 }
1875 case OBB_MCS_UNBIND: {
1876 if (DEBUG_OBB)
1877 Slog.i(TAG, "OBB_MCS_UNBIND");
1878
1879 // Delete pending install
1880 if (mActions.size() > 0) {
1881 mActions.remove(0);
1882 }
1883 if (mActions.size() == 0) {
1884 if (mBound) {
1885 disconnectService();
1886 }
1887 } else {
1888 // There are more pending requests in queue.
1889 // Just post MCS_BOUND message to trigger processing
1890 // of next pending install.
1891 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1892 }
1893 break;
1894 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001895 case OBB_FLUSH_MOUNT_STATE: {
1896 final String path = (String) msg.obj;
1897
1898 if (DEBUG_OBB)
1899 Slog.i(TAG, "Flushing all OBB state for path " + path);
1900
1901 synchronized (mObbMounts) {
1902 final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>();
1903
1904 final Iterator<Entry<String, ObbState>> i =
1905 mObbPathToStateMap.entrySet().iterator();
1906 while (i.hasNext()) {
1907 final Entry<String, ObbState> obbEntry = i.next();
1908
1909 /*
1910 * If this entry's source file is in the volume path
1911 * that got unmounted, remove it because it's no
1912 * longer valid.
1913 */
1914 if (obbEntry.getKey().startsWith(path)) {
1915 obbStatesToRemove.add(obbEntry.getValue());
1916 }
1917 }
1918
1919 for (final ObbState obbState : obbStatesToRemove) {
1920 if (DEBUG_OBB)
1921 Slog.i(TAG, "Removing state for " + obbState.filename);
1922
1923 removeObbStateLocked(obbState);
1924
1925 try {
1926 obbState.token.onObbResult(obbState.filename, obbState.nonce,
1927 OnObbStateChangeListener.UNMOUNTED);
1928 } catch (RemoteException e) {
1929 Slog.i(TAG, "Couldn't send unmount notification for OBB: "
1930 + obbState.filename);
1931 }
1932 }
1933 }
1934 break;
1935 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001936 }
1937 }
1938
1939 private boolean connectToService() {
1940 if (DEBUG_OBB)
1941 Slog.i(TAG, "Trying to bind to DefaultContainerService");
1942
1943 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
1944 if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) {
1945 mBound = true;
1946 return true;
1947 }
1948 return false;
1949 }
1950
1951 private void disconnectService() {
1952 mContainerService = null;
1953 mBound = false;
1954 mContext.unbindService(mDefContainerConn);
1955 }
1956 }
1957
1958 abstract class ObbAction {
1959 private static final int MAX_RETRIES = 3;
1960 private int mRetries;
1961
1962 ObbState mObbState;
1963
1964 ObbAction(ObbState obbState) {
1965 mObbState = obbState;
1966 }
1967
1968 public void execute(ObbActionHandler handler) {
1969 try {
1970 if (DEBUG_OBB)
1971 Slog.i(TAG, "Starting to execute action: " + this.toString());
1972 mRetries++;
1973 if (mRetries > MAX_RETRIES) {
1974 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
Kenny Root480afe72010-10-07 10:17:50 -07001975 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001976 handleError();
1977 return;
1978 } else {
1979 handleExecute();
1980 if (DEBUG_OBB)
1981 Slog.i(TAG, "Posting install MCS_UNBIND");
1982 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1983 }
1984 } catch (RemoteException e) {
1985 if (DEBUG_OBB)
1986 Slog.i(TAG, "Posting install MCS_RECONNECT");
1987 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
1988 } catch (Exception e) {
1989 if (DEBUG_OBB)
1990 Slog.d(TAG, "Error handling OBB action", e);
1991 handleError();
Kenny Root17eb6fb2010-10-06 15:02:52 -07001992 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001993 }
1994 }
1995
Kenny Root05105f72010-09-22 17:29:43 -07001996 abstract void handleExecute() throws RemoteException, IOException;
Kenny Roota02b8b02010-08-05 16:14:17 -07001997 abstract void handleError();
Kenny Root38cf8862010-09-26 14:18:51 -07001998
1999 protected ObbInfo getObbInfo() throws IOException {
2000 ObbInfo obbInfo;
2001 try {
2002 obbInfo = mContainerService.getObbInfo(mObbState.filename);
2003 } catch (RemoteException e) {
2004 Slog.d(TAG, "Couldn't call DefaultContainerService to fetch OBB info for "
2005 + mObbState.filename);
2006 obbInfo = null;
2007 }
2008 if (obbInfo == null) {
2009 throw new IOException("Couldn't read OBB file: " + mObbState.filename);
2010 }
2011 return obbInfo;
2012 }
2013
Kenny Rootaf9d6672010-10-08 09:21:39 -07002014 protected void sendNewStatusOrIgnore(int status) {
2015 if (mObbState == null || mObbState.token == null) {
2016 return;
2017 }
2018
Kenny Root38cf8862010-09-26 14:18:51 -07002019 try {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002020 mObbState.token.onObbResult(mObbState.filename, mObbState.nonce, status);
Kenny Root38cf8862010-09-26 14:18:51 -07002021 } catch (RemoteException e) {
2022 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
2023 }
2024 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002025 }
2026
2027 class MountObbAction extends ObbAction {
2028 private String mKey;
2029
2030 MountObbAction(ObbState obbState, String key) {
2031 super(obbState);
2032 mKey = key;
2033 }
2034
Jason parks5af0b912010-11-29 09:05:25 -06002035 @Override
Kenny Root735de3b2010-09-30 14:11:39 -07002036 public void handleExecute() throws IOException, RemoteException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002037 waitForReady();
2038 warnOnNotMounted();
2039
Kenny Root38cf8862010-09-26 14:18:51 -07002040 final ObbInfo obbInfo = getObbInfo();
2041
Kenny Roota02b8b02010-08-05 16:14:17 -07002042 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002043 Slog.w(TAG, "Denied attempt to mount OBB " + obbInfo.filename
2044 + " which is owned by " + obbInfo.packageName);
2045 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2046 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07002047 }
2048
Kenny Rootaf9d6672010-10-08 09:21:39 -07002049 final boolean isMounted;
2050 synchronized (mObbMounts) {
2051 isMounted = mObbPathToStateMap.containsKey(obbInfo.filename);
2052 }
2053 if (isMounted) {
2054 Slog.w(TAG, "Attempt to mount OBB which is already mounted: " + obbInfo.filename);
2055 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_ALREADY_MOUNTED);
2056 return;
2057 }
2058
2059 /*
2060 * The filename passed in might not be the canonical name, so just
2061 * set the filename to the canonicalized version.
2062 */
2063 mObbState.filename = obbInfo.filename;
2064
2065 final String hashedKey;
2066 if (mKey == null) {
2067 hashedKey = "none";
2068 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002069 try {
Kenny Root3b1abba2010-10-13 15:00:07 -07002070 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
2071
2072 KeySpec ks = new PBEKeySpec(mKey.toCharArray(), obbInfo.salt,
2073 PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE);
2074 SecretKey key = factory.generateSecret(ks);
2075 BigInteger bi = new BigInteger(key.getEncoded());
2076 hashedKey = bi.toString(16);
Kenny Rootaf9d6672010-10-08 09:21:39 -07002077 } catch (NoSuchAlgorithmException e) {
Kenny Root3b1abba2010-10-13 15:00:07 -07002078 Slog.e(TAG, "Could not load PBKDF2 algorithm", e);
2079 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
2080 return;
2081 } catch (InvalidKeySpecException e) {
2082 Slog.e(TAG, "Invalid key spec when loading PBKDF2 algorithm", e);
2083 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root38cf8862010-09-26 14:18:51 -07002084 return;
2085 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002086 }
Kenny Root38cf8862010-09-26 14:18:51 -07002087
Kenny Rootaf9d6672010-10-08 09:21:39 -07002088 int rc = StorageResultCode.OperationSucceeded;
2089 String cmd = String.format("obb mount %s %s %d", mObbState.filename, hashedKey,
2090 mObbState.callerUid);
2091 try {
2092 mConnector.doCommand(cmd);
2093 } catch (NativeDaemonConnectorException e) {
2094 int code = e.getCode();
2095 if (code != VoldResponseCode.OpFailedStorageBusy) {
2096 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002097 }
2098 }
2099
Kenny Rootaf9d6672010-10-08 09:21:39 -07002100 if (rc == StorageResultCode.OperationSucceeded) {
2101 if (DEBUG_OBB)
2102 Slog.d(TAG, "Successfully mounted OBB " + mObbState.filename);
2103
2104 synchronized (mObbMounts) {
2105 addObbStateLocked(mObbState);
2106 }
2107
2108 sendNewStatusOrIgnore(OnObbStateChangeListener.MOUNTED);
Kenny Root02c87302010-07-01 08:10:18 -07002109 } else {
Kenny Root05105f72010-09-22 17:29:43 -07002110 Slog.e(TAG, "Couldn't mount OBB file: " + rc);
Kenny Roota02b8b02010-08-05 16:14:17 -07002111
Kenny Rootaf9d6672010-10-08 09:21:39 -07002112 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT);
Kenny Root02c87302010-07-01 08:10:18 -07002113 }
2114 }
2115
Jason parks5af0b912010-11-29 09:05:25 -06002116 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002117 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002118 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root02c87302010-07-01 08:10:18 -07002119 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002120
2121 @Override
2122 public String toString() {
2123 StringBuilder sb = new StringBuilder();
2124 sb.append("MountObbAction{");
2125 sb.append("filename=");
2126 sb.append(mObbState.filename);
2127 sb.append(",callerUid=");
2128 sb.append(mObbState.callerUid);
2129 sb.append(",token=");
2130 sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002131 sb.append(",binder=");
2132 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002133 sb.append('}');
2134 return sb.toString();
2135 }
2136 }
2137
2138 class UnmountObbAction extends ObbAction {
2139 private boolean mForceUnmount;
2140
2141 UnmountObbAction(ObbState obbState, boolean force) {
2142 super(obbState);
2143 mForceUnmount = force;
2144 }
2145
Jason parks5af0b912010-11-29 09:05:25 -06002146 @Override
Kenny Root38cf8862010-09-26 14:18:51 -07002147 public void handleExecute() throws IOException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002148 waitForReady();
2149 warnOnNotMounted();
2150
Kenny Root38cf8862010-09-26 14:18:51 -07002151 final ObbInfo obbInfo = getObbInfo();
Kenny Roota02b8b02010-08-05 16:14:17 -07002152
Kenny Rootaf9d6672010-10-08 09:21:39 -07002153 final ObbState obbState;
Kenny Root38cf8862010-09-26 14:18:51 -07002154 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002155 obbState = mObbPathToStateMap.get(obbInfo.filename);
2156 }
Kenny Root38cf8862010-09-26 14:18:51 -07002157
Kenny Rootaf9d6672010-10-08 09:21:39 -07002158 if (obbState == null) {
2159 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_NOT_MOUNTED);
2160 return;
2161 }
2162
2163 if (obbState.callerUid != mObbState.callerUid) {
2164 Slog.w(TAG, "Permission denied attempting to unmount OBB " + obbInfo.filename
2165 + " (owned by " + obbInfo.packageName + ")");
2166 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2167 return;
2168 }
2169
2170 mObbState.filename = obbInfo.filename;
2171
2172 int rc = StorageResultCode.OperationSucceeded;
2173 String cmd = String.format("obb unmount %s%s", mObbState.filename,
2174 (mForceUnmount ? " force" : ""));
2175 try {
2176 mConnector.doCommand(cmd);
2177 } catch (NativeDaemonConnectorException e) {
2178 int code = e.getCode();
2179 if (code == VoldResponseCode.OpFailedStorageBusy) {
2180 rc = StorageResultCode.OperationFailedStorageBusy;
2181 } else if (code == VoldResponseCode.OpFailedStorageNotFound) {
2182 // If it's not mounted then we've already won.
2183 rc = StorageResultCode.OperationSucceeded;
2184 } else {
2185 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002186 }
2187 }
2188
Kenny Rootaf9d6672010-10-08 09:21:39 -07002189 if (rc == StorageResultCode.OperationSucceeded) {
2190 synchronized (mObbMounts) {
2191 removeObbStateLocked(obbState);
Kenny Root38cf8862010-09-26 14:18:51 -07002192 }
2193
Kenny Rootaf9d6672010-10-08 09:21:39 -07002194 sendNewStatusOrIgnore(OnObbStateChangeListener.UNMOUNTED);
Kenny Roota02b8b02010-08-05 16:14:17 -07002195 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002196 Slog.w(TAG, "Could not mount OBB: " + mObbState.filename);
2197 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT);
Kenny Roota02b8b02010-08-05 16:14:17 -07002198 }
2199 }
2200
Jason parks5af0b912010-11-29 09:05:25 -06002201 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002202 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002203 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Roota02b8b02010-08-05 16:14:17 -07002204 }
2205
2206 @Override
2207 public String toString() {
2208 StringBuilder sb = new StringBuilder();
2209 sb.append("UnmountObbAction{");
2210 sb.append("filename=");
2211 sb.append(mObbState.filename != null ? mObbState.filename : "null");
2212 sb.append(",force=");
2213 sb.append(mForceUnmount);
2214 sb.append(",callerUid=");
2215 sb.append(mObbState.callerUid);
2216 sb.append(",token=");
2217 sb.append(mObbState.token != null ? mObbState.token.toString() : "null");
Kenny Root735de3b2010-09-30 14:11:39 -07002218 sb.append(",binder=");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002219 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002220 sb.append('}');
2221 return sb.toString();
2222 }
Kenny Root02c87302010-07-01 08:10:18 -07002223 }
Kenny Root38cf8862010-09-26 14:18:51 -07002224
2225 @Override
2226 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2227 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
2228 pw.println("Permission Denial: can't dump ActivityManager from from pid="
2229 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
2230 + " without permission " + android.Manifest.permission.DUMP);
2231 return;
2232 }
2233
Kenny Root38cf8862010-09-26 14:18:51 -07002234 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002235 pw.println(" mObbMounts:");
Kenny Root38cf8862010-09-26 14:18:51 -07002236
Kenny Rootaf9d6672010-10-08 09:21:39 -07002237 final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet().iterator();
2238 while (binders.hasNext()) {
2239 Entry<IBinder, List<ObbState>> e = binders.next();
2240 pw.print(" Key="); pw.println(e.getKey().toString());
2241 final List<ObbState> obbStates = e.getValue();
Kenny Root38cf8862010-09-26 14:18:51 -07002242 for (final ObbState obbState : obbStates) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002243 pw.print(" "); pw.println(obbState.toString());
Kenny Root38cf8862010-09-26 14:18:51 -07002244 }
2245 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002246
2247 pw.println("");
2248 pw.println(" mObbPathToStateMap:");
2249 final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator();
2250 while (maps.hasNext()) {
2251 final Entry<String, ObbState> e = maps.next();
2252 pw.print(" "); pw.print(e.getKey());
2253 pw.print(" -> "); pw.println(e.getValue().toString());
2254 }
Kenny Root38cf8862010-09-26 14:18:51 -07002255 }
2256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257}
2258