blob: 91ada6bf52b1d51ffa9a11f8dbc748f449471643 [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;
147 private String mLegacyState = Environment.MEDIA_REMOVED;
148 private PackageManagerService mPms;
149 private boolean mUmsEnabling;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800150 // Used as a lock for methods that register/unregister listeners.
151 final private ArrayList<MountServiceBinderListener> mListeners =
152 new ArrayList<MountServiceBinderListener>();
San Mehat6a965af22010-02-24 17:47:30 -0800153 private boolean mBooted = false;
154 private boolean mReady = false;
155 private boolean mSendUmsConnectedOnBoot = false;
Mike Lockwood03559752010-07-19 18:25:03 -0400156 // true if we should fake MEDIA_MOUNTED state for external storage
157 private boolean mEmulateExternalStorage = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800158
San Mehat6cdd9c02010-02-09 14:45:20 -0800159 /**
160 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800161 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800162 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800163 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800164
Kenny Root02c87302010-07-01 08:10:18 -0700165 /**
Kenny Root3b1abba2010-10-13 15:00:07 -0700166 * The size of the crypto algorithm key in bits for OBB files. Currently
167 * Twofish is used which takes 128-bit keys.
168 */
169 private static final int CRYPTO_ALGORITHM_KEY_SIZE = 128;
170
171 /**
172 * The number of times to run SHA1 in the PBKDF2 function for OBB files.
173 * 1024 is reasonably secure and not too slow.
174 */
175 private static final int PBKDF2_HASH_ROUNDS = 1024;
176
177 /**
Kenny Roota02b8b02010-08-05 16:14:17 -0700178 * Mounted OBB tracking information. Used to track the current state of all
179 * OBBs.
Kenny Root02c87302010-07-01 08:10:18 -0700180 */
Kenny Root735de3b2010-09-30 14:11:39 -0700181 final private Map<IBinder, List<ObbState>> mObbMounts = new HashMap<IBinder, List<ObbState>>();
Kenny Roota02b8b02010-08-05 16:14:17 -0700182 final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>();
183
184 class ObbState implements IBinder.DeathRecipient {
Kenny Rootaf9d6672010-10-08 09:21:39 -0700185 public ObbState(String filename, int callerUid, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -0700186 throws RemoteException {
Kenny Roota02b8b02010-08-05 16:14:17 -0700187 this.filename = filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700188 this.callerUid = callerUid;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700189 this.token = token;
190 this.nonce = nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700191 }
192
193 // OBB source filename
Kenny Rootaf9d6672010-10-08 09:21:39 -0700194 String filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700195
196 // Binder.callingUid()
Kenny Root05105f72010-09-22 17:29:43 -0700197 final public int callerUid;
Kenny Roota02b8b02010-08-05 16:14:17 -0700198
Kenny Rootaf9d6672010-10-08 09:21:39 -0700199 // Token of remote Binder caller
200 final IObbActionListener token;
201
202 // Identifier to pass back to the token
203 final int nonce;
Kenny Roota02b8b02010-08-05 16:14:17 -0700204
Kenny Root735de3b2010-09-30 14:11:39 -0700205 public IBinder getBinder() {
206 return token.asBinder();
207 }
208
Kenny Roota02b8b02010-08-05 16:14:17 -0700209 @Override
210 public void binderDied() {
211 ObbAction action = new UnmountObbAction(this, true);
212 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root735de3b2010-09-30 14:11:39 -0700213 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700214
Kenny Root5919ac62010-10-05 09:49:40 -0700215 public void link() throws RemoteException {
216 getBinder().linkToDeath(this, 0);
217 }
218
219 public void unlink() {
Kenny Root735de3b2010-09-30 14:11:39 -0700220 getBinder().unlinkToDeath(this, 0);
Kenny Roota02b8b02010-08-05 16:14:17 -0700221 }
Kenny Root38cf8862010-09-26 14:18:51 -0700222
223 @Override
224 public String toString() {
225 StringBuilder sb = new StringBuilder("ObbState{");
226 sb.append("filename=");
227 sb.append(filename);
228 sb.append(",token=");
229 sb.append(token.toString());
230 sb.append(",callerUid=");
231 sb.append(callerUid);
Kenny Root38cf8862010-09-26 14:18:51 -0700232 sb.append('}');
233 return sb.toString();
234 }
Kenny Roota02b8b02010-08-05 16:14:17 -0700235 }
236
237 // OBB Action Handler
238 final private ObbActionHandler mObbActionHandler;
239
240 // OBB action handler messages
241 private static final int OBB_RUN_ACTION = 1;
242 private static final int OBB_MCS_BOUND = 2;
243 private static final int OBB_MCS_UNBIND = 3;
244 private static final int OBB_MCS_RECONNECT = 4;
Kenny Rootaf9d6672010-10-08 09:21:39 -0700245 private static final int OBB_FLUSH_MOUNT_STATE = 5;
Kenny Roota02b8b02010-08-05 16:14:17 -0700246
247 /*
248 * Default Container Service information
249 */
250 static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
251 "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService");
252
253 final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection();
254
255 class DefaultContainerConnection implements ServiceConnection {
256 public void onServiceConnected(ComponentName name, IBinder service) {
257 if (DEBUG_OBB)
258 Slog.i(TAG, "onServiceConnected");
259 IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service);
260 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs));
261 }
262
263 public void onServiceDisconnected(ComponentName name) {
264 if (DEBUG_OBB)
265 Slog.i(TAG, "onServiceDisconnected");
266 }
267 };
268
269 // Used in the ObbActionHandler
270 private IMediaContainerService mContainerService = null;
Kenny Root02c87302010-07-01 08:10:18 -0700271
272 // Handler messages
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800273 private static final int H_UNMOUNT_PM_UPDATE = 1;
274 private static final int H_UNMOUNT_PM_DONE = 2;
275 private static final int H_UNMOUNT_MS = 3;
276 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
277 private static final int MAX_UNMOUNT_RETRIES = 4;
278
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800279 class UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700280 final String path;
281 final boolean force;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800282 int retries;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800283
284 UnmountCallBack(String path, boolean force) {
285 retries = 0;
286 this.path = path;
287 this.force = force;
288 }
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800289
290 void handleFinished() {
San Mehata5078592010-03-25 09:36:54 -0700291 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path);
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800292 doUnmountVolume(path, true);
293 }
294 }
295
296 class UmsEnableCallBack extends UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700297 final String method;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800298
299 UmsEnableCallBack(String path, String method, boolean force) {
300 super(path, force);
301 this.method = method;
302 }
303
304 @Override
305 void handleFinished() {
306 super.handleFinished();
307 doShareUnshareVolume(path, method, true);
308 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800309 }
310
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800311 class ShutdownCallBack extends UnmountCallBack {
312 IMountShutdownObserver observer;
313 ShutdownCallBack(String path, IMountShutdownObserver observer) {
314 super(path, true);
315 this.observer = observer;
316 }
317
318 @Override
319 void handleFinished() {
320 int ret = doUnmountVolume(path, true);
321 if (observer != null) {
322 try {
323 observer.onShutDownComplete(ret);
324 } catch (RemoteException e) {
San Mehata5078592010-03-25 09:36:54 -0700325 Slog.w(TAG, "RemoteException when shutting down");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800326 }
327 }
328 }
329 }
330
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400331 class MountServiceHandler extends Handler {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800332 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700333 boolean mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800334
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400335 MountServiceHandler(Looper l) {
336 super(l);
337 }
338
Jason parks5af0b912010-11-29 09:05:25 -0600339 @Override
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800340 public void handleMessage(Message msg) {
341 switch (msg.what) {
342 case H_UNMOUNT_PM_UPDATE: {
San Mehata5078592010-03-25 09:36:54 -0700343 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800344 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
345 mForceUnmounts.add(ucb);
San Mehata5078592010-03-25 09:36:54 -0700346 if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800347 // Register only if needed.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700348 if (!mUpdatingStatus) {
San Mehata5078592010-03-25 09:36:54 -0700349 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700350 mUpdatingStatus = true;
351 mPms.updateExternalMediaStatus(false, true);
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800352 }
353 break;
354 }
355 case H_UNMOUNT_PM_DONE: {
San Mehata5078592010-03-25 09:36:54 -0700356 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE");
San Mehata5078592010-03-25 09:36:54 -0700357 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700358 mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800359 int size = mForceUnmounts.size();
360 int sizeArr[] = new int[size];
361 int sizeArrN = 0;
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700362 // Kill processes holding references first
363 ActivityManagerService ams = (ActivityManagerService)
364 ServiceManager.getService("activity");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800365 for (int i = 0; i < size; i++) {
366 UnmountCallBack ucb = mForceUnmounts.get(i);
367 String path = ucb.path;
368 boolean done = false;
369 if (!ucb.force) {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800370 done = true;
371 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800372 int pids[] = getStorageUsers(path);
373 if (pids == null || pids.length == 0) {
374 done = true;
375 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800376 // Eliminate system process here?
Dianne Hackborn64825172011-03-02 21:32:58 -0800377 ams.killPids(pids, "unmount media", true);
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700378 // Confirm if file references have been freed.
379 pids = getStorageUsers(path);
380 if (pids == null || pids.length == 0) {
381 done = true;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800382 }
383 }
384 }
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700385 if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) {
386 // Retry again
387 Slog.i(TAG, "Retrying to kill storage users again");
388 mHandler.sendMessageDelayed(
389 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
390 ucb.retries++),
391 RETRY_UNMOUNT_DELAY);
392 } else {
393 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
394 Slog.i(TAG, "Failed to unmount media inspite of " +
395 MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now");
396 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800397 sizeArr[sizeArrN++] = i;
398 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
399 ucb));
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800400 }
401 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800402 // Remove already processed elements from list.
403 for (int i = (sizeArrN-1); i >= 0; i--) {
404 mForceUnmounts.remove(sizeArr[i]);
405 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800406 break;
407 }
408 case H_UNMOUNT_MS : {
San Mehata5078592010-03-25 09:36:54 -0700409 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800410 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800411 ucb.handleFinished();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800412 break;
413 }
414 }
415 }
416 };
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400417 final private HandlerThread mHandlerThread;
418 final private Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800419
San Mehat207e5382010-02-04 20:46:54 -0800420 private void waitForReady() {
421 while (mReady == false) {
422 for (int retries = 5; retries > 0; retries--) {
423 if (mReady) {
424 return;
425 }
426 SystemClock.sleep(1000);
427 }
San Mehata5078592010-03-25 09:36:54 -0700428 Slog.w(TAG, "Waiting too long for mReady!");
San Mehat207e5382010-02-04 20:46:54 -0800429 }
San Mehat1f6301e2010-01-07 22:40:27 -0800430 }
Kenny Root02c87302010-07-01 08:10:18 -0700431
San Mehat207e5382010-02-04 20:46:54 -0800432 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Jason parks5af0b912010-11-29 09:05:25 -0600433 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 public void onReceive(Context context, Intent intent) {
San Mehat91c77612010-01-07 10:39:41 -0800435 String action = intent.getAction();
436
437 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
San Mehat207e5382010-02-04 20:46:54 -0800438 mBooted = true;
San Mehat22dd86e2010-01-12 12:21:18 -0800439
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800440 /*
441 * In the simulator, we need to broadcast a volume mounted event
442 * to make the media scanner run.
443 */
444 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
445 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted);
446 return;
447 }
San Mehatfafb0412010-02-18 19:40:04 -0800448 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600449 @Override
San Mehatfafb0412010-02-18 19:40:04 -0800450 public void run() {
451 try {
452 String path = Environment.getExternalStorageDirectory().getPath();
San Mehat6a254402010-03-22 10:21:00 -0700453 String state = getVolumeState(path);
454
Mike Lockwood03559752010-07-19 18:25:03 -0400455 if (mEmulateExternalStorage) {
456 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Mounted);
457 } else if (state.equals(Environment.MEDIA_UNMOUNTED)) {
San Mehatfafb0412010-02-18 19:40:04 -0800458 int rc = doMountVolume(path);
459 if (rc != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700460 Slog.e(TAG, String.format("Boot-time mount failed (%d)", rc));
San Mehatfafb0412010-02-18 19:40:04 -0800461 }
San Mehat6a254402010-03-22 10:21:00 -0700462 } else if (state.equals(Environment.MEDIA_SHARED)) {
463 /*
464 * Bootstrap UMS enabled state since vold indicates
465 * the volume is shared (runtime restart while ums enabled)
466 */
467 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Shared);
San Mehatfafb0412010-02-18 19:40:04 -0800468 }
San Mehat6a254402010-03-22 10:21:00 -0700469
San Mehat6a965af22010-02-24 17:47:30 -0800470 /*
San Mehat6a254402010-03-22 10:21:00 -0700471 * If UMS was connected on boot, send the connected event
San Mehat6a965af22010-02-24 17:47:30 -0800472 * now that we're up.
473 */
474 if (mSendUmsConnectedOnBoot) {
475 sendUmsIntent(true);
476 mSendUmsConnectedOnBoot = false;
477 }
San Mehatfafb0412010-02-18 19:40:04 -0800478 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700479 Slog.e(TAG, "Boot-time mount exception", ex);
San Mehatfafb0412010-02-18 19:40:04 -0800480 }
San Mehat207e5382010-02-04 20:46:54 -0800481 }
San Mehatfafb0412010-02-18 19:40:04 -0800482 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 }
484 }
485 };
486
San Mehat4270e1e2010-01-29 05:32:19 -0800487 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
488 final IMountServiceListener mListener;
489
490 MountServiceBinderListener(IMountServiceListener listener) {
491 mListener = listener;
Kenny Root02c87302010-07-01 08:10:18 -0700492
San Mehat91c77612010-01-07 10:39:41 -0800493 }
494
San Mehat4270e1e2010-01-29 05:32:19 -0800495 public void binderDied() {
San Mehata5078592010-03-25 09:36:54 -0700496 if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!");
Kenny Roota02b8b02010-08-05 16:14:17 -0700497 synchronized (mListeners) {
San Mehat4270e1e2010-01-29 05:32:19 -0800498 mListeners.remove(this);
499 mListener.asBinder().unlinkToDeath(this, 0);
500 }
501 }
502 }
503
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800504 private void doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800505 // TODO: Add support for multiple share methods
506 if (!method.equals("ums")) {
507 throw new IllegalArgumentException(String.format("Method %s not supported", method));
508 }
509
San Mehat4270e1e2010-01-29 05:32:19 -0800510 try {
511 mConnector.doCommand(String.format(
512 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
513 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -0700514 Slog.e(TAG, "Failed to share/unshare", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800515 }
San Mehat4270e1e2010-01-29 05:32:19 -0800516 }
517
San Mehat207e5382010-02-04 20:46:54 -0800518 private void updatePublicVolumeState(String path, String state) {
San Mehat4270e1e2010-01-29 05:32:19 -0800519 if (!path.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -0700520 Slog.w(TAG, "Multiple volumes not currently supported");
San Mehat4270e1e2010-01-29 05:32:19 -0800521 return;
522 }
San Mehatb1043402010-02-05 08:26:50 -0800523
524 if (mLegacyState.equals(state)) {
San Mehata5078592010-03-25 09:36:54 -0700525 Slog.w(TAG, String.format("Duplicate state transition (%s -> %s)", mLegacyState, state));
San Mehatb1043402010-02-05 08:26:50 -0800526 return;
527 }
Mike Lockwood03559752010-07-19 18:25:03 -0400528 // Update state on PackageManager, but only of real events
529 if (!mEmulateExternalStorage) {
530 if (Environment.MEDIA_UNMOUNTED.equals(state)) {
531 mPms.updateExternalMediaStatus(false, false);
Kenny Rootaf9d6672010-10-08 09:21:39 -0700532
Kenny Root0a9b54e2010-10-13 15:43:35 -0700533 /*
534 * Some OBBs might have been unmounted when this volume was
535 * unmounted, so send a message to the handler to let it know to
536 * remove those from the list of mounted OBBS.
537 */
538 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_FLUSH_MOUNT_STATE,
539 path));
Mike Lockwood03559752010-07-19 18:25:03 -0400540 } else if (Environment.MEDIA_MOUNTED.equals(state)) {
541 mPms.updateExternalMediaStatus(true, false);
542 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800543 }
Kenny Root38cf8862010-09-26 14:18:51 -0700544
San Mehat4270e1e2010-01-29 05:32:19 -0800545 String oldState = mLegacyState;
546 mLegacyState = state;
547
548 synchronized (mListeners) {
549 for (int i = mListeners.size() -1; i >= 0; i--) {
550 MountServiceBinderListener bl = mListeners.get(i);
551 try {
San Mehatb1043402010-02-05 08:26:50 -0800552 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800553 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700554 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800555 mListeners.remove(i);
556 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700557 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800558 }
559 }
560 }
561 }
562
563 /**
564 *
565 * Callback from NativeDaemonConnector
566 */
567 public void onDaemonConnected() {
568 /*
569 * Since we'll be calling back into the NativeDaemonConnector,
570 * we need to do our work in a new thread.
571 */
572 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600573 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800574 public void run() {
575 /**
576 * Determine media state and UMS detection status
577 */
578 String path = Environment.getExternalStorageDirectory().getPath();
579 String state = Environment.MEDIA_REMOVED;
580
581 try {
582 String[] vols = mConnector.doListCommand(
583 "volume list", VoldResponseCode.VolumeListResult);
584 for (String volstr : vols) {
585 String[] tok = volstr.split(" ");
586 // FMT: <label> <mountpoint> <state>
587 if (!tok[1].equals(path)) {
San Mehata5078592010-03-25 09:36:54 -0700588 Slog.w(TAG, String.format(
San Mehat4270e1e2010-01-29 05:32:19 -0800589 "Skipping unknown volume '%s'",tok[1]));
590 continue;
591 }
592 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 }
606 }
607 if (state != null) {
San Mehata5078592010-03-25 09:36:54 -0700608 if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
San Mehat4270e1e2010-01-29 05:32:19 -0800609 updatePublicVolumeState(path, state);
610 }
611 } catch (Exception e) {
San Mehata5078592010-03-25 09:36:54 -0700612 Slog.e(TAG, "Error processing initial volume state", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800613 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
614 }
615
616 try {
San Mehat207e5382010-02-04 20:46:54 -0800617 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800618 notifyShareAvailabilityChange("ums", avail);
619 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700620 Slog.w(TAG, "Failed to get share availability");
San Mehat4270e1e2010-01-29 05:32:19 -0800621 }
San Mehat207e5382010-02-04 20:46:54 -0800622 /*
Jason parks9ed98bc2011-01-17 09:58:35 -0600623 * Now that we've done our initialization, release
San Mehat207e5382010-02-04 20:46:54 -0800624 * the hounds!
625 */
626 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800627 }
628 }.start();
629 }
630
631 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800632 * Callback from NativeDaemonConnector
633 */
634 public boolean onEvent(int code, String raw, String[] cooked) {
635 Intent in = null;
636
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800637 if (DEBUG_EVENTS) {
638 StringBuilder builder = new StringBuilder();
639 builder.append("onEvent::");
640 builder.append(" raw= " + raw);
641 if (cooked != null) {
642 builder.append(" cooked = " );
643 for (String str : cooked) {
644 builder.append(" " + str);
645 }
646 }
San Mehata5078592010-03-25 09:36:54 -0700647 Slog.i(TAG, builder.toString());
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800648 }
San Mehat4270e1e2010-01-29 05:32:19 -0800649 if (code == VoldResponseCode.VolumeStateChange) {
650 /*
651 * One of the volumes we're managing has changed state.
652 * Format: "NNN Volume <label> <path> state changed
653 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
654 */
655 notifyVolumeStateChange(
656 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
657 Integer.parseInt(cooked[10]));
658 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
659 // FMT: NNN Share method <method> now <available|unavailable>
660 boolean avail = false;
661 if (cooked[5].equals("available")) {
662 avail = true;
663 }
664 notifyShareAvailabilityChange(cooked[3], avail);
665 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
666 (code == VoldResponseCode.VolumeDiskRemoved) ||
667 (code == VoldResponseCode.VolumeBadRemoval)) {
668 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
669 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
670 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
671 final String label = cooked[2];
672 final String path = cooked[3];
673 int major = -1;
674 int minor = -1;
675
676 try {
677 String devComp = cooked[6].substring(1, cooked[6].length() -1);
678 String[] devTok = devComp.split(":");
679 major = Integer.parseInt(devTok[0]);
680 minor = Integer.parseInt(devTok[1]);
681 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700682 Slog.e(TAG, "Failed to parse major/minor", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800683 }
684
San Mehat4270e1e2010-01-29 05:32:19 -0800685 if (code == VoldResponseCode.VolumeDiskInserted) {
686 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -0600687 @Override
San Mehat4270e1e2010-01-29 05:32:19 -0800688 public void run() {
689 try {
690 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800691 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700692 Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
San Mehat4270e1e2010-01-29 05:32:19 -0800693 }
694 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700695 Slog.w(TAG, "Failed to mount media on insertion", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800696 }
697 }
698 }.start();
699 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
700 /*
701 * This event gets trumped if we're already in BAD_REMOVAL state
702 */
703 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
704 return true;
705 }
706 /* Send the media unmounted event first */
San Mehata5078592010-03-25 09:36:54 -0700707 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800708 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
709 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
710 mContext.sendBroadcast(in);
711
San Mehata5078592010-03-25 09:36:54 -0700712 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
San Mehat4270e1e2010-01-29 05:32:19 -0800713 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
714 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
715 } else if (code == VoldResponseCode.VolumeBadRemoval) {
San Mehata5078592010-03-25 09:36:54 -0700716 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800717 /* Send the media unmounted event first */
718 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
719 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
720 mContext.sendBroadcast(in);
721
San Mehata5078592010-03-25 09:36:54 -0700722 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
San Mehat4270e1e2010-01-29 05:32:19 -0800723 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
724 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
725 } else {
San Mehata5078592010-03-25 09:36:54 -0700726 Slog.e(TAG, String.format("Unknown code {%d}", code));
San Mehat4270e1e2010-01-29 05:32:19 -0800727 }
728 } else {
729 return false;
730 }
731
732 if (in != null) {
733 mContext.sendBroadcast(in);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400734 }
735 return true;
San Mehat4270e1e2010-01-29 05:32:19 -0800736 }
737
San Mehat207e5382010-02-04 20:46:54 -0800738 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800739 String vs = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -0700740 if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
San Mehat4270e1e2010-01-29 05:32:19 -0800741
742 Intent in = null;
743
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500744 if (oldState == VolumeState.Shared && newState != oldState) {
San Mehata5078592010-03-25 09:36:54 -0700745 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500746 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
747 Uri.parse("file://" + path)));
748 }
749
San Mehat4270e1e2010-01-29 05:32:19 -0800750 if (newState == VolumeState.Init) {
751 } else if (newState == VolumeState.NoMedia) {
752 // NoMedia is handled via Disk Remove events
753 } else if (newState == VolumeState.Idle) {
754 /*
755 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
756 * if we're in the process of enabling UMS
757 */
758 if (!vs.equals(
759 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
760 Environment.MEDIA_NOFS) && !vs.equals(
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800761 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
San Mehata5078592010-03-25 09:36:54 -0700762 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
San Mehat4270e1e2010-01-29 05:32:19 -0800763 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
764 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
765 }
766 } else if (newState == VolumeState.Pending) {
767 } else if (newState == VolumeState.Checking) {
San Mehata5078592010-03-25 09:36:54 -0700768 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
San Mehat4270e1e2010-01-29 05:32:19 -0800769 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
770 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
771 } else if (newState == VolumeState.Mounted) {
San Mehata5078592010-03-25 09:36:54 -0700772 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800773 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
San Mehat4270e1e2010-01-29 05:32:19 -0800774 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
775 in.putExtra("read-only", false);
776 } else if (newState == VolumeState.Unmounting) {
San Mehat4270e1e2010-01-29 05:32:19 -0800777 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
778 } else if (newState == VolumeState.Formatting) {
779 } else if (newState == VolumeState.Shared) {
San Mehata5078592010-03-25 09:36:54 -0700780 if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800781 /* Send the media unmounted event first */
782 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
783 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
784 mContext.sendBroadcast(in);
785
San Mehata5078592010-03-25 09:36:54 -0700786 if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
San Mehat4270e1e2010-01-29 05:32:19 -0800787 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
788 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
San Mehata5078592010-03-25 09:36:54 -0700789 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
San Mehat4270e1e2010-01-29 05:32:19 -0800790 } else if (newState == VolumeState.SharedMnt) {
San Mehata5078592010-03-25 09:36:54 -0700791 Slog.e(TAG, "Live shared mounts not supported yet!");
San Mehat4270e1e2010-01-29 05:32:19 -0800792 return;
793 } else {
San Mehata5078592010-03-25 09:36:54 -0700794 Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800795 }
796
797 if (in != null) {
798 mContext.sendBroadcast(in);
799 }
800 }
801
San Mehat207e5382010-02-04 20:46:54 -0800802 private boolean doGetShareMethodAvailable(String method) {
Kenny Root85fb2062010-06-01 20:50:21 -0700803 ArrayList<String> rsp;
Kenny Roota80ce062010-06-01 13:23:53 -0700804 try {
Kenny Root85fb2062010-06-01 20:50:21 -0700805 rsp = mConnector.doCommand("share status " + method);
Kenny Roota80ce062010-06-01 13:23:53 -0700806 } catch (NativeDaemonConnectorException ex) {
807 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
808 return false;
809 }
San Mehat207e5382010-02-04 20:46:54 -0800810
811 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700812 String[] tok = line.split(" ");
813 if (tok.length < 3) {
814 Slog.e(TAG, "Malformed response to share status " + method);
815 return false;
816 }
817
San Mehat207e5382010-02-04 20:46:54 -0800818 int code;
819 try {
820 code = Integer.parseInt(tok[0]);
821 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700822 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehat207e5382010-02-04 20:46:54 -0800823 return false;
824 }
825 if (code == VoldResponseCode.ShareStatusResult) {
826 if (tok[2].equals("available"))
827 return true;
828 return false;
829 } else {
San Mehata5078592010-03-25 09:36:54 -0700830 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehat207e5382010-02-04 20:46:54 -0800831 return false;
832 }
833 }
San Mehata5078592010-03-25 09:36:54 -0700834 Slog.e(TAG, "Got an empty response");
San Mehat207e5382010-02-04 20:46:54 -0800835 return false;
836 }
837
838 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800839 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800840
San Mehata5078592010-03-25 09:36:54 -0700841 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
San Mehat207e5382010-02-04 20:46:54 -0800842 try {
843 mConnector.doCommand(String.format("volume mount %s", path));
844 } catch (NativeDaemonConnectorException e) {
845 /*
846 * Mount failed for some reason
847 */
848 Intent in = null;
849 int code = e.getCode();
850 if (code == VoldResponseCode.OpFailedNoMedia) {
851 /*
852 * Attempt to mount but no media inserted
853 */
San Mehatb1043402010-02-05 08:26:50 -0800854 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800855 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
San Mehata5078592010-03-25 09:36:54 -0700856 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
San Mehat207e5382010-02-04 20:46:54 -0800857 /*
858 * Media is blank or does not contain a supported filesystem
859 */
860 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
861 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800862 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800863 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehata5078592010-03-25 09:36:54 -0700864 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
San Mehat207e5382010-02-04 20:46:54 -0800865 /*
866 * Volume consistency check failed
867 */
868 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
869 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800870 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800871 } else {
San Mehatb1043402010-02-05 08:26:50 -0800872 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800873 }
874
875 /*
876 * Send broadcast intent (if required for the failure)
877 */
878 if (in != null) {
879 mContext.sendBroadcast(in);
880 }
881 }
882
883 return rc;
884 }
885
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800886 /*
887 * If force is not set, we do not unmount if there are
888 * processes holding references to the volume about to be unmounted.
889 * If force is set, all the processes holding references need to be
890 * killed via the ActivityManager before actually unmounting the volume.
891 * This might even take a while and might be retried after timed delays
892 * to make sure we dont end up in an instable state and kill some core
893 * processes.
894 */
San Mehatd9709982010-02-18 11:43:03 -0800895 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800896 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800897 return VoldResponseCode.OpFailedVolNotMounted;
898 }
Kenny Rootaa485402010-09-14 14:49:41 -0700899
900 /*
901 * Force a GC to make sure AssetManagers in other threads of the
902 * system_server are cleaned up. We have to do this since AssetManager
903 * instances are kept as a WeakReference and it's possible we have files
904 * open on the external storage.
905 */
906 Runtime.getRuntime().gc();
907
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800908 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700909 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800910 try {
San Mehatd9709982010-02-18 11:43:03 -0800911 mConnector.doCommand(String.format(
912 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700913 // We unmounted the volume. None of the asec containers are available now.
914 synchronized (mAsecMountSet) {
915 mAsecMountSet.clear();
916 }
San Mehatb1043402010-02-05 08:26:50 -0800917 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800918 } catch (NativeDaemonConnectorException e) {
919 // Don't worry about mismatch in PackageManager since the
920 // call back will handle the status changes any way.
921 int code = e.getCode();
922 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800923 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800924 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
925 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800926 } else {
San Mehatb1043402010-02-05 08:26:50 -0800927 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800928 }
929 }
930 }
931
932 private int doFormatVolume(String path) {
933 try {
934 String cmd = String.format("volume format %s", path);
935 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800936 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800937 } catch (NativeDaemonConnectorException e) {
938 int code = e.getCode();
939 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800940 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800941 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800942 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800943 } else {
San Mehatb1043402010-02-05 08:26:50 -0800944 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800945 }
946 }
947 }
948
San Mehatb1043402010-02-05 08:26:50 -0800949 private boolean doGetVolumeShared(String path, String method) {
950 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700951 ArrayList<String> rsp;
952
953 try {
954 rsp = mConnector.doCommand(cmd);
955 } catch (NativeDaemonConnectorException ex) {
956 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
957 return false;
958 }
San Mehatb1043402010-02-05 08:26:50 -0800959
960 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700961 String[] tok = line.split(" ");
962 if (tok.length < 3) {
963 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
964 return false;
965 }
966
San Mehatb1043402010-02-05 08:26:50 -0800967 int code;
968 try {
969 code = Integer.parseInt(tok[0]);
970 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700971 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800972 return false;
973 }
974 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700975 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800976 } else {
San Mehata5078592010-03-25 09:36:54 -0700977 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800978 return false;
979 }
980 }
San Mehata5078592010-03-25 09:36:54 -0700981 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800982 return false;
983 }
984
San Mehat207e5382010-02-04 20:46:54 -0800985 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800986 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -0700987 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800988 return;
989 }
990
991 synchronized (mListeners) {
992 for (int i = mListeners.size() -1; i >= 0; i--) {
993 MountServiceBinderListener bl = mListeners.get(i);
994 try {
San Mehatb1043402010-02-05 08:26:50 -0800995 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -0800996 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700997 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800998 mListeners.remove(i);
999 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -07001000 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -08001001 }
1002 }
1003 }
1004
San Mehat207e5382010-02-04 20:46:54 -08001005 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -08001006 sendUmsIntent(avail);
1007 } else {
1008 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -08001009 }
San Mehat2fe718a2010-03-11 12:01:49 -08001010
1011 final String path = Environment.getExternalStorageDirectory().getPath();
1012 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
1013 /*
1014 * USB mass storage disconnected while enabled
1015 */
1016 new Thread() {
Jason parks5af0b912010-11-29 09:05:25 -06001017 @Override
San Mehat2fe718a2010-03-11 12:01:49 -08001018 public void run() {
1019 try {
1020 int rc;
San Mehata5078592010-03-25 09:36:54 -07001021 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -08001022 doShareUnshareVolume(path, "ums", false);
1023 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001024 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -08001025 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
1026 path, rc));
1027 }
1028 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -07001029 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -08001030 }
1031 }
1032 }.start();
1033 }
San Mehat4270e1e2010-01-29 05:32:19 -08001034 }
1035
San Mehat6a965af22010-02-24 17:47:30 -08001036 private void sendUmsIntent(boolean c) {
1037 mContext.sendBroadcast(
1038 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
1039 }
1040
San Mehat207e5382010-02-04 20:46:54 -08001041 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -08001042 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
1043 throw new SecurityException(String.format("Requires %s permission", perm));
1044 }
1045 }
1046
1047 /**
San Mehat207e5382010-02-04 20:46:54 -08001048 * Constructs a new MountService instance
1049 *
1050 * @param context Binder context for this service
1051 */
1052 public MountService(Context context) {
1053 mContext = context;
1054
Mike Lockwood03559752010-07-19 18:25:03 -04001055 mEmulateExternalStorage = context.getResources().getBoolean(
1056 com.android.internal.R.bool.config_emulateExternalStorage);
1057 if (mEmulateExternalStorage) {
1058 Slog.d(TAG, "using emulated external storage");
1059 mLegacyState = Environment.MEDIA_MOUNTED;
1060 }
1061
San Mehat207e5382010-02-04 20:46:54 -08001062 // XXX: This will go away soon in favor of IMountServiceObserver
1063 mPms = (PackageManagerService) ServiceManager.getService("package");
1064
1065 mContext.registerReceiver(mBroadcastReceiver,
1066 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
1067
Daniel Sandler5f27ef42010-03-16 15:42:02 -04001068 mHandlerThread = new HandlerThread("MountService");
1069 mHandlerThread.start();
1070 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
1071
Kenny Roota02b8b02010-08-05 16:14:17 -07001072 // Add OBB Action Handler to MountService thread.
1073 mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper());
1074
Marco Nelissenc34ebce2010-02-18 13:39:41 -08001075 /*
1076 * Vold does not run in the simulator, so pretend the connector thread
1077 * ran and did its thing.
1078 */
1079 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
1080 mReady = true;
1081 mUmsEnabling = true;
1082 return;
1083 }
1084
Kenny Root305bcbf2010-09-03 07:56:38 -07001085 /*
1086 * Create the connection to vold with a maximum queue of twice the
1087 * amount of containers we'd ever expect to have. This keeps an
1088 * "asec list" from blocking a thread repeatedly.
1089 */
1090 mConnector = new NativeDaemonConnector(this, "vold",
1091 PackageManagerService.MAX_CONTAINERS * 2, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001092 mReady = false;
Kenny Root305bcbf2010-09-03 07:56:38 -07001093 Thread thread = new Thread(mConnector, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001094 thread.start();
1095 }
1096
1097 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001098 * Exposed API calls below here
1099 */
1100
1101 public void registerListener(IMountServiceListener listener) {
1102 synchronized (mListeners) {
1103 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
1104 try {
1105 listener.asBinder().linkToDeath(bl, 0);
1106 mListeners.add(bl);
1107 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001108 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -08001109 }
1110 }
1111 }
1112
1113 public void unregisterListener(IMountServiceListener listener) {
1114 synchronized (mListeners) {
1115 for(MountServiceBinderListener bl : mListeners) {
1116 if (bl.mListener == listener) {
1117 mListeners.remove(mListeners.indexOf(bl));
1118 return;
1119 }
1120 }
1121 }
1122 }
1123
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001124 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -08001125 validatePermission(android.Manifest.permission.SHUTDOWN);
1126
San Mehata5078592010-03-25 09:36:54 -07001127 Slog.i(TAG, "Shutting down");
San Mehat4270e1e2010-01-29 05:32:19 -08001128
1129 String path = Environment.getExternalStorageDirectory().getPath();
1130 String state = getVolumeState(path);
San Mehat91c77612010-01-07 10:39:41 -08001131
1132 if (state.equals(Environment.MEDIA_SHARED)) {
1133 /*
1134 * If the media is currently shared, unshare it.
1135 * XXX: This is still dangerous!. We should not
1136 * be rebooting at *all* if UMS is enabled, since
1137 * the UMS host could have dirty FAT cache entries
1138 * yet to flush.
1139 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001140 setUsbMassStorageEnabled(false);
San Mehat91c77612010-01-07 10:39:41 -08001141 } else if (state.equals(Environment.MEDIA_CHECKING)) {
1142 /*
1143 * If the media is being checked, then we need to wait for
1144 * it to complete before being able to proceed.
1145 */
1146 // XXX: @hackbod - Should we disable the ANR timer here?
1147 int retries = 30;
1148 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
1149 try {
1150 Thread.sleep(1000);
1151 } catch (InterruptedException iex) {
San Mehata5078592010-03-25 09:36:54 -07001152 Slog.e(TAG, "Interrupted while waiting for media", iex);
San Mehat91c77612010-01-07 10:39:41 -08001153 break;
1154 }
1155 state = Environment.getExternalStorageState();
1156 }
1157 if (retries == 0) {
San Mehata5078592010-03-25 09:36:54 -07001158 Slog.e(TAG, "Timed out waiting for media to check");
San Mehat91c77612010-01-07 10:39:41 -08001159 }
1160 }
1161
1162 if (state.equals(Environment.MEDIA_MOUNTED)) {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001163 // Post a unmount message.
1164 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
1165 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
Johan Alfven5d0db4d2010-11-09 10:32:25 +01001166 } else if (observer != null) {
1167 /*
1168 * Observer is waiting for onShutDownComplete when we are done.
1169 * Since nothing will be done send notification directly so shutdown
1170 * sequence can continue.
1171 */
1172 try {
1173 observer.onShutDownComplete(StorageResultCode.OperationSucceeded);
1174 } catch (RemoteException e) {
1175 Slog.w(TAG, "RemoteException when shutting down");
1176 }
San Mehat4270e1e2010-01-29 05:32:19 -08001177 }
1178 }
1179
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001180 private boolean getUmsEnabling() {
1181 synchronized (mListeners) {
1182 return mUmsEnabling;
1183 }
1184 }
1185
1186 private void setUmsEnabling(boolean enable) {
1187 synchronized (mListeners) {
Tony Wufc711252010-08-09 16:49:19 +08001188 mUmsEnabling = enable;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001189 }
1190 }
1191
San Mehatb1043402010-02-05 08:26:50 -08001192 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -08001193 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -08001194
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001195 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001196 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001197 }
San Mehatb1043402010-02-05 08:26:50 -08001198 return doGetShareMethodAvailable("ums");
1199 }
1200
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001201 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001202 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001203 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001204
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001205 // TODO: Add support for multiple share methods
1206
1207 /*
1208 * If the volume is mounted and we're enabling then unmount it
1209 */
1210 String path = Environment.getExternalStorageDirectory().getPath();
1211 String vs = getVolumeState(path);
1212 String method = "ums";
1213 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1214 // Override for isUsbMassStorageEnabled()
1215 setUmsEnabling(enable);
1216 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1217 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1218 // Clear override
1219 setUmsEnabling(false);
1220 }
1221 /*
1222 * If we disabled UMS then mount the volume
1223 */
1224 if (!enable) {
1225 doShareUnshareVolume(path, method, enable);
1226 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001227 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001228 " after disabling share method " + method);
1229 /*
1230 * Even though the mount failed, the unshare didn't so don't indicate an error.
1231 * The mountVolume() call will have set the storage state and sent the necessary
1232 * broadcasts.
1233 */
1234 }
1235 }
San Mehatb1043402010-02-05 08:26:50 -08001236 }
1237
1238 public boolean isUsbMassStorageEnabled() {
1239 waitForReady();
1240 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001242
San Mehat7fd0fee2009-12-17 07:12:23 -08001243 /**
1244 * @return state of the volume at the specified mount point
1245 */
San Mehat4270e1e2010-01-29 05:32:19 -08001246 public String getVolumeState(String mountPoint) {
San Mehat7fd0fee2009-12-17 07:12:23 -08001247 /*
1248 * XXX: Until we have multiple volume discovery, just hardwire
1249 * this to /sdcard
1250 */
1251 if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -07001252 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
San Mehat7fd0fee2009-12-17 07:12:23 -08001253 throw new IllegalArgumentException();
1254 }
1255
1256 return mLegacyState;
1257 }
1258
Kenny Roote1ff2142010-10-12 11:20:01 -07001259 public boolean isExternalStorageEmulated() {
1260 return mEmulateExternalStorage;
1261 }
1262
San Mehat4270e1e2010-01-29 05:32:19 -08001263 public int mountVolume(String path) {
1264 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001265
San Mehat207e5382010-02-04 20:46:54 -08001266 waitForReady();
1267 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 }
1269
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001270 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001271 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001272 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001274 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001275 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001276 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1277 Environment.MEDIA_REMOVED.equals(volState) ||
1278 Environment.MEDIA_SHARED.equals(volState) ||
1279 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1280 // Media already unmounted or cannot be unmounted.
1281 // TODO return valid return code when adding observer call back.
1282 return;
1283 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001284 UnmountCallBack ucb = new UnmountCallBack(path, force);
1285 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 }
1287
San Mehat4270e1e2010-01-29 05:32:19 -08001288 public int formatVolume(String path) {
1289 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001290 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001291
San Mehat207e5382010-02-04 20:46:54 -08001292 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 }
1294
San Mehatc1b4ce92010-02-16 17:13:03 -08001295 public int []getStorageUsers(String path) {
1296 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1297 waitForReady();
1298 try {
1299 String[] r = mConnector.doListCommand(
1300 String.format("storage users %s", path),
1301 VoldResponseCode.StorageUsersListResult);
1302 // FMT: <pid> <process name>
1303 int[] data = new int[r.length];
1304 for (int i = 0; i < r.length; i++) {
1305 String []tok = r[i].split(" ");
1306 try {
1307 data[i] = Integer.parseInt(tok[0]);
1308 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001309 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001310 return new int[0];
1311 }
1312 }
1313 return data;
1314 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001315 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001316 return new int[0];
1317 }
1318 }
1319
San Mehatb1043402010-02-05 08:26:50 -08001320 private void warnOnNotMounted() {
1321 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001322 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001323 }
1324 }
1325
San Mehat4270e1e2010-01-29 05:32:19 -08001326 public String[] getSecureContainerList() {
1327 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001328 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001329 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001330
San Mehat4270e1e2010-01-29 05:32:19 -08001331 try {
1332 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1333 } catch (NativeDaemonConnectorException e) {
1334 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 }
1336 }
San Mehat36972292010-01-06 11:06:32 -08001337
San Mehat4270e1e2010-01-29 05:32:19 -08001338 public int createSecureContainer(String id, int sizeMb, String fstype,
1339 String key, int ownerUid) {
1340 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001341 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001342 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001343
San Mehatb1043402010-02-05 08:26:50 -08001344 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001345 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1346 try {
1347 mConnector.doCommand(cmd);
1348 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001349 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001350 }
San Mehata181b212010-02-11 06:50:20 -08001351
1352 if (rc == StorageResultCode.OperationSucceeded) {
1353 synchronized (mAsecMountSet) {
1354 mAsecMountSet.add(id);
1355 }
1356 }
San Mehat4270e1e2010-01-29 05:32:19 -08001357 return rc;
San Mehat36972292010-01-06 11:06:32 -08001358 }
1359
San Mehat4270e1e2010-01-29 05:32:19 -08001360 public int finalizeSecureContainer(String id) {
1361 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001362 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001363
San Mehatb1043402010-02-05 08:26:50 -08001364 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001365 try {
1366 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001367 /*
1368 * Finalization does a remount, so no need
1369 * to update mAsecMountSet
1370 */
San Mehat4270e1e2010-01-29 05:32:19 -08001371 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001372 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001373 }
San Mehat4270e1e2010-01-29 05:32:19 -08001374 return rc;
San Mehat36972292010-01-06 11:06:32 -08001375 }
1376
San Mehatd9709982010-02-18 11:43:03 -08001377 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001378 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001379 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001380 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001381
Kenny Rootaa485402010-09-14 14:49:41 -07001382 /*
1383 * Force a GC to make sure AssetManagers in other threads of the
1384 * system_server are cleaned up. We have to do this since AssetManager
1385 * instances are kept as a WeakReference and it's possible we have files
1386 * open on the external storage.
1387 */
1388 Runtime.getRuntime().gc();
1389
San Mehatb1043402010-02-05 08:26:50 -08001390 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001391 try {
San Mehatd9709982010-02-18 11:43:03 -08001392 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001393 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001394 int code = e.getCode();
1395 if (code == VoldResponseCode.OpFailedStorageBusy) {
1396 rc = StorageResultCode.OperationFailedStorageBusy;
1397 } else {
1398 rc = StorageResultCode.OperationFailedInternalError;
1399 }
San Mehat02735bc2010-01-26 15:18:08 -08001400 }
San Mehata181b212010-02-11 06:50:20 -08001401
1402 if (rc == StorageResultCode.OperationSucceeded) {
1403 synchronized (mAsecMountSet) {
1404 if (mAsecMountSet.contains(id)) {
1405 mAsecMountSet.remove(id);
1406 }
1407 }
1408 }
1409
San Mehat4270e1e2010-01-29 05:32:19 -08001410 return rc;
San Mehat36972292010-01-06 11:06:32 -08001411 }
Jason parks9ed98bc2011-01-17 09:58:35 -06001412
San Mehat4270e1e2010-01-29 05:32:19 -08001413 public int mountSecureContainer(String id, String key, int ownerUid) {
1414 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001415 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001416 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001417
San Mehata181b212010-02-11 06:50:20 -08001418 synchronized (mAsecMountSet) {
1419 if (mAsecMountSet.contains(id)) {
1420 return StorageResultCode.OperationFailedStorageMounted;
1421 }
1422 }
1423
San Mehatb1043402010-02-05 08:26:50 -08001424 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001425 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1426 try {
1427 mConnector.doCommand(cmd);
1428 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001429 int code = e.getCode();
1430 if (code != VoldResponseCode.OpFailedStorageBusy) {
1431 rc = StorageResultCode.OperationFailedInternalError;
1432 }
San Mehat02735bc2010-01-26 15:18:08 -08001433 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001434
1435 if (rc == StorageResultCode.OperationSucceeded) {
1436 synchronized (mAsecMountSet) {
1437 mAsecMountSet.add(id);
1438 }
1439 }
San Mehat4270e1e2010-01-29 05:32:19 -08001440 return rc;
San Mehat36972292010-01-06 11:06:32 -08001441 }
1442
San Mehatd9709982010-02-18 11:43:03 -08001443 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001444 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001445 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001446 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001447
San Mehat6cdd9c02010-02-09 14:45:20 -08001448 synchronized (mAsecMountSet) {
1449 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001450 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001451 }
1452 }
1453
Kenny Rootaa485402010-09-14 14:49:41 -07001454 /*
1455 * Force a GC to make sure AssetManagers in other threads of the
1456 * system_server are cleaned up. We have to do this since AssetManager
1457 * instances are kept as a WeakReference and it's possible we have files
1458 * open on the external storage.
1459 */
1460 Runtime.getRuntime().gc();
1461
San Mehatb1043402010-02-05 08:26:50 -08001462 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001463 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001464 try {
1465 mConnector.doCommand(cmd);
1466 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001467 int code = e.getCode();
1468 if (code == VoldResponseCode.OpFailedStorageBusy) {
1469 rc = StorageResultCode.OperationFailedStorageBusy;
1470 } else {
1471 rc = StorageResultCode.OperationFailedInternalError;
1472 }
San Mehat02735bc2010-01-26 15:18:08 -08001473 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001474
1475 if (rc == StorageResultCode.OperationSucceeded) {
1476 synchronized (mAsecMountSet) {
1477 mAsecMountSet.remove(id);
1478 }
1479 }
San Mehat4270e1e2010-01-29 05:32:19 -08001480 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001481 }
1482
San Mehat6cdd9c02010-02-09 14:45:20 -08001483 public boolean isSecureContainerMounted(String id) {
1484 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1485 waitForReady();
1486 warnOnNotMounted();
1487
1488 synchronized (mAsecMountSet) {
1489 return mAsecMountSet.contains(id);
1490 }
1491 }
1492
San Mehat4270e1e2010-01-29 05:32:19 -08001493 public int renameSecureContainer(String oldId, String newId) {
1494 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001495 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001496 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001497
San Mehata181b212010-02-11 06:50:20 -08001498 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001499 /*
Jason parks9ed98bc2011-01-17 09:58:35 -06001500 * Because a mounted container has active internal state which cannot be
San Mehat85451ee2010-02-24 08:54:18 -08001501 * changed while active, we must ensure both ids are not currently mounted.
1502 */
1503 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001504 return StorageResultCode.OperationFailedStorageMounted;
1505 }
1506 }
1507
San Mehatb1043402010-02-05 08:26:50 -08001508 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001509 String cmd = String.format("asec rename %s %s", oldId, newId);
1510 try {
1511 mConnector.doCommand(cmd);
1512 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001513 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001514 }
San Mehata181b212010-02-11 06:50:20 -08001515
San Mehat4270e1e2010-01-29 05:32:19 -08001516 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001517 }
1518
San Mehat4270e1e2010-01-29 05:32:19 -08001519 public String getSecureContainerPath(String id) {
1520 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001521 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001522 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001523
San Mehat2d66cef2010-03-23 11:12:52 -07001524 try {
1525 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1526 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001527 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001528 if (code != VoldResponseCode.AsecPathResult) {
1529 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1530 }
1531 return tok[1];
1532 } catch (NativeDaemonConnectorException e) {
1533 int code = e.getCode();
1534 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1535 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001536 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001537 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001538 }
1539 }
San Mehat22dd86e2010-01-12 12:21:18 -08001540 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001541
1542 public void finishMediaUpdate() {
1543 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1544 }
Kenny Root02c87302010-07-01 08:10:18 -07001545
Kenny Roota02b8b02010-08-05 16:14:17 -07001546 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
1547 if (callerUid == android.os.Process.SYSTEM_UID) {
1548 return true;
1549 }
1550
Kenny Root02c87302010-07-01 08:10:18 -07001551 if (packageName == null) {
1552 return false;
1553 }
1554
1555 final int packageUid = mPms.getPackageUid(packageName);
1556
1557 if (DEBUG_OBB) {
1558 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
1559 packageUid + ", callerUid = " + callerUid);
1560 }
1561
1562 return callerUid == packageUid;
1563 }
1564
1565 public String getMountedObbPath(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001566 if (filename == null) {
1567 throw new IllegalArgumentException("filename cannot be null");
1568 }
1569
Kenny Root02c87302010-07-01 08:10:18 -07001570 waitForReady();
1571 warnOnNotMounted();
1572
Kenny Root02c87302010-07-01 08:10:18 -07001573 try {
1574 ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename));
1575 String []tok = rsp.get(0).split(" ");
1576 int code = Integer.parseInt(tok[0]);
1577 if (code != VoldResponseCode.AsecPathResult) {
1578 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1579 }
1580 return tok[1];
1581 } catch (NativeDaemonConnectorException e) {
1582 int code = e.getCode();
1583 if (code == VoldResponseCode.OpFailedStorageNotFound) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001584 return null;
Kenny Root02c87302010-07-01 08:10:18 -07001585 } else {
1586 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1587 }
1588 }
1589 }
1590
1591 public boolean isObbMounted(String filename) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07001592 if (filename == null) {
1593 throw new IllegalArgumentException("filename cannot be null");
Kenny Root02c87302010-07-01 08:10:18 -07001594 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001595
1596 synchronized (mObbMounts) {
1597 return mObbPathToStateMap.containsKey(filename);
1598 }
Kenny Root02c87302010-07-01 08:10:18 -07001599 }
1600
Kenny Rootaf9d6672010-10-08 09:21:39 -07001601 public void mountObb(String filename, String key, IObbActionListener token, int nonce)
Kenny Root735de3b2010-09-30 14:11:39 -07001602 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001603 if (filename == null) {
1604 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootaf9d6672010-10-08 09:21:39 -07001605 }
1606
1607 if (token == null) {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001608 throw new IllegalArgumentException("token cannot be null");
1609 }
1610
Kenny Rootaf9d6672010-10-08 09:21:39 -07001611 final int callerUid = Binder.getCallingUid();
1612 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1613 final ObbAction action = new MountObbAction(obbState, key);
Kenny Roota02b8b02010-08-05 16:14:17 -07001614 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
1615
1616 if (DEBUG_OBB)
1617 Slog.i(TAG, "Send to OBB handler: " + action.toString());
Kenny Root02c87302010-07-01 08:10:18 -07001618 }
1619
Kenny Rootaf9d6672010-10-08 09:21:39 -07001620 public void unmountObb(String filename, boolean force, IObbActionListener token, int nonce)
1621 throws RemoteException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001622 if (filename == null) {
1623 throw new IllegalArgumentException("filename cannot be null");
Kenny Rootf1121dc2010-09-29 07:30:53 -07001624 }
1625
Kenny Rootaf9d6672010-10-08 09:21:39 -07001626 final int callerUid = Binder.getCallingUid();
1627 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1628 final ObbAction action = new UnmountObbAction(obbState, force);
Kenny Roota02b8b02010-08-05 16:14:17 -07001629 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root02c87302010-07-01 08:10:18 -07001630
Kenny Roota02b8b02010-08-05 16:14:17 -07001631 if (DEBUG_OBB)
1632 Slog.i(TAG, "Send to OBB handler: " + action.toString());
1633 }
1634
Jason parks5af0b912010-11-29 09:05:25 -06001635 public int decryptStorage(String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06001636 if (TextUtils.isEmpty(password)) {
1637 throw new IllegalArgumentException("password cannot be empty");
Jason parks5af0b912010-11-29 09:05:25 -06001638 }
1639
Jason parks8888c592011-01-20 22:46:41 -06001640 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1641 "no permission to access the crypt keeper");
Jason parks5af0b912010-11-29 09:05:25 -06001642
1643 waitForReady();
1644
1645 if (DEBUG_EVENTS) {
1646 Slog.i(TAG, "decrypting storage...");
1647 }
1648
1649 try {
Jason parks9ed98bc2011-01-17 09:58:35 -06001650 ArrayList<String> rsp = mConnector.doCommand("cryptfs checkpw " + password);
Jason parksf7b3cd42011-01-27 09:28:25 -06001651 String[] tokens = rsp.get(0).split(" ");
Jason parks9ed98bc2011-01-17 09:58:35 -06001652
Jason parksf7b3cd42011-01-27 09:28:25 -06001653 if (tokens == null || tokens.length != 2) {
Jason parks9ed98bc2011-01-17 09:58:35 -06001654 return -1;
1655 }
1656
Jason parksf7b3cd42011-01-27 09:28:25 -06001657 int code = Integer.parseInt(tokens[1]);
Jason parks9ed98bc2011-01-17 09:58:35 -06001658
1659 if (code == 0) {
1660 // Decrypt was successful. Post a delayed message before restarting in order
1661 // to let the UI to clear itself
1662 mHandler.postDelayed(new Runnable() {
1663 public void run() {
1664 mConnector.doCommand(String.format("cryptfs restart"));
1665 }
Jason parksf7b3cd42011-01-27 09:28:25 -06001666 }, 1000); // 1 second
Jason parks9ed98bc2011-01-17 09:58:35 -06001667 }
1668
1669 return code;
Jason parks5af0b912010-11-29 09:05:25 -06001670 } catch (NativeDaemonConnectorException e) {
1671 // Decryption failed
1672 return e.getCode();
1673 }
Jason parks5af0b912010-11-29 09:05:25 -06001674 }
1675
Jason parks56aa5322011-01-07 09:01:15 -06001676 public int encryptStorage(String password) {
Jason parksf7b3cd42011-01-27 09:28:25 -06001677 if (TextUtils.isEmpty(password)) {
1678 throw new IllegalArgumentException("password cannot be empty");
Jason parks56aa5322011-01-07 09:01:15 -06001679 }
1680
Jason parks8888c592011-01-20 22:46:41 -06001681 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1682 "no permission to access the crypt keeper");
Jason parks56aa5322011-01-07 09:01:15 -06001683
1684 waitForReady();
1685
1686 if (DEBUG_EVENTS) {
Jason parks8888c592011-01-20 22:46:41 -06001687 Slog.i(TAG, "encrypting storage...");
Jason parks56aa5322011-01-07 09:01:15 -06001688 }
1689
1690 try {
Jason parks9ed98bc2011-01-17 09:58:35 -06001691 mConnector.doCommand(String.format("cryptfs enablecrypto inplace %s", password));
Jason parks56aa5322011-01-07 09:01:15 -06001692 } catch (NativeDaemonConnectorException e) {
1693 // Encryption failed
1694 return e.getCode();
1695 }
1696
1697 return 0;
1698 }
1699
Jason parksf7b3cd42011-01-27 09:28:25 -06001700 public int changeEncryptionPassword(String password) {
1701 if (TextUtils.isEmpty(password)) {
1702 throw new IllegalArgumentException("password cannot be empty");
1703 }
1704
1705 mContext.enforceCallingOrSelfPermission(Manifest.permission.CRYPT_KEEPER,
1706 "no permission to access the crypt keeper");
1707
1708 waitForReady();
1709
1710 if (DEBUG_EVENTS) {
1711 Slog.i(TAG, "changing encryption password...");
1712 }
1713
1714 try {
1715 ArrayList<String> response = mConnector.doCommand("cryptfs changepw " + password);
1716
1717 String[] tokens = response.get(0).split(" ");
1718
1719 if (tokens == null || tokens.length != 2) {
1720 return -1;
1721 }
1722
1723 return Integer.parseInt(tokens[1]);
1724 } catch (NativeDaemonConnectorException e) {
1725 // Encryption failed
1726 return e.getCode();
1727 }
1728 }
1729
Kenny Rootaf9d6672010-10-08 09:21:39 -07001730 private void addObbStateLocked(ObbState obbState) throws RemoteException {
1731 final IBinder binder = obbState.getBinder();
1732 List<ObbState> obbStates = mObbMounts.get(binder);
Kenny Root5919ac62010-10-05 09:49:40 -07001733
Kenny Rootaf9d6672010-10-08 09:21:39 -07001734 if (obbStates == null) {
1735 obbStates = new ArrayList<ObbState>();
1736 mObbMounts.put(binder, obbStates);
1737 } else {
1738 for (final ObbState o : obbStates) {
1739 if (o.filename.equals(obbState.filename)) {
1740 throw new IllegalStateException("Attempt to add ObbState twice. "
1741 + "This indicates an error in the MountService logic.");
Kenny Root5919ac62010-10-05 09:49:40 -07001742 }
1743 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001744 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001745
1746 obbStates.add(obbState);
1747 try {
1748 obbState.link();
1749 } catch (RemoteException e) {
1750 /*
1751 * The binder died before we could link it, so clean up our state
1752 * and return failure.
1753 */
1754 obbStates.remove(obbState);
1755 if (obbStates.isEmpty()) {
1756 mObbMounts.remove(binder);
1757 }
1758
1759 // Rethrow the error so mountObb can get it
1760 throw e;
1761 }
1762
1763 mObbPathToStateMap.put(obbState.filename, obbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07001764 }
1765
Kenny Rootaf9d6672010-10-08 09:21:39 -07001766 private void removeObbStateLocked(ObbState obbState) {
1767 final IBinder binder = obbState.getBinder();
1768 final List<ObbState> obbStates = mObbMounts.get(binder);
1769 if (obbStates != null) {
1770 if (obbStates.remove(obbState)) {
1771 obbState.unlink();
Kenny Root05105f72010-09-22 17:29:43 -07001772 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001773 if (obbStates.isEmpty()) {
1774 mObbMounts.remove(binder);
1775 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001776 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001777
Kenny Rootaf9d6672010-10-08 09:21:39 -07001778 mObbPathToStateMap.remove(obbState.filename);
Kenny Root38cf8862010-09-26 14:18:51 -07001779 }
1780
Kenny Roota02b8b02010-08-05 16:14:17 -07001781 private class ObbActionHandler extends Handler {
1782 private boolean mBound = false;
Kenny Root480afe72010-10-07 10:17:50 -07001783 private final List<ObbAction> mActions = new LinkedList<ObbAction>();
Kenny Roota02b8b02010-08-05 16:14:17 -07001784
1785 ObbActionHandler(Looper l) {
1786 super(l);
1787 }
1788
1789 @Override
1790 public void handleMessage(Message msg) {
1791 switch (msg.what) {
1792 case OBB_RUN_ACTION: {
Kenny Root480afe72010-10-07 10:17:50 -07001793 final ObbAction action = (ObbAction) msg.obj;
Kenny Roota02b8b02010-08-05 16:14:17 -07001794
1795 if (DEBUG_OBB)
1796 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
1797
1798 // If a bind was already initiated we don't really
1799 // need to do anything. The pending install
1800 // will be processed later on.
1801 if (!mBound) {
1802 // If this is the only one pending we might
1803 // have to bind to the service again.
1804 if (!connectToService()) {
1805 Slog.e(TAG, "Failed to bind to media container service");
1806 action.handleError();
1807 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07001808 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001809 }
Kenny Root735de3b2010-09-30 14:11:39 -07001810
Kenny Root735de3b2010-09-30 14:11:39 -07001811 mActions.add(action);
Kenny Roota02b8b02010-08-05 16:14:17 -07001812 break;
1813 }
1814 case OBB_MCS_BOUND: {
1815 if (DEBUG_OBB)
1816 Slog.i(TAG, "OBB_MCS_BOUND");
1817 if (msg.obj != null) {
1818 mContainerService = (IMediaContainerService) msg.obj;
1819 }
1820 if (mContainerService == null) {
1821 // Something seriously wrong. Bail out
1822 Slog.e(TAG, "Cannot bind to media container service");
1823 for (ObbAction action : mActions) {
1824 // Indicate service bind error
1825 action.handleError();
1826 }
1827 mActions.clear();
1828 } else if (mActions.size() > 0) {
Kenny Root480afe72010-10-07 10:17:50 -07001829 final ObbAction action = mActions.get(0);
Kenny Roota02b8b02010-08-05 16:14:17 -07001830 if (action != null) {
1831 action.execute(this);
1832 }
1833 } else {
1834 // Should never happen ideally.
1835 Slog.w(TAG, "Empty queue");
1836 }
1837 break;
1838 }
1839 case OBB_MCS_RECONNECT: {
1840 if (DEBUG_OBB)
1841 Slog.i(TAG, "OBB_MCS_RECONNECT");
1842 if (mActions.size() > 0) {
1843 if (mBound) {
1844 disconnectService();
1845 }
1846 if (!connectToService()) {
1847 Slog.e(TAG, "Failed to bind to media container service");
1848 for (ObbAction action : mActions) {
1849 // Indicate service bind error
1850 action.handleError();
1851 }
1852 mActions.clear();
1853 }
1854 }
1855 break;
1856 }
1857 case OBB_MCS_UNBIND: {
1858 if (DEBUG_OBB)
1859 Slog.i(TAG, "OBB_MCS_UNBIND");
1860
1861 // Delete pending install
1862 if (mActions.size() > 0) {
1863 mActions.remove(0);
1864 }
1865 if (mActions.size() == 0) {
1866 if (mBound) {
1867 disconnectService();
1868 }
1869 } else {
1870 // There are more pending requests in queue.
1871 // Just post MCS_BOUND message to trigger processing
1872 // of next pending install.
1873 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1874 }
1875 break;
1876 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07001877 case OBB_FLUSH_MOUNT_STATE: {
1878 final String path = (String) msg.obj;
1879
1880 if (DEBUG_OBB)
1881 Slog.i(TAG, "Flushing all OBB state for path " + path);
1882
1883 synchronized (mObbMounts) {
1884 final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>();
1885
1886 final Iterator<Entry<String, ObbState>> i =
1887 mObbPathToStateMap.entrySet().iterator();
1888 while (i.hasNext()) {
1889 final Entry<String, ObbState> obbEntry = i.next();
1890
1891 /*
1892 * If this entry's source file is in the volume path
1893 * that got unmounted, remove it because it's no
1894 * longer valid.
1895 */
1896 if (obbEntry.getKey().startsWith(path)) {
1897 obbStatesToRemove.add(obbEntry.getValue());
1898 }
1899 }
1900
1901 for (final ObbState obbState : obbStatesToRemove) {
1902 if (DEBUG_OBB)
1903 Slog.i(TAG, "Removing state for " + obbState.filename);
1904
1905 removeObbStateLocked(obbState);
1906
1907 try {
1908 obbState.token.onObbResult(obbState.filename, obbState.nonce,
1909 OnObbStateChangeListener.UNMOUNTED);
1910 } catch (RemoteException e) {
1911 Slog.i(TAG, "Couldn't send unmount notification for OBB: "
1912 + obbState.filename);
1913 }
1914 }
1915 }
1916 break;
1917 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001918 }
1919 }
1920
1921 private boolean connectToService() {
1922 if (DEBUG_OBB)
1923 Slog.i(TAG, "Trying to bind to DefaultContainerService");
1924
1925 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
1926 if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) {
1927 mBound = true;
1928 return true;
1929 }
1930 return false;
1931 }
1932
1933 private void disconnectService() {
1934 mContainerService = null;
1935 mBound = false;
1936 mContext.unbindService(mDefContainerConn);
1937 }
1938 }
1939
1940 abstract class ObbAction {
1941 private static final int MAX_RETRIES = 3;
1942 private int mRetries;
1943
1944 ObbState mObbState;
1945
1946 ObbAction(ObbState obbState) {
1947 mObbState = obbState;
1948 }
1949
1950 public void execute(ObbActionHandler handler) {
1951 try {
1952 if (DEBUG_OBB)
1953 Slog.i(TAG, "Starting to execute action: " + this.toString());
1954 mRetries++;
1955 if (mRetries > MAX_RETRIES) {
1956 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
Kenny Root480afe72010-10-07 10:17:50 -07001957 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001958 handleError();
1959 return;
1960 } else {
1961 handleExecute();
1962 if (DEBUG_OBB)
1963 Slog.i(TAG, "Posting install MCS_UNBIND");
1964 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1965 }
1966 } catch (RemoteException e) {
1967 if (DEBUG_OBB)
1968 Slog.i(TAG, "Posting install MCS_RECONNECT");
1969 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
1970 } catch (Exception e) {
1971 if (DEBUG_OBB)
1972 Slog.d(TAG, "Error handling OBB action", e);
1973 handleError();
Kenny Root17eb6fb2010-10-06 15:02:52 -07001974 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
Kenny Roota02b8b02010-08-05 16:14:17 -07001975 }
1976 }
1977
Kenny Root05105f72010-09-22 17:29:43 -07001978 abstract void handleExecute() throws RemoteException, IOException;
Kenny Roota02b8b02010-08-05 16:14:17 -07001979 abstract void handleError();
Kenny Root38cf8862010-09-26 14:18:51 -07001980
1981 protected ObbInfo getObbInfo() throws IOException {
1982 ObbInfo obbInfo;
1983 try {
1984 obbInfo = mContainerService.getObbInfo(mObbState.filename);
1985 } catch (RemoteException e) {
1986 Slog.d(TAG, "Couldn't call DefaultContainerService to fetch OBB info for "
1987 + mObbState.filename);
1988 obbInfo = null;
1989 }
1990 if (obbInfo == null) {
1991 throw new IOException("Couldn't read OBB file: " + mObbState.filename);
1992 }
1993 return obbInfo;
1994 }
1995
Kenny Rootaf9d6672010-10-08 09:21:39 -07001996 protected void sendNewStatusOrIgnore(int status) {
1997 if (mObbState == null || mObbState.token == null) {
1998 return;
1999 }
2000
Kenny Root38cf8862010-09-26 14:18:51 -07002001 try {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002002 mObbState.token.onObbResult(mObbState.filename, mObbState.nonce, status);
Kenny Root38cf8862010-09-26 14:18:51 -07002003 } catch (RemoteException e) {
2004 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
2005 }
2006 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002007 }
2008
2009 class MountObbAction extends ObbAction {
2010 private String mKey;
2011
2012 MountObbAction(ObbState obbState, String key) {
2013 super(obbState);
2014 mKey = key;
2015 }
2016
Jason parks5af0b912010-11-29 09:05:25 -06002017 @Override
Kenny Root735de3b2010-09-30 14:11:39 -07002018 public void handleExecute() throws IOException, RemoteException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002019 waitForReady();
2020 warnOnNotMounted();
2021
Kenny Root38cf8862010-09-26 14:18:51 -07002022 final ObbInfo obbInfo = getObbInfo();
2023
Kenny Roota02b8b02010-08-05 16:14:17 -07002024 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002025 Slog.w(TAG, "Denied attempt to mount OBB " + obbInfo.filename
2026 + " which is owned by " + obbInfo.packageName);
2027 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2028 return;
Kenny Roota02b8b02010-08-05 16:14:17 -07002029 }
2030
Kenny Rootaf9d6672010-10-08 09:21:39 -07002031 final boolean isMounted;
2032 synchronized (mObbMounts) {
2033 isMounted = mObbPathToStateMap.containsKey(obbInfo.filename);
2034 }
2035 if (isMounted) {
2036 Slog.w(TAG, "Attempt to mount OBB which is already mounted: " + obbInfo.filename);
2037 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_ALREADY_MOUNTED);
2038 return;
2039 }
2040
2041 /*
2042 * The filename passed in might not be the canonical name, so just
2043 * set the filename to the canonicalized version.
2044 */
2045 mObbState.filename = obbInfo.filename;
2046
2047 final String hashedKey;
2048 if (mKey == null) {
2049 hashedKey = "none";
2050 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002051 try {
Kenny Root3b1abba2010-10-13 15:00:07 -07002052 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
2053
2054 KeySpec ks = new PBEKeySpec(mKey.toCharArray(), obbInfo.salt,
2055 PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE);
2056 SecretKey key = factory.generateSecret(ks);
2057 BigInteger bi = new BigInteger(key.getEncoded());
2058 hashedKey = bi.toString(16);
Kenny Rootaf9d6672010-10-08 09:21:39 -07002059 } catch (NoSuchAlgorithmException e) {
Kenny Root3b1abba2010-10-13 15:00:07 -07002060 Slog.e(TAG, "Could not load PBKDF2 algorithm", e);
2061 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
2062 return;
2063 } catch (InvalidKeySpecException e) {
2064 Slog.e(TAG, "Invalid key spec when loading PBKDF2 algorithm", e);
2065 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root38cf8862010-09-26 14:18:51 -07002066 return;
2067 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002068 }
Kenny Root38cf8862010-09-26 14:18:51 -07002069
Kenny Rootaf9d6672010-10-08 09:21:39 -07002070 int rc = StorageResultCode.OperationSucceeded;
2071 String cmd = String.format("obb mount %s %s %d", mObbState.filename, hashedKey,
2072 mObbState.callerUid);
2073 try {
2074 mConnector.doCommand(cmd);
2075 } catch (NativeDaemonConnectorException e) {
2076 int code = e.getCode();
2077 if (code != VoldResponseCode.OpFailedStorageBusy) {
2078 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002079 }
2080 }
2081
Kenny Rootaf9d6672010-10-08 09:21:39 -07002082 if (rc == StorageResultCode.OperationSucceeded) {
2083 if (DEBUG_OBB)
2084 Slog.d(TAG, "Successfully mounted OBB " + mObbState.filename);
2085
2086 synchronized (mObbMounts) {
2087 addObbStateLocked(mObbState);
2088 }
2089
2090 sendNewStatusOrIgnore(OnObbStateChangeListener.MOUNTED);
Kenny Root02c87302010-07-01 08:10:18 -07002091 } else {
Kenny Root05105f72010-09-22 17:29:43 -07002092 Slog.e(TAG, "Couldn't mount OBB file: " + rc);
Kenny Roota02b8b02010-08-05 16:14:17 -07002093
Kenny Rootaf9d6672010-10-08 09:21:39 -07002094 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT);
Kenny Root02c87302010-07-01 08:10:18 -07002095 }
2096 }
2097
Jason parks5af0b912010-11-29 09:05:25 -06002098 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002099 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002100 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Root02c87302010-07-01 08:10:18 -07002101 }
Kenny Roota02b8b02010-08-05 16:14:17 -07002102
2103 @Override
2104 public String toString() {
2105 StringBuilder sb = new StringBuilder();
2106 sb.append("MountObbAction{");
2107 sb.append("filename=");
2108 sb.append(mObbState.filename);
2109 sb.append(",callerUid=");
2110 sb.append(mObbState.callerUid);
2111 sb.append(",token=");
2112 sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002113 sb.append(",binder=");
2114 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002115 sb.append('}');
2116 return sb.toString();
2117 }
2118 }
2119
2120 class UnmountObbAction extends ObbAction {
2121 private boolean mForceUnmount;
2122
2123 UnmountObbAction(ObbState obbState, boolean force) {
2124 super(obbState);
2125 mForceUnmount = force;
2126 }
2127
Jason parks5af0b912010-11-29 09:05:25 -06002128 @Override
Kenny Root38cf8862010-09-26 14:18:51 -07002129 public void handleExecute() throws IOException {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002130 waitForReady();
2131 warnOnNotMounted();
2132
Kenny Root38cf8862010-09-26 14:18:51 -07002133 final ObbInfo obbInfo = getObbInfo();
Kenny Roota02b8b02010-08-05 16:14:17 -07002134
Kenny Rootaf9d6672010-10-08 09:21:39 -07002135 final ObbState obbState;
Kenny Root38cf8862010-09-26 14:18:51 -07002136 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002137 obbState = mObbPathToStateMap.get(obbInfo.filename);
2138 }
Kenny Root38cf8862010-09-26 14:18:51 -07002139
Kenny Rootaf9d6672010-10-08 09:21:39 -07002140 if (obbState == null) {
2141 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_NOT_MOUNTED);
2142 return;
2143 }
2144
2145 if (obbState.callerUid != mObbState.callerUid) {
2146 Slog.w(TAG, "Permission denied attempting to unmount OBB " + obbInfo.filename
2147 + " (owned by " + obbInfo.packageName + ")");
2148 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2149 return;
2150 }
2151
2152 mObbState.filename = obbInfo.filename;
2153
2154 int rc = StorageResultCode.OperationSucceeded;
2155 String cmd = String.format("obb unmount %s%s", mObbState.filename,
2156 (mForceUnmount ? " force" : ""));
2157 try {
2158 mConnector.doCommand(cmd);
2159 } catch (NativeDaemonConnectorException e) {
2160 int code = e.getCode();
2161 if (code == VoldResponseCode.OpFailedStorageBusy) {
2162 rc = StorageResultCode.OperationFailedStorageBusy;
2163 } else if (code == VoldResponseCode.OpFailedStorageNotFound) {
2164 // If it's not mounted then we've already won.
2165 rc = StorageResultCode.OperationSucceeded;
2166 } else {
2167 rc = StorageResultCode.OperationFailedInternalError;
Kenny Roota02b8b02010-08-05 16:14:17 -07002168 }
2169 }
2170
Kenny Rootaf9d6672010-10-08 09:21:39 -07002171 if (rc == StorageResultCode.OperationSucceeded) {
2172 synchronized (mObbMounts) {
2173 removeObbStateLocked(obbState);
Kenny Root38cf8862010-09-26 14:18:51 -07002174 }
2175
Kenny Rootaf9d6672010-10-08 09:21:39 -07002176 sendNewStatusOrIgnore(OnObbStateChangeListener.UNMOUNTED);
Kenny Roota02b8b02010-08-05 16:14:17 -07002177 } else {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002178 Slog.w(TAG, "Could not mount OBB: " + mObbState.filename);
2179 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT);
Kenny Roota02b8b02010-08-05 16:14:17 -07002180 }
2181 }
2182
Jason parks5af0b912010-11-29 09:05:25 -06002183 @Override
Kenny Roota02b8b02010-08-05 16:14:17 -07002184 public void handleError() {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002185 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
Kenny Roota02b8b02010-08-05 16:14:17 -07002186 }
2187
2188 @Override
2189 public String toString() {
2190 StringBuilder sb = new StringBuilder();
2191 sb.append("UnmountObbAction{");
2192 sb.append("filename=");
2193 sb.append(mObbState.filename != null ? mObbState.filename : "null");
2194 sb.append(",force=");
2195 sb.append(mForceUnmount);
2196 sb.append(",callerUid=");
2197 sb.append(mObbState.callerUid);
2198 sb.append(",token=");
2199 sb.append(mObbState.token != null ? mObbState.token.toString() : "null");
Kenny Root735de3b2010-09-30 14:11:39 -07002200 sb.append(",binder=");
Kenny Rootaf9d6672010-10-08 09:21:39 -07002201 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
Kenny Roota02b8b02010-08-05 16:14:17 -07002202 sb.append('}');
2203 return sb.toString();
2204 }
Kenny Root02c87302010-07-01 08:10:18 -07002205 }
Kenny Root38cf8862010-09-26 14:18:51 -07002206
2207 @Override
2208 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2209 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
2210 pw.println("Permission Denial: can't dump ActivityManager from from pid="
2211 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
2212 + " without permission " + android.Manifest.permission.DUMP);
2213 return;
2214 }
2215
Kenny Root38cf8862010-09-26 14:18:51 -07002216 synchronized (mObbMounts) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002217 pw.println(" mObbMounts:");
Kenny Root38cf8862010-09-26 14:18:51 -07002218
Kenny Rootaf9d6672010-10-08 09:21:39 -07002219 final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet().iterator();
2220 while (binders.hasNext()) {
2221 Entry<IBinder, List<ObbState>> e = binders.next();
2222 pw.print(" Key="); pw.println(e.getKey().toString());
2223 final List<ObbState> obbStates = e.getValue();
Kenny Root38cf8862010-09-26 14:18:51 -07002224 for (final ObbState obbState : obbStates) {
Kenny Rootaf9d6672010-10-08 09:21:39 -07002225 pw.print(" "); pw.println(obbState.toString());
Kenny Root38cf8862010-09-26 14:18:51 -07002226 }
2227 }
Kenny Rootaf9d6672010-10-08 09:21:39 -07002228
2229 pw.println("");
2230 pw.println(" mObbPathToStateMap:");
2231 final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator();
2232 while (maps.hasNext()) {
2233 final Entry<String, ObbState> e = maps.next();
2234 pw.print(" "); pw.print(e.getKey());
2235 pw.print(" -> "); pw.println(e.getValue().toString());
2236 }
Kenny Root38cf8862010-09-26 14:18:51 -07002237 }
2238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239}
2240