blob: 413d66f3ddf9c1448045dc6b0cbef9f024fbdc95 [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
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080019import com.android.server.am.ActivityManagerService;
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.content.pm.PackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.net.Uri;
San Mehatb1043402010-02-05 08:26:50 -080027import android.os.storage.IMountService;
28import android.os.storage.IMountServiceListener;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080029import android.os.storage.IMountShutdownObserver;
San Mehatb1043402010-02-05 08:26:50 -080030import android.os.storage.StorageResultCode;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080031import android.os.Handler;
Daniel Sandler5f27ef42010-03-16 15:42:02 -040032import android.os.HandlerThread;
33import android.os.Looper;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080034import android.os.Message;
San Mehat4270e1e2010-01-29 05:32:19 -080035import android.os.RemoteException;
36import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.Environment;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -080038import android.os.ServiceManager;
San Mehat207e5382010-02-04 20:46:54 -080039import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.SystemProperties;
San Mehata5078592010-03-25 09:36:54 -070041import android.util.Slog;
San Mehat22dd86e2010-01-12 12:21:18 -080042import java.util.ArrayList;
San Mehat6cdd9c02010-02-09 14:45:20 -080043import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045/**
San Mehatb1043402010-02-05 08:26:50 -080046 * MountService implements back-end services for platform storage
47 * management.
48 * @hide - Applications should use android.os.storage.StorageManager
49 * to access the MountService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 */
San Mehat22dd86e2010-01-12 12:21:18 -080051class MountService extends IMountService.Stub
52 implements INativeDaemonConnectorCallbacks {
San Mehatb1043402010-02-05 08:26:50 -080053 private static final boolean LOCAL_LOGD = false;
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -080054 private static final boolean DEBUG_UNMOUNT = false;
55 private static final boolean DEBUG_EVENTS = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
57 private static final String TAG = "MountService";
58
San Mehat4270e1e2010-01-29 05:32:19 -080059 /*
60 * Internal vold volume state constants
61 */
San Mehat7fd0fee2009-12-17 07:12:23 -080062 class VolumeState {
63 public static final int Init = -1;
64 public static final int NoMedia = 0;
65 public static final int Idle = 1;
66 public static final int Pending = 2;
67 public static final int Checking = 3;
68 public static final int Mounted = 4;
69 public static final int Unmounting = 5;
70 public static final int Formatting = 6;
71 public static final int Shared = 7;
72 public static final int SharedMnt = 8;
73 }
74
San Mehat4270e1e2010-01-29 05:32:19 -080075 /*
76 * Internal vold response code constants
77 */
San Mehat22dd86e2010-01-12 12:21:18 -080078 class VoldResponseCode {
San Mehat4270e1e2010-01-29 05:32:19 -080079 /*
80 * 100 series - Requestion action was initiated; expect another reply
81 * before proceeding with a new command.
82 */
San Mehat22dd86e2010-01-12 12:21:18 -080083 public static final int VolumeListResult = 110;
84 public static final int AsecListResult = 111;
San Mehatc1b4ce92010-02-16 17:13:03 -080085 public static final int StorageUsersListResult = 112;
San Mehat22dd86e2010-01-12 12:21:18 -080086
San Mehat4270e1e2010-01-29 05:32:19 -080087 /*
88 * 200 series - Requestion action has been successfully completed.
89 */
90 public static final int ShareStatusResult = 210;
San Mehat22dd86e2010-01-12 12:21:18 -080091 public static final int AsecPathResult = 211;
San Mehat4270e1e2010-01-29 05:32:19 -080092 public static final int ShareEnabledResult = 212;
San Mehat22dd86e2010-01-12 12:21:18 -080093
San Mehat4270e1e2010-01-29 05:32:19 -080094 /*
95 * 400 series - Command was accepted, but the requested action
96 * did not take place.
97 */
98 public static final int OpFailedNoMedia = 401;
99 public static final int OpFailedMediaBlank = 402;
100 public static final int OpFailedMediaCorrupt = 403;
101 public static final int OpFailedVolNotMounted = 404;
San Mehatd9709982010-02-18 11:43:03 -0800102 public static final int OpFailedStorageBusy = 405;
San Mehat2d66cef2010-03-23 11:12:52 -0700103 public static final int OpFailedStorageNotFound = 406;
San Mehat4270e1e2010-01-29 05:32:19 -0800104
105 /*
106 * 600 series - Unsolicited broadcasts.
107 */
San Mehat22dd86e2010-01-12 12:21:18 -0800108 public static final int VolumeStateChange = 605;
San Mehat22dd86e2010-01-12 12:21:18 -0800109 public static final int ShareAvailabilityChange = 620;
110 public static final int VolumeDiskInserted = 630;
111 public static final int VolumeDiskRemoved = 631;
112 public static final int VolumeBadRemoval = 632;
113 }
114
San Mehat4270e1e2010-01-29 05:32:19 -0800115 private Context mContext;
116 private NativeDaemonConnector mConnector;
117 private String mLegacyState = Environment.MEDIA_REMOVED;
118 private PackageManagerService mPms;
119 private boolean mUmsEnabling;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800120 // Used as a lock for methods that register/unregister listeners.
121 final private ArrayList<MountServiceBinderListener> mListeners =
122 new ArrayList<MountServiceBinderListener>();
San Mehat6a965af22010-02-24 17:47:30 -0800123 private boolean mBooted = false;
124 private boolean mReady = false;
125 private boolean mSendUmsConnectedOnBoot = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800126
San Mehat6cdd9c02010-02-09 14:45:20 -0800127 /**
128 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800129 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800130 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800131 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800132
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800133 private static final int H_UNMOUNT_PM_UPDATE = 1;
134 private static final int H_UNMOUNT_PM_DONE = 2;
135 private static final int H_UNMOUNT_MS = 3;
136 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
137 private static final int MAX_UNMOUNT_RETRIES = 4;
138
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800139 class UnmountCallBack {
140 String path;
141 int retries;
142 boolean force;
143
144 UnmountCallBack(String path, boolean force) {
145 retries = 0;
146 this.path = path;
147 this.force = force;
148 }
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800149
150 void handleFinished() {
San Mehata5078592010-03-25 09:36:54 -0700151 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path);
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800152 doUnmountVolume(path, true);
153 }
154 }
155
156 class UmsEnableCallBack extends UnmountCallBack {
157 String method;
158
159 UmsEnableCallBack(String path, String method, boolean force) {
160 super(path, force);
161 this.method = method;
162 }
163
164 @Override
165 void handleFinished() {
166 super.handleFinished();
167 doShareUnshareVolume(path, method, true);
168 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800169 }
170
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800171 class ShutdownCallBack extends UnmountCallBack {
172 IMountShutdownObserver observer;
173 ShutdownCallBack(String path, IMountShutdownObserver observer) {
174 super(path, true);
175 this.observer = observer;
176 }
177
178 @Override
179 void handleFinished() {
180 int ret = doUnmountVolume(path, true);
181 if (observer != null) {
182 try {
183 observer.onShutDownComplete(ret);
184 } catch (RemoteException e) {
San Mehata5078592010-03-25 09:36:54 -0700185 Slog.w(TAG, "RemoteException when shutting down");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800186 }
187 }
188 }
189 }
190
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400191 class MountServiceHandler extends Handler {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800192 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700193 boolean mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800194
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400195 MountServiceHandler(Looper l) {
196 super(l);
197 }
198
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800199 public void handleMessage(Message msg) {
200 switch (msg.what) {
201 case H_UNMOUNT_PM_UPDATE: {
San Mehata5078592010-03-25 09:36:54 -0700202 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800203 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
204 mForceUnmounts.add(ucb);
San Mehata5078592010-03-25 09:36:54 -0700205 if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800206 // Register only if needed.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700207 if (!mUpdatingStatus) {
San Mehata5078592010-03-25 09:36:54 -0700208 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700209 mUpdatingStatus = true;
210 mPms.updateExternalMediaStatus(false, true);
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800211 }
212 break;
213 }
214 case H_UNMOUNT_PM_DONE: {
San Mehata5078592010-03-25 09:36:54 -0700215 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE");
San Mehata5078592010-03-25 09:36:54 -0700216 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700217 mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800218 int size = mForceUnmounts.size();
219 int sizeArr[] = new int[size];
220 int sizeArrN = 0;
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700221 // Kill processes holding references first
222 ActivityManagerService ams = (ActivityManagerService)
223 ServiceManager.getService("activity");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800224 for (int i = 0; i < size; i++) {
225 UnmountCallBack ucb = mForceUnmounts.get(i);
226 String path = ucb.path;
227 boolean done = false;
228 if (!ucb.force) {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800229 done = true;
230 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800231 int pids[] = getStorageUsers(path);
232 if (pids == null || pids.length == 0) {
233 done = true;
234 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800235 // Eliminate system process here?
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700236 ams.killPids(pids, "unmount media");
237 // Confirm if file references have been freed.
238 pids = getStorageUsers(path);
239 if (pids == null || pids.length == 0) {
240 done = true;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800241 }
242 }
243 }
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700244 if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) {
245 // Retry again
246 Slog.i(TAG, "Retrying to kill storage users again");
247 mHandler.sendMessageDelayed(
248 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
249 ucb.retries++),
250 RETRY_UNMOUNT_DELAY);
251 } else {
252 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
253 Slog.i(TAG, "Failed to unmount media inspite of " +
254 MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now");
255 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800256 sizeArr[sizeArrN++] = i;
257 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
258 ucb));
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800259 }
260 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800261 // Remove already processed elements from list.
262 for (int i = (sizeArrN-1); i >= 0; i--) {
263 mForceUnmounts.remove(sizeArr[i]);
264 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800265 break;
266 }
267 case H_UNMOUNT_MS : {
San Mehata5078592010-03-25 09:36:54 -0700268 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800269 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800270 ucb.handleFinished();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800271 break;
272 }
273 }
274 }
275 };
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400276 final private HandlerThread mHandlerThread;
277 final private Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800278
San Mehat207e5382010-02-04 20:46:54 -0800279 private void waitForReady() {
280 while (mReady == false) {
281 for (int retries = 5; retries > 0; retries--) {
282 if (mReady) {
283 return;
284 }
285 SystemClock.sleep(1000);
286 }
San Mehata5078592010-03-25 09:36:54 -0700287 Slog.w(TAG, "Waiting too long for mReady!");
San Mehat207e5382010-02-04 20:46:54 -0800288 }
San Mehat1f6301e2010-01-07 22:40:27 -0800289 }
290
San Mehat207e5382010-02-04 20:46:54 -0800291 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 public void onReceive(Context context, Intent intent) {
San Mehat91c77612010-01-07 10:39:41 -0800293 String action = intent.getAction();
294
295 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
San Mehat207e5382010-02-04 20:46:54 -0800296 mBooted = true;
San Mehat22dd86e2010-01-12 12:21:18 -0800297
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800298 /*
299 * In the simulator, we need to broadcast a volume mounted event
300 * to make the media scanner run.
301 */
302 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
303 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted);
304 return;
305 }
San Mehatfafb0412010-02-18 19:40:04 -0800306 new Thread() {
307 public void run() {
308 try {
309 String path = Environment.getExternalStorageDirectory().getPath();
San Mehat6a254402010-03-22 10:21:00 -0700310 String state = getVolumeState(path);
311
312 if (state.equals(Environment.MEDIA_UNMOUNTED)) {
San Mehatfafb0412010-02-18 19:40:04 -0800313 int rc = doMountVolume(path);
314 if (rc != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700315 Slog.e(TAG, String.format("Boot-time mount failed (%d)", rc));
San Mehatfafb0412010-02-18 19:40:04 -0800316 }
San Mehat6a254402010-03-22 10:21:00 -0700317 } else if (state.equals(Environment.MEDIA_SHARED)) {
318 /*
319 * Bootstrap UMS enabled state since vold indicates
320 * the volume is shared (runtime restart while ums enabled)
321 */
322 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Shared);
San Mehatfafb0412010-02-18 19:40:04 -0800323 }
San Mehat6a254402010-03-22 10:21:00 -0700324
San Mehat6a965af22010-02-24 17:47:30 -0800325 /*
San Mehat6a254402010-03-22 10:21:00 -0700326 * If UMS was connected on boot, send the connected event
San Mehat6a965af22010-02-24 17:47:30 -0800327 * now that we're up.
328 */
329 if (mSendUmsConnectedOnBoot) {
330 sendUmsIntent(true);
331 mSendUmsConnectedOnBoot = false;
332 }
San Mehatfafb0412010-02-18 19:40:04 -0800333 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700334 Slog.e(TAG, "Boot-time mount exception", ex);
San Mehatfafb0412010-02-18 19:40:04 -0800335 }
San Mehat207e5382010-02-04 20:46:54 -0800336 }
San Mehatfafb0412010-02-18 19:40:04 -0800337 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 }
339 }
340 };
341
San Mehat4270e1e2010-01-29 05:32:19 -0800342 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
343 final IMountServiceListener mListener;
344
345 MountServiceBinderListener(IMountServiceListener listener) {
346 mListener = listener;
347
San Mehat91c77612010-01-07 10:39:41 -0800348 }
349
San Mehat4270e1e2010-01-29 05:32:19 -0800350 public void binderDied() {
San Mehata5078592010-03-25 09:36:54 -0700351 if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!");
San Mehat4270e1e2010-01-29 05:32:19 -0800352 synchronized(mListeners) {
353 mListeners.remove(this);
354 mListener.asBinder().unlinkToDeath(this, 0);
355 }
356 }
357 }
358
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800359 private void doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800360 // TODO: Add support for multiple share methods
361 if (!method.equals("ums")) {
362 throw new IllegalArgumentException(String.format("Method %s not supported", method));
363 }
364
San Mehat4270e1e2010-01-29 05:32:19 -0800365 try {
366 mConnector.doCommand(String.format(
367 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
368 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -0700369 Slog.e(TAG, "Failed to share/unshare", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800370 }
San Mehat4270e1e2010-01-29 05:32:19 -0800371 }
372
San Mehat207e5382010-02-04 20:46:54 -0800373 private void updatePublicVolumeState(String path, String state) {
San Mehat4270e1e2010-01-29 05:32:19 -0800374 if (!path.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -0700375 Slog.w(TAG, "Multiple volumes not currently supported");
San Mehat4270e1e2010-01-29 05:32:19 -0800376 return;
377 }
San Mehatb1043402010-02-05 08:26:50 -0800378
379 if (mLegacyState.equals(state)) {
San Mehata5078592010-03-25 09:36:54 -0700380 Slog.w(TAG, String.format("Duplicate state transition (%s -> %s)", mLegacyState, state));
San Mehatb1043402010-02-05 08:26:50 -0800381 return;
382 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800383 // Update state on PackageManager
384 if (Environment.MEDIA_UNMOUNTED.equals(state)) {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700385 mPms.updateExternalMediaStatus(false, false);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800386 } else if (Environment.MEDIA_MOUNTED.equals(state)) {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700387 mPms.updateExternalMediaStatus(true, false);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800388 }
San Mehat4270e1e2010-01-29 05:32:19 -0800389 String oldState = mLegacyState;
390 mLegacyState = state;
391
392 synchronized (mListeners) {
393 for (int i = mListeners.size() -1; i >= 0; i--) {
394 MountServiceBinderListener bl = mListeners.get(i);
395 try {
San Mehatb1043402010-02-05 08:26:50 -0800396 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800397 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700398 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800399 mListeners.remove(i);
400 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700401 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800402 }
403 }
404 }
405 }
406
407 /**
408 *
409 * Callback from NativeDaemonConnector
410 */
411 public void onDaemonConnected() {
412 /*
413 * Since we'll be calling back into the NativeDaemonConnector,
414 * we need to do our work in a new thread.
415 */
416 new Thread() {
417 public void run() {
418 /**
419 * Determine media state and UMS detection status
420 */
421 String path = Environment.getExternalStorageDirectory().getPath();
422 String state = Environment.MEDIA_REMOVED;
423
424 try {
425 String[] vols = mConnector.doListCommand(
426 "volume list", VoldResponseCode.VolumeListResult);
427 for (String volstr : vols) {
428 String[] tok = volstr.split(" ");
429 // FMT: <label> <mountpoint> <state>
430 if (!tok[1].equals(path)) {
San Mehata5078592010-03-25 09:36:54 -0700431 Slog.w(TAG, String.format(
San Mehat4270e1e2010-01-29 05:32:19 -0800432 "Skipping unknown volume '%s'",tok[1]));
433 continue;
434 }
435 int st = Integer.parseInt(tok[2]);
436 if (st == VolumeState.NoMedia) {
437 state = Environment.MEDIA_REMOVED;
438 } else if (st == VolumeState.Idle) {
San Mehat207e5382010-02-04 20:46:54 -0800439 state = Environment.MEDIA_UNMOUNTED;
San Mehat4270e1e2010-01-29 05:32:19 -0800440 } else if (st == VolumeState.Mounted) {
441 state = Environment.MEDIA_MOUNTED;
San Mehata5078592010-03-25 09:36:54 -0700442 Slog.i(TAG, "Media already mounted on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800443 } else if (st == VolumeState.Shared) {
444 state = Environment.MEDIA_SHARED;
San Mehata5078592010-03-25 09:36:54 -0700445 Slog.i(TAG, "Media shared on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800446 } else {
447 throw new Exception(String.format("Unexpected state %d", st));
448 }
449 }
450 if (state != null) {
San Mehata5078592010-03-25 09:36:54 -0700451 if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
San Mehat4270e1e2010-01-29 05:32:19 -0800452 updatePublicVolumeState(path, state);
453 }
454 } catch (Exception e) {
San Mehata5078592010-03-25 09:36:54 -0700455 Slog.e(TAG, "Error processing initial volume state", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800456 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
457 }
458
459 try {
San Mehat207e5382010-02-04 20:46:54 -0800460 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800461 notifyShareAvailabilityChange("ums", avail);
462 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700463 Slog.w(TAG, "Failed to get share availability");
San Mehat4270e1e2010-01-29 05:32:19 -0800464 }
San Mehat207e5382010-02-04 20:46:54 -0800465 /*
466 * Now that we've done our initialization, release
467 * the hounds!
468 */
469 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800470 }
471 }.start();
472 }
473
474 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800475 * Callback from NativeDaemonConnector
476 */
477 public boolean onEvent(int code, String raw, String[] cooked) {
478 Intent in = null;
479
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800480 if (DEBUG_EVENTS) {
481 StringBuilder builder = new StringBuilder();
482 builder.append("onEvent::");
483 builder.append(" raw= " + raw);
484 if (cooked != null) {
485 builder.append(" cooked = " );
486 for (String str : cooked) {
487 builder.append(" " + str);
488 }
489 }
San Mehata5078592010-03-25 09:36:54 -0700490 Slog.i(TAG, builder.toString());
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800491 }
San Mehat4270e1e2010-01-29 05:32:19 -0800492 if (code == VoldResponseCode.VolumeStateChange) {
493 /*
494 * One of the volumes we're managing has changed state.
495 * Format: "NNN Volume <label> <path> state changed
496 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
497 */
498 notifyVolumeStateChange(
499 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
500 Integer.parseInt(cooked[10]));
501 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
502 // FMT: NNN Share method <method> now <available|unavailable>
503 boolean avail = false;
504 if (cooked[5].equals("available")) {
505 avail = true;
506 }
507 notifyShareAvailabilityChange(cooked[3], avail);
508 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
509 (code == VoldResponseCode.VolumeDiskRemoved) ||
510 (code == VoldResponseCode.VolumeBadRemoval)) {
511 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
512 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
513 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
514 final String label = cooked[2];
515 final String path = cooked[3];
516 int major = -1;
517 int minor = -1;
518
519 try {
520 String devComp = cooked[6].substring(1, cooked[6].length() -1);
521 String[] devTok = devComp.split(":");
522 major = Integer.parseInt(devTok[0]);
523 minor = Integer.parseInt(devTok[1]);
524 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700525 Slog.e(TAG, "Failed to parse major/minor", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800526 }
527
San Mehat4270e1e2010-01-29 05:32:19 -0800528 if (code == VoldResponseCode.VolumeDiskInserted) {
529 new Thread() {
530 public void run() {
531 try {
532 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800533 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700534 Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
San Mehat4270e1e2010-01-29 05:32:19 -0800535 }
536 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700537 Slog.w(TAG, "Failed to mount media on insertion", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800538 }
539 }
540 }.start();
541 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
542 /*
543 * This event gets trumped if we're already in BAD_REMOVAL state
544 */
545 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
546 return true;
547 }
548 /* Send the media unmounted event first */
San Mehata5078592010-03-25 09:36:54 -0700549 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800550 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
551 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
552 mContext.sendBroadcast(in);
553
San Mehata5078592010-03-25 09:36:54 -0700554 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
San Mehat4270e1e2010-01-29 05:32:19 -0800555 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
556 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
557 } else if (code == VoldResponseCode.VolumeBadRemoval) {
San Mehata5078592010-03-25 09:36:54 -0700558 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800559 /* Send the media unmounted event first */
560 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
561 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
562 mContext.sendBroadcast(in);
563
San Mehata5078592010-03-25 09:36:54 -0700564 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
San Mehat4270e1e2010-01-29 05:32:19 -0800565 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
566 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
567 } else {
San Mehata5078592010-03-25 09:36:54 -0700568 Slog.e(TAG, String.format("Unknown code {%d}", code));
San Mehat4270e1e2010-01-29 05:32:19 -0800569 }
570 } else {
571 return false;
572 }
573
574 if (in != null) {
575 mContext.sendBroadcast(in);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400576 }
577 return true;
San Mehat4270e1e2010-01-29 05:32:19 -0800578 }
579
San Mehat207e5382010-02-04 20:46:54 -0800580 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800581 String vs = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -0700582 if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
San Mehat4270e1e2010-01-29 05:32:19 -0800583
584 Intent in = null;
585
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500586 if (oldState == VolumeState.Shared && newState != oldState) {
San Mehata5078592010-03-25 09:36:54 -0700587 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500588 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
589 Uri.parse("file://" + path)));
590 }
591
San Mehat4270e1e2010-01-29 05:32:19 -0800592 if (newState == VolumeState.Init) {
593 } else if (newState == VolumeState.NoMedia) {
594 // NoMedia is handled via Disk Remove events
595 } else if (newState == VolumeState.Idle) {
596 /*
597 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
598 * if we're in the process of enabling UMS
599 */
600 if (!vs.equals(
601 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
602 Environment.MEDIA_NOFS) && !vs.equals(
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800603 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
San Mehata5078592010-03-25 09:36:54 -0700604 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
San Mehat4270e1e2010-01-29 05:32:19 -0800605 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
606 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
607 }
608 } else if (newState == VolumeState.Pending) {
609 } else if (newState == VolumeState.Checking) {
San Mehata5078592010-03-25 09:36:54 -0700610 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
San Mehat4270e1e2010-01-29 05:32:19 -0800611 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
612 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
613 } else if (newState == VolumeState.Mounted) {
San Mehata5078592010-03-25 09:36:54 -0700614 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800615 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
San Mehat4270e1e2010-01-29 05:32:19 -0800616 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
617 in.putExtra("read-only", false);
618 } else if (newState == VolumeState.Unmounting) {
San Mehat4270e1e2010-01-29 05:32:19 -0800619 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
620 } else if (newState == VolumeState.Formatting) {
621 } else if (newState == VolumeState.Shared) {
San Mehata5078592010-03-25 09:36:54 -0700622 if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800623 /* Send the media unmounted event first */
624 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
625 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
626 mContext.sendBroadcast(in);
627
San Mehata5078592010-03-25 09:36:54 -0700628 if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
San Mehat4270e1e2010-01-29 05:32:19 -0800629 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
630 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
San Mehata5078592010-03-25 09:36:54 -0700631 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
San Mehat4270e1e2010-01-29 05:32:19 -0800632 } else if (newState == VolumeState.SharedMnt) {
San Mehata5078592010-03-25 09:36:54 -0700633 Slog.e(TAG, "Live shared mounts not supported yet!");
San Mehat4270e1e2010-01-29 05:32:19 -0800634 return;
635 } else {
San Mehata5078592010-03-25 09:36:54 -0700636 Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800637 }
638
639 if (in != null) {
640 mContext.sendBroadcast(in);
641 }
642 }
643
San Mehat207e5382010-02-04 20:46:54 -0800644 private boolean doGetShareMethodAvailable(String method) {
Kenny Roota80ce062010-06-01 13:23:53 -0700645 try {
646 ArrayList<String> rsp = mConnector.doCommand("share status " + method);
647 } catch (NativeDaemonConnectorException ex) {
648 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
649 return false;
650 }
San Mehat207e5382010-02-04 20:46:54 -0800651
652 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700653 String[] tok = line.split(" ");
654 if (tok.length < 3) {
655 Slog.e(TAG, "Malformed response to share status " + method);
656 return false;
657 }
658
San Mehat207e5382010-02-04 20:46:54 -0800659 int code;
660 try {
661 code = Integer.parseInt(tok[0]);
662 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700663 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehat207e5382010-02-04 20:46:54 -0800664 return false;
665 }
666 if (code == VoldResponseCode.ShareStatusResult) {
667 if (tok[2].equals("available"))
668 return true;
669 return false;
670 } else {
San Mehata5078592010-03-25 09:36:54 -0700671 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehat207e5382010-02-04 20:46:54 -0800672 return false;
673 }
674 }
San Mehata5078592010-03-25 09:36:54 -0700675 Slog.e(TAG, "Got an empty response");
San Mehat207e5382010-02-04 20:46:54 -0800676 return false;
677 }
678
679 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800680 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800681
San Mehata5078592010-03-25 09:36:54 -0700682 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
San Mehat207e5382010-02-04 20:46:54 -0800683 try {
684 mConnector.doCommand(String.format("volume mount %s", path));
685 } catch (NativeDaemonConnectorException e) {
686 /*
687 * Mount failed for some reason
688 */
689 Intent in = null;
690 int code = e.getCode();
691 if (code == VoldResponseCode.OpFailedNoMedia) {
692 /*
693 * Attempt to mount but no media inserted
694 */
San Mehatb1043402010-02-05 08:26:50 -0800695 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800696 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
San Mehata5078592010-03-25 09:36:54 -0700697 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
San Mehat207e5382010-02-04 20:46:54 -0800698 /*
699 * Media is blank or does not contain a supported filesystem
700 */
701 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
702 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800703 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800704 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehata5078592010-03-25 09:36:54 -0700705 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
San Mehat207e5382010-02-04 20:46:54 -0800706 /*
707 * Volume consistency check failed
708 */
709 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
710 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800711 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800712 } else {
San Mehatb1043402010-02-05 08:26:50 -0800713 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800714 }
715
716 /*
717 * Send broadcast intent (if required for the failure)
718 */
719 if (in != null) {
720 mContext.sendBroadcast(in);
721 }
722 }
723
724 return rc;
725 }
726
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800727 /*
728 * If force is not set, we do not unmount if there are
729 * processes holding references to the volume about to be unmounted.
730 * If force is set, all the processes holding references need to be
731 * killed via the ActivityManager before actually unmounting the volume.
732 * This might even take a while and might be retried after timed delays
733 * to make sure we dont end up in an instable state and kill some core
734 * processes.
735 */
San Mehatd9709982010-02-18 11:43:03 -0800736 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800737 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800738 return VoldResponseCode.OpFailedVolNotMounted;
739 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800740 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700741 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800742 try {
San Mehatd9709982010-02-18 11:43:03 -0800743 mConnector.doCommand(String.format(
744 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700745 // We unmounted the volume. None of the asec containers are available now.
746 synchronized (mAsecMountSet) {
747 mAsecMountSet.clear();
748 }
San Mehatb1043402010-02-05 08:26:50 -0800749 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800750 } catch (NativeDaemonConnectorException e) {
751 // Don't worry about mismatch in PackageManager since the
752 // call back will handle the status changes any way.
753 int code = e.getCode();
754 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800755 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800756 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
757 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800758 } else {
San Mehatb1043402010-02-05 08:26:50 -0800759 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800760 }
761 }
762 }
763
764 private int doFormatVolume(String path) {
765 try {
766 String cmd = String.format("volume format %s", path);
767 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800768 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800769 } catch (NativeDaemonConnectorException e) {
770 int code = e.getCode();
771 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800772 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800773 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800774 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800775 } else {
San Mehatb1043402010-02-05 08:26:50 -0800776 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800777 }
778 }
779 }
780
San Mehatb1043402010-02-05 08:26:50 -0800781 private boolean doGetVolumeShared(String path, String method) {
782 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700783 ArrayList<String> rsp;
784
785 try {
786 rsp = mConnector.doCommand(cmd);
787 } catch (NativeDaemonConnectorException ex) {
788 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
789 return false;
790 }
San Mehatb1043402010-02-05 08:26:50 -0800791
792 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700793 String[] tok = line.split(" ");
794 if (tok.length < 3) {
795 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
796 return false;
797 }
798
San Mehatb1043402010-02-05 08:26:50 -0800799 int code;
800 try {
801 code = Integer.parseInt(tok[0]);
802 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700803 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800804 return false;
805 }
806 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700807 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800808 } else {
San Mehata5078592010-03-25 09:36:54 -0700809 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800810 return false;
811 }
812 }
San Mehata5078592010-03-25 09:36:54 -0700813 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800814 return false;
815 }
816
San Mehat207e5382010-02-04 20:46:54 -0800817 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800818 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -0700819 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800820 return;
821 }
822
823 synchronized (mListeners) {
824 for (int i = mListeners.size() -1; i >= 0; i--) {
825 MountServiceBinderListener bl = mListeners.get(i);
826 try {
San Mehatb1043402010-02-05 08:26:50 -0800827 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -0800828 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700829 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800830 mListeners.remove(i);
831 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700832 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800833 }
834 }
835 }
836
San Mehat207e5382010-02-04 20:46:54 -0800837 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -0800838 sendUmsIntent(avail);
839 } else {
840 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -0800841 }
San Mehat2fe718a2010-03-11 12:01:49 -0800842
843 final String path = Environment.getExternalStorageDirectory().getPath();
844 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
845 /*
846 * USB mass storage disconnected while enabled
847 */
848 new Thread() {
849 public void run() {
850 try {
851 int rc;
San Mehata5078592010-03-25 09:36:54 -0700852 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -0800853 doShareUnshareVolume(path, "ums", false);
854 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700855 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -0800856 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
857 path, rc));
858 }
859 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700860 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -0800861 }
862 }
863 }.start();
864 }
San Mehat4270e1e2010-01-29 05:32:19 -0800865 }
866
San Mehat6a965af22010-02-24 17:47:30 -0800867 private void sendUmsIntent(boolean c) {
868 mContext.sendBroadcast(
869 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
870 }
871
San Mehat207e5382010-02-04 20:46:54 -0800872 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -0800873 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
874 throw new SecurityException(String.format("Requires %s permission", perm));
875 }
876 }
877
878 /**
San Mehat207e5382010-02-04 20:46:54 -0800879 * Constructs a new MountService instance
880 *
881 * @param context Binder context for this service
882 */
883 public MountService(Context context) {
884 mContext = context;
885
San Mehat207e5382010-02-04 20:46:54 -0800886 // XXX: This will go away soon in favor of IMountServiceObserver
887 mPms = (PackageManagerService) ServiceManager.getService("package");
888
889 mContext.registerReceiver(mBroadcastReceiver,
890 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
891
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400892 mHandlerThread = new HandlerThread("MountService");
893 mHandlerThread.start();
894 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
895
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800896 /*
897 * Vold does not run in the simulator, so pretend the connector thread
898 * ran and did its thing.
899 */
900 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
901 mReady = true;
902 mUmsEnabling = true;
903 return;
904 }
905
San Mehat207e5382010-02-04 20:46:54 -0800906 mConnector = new NativeDaemonConnector(this, "vold", 10, "VoldConnector");
907 mReady = false;
908 Thread thread = new Thread(mConnector, NativeDaemonConnector.class.getName());
909 thread.start();
910 }
911
912 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800913 * Exposed API calls below here
914 */
915
916 public void registerListener(IMountServiceListener listener) {
917 synchronized (mListeners) {
918 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
919 try {
920 listener.asBinder().linkToDeath(bl, 0);
921 mListeners.add(bl);
922 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700923 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -0800924 }
925 }
926 }
927
928 public void unregisterListener(IMountServiceListener listener) {
929 synchronized (mListeners) {
930 for(MountServiceBinderListener bl : mListeners) {
931 if (bl.mListener == listener) {
932 mListeners.remove(mListeners.indexOf(bl));
933 return;
934 }
935 }
936 }
937 }
938
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800939 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -0800940 validatePermission(android.Manifest.permission.SHUTDOWN);
941
San Mehata5078592010-03-25 09:36:54 -0700942 Slog.i(TAG, "Shutting down");
San Mehat4270e1e2010-01-29 05:32:19 -0800943
944 String path = Environment.getExternalStorageDirectory().getPath();
945 String state = getVolumeState(path);
San Mehat91c77612010-01-07 10:39:41 -0800946
947 if (state.equals(Environment.MEDIA_SHARED)) {
948 /*
949 * If the media is currently shared, unshare it.
950 * XXX: This is still dangerous!. We should not
951 * be rebooting at *all* if UMS is enabled, since
952 * the UMS host could have dirty FAT cache entries
953 * yet to flush.
954 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800955 setUsbMassStorageEnabled(false);
San Mehat91c77612010-01-07 10:39:41 -0800956 } else if (state.equals(Environment.MEDIA_CHECKING)) {
957 /*
958 * If the media is being checked, then we need to wait for
959 * it to complete before being able to proceed.
960 */
961 // XXX: @hackbod - Should we disable the ANR timer here?
962 int retries = 30;
963 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
964 try {
965 Thread.sleep(1000);
966 } catch (InterruptedException iex) {
San Mehata5078592010-03-25 09:36:54 -0700967 Slog.e(TAG, "Interrupted while waiting for media", iex);
San Mehat91c77612010-01-07 10:39:41 -0800968 break;
969 }
970 state = Environment.getExternalStorageState();
971 }
972 if (retries == 0) {
San Mehata5078592010-03-25 09:36:54 -0700973 Slog.e(TAG, "Timed out waiting for media to check");
San Mehat91c77612010-01-07 10:39:41 -0800974 }
975 }
976
977 if (state.equals(Environment.MEDIA_MOUNTED)) {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800978 // Post a unmount message.
979 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
980 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
San Mehat4270e1e2010-01-29 05:32:19 -0800981 }
982 }
983
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800984 private boolean getUmsEnabling() {
985 synchronized (mListeners) {
986 return mUmsEnabling;
987 }
988 }
989
990 private void setUmsEnabling(boolean enable) {
991 synchronized (mListeners) {
992 mUmsEnabling = true;
993 }
994 }
995
San Mehatb1043402010-02-05 08:26:50 -0800996 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -0800997 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -0800998
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800999 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001000 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001001 }
San Mehatb1043402010-02-05 08:26:50 -08001002 return doGetShareMethodAvailable("ums");
1003 }
1004
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001005 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001006 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001007 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001008
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001009 // TODO: Add support for multiple share methods
1010
1011 /*
1012 * If the volume is mounted and we're enabling then unmount it
1013 */
1014 String path = Environment.getExternalStorageDirectory().getPath();
1015 String vs = getVolumeState(path);
1016 String method = "ums";
1017 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1018 // Override for isUsbMassStorageEnabled()
1019 setUmsEnabling(enable);
1020 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1021 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1022 // Clear override
1023 setUmsEnabling(false);
1024 }
1025 /*
1026 * If we disabled UMS then mount the volume
1027 */
1028 if (!enable) {
1029 doShareUnshareVolume(path, method, enable);
1030 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001031 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001032 " after disabling share method " + method);
1033 /*
1034 * Even though the mount failed, the unshare didn't so don't indicate an error.
1035 * The mountVolume() call will have set the storage state and sent the necessary
1036 * broadcasts.
1037 */
1038 }
1039 }
San Mehatb1043402010-02-05 08:26:50 -08001040 }
1041
1042 public boolean isUsbMassStorageEnabled() {
1043 waitForReady();
1044 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 }
San Mehat4270e1e2010-01-29 05:32:19 -08001046
San Mehat7fd0fee2009-12-17 07:12:23 -08001047 /**
1048 * @return state of the volume at the specified mount point
1049 */
San Mehat4270e1e2010-01-29 05:32:19 -08001050 public String getVolumeState(String mountPoint) {
San Mehat7fd0fee2009-12-17 07:12:23 -08001051 /*
1052 * XXX: Until we have multiple volume discovery, just hardwire
1053 * this to /sdcard
1054 */
1055 if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -07001056 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
San Mehat7fd0fee2009-12-17 07:12:23 -08001057 throw new IllegalArgumentException();
1058 }
1059
1060 return mLegacyState;
1061 }
1062
San Mehat4270e1e2010-01-29 05:32:19 -08001063 public int mountVolume(String path) {
1064 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001065
San Mehat207e5382010-02-04 20:46:54 -08001066 waitForReady();
1067 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 }
1069
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001070 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001071 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001072 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001074 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001075 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001076 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1077 Environment.MEDIA_REMOVED.equals(volState) ||
1078 Environment.MEDIA_SHARED.equals(volState) ||
1079 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1080 // Media already unmounted or cannot be unmounted.
1081 // TODO return valid return code when adding observer call back.
1082 return;
1083 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001084 UnmountCallBack ucb = new UnmountCallBack(path, force);
1085 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 }
1087
San Mehat4270e1e2010-01-29 05:32:19 -08001088 public int formatVolume(String path) {
1089 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001090 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001091
San Mehat207e5382010-02-04 20:46:54 -08001092 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 }
1094
San Mehatc1b4ce92010-02-16 17:13:03 -08001095 public int []getStorageUsers(String path) {
1096 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1097 waitForReady();
1098 try {
1099 String[] r = mConnector.doListCommand(
1100 String.format("storage users %s", path),
1101 VoldResponseCode.StorageUsersListResult);
1102 // FMT: <pid> <process name>
1103 int[] data = new int[r.length];
1104 for (int i = 0; i < r.length; i++) {
1105 String []tok = r[i].split(" ");
1106 try {
1107 data[i] = Integer.parseInt(tok[0]);
1108 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001109 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001110 return new int[0];
1111 }
1112 }
1113 return data;
1114 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001115 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001116 return new int[0];
1117 }
1118 }
1119
San Mehatb1043402010-02-05 08:26:50 -08001120 private void warnOnNotMounted() {
1121 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001122 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001123 }
1124 }
1125
San Mehat4270e1e2010-01-29 05:32:19 -08001126 public String[] getSecureContainerList() {
1127 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001128 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001129 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001130
San Mehat4270e1e2010-01-29 05:32:19 -08001131 try {
1132 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1133 } catch (NativeDaemonConnectorException e) {
1134 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 }
1136 }
San Mehat36972292010-01-06 11:06:32 -08001137
San Mehat4270e1e2010-01-29 05:32:19 -08001138 public int createSecureContainer(String id, int sizeMb, String fstype,
1139 String key, int ownerUid) {
1140 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001141 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001142 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001143
San Mehatb1043402010-02-05 08:26:50 -08001144 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001145 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1146 try {
1147 mConnector.doCommand(cmd);
1148 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001149 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001150 }
San Mehata181b212010-02-11 06:50:20 -08001151
1152 if (rc == StorageResultCode.OperationSucceeded) {
1153 synchronized (mAsecMountSet) {
1154 mAsecMountSet.add(id);
1155 }
1156 }
San Mehat4270e1e2010-01-29 05:32:19 -08001157 return rc;
San Mehat36972292010-01-06 11:06:32 -08001158 }
1159
San Mehat4270e1e2010-01-29 05:32:19 -08001160 public int finalizeSecureContainer(String id) {
1161 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001162 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001163
San Mehatb1043402010-02-05 08:26:50 -08001164 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001165 try {
1166 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001167 /*
1168 * Finalization does a remount, so no need
1169 * to update mAsecMountSet
1170 */
San Mehat4270e1e2010-01-29 05:32:19 -08001171 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001172 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001173 }
San Mehat4270e1e2010-01-29 05:32:19 -08001174 return rc;
San Mehat36972292010-01-06 11:06:32 -08001175 }
1176
San Mehatd9709982010-02-18 11:43:03 -08001177 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001178 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001179 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001180 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001181
San Mehatb1043402010-02-05 08:26:50 -08001182 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001183 try {
San Mehatd9709982010-02-18 11:43:03 -08001184 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001185 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001186 int code = e.getCode();
1187 if (code == VoldResponseCode.OpFailedStorageBusy) {
1188 rc = StorageResultCode.OperationFailedStorageBusy;
1189 } else {
1190 rc = StorageResultCode.OperationFailedInternalError;
1191 }
San Mehat02735bc2010-01-26 15:18:08 -08001192 }
San Mehata181b212010-02-11 06:50:20 -08001193
1194 if (rc == StorageResultCode.OperationSucceeded) {
1195 synchronized (mAsecMountSet) {
1196 if (mAsecMountSet.contains(id)) {
1197 mAsecMountSet.remove(id);
1198 }
1199 }
1200 }
1201
San Mehat4270e1e2010-01-29 05:32:19 -08001202 return rc;
San Mehat36972292010-01-06 11:06:32 -08001203 }
1204
San Mehat4270e1e2010-01-29 05:32:19 -08001205 public int mountSecureContainer(String id, String key, int ownerUid) {
1206 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001207 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001208 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001209
San Mehata181b212010-02-11 06:50:20 -08001210 synchronized (mAsecMountSet) {
1211 if (mAsecMountSet.contains(id)) {
1212 return StorageResultCode.OperationFailedStorageMounted;
1213 }
1214 }
1215
San Mehatb1043402010-02-05 08:26:50 -08001216 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001217 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1218 try {
1219 mConnector.doCommand(cmd);
1220 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001221 int code = e.getCode();
1222 if (code != VoldResponseCode.OpFailedStorageBusy) {
1223 rc = StorageResultCode.OperationFailedInternalError;
1224 }
San Mehat02735bc2010-01-26 15:18:08 -08001225 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001226
1227 if (rc == StorageResultCode.OperationSucceeded) {
1228 synchronized (mAsecMountSet) {
1229 mAsecMountSet.add(id);
1230 }
1231 }
San Mehat4270e1e2010-01-29 05:32:19 -08001232 return rc;
San Mehat36972292010-01-06 11:06:32 -08001233 }
1234
San Mehatd9709982010-02-18 11:43:03 -08001235 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001236 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001237 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001238 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001239
San Mehat6cdd9c02010-02-09 14:45:20 -08001240 synchronized (mAsecMountSet) {
1241 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001242 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001243 }
1244 }
1245
San Mehatb1043402010-02-05 08:26:50 -08001246 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001247 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001248 try {
1249 mConnector.doCommand(cmd);
1250 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001251 int code = e.getCode();
1252 if (code == VoldResponseCode.OpFailedStorageBusy) {
1253 rc = StorageResultCode.OperationFailedStorageBusy;
1254 } else {
1255 rc = StorageResultCode.OperationFailedInternalError;
1256 }
San Mehat02735bc2010-01-26 15:18:08 -08001257 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001258
1259 if (rc == StorageResultCode.OperationSucceeded) {
1260 synchronized (mAsecMountSet) {
1261 mAsecMountSet.remove(id);
1262 }
1263 }
San Mehat4270e1e2010-01-29 05:32:19 -08001264 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001265 }
1266
San Mehat6cdd9c02010-02-09 14:45:20 -08001267 public boolean isSecureContainerMounted(String id) {
1268 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1269 waitForReady();
1270 warnOnNotMounted();
1271
1272 synchronized (mAsecMountSet) {
1273 return mAsecMountSet.contains(id);
1274 }
1275 }
1276
San Mehat4270e1e2010-01-29 05:32:19 -08001277 public int renameSecureContainer(String oldId, String newId) {
1278 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001279 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001280 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001281
San Mehata181b212010-02-11 06:50:20 -08001282 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001283 /*
1284 * Because a mounted container has active internal state which cannot be
1285 * changed while active, we must ensure both ids are not currently mounted.
1286 */
1287 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001288 return StorageResultCode.OperationFailedStorageMounted;
1289 }
1290 }
1291
San Mehatb1043402010-02-05 08:26:50 -08001292 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001293 String cmd = String.format("asec rename %s %s", oldId, newId);
1294 try {
1295 mConnector.doCommand(cmd);
1296 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001297 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001298 }
San Mehata181b212010-02-11 06:50:20 -08001299
San Mehat4270e1e2010-01-29 05:32:19 -08001300 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001301 }
1302
San Mehat4270e1e2010-01-29 05:32:19 -08001303 public String getSecureContainerPath(String id) {
1304 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001305 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001306 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001307
San Mehat2d66cef2010-03-23 11:12:52 -07001308 try {
1309 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1310 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001311 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001312 if (code != VoldResponseCode.AsecPathResult) {
1313 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1314 }
1315 return tok[1];
1316 } catch (NativeDaemonConnectorException e) {
1317 int code = e.getCode();
1318 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1319 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001320 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001321 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001322 }
1323 }
San Mehat22dd86e2010-01-12 12:21:18 -08001324 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001325
1326 public void finishMediaUpdate() {
1327 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329}
1330