blob: 461a21ed11c39742dc916cb4fd46b7e7c657b026 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
Kenny Root3b1abba2010-10-13 15:00:07 -070068import javax.crypto.SecretKey;
69import javax.crypto.SecretKeyFactory;
70import javax.crypto.spec.PBEKeySpec;
71
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072/**
San Mehatb1043402010-02-05 08:26:50 -080073 * MountService implements back-end services for platform storage
74 * management.
75 * @hide - Applications should use android.os.storage.StorageManager
76 * to access the MountService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 */
Jason parks5af0b912010-11-29 09:05:25 -060078class MountService extends IMountService.Stub implements INativeDaemonConnectorCallbacks {
79
San Mehatb1043402010-02-05 08:26:50 -080080 private static final boolean LOCAL_LOGD = false;
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -080081 private static final boolean DEBUG_UNMOUNT = false;
82 private static final boolean DEBUG_EVENTS = false;
Kenny Rootb7db2722011-01-25 16:39:35 -080083 private static final boolean DEBUG_OBB = false;
Kenny Root02c87302010-07-01 08:10:18 -070084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 private static final String TAG = "MountService";
86
Kenny Root305bcbf2010-09-03 07:56:38 -070087 private static final String VOLD_TAG = "VoldConnector";
88
San Mehat4270e1e2010-01-29 05:32:19 -080089 /*
90 * Internal vold volume state constants
91 */
San Mehat7fd0fee2009-12-17 07:12:23 -080092 class VolumeState {
93 public static final int Init = -1;
94 public static final int NoMedia = 0;
95 public static final int Idle = 1;
96 public static final int Pending = 2;
97 public static final int Checking = 3;
98 public static final int Mounted = 4;
99 public static final int Unmounting = 5;
100 public static final int Formatting = 6;
101 public static final int Shared = 7;
102 public static final int SharedMnt = 8;
103 }
104
San Mehat4270e1e2010-01-29 05:32:19 -0800105 /*
106 * Internal vold response code constants
107 */
San Mehat22dd86e2010-01-12 12:21:18 -0800108 class VoldResponseCode {
San Mehat4270e1e2010-01-29 05:32:19 -0800109 /*
110 * 100 series - Requestion action was initiated; expect another reply
111 * before proceeding with a new command.
112 */
San Mehat22dd86e2010-01-12 12:21:18 -0800113 public static final int VolumeListResult = 110;
114 public static final int AsecListResult = 111;
San Mehatc1b4ce92010-02-16 17:13:03 -0800115 public static final int StorageUsersListResult = 112;
San Mehat22dd86e2010-01-12 12:21:18 -0800116
San Mehat4270e1e2010-01-29 05:32:19 -0800117 /*
118 * 200 series - Requestion action has been successfully completed.
119 */
120 public static final int ShareStatusResult = 210;
San Mehat22dd86e2010-01-12 12:21:18 -0800121 public static final int AsecPathResult = 211;
San Mehat4270e1e2010-01-29 05:32:19 -0800122 public static final int ShareEnabledResult = 212;
San Mehat22dd86e2010-01-12 12:21:18 -0800123
San Mehat4270e1e2010-01-29 05:32:19 -0800124 /*
125 * 400 series - Command was accepted, but the requested action
126 * did not take place.
127 */
128 public static final int OpFailedNoMedia = 401;
129 public static final int OpFailedMediaBlank = 402;
130 public static final int OpFailedMediaCorrupt = 403;
131 public static final int OpFailedVolNotMounted = 404;
San Mehatd9709982010-02-18 11:43:03 -0800132 public static final int OpFailedStorageBusy = 405;
San Mehat2d66cef2010-03-23 11:12:52 -0700133 public static final int OpFailedStorageNotFound = 406;
San Mehat4270e1e2010-01-29 05:32:19 -0800134
135 /*
136 * 600 series - Unsolicited broadcasts.
137 */
San Mehat22dd86e2010-01-12 12:21:18 -0800138 public static final int VolumeStateChange = 605;
San Mehat22dd86e2010-01-12 12:21:18 -0800139 public static final int ShareAvailabilityChange = 620;
140 public static final int VolumeDiskInserted = 630;
141 public static final int VolumeDiskRemoved = 631;
142 public static final int VolumeBadRemoval = 632;
143 }
144
San Mehat4270e1e2010-01-29 05:32:19 -0800145 private Context mContext;
146 private NativeDaemonConnector mConnector;
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400147 private final HashMap<String, String> mVolumeStates = new HashMap<String, String>();
148 private String mExternalStoragePath;
San Mehat4270e1e2010-01-29 05:32:19 -0800149 private PackageManagerService mPms;
150 private boolean mUmsEnabling;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800151 // Used as a lock for methods that register/unregister listeners.
152 final private ArrayList<MountServiceBinderListener> mListeners =
153 new ArrayList<MountServiceBinderListener>();
San Mehat6a965af22010-02-24 17:47:30 -0800154 private boolean mBooted = false;
155 private boolean mReady = false;
156 private boolean mSendUmsConnectedOnBoot = false;
Mike Lockwood03559752010-07-19 18:25:03 -0400157 // true if we should fake MEDIA_MOUNTED state for external storage
158 private boolean mEmulateExternalStorage = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800159
San Mehat6cdd9c02010-02-09 14:45:20 -0800160 /**
161 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800162 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800163 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800164 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800165
Kenny Root02c87302010-07-01 08:10:18 -0700166 /**
Kenny Root3b1abba2010-10-13 15:00:07 -0700167 * The size of the crypto algorithm key in bits for OBB files. Currently
168 * Twofish is used which takes 128-bit keys.
169 */
170 private static final int CRYPTO_ALGORITHM_KEY_SIZE = 128;
171
172 /**
173 * The number of times to run SHA1 in the PBKDF2 function for OBB files.
174 * 1024 is reasonably secure and not too slow.
175 */
176 private static final int PBKDF2_HASH_ROUNDS = 1024;
177
178 /**
Kenny Roota02b8b02010-08-05 16:14:17 -0700179 * Mounted OBB tracking information. Used to track the current state of all
180 * OBBs.
Kenny Root02c87302010-07-01 08:10:18 -0700181 */
Kenny Root735de3b2010-09-30 14:11:39 -0700182 final private Map<IBinder, List<ObbState>> mObbMounts = new HashMap<IBinder, List<ObbState>>();
Kenny Roota02b8b02010-08-05 16:14:17 -0700183 final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>();
184
185 class ObbState implements IBinder.DeathRecipient {
Kenny Rootaf9d6672010-10-08 09:21:39 -0700186 public ObbState(String filename, int callerUid, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -0700187 throws RemoteException {
Kenny Roota02b8b02010-08-05 16:14:17 -0700188 this.filename = filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700189 this.callerUid = callerUid;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700190 this.token = token;
191 this.nonce = nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700192 }
193
194 // OBB source filename
Kenny Rootaf9d6672010-10-08 09:21:39 -0700195 String filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700196
197 // Binder.callingUid()
Kenny Root05105f72010-09-22 17:29:43 -0700198 final public int callerUid;
Kenny Roota02b8b02010-08-05 16:14:17 -0700199
Kenny Rootaf9d6672010-10-08 09:21:39 -0700200 // Token of remote Binder caller
201 final IObbActionListener token;
202
203 // Identifier to pass back to the token
204 final int nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700205
Kenny Root735de3b2010-09-30 14:11:39 -0700206 public IBinder getBinder() {
207 return token.asBinder();
208 }
209
Kenny Roota02b8b02010-08-05 16:14:17 -0700210 @Override
211 public void binderDied() {
212 ObbAction action = new UnmountObbAction(this, true);
213 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root735de3b2010-09-30 14:11:39 -0700214 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700215
Kenny Root5919ac62010-10-05 09:49:40 -0700216 public void link() throws RemoteException {
217 getBinder().linkToDeath(this, 0);
218 }
219
220 public void unlink() {
Kenny Root735de3b2010-09-30 14:11:39 -0700221 getBinder().unlinkToDeath(this, 0);
Kenny Roota02b8b02010-08-05 16:14:17 -0700222 }
Kenny Root38cf8862010-09-26 14:18:51 -0700223
224 @Override
225 public String toString() {
226 StringBuilder sb = new StringBuilder("ObbState{");
227 sb.append("filename=");
228 sb.append(filename);
229 sb.append(",token=");
230 sb.append(token.toString());
231 sb.append(",callerUid=");
232 sb.append(callerUid);
Kenny Root38cf8862010-09-26 14:18:51 -0700233 sb.append('}');
234 return sb.toString();
235 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700236 }
237
238 // OBB Action Handler
239 final private ObbActionHandler mObbActionHandler;
240
241 // OBB action handler messages
242 private static final int OBB_RUN_ACTION = 1;
243 private static final int OBB_MCS_BOUND = 2;
244 private static final int OBB_MCS_UNBIND = 3;
245 private static final int OBB_MCS_RECONNECT = 4;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700246 private static final int OBB_FLUSH_MOUNT_STATE = 5;
Kenny Roota02b8b02010-08-05 16:14:17 -0700247
248 /*
249 * Default Container Service information
250 */
251 static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
252 "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService");
253
254 final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection();
255
256 class DefaultContainerConnection implements ServiceConnection {
257 public void onServiceConnected(ComponentName name, IBinder service) {
258 if (DEBUG_OBB)
259 Slog.i(TAG, "onServiceConnected");
260 IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service);
261 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs));
262 }
263
264 public void onServiceDisconnected(ComponentName name) {
265 if (DEBUG_OBB)
266 Slog.i(TAG, "onServiceDisconnected");
267 }
268 };
269
270 // Used in the ObbActionHandler
271 private IMediaContainerService mContainerService = null;
Kenny Root02c87302010-07-01 08:10:18 -0700272
273 // Handler messages
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800274 private static final int H_UNMOUNT_PM_UPDATE = 1;
275 private static final int H_UNMOUNT_PM_DONE = 2;
276 private static final int H_UNMOUNT_MS = 3;
277 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
278 private static final int MAX_UNMOUNT_RETRIES = 4;
279
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800280 class UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700281 final String path;
282 final boolean force;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800283 int retries;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800284
285 UnmountCallBack(String path, boolean force) {
286 retries = 0;
287 this.path = path;
288 this.force = force;
289 }
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800290
291 void handleFinished() {
San Mehata5078592010-03-25 09:36:54 -0700292 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path);
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800293 doUnmountVolume(path, true);
294 }
295 }
296
297 class UmsEnableCallBack extends UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700298 final String method;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800299
300 UmsEnableCallBack(String path, String method, boolean force) {
301 super(path, force);
302 this.method = method;
303 }
304
305 @Override
306 void handleFinished() {
307 super.handleFinished();
308 doShareUnshareVolume(path, method, true);
309 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800310 }
311
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800312 class ShutdownCallBack extends UnmountCallBack {
313 IMountShutdownObserver observer;
314 ShutdownCallBack(String path, IMountShutdownObserver observer) {
315 super(path, true);
316 this.observer = observer;
317 }
318
319 @Override
320 void handleFinished() {
321 int ret = doUnmountVolume(path, true);
322 if (observer != null) {
323 try {
324 observer.onShutDownComplete(ret);
325 } catch (RemoteException e) {
San Mehata5078592010-03-25 09:36:54 -0700326 Slog.w(TAG, "RemoteException when shutting down");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800327 }
328 }
329 }
330 }
331
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400332 class MountServiceHandler extends Handler {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800333 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700334 boolean mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800335
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400336 MountServiceHandler(Looper l) {
337 super(l);
338 }
339
Jason parks5af0b912010-11-29 09:05:25 -0600340 @Override
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800341 public void handleMessage(Message msg) {
342 switch (msg.what) {
343 case H_UNMOUNT_PM_UPDATE: {
San Mehata5078592010-03-25 09:36:54 -0700344 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800345 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
346 mForceUnmounts.add(ucb);
San Mehata5078592010-03-25 09:36:54 -0700347 if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800348 // Register only if needed.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700349 if (!mUpdatingStatus) {
San Mehata5078592010-03-25 09:36:54 -0700350 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700351 mUpdatingStatus = true;
352 mPms.updateExternalMediaStatus(false, true);
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800353 }
354 break;
355 }
356 case H_UNMOUNT_PM_DONE: {
San Mehata5078592010-03-25 09:36:54 -0700357 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE");
San Mehata5078592010-03-25 09:36:54 -0700358 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700359 mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800360 int size = mForceUnmounts.size();
361 int sizeArr[] = new int[size];
362 int sizeArrN = 0;
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700363 // Kill processes holding references first
364 ActivityManagerService ams = (ActivityManagerService)
365 ServiceManager.getService("activity");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800366 for (int i = 0; i < size; i++) {
367 UnmountCallBack ucb = mForceUnmounts.get(i);
368 String path = ucb.path;
369 boolean done = false;
370 if (!ucb.force) {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800371 done = true;
372 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800373 int pids[] = getStorageUsers(path);
374 if (pids == null || pids.length == 0) {
375 done = true;
376 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800377 // Eliminate system process here?
Dianne Hackborn64825172011-03-02 21:32:58 -0800378 ams.killPids(pids, "unmount media", true);
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700379 // Confirm if file references have been freed.
380 pids = getStorageUsers(path);
381 if (pids == null || pids.length == 0) {
382 done = true;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800383 }
384 }
385 }
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700386 if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) {
387 // Retry again
388 Slog.i(TAG, "Retrying to kill storage users again");
389 mHandler.sendMessageDelayed(
390 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
391 ucb.retries++),
392 RETRY_UNMOUNT_DELAY);
393 } else {
394 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
395 Slog.i(TAG, "Failed to unmount media inspite of " +
396 MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now");
397 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800398 sizeArr[sizeArrN++] = i;
399 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
400 ucb));
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800401 }
402 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800403 // Remove already processed elements from list.
404 for (int i = (sizeArrN-1); i >= 0; i--) {
405 mForceUnmounts.remove(sizeArr[i]);
406 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800407 break;
408 }
409 case H_UNMOUNT_MS : {
San Mehata5078592010-03-25 09:36:54 -0700410 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800411 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800412 ucb.handleFinished();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800413 break;
414 }
415 }
416 }
417 };
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400418 final private HandlerThread mHandlerThread;
419 final private Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800420
San Mehat207e5382010-02-04 20:46:54 -0800421 private void waitForReady() {
422 while (mReady == false) {
423 for (int retries = 5; retries > 0; retries--) {
424 if (mReady) {
425 return;
426 }
427 SystemClock.sleep(1000);
428 }
San Mehata5078592010-03-25 09:36:54 -0700429 Slog.w(TAG, "Waiting too long for mReady!");
San Mehat207e5382010-02-04 20:46:54 -0800430 }
San Mehat1f6301e2010-01-07 22:40:27 -0800431 }
Kenny Root02c87302010-07-01 08:10:18 -0700432
San Mehat207e5382010-02-04 20:46:54 -0800433 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Jason parks5af0b912010-11-29 09:05:25 -0600434 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 public void onReceive(Context context, Intent intent) {
San Mehat91c77612010-01-07 10:39:41 -0800436 String action = intent.getAction();
437
438 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
San Mehat207e5382010-02-04 20:46:54 -0800439 mBooted = true;
San Mehat22dd86e2010-01-12 12:21:18 -0800440
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800441 /*
442 * In the simulator, we need to broadcast a volume mounted event
443 * to make the media scanner run.
444 */
445 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
446 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted);
447 return;
448 }
San Mehatfafb0412010-02-18 19:40:04 -0800449 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600450 @Override
San Mehatfafb0412010-02-18 19:40:04 -0800451 public void run() {
452 try {
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400453 synchronized (mVolumeStates) {
454 for (String path : mVolumeStates.keySet()) {
455 String state = mVolumeStates.get(path);
San Mehat6a254402010-03-22 10:21:00 -0700456
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400457 if (state.equals(Environment.MEDIA_UNMOUNTED)) {
458 int rc = doMountVolume(path);
459 if (rc != StorageResultCode.OperationSucceeded) {
460 Slog.e(TAG, String.format("Boot-time mount failed (%d)",
461 rc));
462 }
463 } else if (state.equals(Environment.MEDIA_SHARED)) {
464 /*
465 * Bootstrap UMS enabled state since vold indicates
466 * the volume is shared (runtime restart while ums enabled)
467 */
468 notifyVolumeStateChange(null, path, VolumeState.NoMedia,
469 VolumeState.Shared);
470 }
San Mehatfafb0412010-02-18 19:40:04 -0800471 }
472 }
San Mehat6a254402010-03-22 10:21:00 -0700473
San Mehat6a965af22010-02-24 17:47:30 -0800474 /*
San Mehat6a254402010-03-22 10:21:00 -0700475 * If UMS was connected on boot, send the connected event
San Mehat6a965af22010-02-24 17:47:30 -0800476 * now that we're up.
477 */
478 if (mSendUmsConnectedOnBoot) {
479 sendUmsIntent(true);
480 mSendUmsConnectedOnBoot = false;
481 }
San Mehatfafb0412010-02-18 19:40:04 -0800482 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700483 Slog.e(TAG, "Boot-time mount exception", ex);
San Mehatfafb0412010-02-18 19:40:04 -0800484 }
San Mehat207e5382010-02-04 20:46:54 -0800485 }
San Mehatfafb0412010-02-18 19:40:04 -0800486 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 }
488 }
489 };
490
San Mehat4270e1e2010-01-29 05:32:19 -0800491 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
492 final IMountServiceListener mListener;
493
494 MountServiceBinderListener(IMountServiceListener listener) {
495 mListener = listener;
Kenny Root02c87302010-07-01 08:10:18 -0700496
San Mehat91c77612010-01-07 10:39:41 -0800497 }
498
San Mehat4270e1e2010-01-29 05:32:19 -0800499 public void binderDied() {
San Mehata5078592010-03-25 09:36:54 -0700500 if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!");
Kenny Roota02b8b02010-08-05 16:14:17 -0700501 synchronized (mListeners) {
San Mehat4270e1e2010-01-29 05:32:19 -0800502 mListeners.remove(this);
503 mListener.asBinder().unlinkToDeath(this, 0);
504 }
505 }
506 }
507
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800508 private void doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800509 // TODO: Add support for multiple share methods
510 if (!method.equals("ums")) {
511 throw new IllegalArgumentException(String.format("Method %s not supported", method));
512 }
513
San Mehat4270e1e2010-01-29 05:32:19 -0800514 try {
515 mConnector.doCommand(String.format(
516 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
517 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -0700518 Slog.e(TAG, "Failed to share/unshare", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800519 }
San Mehat4270e1e2010-01-29 05:32:19 -0800520 }
521
San Mehat207e5382010-02-04 20:46:54 -0800522 private void updatePublicVolumeState(String path, String state) {
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400523 String oldState;
524 synchronized(mVolumeStates) {
525 oldState = mVolumeStates.put(path, state);
526 }
527 if (state.equals(oldState)) {
528 Slog.w(TAG, String.format("Duplicate state transition (%s -> %s) for %s",
529 state, state, path));
San Mehat4270e1e2010-01-29 05:32:19 -0800530 return;
531 }
San Mehatb1043402010-02-05 08:26:50 -0800532
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400533 Slog.d(TAG, "volume state changed for " + path + " (" + oldState + " -> " + state + ")");
Kenny Rootaf9d6672010-10-08 09:21:39 -0700534
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400535 if (path.equals(mExternalStoragePath)) {
536 // Update state on PackageManager, but only of real events
537 if (!mEmulateExternalStorage) {
538 if (Environment.MEDIA_UNMOUNTED.equals(state)) {
539 mPms.updateExternalMediaStatus(false, false);
540
541 /*
542 * Some OBBs might have been unmounted when this volume was
543 * unmounted, so send a message to the handler to let it know to
544 * remove those from the list of mounted OBBS.
545 */
546 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(
547 OBB_FLUSH_MOUNT_STATE, path));
548 } else if (Environment.MEDIA_MOUNTED.equals(state)) {
549 mPms.updateExternalMediaStatus(true, false);
550 }
Mike Lockwood03559752010-07-19 18:25:03 -0400551 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800552 }
San Mehat4270e1e2010-01-29 05:32:19 -0800553 synchronized (mListeners) {
554 for (int i = mListeners.size() -1; i >= 0; i--) {
555 MountServiceBinderListener bl = mListeners.get(i);
556 try {
San Mehatb1043402010-02-05 08:26:50 -0800557 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800558 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700559 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800560 mListeners.remove(i);
561 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700562 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800563 }
564 }
565 }
566 }
567
568 /**
569 *
570 * Callback from NativeDaemonConnector
571 */
572 public void onDaemonConnected() {
573 /*
574 * Since we'll be calling back into the NativeDaemonConnector,
575 * we need to do our work in a new thread.
576 */
577 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600578 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800579 public void run() {
580 /**
581 * Determine media state and UMS detection status
582 */
San Mehat4270e1e2010-01-29 05:32:19 -0800583 try {
584 String[] vols = mConnector.doListCommand(
585 "volume list", VoldResponseCode.VolumeListResult);
586 for (String volstr : vols) {
587 String[] tok = volstr.split(" ");
588 // FMT: <label> <mountpoint> <state>
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400589 String path = tok[1];
590 String state = Environment.MEDIA_REMOVED;
591
San Mehat4270e1e2010-01-29 05:32:19 -0800592 int st = Integer.parseInt(tok[2]);
593 if (st == VolumeState.NoMedia) {
594 state = Environment.MEDIA_REMOVED;
595 } else if (st == VolumeState.Idle) {
San Mehat207e5382010-02-04 20:46:54 -0800596 state = Environment.MEDIA_UNMOUNTED;
San Mehat4270e1e2010-01-29 05:32:19 -0800597 } else if (st == VolumeState.Mounted) {
598 state = Environment.MEDIA_MOUNTED;
San Mehata5078592010-03-25 09:36:54 -0700599 Slog.i(TAG, "Media already mounted on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800600 } else if (st == VolumeState.Shared) {
601 state = Environment.MEDIA_SHARED;
San Mehata5078592010-03-25 09:36:54 -0700602 Slog.i(TAG, "Media shared on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800603 } else {
604 throw new Exception(String.format("Unexpected state %d", st));
605 }
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400606
607 if (state != null) {
608 if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
609 updatePublicVolumeState(path, state);
610 }
San Mehat4270e1e2010-01-29 05:32:19 -0800611 }
612 } catch (Exception e) {
San Mehata5078592010-03-25 09:36:54 -0700613 Slog.e(TAG, "Error processing initial volume state", e);
Mike Lockwoodf097fc22011-03-23 14:52:34 -0400614 updatePublicVolumeState(mExternalStoragePath, Environment.MEDIA_REMOVED);
San Mehat4270e1e2010-01-29 05:32:19 -0800615 }
616
617 try {
San Mehat207e5382010-02-04 20:46:54 -0800618 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800619 notifyShareAvailabilityChange("ums", avail);
620 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700621 Slog.w(TAG, "Failed to get share availability");
San Mehat4270e1e2010-01-29 05:32:19 -0800622 }
San Mehat207e5382010-02-04 20:46:54 -0800623 /*
Jason parks9ed98bc2011-01-17 09:58:35 -0600624 * Now that we've done our initialization, release
San Mehat207e5382010-02-04 20:46:54 -0800625 * the hounds!
626 */
627 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800628 }
629 }.start();
630 }
631
632 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800633 * Callback from NativeDaemonConnector
634 */
635 public boolean onEvent(int code, String raw, String[] cooked) {
636 Intent in = null;
637
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800638 if (DEBUG_EVENTS) {
639 StringBuilder builder = new StringBuilder();
640 builder.append("onEvent::");
641 builder.append(" raw= " + raw);
642 if (cooked != null) {
643 builder.append(" cooked = " );
644 for (String str : cooked) {
645 builder.append(" " + str);
646 }
647 }
San Mehata5078592010-03-25 09:36:54 -0700648 Slog.i(TAG, builder.toString());
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800649 }
San Mehat4270e1e2010-01-29 05:32:19 -0800650 if (code == VoldResponseCode.VolumeStateChange) {
651 /*
652 * One of the volumes we're managing has changed state.
653 * Format: "NNN Volume <label> <path> state changed
654 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
655 */
656 notifyVolumeStateChange(
657 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
658 Integer.parseInt(cooked[10]));
659 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
660 // FMT: NNN Share method <method> now <available|unavailable>
661 boolean avail = false;
662 if (cooked[5].equals("available")) {
663 avail = true;
664 }
665 notifyShareAvailabilityChange(cooked[3], avail);
666 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
667 (code == VoldResponseCode.VolumeDiskRemoved) ||
668 (code == VoldResponseCode.VolumeBadRemoval)) {
669 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
670 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
671 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
672 final String label = cooked[2];
673 final String path = cooked[3];
674 int major = -1;
675 int minor = -1;
676
677 try {
678 String devComp = cooked[6].substring(1, cooked[6].length() -1);
679 String[] devTok = devComp.split(":");
680 major = Integer.parseInt(devTok[0]);
681 minor = Integer.parseInt(devTok[1]);
682 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700683 Slog.e(TAG, "Failed to parse major/minor", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800684 }
685
San Mehat4270e1e2010-01-29 05:32:19 -0800686 if (code == VoldResponseCode.VolumeDiskInserted) {
687 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600688 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800689 public void run() {
690 try {
691 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800692 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700693 Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
San Mehat4270e1e2010-01-29 05:32:19 -0800694 }
695 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700696 Slog.w(TAG, "Failed to mount media on insertion", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800697 }
698 }
699 }.start();
700 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
701 /*
702 * This event gets trumped if we're already in BAD_REMOVAL state
703 */
704 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
705 return true;
706 }
707 /* Send the media unmounted event first */
San Mehata5078592010-03-25 09:36:54 -0700708 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800709 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
710 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
711 mContext.sendBroadcast(in);
712
San Mehata5078592010-03-25 09:36:54 -0700713 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
San Mehat4270e1e2010-01-29 05:32:19 -0800714 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
715 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
716 } else if (code == VoldResponseCode.VolumeBadRemoval) {
San Mehata5078592010-03-25 09:36:54 -0700717 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800718 /* Send the media unmounted event first */
719 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
720 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
721 mContext.sendBroadcast(in);
722
San Mehata5078592010-03-25 09:36:54 -0700723 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
San Mehat4270e1e2010-01-29 05:32:19 -0800724 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
725 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
726 } else {
San Mehata5078592010-03-25 09:36:54 -0700727 Slog.e(TAG, String.format("Unknown code {%d}", code));
San Mehat4270e1e2010-01-29 05:32:19 -0800728 }
729 } else {
730 return false;
731 }
732
733 if (in != null) {
734 mContext.sendBroadcast(in);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400735 }
736 return true;
San Mehat4270e1e2010-01-29 05:32:19 -0800737 }
738
San Mehat207e5382010-02-04 20:46:54 -0800739 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800740 String vs = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -0700741 if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
San Mehat4270e1e2010-01-29 05:32:19 -0800742
743 Intent in = null;
744
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500745 if (oldState == VolumeState.Shared && newState != oldState) {
San Mehata5078592010-03-25 09:36:54 -0700746 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500747 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
748 Uri.parse("file://" + path)));
749 }
750
San Mehat4270e1e2010-01-29 05:32:19 -0800751 if (newState == VolumeState.Init) {
752 } else if (newState == VolumeState.NoMedia) {
753 // NoMedia is handled via Disk Remove events
754 } else if (newState == VolumeState.Idle) {
755 /*
756 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
757 * if we're in the process of enabling UMS
758 */
759 if (!vs.equals(
760 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
761 Environment.MEDIA_NOFS) && !vs.equals(
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800762 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
San Mehata5078592010-03-25 09:36:54 -0700763 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
San Mehat4270e1e2010-01-29 05:32:19 -0800764 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
765 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
766 }
767 } else if (newState == VolumeState.Pending) {
768 } else if (newState == VolumeState.Checking) {
San Mehata5078592010-03-25 09:36:54 -0700769 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
San Mehat4270e1e2010-01-29 05:32:19 -0800770 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
771 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
772 } else if (newState == VolumeState.Mounted) {
San Mehata5078592010-03-25 09:36:54 -0700773 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800774 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
San Mehat4270e1e2010-01-29 05:32:19 -0800775 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
776 in.putExtra("read-only", false);
777 } else if (newState == VolumeState.Unmounting) {
San Mehat4270e1e2010-01-29 05:32:19 -0800778 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
779 } else if (newState == VolumeState.Formatting) {
780 } else if (newState == VolumeState.Shared) {
San Mehata5078592010-03-25 09:36:54 -0700781 if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800782 /* Send the media unmounted event first */
783 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
784 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
785 mContext.sendBroadcast(in);
786
San Mehata5078592010-03-25 09:36:54 -0700787 if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
San Mehat4270e1e2010-01-29 05:32:19 -0800788 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
789 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
San Mehata5078592010-03-25 09:36:54 -0700790 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
San Mehat4270e1e2010-01-29 05:32:19 -0800791 } else if (newState == VolumeState.SharedMnt) {
San Mehata5078592010-03-25 09:36:54 -0700792 Slog.e(TAG, "Live shared mounts not supported yet!");
San Mehat4270e1e2010-01-29 05:32:19 -0800793 return;
794 } else {
San Mehata5078592010-03-25 09:36:54 -0700795 Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800796 }
797
798 if (in != null) {
799 mContext.sendBroadcast(in);
800 }
801 }
802
San Mehat207e5382010-02-04 20:46:54 -0800803 private boolean doGetShareMethodAvailable(String method) {
Kenny Root85fb2062010-06-01 20:50:21 -0700804 ArrayList<String> rsp;
Kenny Roota80ce062010-06-01 13:23:53 -0700805 try {
Kenny Root85fb2062010-06-01 20:50:21 -0700806 rsp = mConnector.doCommand("share status " + method);
Kenny Roota80ce062010-06-01 13:23:53 -0700807 } catch (NativeDaemonConnectorException ex) {
808 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
809 return false;
810 }
San Mehat207e5382010-02-04 20:46:54 -0800811
812 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700813 String[] tok = line.split(" ");
814 if (tok.length < 3) {
815 Slog.e(TAG, "Malformed response to share status " + method);
816 return false;
817 }
818
San Mehat207e5382010-02-04 20:46:54 -0800819 int code;
820 try {
821 code = Integer.parseInt(tok[0]);
822 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700823 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehat207e5382010-02-04 20:46:54 -0800824 return false;
825 }
826 if (code == VoldResponseCode.ShareStatusResult) {
827 if (tok[2].equals("available"))
828 return true;
829 return false;
830 } else {
San Mehata5078592010-03-25 09:36:54 -0700831 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehat207e5382010-02-04 20:46:54 -0800832 return false;
833 }
834 }
San Mehata5078592010-03-25 09:36:54 -0700835 Slog.e(TAG, "Got an empty response");
San Mehat207e5382010-02-04 20:46:54 -0800836 return false;
837 }
838
839 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800840 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800841
San Mehata5078592010-03-25 09:36:54 -0700842 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
San Mehat207e5382010-02-04 20:46:54 -0800843 try {
844 mConnector.doCommand(String.format("volume mount %s", path));
845 } catch (NativeDaemonConnectorException e) {
846 /*
847 * Mount failed for some reason
848 */
849 Intent in = null;
850 int code = e.getCode();
851 if (code == VoldResponseCode.OpFailedNoMedia) {
852 /*
853 * Attempt to mount but no media inserted
854 */
San Mehatb1043402010-02-05 08:26:50 -0800855 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800856 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
San Mehata5078592010-03-25 09:36:54 -0700857 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
San Mehat207e5382010-02-04 20:46:54 -0800858 /*
859 * Media is blank or does not contain a supported filesystem
860 */
861 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
862 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800863 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800864 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehata5078592010-03-25 09:36:54 -0700865 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
San Mehat207e5382010-02-04 20:46:54 -0800866 /*
867 * Volume consistency check failed
868 */
869 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
870 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800871 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800872 } else {
San Mehatb1043402010-02-05 08:26:50 -0800873 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800874 }
875
876 /*
877 * Send broadcast intent (if required for the failure)
878 */
879 if (in != null) {
880 mContext.sendBroadcast(in);
881 }
882 }
883
884 return rc;
885 }
886
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800887 /*
888 * If force is not set, we do not unmount if there are
889 * processes holding references to the volume about to be unmounted.
890 * If force is set, all the processes holding references need to be
891 * killed via the ActivityManager before actually unmounting the volume.
892 * This might even take a while and might be retried after timed delays
893 * to make sure we dont end up in an instable state and kill some core
894 * processes.
895 */
San Mehatd9709982010-02-18 11:43:03 -0800896 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800897 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800898 return VoldResponseCode.OpFailedVolNotMounted;
899 }
Kenny Rootaa485402010-09-14 14:49:41 -0700900
901 /*
902 * Force a GC to make sure AssetManagers in other threads of the
903 * system_server are cleaned up. We have to do this since AssetManager
904 * instances are kept as a WeakReference and it's possible we have files
905 * open on the external storage.
906 */
907 Runtime.getRuntime().gc();
908
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800909 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700910 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800911 try {
San Mehatd9709982010-02-18 11:43:03 -0800912 mConnector.doCommand(String.format(
913 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700914 // We unmounted the volume. None of the asec containers are available now.
915 synchronized (mAsecMountSet) {
916 mAsecMountSet.clear();
917 }
San Mehatb1043402010-02-05 08:26:50 -0800918 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800919 } catch (NativeDaemonConnectorException e) {
920 // Don't worry about mismatch in PackageManager since the
921 // call back will handle the status changes any way.
922 int code = e.getCode();
923 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800924 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800925 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
926 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800927 } else {
San Mehatb1043402010-02-05 08:26:50 -0800928 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800929 }
930 }
931 }
932
933 private int doFormatVolume(String path) {
934 try {
935 String cmd = String.format("volume format %s", path);
936 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800937 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800938 } catch (NativeDaemonConnectorException e) {
939 int code = e.getCode();
940 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800941 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800942 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800943 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800944 } else {
San Mehatb1043402010-02-05 08:26:50 -0800945 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800946 }
947 }
948 }
949
San Mehatb1043402010-02-05 08:26:50 -0800950 private boolean doGetVolumeShared(String path, String method) {
951 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700952 ArrayList<String> rsp;
953
954 try {
955 rsp = mConnector.doCommand(cmd);
956 } catch (NativeDaemonConnectorException ex) {
957 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
958 return false;
959 }
San Mehatb1043402010-02-05 08:26:50 -0800960
961 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700962 String[] tok = line.split(" ");
963 if (tok.length < 3) {
964 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
965 return false;
966 }
967
San Mehatb1043402010-02-05 08:26:50 -0800968 int code;
969 try {
970 code = Integer.parseInt(tok[0]);
971 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700972 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800973 return false;
974 }
975 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700976 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800977 } else {
San Mehata5078592010-03-25 09:36:54 -0700978 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800979 return false;
980 }
981 }
San Mehata5078592010-03-25 09:36:54 -0700982 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800983 return false;
984 }
985
San Mehat207e5382010-02-04 20:46:54 -0800986 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800987 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -0700988 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800989 return;
990 }
991
992 synchronized (mListeners) {
993 for (int i = mListeners.size() -1; i >= 0; i--) {
994 MountServiceBinderListener bl = mListeners.get(i);
995 try {
San Mehatb1043402010-02-05 08:26:50 -0800996 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -0800997 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700998 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800999 mListeners.remove(i);
1000 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -07001001 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -08001002 }
1003 }
1004 }
1005
San Mehat207e5382010-02-04 20:46:54 -08001006 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -08001007 sendUmsIntent(avail);
1008 } else {
1009 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -08001010 }
San Mehat2fe718a2010-03-11 12:01:49 -08001011
1012 final String path = Environment.getExternalStorageDirectory().getPath();
1013 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
1014 /*
1015 * USB mass storage disconnected while enabled
1016 */
1017 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -06001018 @Override
San Mehat2fe718a2010-03-11 12:01:49 -08001019 public void run() {
1020 try {
1021 int rc;
San Mehata5078592010-03-25 09:36:54 -07001022 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -08001023 doShareUnshareVolume(path, "ums", false);
1024 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001025 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -08001026 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
1027 path, rc));
1028 }
1029 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -07001030 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -08001031 }
1032 }
1033 }.start();
1034 }
San Mehat4270e1e2010-01-29 05:32:19 -08001035 }
1036
San Mehat6a965af22010-02-24 17:47:30 -08001037 private void sendUmsIntent(boolean c) {
1038 mContext.sendBroadcast(
1039 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
1040 }
1041
San Mehat207e5382010-02-04 20:46:54 -08001042 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -08001043 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
1044 throw new SecurityException(String.format("Requires %s permission", perm));
1045 }
1046 }
1047
1048 /**
San Mehat207e5382010-02-04 20:46:54 -08001049 * Constructs a new MountService instance
1050 *
1051 * @param context Binder context for this service
1052 */
1053 public MountService(Context context) {
1054 mContext = context;
1055
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001056 mExternalStoragePath = Environment.getExternalStorageDirectory().getPath();
Mike Lockwood03559752010-07-19 18:25:03 -04001057 mEmulateExternalStorage = context.getResources().getBoolean(
1058 com.android.internal.R.bool.config_emulateExternalStorage);
1059 if (mEmulateExternalStorage) {
1060 Slog.d(TAG, "using emulated external storage");
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001061 mVolumeStates.put(mExternalStoragePath, Environment.MEDIA_MOUNTED);
Mike Lockwood03559752010-07-19 18:25:03 -04001062 }
1063
San Mehat207e5382010-02-04 20:46:54 -08001064 // XXX: This will go away soon in favor of IMountServiceObserver
1065 mPms = (PackageManagerService) ServiceManager.getService("package");
1066
1067 mContext.registerReceiver(mBroadcastReceiver,
1068 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
1069
Daniel Sandler5f27ef42010-03-16 15:42:02 -04001070 mHandlerThread = new HandlerThread("MountService");
1071 mHandlerThread.start();
1072 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
1073
Kenny Roota02b8b02010-08-05 16:14:17 -07001074 // Add OBB Action Handler to MountService thread.
1075 mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper());
1076
Marco Nelissenc34ebce2010-02-18 13:39:41 -08001077 /*
1078 * Vold does not run in the simulator, so pretend the connector thread
1079 * ran and did its thing.
1080 */
1081 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
1082 mReady = true;
1083 mUmsEnabling = true;
1084 return;
1085 }
1086
Kenny Root305bcbf2010-09-03 07:56:38 -07001087 /*
1088 * Create the connection to vold with a maximum queue of twice the
1089 * amount of containers we'd ever expect to have. This keeps an
1090 * "asec list" from blocking a thread repeatedly.
1091 */
1092 mConnector = new NativeDaemonConnector(this, "vold",
1093 PackageManagerService.MAX_CONTAINERS * 2, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001094 mReady = false;
Kenny Root305bcbf2010-09-03 07:56:38 -07001095 Thread thread = new Thread(mConnector, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001096 thread.start();
1097 }
1098
1099 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001100 * Exposed API calls below here
1101 */
1102
1103 public void registerListener(IMountServiceListener listener) {
1104 synchronized (mListeners) {
1105 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
1106 try {
1107 listener.asBinder().linkToDeath(bl, 0);
1108 mListeners.add(bl);
1109 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001110 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -08001111 }
1112 }
1113 }
1114
1115 public void unregisterListener(IMountServiceListener listener) {
1116 synchronized (mListeners) {
1117 for(MountServiceBinderListener bl : mListeners) {
1118 if (bl.mListener == listener) {
1119 mListeners.remove(mListeners.indexOf(bl));
1120 return;
1121 }
1122 }
1123 }
1124 }
1125
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001126 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -08001127 validatePermission(android.Manifest.permission.SHUTDOWN);
1128
San Mehata5078592010-03-25 09:36:54 -07001129 Slog.i(TAG, "Shutting down");
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001130 synchronized (mVolumeStates) {
1131 for (String path : mVolumeStates.keySet()) {
1132 String state = mVolumeStates.get(path);
San Mehat4270e1e2010-01-29 05:32:19 -08001133
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001134 if (state.equals(Environment.MEDIA_SHARED)) {
1135 /*
1136 * If the media is currently shared, unshare it.
1137 * XXX: This is still dangerous!. We should not
1138 * be rebooting at *all* if UMS is enabled, since
1139 * the UMS host could have dirty FAT cache entries
1140 * yet to flush.
1141 */
1142 setUsbMassStorageEnabled(false);
1143 } else if (state.equals(Environment.MEDIA_CHECKING)) {
1144 /*
1145 * If the media is being checked, then we need to wait for
1146 * it to complete before being able to proceed.
1147 */
1148 // XXX: @hackbod - Should we disable the ANR timer here?
1149 int retries = 30;
1150 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
1151 try {
1152 Thread.sleep(1000);
1153 } catch (InterruptedException iex) {
1154 Slog.e(TAG, "Interrupted while waiting for media", iex);
1155 break;
1156 }
1157 state = Environment.getExternalStorageState();
1158 }
1159 if (retries == 0) {
1160 Slog.e(TAG, "Timed out waiting for media to check");
1161 }
San Mehat91c77612010-01-07 10:39:41 -08001162 }
San Mehat91c77612010-01-07 10:39:41 -08001163
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001164 if (state.equals(Environment.MEDIA_MOUNTED)) {
1165 // Post a unmount message.
1166 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
1167 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
1168 } else if (observer != null) {
1169 /*
1170 * Observer is waiting for onShutDownComplete when we are done.
1171 * Since nothing will be done send notification directly so shutdown
1172 * sequence can continue.
1173 */
1174 try {
1175 observer.onShutDownComplete(StorageResultCode.OperationSucceeded);
1176 } catch (RemoteException e) {
1177 Slog.w(TAG, "RemoteException when shutting down");
1178 }
1179 }
Johan Alfven5d0db4d2010-11-09 10:32:25 +01001180 }
San Mehat4270e1e2010-01-29 05:32:19 -08001181 }
1182 }
1183
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001184 private boolean getUmsEnabling() {
1185 synchronized (mListeners) {
1186 return mUmsEnabling;
1187 }
1188 }
1189
1190 private void setUmsEnabling(boolean enable) {
1191 synchronized (mListeners) {
Tony Wufc711252010-08-09 16:49:19 +08001192 mUmsEnabling = enable;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001193 }
1194 }
1195
San Mehatb1043402010-02-05 08:26:50 -08001196 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -08001197 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -08001198
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001199 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001200 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001201 }
San Mehatb1043402010-02-05 08:26:50 -08001202 return doGetShareMethodAvailable("ums");
1203 }
1204
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001205 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001206 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001207 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001208
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001209 // TODO: Add support for multiple share methods
1210
1211 /*
1212 * If the volume is mounted and we're enabling then unmount it
1213 */
1214 String path = Environment.getExternalStorageDirectory().getPath();
1215 String vs = getVolumeState(path);
1216 String method = "ums";
1217 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1218 // Override for isUsbMassStorageEnabled()
1219 setUmsEnabling(enable);
1220 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1221 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1222 // Clear override
1223 setUmsEnabling(false);
1224 }
1225 /*
1226 * If we disabled UMS then mount the volume
1227 */
1228 if (!enable) {
1229 doShareUnshareVolume(path, method, enable);
1230 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001231 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001232 " after disabling share method " + method);
1233 /*
1234 * Even though the mount failed, the unshare didn't so don't indicate an error.
1235 * The mountVolume() call will have set the storage state and sent the necessary
1236 * broadcasts.
1237 */
1238 }
1239 }
San Mehatb1043402010-02-05 08:26:50 -08001240 }
1241
1242 public boolean isUsbMassStorageEnabled() {
1243 waitForReady();
1244 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001246
San Mehat7fd0fee2009-12-17 07:12:23 -08001247 /**
1248 * @return state of the volume at the specified mount point
1249 */
San Mehat4270e1e2010-01-29 05:32:19 -08001250 public String getVolumeState(String mountPoint) {
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001251 synchronized (mVolumeStates) {
1252 String state = mVolumeStates.get(mountPoint);
1253 if (state == null) {
1254 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
1255 throw new IllegalArgumentException();
1256 }
San Mehat7fd0fee2009-12-17 07:12:23 -08001257
Mike Lockwoodf097fc22011-03-23 14:52:34 -04001258 return state;
1259 }
San Mehat7fd0fee2009-12-17 07:12:23 -08001260 }
1261
Kenny Roote1ff2142010-10-12 11:20:01 -07001262 public boolean isExternalStorageEmulated() {
1263 return mEmulateExternalStorage;
1264 }
1265
San Mehat4270e1e2010-01-29 05:32:19 -08001266 public int mountVolume(String path) {
1267 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001268
San Mehat207e5382010-02-04 20:46:54 -08001269 waitForReady();
1270 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 }
1272
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001273 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001274 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001275 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001277 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001278 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001279 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1280 Environment.MEDIA_REMOVED.equals(volState) ||
1281 Environment.MEDIA_SHARED.equals(volState) ||
1282 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1283 // Media already unmounted or cannot be unmounted.
1284 // TODO return valid return code when adding observer call back.
1285 return;
1286 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001287 UnmountCallBack ucb = new UnmountCallBack(path, force);
1288 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 }
1290
San Mehat4270e1e2010-01-29 05:32:19 -08001291 public int formatVolume(String path) {
1292 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001293 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001294
San Mehat207e5382010-02-04 20:46:54 -08001295 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 }
1297
San Mehatc1b4ce92010-02-16 17:13:03 -08001298 public int []getStorageUsers(String path) {
1299 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1300 waitForReady();
1301 try {
1302 String[] r = mConnector.doListCommand(
1303 String.format("storage users %s", path),
1304 VoldResponseCode.StorageUsersListResult);
1305 // FMT: <pid> <process name>
1306 int[] data = new int[r.length];
1307 for (int i = 0; i < r.length; i++) {
1308 String []tok = r[i].split(" ");
1309 try {
1310 data[i] = Integer.parseInt(tok[0]);
1311 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001312 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001313 return new int[0];
1314 }
1315 }
1316 return data;
1317 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001318 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001319 return new int[0];
1320 }
1321 }
1322
San Mehatb1043402010-02-05 08:26:50 -08001323 private void warnOnNotMounted() {
1324 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001325 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001326 }
1327 }
1328
San Mehat4270e1e2010-01-29 05:32:19 -08001329 public String[] getSecureContainerList() {
1330 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001331 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001332 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001333
San Mehat4270e1e2010-01-29 05:32:19 -08001334 try {
1335 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1336 } catch (NativeDaemonConnectorException e) {
1337 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
1339 }
San Mehat36972292010-01-06 11:06:32 -08001340
San Mehat4270e1e2010-01-29 05:32:19 -08001341 public int createSecureContainer(String id, int sizeMb, String fstype,
1342 String key, int ownerUid) {
1343 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001344 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001345 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001346
San Mehatb1043402010-02-05 08:26:50 -08001347 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001348 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1349 try {
1350 mConnector.doCommand(cmd);
1351 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001352 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001353 }
San Mehata181b212010-02-11 06:50:20 -08001354
1355 if (rc == StorageResultCode.OperationSucceeded) {
1356 synchronized (mAsecMountSet) {
1357 mAsecMountSet.add(id);
1358 }
1359 }
San Mehat4270e1e2010-01-29 05:32:19 -08001360 return rc;
San Mehat36972292010-01-06 11:06:32 -08001361 }
1362
San Mehat4270e1e2010-01-29 05:32:19 -08001363 public int finalizeSecureContainer(String id) {
1364 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001365 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001366
San Mehatb1043402010-02-05 08:26:50 -08001367 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001368 try {
1369 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001370 /*
1371 * Finalization does a remount, so no need
1372 * to update mAsecMountSet
1373 */
San Mehat4270e1e2010-01-29 05:32:19 -08001374 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001375 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001376 }
San Mehat4270e1e2010-01-29 05:32:19 -08001377 return rc;
San Mehat36972292010-01-06 11:06:32 -08001378 }
1379
San Mehatd9709982010-02-18 11:43:03 -08001380 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001381 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001382 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001383 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001384
Kenny Rootaa485402010-09-14 14:49:41 -07001385 /*
1386 * Force a GC to make sure AssetManagers in other threads of the
1387 * system_server are cleaned up. We have to do this since AssetManager
1388 * instances are kept as a WeakReference and it's possible we have files
1389 * open on the external storage.
1390 */
1391 Runtime.getRuntime().gc();
1392
San Mehatb1043402010-02-05 08:26:50 -08001393 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001394 try {
San Mehatd9709982010-02-18 11:43:03 -08001395 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001396 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001397 int code = e.getCode();
1398 if (code == VoldResponseCode.OpFailedStorageBusy) {
1399 rc = StorageResultCode.OperationFailedStorageBusy;
1400 } else {
1401 rc = StorageResultCode.OperationFailedInternalError;
1402 }
San Mehat02735bc2010-01-26 15:18:08 -08001403 }
San Mehata181b212010-02-11 06:50:20 -08001404
1405 if (rc == StorageResultCode.OperationSucceeded) {
1406 synchronized (mAsecMountSet) {
1407 if (mAsecMountSet.contains(id)) {
1408 mAsecMountSet.remove(id);
1409 }
1410 }
1411 }
1412
San Mehat4270e1e2010-01-29 05:32:19 -08001413 return rc;
San Mehat36972292010-01-06 11:06:32 -08001414 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001415
San Mehat4270e1e2010-01-29 05:32:19 -08001416 public int mountSecureContainer(String id, String key, int ownerUid) {
1417 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001418 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001419 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001420
San Mehata181b212010-02-11 06:50:20 -08001421 synchronized (mAsecMountSet) {
1422 if (mAsecMountSet.contains(id)) {
1423 return StorageResultCode.OperationFailedStorageMounted;
1424 }
1425 }
1426
San Mehatb1043402010-02-05 08:26:50 -08001427 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001428 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1429 try {
1430 mConnector.doCommand(cmd);
1431 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001432 int code = e.getCode();
1433 if (code != VoldResponseCode.OpFailedStorageBusy) {
1434 rc = StorageResultCode.OperationFailedInternalError;
1435 }
San Mehat02735bc2010-01-26 15:18:08 -08001436 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001437
1438 if (rc == StorageResultCode.OperationSucceeded) {
1439 synchronized (mAsecMountSet) {
1440 mAsecMountSet.add(id);
1441 }
1442 }
San Mehat4270e1e2010-01-29 05:32:19 -08001443 return rc;
San Mehat36972292010-01-06 11:06:32 -08001444 }
1445
San Mehatd9709982010-02-18 11:43:03 -08001446 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001447 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001448 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001449 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001450
San Mehat6cdd9c02010-02-09 14:45:20 -08001451 synchronized (mAsecMountSet) {
1452 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001453 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001454 }
1455 }
1456
Kenny Rootaa485402010-09-14 14:49:41 -07001457 /*
1458 * Force a GC to make sure AssetManagers in other threads of the
1459 * system_server are cleaned up. We have to do this since AssetManager
1460 * instances are kept as a WeakReference and it's possible we have files
1461 * open on the external storage.
1462 */
1463 Runtime.getRuntime().gc();
1464
San Mehatb1043402010-02-05 08:26:50 -08001465 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001466 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001467 try {
1468 mConnector.doCommand(cmd);
1469 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001470 int code = e.getCode();
1471 if (code == VoldResponseCode.OpFailedStorageBusy) {
1472 rc = StorageResultCode.OperationFailedStorageBusy;
1473 } else {
1474 rc = StorageResultCode.OperationFailedInternalError;
1475 }
San Mehat02735bc2010-01-26 15:18:08 -08001476 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001477
1478 if (rc == StorageResultCode.OperationSucceeded) {
1479 synchronized (mAsecMountSet) {
1480 mAsecMountSet.remove(id);
1481 }
1482 }
San Mehat4270e1e2010-01-29 05:32:19 -08001483 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001484 }
1485
San Mehat6cdd9c02010-02-09 14:45:20 -08001486 public boolean isSecureContainerMounted(String id) {
1487 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1488 waitForReady();
1489 warnOnNotMounted();
1490
1491 synchronized (mAsecMountSet) {
1492 return mAsecMountSet.contains(id);
1493 }
1494 }
1495
San Mehat4270e1e2010-01-29 05:32:19 -08001496 public int renameSecureContainer(String oldId, String newId) {
1497 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001498 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001499 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001500
San Mehata181b212010-02-11 06:50:20 -08001501 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001502 /*
Jason parks9ed98bc2011-01-17 09:58:35 -06001503 * Because a mounted container has active internal state which cannot be
San Mehat85451ee2010-02-24 08:54:18 -08001504 * changed while active, we must ensure both ids are not currently mounted.
1505 */
1506 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001507 return StorageResultCode.OperationFailedStorageMounted;
1508 }
1509 }
1510
San Mehatb1043402010-02-05 08:26:50 -08001511 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001512 String cmd = String.format("asec rename %s %s", oldId, newId);
1513 try {
1514 mConnector.doCommand(cmd);
1515 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001516 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001517 }
San Mehata181b212010-02-11 06:50:20 -08001518
San Mehat4270e1e2010-01-29 05:32:19 -08001519 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001520 }
1521
San Mehat4270e1e2010-01-29 05:32:19 -08001522 public String getSecureContainerPath(String id) {
1523 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001524 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001525 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001526
San Mehat2d66cef2010-03-23 11:12:52 -07001527 try {
1528 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1529 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001530 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001531 if (code != VoldResponseCode.AsecPathResult) {
1532 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1533 }
1534 return tok[1];
1535 } catch (NativeDaemonConnectorException e) {
1536 int code = e.getCode();
1537 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1538 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001539 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001540 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001541 }
1542 }
San Mehat22dd86e2010-01-12 12:21:18 -08001543 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001544
1545 public void finishMediaUpdate() {
1546 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1547 }
Kenny Root02c87302010-07-01 08:10:18 -07001548
Kenny Roota02b8b02010-08-05 16:14:17 -07001549 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
1550 if (callerUid == android.os.Process.SYSTEM_UID) {
1551 return true;
1552 }
1553
Kenny Root02c87302010-07-01 08:10:18 -07001554 if (packageName == null) {
1555 return false;
1556 }
1557
1558 final int packageUid = mPms.getPackageUid(packageName);
1559
1560 if (DEBUG_OBB) {
1561 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
1562 packageUid + ", callerUid = " + callerUid);
1563 }
1564
1565 return callerUid == packageUid;
1566 }
1567
1568 public String getMountedObbPath(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001569 if (filename == null) {
1570 throw new IllegalArgumentException("filename cannot be null");
1571 }
1572
Kenny Root02c87302010-07-01 08:10:18 -07001573 waitForReady();
1574 warnOnNotMounted();
1575
Kenny Root02c87302010-07-01 08:10:18 -07001576 try {
1577 ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename));
1578 String []tok = rsp.get(0).split(" ");
1579 int code = Integer.parseInt(tok[0]);
1580 if (code != VoldResponseCode.AsecPathResult) {
1581 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1582 }
1583 return tok[1];
1584 } catch (NativeDaemonConnectorException e) {
1585 int code = e.getCode();
1586 if (code == VoldResponseCode.OpFailedStorageNotFound) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001587 return null;
Kenny Root02c87302010-07-01 08:10:18 -07001588 } else {
1589 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1590 }
1591 }
1592 }
1593
1594 public boolean isObbMounted(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001595 if (filename == null) {
1596 throw new IllegalArgumentException("filename cannot be null");
Kenny Root02c87302010-07-01 08:10:18 -07001597 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001598
1599 synchronized (mObbMounts) {
1600 return mObbPathToStateMap.containsKey(filename);
1601 }
Kenny Root02c87302010-07-01 08:10:18 -07001602 }
1603
Kenny Rootaf9d6672010-10-08 09:21:39 -07001604 public void mountObb(String filename, String key, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -07001605 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001606 if (filename == null) {
1607 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootaf9d6672010-10-08 09:21:39 -07001608 }
1609
1610 if (token == null) {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001611 throw new IllegalArgumentException("token cannot be null");
1612 }
1613
Kenny Rootaf9d6672010-10-08 09:21:39 -07001614 final int callerUid = Binder.getCallingUid();
1615 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1616 final ObbAction action = new MountObbAction(obbState, key);
Kenny Roota02b8b02010-08-05 16:14:17 -07001617 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
1618
1619 if (DEBUG_OBB)
1620 Slog.i(TAG, "Send to OBB handler: " + action.toString());
Kenny Root02c87302010-07-01 08:10:18 -07001621 }
1622
Kenny Rootaf9d6672010-10-08 09:21:39 -07001623 public void unmountObb(String filename, boolean force, IObbActionListener token, int nonce)
1624 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001625 if (filename == null) {
1626 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootf1121dc2010-09-29 07:30:53 -07001627 }
1628
Kenny Rootaf9d6672010-10-08 09:21:39 -07001629 final int callerUid = Binder.getCallingUid();
1630 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1631 final ObbAction action = new UnmountObbAction(obbState, force);
Kenny Roota02b8b02010-08-05 16:14:17 -07001632 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root02c87302010-07-01 08:10:18 -07001633
Kenny Roota02b8b02010-08-05 16:14:17 -07001634 if (DEBUG_OBB)
1635 Slog.i(TAG, "Send to OBB handler: " + action.toString());
1636 }
1637
Jason parks5af0b912010-11-29 09:05:25 -06001638 public int decryptStorage(String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06001639 if (TextUtils.isEmpty(password)) {
1640 throw new IllegalArgumentException("password cannot be empty");
Jason parks5af0b912010-11-29 09:05:25 -06001641 }
1642
Jason parks8888c592011-01-20 22:46:41 -06001643 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1644 "no permission to access the crypt keeper");
Jason parks5af0b912010-11-29 09:05:25 -06001645
1646 waitForReady();
1647
1648 if (DEBUG_EVENTS) {
1649 Slog.i(TAG, "decrypting storage...");
1650 }
1651
1652 try {
Jason parks9ed98bc2011-01-17 09:58:35 -06001653 ArrayList<String> rsp = mConnector.doCommand("cryptfs checkpw " + password);
Jason parksf7b3cd42011-01-27 09:28:25 -06001654 String[] tokens = rsp.get(0).split(" ");
Jason parks9ed98bc2011-01-17 09:58:35 -06001655
Jason parksf7b3cd42011-01-27 09:28:25 -06001656 if (tokens == null || tokens.length != 2) {
Jason parks9ed98bc2011-01-17 09:58:35 -06001657 return -1;
1658 }
1659
Jason parksf7b3cd42011-01-27 09:28:25 -06001660 int code = Integer.parseInt(tokens[1]);
Jason parks9ed98bc2011-01-17 09:58:35 -06001661
1662 if (code == 0) {
1663 // Decrypt was successful. Post a delayed message before restarting in order
1664 // to let the UI to clear itself
1665 mHandler.postDelayed(new Runnable() {
1666 public void run() {
1667 mConnector.doCommand(String.format("cryptfs restart"));
1668 }
Jason parksf7b3cd42011-01-27 09:28:25 -06001669 }, 1000); // 1 second
Jason parks9ed98bc2011-01-17 09:58:35 -06001670 }
1671
1672 return code;
Jason parks5af0b912010-11-29 09:05:25 -06001673 } catch (NativeDaemonConnectorException e) {
1674 // Decryption failed
1675 return e.getCode();
1676 }
Jason parks5af0b912010-11-29 09:05:25 -06001677 }
1678
Jason parks56aa5322011-01-07 09:01:15 -06001679 public int encryptStorage(String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06001680 if (TextUtils.isEmpty(password)) {
1681 throw new IllegalArgumentException("password cannot be empty");
Jason parks56aa5322011-01-07 09:01:15 -06001682 }
1683
Jason parks8888c592011-01-20 22:46:41 -06001684 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1685 "no permission to access the crypt keeper");
Jason parks56aa5322011-01-07 09:01:15 -06001686
1687 waitForReady();
1688
1689 if (DEBUG_EVENTS) {
Jason parks8888c592011-01-20 22:46:41 -06001690 Slog.i(TAG, "encrypting storage...");
Jason parks56aa5322011-01-07 09:01:15 -06001691 }
1692
1693 try {
Jason parks9ed98bc2011-01-17 09:58:35 -06001694 mConnector.doCommand(String.format("cryptfs enablecrypto inplace %s", password));
Jason parks56aa5322011-01-07 09:01:15 -06001695 } catch (NativeDaemonConnectorException e) {
1696 // Encryption failed
1697 return e.getCode();
1698 }
1699
1700 return 0;
1701 }
1702
Jason parksf7b3cd42011-01-27 09:28:25 -06001703 public int changeEncryptionPassword(String password) {
1704 if (TextUtils.isEmpty(password)) {
1705 throw new IllegalArgumentException("password cannot be empty");
1706 }
1707
1708 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1709 "no permission to access the crypt keeper");
1710
1711 waitForReady();
1712
1713 if (DEBUG_EVENTS) {
1714 Slog.i(TAG, "changing encryption password...");
1715 }
1716
1717 try {
1718 ArrayList<String> response = mConnector.doCommand("cryptfs changepw " + password);
1719
1720 String[] tokens = response.get(0).split(" ");
1721
1722 if (tokens == null || tokens.length != 2) {
1723 return -1;
1724 }
1725
1726 return Integer.parseInt(tokens[1]);
1727 } catch (NativeDaemonConnectorException e) {
1728 // Encryption failed
1729 return e.getCode();
1730 }
1731 }
1732
Kenny Rootaf9d6672010-10-08 09:21:39 -07001733 private void addObbStateLocked(ObbState obbState) throws RemoteException {
1734 final IBinder binder = obbState.getBinder();
1735 List<ObbState> obbStates = mObbMounts.get(binder);
Kenny Root5919ac62010-10-05 09:49:40 -07001736
Kenny Rootaf9d6672010-10-08 09:21:39 -07001737 if (obbStates == null) {
1738 obbStates = new ArrayList<ObbState>();
1739 mObbMounts.put(binder, obbStates);
1740 } else {
1741 for (final ObbState o : obbStates) {
1742 if (o.filename.equals(obbState.filename)) {
1743 throw new IllegalStateException("Attempt to add ObbState twice. "
1744 + "This indicates an error in the MountService logic.");
Kenny Root5919ac62010-10-05 09:49:40 -07001745 }
1746 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001747 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001748
1749 obbStates.add(obbState);
1750 try {
1751 obbState.link();
1752 } catch (RemoteException e) {
1753 /*
1754 * The binder died before we could link it, so clean up our state
1755 * and return failure.
1756 */
1757 obbStates.remove(obbState);
1758 if (obbStates.isEmpty()) {
1759 mObbMounts.remove(binder);
1760 }
1761
1762 // Rethrow the error so mountObb can get it
1763 throw e;
1764 }
1765
1766 mObbPathToStateMap.put(obbState.filename, obbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07001767 }
1768
Kenny Rootaf9d6672010-10-08 09:21:39 -07001769 private void removeObbStateLocked(ObbState obbState) {
1770 final IBinder binder = obbState.getBinder();
1771 final List<ObbState> obbStates = mObbMounts.get(binder);
1772 if (obbStates != null) {
1773 if (obbStates.remove(obbState)) {
1774 obbState.unlink();
Kenny Root05105f72010-09-22 17:29:43 -07001775 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001776 if (obbStates.isEmpty()) {
1777 mObbMounts.remove(binder);
1778 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001779 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001780
Kenny Rootaf9d6672010-10-08 09:21:39 -07001781 mObbPathToStateMap.remove(obbState.filename);
Kenny Root38cf8862010-09-26 14:18:51 -07001782 }
1783
Kenny Roota02b8b02010-08-05 16:14:17 -07001784 private class ObbActionHandler extends Handler {
1785 private boolean mBound = false;
Kenny Root480afe72010-10-07 10:17:50 -07001786 private final List<ObbAction> mActions = new LinkedList<ObbAction>();
Kenny Roota02b8b02010-08-05 16:14:17 -07001787
1788 ObbActionHandler(Looper l) {
1789 super(l);
1790 }
1791
1792 @Override
1793 public void handleMessage(Message msg) {
1794 switch (msg.what) {
1795 case OBB_RUN_ACTION: {
Kenny Root480afe72010-10-07 10:17:50 -07001796 final ObbAction action = (ObbAction) msg.obj;
Kenny Roota02b8b02010-08-05 16:14:17 -07001797
1798 if (DEBUG_OBB)
1799 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
1800
1801 // If a bind was already initiated we don't really
1802 // need to do anything. The pending install
1803 // will be processed later on.
1804 if (!mBound) {
1805 // If this is the only one pending we might
1806 // have to bind to the service again.
1807 if (!connectToService()) {
1808 Slog.e(TAG, "Failed to bind to media container service");
1809 action.handleError();
1810 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07001811 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001812 }
Kenny Root735de3b2010-09-30 14:11:39 -07001813
Kenny Root735de3b2010-09-30 14:11:39 -07001814 mActions.add(action);
Kenny Roota02b8b02010-08-05 16:14:17 -07001815 break;
1816 }
1817 case OBB_MCS_BOUND: {
1818 if (DEBUG_OBB)
1819 Slog.i(TAG, "OBB_MCS_BOUND");
1820 if (msg.obj != null) {
1821 mContainerService = (IMediaContainerService) msg.obj;
1822 }
1823 if (mContainerService == null) {
1824 // Something seriously wrong. Bail out
1825 Slog.e(TAG, "Cannot bind to media container service");
1826 for (ObbAction action : mActions) {
1827 // Indicate service bind error
1828 action.handleError();
1829 }
1830 mActions.clear();
1831 } else if (mActions.size() > 0) {
Kenny Root480afe72010-10-07 10:17:50 -07001832 final ObbAction action = mActions.get(0);
Kenny Roota02b8b02010-08-05 16:14:17 -07001833 if (action != null) {
1834 action.execute(this);
1835 }
1836 } else {
1837 // Should never happen ideally.
1838 Slog.w(TAG, "Empty queue");
1839 }
1840 break;
1841 }
1842 case OBB_MCS_RECONNECT: {
1843 if (DEBUG_OBB)
1844 Slog.i(TAG, "OBB_MCS_RECONNECT");
1845 if (mActions.size() > 0) {
1846 if (mBound) {
1847 disconnectService();
1848 }
1849 if (!connectToService()) {
1850 Slog.e(TAG, "Failed to bind to media container service");
1851 for (ObbAction action : mActions) {
1852 // Indicate service bind error
1853 action.handleError();
1854 }
1855 mActions.clear();
1856 }
1857 }
1858 break;
1859 }
1860 case OBB_MCS_UNBIND: {
1861 if (DEBUG_OBB)
1862 Slog.i(TAG, "OBB_MCS_UNBIND");
1863
1864 // Delete pending install
1865 if (mActions.size() > 0) {
1866 mActions.remove(0);
1867 }
1868 if (mActions.size() == 0) {
1869 if (mBound) {
1870 disconnectService();
1871 }
1872 } else {
1873 // There are more pending requests in queue.
1874 // Just post MCS_BOUND message to trigger processing
1875 // of next pending install.
1876 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1877 }
1878 break;
1879 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001880 case OBB_FLUSH_MOUNT_STATE: {
1881 final String path = (String) msg.obj;
1882
1883 if (DEBUG_OBB)
1884 Slog.i(TAG, "Flushing all OBB state for path " + path);
1885
1886 synchronized (mObbMounts) {
1887 final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>();
1888
1889 final Iterator<Entry<String, ObbState>> i =
1890 mObbPathToStateMap.entrySet().iterator();
1891 while (i.hasNext()) {
1892 final Entry<String, ObbState> obbEntry = i.next();
1893
1894 /*
1895 * If this entry's source file is in the volume path
1896 * that got unmounted, remove it because it's no
1897 * longer valid.
1898 */
1899 if (obbEntry.getKey().startsWith(path)) {
1900 obbStatesToRemove.add(obbEntry.getValue());
1901 }
1902 }
1903
1904 for (final ObbState obbState : obbStatesToRemove) {
1905 if (DEBUG_OBB)
1906 Slog.i(TAG, "Removing state for " + obbState.filename);
1907
1908 removeObbStateLocked(obbState);
1909
1910 try {
1911 obbState.token.onObbResult(obbState.filename, obbState.nonce,
1912 OnObbStateChangeListener.UNMOUNTED);
1913 } catch (RemoteException e) {
1914 Slog.i(TAG, "Couldn't send unmount notification for OBB: "
1915 + obbState.filename);
1916 }
1917 }
1918 }
1919 break;
1920 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001921 }
1922 }
1923
1924 private boolean connectToService() {
1925 if (DEBUG_OBB)
1926 Slog.i(TAG, "Trying to bind to DefaultContainerService");
1927
1928 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
1929 if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) {
1930 mBound = true;
1931 return true;
1932 }
1933 return false;
1934 }
1935
1936 private void disconnectService() {
1937 mContainerService = null;
1938 mBound = false;
1939 mContext.unbindService(mDefContainerConn);
1940 }
1941 }
1942
1943 abstract class ObbAction {
1944 private static final int MAX_RETRIES = 3;
1945 private int mRetries;
1946
1947 ObbState mObbState;
1948
1949 ObbAction(ObbState obbState) {
1950 mObbState = obbState;
1951 }
1952
1953 public void execute(ObbActionHandler handler) {
1954 try {
1955 if (DEBUG_OBB)
1956 Slog.i(TAG, "Starting to execute action: " + this.toString());
1957 mRetries++;
1958 if (mRetries > MAX_RETRIES) {
1959 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
Kenny Root480afe72010-10-07 10:17:50 -07001960 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001961 handleError();
1962 return;
1963 } else {
1964 handleExecute();
1965 if (DEBUG_OBB)
1966 Slog.i(TAG, "Posting install MCS_UNBIND");
1967 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1968 }
1969 } catch (RemoteException e) {
1970 if (DEBUG_OBB)
1971 Slog.i(TAG, "Posting install MCS_RECONNECT");
1972 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
1973 } catch (Exception e) {
1974 if (DEBUG_OBB)
1975 Slog.d(TAG, "Error handling OBB action", e);
1976 handleError();
Kenny Root17eb6fb2010-10-06 15:02:52 -07001977 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001978 }
1979 }
1980
Kenny Root05105f72010-09-22 17:29:43 -07001981 abstract void handleExecute() throws RemoteException, IOException;
Kenny Roota02b8b02010-08-05 16:14:17 -07001982 abstract void handleError();
Kenny Root38cf8862010-09-26 14:18:51 -07001983
1984 protected ObbInfo getObbInfo() throws IOException {
1985 ObbInfo obbInfo;
1986 try {
1987 obbInfo = mContainerService.getObbInfo(mObbState.filename);
1988 } catch (RemoteException e) {
1989 Slog.d(TAG, "Couldn't call DefaultContainerService to fetch OBB info for "
1990 + mObbState.filename);
1991 obbInfo = null;
1992 }
1993 if (obbInfo == null) {
1994 throw new IOException("Couldn't read OBB file: " + mObbState.filename);
1995 }
1996 return obbInfo;
1997 }
1998
Kenny Rootaf9d6672010-10-08 09:21:39 -07001999 protected void sendNewStatusOrIgnore(int status) {
2000 if (mObbState == null || mObbState.token == null) {
2001 return;
2002 }
2003
Kenny Root38cf8862010-09-26 14:18:51 -07002004 try {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002005 mObbState.token.onObbResult(mObbState.filename, mObbState.nonce, status);
Kenny Root38cf8862010-09-26 14:18:51 -07002006 } catch (RemoteException e) {
2007 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
2008 }
2009 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002010 }
2011
2012 class MountObbAction extends ObbAction {
2013 private String mKey;
2014
2015 MountObbAction(ObbState obbState, String key) {
2016 super(obbState);
2017 mKey = key;
2018 }
2019
Jason parks5af0b912010-11-29 09:05:25 -06002020 @Override
Kenny Root735de3b2010-09-30 14:11:39 -07002021 public void handleExecute() throws IOException, RemoteException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002022 waitForReady();
2023 warnOnNotMounted();
2024
Kenny Root38cf8862010-09-26 14:18:51 -07002025 final ObbInfo obbInfo = getObbInfo();
2026
Kenny Roota02b8b02010-08-05 16:14:17 -07002027 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002028 Slog.w(TAG, "Denied attempt to mount OBB " + obbInfo.filename
2029 + " which is owned by " + obbInfo.packageName);
2030 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2031 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07002032 }
2033
Kenny Rootaf9d6672010-10-08 09:21:39 -07002034 final boolean isMounted;
2035 synchronized (mObbMounts) {
2036 isMounted = mObbPathToStateMap.containsKey(obbInfo.filename);
2037 }
2038 if (isMounted) {
2039 Slog.w(TAG, "Attempt to mount OBB which is already mounted: " + obbInfo.filename);
2040 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_ALREADY_MOUNTED);
2041 return;
2042 }
2043
2044 /*
2045 * The filename passed in might not be the canonical name, so just
2046 * set the filename to the canonicalized version.
2047 */
2048 mObbState.filename = obbInfo.filename;
2049
2050 final String hashedKey;
2051 if (mKey == null) {
2052 hashedKey = "none";
2053 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002054 try {
Kenny Root3b1abba2010-10-13 15:00:07 -07002055 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
2056
2057 KeySpec ks = new PBEKeySpec(mKey.toCharArray(), obbInfo.salt,
2058 PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE);
2059 SecretKey key = factory.generateSecret(ks);
2060 BigInteger bi = new BigInteger(key.getEncoded());
2061 hashedKey = bi.toString(16);
Kenny Rootaf9d6672010-10-08 09:21:39 -07002062 } catch (NoSuchAlgorithmException e) {
Kenny Root3b1abba2010-10-13 15:00:07 -07002063 Slog.e(TAG, "Could not load PBKDF2 algorithm", e);
2064 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
2065 return;
2066 } catch (InvalidKeySpecException e) {
2067 Slog.e(TAG, "Invalid key spec when loading PBKDF2 algorithm", e);
2068 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root38cf8862010-09-26 14:18:51 -07002069 return;
2070 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002071 }
Kenny Root38cf8862010-09-26 14:18:51 -07002072
Kenny Rootaf9d6672010-10-08 09:21:39 -07002073 int rc = StorageResultCode.OperationSucceeded;
2074 String cmd = String.format("obb mount %s %s %d", mObbState.filename, hashedKey,
2075 mObbState.callerUid);
2076 try {
2077 mConnector.doCommand(cmd);
2078 } catch (NativeDaemonConnectorException e) {
2079 int code = e.getCode();
2080 if (code != VoldResponseCode.OpFailedStorageBusy) {
2081 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002082 }
2083 }
2084
Kenny Rootaf9d6672010-10-08 09:21:39 -07002085 if (rc == StorageResultCode.OperationSucceeded) {
2086 if (DEBUG_OBB)
2087 Slog.d(TAG, "Successfully mounted OBB " + mObbState.filename);
2088
2089 synchronized (mObbMounts) {
2090 addObbStateLocked(mObbState);
2091 }
2092
2093 sendNewStatusOrIgnore(OnObbStateChangeListener.MOUNTED);
Kenny Root02c87302010-07-01 08:10:18 -07002094 } else {
Kenny Root05105f72010-09-22 17:29:43 -07002095 Slog.e(TAG, "Couldn't mount OBB file: " + rc);
Kenny Roota02b8b02010-08-05 16:14:17 -07002096
Kenny Rootaf9d6672010-10-08 09:21:39 -07002097 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT);
Kenny Root02c87302010-07-01 08:10:18 -07002098 }
2099 }
2100
Jason parks5af0b912010-11-29 09:05:25 -06002101 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002102 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002103 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root02c87302010-07-01 08:10:18 -07002104 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002105
2106 @Override
2107 public String toString() {
2108 StringBuilder sb = new StringBuilder();
2109 sb.append("MountObbAction{");
2110 sb.append("filename=");
2111 sb.append(mObbState.filename);
2112 sb.append(",callerUid=");
2113 sb.append(mObbState.callerUid);
2114 sb.append(",token=");
2115 sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002116 sb.append(",binder=");
2117 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002118 sb.append('}');
2119 return sb.toString();
2120 }
2121 }
2122
2123 class UnmountObbAction extends ObbAction {
2124 private boolean mForceUnmount;
2125
2126 UnmountObbAction(ObbState obbState, boolean force) {
2127 super(obbState);
2128 mForceUnmount = force;
2129 }
2130
Jason parks5af0b912010-11-29 09:05:25 -06002131 @Override
Kenny Root38cf8862010-09-26 14:18:51 -07002132 public void handleExecute() throws IOException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002133 waitForReady();
2134 warnOnNotMounted();
2135
Kenny Root38cf8862010-09-26 14:18:51 -07002136 final ObbInfo obbInfo = getObbInfo();
Kenny Roota02b8b02010-08-05 16:14:17 -07002137
Kenny Rootaf9d6672010-10-08 09:21:39 -07002138 final ObbState obbState;
Kenny Root38cf8862010-09-26 14:18:51 -07002139 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002140 obbState = mObbPathToStateMap.get(obbInfo.filename);
2141 }
Kenny Root38cf8862010-09-26 14:18:51 -07002142
Kenny Rootaf9d6672010-10-08 09:21:39 -07002143 if (obbState == null) {
2144 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_NOT_MOUNTED);
2145 return;
2146 }
2147
2148 if (obbState.callerUid != mObbState.callerUid) {
2149 Slog.w(TAG, "Permission denied attempting to unmount OBB " + obbInfo.filename
2150 + " (owned by " + obbInfo.packageName + ")");
2151 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2152 return;
2153 }
2154
2155 mObbState.filename = obbInfo.filename;
2156
2157 int rc = StorageResultCode.OperationSucceeded;
2158 String cmd = String.format("obb unmount %s%s", mObbState.filename,
2159 (mForceUnmount ? " force" : ""));
2160 try {
2161 mConnector.doCommand(cmd);
2162 } catch (NativeDaemonConnectorException e) {
2163 int code = e.getCode();
2164 if (code == VoldResponseCode.OpFailedStorageBusy) {
2165 rc = StorageResultCode.OperationFailedStorageBusy;
2166 } else if (code == VoldResponseCode.OpFailedStorageNotFound) {
2167 // If it's not mounted then we've already won.
2168 rc = StorageResultCode.OperationSucceeded;
2169 } else {
2170 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002171 }
2172 }
2173
Kenny Rootaf9d6672010-10-08 09:21:39 -07002174 if (rc == StorageResultCode.OperationSucceeded) {
2175 synchronized (mObbMounts) {
2176 removeObbStateLocked(obbState);
Kenny Root38cf8862010-09-26 14:18:51 -07002177 }
2178
Kenny Rootaf9d6672010-10-08 09:21:39 -07002179 sendNewStatusOrIgnore(OnObbStateChangeListener.UNMOUNTED);
Kenny Roota02b8b02010-08-05 16:14:17 -07002180 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002181 Slog.w(TAG, "Could not mount OBB: " + mObbState.filename);
2182 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT);
Kenny Roota02b8b02010-08-05 16:14:17 -07002183 }
2184 }
2185
Jason parks5af0b912010-11-29 09:05:25 -06002186 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002187 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002188 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Roota02b8b02010-08-05 16:14:17 -07002189 }
2190
2191 @Override
2192 public String toString() {
2193 StringBuilder sb = new StringBuilder();
2194 sb.append("UnmountObbAction{");
2195 sb.append("filename=");
2196 sb.append(mObbState.filename != null ? mObbState.filename : "null");
2197 sb.append(",force=");
2198 sb.append(mForceUnmount);
2199 sb.append(",callerUid=");
2200 sb.append(mObbState.callerUid);
2201 sb.append(",token=");
2202 sb.append(mObbState.token != null ? mObbState.token.toString() : "null");
Kenny Root735de3b2010-09-30 14:11:39 -07002203 sb.append(",binder=");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002204 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002205 sb.append('}');
2206 return sb.toString();
2207 }
Kenny Root02c87302010-07-01 08:10:18 -07002208 }
Kenny Root38cf8862010-09-26 14:18:51 -07002209
2210 @Override
2211 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2212 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
2213 pw.println("Permission Denial: can't dump ActivityManager from from pid="
2214 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
2215 + " without permission " + android.Manifest.permission.DUMP);
2216 return;
2217 }
2218
Kenny Root38cf8862010-09-26 14:18:51 -07002219 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002220 pw.println(" mObbMounts:");
Kenny Root38cf8862010-09-26 14:18:51 -07002221
Kenny Rootaf9d6672010-10-08 09:21:39 -07002222 final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet().iterator();
2223 while (binders.hasNext()) {
2224 Entry<IBinder, List<ObbState>> e = binders.next();
2225 pw.print(" Key="); pw.println(e.getKey().toString());
2226 final List<ObbState> obbStates = e.getValue();
Kenny Root38cf8862010-09-26 14:18:51 -07002227 for (final ObbState obbState : obbStates) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002228 pw.print(" "); pw.println(obbState.toString());
Kenny Root38cf8862010-09-26 14:18:51 -07002229 }
2230 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002231
2232 pw.println("");
2233 pw.println(" mObbPathToStateMap:");
2234 final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator();
2235 while (maps.hasNext()) {
2236 final Entry<String, ObbState> e = maps.next();
2237 pw.print(" "); pw.print(e.getKey());
2238 pw.print(" -> "); pw.println(e.getValue().toString());
2239 }
Kenny Root38cf8862010-09-26 14:18:51 -07002240 }
2241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242}
2243