blob: ca813fe96fe9f318aff77e4aa40624d7316ce1a5 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Kenny Roota02b8b02010-08-05 16:14:17 -070019import com.android.internal.app.IMediaContainerService;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080020import com.android.server.am.ActivityManagerService;
21
Jason parks8888c592011-01-20 22:46:41 -060022import android.Manifest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.BroadcastReceiver;
Kenny Roota02b8b02010-08-05 16:14:17 -070024import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
Kenny Roota02b8b02010-08-05 16:14:17 -070028import android.content.ServiceConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.pm.PackageManager;
Kenny Root02c87302010-07-01 08:10:18 -070030import android.content.res.ObbInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.net.Uri;
Kenny Root02c87302010-07-01 08:10:18 -070032import android.os.Binder;
Kenny Roota02b8b02010-08-05 16:14:17 -070033import android.os.Environment;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080034import android.os.Handler;
Daniel Sandler5f27ef42010-03-16 15:42:02 -040035import android.os.HandlerThread;
Kenny Roota02b8b02010-08-05 16:14:17 -070036import android.os.IBinder;
Daniel Sandler5f27ef42010-03-16 15:42:02 -040037import android.os.Looper;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080038import android.os.Message;
San Mehat4270e1e2010-01-29 05:32:19 -080039import android.os.RemoteException;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -080040import android.os.ServiceManager;
San Mehat207e5382010-02-04 20:46:54 -080041import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.SystemProperties;
Kenny Roota02b8b02010-08-05 16:14:17 -070043import android.os.storage.IMountService;
44import android.os.storage.IMountServiceListener;
45import android.os.storage.IMountShutdownObserver;
46import android.os.storage.IObbActionListener;
Kenny Rootaf9d6672010-10-08 09:21:39 -070047import android.os.storage.OnObbStateChangeListener;
Kenny Roota02b8b02010-08-05 16:14:17 -070048import android.os.storage.StorageResultCode;
Jason parksf7b3cd42011-01-27 09:28:25 -060049import android.text.TextUtils;
San Mehata5078592010-03-25 09:36:54 -070050import android.util.Slog;
Kenny Roota02b8b02010-08-05 16:14:17 -070051
Kenny Root38cf8862010-09-26 14:18:51 -070052import java.io.FileDescriptor;
Kenny Root05105f72010-09-22 17:29:43 -070053import java.io.IOException;
Kenny Root38cf8862010-09-26 14:18:51 -070054import java.io.PrintWriter;
Kenny Root3b1abba2010-10-13 15:00:07 -070055import java.math.BigInteger;
Kenny Root735de3b2010-09-30 14:11:39 -070056import java.security.NoSuchAlgorithmException;
Kenny Root3b1abba2010-10-13 15:00:07 -070057import java.security.spec.InvalidKeySpecException;
58import java.security.spec.KeySpec;
San Mehat22dd86e2010-01-12 12:21:18 -080059import java.util.ArrayList;
Kenny Roota02b8b02010-08-05 16:14:17 -070060import java.util.HashMap;
San Mehat6cdd9c02010-02-09 14:45:20 -080061import java.util.HashSet;
Kenny Root38cf8862010-09-26 14:18:51 -070062import java.util.Iterator;
Kenny Roota02b8b02010-08-05 16:14:17 -070063import java.util.LinkedList;
64import java.util.List;
65import java.util.Map;
Kenny Root38cf8862010-09-26 14:18:51 -070066import java.util.Map.Entry;
Mike Lockwoodd967f462011-03-24 08:12:30 -070067import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
Kenny Root3b1abba2010-10-13 15:00:07 -070069import javax.crypto.SecretKey;
70import javax.crypto.SecretKeyFactory;
71import javax.crypto.spec.PBEKeySpec;
72
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073/**
San Mehatb1043402010-02-05 08:26:50 -080074 * MountService implements back-end services for platform storage
75 * management.
76 * @hide - Applications should use android.os.storage.StorageManager
77 * to access the MountService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 */
Jason parks5af0b912010-11-29 09:05:25 -060079class MountService extends IMountService.Stub implements INativeDaemonConnectorCallbacks {
80
San Mehatb1043402010-02-05 08:26:50 -080081 private static final boolean LOCAL_LOGD = false;
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -080082 private static final boolean DEBUG_UNMOUNT = false;
83 private static final boolean DEBUG_EVENTS = false;
Kenny Rootb7db2722011-01-25 16:39:35 -080084 private static final boolean DEBUG_OBB = false;
Kenny Root02c87302010-07-01 08:10:18 -070085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 private static final String TAG = "MountService";
87
Kenny Root305bcbf2010-09-03 07:56:38 -070088 private static final String VOLD_TAG = "VoldConnector";
89
San Mehat4270e1e2010-01-29 05:32:19 -080090 /*
91 * Internal vold volume state constants
92 */
San Mehat7fd0fee2009-12-17 07:12:23 -080093 class VolumeState {
94 public static final int Init = -1;
95 public static final int NoMedia = 0;
96 public static final int Idle = 1;
97 public static final int Pending = 2;
98 public static final int Checking = 3;
99 public static final int Mounted = 4;
100 public static final int Unmounting = 5;
101 public static final int Formatting = 6;
102 public static final int Shared = 7;
103 public static final int SharedMnt = 8;
104 }
105
San Mehat4270e1e2010-01-29 05:32:19 -0800106 /*
107 * Internal vold response code constants
108 */
San Mehat22dd86e2010-01-12 12:21:18 -0800109 class VoldResponseCode {
San Mehat4270e1e2010-01-29 05:32:19 -0800110 /*
111 * 100 series - Requestion action was initiated; expect another reply
112 * before proceeding with a new command.
113 */
San Mehat22dd86e2010-01-12 12:21:18 -0800114 public static final int VolumeListResult = 110;
115 public static final int AsecListResult = 111;
San Mehatc1b4ce92010-02-16 17:13:03 -0800116 public static final int StorageUsersListResult = 112;
San Mehat22dd86e2010-01-12 12:21:18 -0800117
San Mehat4270e1e2010-01-29 05:32:19 -0800118 /*
119 * 200 series - Requestion action has been successfully completed.
120 */
121 public static final int ShareStatusResult = 210;
San Mehat22dd86e2010-01-12 12:21:18 -0800122 public static final int AsecPathResult = 211;
San Mehat4270e1e2010-01-29 05:32:19 -0800123 public static final int ShareEnabledResult = 212;
San Mehat22dd86e2010-01-12 12:21:18 -0800124
San Mehat4270e1e2010-01-29 05:32:19 -0800125 /*
126 * 400 series - Command was accepted, but the requested action
127 * did not take place.
128 */
129 public static final int OpFailedNoMedia = 401;
130 public static final int OpFailedMediaBlank = 402;
131 public static final int OpFailedMediaCorrupt = 403;
132 public static final int OpFailedVolNotMounted = 404;
San Mehatd9709982010-02-18 11:43:03 -0800133 public static final int OpFailedStorageBusy = 405;
San Mehat2d66cef2010-03-23 11:12:52 -0700134 public static final int OpFailedStorageNotFound = 406;
San Mehat4270e1e2010-01-29 05:32:19 -0800135
136 /*
137 * 600 series - Unsolicited broadcasts.
138 */
San Mehat22dd86e2010-01-12 12:21:18 -0800139 public static final int VolumeStateChange = 605;
San Mehat22dd86e2010-01-12 12:21:18 -0800140 public static final int ShareAvailabilityChange = 620;
141 public static final int VolumeDiskInserted = 630;
142 public static final int VolumeDiskRemoved = 631;
143 public static final int VolumeBadRemoval = 632;
144 }
145
San Mehat4270e1e2010-01-29 05:32:19 -0800146 private Context mContext;
147 private NativeDaemonConnector mConnector;
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400148 private final HashMap<String, String> mVolumeStates = new HashMap<String, String>();
149 private String mExternalStoragePath;
San Mehat4270e1e2010-01-29 05:32:19 -0800150 private PackageManagerService mPms;
151 private boolean mUmsEnabling;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800152 // Used as a lock for methods that register/unregister listeners.
153 final private ArrayList<MountServiceBinderListener> mListeners =
154 new ArrayList<MountServiceBinderListener>();
San Mehat6a965af22010-02-24 17:47:30 -0800155 private boolean mBooted = false;
156 private boolean mReady = false;
157 private boolean mSendUmsConnectedOnBoot = false;
Mike Lockwood03559752010-07-19 18:25:03 -0400158 // true if we should fake MEDIA_MOUNTED state for external storage
159 private boolean mEmulateExternalStorage = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800160
San Mehat6cdd9c02010-02-09 14:45:20 -0800161 /**
162 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800163 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800164 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800165 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800166
Kenny Root02c87302010-07-01 08:10:18 -0700167 /**
Kenny Root3b1abba2010-10-13 15:00:07 -0700168 * The size of the crypto algorithm key in bits for OBB files. Currently
169 * Twofish is used which takes 128-bit keys.
170 */
171 private static final int CRYPTO_ALGORITHM_KEY_SIZE = 128;
172
173 /**
174 * The number of times to run SHA1 in the PBKDF2 function for OBB files.
175 * 1024 is reasonably secure and not too slow.
176 */
177 private static final int PBKDF2_HASH_ROUNDS = 1024;
178
179 /**
Kenny Roota02b8b02010-08-05 16:14:17 -0700180 * Mounted OBB tracking information. Used to track the current state of all
181 * OBBs.
Kenny Root02c87302010-07-01 08:10:18 -0700182 */
Kenny Root735de3b2010-09-30 14:11:39 -0700183 final private Map<IBinder, List<ObbState>> mObbMounts = new HashMap<IBinder, List<ObbState>>();
Kenny Roota02b8b02010-08-05 16:14:17 -0700184 final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>();
185
186 class ObbState implements IBinder.DeathRecipient {
Kenny Rootaf9d6672010-10-08 09:21:39 -0700187 public ObbState(String filename, int callerUid, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -0700188 throws RemoteException {
Kenny Roota02b8b02010-08-05 16:14:17 -0700189 this.filename = filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700190 this.callerUid = callerUid;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700191 this.token = token;
192 this.nonce = nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700193 }
194
195 // OBB source filename
Kenny Rootaf9d6672010-10-08 09:21:39 -0700196 String filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700197
198 // Binder.callingUid()
Kenny Root05105f72010-09-22 17:29:43 -0700199 final public int callerUid;
Kenny Roota02b8b02010-08-05 16:14:17 -0700200
Kenny Rootaf9d6672010-10-08 09:21:39 -0700201 // Token of remote Binder caller
202 final IObbActionListener token;
203
204 // Identifier to pass back to the token
205 final int nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700206
Kenny Root735de3b2010-09-30 14:11:39 -0700207 public IBinder getBinder() {
208 return token.asBinder();
209 }
210
Kenny Roota02b8b02010-08-05 16:14:17 -0700211 @Override
212 public void binderDied() {
213 ObbAction action = new UnmountObbAction(this, true);
214 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root735de3b2010-09-30 14:11:39 -0700215 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700216
Kenny Root5919ac62010-10-05 09:49:40 -0700217 public void link() throws RemoteException {
218 getBinder().linkToDeath(this, 0);
219 }
220
221 public void unlink() {
Kenny Root735de3b2010-09-30 14:11:39 -0700222 getBinder().unlinkToDeath(this, 0);
Kenny Roota02b8b02010-08-05 16:14:17 -0700223 }
Kenny Root38cf8862010-09-26 14:18:51 -0700224
225 @Override
226 public String toString() {
227 StringBuilder sb = new StringBuilder("ObbState{");
228 sb.append("filename=");
229 sb.append(filename);
230 sb.append(",token=");
231 sb.append(token.toString());
232 sb.append(",callerUid=");
233 sb.append(callerUid);
Kenny Root38cf8862010-09-26 14:18:51 -0700234 sb.append('}');
235 return sb.toString();
236 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700237 }
238
239 // OBB Action Handler
240 final private ObbActionHandler mObbActionHandler;
241
242 // OBB action handler messages
243 private static final int OBB_RUN_ACTION = 1;
244 private static final int OBB_MCS_BOUND = 2;
245 private static final int OBB_MCS_UNBIND = 3;
246 private static final int OBB_MCS_RECONNECT = 4;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700247 private static final int OBB_FLUSH_MOUNT_STATE = 5;
Kenny Roota02b8b02010-08-05 16:14:17 -0700248
249 /*
250 * Default Container Service information
251 */
252 static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
253 "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService");
254
255 final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection();
256
257 class DefaultContainerConnection implements ServiceConnection {
258 public void onServiceConnected(ComponentName name, IBinder service) {
259 if (DEBUG_OBB)
260 Slog.i(TAG, "onServiceConnected");
261 IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service);
262 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs));
263 }
264
265 public void onServiceDisconnected(ComponentName name) {
266 if (DEBUG_OBB)
267 Slog.i(TAG, "onServiceDisconnected");
268 }
269 };
270
271 // Used in the ObbActionHandler
272 private IMediaContainerService mContainerService = null;
Kenny Root02c87302010-07-01 08:10:18 -0700273
274 // Handler messages
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800275 private static final int H_UNMOUNT_PM_UPDATE = 1;
276 private static final int H_UNMOUNT_PM_DONE = 2;
277 private static final int H_UNMOUNT_MS = 3;
278 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
279 private static final int MAX_UNMOUNT_RETRIES = 4;
280
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800281 class UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700282 final String path;
283 final boolean force;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800284 int retries;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800285
286 UnmountCallBack(String path, boolean force) {
287 retries = 0;
288 this.path = path;
289 this.force = force;
290 }
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800291
292 void handleFinished() {
San Mehata5078592010-03-25 09:36:54 -0700293 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path);
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800294 doUnmountVolume(path, true);
295 }
296 }
297
298 class UmsEnableCallBack extends UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700299 final String method;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800300
301 UmsEnableCallBack(String path, String method, boolean force) {
302 super(path, force);
303 this.method = method;
304 }
305
306 @Override
307 void handleFinished() {
308 super.handleFinished();
309 doShareUnshareVolume(path, method, true);
310 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800311 }
312
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800313 class ShutdownCallBack extends UnmountCallBack {
314 IMountShutdownObserver observer;
315 ShutdownCallBack(String path, IMountShutdownObserver observer) {
316 super(path, true);
317 this.observer = observer;
318 }
319
320 @Override
321 void handleFinished() {
322 int ret = doUnmountVolume(path, true);
323 if (observer != null) {
324 try {
325 observer.onShutDownComplete(ret);
326 } catch (RemoteException e) {
San Mehata5078592010-03-25 09:36:54 -0700327 Slog.w(TAG, "RemoteException when shutting down");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800328 }
329 }
330 }
331 }
332
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400333 class MountServiceHandler extends Handler {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800334 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700335 boolean mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800336
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400337 MountServiceHandler(Looper l) {
338 super(l);
339 }
340
Jason parks5af0b912010-11-29 09:05:25 -0600341 @Override
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800342 public void handleMessage(Message msg) {
343 switch (msg.what) {
344 case H_UNMOUNT_PM_UPDATE: {
San Mehata5078592010-03-25 09:36:54 -0700345 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800346 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
347 mForceUnmounts.add(ucb);
San Mehata5078592010-03-25 09:36:54 -0700348 if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800349 // Register only if needed.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700350 if (!mUpdatingStatus) {
San Mehata5078592010-03-25 09:36:54 -0700351 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700352 mUpdatingStatus = true;
353 mPms.updateExternalMediaStatus(false, true);
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800354 }
355 break;
356 }
357 case H_UNMOUNT_PM_DONE: {
San Mehata5078592010-03-25 09:36:54 -0700358 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE");
San Mehata5078592010-03-25 09:36:54 -0700359 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700360 mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800361 int size = mForceUnmounts.size();
362 int sizeArr[] = new int[size];
363 int sizeArrN = 0;
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700364 // Kill processes holding references first
365 ActivityManagerService ams = (ActivityManagerService)
366 ServiceManager.getService("activity");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800367 for (int i = 0; i < size; i++) {
368 UnmountCallBack ucb = mForceUnmounts.get(i);
369 String path = ucb.path;
370 boolean done = false;
371 if (!ucb.force) {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800372 done = true;
373 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800374 int pids[] = getStorageUsers(path);
375 if (pids == null || pids.length == 0) {
376 done = true;
377 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800378 // Eliminate system process here?
Dianne Hackborn64825172011-03-02 21:32:58 -0800379 ams.killPids(pids, "unmount media", true);
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700380 // Confirm if file references have been freed.
381 pids = getStorageUsers(path);
382 if (pids == null || pids.length == 0) {
383 done = true;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800384 }
385 }
386 }
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700387 if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) {
388 // Retry again
389 Slog.i(TAG, "Retrying to kill storage users again");
390 mHandler.sendMessageDelayed(
391 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
392 ucb.retries++),
393 RETRY_UNMOUNT_DELAY);
394 } else {
395 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
396 Slog.i(TAG, "Failed to unmount media inspite of " +
397 MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now");
398 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800399 sizeArr[sizeArrN++] = i;
400 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
401 ucb));
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800402 }
403 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800404 // Remove already processed elements from list.
405 for (int i = (sizeArrN-1); i >= 0; i--) {
406 mForceUnmounts.remove(sizeArr[i]);
407 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800408 break;
409 }
410 case H_UNMOUNT_MS : {
San Mehata5078592010-03-25 09:36:54 -0700411 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800412 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800413 ucb.handleFinished();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800414 break;
415 }
416 }
417 }
418 };
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400419 final private HandlerThread mHandlerThread;
420 final private Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800421
San Mehat207e5382010-02-04 20:46:54 -0800422 private void waitForReady() {
423 while (mReady == false) {
424 for (int retries = 5; retries > 0; retries--) {
425 if (mReady) {
426 return;
427 }
428 SystemClock.sleep(1000);
429 }
San Mehata5078592010-03-25 09:36:54 -0700430 Slog.w(TAG, "Waiting too long for mReady!");
San Mehat207e5382010-02-04 20:46:54 -0800431 }
San Mehat1f6301e2010-01-07 22:40:27 -0800432 }
Kenny Root02c87302010-07-01 08:10:18 -0700433
San Mehat207e5382010-02-04 20:46:54 -0800434 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Jason parks5af0b912010-11-29 09:05:25 -0600435 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 public void onReceive(Context context, Intent intent) {
San Mehat91c77612010-01-07 10:39:41 -0800437 String action = intent.getAction();
438
439 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
San Mehat207e5382010-02-04 20:46:54 -0800440 mBooted = true;
San Mehat22dd86e2010-01-12 12:21:18 -0800441
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800442 /*
443 * In the simulator, we need to broadcast a volume mounted event
444 * to make the media scanner run.
445 */
446 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
Mike Lockwoodb9135632011-04-05 10:05:47 -0400447 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia,
448 VolumeState.Mounted);
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800449 return;
450 }
San Mehatfafb0412010-02-18 19:40:04 -0800451 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600452 @Override
San Mehatfafb0412010-02-18 19:40:04 -0800453 public void run() {
454 try {
Mike Lockwoodb9135632011-04-05 10:05:47 -0400455 // it is not safe to call vold with mVolumeStates locked
456 // so we make a copy of the paths and states and process them
457 // outside the lock
458 String[] paths, states;
459 int count;
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400460 synchronized (mVolumeStates) {
Mike Lockwoodb9135632011-04-05 10:05:47 -0400461 Set<String> keys = mVolumeStates.keySet();
462 count = keys.size();
463 paths = (String[])keys.toArray(new String[count]);
464 states = new String[count];
465 for (int i = 0; i < count; i++) {
466 states[i] = mVolumeStates.get(paths[i]);
467 }
468 }
San Mehat6a254402010-03-22 10:21:00 -0700469
Mike Lockwoodb9135632011-04-05 10:05:47 -0400470 for (int i = 0; i < count; i++) {
471 String path = paths[i];
472 String state = states[i];
473
474 if (state.equals(Environment.MEDIA_UNMOUNTED)) {
475 int rc = doMountVolume(path);
476 if (rc != StorageResultCode.OperationSucceeded) {
477 Slog.e(TAG, String.format("Boot-time mount failed (%d)",
478 rc));
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400479 }
Mike Lockwoodb9135632011-04-05 10:05:47 -0400480 } else if (state.equals(Environment.MEDIA_SHARED)) {
481 /*
482 * Bootstrap UMS enabled state since vold indicates
483 * the volume is shared (runtime restart while ums enabled)
484 */
485 notifyVolumeStateChange(null, path, VolumeState.NoMedia,
486 VolumeState.Shared);
San Mehatfafb0412010-02-18 19:40:04 -0800487 }
488 }
San Mehat6a254402010-03-22 10:21:00 -0700489
San Mehat6a965af22010-02-24 17:47:30 -0800490 /*
San Mehat6a254402010-03-22 10:21:00 -0700491 * If UMS was connected on boot, send the connected event
San Mehat6a965af22010-02-24 17:47:30 -0800492 * now that we're up.
493 */
494 if (mSendUmsConnectedOnBoot) {
495 sendUmsIntent(true);
496 mSendUmsConnectedOnBoot = false;
497 }
San Mehatfafb0412010-02-18 19:40:04 -0800498 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700499 Slog.e(TAG, "Boot-time mount exception", ex);
San Mehatfafb0412010-02-18 19:40:04 -0800500 }
San Mehat207e5382010-02-04 20:46:54 -0800501 }
San Mehatfafb0412010-02-18 19:40:04 -0800502 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 }
504 }
505 };
506
San Mehat4270e1e2010-01-29 05:32:19 -0800507 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
508 final IMountServiceListener mListener;
509
510 MountServiceBinderListener(IMountServiceListener listener) {
511 mListener = listener;
Kenny Root02c87302010-07-01 08:10:18 -0700512
San Mehat91c77612010-01-07 10:39:41 -0800513 }
514
San Mehat4270e1e2010-01-29 05:32:19 -0800515 public void binderDied() {
San Mehata5078592010-03-25 09:36:54 -0700516 if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!");
Kenny Roota02b8b02010-08-05 16:14:17 -0700517 synchronized (mListeners) {
San Mehat4270e1e2010-01-29 05:32:19 -0800518 mListeners.remove(this);
519 mListener.asBinder().unlinkToDeath(this, 0);
520 }
521 }
522 }
523
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800524 private void doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800525 // TODO: Add support for multiple share methods
526 if (!method.equals("ums")) {
527 throw new IllegalArgumentException(String.format("Method %s not supported", method));
528 }
529
San Mehat4270e1e2010-01-29 05:32:19 -0800530 try {
531 mConnector.doCommand(String.format(
532 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
533 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -0700534 Slog.e(TAG, "Failed to share/unshare", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800535 }
San Mehat4270e1e2010-01-29 05:32:19 -0800536 }
537
San Mehat207e5382010-02-04 20:46:54 -0800538 private void updatePublicVolumeState(String path, String state) {
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400539 String oldState;
540 synchronized(mVolumeStates) {
541 oldState = mVolumeStates.put(path, state);
542 }
543 if (state.equals(oldState)) {
544 Slog.w(TAG, String.format("Duplicate state transition (%s -> %s) for %s",
545 state, state, path));
San Mehat4270e1e2010-01-29 05:32:19 -0800546 return;
547 }
San Mehatb1043402010-02-05 08:26:50 -0800548
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400549 Slog.d(TAG, "volume state changed for " + path + " (" + oldState + " -> " + state + ")");
Kenny Rootaf9d6672010-10-08 09:21:39 -0700550
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400551 if (path.equals(mExternalStoragePath)) {
552 // Update state on PackageManager, but only of real events
553 if (!mEmulateExternalStorage) {
554 if (Environment.MEDIA_UNMOUNTED.equals(state)) {
555 mPms.updateExternalMediaStatus(false, false);
556
557 /*
558 * Some OBBs might have been unmounted when this volume was
559 * unmounted, so send a message to the handler to let it know to
560 * remove those from the list of mounted OBBS.
561 */
562 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(
563 OBB_FLUSH_MOUNT_STATE, path));
564 } else if (Environment.MEDIA_MOUNTED.equals(state)) {
565 mPms.updateExternalMediaStatus(true, false);
566 }
Mike Lockwood03559752010-07-19 18:25:03 -0400567 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800568 }
San Mehat4270e1e2010-01-29 05:32:19 -0800569 synchronized (mListeners) {
570 for (int i = mListeners.size() -1; i >= 0; i--) {
571 MountServiceBinderListener bl = mListeners.get(i);
572 try {
San Mehatb1043402010-02-05 08:26:50 -0800573 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800574 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700575 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800576 mListeners.remove(i);
577 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700578 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800579 }
580 }
581 }
582 }
583
584 /**
585 *
586 * Callback from NativeDaemonConnector
587 */
588 public void onDaemonConnected() {
589 /*
590 * Since we'll be calling back into the NativeDaemonConnector,
591 * we need to do our work in a new thread.
592 */
593 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600594 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800595 public void run() {
596 /**
597 * Determine media state and UMS detection status
598 */
San Mehat4270e1e2010-01-29 05:32:19 -0800599 try {
600 String[] vols = mConnector.doListCommand(
601 "volume list", VoldResponseCode.VolumeListResult);
602 for (String volstr : vols) {
603 String[] tok = volstr.split(" ");
604 // FMT: <label> <mountpoint> <state>
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400605 String path = tok[1];
606 String state = Environment.MEDIA_REMOVED;
607
San Mehat4270e1e2010-01-29 05:32:19 -0800608 int st = Integer.parseInt(tok[2]);
609 if (st == VolumeState.NoMedia) {
610 state = Environment.MEDIA_REMOVED;
611 } else if (st == VolumeState.Idle) {
San Mehat207e5382010-02-04 20:46:54 -0800612 state = Environment.MEDIA_UNMOUNTED;
San Mehat4270e1e2010-01-29 05:32:19 -0800613 } else if (st == VolumeState.Mounted) {
614 state = Environment.MEDIA_MOUNTED;
San Mehata5078592010-03-25 09:36:54 -0700615 Slog.i(TAG, "Media already mounted on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800616 } else if (st == VolumeState.Shared) {
617 state = Environment.MEDIA_SHARED;
San Mehata5078592010-03-25 09:36:54 -0700618 Slog.i(TAG, "Media shared on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800619 } else {
620 throw new Exception(String.format("Unexpected state %d", st));
621 }
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400622
623 if (state != null) {
624 if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
625 updatePublicVolumeState(path, state);
626 }
San Mehat4270e1e2010-01-29 05:32:19 -0800627 }
628 } catch (Exception e) {
San Mehata5078592010-03-25 09:36:54 -0700629 Slog.e(TAG, "Error processing initial volume state", e);
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400630 updatePublicVolumeState(mExternalStoragePath, Environment.MEDIA_REMOVED);
San Mehat4270e1e2010-01-29 05:32:19 -0800631 }
632
633 try {
San Mehat207e5382010-02-04 20:46:54 -0800634 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800635 notifyShareAvailabilityChange("ums", avail);
636 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700637 Slog.w(TAG, "Failed to get share availability");
San Mehat4270e1e2010-01-29 05:32:19 -0800638 }
San Mehat207e5382010-02-04 20:46:54 -0800639 /*
Jason parks9ed98bc2011-01-17 09:58:35 -0600640 * Now that we've done our initialization, release
San Mehat207e5382010-02-04 20:46:54 -0800641 * the hounds!
642 */
643 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800644 }
645 }.start();
646 }
647
648 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800649 * Callback from NativeDaemonConnector
650 */
651 public boolean onEvent(int code, String raw, String[] cooked) {
652 Intent in = null;
653
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800654 if (DEBUG_EVENTS) {
655 StringBuilder builder = new StringBuilder();
656 builder.append("onEvent::");
657 builder.append(" raw= " + raw);
658 if (cooked != null) {
659 builder.append(" cooked = " );
660 for (String str : cooked) {
661 builder.append(" " + str);
662 }
663 }
San Mehata5078592010-03-25 09:36:54 -0700664 Slog.i(TAG, builder.toString());
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800665 }
San Mehat4270e1e2010-01-29 05:32:19 -0800666 if (code == VoldResponseCode.VolumeStateChange) {
667 /*
668 * One of the volumes we're managing has changed state.
669 * Format: "NNN Volume <label> <path> state changed
670 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
671 */
672 notifyVolumeStateChange(
673 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
674 Integer.parseInt(cooked[10]));
675 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
676 // FMT: NNN Share method <method> now <available|unavailable>
677 boolean avail = false;
678 if (cooked[5].equals("available")) {
679 avail = true;
680 }
681 notifyShareAvailabilityChange(cooked[3], avail);
682 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
683 (code == VoldResponseCode.VolumeDiskRemoved) ||
684 (code == VoldResponseCode.VolumeBadRemoval)) {
685 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
686 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
687 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
688 final String label = cooked[2];
689 final String path = cooked[3];
690 int major = -1;
691 int minor = -1;
692
693 try {
694 String devComp = cooked[6].substring(1, cooked[6].length() -1);
695 String[] devTok = devComp.split(":");
696 major = Integer.parseInt(devTok[0]);
697 minor = Integer.parseInt(devTok[1]);
698 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700699 Slog.e(TAG, "Failed to parse major/minor", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800700 }
701
San Mehat4270e1e2010-01-29 05:32:19 -0800702 if (code == VoldResponseCode.VolumeDiskInserted) {
703 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600704 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800705 public void run() {
706 try {
707 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800708 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700709 Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
San Mehat4270e1e2010-01-29 05:32:19 -0800710 }
711 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700712 Slog.w(TAG, "Failed to mount media on insertion", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800713 }
714 }
715 }.start();
716 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
717 /*
718 * This event gets trumped if we're already in BAD_REMOVAL state
719 */
720 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
721 return true;
722 }
723 /* Send the media unmounted event first */
San Mehata5078592010-03-25 09:36:54 -0700724 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800725 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
726 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
727 mContext.sendBroadcast(in);
728
San Mehata5078592010-03-25 09:36:54 -0700729 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
San Mehat4270e1e2010-01-29 05:32:19 -0800730 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
731 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
732 } else if (code == VoldResponseCode.VolumeBadRemoval) {
San Mehata5078592010-03-25 09:36:54 -0700733 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800734 /* Send the media unmounted event first */
735 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
736 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
737 mContext.sendBroadcast(in);
738
San Mehata5078592010-03-25 09:36:54 -0700739 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
San Mehat4270e1e2010-01-29 05:32:19 -0800740 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
741 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
742 } else {
San Mehata5078592010-03-25 09:36:54 -0700743 Slog.e(TAG, String.format("Unknown code {%d}", code));
San Mehat4270e1e2010-01-29 05:32:19 -0800744 }
745 } else {
746 return false;
747 }
748
749 if (in != null) {
750 mContext.sendBroadcast(in);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400751 }
752 return true;
San Mehat4270e1e2010-01-29 05:32:19 -0800753 }
754
San Mehat207e5382010-02-04 20:46:54 -0800755 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800756 String vs = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -0700757 if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
San Mehat4270e1e2010-01-29 05:32:19 -0800758
759 Intent in = null;
760
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500761 if (oldState == VolumeState.Shared && newState != oldState) {
San Mehata5078592010-03-25 09:36:54 -0700762 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500763 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
764 Uri.parse("file://" + path)));
765 }
766
San Mehat4270e1e2010-01-29 05:32:19 -0800767 if (newState == VolumeState.Init) {
768 } else if (newState == VolumeState.NoMedia) {
769 // NoMedia is handled via Disk Remove events
770 } else if (newState == VolumeState.Idle) {
771 /*
772 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
773 * if we're in the process of enabling UMS
774 */
775 if (!vs.equals(
776 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
777 Environment.MEDIA_NOFS) && !vs.equals(
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800778 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
San Mehata5078592010-03-25 09:36:54 -0700779 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
San Mehat4270e1e2010-01-29 05:32:19 -0800780 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
781 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
782 }
783 } else if (newState == VolumeState.Pending) {
784 } else if (newState == VolumeState.Checking) {
San Mehata5078592010-03-25 09:36:54 -0700785 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
San Mehat4270e1e2010-01-29 05:32:19 -0800786 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
787 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
788 } else if (newState == VolumeState.Mounted) {
San Mehata5078592010-03-25 09:36:54 -0700789 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800790 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
San Mehat4270e1e2010-01-29 05:32:19 -0800791 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
792 in.putExtra("read-only", false);
793 } else if (newState == VolumeState.Unmounting) {
San Mehat4270e1e2010-01-29 05:32:19 -0800794 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
795 } else if (newState == VolumeState.Formatting) {
796 } else if (newState == VolumeState.Shared) {
San Mehata5078592010-03-25 09:36:54 -0700797 if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800798 /* Send the media unmounted event first */
799 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
800 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
801 mContext.sendBroadcast(in);
802
San Mehata5078592010-03-25 09:36:54 -0700803 if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
San Mehat4270e1e2010-01-29 05:32:19 -0800804 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
805 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
San Mehata5078592010-03-25 09:36:54 -0700806 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
San Mehat4270e1e2010-01-29 05:32:19 -0800807 } else if (newState == VolumeState.SharedMnt) {
San Mehata5078592010-03-25 09:36:54 -0700808 Slog.e(TAG, "Live shared mounts not supported yet!");
San Mehat4270e1e2010-01-29 05:32:19 -0800809 return;
810 } else {
San Mehata5078592010-03-25 09:36:54 -0700811 Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800812 }
813
814 if (in != null) {
815 mContext.sendBroadcast(in);
816 }
817 }
818
San Mehat207e5382010-02-04 20:46:54 -0800819 private boolean doGetShareMethodAvailable(String method) {
Kenny Root85fb2062010-06-01 20:50:21 -0700820 ArrayList<String> rsp;
Kenny Roota80ce062010-06-01 13:23:53 -0700821 try {
Kenny Root85fb2062010-06-01 20:50:21 -0700822 rsp = mConnector.doCommand("share status " + method);
Kenny Roota80ce062010-06-01 13:23:53 -0700823 } catch (NativeDaemonConnectorException ex) {
824 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
825 return false;
826 }
San Mehat207e5382010-02-04 20:46:54 -0800827
828 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700829 String[] tok = line.split(" ");
830 if (tok.length < 3) {
831 Slog.e(TAG, "Malformed response to share status " + method);
832 return false;
833 }
834
San Mehat207e5382010-02-04 20:46:54 -0800835 int code;
836 try {
837 code = Integer.parseInt(tok[0]);
838 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700839 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehat207e5382010-02-04 20:46:54 -0800840 return false;
841 }
842 if (code == VoldResponseCode.ShareStatusResult) {
843 if (tok[2].equals("available"))
844 return true;
845 return false;
846 } else {
San Mehata5078592010-03-25 09:36:54 -0700847 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehat207e5382010-02-04 20:46:54 -0800848 return false;
849 }
850 }
San Mehata5078592010-03-25 09:36:54 -0700851 Slog.e(TAG, "Got an empty response");
San Mehat207e5382010-02-04 20:46:54 -0800852 return false;
853 }
854
855 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800856 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800857
San Mehata5078592010-03-25 09:36:54 -0700858 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
San Mehat207e5382010-02-04 20:46:54 -0800859 try {
860 mConnector.doCommand(String.format("volume mount %s", path));
861 } catch (NativeDaemonConnectorException e) {
862 /*
863 * Mount failed for some reason
864 */
865 Intent in = null;
866 int code = e.getCode();
867 if (code == VoldResponseCode.OpFailedNoMedia) {
868 /*
869 * Attempt to mount but no media inserted
870 */
San Mehatb1043402010-02-05 08:26:50 -0800871 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800872 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
San Mehata5078592010-03-25 09:36:54 -0700873 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
San Mehat207e5382010-02-04 20:46:54 -0800874 /*
875 * Media is blank or does not contain a supported filesystem
876 */
877 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
878 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800879 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800880 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehata5078592010-03-25 09:36:54 -0700881 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
San Mehat207e5382010-02-04 20:46:54 -0800882 /*
883 * Volume consistency check failed
884 */
885 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
886 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800887 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800888 } else {
San Mehatb1043402010-02-05 08:26:50 -0800889 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800890 }
891
892 /*
893 * Send broadcast intent (if required for the failure)
894 */
895 if (in != null) {
896 mContext.sendBroadcast(in);
897 }
898 }
899
900 return rc;
901 }
902
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800903 /*
904 * If force is not set, we do not unmount if there are
905 * processes holding references to the volume about to be unmounted.
906 * If force is set, all the processes holding references need to be
907 * killed via the ActivityManager before actually unmounting the volume.
908 * This might even take a while and might be retried after timed delays
909 * to make sure we dont end up in an instable state and kill some core
910 * processes.
911 */
San Mehatd9709982010-02-18 11:43:03 -0800912 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800913 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800914 return VoldResponseCode.OpFailedVolNotMounted;
915 }
Kenny Rootaa485402010-09-14 14:49:41 -0700916
917 /*
918 * Force a GC to make sure AssetManagers in other threads of the
919 * system_server are cleaned up. We have to do this since AssetManager
920 * instances are kept as a WeakReference and it's possible we have files
921 * open on the external storage.
922 */
923 Runtime.getRuntime().gc();
924
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800925 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700926 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800927 try {
San Mehatd9709982010-02-18 11:43:03 -0800928 mConnector.doCommand(String.format(
929 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700930 // We unmounted the volume. None of the asec containers are available now.
931 synchronized (mAsecMountSet) {
932 mAsecMountSet.clear();
933 }
San Mehatb1043402010-02-05 08:26:50 -0800934 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800935 } catch (NativeDaemonConnectorException e) {
936 // Don't worry about mismatch in PackageManager since the
937 // call back will handle the status changes any way.
938 int code = e.getCode();
939 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800940 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800941 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
942 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800943 } else {
San Mehatb1043402010-02-05 08:26:50 -0800944 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800945 }
946 }
947 }
948
949 private int doFormatVolume(String path) {
950 try {
951 String cmd = String.format("volume format %s", path);
952 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800953 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800954 } catch (NativeDaemonConnectorException e) {
955 int code = e.getCode();
956 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800957 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800958 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800959 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800960 } else {
San Mehatb1043402010-02-05 08:26:50 -0800961 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800962 }
963 }
964 }
965
San Mehatb1043402010-02-05 08:26:50 -0800966 private boolean doGetVolumeShared(String path, String method) {
967 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700968 ArrayList<String> rsp;
969
970 try {
971 rsp = mConnector.doCommand(cmd);
972 } catch (NativeDaemonConnectorException ex) {
973 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
974 return false;
975 }
San Mehatb1043402010-02-05 08:26:50 -0800976
977 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700978 String[] tok = line.split(" ");
979 if (tok.length < 3) {
980 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
981 return false;
982 }
983
San Mehatb1043402010-02-05 08:26:50 -0800984 int code;
985 try {
986 code = Integer.parseInt(tok[0]);
987 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700988 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800989 return false;
990 }
991 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700992 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800993 } else {
San Mehata5078592010-03-25 09:36:54 -0700994 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800995 return false;
996 }
997 }
San Mehata5078592010-03-25 09:36:54 -0700998 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800999 return false;
1000 }
1001
San Mehat207e5382010-02-04 20:46:54 -08001002 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -08001003 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -07001004 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -08001005 return;
1006 }
1007
1008 synchronized (mListeners) {
1009 for (int i = mListeners.size() -1; i >= 0; i--) {
1010 MountServiceBinderListener bl = mListeners.get(i);
1011 try {
San Mehatb1043402010-02-05 08:26:50 -08001012 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -08001013 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001014 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -08001015 mListeners.remove(i);
1016 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -07001017 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -08001018 }
1019 }
1020 }
1021
San Mehat207e5382010-02-04 20:46:54 -08001022 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -08001023 sendUmsIntent(avail);
1024 } else {
1025 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -08001026 }
San Mehat2fe718a2010-03-11 12:01:49 -08001027
1028 final String path = Environment.getExternalStorageDirectory().getPath();
1029 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
1030 /*
1031 * USB mass storage disconnected while enabled
1032 */
1033 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -06001034 @Override
San Mehat2fe718a2010-03-11 12:01:49 -08001035 public void run() {
1036 try {
1037 int rc;
San Mehata5078592010-03-25 09:36:54 -07001038 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -08001039 doShareUnshareVolume(path, "ums", false);
1040 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001041 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -08001042 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
1043 path, rc));
1044 }
1045 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -07001046 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -08001047 }
1048 }
1049 }.start();
1050 }
San Mehat4270e1e2010-01-29 05:32:19 -08001051 }
1052
San Mehat6a965af22010-02-24 17:47:30 -08001053 private void sendUmsIntent(boolean c) {
1054 mContext.sendBroadcast(
1055 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
1056 }
1057
San Mehat207e5382010-02-04 20:46:54 -08001058 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -08001059 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
1060 throw new SecurityException(String.format("Requires %s permission", perm));
1061 }
1062 }
1063
1064 /**
San Mehat207e5382010-02-04 20:46:54 -08001065 * Constructs a new MountService instance
1066 *
1067 * @param context Binder context for this service
1068 */
1069 public MountService(Context context) {
1070 mContext = context;
1071
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001072 mExternalStoragePath = Environment.getExternalStorageDirectory().getPath();
Mike Lockwood03559752010-07-19 18:25:03 -04001073 mEmulateExternalStorage = context.getResources().getBoolean(
1074 com.android.internal.R.bool.config_emulateExternalStorage);
1075 if (mEmulateExternalStorage) {
1076 Slog.d(TAG, "using emulated external storage");
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001077 mVolumeStates.put(mExternalStoragePath, Environment.MEDIA_MOUNTED);
Mike Lockwood03559752010-07-19 18:25:03 -04001078 }
1079
San Mehat207e5382010-02-04 20:46:54 -08001080 // XXX: This will go away soon in favor of IMountServiceObserver
1081 mPms = (PackageManagerService) ServiceManager.getService("package");
1082
1083 mContext.registerReceiver(mBroadcastReceiver,
1084 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
1085
Daniel Sandler5f27ef42010-03-16 15:42:02 -04001086 mHandlerThread = new HandlerThread("MountService");
1087 mHandlerThread.start();
1088 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
1089
Kenny Roota02b8b02010-08-05 16:14:17 -07001090 // Add OBB Action Handler to MountService thread.
1091 mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper());
1092
Marco Nelissenc34ebce2010-02-18 13:39:41 -08001093 /*
1094 * Vold does not run in the simulator, so pretend the connector thread
1095 * ran and did its thing.
1096 */
1097 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
1098 mReady = true;
1099 mUmsEnabling = true;
1100 return;
1101 }
1102
Kenny Root305bcbf2010-09-03 07:56:38 -07001103 /*
1104 * Create the connection to vold with a maximum queue of twice the
1105 * amount of containers we'd ever expect to have. This keeps an
1106 * "asec list" from blocking a thread repeatedly.
1107 */
1108 mConnector = new NativeDaemonConnector(this, "vold",
1109 PackageManagerService.MAX_CONTAINERS * 2, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001110 mReady = false;
Kenny Root305bcbf2010-09-03 07:56:38 -07001111 Thread thread = new Thread(mConnector, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001112 thread.start();
1113 }
1114
1115 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001116 * Exposed API calls below here
1117 */
1118
1119 public void registerListener(IMountServiceListener listener) {
1120 synchronized (mListeners) {
1121 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
1122 try {
1123 listener.asBinder().linkToDeath(bl, 0);
1124 mListeners.add(bl);
1125 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001126 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -08001127 }
1128 }
1129 }
1130
1131 public void unregisterListener(IMountServiceListener listener) {
1132 synchronized (mListeners) {
1133 for(MountServiceBinderListener bl : mListeners) {
1134 if (bl.mListener == listener) {
1135 mListeners.remove(mListeners.indexOf(bl));
1136 return;
1137 }
1138 }
1139 }
1140 }
1141
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001142 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -08001143 validatePermission(android.Manifest.permission.SHUTDOWN);
1144
San Mehata5078592010-03-25 09:36:54 -07001145 Slog.i(TAG, "Shutting down");
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001146 synchronized (mVolumeStates) {
1147 for (String path : mVolumeStates.keySet()) {
1148 String state = mVolumeStates.get(path);
San Mehat4270e1e2010-01-29 05:32:19 -08001149
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001150 if (state.equals(Environment.MEDIA_SHARED)) {
1151 /*
1152 * If the media is currently shared, unshare it.
1153 * XXX: This is still dangerous!. We should not
1154 * be rebooting at *all* if UMS is enabled, since
1155 * the UMS host could have dirty FAT cache entries
1156 * yet to flush.
1157 */
1158 setUsbMassStorageEnabled(false);
1159 } else if (state.equals(Environment.MEDIA_CHECKING)) {
1160 /*
1161 * If the media is being checked, then we need to wait for
1162 * it to complete before being able to proceed.
1163 */
1164 // XXX: @hackbod - Should we disable the ANR timer here?
1165 int retries = 30;
1166 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
1167 try {
1168 Thread.sleep(1000);
1169 } catch (InterruptedException iex) {
1170 Slog.e(TAG, "Interrupted while waiting for media", iex);
1171 break;
1172 }
1173 state = Environment.getExternalStorageState();
1174 }
1175 if (retries == 0) {
1176 Slog.e(TAG, "Timed out waiting for media to check");
1177 }
San Mehat91c77612010-01-07 10:39:41 -08001178 }
San Mehat91c77612010-01-07 10:39:41 -08001179
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001180 if (state.equals(Environment.MEDIA_MOUNTED)) {
1181 // Post a unmount message.
1182 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
1183 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
1184 } else if (observer != null) {
1185 /*
1186 * Observer is waiting for onShutDownComplete when we are done.
1187 * Since nothing will be done send notification directly so shutdown
1188 * sequence can continue.
1189 */
1190 try {
1191 observer.onShutDownComplete(StorageResultCode.OperationSucceeded);
1192 } catch (RemoteException e) {
1193 Slog.w(TAG, "RemoteException when shutting down");
1194 }
1195 }
Johan Alfven5d0db4d2010-11-09 10:32:25 +01001196 }
San Mehat4270e1e2010-01-29 05:32:19 -08001197 }
1198 }
1199
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001200 private boolean getUmsEnabling() {
1201 synchronized (mListeners) {
1202 return mUmsEnabling;
1203 }
1204 }
1205
1206 private void setUmsEnabling(boolean enable) {
1207 synchronized (mListeners) {
Tony Wufc711252010-08-09 16:49:19 +08001208 mUmsEnabling = enable;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001209 }
1210 }
1211
San Mehatb1043402010-02-05 08:26:50 -08001212 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -08001213 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -08001214
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001215 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001216 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001217 }
San Mehatb1043402010-02-05 08:26:50 -08001218 return doGetShareMethodAvailable("ums");
1219 }
1220
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001221 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001222 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001223 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001224
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001225 // TODO: Add support for multiple share methods
1226
1227 /*
1228 * If the volume is mounted and we're enabling then unmount it
1229 */
1230 String path = Environment.getExternalStorageDirectory().getPath();
1231 String vs = getVolumeState(path);
1232 String method = "ums";
1233 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1234 // Override for isUsbMassStorageEnabled()
1235 setUmsEnabling(enable);
1236 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1237 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1238 // Clear override
1239 setUmsEnabling(false);
1240 }
1241 /*
1242 * If we disabled UMS then mount the volume
1243 */
1244 if (!enable) {
1245 doShareUnshareVolume(path, method, enable);
1246 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001247 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001248 " after disabling share method " + method);
1249 /*
1250 * Even though the mount failed, the unshare didn't so don't indicate an error.
1251 * The mountVolume() call will have set the storage state and sent the necessary
1252 * broadcasts.
1253 */
1254 }
1255 }
San Mehatb1043402010-02-05 08:26:50 -08001256 }
1257
1258 public boolean isUsbMassStorageEnabled() {
1259 waitForReady();
1260 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001262
San Mehat7fd0fee2009-12-17 07:12:23 -08001263 /**
1264 * @return state of the volume at the specified mount point
1265 */
San Mehat4270e1e2010-01-29 05:32:19 -08001266 public String getVolumeState(String mountPoint) {
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001267 synchronized (mVolumeStates) {
1268 String state = mVolumeStates.get(mountPoint);
1269 if (state == null) {
1270 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
1271 throw new IllegalArgumentException();
1272 }
San Mehat7fd0fee2009-12-17 07:12:23 -08001273
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001274 return state;
1275 }
San Mehat7fd0fee2009-12-17 07:12:23 -08001276 }
1277
Kenny Roote1ff2142010-10-12 11:20:01 -07001278 public boolean isExternalStorageEmulated() {
1279 return mEmulateExternalStorage;
1280 }
1281
San Mehat4270e1e2010-01-29 05:32:19 -08001282 public int mountVolume(String path) {
1283 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001284
San Mehat207e5382010-02-04 20:46:54 -08001285 waitForReady();
1286 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 }
1288
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001289 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001290 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001291 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001293 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001294 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001295 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1296 Environment.MEDIA_REMOVED.equals(volState) ||
1297 Environment.MEDIA_SHARED.equals(volState) ||
1298 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1299 // Media already unmounted or cannot be unmounted.
1300 // TODO return valid return code when adding observer call back.
1301 return;
1302 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001303 UnmountCallBack ucb = new UnmountCallBack(path, force);
1304 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 }
1306
San Mehat4270e1e2010-01-29 05:32:19 -08001307 public int formatVolume(String path) {
1308 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001309 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001310
San Mehat207e5382010-02-04 20:46:54 -08001311 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 }
1313
San Mehatc1b4ce92010-02-16 17:13:03 -08001314 public int []getStorageUsers(String path) {
1315 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1316 waitForReady();
1317 try {
1318 String[] r = mConnector.doListCommand(
1319 String.format("storage users %s", path),
1320 VoldResponseCode.StorageUsersListResult);
1321 // FMT: <pid> <process name>
1322 int[] data = new int[r.length];
1323 for (int i = 0; i < r.length; i++) {
1324 String []tok = r[i].split(" ");
1325 try {
1326 data[i] = Integer.parseInt(tok[0]);
1327 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001328 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001329 return new int[0];
1330 }
1331 }
1332 return data;
1333 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001334 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001335 return new int[0];
1336 }
1337 }
1338
San Mehatb1043402010-02-05 08:26:50 -08001339 private void warnOnNotMounted() {
1340 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001341 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001342 }
1343 }
1344
San Mehat4270e1e2010-01-29 05:32:19 -08001345 public String[] getSecureContainerList() {
1346 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001347 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001348 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001349
San Mehat4270e1e2010-01-29 05:32:19 -08001350 try {
1351 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1352 } catch (NativeDaemonConnectorException e) {
1353 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 }
1355 }
San Mehat36972292010-01-06 11:06:32 -08001356
San Mehat4270e1e2010-01-29 05:32:19 -08001357 public int createSecureContainer(String id, int sizeMb, String fstype,
1358 String key, int ownerUid) {
1359 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001360 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001361 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001362
San Mehatb1043402010-02-05 08:26:50 -08001363 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001364 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1365 try {
1366 mConnector.doCommand(cmd);
1367 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001368 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001369 }
San Mehata181b212010-02-11 06:50:20 -08001370
1371 if (rc == StorageResultCode.OperationSucceeded) {
1372 synchronized (mAsecMountSet) {
1373 mAsecMountSet.add(id);
1374 }
1375 }
San Mehat4270e1e2010-01-29 05:32:19 -08001376 return rc;
San Mehat36972292010-01-06 11:06:32 -08001377 }
1378
San Mehat4270e1e2010-01-29 05:32:19 -08001379 public int finalizeSecureContainer(String id) {
1380 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001381 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001382
San Mehatb1043402010-02-05 08:26:50 -08001383 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001384 try {
1385 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001386 /*
1387 * Finalization does a remount, so no need
1388 * to update mAsecMountSet
1389 */
San Mehat4270e1e2010-01-29 05:32:19 -08001390 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001391 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001392 }
San Mehat4270e1e2010-01-29 05:32:19 -08001393 return rc;
San Mehat36972292010-01-06 11:06:32 -08001394 }
1395
San Mehatd9709982010-02-18 11:43:03 -08001396 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001397 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001398 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001399 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001400
Kenny Rootaa485402010-09-14 14:49:41 -07001401 /*
1402 * Force a GC to make sure AssetManagers in other threads of the
1403 * system_server are cleaned up. We have to do this since AssetManager
1404 * instances are kept as a WeakReference and it's possible we have files
1405 * open on the external storage.
1406 */
1407 Runtime.getRuntime().gc();
1408
San Mehatb1043402010-02-05 08:26:50 -08001409 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001410 try {
San Mehatd9709982010-02-18 11:43:03 -08001411 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001412 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001413 int code = e.getCode();
1414 if (code == VoldResponseCode.OpFailedStorageBusy) {
1415 rc = StorageResultCode.OperationFailedStorageBusy;
1416 } else {
1417 rc = StorageResultCode.OperationFailedInternalError;
1418 }
San Mehat02735bc2010-01-26 15:18:08 -08001419 }
San Mehata181b212010-02-11 06:50:20 -08001420
1421 if (rc == StorageResultCode.OperationSucceeded) {
1422 synchronized (mAsecMountSet) {
1423 if (mAsecMountSet.contains(id)) {
1424 mAsecMountSet.remove(id);
1425 }
1426 }
1427 }
1428
San Mehat4270e1e2010-01-29 05:32:19 -08001429 return rc;
San Mehat36972292010-01-06 11:06:32 -08001430 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001431
San Mehat4270e1e2010-01-29 05:32:19 -08001432 public int mountSecureContainer(String id, String key, int ownerUid) {
1433 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001434 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001435 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001436
San Mehata181b212010-02-11 06:50:20 -08001437 synchronized (mAsecMountSet) {
1438 if (mAsecMountSet.contains(id)) {
1439 return StorageResultCode.OperationFailedStorageMounted;
1440 }
1441 }
1442
San Mehatb1043402010-02-05 08:26:50 -08001443 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001444 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1445 try {
1446 mConnector.doCommand(cmd);
1447 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001448 int code = e.getCode();
1449 if (code != VoldResponseCode.OpFailedStorageBusy) {
1450 rc = StorageResultCode.OperationFailedInternalError;
1451 }
San Mehat02735bc2010-01-26 15:18:08 -08001452 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001453
1454 if (rc == StorageResultCode.OperationSucceeded) {
1455 synchronized (mAsecMountSet) {
1456 mAsecMountSet.add(id);
1457 }
1458 }
San Mehat4270e1e2010-01-29 05:32:19 -08001459 return rc;
San Mehat36972292010-01-06 11:06:32 -08001460 }
1461
San Mehatd9709982010-02-18 11:43:03 -08001462 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001463 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001464 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001465 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001466
San Mehat6cdd9c02010-02-09 14:45:20 -08001467 synchronized (mAsecMountSet) {
1468 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001469 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001470 }
1471 }
1472
Kenny Rootaa485402010-09-14 14:49:41 -07001473 /*
1474 * Force a GC to make sure AssetManagers in other threads of the
1475 * system_server are cleaned up. We have to do this since AssetManager
1476 * instances are kept as a WeakReference and it's possible we have files
1477 * open on the external storage.
1478 */
1479 Runtime.getRuntime().gc();
1480
San Mehatb1043402010-02-05 08:26:50 -08001481 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001482 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001483 try {
1484 mConnector.doCommand(cmd);
1485 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001486 int code = e.getCode();
1487 if (code == VoldResponseCode.OpFailedStorageBusy) {
1488 rc = StorageResultCode.OperationFailedStorageBusy;
1489 } else {
1490 rc = StorageResultCode.OperationFailedInternalError;
1491 }
San Mehat02735bc2010-01-26 15:18:08 -08001492 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001493
1494 if (rc == StorageResultCode.OperationSucceeded) {
1495 synchronized (mAsecMountSet) {
1496 mAsecMountSet.remove(id);
1497 }
1498 }
San Mehat4270e1e2010-01-29 05:32:19 -08001499 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001500 }
1501
San Mehat6cdd9c02010-02-09 14:45:20 -08001502 public boolean isSecureContainerMounted(String id) {
1503 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1504 waitForReady();
1505 warnOnNotMounted();
1506
1507 synchronized (mAsecMountSet) {
1508 return mAsecMountSet.contains(id);
1509 }
1510 }
1511
San Mehat4270e1e2010-01-29 05:32:19 -08001512 public int renameSecureContainer(String oldId, String newId) {
1513 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001514 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001515 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001516
San Mehata181b212010-02-11 06:50:20 -08001517 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001518 /*
Jason parks9ed98bc2011-01-17 09:58:35 -06001519 * Because a mounted container has active internal state which cannot be
San Mehat85451ee2010-02-24 08:54:18 -08001520 * changed while active, we must ensure both ids are not currently mounted.
1521 */
1522 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001523 return StorageResultCode.OperationFailedStorageMounted;
1524 }
1525 }
1526
San Mehatb1043402010-02-05 08:26:50 -08001527 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001528 String cmd = String.format("asec rename %s %s", oldId, newId);
1529 try {
1530 mConnector.doCommand(cmd);
1531 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001532 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001533 }
San Mehata181b212010-02-11 06:50:20 -08001534
San Mehat4270e1e2010-01-29 05:32:19 -08001535 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001536 }
1537
San Mehat4270e1e2010-01-29 05:32:19 -08001538 public String getSecureContainerPath(String id) {
1539 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001540 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001541 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001542
San Mehat2d66cef2010-03-23 11:12:52 -07001543 try {
1544 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1545 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001546 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001547 if (code != VoldResponseCode.AsecPathResult) {
1548 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1549 }
1550 return tok[1];
1551 } catch (NativeDaemonConnectorException e) {
1552 int code = e.getCode();
1553 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1554 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001555 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001556 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001557 }
1558 }
San Mehat22dd86e2010-01-12 12:21:18 -08001559 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001560
1561 public void finishMediaUpdate() {
1562 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1563 }
Kenny Root02c87302010-07-01 08:10:18 -07001564
Kenny Roota02b8b02010-08-05 16:14:17 -07001565 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
1566 if (callerUid == android.os.Process.SYSTEM_UID) {
1567 return true;
1568 }
1569
Kenny Root02c87302010-07-01 08:10:18 -07001570 if (packageName == null) {
1571 return false;
1572 }
1573
1574 final int packageUid = mPms.getPackageUid(packageName);
1575
1576 if (DEBUG_OBB) {
1577 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
1578 packageUid + ", callerUid = " + callerUid);
1579 }
1580
1581 return callerUid == packageUid;
1582 }
1583
1584 public String getMountedObbPath(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001585 if (filename == null) {
1586 throw new IllegalArgumentException("filename cannot be null");
1587 }
1588
Kenny Root02c87302010-07-01 08:10:18 -07001589 waitForReady();
1590 warnOnNotMounted();
1591
Kenny Root02c87302010-07-01 08:10:18 -07001592 try {
1593 ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename));
1594 String []tok = rsp.get(0).split(" ");
1595 int code = Integer.parseInt(tok[0]);
1596 if (code != VoldResponseCode.AsecPathResult) {
1597 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1598 }
1599 return tok[1];
1600 } catch (NativeDaemonConnectorException e) {
1601 int code = e.getCode();
1602 if (code == VoldResponseCode.OpFailedStorageNotFound) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001603 return null;
Kenny Root02c87302010-07-01 08:10:18 -07001604 } else {
1605 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1606 }
1607 }
1608 }
1609
1610 public boolean isObbMounted(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001611 if (filename == null) {
1612 throw new IllegalArgumentException("filename cannot be null");
Kenny Root02c87302010-07-01 08:10:18 -07001613 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001614
1615 synchronized (mObbMounts) {
1616 return mObbPathToStateMap.containsKey(filename);
1617 }
Kenny Root02c87302010-07-01 08:10:18 -07001618 }
1619
Kenny Rootaf9d6672010-10-08 09:21:39 -07001620 public void mountObb(String filename, String key, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -07001621 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001622 if (filename == null) {
1623 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootaf9d6672010-10-08 09:21:39 -07001624 }
1625
1626 if (token == null) {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001627 throw new IllegalArgumentException("token cannot be null");
1628 }
1629
Kenny Rootaf9d6672010-10-08 09:21:39 -07001630 final int callerUid = Binder.getCallingUid();
1631 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1632 final ObbAction action = new MountObbAction(obbState, key);
Kenny Roota02b8b02010-08-05 16:14:17 -07001633 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
1634
1635 if (DEBUG_OBB)
1636 Slog.i(TAG, "Send to OBB handler: " + action.toString());
Kenny Root02c87302010-07-01 08:10:18 -07001637 }
1638
Kenny Rootaf9d6672010-10-08 09:21:39 -07001639 public void unmountObb(String filename, boolean force, IObbActionListener token, int nonce)
1640 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001641 if (filename == null) {
1642 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootf1121dc2010-09-29 07:30:53 -07001643 }
1644
Kenny Rootaf9d6672010-10-08 09:21:39 -07001645 final int callerUid = Binder.getCallingUid();
1646 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1647 final ObbAction action = new UnmountObbAction(obbState, force);
Kenny Roota02b8b02010-08-05 16:14:17 -07001648 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root02c87302010-07-01 08:10:18 -07001649
Kenny Roota02b8b02010-08-05 16:14:17 -07001650 if (DEBUG_OBB)
1651 Slog.i(TAG, "Send to OBB handler: " + action.toString());
1652 }
1653
Jason parks5af0b912010-11-29 09:05:25 -06001654 public int decryptStorage(String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06001655 if (TextUtils.isEmpty(password)) {
1656 throw new IllegalArgumentException("password cannot be empty");
Jason parks5af0b912010-11-29 09:05:25 -06001657 }
1658
Jason parks8888c592011-01-20 22:46:41 -06001659 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1660 "no permission to access the crypt keeper");
Jason parks5af0b912010-11-29 09:05:25 -06001661
1662 waitForReady();
1663
1664 if (DEBUG_EVENTS) {
1665 Slog.i(TAG, "decrypting storage...");
1666 }
1667
1668 try {
Jason parks9ed98bc2011-01-17 09:58:35 -06001669 ArrayList<String> rsp = mConnector.doCommand("cryptfs checkpw " + password);
Jason parksf7b3cd42011-01-27 09:28:25 -06001670 String[] tokens = rsp.get(0).split(" ");
Jason parks9ed98bc2011-01-17 09:58:35 -06001671
Jason parksf7b3cd42011-01-27 09:28:25 -06001672 if (tokens == null || tokens.length != 2) {
Jason parks9ed98bc2011-01-17 09:58:35 -06001673 return -1;
1674 }
1675
Jason parksf7b3cd42011-01-27 09:28:25 -06001676 int code = Integer.parseInt(tokens[1]);
Jason parks9ed98bc2011-01-17 09:58:35 -06001677
1678 if (code == 0) {
1679 // Decrypt was successful. Post a delayed message before restarting in order
1680 // to let the UI to clear itself
1681 mHandler.postDelayed(new Runnable() {
1682 public void run() {
1683 mConnector.doCommand(String.format("cryptfs restart"));
1684 }
Jason parksf7b3cd42011-01-27 09:28:25 -06001685 }, 1000); // 1 second
Jason parks9ed98bc2011-01-17 09:58:35 -06001686 }
1687
1688 return code;
Jason parks5af0b912010-11-29 09:05:25 -06001689 } catch (NativeDaemonConnectorException e) {
1690 // Decryption failed
1691 return e.getCode();
1692 }
Jason parks5af0b912010-11-29 09:05:25 -06001693 }
1694
Jason parks56aa5322011-01-07 09:01:15 -06001695 public int encryptStorage(String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06001696 if (TextUtils.isEmpty(password)) {
1697 throw new IllegalArgumentException("password cannot be empty");
Jason parks56aa5322011-01-07 09:01:15 -06001698 }
1699
Jason parks8888c592011-01-20 22:46:41 -06001700 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1701 "no permission to access the crypt keeper");
Jason parks56aa5322011-01-07 09:01:15 -06001702
1703 waitForReady();
1704
1705 if (DEBUG_EVENTS) {
Jason parks8888c592011-01-20 22:46:41 -06001706 Slog.i(TAG, "encrypting storage...");
Jason parks56aa5322011-01-07 09:01:15 -06001707 }
1708
1709 try {
Jason parks9ed98bc2011-01-17 09:58:35 -06001710 mConnector.doCommand(String.format("cryptfs enablecrypto inplace %s", password));
Jason parks56aa5322011-01-07 09:01:15 -06001711 } catch (NativeDaemonConnectorException e) {
1712 // Encryption failed
1713 return e.getCode();
1714 }
1715
1716 return 0;
1717 }
1718
Jason parksf7b3cd42011-01-27 09:28:25 -06001719 public int changeEncryptionPassword(String password) {
1720 if (TextUtils.isEmpty(password)) {
1721 throw new IllegalArgumentException("password cannot be empty");
1722 }
1723
1724 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1725 "no permission to access the crypt keeper");
1726
1727 waitForReady();
1728
1729 if (DEBUG_EVENTS) {
1730 Slog.i(TAG, "changing encryption password...");
1731 }
1732
1733 try {
1734 ArrayList<String> response = mConnector.doCommand("cryptfs changepw " + password);
1735
1736 String[] tokens = response.get(0).split(" ");
1737
1738 if (tokens == null || tokens.length != 2) {
1739 return -1;
1740 }
1741
1742 return Integer.parseInt(tokens[1]);
1743 } catch (NativeDaemonConnectorException e) {
1744 // Encryption failed
1745 return e.getCode();
1746 }
1747 }
1748
Mike Lockwoodd967f462011-03-24 08:12:30 -07001749 public String[] getVolumeList() {
1750 synchronized(mVolumeStates) {
1751 Set<String> volumes = mVolumeStates.keySet();
1752 String[] result = new String[volumes.size()];
1753 int i = 0;
1754 for (String volume : volumes) {
1755 result[i++] = volume;
1756 }
1757 return result;
1758 }
1759 }
1760
Kenny Rootaf9d6672010-10-08 09:21:39 -07001761 private void addObbStateLocked(ObbState obbState) throws RemoteException {
1762 final IBinder binder = obbState.getBinder();
1763 List<ObbState> obbStates = mObbMounts.get(binder);
Kenny Root5919ac62010-10-05 09:49:40 -07001764
Kenny Rootaf9d6672010-10-08 09:21:39 -07001765 if (obbStates == null) {
1766 obbStates = new ArrayList<ObbState>();
1767 mObbMounts.put(binder, obbStates);
1768 } else {
1769 for (final ObbState o : obbStates) {
1770 if (o.filename.equals(obbState.filename)) {
1771 throw new IllegalStateException("Attempt to add ObbState twice. "
1772 + "This indicates an error in the MountService logic.");
Kenny Root5919ac62010-10-05 09:49:40 -07001773 }
1774 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001775 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001776
1777 obbStates.add(obbState);
1778 try {
1779 obbState.link();
1780 } catch (RemoteException e) {
1781 /*
1782 * The binder died before we could link it, so clean up our state
1783 * and return failure.
1784 */
1785 obbStates.remove(obbState);
1786 if (obbStates.isEmpty()) {
1787 mObbMounts.remove(binder);
1788 }
1789
1790 // Rethrow the error so mountObb can get it
1791 throw e;
1792 }
1793
1794 mObbPathToStateMap.put(obbState.filename, obbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07001795 }
1796
Kenny Rootaf9d6672010-10-08 09:21:39 -07001797 private void removeObbStateLocked(ObbState obbState) {
1798 final IBinder binder = obbState.getBinder();
1799 final List<ObbState> obbStates = mObbMounts.get(binder);
1800 if (obbStates != null) {
1801 if (obbStates.remove(obbState)) {
1802 obbState.unlink();
Kenny Root05105f72010-09-22 17:29:43 -07001803 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001804 if (obbStates.isEmpty()) {
1805 mObbMounts.remove(binder);
1806 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001807 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001808
Kenny Rootaf9d6672010-10-08 09:21:39 -07001809 mObbPathToStateMap.remove(obbState.filename);
Kenny Root38cf8862010-09-26 14:18:51 -07001810 }
1811
Kenny Roota02b8b02010-08-05 16:14:17 -07001812 private class ObbActionHandler extends Handler {
1813 private boolean mBound = false;
Kenny Root480afe72010-10-07 10:17:50 -07001814 private final List<ObbAction> mActions = new LinkedList<ObbAction>();
Kenny Roota02b8b02010-08-05 16:14:17 -07001815
1816 ObbActionHandler(Looper l) {
1817 super(l);
1818 }
1819
1820 @Override
1821 public void handleMessage(Message msg) {
1822 switch (msg.what) {
1823 case OBB_RUN_ACTION: {
Kenny Root480afe72010-10-07 10:17:50 -07001824 final ObbAction action = (ObbAction) msg.obj;
Kenny Roota02b8b02010-08-05 16:14:17 -07001825
1826 if (DEBUG_OBB)
1827 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
1828
1829 // If a bind was already initiated we don't really
1830 // need to do anything. The pending install
1831 // will be processed later on.
1832 if (!mBound) {
1833 // If this is the only one pending we might
1834 // have to bind to the service again.
1835 if (!connectToService()) {
1836 Slog.e(TAG, "Failed to bind to media container service");
1837 action.handleError();
1838 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07001839 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001840 }
Kenny Root735de3b2010-09-30 14:11:39 -07001841
Kenny Root735de3b2010-09-30 14:11:39 -07001842 mActions.add(action);
Kenny Roota02b8b02010-08-05 16:14:17 -07001843 break;
1844 }
1845 case OBB_MCS_BOUND: {
1846 if (DEBUG_OBB)
1847 Slog.i(TAG, "OBB_MCS_BOUND");
1848 if (msg.obj != null) {
1849 mContainerService = (IMediaContainerService) msg.obj;
1850 }
1851 if (mContainerService == null) {
1852 // Something seriously wrong. Bail out
1853 Slog.e(TAG, "Cannot bind to media container service");
1854 for (ObbAction action : mActions) {
1855 // Indicate service bind error
1856 action.handleError();
1857 }
1858 mActions.clear();
1859 } else if (mActions.size() > 0) {
Kenny Root480afe72010-10-07 10:17:50 -07001860 final ObbAction action = mActions.get(0);
Kenny Roota02b8b02010-08-05 16:14:17 -07001861 if (action != null) {
1862 action.execute(this);
1863 }
1864 } else {
1865 // Should never happen ideally.
1866 Slog.w(TAG, "Empty queue");
1867 }
1868 break;
1869 }
1870 case OBB_MCS_RECONNECT: {
1871 if (DEBUG_OBB)
1872 Slog.i(TAG, "OBB_MCS_RECONNECT");
1873 if (mActions.size() > 0) {
1874 if (mBound) {
1875 disconnectService();
1876 }
1877 if (!connectToService()) {
1878 Slog.e(TAG, "Failed to bind to media container service");
1879 for (ObbAction action : mActions) {
1880 // Indicate service bind error
1881 action.handleError();
1882 }
1883 mActions.clear();
1884 }
1885 }
1886 break;
1887 }
1888 case OBB_MCS_UNBIND: {
1889 if (DEBUG_OBB)
1890 Slog.i(TAG, "OBB_MCS_UNBIND");
1891
1892 // Delete pending install
1893 if (mActions.size() > 0) {
1894 mActions.remove(0);
1895 }
1896 if (mActions.size() == 0) {
1897 if (mBound) {
1898 disconnectService();
1899 }
1900 } else {
1901 // There are more pending requests in queue.
1902 // Just post MCS_BOUND message to trigger processing
1903 // of next pending install.
1904 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1905 }
1906 break;
1907 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001908 case OBB_FLUSH_MOUNT_STATE: {
1909 final String path = (String) msg.obj;
1910
1911 if (DEBUG_OBB)
1912 Slog.i(TAG, "Flushing all OBB state for path " + path);
1913
1914 synchronized (mObbMounts) {
1915 final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>();
1916
1917 final Iterator<Entry<String, ObbState>> i =
1918 mObbPathToStateMap.entrySet().iterator();
1919 while (i.hasNext()) {
1920 final Entry<String, ObbState> obbEntry = i.next();
1921
1922 /*
1923 * If this entry's source file is in the volume path
1924 * that got unmounted, remove it because it's no
1925 * longer valid.
1926 */
1927 if (obbEntry.getKey().startsWith(path)) {
1928 obbStatesToRemove.add(obbEntry.getValue());
1929 }
1930 }
1931
1932 for (final ObbState obbState : obbStatesToRemove) {
1933 if (DEBUG_OBB)
1934 Slog.i(TAG, "Removing state for " + obbState.filename);
1935
1936 removeObbStateLocked(obbState);
1937
1938 try {
1939 obbState.token.onObbResult(obbState.filename, obbState.nonce,
1940 OnObbStateChangeListener.UNMOUNTED);
1941 } catch (RemoteException e) {
1942 Slog.i(TAG, "Couldn't send unmount notification for OBB: "
1943 + obbState.filename);
1944 }
1945 }
1946 }
1947 break;
1948 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001949 }
1950 }
1951
1952 private boolean connectToService() {
1953 if (DEBUG_OBB)
1954 Slog.i(TAG, "Trying to bind to DefaultContainerService");
1955
1956 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
1957 if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) {
1958 mBound = true;
1959 return true;
1960 }
1961 return false;
1962 }
1963
1964 private void disconnectService() {
1965 mContainerService = null;
1966 mBound = false;
1967 mContext.unbindService(mDefContainerConn);
1968 }
1969 }
1970
1971 abstract class ObbAction {
1972 private static final int MAX_RETRIES = 3;
1973 private int mRetries;
1974
1975 ObbState mObbState;
1976
1977 ObbAction(ObbState obbState) {
1978 mObbState = obbState;
1979 }
1980
1981 public void execute(ObbActionHandler handler) {
1982 try {
1983 if (DEBUG_OBB)
1984 Slog.i(TAG, "Starting to execute action: " + this.toString());
1985 mRetries++;
1986 if (mRetries > MAX_RETRIES) {
1987 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
Kenny Root480afe72010-10-07 10:17:50 -07001988 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001989 handleError();
1990 return;
1991 } else {
1992 handleExecute();
1993 if (DEBUG_OBB)
1994 Slog.i(TAG, "Posting install MCS_UNBIND");
1995 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1996 }
1997 } catch (RemoteException e) {
1998 if (DEBUG_OBB)
1999 Slog.i(TAG, "Posting install MCS_RECONNECT");
2000 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
2001 } catch (Exception e) {
2002 if (DEBUG_OBB)
2003 Slog.d(TAG, "Error handling OBB action", e);
2004 handleError();
Kenny Root17eb6fb2010-10-06 15:02:52 -07002005 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07002006 }
2007 }
2008
Kenny Root05105f72010-09-22 17:29:43 -07002009 abstract void handleExecute() throws RemoteException, IOException;
Kenny Roota02b8b02010-08-05 16:14:17 -07002010 abstract void handleError();
Kenny Root38cf8862010-09-26 14:18:51 -07002011
2012 protected ObbInfo getObbInfo() throws IOException {
2013 ObbInfo obbInfo;
2014 try {
2015 obbInfo = mContainerService.getObbInfo(mObbState.filename);
2016 } catch (RemoteException e) {
2017 Slog.d(TAG, "Couldn't call DefaultContainerService to fetch OBB info for "
2018 + mObbState.filename);
2019 obbInfo = null;
2020 }
2021 if (obbInfo == null) {
2022 throw new IOException("Couldn't read OBB file: " + mObbState.filename);
2023 }
2024 return obbInfo;
2025 }
2026
Kenny Rootaf9d6672010-10-08 09:21:39 -07002027 protected void sendNewStatusOrIgnore(int status) {
2028 if (mObbState == null || mObbState.token == null) {
2029 return;
2030 }
2031
Kenny Root38cf8862010-09-26 14:18:51 -07002032 try {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002033 mObbState.token.onObbResult(mObbState.filename, mObbState.nonce, status);
Kenny Root38cf8862010-09-26 14:18:51 -07002034 } catch (RemoteException e) {
2035 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
2036 }
2037 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002038 }
2039
2040 class MountObbAction extends ObbAction {
2041 private String mKey;
2042
2043 MountObbAction(ObbState obbState, String key) {
2044 super(obbState);
2045 mKey = key;
2046 }
2047
Jason parks5af0b912010-11-29 09:05:25 -06002048 @Override
Kenny Root735de3b2010-09-30 14:11:39 -07002049 public void handleExecute() throws IOException, RemoteException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002050 waitForReady();
2051 warnOnNotMounted();
2052
Kenny Root38cf8862010-09-26 14:18:51 -07002053 final ObbInfo obbInfo = getObbInfo();
2054
Kenny Roota02b8b02010-08-05 16:14:17 -07002055 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002056 Slog.w(TAG, "Denied attempt to mount OBB " + obbInfo.filename
2057 + " which is owned by " + obbInfo.packageName);
2058 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2059 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07002060 }
2061
Kenny Rootaf9d6672010-10-08 09:21:39 -07002062 final boolean isMounted;
2063 synchronized (mObbMounts) {
2064 isMounted = mObbPathToStateMap.containsKey(obbInfo.filename);
2065 }
2066 if (isMounted) {
2067 Slog.w(TAG, "Attempt to mount OBB which is already mounted: " + obbInfo.filename);
2068 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_ALREADY_MOUNTED);
2069 return;
2070 }
2071
2072 /*
2073 * The filename passed in might not be the canonical name, so just
2074 * set the filename to the canonicalized version.
2075 */
2076 mObbState.filename = obbInfo.filename;
2077
2078 final String hashedKey;
2079 if (mKey == null) {
2080 hashedKey = "none";
2081 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002082 try {
Kenny Root3b1abba2010-10-13 15:00:07 -07002083 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
2084
2085 KeySpec ks = new PBEKeySpec(mKey.toCharArray(), obbInfo.salt,
2086 PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE);
2087 SecretKey key = factory.generateSecret(ks);
2088 BigInteger bi = new BigInteger(key.getEncoded());
2089 hashedKey = bi.toString(16);
Kenny Rootaf9d6672010-10-08 09:21:39 -07002090 } catch (NoSuchAlgorithmException e) {
Kenny Root3b1abba2010-10-13 15:00:07 -07002091 Slog.e(TAG, "Could not load PBKDF2 algorithm", e);
2092 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
2093 return;
2094 } catch (InvalidKeySpecException e) {
2095 Slog.e(TAG, "Invalid key spec when loading PBKDF2 algorithm", e);
2096 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root38cf8862010-09-26 14:18:51 -07002097 return;
2098 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002099 }
Kenny Root38cf8862010-09-26 14:18:51 -07002100
Kenny Rootaf9d6672010-10-08 09:21:39 -07002101 int rc = StorageResultCode.OperationSucceeded;
2102 String cmd = String.format("obb mount %s %s %d", mObbState.filename, hashedKey,
2103 mObbState.callerUid);
2104 try {
2105 mConnector.doCommand(cmd);
2106 } catch (NativeDaemonConnectorException e) {
2107 int code = e.getCode();
2108 if (code != VoldResponseCode.OpFailedStorageBusy) {
2109 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002110 }
2111 }
2112
Kenny Rootaf9d6672010-10-08 09:21:39 -07002113 if (rc == StorageResultCode.OperationSucceeded) {
2114 if (DEBUG_OBB)
2115 Slog.d(TAG, "Successfully mounted OBB " + mObbState.filename);
2116
2117 synchronized (mObbMounts) {
2118 addObbStateLocked(mObbState);
2119 }
2120
2121 sendNewStatusOrIgnore(OnObbStateChangeListener.MOUNTED);
Kenny Root02c87302010-07-01 08:10:18 -07002122 } else {
Kenny Root05105f72010-09-22 17:29:43 -07002123 Slog.e(TAG, "Couldn't mount OBB file: " + rc);
Kenny Roota02b8b02010-08-05 16:14:17 -07002124
Kenny Rootaf9d6672010-10-08 09:21:39 -07002125 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT);
Kenny Root02c87302010-07-01 08:10:18 -07002126 }
2127 }
2128
Jason parks5af0b912010-11-29 09:05:25 -06002129 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002130 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002131 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root02c87302010-07-01 08:10:18 -07002132 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002133
2134 @Override
2135 public String toString() {
2136 StringBuilder sb = new StringBuilder();
2137 sb.append("MountObbAction{");
2138 sb.append("filename=");
2139 sb.append(mObbState.filename);
2140 sb.append(",callerUid=");
2141 sb.append(mObbState.callerUid);
2142 sb.append(",token=");
2143 sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002144 sb.append(",binder=");
2145 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002146 sb.append('}');
2147 return sb.toString();
2148 }
2149 }
2150
2151 class UnmountObbAction extends ObbAction {
2152 private boolean mForceUnmount;
2153
2154 UnmountObbAction(ObbState obbState, boolean force) {
2155 super(obbState);
2156 mForceUnmount = force;
2157 }
2158
Jason parks5af0b912010-11-29 09:05:25 -06002159 @Override
Kenny Root38cf8862010-09-26 14:18:51 -07002160 public void handleExecute() throws IOException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002161 waitForReady();
2162 warnOnNotMounted();
2163
Kenny Root38cf8862010-09-26 14:18:51 -07002164 final ObbInfo obbInfo = getObbInfo();
Kenny Roota02b8b02010-08-05 16:14:17 -07002165
Kenny Rootaf9d6672010-10-08 09:21:39 -07002166 final ObbState obbState;
Kenny Root38cf8862010-09-26 14:18:51 -07002167 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002168 obbState = mObbPathToStateMap.get(obbInfo.filename);
2169 }
Kenny Root38cf8862010-09-26 14:18:51 -07002170
Kenny Rootaf9d6672010-10-08 09:21:39 -07002171 if (obbState == null) {
2172 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_NOT_MOUNTED);
2173 return;
2174 }
2175
2176 if (obbState.callerUid != mObbState.callerUid) {
2177 Slog.w(TAG, "Permission denied attempting to unmount OBB " + obbInfo.filename
2178 + " (owned by " + obbInfo.packageName + ")");
2179 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2180 return;
2181 }
2182
2183 mObbState.filename = obbInfo.filename;
2184
2185 int rc = StorageResultCode.OperationSucceeded;
2186 String cmd = String.format("obb unmount %s%s", mObbState.filename,
2187 (mForceUnmount ? " force" : ""));
2188 try {
2189 mConnector.doCommand(cmd);
2190 } catch (NativeDaemonConnectorException e) {
2191 int code = e.getCode();
2192 if (code == VoldResponseCode.OpFailedStorageBusy) {
2193 rc = StorageResultCode.OperationFailedStorageBusy;
2194 } else if (code == VoldResponseCode.OpFailedStorageNotFound) {
2195 // If it's not mounted then we've already won.
2196 rc = StorageResultCode.OperationSucceeded;
2197 } else {
2198 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002199 }
2200 }
2201
Kenny Rootaf9d6672010-10-08 09:21:39 -07002202 if (rc == StorageResultCode.OperationSucceeded) {
2203 synchronized (mObbMounts) {
2204 removeObbStateLocked(obbState);
Kenny Root38cf8862010-09-26 14:18:51 -07002205 }
2206
Kenny Rootaf9d6672010-10-08 09:21:39 -07002207 sendNewStatusOrIgnore(OnObbStateChangeListener.UNMOUNTED);
Kenny Roota02b8b02010-08-05 16:14:17 -07002208 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002209 Slog.w(TAG, "Could not mount OBB: " + mObbState.filename);
2210 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT);
Kenny Roota02b8b02010-08-05 16:14:17 -07002211 }
2212 }
2213
Jason parks5af0b912010-11-29 09:05:25 -06002214 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002215 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002216 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Roota02b8b02010-08-05 16:14:17 -07002217 }
2218
2219 @Override
2220 public String toString() {
2221 StringBuilder sb = new StringBuilder();
2222 sb.append("UnmountObbAction{");
2223 sb.append("filename=");
2224 sb.append(mObbState.filename != null ? mObbState.filename : "null");
2225 sb.append(",force=");
2226 sb.append(mForceUnmount);
2227 sb.append(",callerUid=");
2228 sb.append(mObbState.callerUid);
2229 sb.append(",token=");
2230 sb.append(mObbState.token != null ? mObbState.token.toString() : "null");
Kenny Root735de3b2010-09-30 14:11:39 -07002231 sb.append(",binder=");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002232 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002233 sb.append('}');
2234 return sb.toString();
2235 }
Kenny Root02c87302010-07-01 08:10:18 -07002236 }
Kenny Root38cf8862010-09-26 14:18:51 -07002237
2238 @Override
2239 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2240 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
2241 pw.println("Permission Denial: can't dump ActivityManager from from pid="
2242 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
2243 + " without permission " + android.Manifest.permission.DUMP);
2244 return;
2245 }
2246
Kenny Root38cf8862010-09-26 14:18:51 -07002247 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002248 pw.println(" mObbMounts:");
Kenny Root38cf8862010-09-26 14:18:51 -07002249
Kenny Rootaf9d6672010-10-08 09:21:39 -07002250 final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet().iterator();
2251 while (binders.hasNext()) {
2252 Entry<IBinder, List<ObbState>> e = binders.next();
2253 pw.print(" Key="); pw.println(e.getKey().toString());
2254 final List<ObbState> obbStates = e.getValue();
Kenny Root38cf8862010-09-26 14:18:51 -07002255 for (final ObbState obbState : obbStates) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002256 pw.print(" "); pw.println(obbState.toString());
Kenny Root38cf8862010-09-26 14:18:51 -07002257 }
2258 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002259
2260 pw.println("");
2261 pw.println(" mObbPathToStateMap:");
2262 final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator();
2263 while (maps.hasNext()) {
2264 final Entry<String, ObbState> e = maps.next();
2265 pw.print(" "); pw.print(e.getKey());
2266 pw.print(" -> "); pw.println(e.getValue().toString());
2267 }
Kenny Root38cf8862010-09-26 14:18:51 -07002268 }
2269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270}
2271