blob: 6c2f1b2d77b8da0eba9e9aa6606e5c83223aceb7 [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 Root85fb2062010-06-01 20:50:21 -0700645 ArrayList<String> rsp;
Kenny Roota80ce062010-06-01 13:23:53 -0700646 try {
Kenny Root85fb2062010-06-01 20:50:21 -0700647 rsp = mConnector.doCommand("share status " + method);
Kenny Roota80ce062010-06-01 13:23:53 -0700648 } catch (NativeDaemonConnectorException ex) {
649 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
650 return false;
651 }
San Mehat207e5382010-02-04 20:46:54 -0800652
653 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700654 String[] tok = line.split(" ");
655 if (tok.length < 3) {
656 Slog.e(TAG, "Malformed response to share status " + method);
657 return false;
658 }
659
San Mehat207e5382010-02-04 20:46:54 -0800660 int code;
661 try {
662 code = Integer.parseInt(tok[0]);
663 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700664 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehat207e5382010-02-04 20:46:54 -0800665 return false;
666 }
667 if (code == VoldResponseCode.ShareStatusResult) {
668 if (tok[2].equals("available"))
669 return true;
670 return false;
671 } else {
San Mehata5078592010-03-25 09:36:54 -0700672 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehat207e5382010-02-04 20:46:54 -0800673 return false;
674 }
675 }
San Mehata5078592010-03-25 09:36:54 -0700676 Slog.e(TAG, "Got an empty response");
San Mehat207e5382010-02-04 20:46:54 -0800677 return false;
678 }
679
680 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800681 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800682
San Mehata5078592010-03-25 09:36:54 -0700683 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
San Mehat207e5382010-02-04 20:46:54 -0800684 try {
685 mConnector.doCommand(String.format("volume mount %s", path));
686 } catch (NativeDaemonConnectorException e) {
687 /*
688 * Mount failed for some reason
689 */
690 Intent in = null;
691 int code = e.getCode();
692 if (code == VoldResponseCode.OpFailedNoMedia) {
693 /*
694 * Attempt to mount but no media inserted
695 */
San Mehatb1043402010-02-05 08:26:50 -0800696 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800697 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
San Mehata5078592010-03-25 09:36:54 -0700698 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
San Mehat207e5382010-02-04 20:46:54 -0800699 /*
700 * Media is blank or does not contain a supported filesystem
701 */
702 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
703 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800704 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800705 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehata5078592010-03-25 09:36:54 -0700706 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
San Mehat207e5382010-02-04 20:46:54 -0800707 /*
708 * Volume consistency check failed
709 */
710 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
711 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800712 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800713 } else {
San Mehatb1043402010-02-05 08:26:50 -0800714 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800715 }
716
717 /*
718 * Send broadcast intent (if required for the failure)
719 */
720 if (in != null) {
721 mContext.sendBroadcast(in);
722 }
723 }
724
725 return rc;
726 }
727
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800728 /*
729 * If force is not set, we do not unmount if there are
730 * processes holding references to the volume about to be unmounted.
731 * If force is set, all the processes holding references need to be
732 * killed via the ActivityManager before actually unmounting the volume.
733 * This might even take a while and might be retried after timed delays
734 * to make sure we dont end up in an instable state and kill some core
735 * processes.
736 */
San Mehatd9709982010-02-18 11:43:03 -0800737 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800738 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800739 return VoldResponseCode.OpFailedVolNotMounted;
740 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800741 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700742 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800743 try {
San Mehatd9709982010-02-18 11:43:03 -0800744 mConnector.doCommand(String.format(
745 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700746 // We unmounted the volume. None of the asec containers are available now.
747 synchronized (mAsecMountSet) {
748 mAsecMountSet.clear();
749 }
San Mehatb1043402010-02-05 08:26:50 -0800750 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800751 } catch (NativeDaemonConnectorException e) {
752 // Don't worry about mismatch in PackageManager since the
753 // call back will handle the status changes any way.
754 int code = e.getCode();
755 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800756 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800757 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
758 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800759 } else {
San Mehatb1043402010-02-05 08:26:50 -0800760 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800761 }
762 }
763 }
764
765 private int doFormatVolume(String path) {
766 try {
767 String cmd = String.format("volume format %s", path);
768 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800769 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800770 } catch (NativeDaemonConnectorException e) {
771 int code = e.getCode();
772 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800773 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800774 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800775 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800776 } else {
San Mehatb1043402010-02-05 08:26:50 -0800777 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800778 }
779 }
780 }
781
San Mehatb1043402010-02-05 08:26:50 -0800782 private boolean doGetVolumeShared(String path, String method) {
783 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700784 ArrayList<String> rsp;
785
786 try {
787 rsp = mConnector.doCommand(cmd);
788 } catch (NativeDaemonConnectorException ex) {
789 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
790 return false;
791 }
San Mehatb1043402010-02-05 08:26:50 -0800792
793 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700794 String[] tok = line.split(" ");
795 if (tok.length < 3) {
796 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
797 return false;
798 }
799
San Mehatb1043402010-02-05 08:26:50 -0800800 int code;
801 try {
802 code = Integer.parseInt(tok[0]);
803 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700804 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800805 return false;
806 }
807 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700808 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800809 } else {
San Mehata5078592010-03-25 09:36:54 -0700810 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800811 return false;
812 }
813 }
San Mehata5078592010-03-25 09:36:54 -0700814 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800815 return false;
816 }
817
San Mehat207e5382010-02-04 20:46:54 -0800818 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800819 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -0700820 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800821 return;
822 }
823
824 synchronized (mListeners) {
825 for (int i = mListeners.size() -1; i >= 0; i--) {
826 MountServiceBinderListener bl = mListeners.get(i);
827 try {
San Mehatb1043402010-02-05 08:26:50 -0800828 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -0800829 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700830 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800831 mListeners.remove(i);
832 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700833 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800834 }
835 }
836 }
837
San Mehat207e5382010-02-04 20:46:54 -0800838 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -0800839 sendUmsIntent(avail);
840 } else {
841 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -0800842 }
San Mehat2fe718a2010-03-11 12:01:49 -0800843
844 final String path = Environment.getExternalStorageDirectory().getPath();
845 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
846 /*
847 * USB mass storage disconnected while enabled
848 */
849 new Thread() {
850 public void run() {
851 try {
852 int rc;
San Mehata5078592010-03-25 09:36:54 -0700853 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -0800854 doShareUnshareVolume(path, "ums", false);
855 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700856 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -0800857 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
858 path, rc));
859 }
860 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700861 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -0800862 }
863 }
864 }.start();
865 }
San Mehat4270e1e2010-01-29 05:32:19 -0800866 }
867
San Mehat6a965af22010-02-24 17:47:30 -0800868 private void sendUmsIntent(boolean c) {
869 mContext.sendBroadcast(
870 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
871 }
872
San Mehat207e5382010-02-04 20:46:54 -0800873 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -0800874 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
875 throw new SecurityException(String.format("Requires %s permission", perm));
876 }
877 }
878
879 /**
San Mehat207e5382010-02-04 20:46:54 -0800880 * Constructs a new MountService instance
881 *
882 * @param context Binder context for this service
883 */
884 public MountService(Context context) {
885 mContext = context;
886
San Mehat207e5382010-02-04 20:46:54 -0800887 // XXX: This will go away soon in favor of IMountServiceObserver
888 mPms = (PackageManagerService) ServiceManager.getService("package");
889
890 mContext.registerReceiver(mBroadcastReceiver,
891 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
892
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400893 mHandlerThread = new HandlerThread("MountService");
894 mHandlerThread.start();
895 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
896
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800897 /*
898 * Vold does not run in the simulator, so pretend the connector thread
899 * ran and did its thing.
900 */
901 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
902 mReady = true;
903 mUmsEnabling = true;
904 return;
905 }
906
San Mehat207e5382010-02-04 20:46:54 -0800907 mConnector = new NativeDaemonConnector(this, "vold", 10, "VoldConnector");
908 mReady = false;
909 Thread thread = new Thread(mConnector, NativeDaemonConnector.class.getName());
910 thread.start();
911 }
912
913 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800914 * Exposed API calls below here
915 */
916
917 public void registerListener(IMountServiceListener listener) {
918 synchronized (mListeners) {
919 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
920 try {
921 listener.asBinder().linkToDeath(bl, 0);
922 mListeners.add(bl);
923 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700924 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -0800925 }
926 }
927 }
928
929 public void unregisterListener(IMountServiceListener listener) {
930 synchronized (mListeners) {
931 for(MountServiceBinderListener bl : mListeners) {
932 if (bl.mListener == listener) {
933 mListeners.remove(mListeners.indexOf(bl));
934 return;
935 }
936 }
937 }
938 }
939
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800940 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -0800941 validatePermission(android.Manifest.permission.SHUTDOWN);
942
San Mehata5078592010-03-25 09:36:54 -0700943 Slog.i(TAG, "Shutting down");
San Mehat4270e1e2010-01-29 05:32:19 -0800944
945 String path = Environment.getExternalStorageDirectory().getPath();
946 String state = getVolumeState(path);
San Mehat91c77612010-01-07 10:39:41 -0800947
948 if (state.equals(Environment.MEDIA_SHARED)) {
949 /*
950 * If the media is currently shared, unshare it.
951 * XXX: This is still dangerous!. We should not
952 * be rebooting at *all* if UMS is enabled, since
953 * the UMS host could have dirty FAT cache entries
954 * yet to flush.
955 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800956 setUsbMassStorageEnabled(false);
San Mehat91c77612010-01-07 10:39:41 -0800957 } else if (state.equals(Environment.MEDIA_CHECKING)) {
958 /*
959 * If the media is being checked, then we need to wait for
960 * it to complete before being able to proceed.
961 */
962 // XXX: @hackbod - Should we disable the ANR timer here?
963 int retries = 30;
964 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
965 try {
966 Thread.sleep(1000);
967 } catch (InterruptedException iex) {
San Mehata5078592010-03-25 09:36:54 -0700968 Slog.e(TAG, "Interrupted while waiting for media", iex);
San Mehat91c77612010-01-07 10:39:41 -0800969 break;
970 }
971 state = Environment.getExternalStorageState();
972 }
973 if (retries == 0) {
San Mehata5078592010-03-25 09:36:54 -0700974 Slog.e(TAG, "Timed out waiting for media to check");
San Mehat91c77612010-01-07 10:39:41 -0800975 }
976 }
977
978 if (state.equals(Environment.MEDIA_MOUNTED)) {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800979 // Post a unmount message.
980 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
981 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
San Mehat4270e1e2010-01-29 05:32:19 -0800982 }
983 }
984
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800985 private boolean getUmsEnabling() {
986 synchronized (mListeners) {
987 return mUmsEnabling;
988 }
989 }
990
991 private void setUmsEnabling(boolean enable) {
992 synchronized (mListeners) {
993 mUmsEnabling = true;
994 }
995 }
996
San Mehatb1043402010-02-05 08:26:50 -0800997 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -0800998 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -0800999
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001000 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001001 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001002 }
San Mehatb1043402010-02-05 08:26:50 -08001003 return doGetShareMethodAvailable("ums");
1004 }
1005
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001006 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001007 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001008 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001009
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001010 // TODO: Add support for multiple share methods
1011
1012 /*
1013 * If the volume is mounted and we're enabling then unmount it
1014 */
1015 String path = Environment.getExternalStorageDirectory().getPath();
1016 String vs = getVolumeState(path);
1017 String method = "ums";
1018 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1019 // Override for isUsbMassStorageEnabled()
1020 setUmsEnabling(enable);
1021 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1022 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1023 // Clear override
1024 setUmsEnabling(false);
1025 }
1026 /*
1027 * If we disabled UMS then mount the volume
1028 */
1029 if (!enable) {
1030 doShareUnshareVolume(path, method, enable);
1031 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001032 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001033 " after disabling share method " + method);
1034 /*
1035 * Even though the mount failed, the unshare didn't so don't indicate an error.
1036 * The mountVolume() call will have set the storage state and sent the necessary
1037 * broadcasts.
1038 */
1039 }
1040 }
San Mehatb1043402010-02-05 08:26:50 -08001041 }
1042
1043 public boolean isUsbMassStorageEnabled() {
1044 waitForReady();
1045 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 }
San Mehat4270e1e2010-01-29 05:32:19 -08001047
San Mehat7fd0fee2009-12-17 07:12:23 -08001048 /**
1049 * @return state of the volume at the specified mount point
1050 */
San Mehat4270e1e2010-01-29 05:32:19 -08001051 public String getVolumeState(String mountPoint) {
San Mehat7fd0fee2009-12-17 07:12:23 -08001052 /*
1053 * XXX: Until we have multiple volume discovery, just hardwire
1054 * this to /sdcard
1055 */
1056 if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -07001057 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
San Mehat7fd0fee2009-12-17 07:12:23 -08001058 throw new IllegalArgumentException();
1059 }
1060
1061 return mLegacyState;
1062 }
1063
San Mehat4270e1e2010-01-29 05:32:19 -08001064 public int mountVolume(String path) {
1065 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001066
San Mehat207e5382010-02-04 20:46:54 -08001067 waitForReady();
1068 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
1070
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001071 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001072 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001073 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001075 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001076 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001077 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1078 Environment.MEDIA_REMOVED.equals(volState) ||
1079 Environment.MEDIA_SHARED.equals(volState) ||
1080 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1081 // Media already unmounted or cannot be unmounted.
1082 // TODO return valid return code when adding observer call back.
1083 return;
1084 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001085 UnmountCallBack ucb = new UnmountCallBack(path, force);
1086 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088
San Mehat4270e1e2010-01-29 05:32:19 -08001089 public int formatVolume(String path) {
1090 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001091 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001092
San Mehat207e5382010-02-04 20:46:54 -08001093 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
1095
San Mehatc1b4ce92010-02-16 17:13:03 -08001096 public int []getStorageUsers(String path) {
1097 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1098 waitForReady();
1099 try {
1100 String[] r = mConnector.doListCommand(
1101 String.format("storage users %s", path),
1102 VoldResponseCode.StorageUsersListResult);
1103 // FMT: <pid> <process name>
1104 int[] data = new int[r.length];
1105 for (int i = 0; i < r.length; i++) {
1106 String []tok = r[i].split(" ");
1107 try {
1108 data[i] = Integer.parseInt(tok[0]);
1109 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001110 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001111 return new int[0];
1112 }
1113 }
1114 return data;
1115 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001116 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001117 return new int[0];
1118 }
1119 }
1120
San Mehatb1043402010-02-05 08:26:50 -08001121 private void warnOnNotMounted() {
1122 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001123 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001124 }
1125 }
1126
San Mehat4270e1e2010-01-29 05:32:19 -08001127 public String[] getSecureContainerList() {
1128 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001129 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001130 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001131
San Mehat4270e1e2010-01-29 05:32:19 -08001132 try {
1133 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1134 } catch (NativeDaemonConnectorException e) {
1135 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 }
1137 }
San Mehat36972292010-01-06 11:06:32 -08001138
San Mehat4270e1e2010-01-29 05:32:19 -08001139 public int createSecureContainer(String id, int sizeMb, String fstype,
1140 String key, int ownerUid) {
1141 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001142 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001143 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001144
San Mehatb1043402010-02-05 08:26:50 -08001145 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001146 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1147 try {
1148 mConnector.doCommand(cmd);
1149 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001150 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001151 }
San Mehata181b212010-02-11 06:50:20 -08001152
1153 if (rc == StorageResultCode.OperationSucceeded) {
1154 synchronized (mAsecMountSet) {
1155 mAsecMountSet.add(id);
1156 }
1157 }
San Mehat4270e1e2010-01-29 05:32:19 -08001158 return rc;
San Mehat36972292010-01-06 11:06:32 -08001159 }
1160
San Mehat4270e1e2010-01-29 05:32:19 -08001161 public int finalizeSecureContainer(String id) {
1162 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001163 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001164
San Mehatb1043402010-02-05 08:26:50 -08001165 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001166 try {
1167 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001168 /*
1169 * Finalization does a remount, so no need
1170 * to update mAsecMountSet
1171 */
San Mehat4270e1e2010-01-29 05:32:19 -08001172 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001173 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001174 }
San Mehat4270e1e2010-01-29 05:32:19 -08001175 return rc;
San Mehat36972292010-01-06 11:06:32 -08001176 }
1177
San Mehatd9709982010-02-18 11:43:03 -08001178 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001179 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001180 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001181 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001182
San Mehatb1043402010-02-05 08:26:50 -08001183 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001184 try {
San Mehatd9709982010-02-18 11:43:03 -08001185 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001186 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001187 int code = e.getCode();
1188 if (code == VoldResponseCode.OpFailedStorageBusy) {
1189 rc = StorageResultCode.OperationFailedStorageBusy;
1190 } else {
1191 rc = StorageResultCode.OperationFailedInternalError;
1192 }
San Mehat02735bc2010-01-26 15:18:08 -08001193 }
San Mehata181b212010-02-11 06:50:20 -08001194
1195 if (rc == StorageResultCode.OperationSucceeded) {
1196 synchronized (mAsecMountSet) {
1197 if (mAsecMountSet.contains(id)) {
1198 mAsecMountSet.remove(id);
1199 }
1200 }
1201 }
1202
San Mehat4270e1e2010-01-29 05:32:19 -08001203 return rc;
San Mehat36972292010-01-06 11:06:32 -08001204 }
1205
San Mehat4270e1e2010-01-29 05:32:19 -08001206 public int mountSecureContainer(String id, String key, int ownerUid) {
1207 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001208 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001209 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001210
San Mehata181b212010-02-11 06:50:20 -08001211 synchronized (mAsecMountSet) {
1212 if (mAsecMountSet.contains(id)) {
1213 return StorageResultCode.OperationFailedStorageMounted;
1214 }
1215 }
1216
San Mehatb1043402010-02-05 08:26:50 -08001217 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001218 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1219 try {
1220 mConnector.doCommand(cmd);
1221 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001222 int code = e.getCode();
1223 if (code != VoldResponseCode.OpFailedStorageBusy) {
1224 rc = StorageResultCode.OperationFailedInternalError;
1225 }
San Mehat02735bc2010-01-26 15:18:08 -08001226 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001227
1228 if (rc == StorageResultCode.OperationSucceeded) {
1229 synchronized (mAsecMountSet) {
1230 mAsecMountSet.add(id);
1231 }
1232 }
San Mehat4270e1e2010-01-29 05:32:19 -08001233 return rc;
San Mehat36972292010-01-06 11:06:32 -08001234 }
1235
San Mehatd9709982010-02-18 11:43:03 -08001236 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001237 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001238 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001239 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001240
San Mehat6cdd9c02010-02-09 14:45:20 -08001241 synchronized (mAsecMountSet) {
1242 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001243 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001244 }
1245 }
1246
San Mehatb1043402010-02-05 08:26:50 -08001247 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001248 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001249 try {
1250 mConnector.doCommand(cmd);
1251 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001252 int code = e.getCode();
1253 if (code == VoldResponseCode.OpFailedStorageBusy) {
1254 rc = StorageResultCode.OperationFailedStorageBusy;
1255 } else {
1256 rc = StorageResultCode.OperationFailedInternalError;
1257 }
San Mehat02735bc2010-01-26 15:18:08 -08001258 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001259
1260 if (rc == StorageResultCode.OperationSucceeded) {
1261 synchronized (mAsecMountSet) {
1262 mAsecMountSet.remove(id);
1263 }
1264 }
San Mehat4270e1e2010-01-29 05:32:19 -08001265 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001266 }
1267
San Mehat6cdd9c02010-02-09 14:45:20 -08001268 public boolean isSecureContainerMounted(String id) {
1269 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1270 waitForReady();
1271 warnOnNotMounted();
1272
1273 synchronized (mAsecMountSet) {
1274 return mAsecMountSet.contains(id);
1275 }
1276 }
1277
San Mehat4270e1e2010-01-29 05:32:19 -08001278 public int renameSecureContainer(String oldId, String newId) {
1279 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001280 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001281 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001282
San Mehata181b212010-02-11 06:50:20 -08001283 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001284 /*
1285 * Because a mounted container has active internal state which cannot be
1286 * changed while active, we must ensure both ids are not currently mounted.
1287 */
1288 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001289 return StorageResultCode.OperationFailedStorageMounted;
1290 }
1291 }
1292
San Mehatb1043402010-02-05 08:26:50 -08001293 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001294 String cmd = String.format("asec rename %s %s", oldId, newId);
1295 try {
1296 mConnector.doCommand(cmd);
1297 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001298 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001299 }
San Mehata181b212010-02-11 06:50:20 -08001300
San Mehat4270e1e2010-01-29 05:32:19 -08001301 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001302 }
1303
San Mehat4270e1e2010-01-29 05:32:19 -08001304 public String getSecureContainerPath(String id) {
1305 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001306 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001307 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001308
San Mehat2d66cef2010-03-23 11:12:52 -07001309 try {
1310 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1311 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001312 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001313 if (code != VoldResponseCode.AsecPathResult) {
1314 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1315 }
1316 return tok[1];
1317 } catch (NativeDaemonConnectorException e) {
1318 int code = e.getCode();
1319 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1320 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001321 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001322 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001323 }
1324 }
San Mehat22dd86e2010-01-12 12:21:18 -08001325 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001326
1327 public void finishMediaUpdate() {
1328 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330}
1331