blob: 6e8b42e8ca4317c9e8218b5f9e25c9d2d1964b0f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Kenny Roota02b8b02010-08-05 16:14:17 -070019import com.android.internal.app.IMediaContainerService;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080020import com.android.server.am.ActivityManagerService;
21
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.BroadcastReceiver;
Kenny Roota02b8b02010-08-05 16:14:17 -070023import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
Kenny Roota02b8b02010-08-05 16:14:17 -070027import android.content.ServiceConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.pm.PackageManager;
Kenny Root02c87302010-07-01 08:10:18 -070029import android.content.res.ObbInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.net.Uri;
Kenny Root02c87302010-07-01 08:10:18 -070031import android.os.Binder;
Kenny Roota02b8b02010-08-05 16:14:17 -070032import android.os.Environment;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080033import android.os.Handler;
Daniel Sandler5f27ef42010-03-16 15:42:02 -040034import android.os.HandlerThread;
Kenny Roota02b8b02010-08-05 16:14:17 -070035import android.os.IBinder;
Daniel Sandler5f27ef42010-03-16 15:42:02 -040036import android.os.Looper;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080037import android.os.Message;
San Mehat4270e1e2010-01-29 05:32:19 -080038import android.os.RemoteException;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -080039import android.os.ServiceManager;
San Mehat207e5382010-02-04 20:46:54 -080040import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.SystemProperties;
Kenny Roota02b8b02010-08-05 16:14:17 -070042import android.os.storage.IMountService;
43import android.os.storage.IMountServiceListener;
44import android.os.storage.IMountShutdownObserver;
45import android.os.storage.IObbActionListener;
46import android.os.storage.StorageResultCode;
San Mehata5078592010-03-25 09:36:54 -070047import android.util.Slog;
Kenny Roota02b8b02010-08-05 16:14:17 -070048
San Mehat22dd86e2010-01-12 12:21:18 -080049import java.util.ArrayList;
Kenny Roota02b8b02010-08-05 16:14:17 -070050import java.util.HashMap;
San Mehat6cdd9c02010-02-09 14:45:20 -080051import java.util.HashSet;
Kenny Roota02b8b02010-08-05 16:14:17 -070052import java.util.LinkedList;
53import java.util.List;
54import java.util.Map;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056/**
San Mehatb1043402010-02-05 08:26:50 -080057 * MountService implements back-end services for platform storage
58 * management.
59 * @hide - Applications should use android.os.storage.StorageManager
60 * to access the MountService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 */
San Mehat22dd86e2010-01-12 12:21:18 -080062class MountService extends IMountService.Stub
63 implements INativeDaemonConnectorCallbacks {
San Mehatb1043402010-02-05 08:26:50 -080064 private static final boolean LOCAL_LOGD = false;
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -080065 private static final boolean DEBUG_UNMOUNT = false;
66 private static final boolean DEBUG_EVENTS = false;
Kenny Root02c87302010-07-01 08:10:18 -070067 private static final boolean DEBUG_OBB = true;
68
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 private static final String TAG = "MountService";
70
Kenny Root305bcbf2010-09-03 07:56:38 -070071 private static final String VOLD_TAG = "VoldConnector";
72
San Mehat4270e1e2010-01-29 05:32:19 -080073 /*
74 * Internal vold volume state constants
75 */
San Mehat7fd0fee2009-12-17 07:12:23 -080076 class VolumeState {
77 public static final int Init = -1;
78 public static final int NoMedia = 0;
79 public static final int Idle = 1;
80 public static final int Pending = 2;
81 public static final int Checking = 3;
82 public static final int Mounted = 4;
83 public static final int Unmounting = 5;
84 public static final int Formatting = 6;
85 public static final int Shared = 7;
86 public static final int SharedMnt = 8;
87 }
88
San Mehat4270e1e2010-01-29 05:32:19 -080089 /*
90 * Internal vold response code constants
91 */
San Mehat22dd86e2010-01-12 12:21:18 -080092 class VoldResponseCode {
San Mehat4270e1e2010-01-29 05:32:19 -080093 /*
94 * 100 series - Requestion action was initiated; expect another reply
95 * before proceeding with a new command.
96 */
San Mehat22dd86e2010-01-12 12:21:18 -080097 public static final int VolumeListResult = 110;
98 public static final int AsecListResult = 111;
San Mehatc1b4ce92010-02-16 17:13:03 -080099 public static final int StorageUsersListResult = 112;
San Mehat22dd86e2010-01-12 12:21:18 -0800100
San Mehat4270e1e2010-01-29 05:32:19 -0800101 /*
102 * 200 series - Requestion action has been successfully completed.
103 */
104 public static final int ShareStatusResult = 210;
San Mehat22dd86e2010-01-12 12:21:18 -0800105 public static final int AsecPathResult = 211;
San Mehat4270e1e2010-01-29 05:32:19 -0800106 public static final int ShareEnabledResult = 212;
San Mehat22dd86e2010-01-12 12:21:18 -0800107
San Mehat4270e1e2010-01-29 05:32:19 -0800108 /*
109 * 400 series - Command was accepted, but the requested action
110 * did not take place.
111 */
112 public static final int OpFailedNoMedia = 401;
113 public static final int OpFailedMediaBlank = 402;
114 public static final int OpFailedMediaCorrupt = 403;
115 public static final int OpFailedVolNotMounted = 404;
San Mehatd9709982010-02-18 11:43:03 -0800116 public static final int OpFailedStorageBusy = 405;
San Mehat2d66cef2010-03-23 11:12:52 -0700117 public static final int OpFailedStorageNotFound = 406;
San Mehat4270e1e2010-01-29 05:32:19 -0800118
119 /*
120 * 600 series - Unsolicited broadcasts.
121 */
San Mehat22dd86e2010-01-12 12:21:18 -0800122 public static final int VolumeStateChange = 605;
San Mehat22dd86e2010-01-12 12:21:18 -0800123 public static final int ShareAvailabilityChange = 620;
124 public static final int VolumeDiskInserted = 630;
125 public static final int VolumeDiskRemoved = 631;
126 public static final int VolumeBadRemoval = 632;
127 }
128
San Mehat4270e1e2010-01-29 05:32:19 -0800129 private Context mContext;
130 private NativeDaemonConnector mConnector;
131 private String mLegacyState = Environment.MEDIA_REMOVED;
132 private PackageManagerService mPms;
133 private boolean mUmsEnabling;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800134 // Used as a lock for methods that register/unregister listeners.
135 final private ArrayList<MountServiceBinderListener> mListeners =
136 new ArrayList<MountServiceBinderListener>();
San Mehat6a965af22010-02-24 17:47:30 -0800137 private boolean mBooted = false;
138 private boolean mReady = false;
139 private boolean mSendUmsConnectedOnBoot = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800140
San Mehat6cdd9c02010-02-09 14:45:20 -0800141 /**
142 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800143 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800144 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800145 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800146
Kenny Root02c87302010-07-01 08:10:18 -0700147 /**
Kenny Roota02b8b02010-08-05 16:14:17 -0700148 * Mounted OBB tracking information. Used to track the current state of all
149 * OBBs.
Kenny Root02c87302010-07-01 08:10:18 -0700150 */
Kenny Roota02b8b02010-08-05 16:14:17 -0700151 final private Map<IObbActionListener, ObbState> mObbMounts = new HashMap<IObbActionListener, ObbState>();
152 final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>();
153
154 class ObbState implements IBinder.DeathRecipient {
155 public ObbState(String filename, IObbActionListener token, int callerUid) {
156 this.filename = filename;
157 this.token = token;
158 this.callerUid = callerUid;
159 mounted = false;
160 }
161
162 // OBB source filename
163 String filename;
164
165 // Token of remote Binder caller
166 IObbActionListener token;
167
168 // Binder.callingUid()
169 public int callerUid;
170
171 // Whether this is mounted currently.
172 boolean mounted;
173
174 @Override
175 public void binderDied() {
176 ObbAction action = new UnmountObbAction(this, true);
177 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
178
179 removeObbState(this);
180
181 token.asBinder().unlinkToDeath(this, 0);
182 }
183 }
184
185 // OBB Action Handler
186 final private ObbActionHandler mObbActionHandler;
187
188 // OBB action handler messages
189 private static final int OBB_RUN_ACTION = 1;
190 private static final int OBB_MCS_BOUND = 2;
191 private static final int OBB_MCS_UNBIND = 3;
192 private static final int OBB_MCS_RECONNECT = 4;
193 private static final int OBB_MCS_GIVE_UP = 5;
194
195 /*
196 * Default Container Service information
197 */
198 static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
199 "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService");
200
201 final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection();
202
203 class DefaultContainerConnection implements ServiceConnection {
204 public void onServiceConnected(ComponentName name, IBinder service) {
205 if (DEBUG_OBB)
206 Slog.i(TAG, "onServiceConnected");
207 IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service);
208 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs));
209 }
210
211 public void onServiceDisconnected(ComponentName name) {
212 if (DEBUG_OBB)
213 Slog.i(TAG, "onServiceDisconnected");
214 }
215 };
216
217 // Used in the ObbActionHandler
218 private IMediaContainerService mContainerService = null;
Kenny Root02c87302010-07-01 08:10:18 -0700219
220 // Handler messages
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800221 private static final int H_UNMOUNT_PM_UPDATE = 1;
222 private static final int H_UNMOUNT_PM_DONE = 2;
223 private static final int H_UNMOUNT_MS = 3;
224 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
225 private static final int MAX_UNMOUNT_RETRIES = 4;
226
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800227 class UnmountCallBack {
228 String path;
229 int retries;
230 boolean force;
231
232 UnmountCallBack(String path, boolean force) {
233 retries = 0;
234 this.path = path;
235 this.force = force;
236 }
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800237
238 void handleFinished() {
San Mehata5078592010-03-25 09:36:54 -0700239 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path);
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800240 doUnmountVolume(path, true);
241 }
242 }
243
244 class UmsEnableCallBack extends UnmountCallBack {
245 String method;
246
247 UmsEnableCallBack(String path, String method, boolean force) {
248 super(path, force);
249 this.method = method;
250 }
251
252 @Override
253 void handleFinished() {
254 super.handleFinished();
255 doShareUnshareVolume(path, method, true);
256 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800257 }
258
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800259 class ShutdownCallBack extends UnmountCallBack {
260 IMountShutdownObserver observer;
261 ShutdownCallBack(String path, IMountShutdownObserver observer) {
262 super(path, true);
263 this.observer = observer;
264 }
265
266 @Override
267 void handleFinished() {
268 int ret = doUnmountVolume(path, true);
269 if (observer != null) {
270 try {
271 observer.onShutDownComplete(ret);
272 } catch (RemoteException e) {
San Mehata5078592010-03-25 09:36:54 -0700273 Slog.w(TAG, "RemoteException when shutting down");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800274 }
275 }
276 }
277 }
278
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400279 class MountServiceHandler extends Handler {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800280 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700281 boolean mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800282
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400283 MountServiceHandler(Looper l) {
284 super(l);
285 }
286
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800287 public void handleMessage(Message msg) {
288 switch (msg.what) {
289 case H_UNMOUNT_PM_UPDATE: {
San Mehata5078592010-03-25 09:36:54 -0700290 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800291 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
292 mForceUnmounts.add(ucb);
San Mehata5078592010-03-25 09:36:54 -0700293 if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800294 // Register only if needed.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700295 if (!mUpdatingStatus) {
San Mehata5078592010-03-25 09:36:54 -0700296 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700297 mUpdatingStatus = true;
298 mPms.updateExternalMediaStatus(false, true);
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800299 }
300 break;
301 }
302 case H_UNMOUNT_PM_DONE: {
San Mehata5078592010-03-25 09:36:54 -0700303 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE");
San Mehata5078592010-03-25 09:36:54 -0700304 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700305 mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800306 int size = mForceUnmounts.size();
307 int sizeArr[] = new int[size];
308 int sizeArrN = 0;
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700309 // Kill processes holding references first
310 ActivityManagerService ams = (ActivityManagerService)
311 ServiceManager.getService("activity");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800312 for (int i = 0; i < size; i++) {
313 UnmountCallBack ucb = mForceUnmounts.get(i);
314 String path = ucb.path;
315 boolean done = false;
316 if (!ucb.force) {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800317 done = true;
318 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800319 int pids[] = getStorageUsers(path);
320 if (pids == null || pids.length == 0) {
321 done = true;
322 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800323 // Eliminate system process here?
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700324 ams.killPids(pids, "unmount media");
325 // Confirm if file references have been freed.
326 pids = getStorageUsers(path);
327 if (pids == null || pids.length == 0) {
328 done = true;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800329 }
330 }
331 }
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700332 if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) {
333 // Retry again
334 Slog.i(TAG, "Retrying to kill storage users again");
335 mHandler.sendMessageDelayed(
336 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
337 ucb.retries++),
338 RETRY_UNMOUNT_DELAY);
339 } else {
340 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
341 Slog.i(TAG, "Failed to unmount media inspite of " +
342 MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now");
343 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800344 sizeArr[sizeArrN++] = i;
345 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
346 ucb));
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800347 }
348 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800349 // Remove already processed elements from list.
350 for (int i = (sizeArrN-1); i >= 0; i--) {
351 mForceUnmounts.remove(sizeArr[i]);
352 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800353 break;
354 }
355 case H_UNMOUNT_MS : {
San Mehata5078592010-03-25 09:36:54 -0700356 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800357 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800358 ucb.handleFinished();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800359 break;
360 }
361 }
362 }
363 };
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400364 final private HandlerThread mHandlerThread;
365 final private Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800366
San Mehat207e5382010-02-04 20:46:54 -0800367 private void waitForReady() {
368 while (mReady == false) {
369 for (int retries = 5; retries > 0; retries--) {
370 if (mReady) {
371 return;
372 }
373 SystemClock.sleep(1000);
374 }
San Mehata5078592010-03-25 09:36:54 -0700375 Slog.w(TAG, "Waiting too long for mReady!");
San Mehat207e5382010-02-04 20:46:54 -0800376 }
San Mehat1f6301e2010-01-07 22:40:27 -0800377 }
Kenny Root02c87302010-07-01 08:10:18 -0700378
San Mehat207e5382010-02-04 20:46:54 -0800379 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 public void onReceive(Context context, Intent intent) {
San Mehat91c77612010-01-07 10:39:41 -0800381 String action = intent.getAction();
382
383 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
San Mehat207e5382010-02-04 20:46:54 -0800384 mBooted = true;
San Mehat22dd86e2010-01-12 12:21:18 -0800385
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800386 /*
387 * In the simulator, we need to broadcast a volume mounted event
388 * to make the media scanner run.
389 */
390 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
391 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted);
392 return;
393 }
San Mehatfafb0412010-02-18 19:40:04 -0800394 new Thread() {
395 public void run() {
396 try {
397 String path = Environment.getExternalStorageDirectory().getPath();
San Mehat6a254402010-03-22 10:21:00 -0700398 String state = getVolumeState(path);
399
400 if (state.equals(Environment.MEDIA_UNMOUNTED)) {
San Mehatfafb0412010-02-18 19:40:04 -0800401 int rc = doMountVolume(path);
402 if (rc != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700403 Slog.e(TAG, String.format("Boot-time mount failed (%d)", rc));
San Mehatfafb0412010-02-18 19:40:04 -0800404 }
San Mehat6a254402010-03-22 10:21:00 -0700405 } else if (state.equals(Environment.MEDIA_SHARED)) {
406 /*
407 * Bootstrap UMS enabled state since vold indicates
408 * the volume is shared (runtime restart while ums enabled)
409 */
410 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Shared);
San Mehatfafb0412010-02-18 19:40:04 -0800411 }
San Mehat6a254402010-03-22 10:21:00 -0700412
San Mehat6a965af22010-02-24 17:47:30 -0800413 /*
San Mehat6a254402010-03-22 10:21:00 -0700414 * If UMS was connected on boot, send the connected event
San Mehat6a965af22010-02-24 17:47:30 -0800415 * now that we're up.
416 */
417 if (mSendUmsConnectedOnBoot) {
418 sendUmsIntent(true);
419 mSendUmsConnectedOnBoot = false;
420 }
San Mehatfafb0412010-02-18 19:40:04 -0800421 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700422 Slog.e(TAG, "Boot-time mount exception", ex);
San Mehatfafb0412010-02-18 19:40:04 -0800423 }
San Mehat207e5382010-02-04 20:46:54 -0800424 }
San Mehatfafb0412010-02-18 19:40:04 -0800425 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 }
427 }
428 };
429
San Mehat4270e1e2010-01-29 05:32:19 -0800430 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
431 final IMountServiceListener mListener;
432
433 MountServiceBinderListener(IMountServiceListener listener) {
434 mListener = listener;
Kenny Root02c87302010-07-01 08:10:18 -0700435
San Mehat91c77612010-01-07 10:39:41 -0800436 }
437
San Mehat4270e1e2010-01-29 05:32:19 -0800438 public void binderDied() {
San Mehata5078592010-03-25 09:36:54 -0700439 if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!");
Kenny Roota02b8b02010-08-05 16:14:17 -0700440 synchronized (mListeners) {
San Mehat4270e1e2010-01-29 05:32:19 -0800441 mListeners.remove(this);
442 mListener.asBinder().unlinkToDeath(this, 0);
443 }
444 }
445 }
446
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800447 private void doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800448 // TODO: Add support for multiple share methods
449 if (!method.equals("ums")) {
450 throw new IllegalArgumentException(String.format("Method %s not supported", method));
451 }
452
San Mehat4270e1e2010-01-29 05:32:19 -0800453 try {
454 mConnector.doCommand(String.format(
455 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
456 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -0700457 Slog.e(TAG, "Failed to share/unshare", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800458 }
San Mehat4270e1e2010-01-29 05:32:19 -0800459 }
460
San Mehat207e5382010-02-04 20:46:54 -0800461 private void updatePublicVolumeState(String path, String state) {
San Mehat4270e1e2010-01-29 05:32:19 -0800462 if (!path.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -0700463 Slog.w(TAG, "Multiple volumes not currently supported");
San Mehat4270e1e2010-01-29 05:32:19 -0800464 return;
465 }
San Mehatb1043402010-02-05 08:26:50 -0800466
467 if (mLegacyState.equals(state)) {
San Mehata5078592010-03-25 09:36:54 -0700468 Slog.w(TAG, String.format("Duplicate state transition (%s -> %s)", mLegacyState, state));
San Mehatb1043402010-02-05 08:26:50 -0800469 return;
470 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800471 // Update state on PackageManager
472 if (Environment.MEDIA_UNMOUNTED.equals(state)) {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700473 mPms.updateExternalMediaStatus(false, false);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800474 } else if (Environment.MEDIA_MOUNTED.equals(state)) {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700475 mPms.updateExternalMediaStatus(true, false);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800476 }
San Mehat4270e1e2010-01-29 05:32:19 -0800477 String oldState = mLegacyState;
478 mLegacyState = state;
479
480 synchronized (mListeners) {
481 for (int i = mListeners.size() -1; i >= 0; i--) {
482 MountServiceBinderListener bl = mListeners.get(i);
483 try {
San Mehatb1043402010-02-05 08:26:50 -0800484 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800485 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700486 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800487 mListeners.remove(i);
488 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700489 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800490 }
491 }
492 }
493 }
494
495 /**
496 *
497 * Callback from NativeDaemonConnector
498 */
499 public void onDaemonConnected() {
500 /*
501 * Since we'll be calling back into the NativeDaemonConnector,
502 * we need to do our work in a new thread.
503 */
504 new Thread() {
505 public void run() {
506 /**
507 * Determine media state and UMS detection status
508 */
509 String path = Environment.getExternalStorageDirectory().getPath();
510 String state = Environment.MEDIA_REMOVED;
511
512 try {
513 String[] vols = mConnector.doListCommand(
514 "volume list", VoldResponseCode.VolumeListResult);
515 for (String volstr : vols) {
516 String[] tok = volstr.split(" ");
517 // FMT: <label> <mountpoint> <state>
518 if (!tok[1].equals(path)) {
San Mehata5078592010-03-25 09:36:54 -0700519 Slog.w(TAG, String.format(
San Mehat4270e1e2010-01-29 05:32:19 -0800520 "Skipping unknown volume '%s'",tok[1]));
521 continue;
522 }
523 int st = Integer.parseInt(tok[2]);
524 if (st == VolumeState.NoMedia) {
525 state = Environment.MEDIA_REMOVED;
526 } else if (st == VolumeState.Idle) {
San Mehat207e5382010-02-04 20:46:54 -0800527 state = Environment.MEDIA_UNMOUNTED;
San Mehat4270e1e2010-01-29 05:32:19 -0800528 } else if (st == VolumeState.Mounted) {
529 state = Environment.MEDIA_MOUNTED;
San Mehata5078592010-03-25 09:36:54 -0700530 Slog.i(TAG, "Media already mounted on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800531 } else if (st == VolumeState.Shared) {
532 state = Environment.MEDIA_SHARED;
San Mehata5078592010-03-25 09:36:54 -0700533 Slog.i(TAG, "Media shared on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800534 } else {
535 throw new Exception(String.format("Unexpected state %d", st));
536 }
537 }
538 if (state != null) {
San Mehata5078592010-03-25 09:36:54 -0700539 if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
San Mehat4270e1e2010-01-29 05:32:19 -0800540 updatePublicVolumeState(path, state);
541 }
542 } catch (Exception e) {
San Mehata5078592010-03-25 09:36:54 -0700543 Slog.e(TAG, "Error processing initial volume state", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800544 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
545 }
546
547 try {
San Mehat207e5382010-02-04 20:46:54 -0800548 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800549 notifyShareAvailabilityChange("ums", avail);
550 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700551 Slog.w(TAG, "Failed to get share availability");
San Mehat4270e1e2010-01-29 05:32:19 -0800552 }
San Mehat207e5382010-02-04 20:46:54 -0800553 /*
554 * Now that we've done our initialization, release
555 * the hounds!
556 */
557 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800558 }
559 }.start();
560 }
561
562 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800563 * Callback from NativeDaemonConnector
564 */
565 public boolean onEvent(int code, String raw, String[] cooked) {
566 Intent in = null;
567
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800568 if (DEBUG_EVENTS) {
569 StringBuilder builder = new StringBuilder();
570 builder.append("onEvent::");
571 builder.append(" raw= " + raw);
572 if (cooked != null) {
573 builder.append(" cooked = " );
574 for (String str : cooked) {
575 builder.append(" " + str);
576 }
577 }
San Mehata5078592010-03-25 09:36:54 -0700578 Slog.i(TAG, builder.toString());
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800579 }
San Mehat4270e1e2010-01-29 05:32:19 -0800580 if (code == VoldResponseCode.VolumeStateChange) {
581 /*
582 * One of the volumes we're managing has changed state.
583 * Format: "NNN Volume <label> <path> state changed
584 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
585 */
586 notifyVolumeStateChange(
587 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
588 Integer.parseInt(cooked[10]));
589 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
590 // FMT: NNN Share method <method> now <available|unavailable>
591 boolean avail = false;
592 if (cooked[5].equals("available")) {
593 avail = true;
594 }
595 notifyShareAvailabilityChange(cooked[3], avail);
596 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
597 (code == VoldResponseCode.VolumeDiskRemoved) ||
598 (code == VoldResponseCode.VolumeBadRemoval)) {
599 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
600 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
601 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
602 final String label = cooked[2];
603 final String path = cooked[3];
604 int major = -1;
605 int minor = -1;
606
607 try {
608 String devComp = cooked[6].substring(1, cooked[6].length() -1);
609 String[] devTok = devComp.split(":");
610 major = Integer.parseInt(devTok[0]);
611 minor = Integer.parseInt(devTok[1]);
612 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700613 Slog.e(TAG, "Failed to parse major/minor", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800614 }
615
San Mehat4270e1e2010-01-29 05:32:19 -0800616 if (code == VoldResponseCode.VolumeDiskInserted) {
617 new Thread() {
618 public void run() {
619 try {
620 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800621 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700622 Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
San Mehat4270e1e2010-01-29 05:32:19 -0800623 }
624 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700625 Slog.w(TAG, "Failed to mount media on insertion", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800626 }
627 }
628 }.start();
629 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
630 /*
631 * This event gets trumped if we're already in BAD_REMOVAL state
632 */
633 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
634 return true;
635 }
636 /* Send the media unmounted event first */
San Mehata5078592010-03-25 09:36:54 -0700637 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800638 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
639 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
640 mContext.sendBroadcast(in);
641
San Mehata5078592010-03-25 09:36:54 -0700642 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
San Mehat4270e1e2010-01-29 05:32:19 -0800643 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
644 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
645 } else if (code == VoldResponseCode.VolumeBadRemoval) {
San Mehata5078592010-03-25 09:36:54 -0700646 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800647 /* Send the media unmounted event first */
648 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
649 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
650 mContext.sendBroadcast(in);
651
San Mehata5078592010-03-25 09:36:54 -0700652 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
San Mehat4270e1e2010-01-29 05:32:19 -0800653 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
654 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
655 } else {
San Mehata5078592010-03-25 09:36:54 -0700656 Slog.e(TAG, String.format("Unknown code {%d}", code));
San Mehat4270e1e2010-01-29 05:32:19 -0800657 }
658 } else {
659 return false;
660 }
661
662 if (in != null) {
663 mContext.sendBroadcast(in);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400664 }
665 return true;
San Mehat4270e1e2010-01-29 05:32:19 -0800666 }
667
San Mehat207e5382010-02-04 20:46:54 -0800668 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800669 String vs = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -0700670 if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
San Mehat4270e1e2010-01-29 05:32:19 -0800671
672 Intent in = null;
673
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500674 if (oldState == VolumeState.Shared && newState != oldState) {
San Mehata5078592010-03-25 09:36:54 -0700675 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500676 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
677 Uri.parse("file://" + path)));
678 }
679
San Mehat4270e1e2010-01-29 05:32:19 -0800680 if (newState == VolumeState.Init) {
681 } else if (newState == VolumeState.NoMedia) {
682 // NoMedia is handled via Disk Remove events
683 } else if (newState == VolumeState.Idle) {
684 /*
685 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
686 * if we're in the process of enabling UMS
687 */
688 if (!vs.equals(
689 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
690 Environment.MEDIA_NOFS) && !vs.equals(
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800691 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
San Mehata5078592010-03-25 09:36:54 -0700692 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
San Mehat4270e1e2010-01-29 05:32:19 -0800693 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
694 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
695 }
696 } else if (newState == VolumeState.Pending) {
697 } else if (newState == VolumeState.Checking) {
San Mehata5078592010-03-25 09:36:54 -0700698 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
San Mehat4270e1e2010-01-29 05:32:19 -0800699 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
700 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
701 } else if (newState == VolumeState.Mounted) {
San Mehata5078592010-03-25 09:36:54 -0700702 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800703 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
San Mehat4270e1e2010-01-29 05:32:19 -0800704 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
705 in.putExtra("read-only", false);
706 } else if (newState == VolumeState.Unmounting) {
San Mehat4270e1e2010-01-29 05:32:19 -0800707 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
708 } else if (newState == VolumeState.Formatting) {
709 } else if (newState == VolumeState.Shared) {
San Mehata5078592010-03-25 09:36:54 -0700710 if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800711 /* Send the media unmounted event first */
712 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
713 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
714 mContext.sendBroadcast(in);
715
San Mehata5078592010-03-25 09:36:54 -0700716 if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
San Mehat4270e1e2010-01-29 05:32:19 -0800717 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
718 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
San Mehata5078592010-03-25 09:36:54 -0700719 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
San Mehat4270e1e2010-01-29 05:32:19 -0800720 } else if (newState == VolumeState.SharedMnt) {
San Mehata5078592010-03-25 09:36:54 -0700721 Slog.e(TAG, "Live shared mounts not supported yet!");
San Mehat4270e1e2010-01-29 05:32:19 -0800722 return;
723 } else {
San Mehata5078592010-03-25 09:36:54 -0700724 Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800725 }
726
727 if (in != null) {
728 mContext.sendBroadcast(in);
729 }
730 }
731
San Mehat207e5382010-02-04 20:46:54 -0800732 private boolean doGetShareMethodAvailable(String method) {
Kenny Root85fb2062010-06-01 20:50:21 -0700733 ArrayList<String> rsp;
Kenny Roota80ce062010-06-01 13:23:53 -0700734 try {
Kenny Root85fb2062010-06-01 20:50:21 -0700735 rsp = mConnector.doCommand("share status " + method);
Kenny Roota80ce062010-06-01 13:23:53 -0700736 } catch (NativeDaemonConnectorException ex) {
737 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
738 return false;
739 }
San Mehat207e5382010-02-04 20:46:54 -0800740
741 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700742 String[] tok = line.split(" ");
743 if (tok.length < 3) {
744 Slog.e(TAG, "Malformed response to share status " + method);
745 return false;
746 }
747
San Mehat207e5382010-02-04 20:46:54 -0800748 int code;
749 try {
750 code = Integer.parseInt(tok[0]);
751 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700752 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehat207e5382010-02-04 20:46:54 -0800753 return false;
754 }
755 if (code == VoldResponseCode.ShareStatusResult) {
756 if (tok[2].equals("available"))
757 return true;
758 return false;
759 } else {
San Mehata5078592010-03-25 09:36:54 -0700760 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehat207e5382010-02-04 20:46:54 -0800761 return false;
762 }
763 }
San Mehata5078592010-03-25 09:36:54 -0700764 Slog.e(TAG, "Got an empty response");
San Mehat207e5382010-02-04 20:46:54 -0800765 return false;
766 }
767
768 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800769 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800770
San Mehata5078592010-03-25 09:36:54 -0700771 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
San Mehat207e5382010-02-04 20:46:54 -0800772 try {
773 mConnector.doCommand(String.format("volume mount %s", path));
774 } catch (NativeDaemonConnectorException e) {
775 /*
776 * Mount failed for some reason
777 */
778 Intent in = null;
779 int code = e.getCode();
780 if (code == VoldResponseCode.OpFailedNoMedia) {
781 /*
782 * Attempt to mount but no media inserted
783 */
San Mehatb1043402010-02-05 08:26:50 -0800784 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800785 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
San Mehata5078592010-03-25 09:36:54 -0700786 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
San Mehat207e5382010-02-04 20:46:54 -0800787 /*
788 * Media is blank or does not contain a supported filesystem
789 */
790 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
791 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800792 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800793 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehata5078592010-03-25 09:36:54 -0700794 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
San Mehat207e5382010-02-04 20:46:54 -0800795 /*
796 * Volume consistency check failed
797 */
798 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
799 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800800 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800801 } else {
San Mehatb1043402010-02-05 08:26:50 -0800802 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800803 }
804
805 /*
806 * Send broadcast intent (if required for the failure)
807 */
808 if (in != null) {
809 mContext.sendBroadcast(in);
810 }
811 }
812
813 return rc;
814 }
815
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800816 /*
817 * If force is not set, we do not unmount if there are
818 * processes holding references to the volume about to be unmounted.
819 * If force is set, all the processes holding references need to be
820 * killed via the ActivityManager before actually unmounting the volume.
821 * This might even take a while and might be retried after timed delays
822 * to make sure we dont end up in an instable state and kill some core
823 * processes.
824 */
San Mehatd9709982010-02-18 11:43:03 -0800825 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800826 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800827 return VoldResponseCode.OpFailedVolNotMounted;
828 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800829 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700830 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800831 try {
San Mehatd9709982010-02-18 11:43:03 -0800832 mConnector.doCommand(String.format(
833 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700834 // We unmounted the volume. None of the asec containers are available now.
835 synchronized (mAsecMountSet) {
836 mAsecMountSet.clear();
837 }
San Mehatb1043402010-02-05 08:26:50 -0800838 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800839 } catch (NativeDaemonConnectorException e) {
840 // Don't worry about mismatch in PackageManager since the
841 // call back will handle the status changes any way.
842 int code = e.getCode();
843 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800844 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800845 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
846 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800847 } else {
San Mehatb1043402010-02-05 08:26:50 -0800848 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800849 }
850 }
851 }
852
853 private int doFormatVolume(String path) {
854 try {
855 String cmd = String.format("volume format %s", path);
856 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800857 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800858 } catch (NativeDaemonConnectorException e) {
859 int code = e.getCode();
860 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800861 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800862 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800863 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800864 } else {
San Mehatb1043402010-02-05 08:26:50 -0800865 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800866 }
867 }
868 }
869
San Mehatb1043402010-02-05 08:26:50 -0800870 private boolean doGetVolumeShared(String path, String method) {
871 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700872 ArrayList<String> rsp;
873
874 try {
875 rsp = mConnector.doCommand(cmd);
876 } catch (NativeDaemonConnectorException ex) {
877 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
878 return false;
879 }
San Mehatb1043402010-02-05 08:26:50 -0800880
881 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700882 String[] tok = line.split(" ");
883 if (tok.length < 3) {
884 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
885 return false;
886 }
887
San Mehatb1043402010-02-05 08:26:50 -0800888 int code;
889 try {
890 code = Integer.parseInt(tok[0]);
891 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700892 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800893 return false;
894 }
895 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700896 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800897 } else {
San Mehata5078592010-03-25 09:36:54 -0700898 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800899 return false;
900 }
901 }
San Mehata5078592010-03-25 09:36:54 -0700902 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800903 return false;
904 }
905
San Mehat207e5382010-02-04 20:46:54 -0800906 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800907 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -0700908 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800909 return;
910 }
911
912 synchronized (mListeners) {
913 for (int i = mListeners.size() -1; i >= 0; i--) {
914 MountServiceBinderListener bl = mListeners.get(i);
915 try {
San Mehatb1043402010-02-05 08:26:50 -0800916 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -0800917 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700918 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800919 mListeners.remove(i);
920 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700921 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800922 }
923 }
924 }
925
San Mehat207e5382010-02-04 20:46:54 -0800926 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -0800927 sendUmsIntent(avail);
928 } else {
929 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -0800930 }
San Mehat2fe718a2010-03-11 12:01:49 -0800931
932 final String path = Environment.getExternalStorageDirectory().getPath();
933 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
934 /*
935 * USB mass storage disconnected while enabled
936 */
937 new Thread() {
938 public void run() {
939 try {
940 int rc;
San Mehata5078592010-03-25 09:36:54 -0700941 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -0800942 doShareUnshareVolume(path, "ums", false);
943 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700944 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -0800945 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
946 path, rc));
947 }
948 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700949 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -0800950 }
951 }
952 }.start();
953 }
San Mehat4270e1e2010-01-29 05:32:19 -0800954 }
955
San Mehat6a965af22010-02-24 17:47:30 -0800956 private void sendUmsIntent(boolean c) {
957 mContext.sendBroadcast(
958 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
959 }
960
San Mehat207e5382010-02-04 20:46:54 -0800961 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -0800962 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
963 throw new SecurityException(String.format("Requires %s permission", perm));
964 }
965 }
966
967 /**
San Mehat207e5382010-02-04 20:46:54 -0800968 * Constructs a new MountService instance
969 *
970 * @param context Binder context for this service
971 */
972 public MountService(Context context) {
973 mContext = context;
974
San Mehat207e5382010-02-04 20:46:54 -0800975 // XXX: This will go away soon in favor of IMountServiceObserver
976 mPms = (PackageManagerService) ServiceManager.getService("package");
977
978 mContext.registerReceiver(mBroadcastReceiver,
979 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
980
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400981 mHandlerThread = new HandlerThread("MountService");
982 mHandlerThread.start();
983 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
984
Kenny Roota02b8b02010-08-05 16:14:17 -0700985 // Add OBB Action Handler to MountService thread.
986 mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper());
987
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800988 /*
989 * Vold does not run in the simulator, so pretend the connector thread
990 * ran and did its thing.
991 */
992 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
993 mReady = true;
994 mUmsEnabling = true;
995 return;
996 }
997
Kenny Root305bcbf2010-09-03 07:56:38 -0700998 /*
999 * Create the connection to vold with a maximum queue of twice the
1000 * amount of containers we'd ever expect to have. This keeps an
1001 * "asec list" from blocking a thread repeatedly.
1002 */
1003 mConnector = new NativeDaemonConnector(this, "vold",
1004 PackageManagerService.MAX_CONTAINERS * 2, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001005 mReady = false;
Kenny Root305bcbf2010-09-03 07:56:38 -07001006 Thread thread = new Thread(mConnector, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001007 thread.start();
1008 }
1009
1010 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001011 * Exposed API calls below here
1012 */
1013
1014 public void registerListener(IMountServiceListener listener) {
1015 synchronized (mListeners) {
1016 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
1017 try {
1018 listener.asBinder().linkToDeath(bl, 0);
1019 mListeners.add(bl);
1020 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001021 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -08001022 }
1023 }
1024 }
1025
1026 public void unregisterListener(IMountServiceListener listener) {
1027 synchronized (mListeners) {
1028 for(MountServiceBinderListener bl : mListeners) {
1029 if (bl.mListener == listener) {
1030 mListeners.remove(mListeners.indexOf(bl));
1031 return;
1032 }
1033 }
1034 }
1035 }
1036
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001037 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -08001038 validatePermission(android.Manifest.permission.SHUTDOWN);
1039
San Mehata5078592010-03-25 09:36:54 -07001040 Slog.i(TAG, "Shutting down");
San Mehat4270e1e2010-01-29 05:32:19 -08001041
1042 String path = Environment.getExternalStorageDirectory().getPath();
1043 String state = getVolumeState(path);
San Mehat91c77612010-01-07 10:39:41 -08001044
1045 if (state.equals(Environment.MEDIA_SHARED)) {
1046 /*
1047 * If the media is currently shared, unshare it.
1048 * XXX: This is still dangerous!. We should not
1049 * be rebooting at *all* if UMS is enabled, since
1050 * the UMS host could have dirty FAT cache entries
1051 * yet to flush.
1052 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001053 setUsbMassStorageEnabled(false);
San Mehat91c77612010-01-07 10:39:41 -08001054 } else if (state.equals(Environment.MEDIA_CHECKING)) {
1055 /*
1056 * If the media is being checked, then we need to wait for
1057 * it to complete before being able to proceed.
1058 */
1059 // XXX: @hackbod - Should we disable the ANR timer here?
1060 int retries = 30;
1061 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
1062 try {
1063 Thread.sleep(1000);
1064 } catch (InterruptedException iex) {
San Mehata5078592010-03-25 09:36:54 -07001065 Slog.e(TAG, "Interrupted while waiting for media", iex);
San Mehat91c77612010-01-07 10:39:41 -08001066 break;
1067 }
1068 state = Environment.getExternalStorageState();
1069 }
1070 if (retries == 0) {
San Mehata5078592010-03-25 09:36:54 -07001071 Slog.e(TAG, "Timed out waiting for media to check");
San Mehat91c77612010-01-07 10:39:41 -08001072 }
1073 }
1074
1075 if (state.equals(Environment.MEDIA_MOUNTED)) {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001076 // Post a unmount message.
1077 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
1078 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
San Mehat4270e1e2010-01-29 05:32:19 -08001079 }
1080 }
1081
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001082 private boolean getUmsEnabling() {
1083 synchronized (mListeners) {
1084 return mUmsEnabling;
1085 }
1086 }
1087
1088 private void setUmsEnabling(boolean enable) {
1089 synchronized (mListeners) {
1090 mUmsEnabling = true;
1091 }
1092 }
1093
San Mehatb1043402010-02-05 08:26:50 -08001094 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -08001095 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -08001096
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001097 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001098 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001099 }
San Mehatb1043402010-02-05 08:26:50 -08001100 return doGetShareMethodAvailable("ums");
1101 }
1102
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001103 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001104 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001105 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001106
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001107 // TODO: Add support for multiple share methods
1108
1109 /*
1110 * If the volume is mounted and we're enabling then unmount it
1111 */
1112 String path = Environment.getExternalStorageDirectory().getPath();
1113 String vs = getVolumeState(path);
1114 String method = "ums";
1115 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1116 // Override for isUsbMassStorageEnabled()
1117 setUmsEnabling(enable);
1118 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1119 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1120 // Clear override
1121 setUmsEnabling(false);
1122 }
1123 /*
1124 * If we disabled UMS then mount the volume
1125 */
1126 if (!enable) {
1127 doShareUnshareVolume(path, method, enable);
1128 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001129 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001130 " after disabling share method " + method);
1131 /*
1132 * Even though the mount failed, the unshare didn't so don't indicate an error.
1133 * The mountVolume() call will have set the storage state and sent the necessary
1134 * broadcasts.
1135 */
1136 }
1137 }
San Mehatb1043402010-02-05 08:26:50 -08001138 }
1139
1140 public boolean isUsbMassStorageEnabled() {
1141 waitForReady();
1142 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 }
San Mehat4270e1e2010-01-29 05:32:19 -08001144
San Mehat7fd0fee2009-12-17 07:12:23 -08001145 /**
1146 * @return state of the volume at the specified mount point
1147 */
San Mehat4270e1e2010-01-29 05:32:19 -08001148 public String getVolumeState(String mountPoint) {
San Mehat7fd0fee2009-12-17 07:12:23 -08001149 /*
1150 * XXX: Until we have multiple volume discovery, just hardwire
1151 * this to /sdcard
1152 */
1153 if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -07001154 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
San Mehat7fd0fee2009-12-17 07:12:23 -08001155 throw new IllegalArgumentException();
1156 }
1157
1158 return mLegacyState;
1159 }
1160
San Mehat4270e1e2010-01-29 05:32:19 -08001161 public int mountVolume(String path) {
1162 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001163
San Mehat207e5382010-02-04 20:46:54 -08001164 waitForReady();
1165 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 }
1167
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001168 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001169 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001170 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001172 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001173 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001174 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1175 Environment.MEDIA_REMOVED.equals(volState) ||
1176 Environment.MEDIA_SHARED.equals(volState) ||
1177 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1178 // Media already unmounted or cannot be unmounted.
1179 // TODO return valid return code when adding observer call back.
1180 return;
1181 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001182 UnmountCallBack ucb = new UnmountCallBack(path, force);
1183 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 }
1185
San Mehat4270e1e2010-01-29 05:32:19 -08001186 public int formatVolume(String path) {
1187 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001188 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001189
San Mehat207e5382010-02-04 20:46:54 -08001190 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192
San Mehatc1b4ce92010-02-16 17:13:03 -08001193 public int []getStorageUsers(String path) {
1194 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1195 waitForReady();
1196 try {
1197 String[] r = mConnector.doListCommand(
1198 String.format("storage users %s", path),
1199 VoldResponseCode.StorageUsersListResult);
1200 // FMT: <pid> <process name>
1201 int[] data = new int[r.length];
1202 for (int i = 0; i < r.length; i++) {
1203 String []tok = r[i].split(" ");
1204 try {
1205 data[i] = Integer.parseInt(tok[0]);
1206 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001207 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001208 return new int[0];
1209 }
1210 }
1211 return data;
1212 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001213 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001214 return new int[0];
1215 }
1216 }
1217
San Mehatb1043402010-02-05 08:26:50 -08001218 private void warnOnNotMounted() {
1219 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001220 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001221 }
1222 }
1223
San Mehat4270e1e2010-01-29 05:32:19 -08001224 public String[] getSecureContainerList() {
1225 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001226 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001227 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001228
San Mehat4270e1e2010-01-29 05:32:19 -08001229 try {
1230 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1231 } catch (NativeDaemonConnectorException e) {
1232 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 }
1234 }
San Mehat36972292010-01-06 11:06:32 -08001235
San Mehat4270e1e2010-01-29 05:32:19 -08001236 public int createSecureContainer(String id, int sizeMb, String fstype,
1237 String key, int ownerUid) {
1238 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001239 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001240 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001241
San Mehatb1043402010-02-05 08:26:50 -08001242 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001243 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1244 try {
1245 mConnector.doCommand(cmd);
1246 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001247 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001248 }
San Mehata181b212010-02-11 06:50:20 -08001249
1250 if (rc == StorageResultCode.OperationSucceeded) {
1251 synchronized (mAsecMountSet) {
1252 mAsecMountSet.add(id);
1253 }
1254 }
San Mehat4270e1e2010-01-29 05:32:19 -08001255 return rc;
San Mehat36972292010-01-06 11:06:32 -08001256 }
1257
San Mehat4270e1e2010-01-29 05:32:19 -08001258 public int finalizeSecureContainer(String id) {
1259 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001260 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001261
San Mehatb1043402010-02-05 08:26:50 -08001262 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001263 try {
1264 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001265 /*
1266 * Finalization does a remount, so no need
1267 * to update mAsecMountSet
1268 */
San Mehat4270e1e2010-01-29 05:32:19 -08001269 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001270 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001271 }
San Mehat4270e1e2010-01-29 05:32:19 -08001272 return rc;
San Mehat36972292010-01-06 11:06:32 -08001273 }
1274
San Mehatd9709982010-02-18 11:43:03 -08001275 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001276 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001277 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001278 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001279
San Mehatb1043402010-02-05 08:26:50 -08001280 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001281 try {
San Mehatd9709982010-02-18 11:43:03 -08001282 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001283 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001284 int code = e.getCode();
1285 if (code == VoldResponseCode.OpFailedStorageBusy) {
1286 rc = StorageResultCode.OperationFailedStorageBusy;
1287 } else {
1288 rc = StorageResultCode.OperationFailedInternalError;
1289 }
San Mehat02735bc2010-01-26 15:18:08 -08001290 }
San Mehata181b212010-02-11 06:50:20 -08001291
1292 if (rc == StorageResultCode.OperationSucceeded) {
1293 synchronized (mAsecMountSet) {
1294 if (mAsecMountSet.contains(id)) {
1295 mAsecMountSet.remove(id);
1296 }
1297 }
1298 }
1299
San Mehat4270e1e2010-01-29 05:32:19 -08001300 return rc;
San Mehat36972292010-01-06 11:06:32 -08001301 }
1302
San Mehat4270e1e2010-01-29 05:32:19 -08001303 public int mountSecureContainer(String id, String key, int ownerUid) {
1304 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001305 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001306 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001307
San Mehata181b212010-02-11 06:50:20 -08001308 synchronized (mAsecMountSet) {
1309 if (mAsecMountSet.contains(id)) {
1310 return StorageResultCode.OperationFailedStorageMounted;
1311 }
1312 }
1313
San Mehatb1043402010-02-05 08:26:50 -08001314 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001315 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1316 try {
1317 mConnector.doCommand(cmd);
1318 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001319 int code = e.getCode();
1320 if (code != VoldResponseCode.OpFailedStorageBusy) {
1321 rc = StorageResultCode.OperationFailedInternalError;
1322 }
San Mehat02735bc2010-01-26 15:18:08 -08001323 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001324
1325 if (rc == StorageResultCode.OperationSucceeded) {
1326 synchronized (mAsecMountSet) {
1327 mAsecMountSet.add(id);
1328 }
1329 }
San Mehat4270e1e2010-01-29 05:32:19 -08001330 return rc;
San Mehat36972292010-01-06 11:06:32 -08001331 }
1332
San Mehatd9709982010-02-18 11:43:03 -08001333 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001334 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001335 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001336 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001337
San Mehat6cdd9c02010-02-09 14:45:20 -08001338 synchronized (mAsecMountSet) {
1339 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001340 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001341 }
1342 }
1343
San Mehatb1043402010-02-05 08:26:50 -08001344 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001345 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001346 try {
1347 mConnector.doCommand(cmd);
1348 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001349 int code = e.getCode();
1350 if (code == VoldResponseCode.OpFailedStorageBusy) {
1351 rc = StorageResultCode.OperationFailedStorageBusy;
1352 } else {
1353 rc = StorageResultCode.OperationFailedInternalError;
1354 }
San Mehat02735bc2010-01-26 15:18:08 -08001355 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001356
1357 if (rc == StorageResultCode.OperationSucceeded) {
1358 synchronized (mAsecMountSet) {
1359 mAsecMountSet.remove(id);
1360 }
1361 }
San Mehat4270e1e2010-01-29 05:32:19 -08001362 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001363 }
1364
San Mehat6cdd9c02010-02-09 14:45:20 -08001365 public boolean isSecureContainerMounted(String id) {
1366 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1367 waitForReady();
1368 warnOnNotMounted();
1369
1370 synchronized (mAsecMountSet) {
1371 return mAsecMountSet.contains(id);
1372 }
1373 }
1374
San Mehat4270e1e2010-01-29 05:32:19 -08001375 public int renameSecureContainer(String oldId, String newId) {
1376 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001377 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001378 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001379
San Mehata181b212010-02-11 06:50:20 -08001380 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001381 /*
1382 * Because a mounted container has active internal state which cannot be
1383 * changed while active, we must ensure both ids are not currently mounted.
1384 */
1385 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001386 return StorageResultCode.OperationFailedStorageMounted;
1387 }
1388 }
1389
San Mehatb1043402010-02-05 08:26:50 -08001390 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001391 String cmd = String.format("asec rename %s %s", oldId, newId);
1392 try {
1393 mConnector.doCommand(cmd);
1394 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001395 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001396 }
San Mehata181b212010-02-11 06:50:20 -08001397
San Mehat4270e1e2010-01-29 05:32:19 -08001398 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001399 }
1400
San Mehat4270e1e2010-01-29 05:32:19 -08001401 public String getSecureContainerPath(String id) {
1402 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001403 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001404 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001405
San Mehat2d66cef2010-03-23 11:12:52 -07001406 try {
1407 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1408 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001409 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001410 if (code != VoldResponseCode.AsecPathResult) {
1411 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1412 }
1413 return tok[1];
1414 } catch (NativeDaemonConnectorException e) {
1415 int code = e.getCode();
1416 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1417 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001418 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001419 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001420 }
1421 }
San Mehat22dd86e2010-01-12 12:21:18 -08001422 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001423
1424 public void finishMediaUpdate() {
1425 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1426 }
Kenny Root02c87302010-07-01 08:10:18 -07001427
Kenny Roota02b8b02010-08-05 16:14:17 -07001428 private boolean isCallerOwnerOfPackageOrSystem(String packageName) {
Kenny Root02c87302010-07-01 08:10:18 -07001429 final int callerUid = Binder.getCallingUid();
Kenny Roota02b8b02010-08-05 16:14:17 -07001430 return isUidOwnerOfPackageOrSystem(packageName, callerUid);
Kenny Root02c87302010-07-01 08:10:18 -07001431 }
1432
Kenny Roota02b8b02010-08-05 16:14:17 -07001433 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
1434 if (callerUid == android.os.Process.SYSTEM_UID) {
1435 return true;
1436 }
1437
Kenny Root02c87302010-07-01 08:10:18 -07001438 if (packageName == null) {
1439 return false;
1440 }
1441
1442 final int packageUid = mPms.getPackageUid(packageName);
1443
1444 if (DEBUG_OBB) {
1445 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
1446 packageUid + ", callerUid = " + callerUid);
1447 }
1448
1449 return callerUid == packageUid;
1450 }
1451
1452 public String getMountedObbPath(String filename) {
1453 waitForReady();
1454 warnOnNotMounted();
1455
Kenny Root02c87302010-07-01 08:10:18 -07001456 try {
1457 ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename));
1458 String []tok = rsp.get(0).split(" ");
1459 int code = Integer.parseInt(tok[0]);
1460 if (code != VoldResponseCode.AsecPathResult) {
1461 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1462 }
1463 return tok[1];
1464 } catch (NativeDaemonConnectorException e) {
1465 int code = e.getCode();
1466 if (code == VoldResponseCode.OpFailedStorageNotFound) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001467 return null;
Kenny Root02c87302010-07-01 08:10:18 -07001468 } else {
1469 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1470 }
1471 }
1472 }
1473
1474 public boolean isObbMounted(String filename) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001475 synchronized (mObbMounts) {
1476 return mObbPathToStateMap.containsKey(filename);
Kenny Root02c87302010-07-01 08:10:18 -07001477 }
1478 }
1479
Kenny Roota02b8b02010-08-05 16:14:17 -07001480 public void mountObb(String filename, String key, IObbActionListener token) {
Kenny Root02c87302010-07-01 08:10:18 -07001481 waitForReady();
1482 warnOnNotMounted();
1483
Kenny Roota02b8b02010-08-05 16:14:17 -07001484 final ObbState obbState;
1485
1486 synchronized (mObbMounts) {
1487 if (isObbMounted(filename)) {
1488 throw new IllegalArgumentException("OBB file is already mounted");
Kenny Root02c87302010-07-01 08:10:18 -07001489 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001490
1491 if (mObbMounts.containsKey(token)) {
1492 throw new IllegalArgumentException("You may only have one OBB mounted at a time");
1493 }
1494
1495 final int callerUid = Binder.getCallingUid();
1496 obbState = new ObbState(filename, token, callerUid);
1497 addObbState(obbState);
Kenny Root02c87302010-07-01 08:10:18 -07001498 }
1499
Kenny Root02c87302010-07-01 08:10:18 -07001500 try {
Kenny Roota02b8b02010-08-05 16:14:17 -07001501 token.asBinder().linkToDeath(obbState, 0);
1502 } catch (RemoteException rex) {
1503 Slog.e(TAG, "Failed to link to listener death");
Kenny Root02c87302010-07-01 08:10:18 -07001504 }
1505
Kenny Roota02b8b02010-08-05 16:14:17 -07001506 MountObbAction action = new MountObbAction(obbState, key);
1507 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
1508
1509 if (DEBUG_OBB)
1510 Slog.i(TAG, "Send to OBB handler: " + action.toString());
Kenny Root02c87302010-07-01 08:10:18 -07001511 }
1512
Kenny Roota02b8b02010-08-05 16:14:17 -07001513 public void unmountObb(String filename, boolean force, IObbActionListener token) {
1514 final ObbState obbState;
Kenny Root02c87302010-07-01 08:10:18 -07001515
Kenny Roota02b8b02010-08-05 16:14:17 -07001516 synchronized (mObbMounts) {
1517 if (!isObbMounted(filename)) {
1518 throw new IllegalArgumentException("OBB is not mounted");
1519 }
1520 obbState = mObbPathToStateMap.get(filename);
Kenny Root02c87302010-07-01 08:10:18 -07001521 }
1522
Kenny Roota02b8b02010-08-05 16:14:17 -07001523 UnmountObbAction action = new UnmountObbAction(obbState, force);
1524 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root02c87302010-07-01 08:10:18 -07001525
Kenny Roota02b8b02010-08-05 16:14:17 -07001526 if (DEBUG_OBB)
1527 Slog.i(TAG, "Send to OBB handler: " + action.toString());
1528 }
1529
1530 private void addObbState(ObbState obbState) {
1531 synchronized (mObbMounts) {
1532 mObbMounts.put(obbState.token, obbState);
1533 mObbPathToStateMap.put(obbState.filename, obbState);
1534 }
1535 }
1536
1537 private void removeObbState(ObbState obbState) {
1538 synchronized (mObbMounts) {
1539 mObbMounts.remove(obbState.token);
1540 mObbPathToStateMap.remove(obbState.filename);
1541 }
1542 }
1543
1544 private class ObbActionHandler extends Handler {
1545 private boolean mBound = false;
1546 private List<ObbAction> mActions = new LinkedList<ObbAction>();
1547
1548 ObbActionHandler(Looper l) {
1549 super(l);
1550 }
1551
1552 @Override
1553 public void handleMessage(Message msg) {
1554 switch (msg.what) {
1555 case OBB_RUN_ACTION: {
1556 ObbAction action = (ObbAction) msg.obj;
1557
1558 if (DEBUG_OBB)
1559 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
1560
1561 // If a bind was already initiated we don't really
1562 // need to do anything. The pending install
1563 // will be processed later on.
1564 if (!mBound) {
1565 // If this is the only one pending we might
1566 // have to bind to the service again.
1567 if (!connectToService()) {
1568 Slog.e(TAG, "Failed to bind to media container service");
1569 action.handleError();
1570 return;
1571 } else {
1572 // Once we bind to the service, the first
1573 // pending request will be processed.
1574 mActions.add(action);
1575 }
1576 } else {
1577 // Already bound to the service. Just make
1578 // sure we trigger off processing the first request.
1579 if (mActions.size() == 0) {
1580 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1581 }
1582
1583 mActions.add(action);
1584 }
1585 break;
1586 }
1587 case OBB_MCS_BOUND: {
1588 if (DEBUG_OBB)
1589 Slog.i(TAG, "OBB_MCS_BOUND");
1590 if (msg.obj != null) {
1591 mContainerService = (IMediaContainerService) msg.obj;
1592 }
1593 if (mContainerService == null) {
1594 // Something seriously wrong. Bail out
1595 Slog.e(TAG, "Cannot bind to media container service");
1596 for (ObbAction action : mActions) {
1597 // Indicate service bind error
1598 action.handleError();
1599 }
1600 mActions.clear();
1601 } else if (mActions.size() > 0) {
1602 ObbAction action = mActions.get(0);
1603 if (action != null) {
1604 action.execute(this);
1605 }
1606 } else {
1607 // Should never happen ideally.
1608 Slog.w(TAG, "Empty queue");
1609 }
1610 break;
1611 }
1612 case OBB_MCS_RECONNECT: {
1613 if (DEBUG_OBB)
1614 Slog.i(TAG, "OBB_MCS_RECONNECT");
1615 if (mActions.size() > 0) {
1616 if (mBound) {
1617 disconnectService();
1618 }
1619 if (!connectToService()) {
1620 Slog.e(TAG, "Failed to bind to media container service");
1621 for (ObbAction action : mActions) {
1622 // Indicate service bind error
1623 action.handleError();
1624 }
1625 mActions.clear();
1626 }
1627 }
1628 break;
1629 }
1630 case OBB_MCS_UNBIND: {
1631 if (DEBUG_OBB)
1632 Slog.i(TAG, "OBB_MCS_UNBIND");
1633
1634 // Delete pending install
1635 if (mActions.size() > 0) {
1636 mActions.remove(0);
1637 }
1638 if (mActions.size() == 0) {
1639 if (mBound) {
1640 disconnectService();
1641 }
1642 } else {
1643 // There are more pending requests in queue.
1644 // Just post MCS_BOUND message to trigger processing
1645 // of next pending install.
1646 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1647 }
1648 break;
1649 }
1650 case OBB_MCS_GIVE_UP: {
1651 if (DEBUG_OBB)
1652 Slog.i(TAG, "OBB_MCS_GIVE_UP");
1653 mActions.remove(0);
1654 break;
1655 }
1656 }
1657 }
1658
1659 private boolean connectToService() {
1660 if (DEBUG_OBB)
1661 Slog.i(TAG, "Trying to bind to DefaultContainerService");
1662
1663 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
1664 if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) {
1665 mBound = true;
1666 return true;
1667 }
1668 return false;
1669 }
1670
1671 private void disconnectService() {
1672 mContainerService = null;
1673 mBound = false;
1674 mContext.unbindService(mDefContainerConn);
1675 }
1676 }
1677
1678 abstract class ObbAction {
1679 private static final int MAX_RETRIES = 3;
1680 private int mRetries;
1681
1682 ObbState mObbState;
1683
1684 ObbAction(ObbState obbState) {
1685 mObbState = obbState;
1686 }
1687
1688 public void execute(ObbActionHandler handler) {
1689 try {
1690 if (DEBUG_OBB)
1691 Slog.i(TAG, "Starting to execute action: " + this.toString());
1692 mRetries++;
1693 if (mRetries > MAX_RETRIES) {
1694 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
1695 mObbActionHandler.sendEmptyMessage(OBB_MCS_GIVE_UP);
1696 handleError();
1697 return;
1698 } else {
1699 handleExecute();
1700 if (DEBUG_OBB)
1701 Slog.i(TAG, "Posting install MCS_UNBIND");
1702 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1703 }
1704 } catch (RemoteException e) {
1705 if (DEBUG_OBB)
1706 Slog.i(TAG, "Posting install MCS_RECONNECT");
1707 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
1708 } catch (Exception e) {
1709 if (DEBUG_OBB)
1710 Slog.d(TAG, "Error handling OBB action", e);
1711 handleError();
1712 }
1713 }
1714
1715 abstract void handleExecute() throws RemoteException;
1716 abstract void handleError();
1717 }
1718
1719 class MountObbAction extends ObbAction {
1720 private String mKey;
1721
1722 MountObbAction(ObbState obbState, String key) {
1723 super(obbState);
1724 mKey = key;
1725 }
1726
1727 public void handleExecute() throws RemoteException {
1728 ObbInfo obbInfo = mContainerService.getObbInfo(mObbState.filename);
1729 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) {
1730 throw new IllegalArgumentException("Caller package does not match OBB file");
1731 }
1732
1733 if (mKey == null) {
1734 mKey = "none";
1735 }
1736
1737 int rc = StorageResultCode.OperationSucceeded;
1738 String cmd = String.format("obb mount %s %s %d", mObbState.filename, mKey,
1739 mObbState.callerUid);
1740 try {
1741 mConnector.doCommand(cmd);
1742 } catch (NativeDaemonConnectorException e) {
1743 int code = e.getCode();
1744 if (code != VoldResponseCode.OpFailedStorageBusy) {
1745 rc = StorageResultCode.OperationFailedInternalError;
1746 }
1747 }
1748
1749 if (rc == StorageResultCode.OperationSucceeded) {
1750 try {
1751 mObbState.token.onObbResult(mObbState.filename, "mounted");
1752 } catch (RemoteException e) {
1753 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1754 }
Kenny Root02c87302010-07-01 08:10:18 -07001755 } else {
Kenny Roota02b8b02010-08-05 16:14:17 -07001756 Slog.e(TAG, "Couldn't mount OBB file");
1757
1758 // We didn't succeed, so remove this from the mount-set.
1759 removeObbState(mObbState);
Kenny Root02c87302010-07-01 08:10:18 -07001760 }
1761 }
1762
Kenny Roota02b8b02010-08-05 16:14:17 -07001763 public void handleError() {
1764 removeObbState(mObbState);
1765
1766 try {
1767 mObbState.token.onObbResult(mObbState.filename, "error");
1768 } catch (RemoteException e) {
1769 Slog.e(TAG, "Couldn't send back OBB mount error for " + mObbState.filename);
Kenny Root02c87302010-07-01 08:10:18 -07001770 }
1771 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001772
1773 @Override
1774 public String toString() {
1775 StringBuilder sb = new StringBuilder();
1776 sb.append("MountObbAction{");
1777 sb.append("filename=");
1778 sb.append(mObbState.filename);
1779 sb.append(",callerUid=");
1780 sb.append(mObbState.callerUid);
1781 sb.append(",token=");
1782 sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL");
1783 sb.append('}');
1784 return sb.toString();
1785 }
1786 }
1787
1788 class UnmountObbAction extends ObbAction {
1789 private boolean mForceUnmount;
1790
1791 UnmountObbAction(ObbState obbState, boolean force) {
1792 super(obbState);
1793 mForceUnmount = force;
1794 }
1795
1796 public void handleExecute() throws RemoteException {
1797 ObbInfo obbInfo = mContainerService.getObbInfo(mObbState.filename);
1798
1799 if (!isCallerOwnerOfPackageOrSystem(obbInfo.packageName)) {
1800 throw new IllegalArgumentException("Caller package does not match OBB file");
1801 }
1802
1803 int rc = StorageResultCode.OperationSucceeded;
1804 String cmd = String.format("obb unmount %s%s", mObbState.filename,
1805 (mForceUnmount ? " force" : ""));
1806 try {
1807 mConnector.doCommand(cmd);
1808 } catch (NativeDaemonConnectorException e) {
1809 int code = e.getCode();
1810 if (code == VoldResponseCode.OpFailedStorageBusy) {
1811 rc = StorageResultCode.OperationFailedStorageBusy;
1812 } else {
1813 rc = StorageResultCode.OperationFailedInternalError;
1814 }
1815 }
1816
1817 if (rc == StorageResultCode.OperationSucceeded) {
1818 removeObbState(mObbState);
1819
1820 try {
1821 mObbState.token.onObbResult(mObbState.filename, "unmounted");
1822 } catch (RemoteException e) {
1823 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1824 }
1825 } else {
1826 try {
1827 mObbState.token.onObbResult(mObbState.filename, "error");
1828 } catch (RemoteException e) {
1829 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1830 }
1831 }
1832 }
1833
1834 public void handleError() {
1835 removeObbState(mObbState);
1836
1837 try {
1838 mObbState.token.onObbResult(mObbState.filename, "error");
1839 } catch (RemoteException e) {
1840 Slog.e(TAG, "Couldn't send back OBB unmount error for " + mObbState.filename);
1841 }
1842 }
1843
1844 @Override
1845 public String toString() {
1846 StringBuilder sb = new StringBuilder();
1847 sb.append("UnmountObbAction{");
1848 sb.append("filename=");
1849 sb.append(mObbState.filename != null ? mObbState.filename : "null");
1850 sb.append(",force=");
1851 sb.append(mForceUnmount);
1852 sb.append(",callerUid=");
1853 sb.append(mObbState.callerUid);
1854 sb.append(",token=");
1855 sb.append(mObbState.token != null ? mObbState.token.toString() : "null");
1856 sb.append('}');
1857 return sb.toString();
1858 }
Kenny Root02c87302010-07-01 08:10:18 -07001859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860}
1861