blob: c07007a664898a7e710bd3fd3e0f9483f1890431 [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;
Mike Lockwood03559752010-07-19 18:25:03 -0400140 // true if we should fake MEDIA_MOUNTED state for external storage
141 private boolean mEmulateExternalStorage = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800142
San Mehat6cdd9c02010-02-09 14:45:20 -0800143 /**
144 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800145 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800146 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800147 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800148
Kenny Root02c87302010-07-01 08:10:18 -0700149 /**
Kenny Roota02b8b02010-08-05 16:14:17 -0700150 * Mounted OBB tracking information. Used to track the current state of all
151 * OBBs.
Kenny Root02c87302010-07-01 08:10:18 -0700152 */
Kenny Roota02b8b02010-08-05 16:14:17 -0700153 final private Map<IObbActionListener, ObbState> mObbMounts = new HashMap<IObbActionListener, ObbState>();
154 final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>();
155
156 class ObbState implements IBinder.DeathRecipient {
157 public ObbState(String filename, IObbActionListener token, int callerUid) {
158 this.filename = filename;
159 this.token = token;
160 this.callerUid = callerUid;
161 mounted = false;
162 }
163
164 // OBB source filename
165 String filename;
166
167 // Token of remote Binder caller
168 IObbActionListener token;
169
170 // Binder.callingUid()
171 public int callerUid;
172
173 // Whether this is mounted currently.
174 boolean mounted;
175
176 @Override
177 public void binderDied() {
178 ObbAction action = new UnmountObbAction(this, true);
179 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
180
181 removeObbState(this);
182
183 token.asBinder().unlinkToDeath(this, 0);
184 }
185 }
186
187 // OBB Action Handler
188 final private ObbActionHandler mObbActionHandler;
189
190 // OBB action handler messages
191 private static final int OBB_RUN_ACTION = 1;
192 private static final int OBB_MCS_BOUND = 2;
193 private static final int OBB_MCS_UNBIND = 3;
194 private static final int OBB_MCS_RECONNECT = 4;
195 private static final int OBB_MCS_GIVE_UP = 5;
196
197 /*
198 * Default Container Service information
199 */
200 static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
201 "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService");
202
203 final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection();
204
205 class DefaultContainerConnection implements ServiceConnection {
206 public void onServiceConnected(ComponentName name, IBinder service) {
207 if (DEBUG_OBB)
208 Slog.i(TAG, "onServiceConnected");
209 IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service);
210 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs));
211 }
212
213 public void onServiceDisconnected(ComponentName name) {
214 if (DEBUG_OBB)
215 Slog.i(TAG, "onServiceDisconnected");
216 }
217 };
218
219 // Used in the ObbActionHandler
220 private IMediaContainerService mContainerService = null;
Kenny Root02c87302010-07-01 08:10:18 -0700221
222 // Handler messages
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800223 private static final int H_UNMOUNT_PM_UPDATE = 1;
224 private static final int H_UNMOUNT_PM_DONE = 2;
225 private static final int H_UNMOUNT_MS = 3;
226 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
227 private static final int MAX_UNMOUNT_RETRIES = 4;
228
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800229 class UnmountCallBack {
230 String path;
231 int retries;
232 boolean force;
233
234 UnmountCallBack(String path, boolean force) {
235 retries = 0;
236 this.path = path;
237 this.force = force;
238 }
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800239
240 void handleFinished() {
San Mehata5078592010-03-25 09:36:54 -0700241 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path);
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800242 doUnmountVolume(path, true);
243 }
244 }
245
246 class UmsEnableCallBack extends UnmountCallBack {
247 String method;
248
249 UmsEnableCallBack(String path, String method, boolean force) {
250 super(path, force);
251 this.method = method;
252 }
253
254 @Override
255 void handleFinished() {
256 super.handleFinished();
257 doShareUnshareVolume(path, method, true);
258 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800259 }
260
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800261 class ShutdownCallBack extends UnmountCallBack {
262 IMountShutdownObserver observer;
263 ShutdownCallBack(String path, IMountShutdownObserver observer) {
264 super(path, true);
265 this.observer = observer;
266 }
267
268 @Override
269 void handleFinished() {
270 int ret = doUnmountVolume(path, true);
271 if (observer != null) {
272 try {
273 observer.onShutDownComplete(ret);
274 } catch (RemoteException e) {
San Mehata5078592010-03-25 09:36:54 -0700275 Slog.w(TAG, "RemoteException when shutting down");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800276 }
277 }
278 }
279 }
280
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400281 class MountServiceHandler extends Handler {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800282 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700283 boolean mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800284
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400285 MountServiceHandler(Looper l) {
286 super(l);
287 }
288
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800289 public void handleMessage(Message msg) {
290 switch (msg.what) {
291 case H_UNMOUNT_PM_UPDATE: {
San Mehata5078592010-03-25 09:36:54 -0700292 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800293 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
294 mForceUnmounts.add(ucb);
San Mehata5078592010-03-25 09:36:54 -0700295 if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800296 // Register only if needed.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700297 if (!mUpdatingStatus) {
San Mehata5078592010-03-25 09:36:54 -0700298 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700299 mUpdatingStatus = true;
300 mPms.updateExternalMediaStatus(false, true);
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800301 }
302 break;
303 }
304 case H_UNMOUNT_PM_DONE: {
San Mehata5078592010-03-25 09:36:54 -0700305 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE");
San Mehata5078592010-03-25 09:36:54 -0700306 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700307 mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800308 int size = mForceUnmounts.size();
309 int sizeArr[] = new int[size];
310 int sizeArrN = 0;
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700311 // Kill processes holding references first
312 ActivityManagerService ams = (ActivityManagerService)
313 ServiceManager.getService("activity");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800314 for (int i = 0; i < size; i++) {
315 UnmountCallBack ucb = mForceUnmounts.get(i);
316 String path = ucb.path;
317 boolean done = false;
318 if (!ucb.force) {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800319 done = true;
320 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800321 int pids[] = getStorageUsers(path);
322 if (pids == null || pids.length == 0) {
323 done = true;
324 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800325 // Eliminate system process here?
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700326 ams.killPids(pids, "unmount media");
327 // Confirm if file references have been freed.
328 pids = getStorageUsers(path);
329 if (pids == null || pids.length == 0) {
330 done = true;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800331 }
332 }
333 }
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700334 if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) {
335 // Retry again
336 Slog.i(TAG, "Retrying to kill storage users again");
337 mHandler.sendMessageDelayed(
338 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
339 ucb.retries++),
340 RETRY_UNMOUNT_DELAY);
341 } else {
342 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
343 Slog.i(TAG, "Failed to unmount media inspite of " +
344 MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now");
345 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800346 sizeArr[sizeArrN++] = i;
347 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
348 ucb));
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800349 }
350 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800351 // Remove already processed elements from list.
352 for (int i = (sizeArrN-1); i >= 0; i--) {
353 mForceUnmounts.remove(sizeArr[i]);
354 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800355 break;
356 }
357 case H_UNMOUNT_MS : {
San Mehata5078592010-03-25 09:36:54 -0700358 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800359 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800360 ucb.handleFinished();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800361 break;
362 }
363 }
364 }
365 };
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400366 final private HandlerThread mHandlerThread;
367 final private Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800368
San Mehat207e5382010-02-04 20:46:54 -0800369 private void waitForReady() {
370 while (mReady == false) {
371 for (int retries = 5; retries > 0; retries--) {
372 if (mReady) {
373 return;
374 }
375 SystemClock.sleep(1000);
376 }
San Mehata5078592010-03-25 09:36:54 -0700377 Slog.w(TAG, "Waiting too long for mReady!");
San Mehat207e5382010-02-04 20:46:54 -0800378 }
San Mehat1f6301e2010-01-07 22:40:27 -0800379 }
Kenny Root02c87302010-07-01 08:10:18 -0700380
San Mehat207e5382010-02-04 20:46:54 -0800381 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 public void onReceive(Context context, Intent intent) {
San Mehat91c77612010-01-07 10:39:41 -0800383 String action = intent.getAction();
384
385 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
San Mehat207e5382010-02-04 20:46:54 -0800386 mBooted = true;
San Mehat22dd86e2010-01-12 12:21:18 -0800387
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800388 /*
389 * In the simulator, we need to broadcast a volume mounted event
390 * to make the media scanner run.
391 */
392 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
393 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted);
394 return;
395 }
San Mehatfafb0412010-02-18 19:40:04 -0800396 new Thread() {
397 public void run() {
398 try {
399 String path = Environment.getExternalStorageDirectory().getPath();
San Mehat6a254402010-03-22 10:21:00 -0700400 String state = getVolumeState(path);
401
Mike Lockwood03559752010-07-19 18:25:03 -0400402 if (mEmulateExternalStorage) {
403 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Mounted);
404 } else if (state.equals(Environment.MEDIA_UNMOUNTED)) {
San Mehatfafb0412010-02-18 19:40:04 -0800405 int rc = doMountVolume(path);
406 if (rc != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700407 Slog.e(TAG, String.format("Boot-time mount failed (%d)", rc));
San Mehatfafb0412010-02-18 19:40:04 -0800408 }
San Mehat6a254402010-03-22 10:21:00 -0700409 } else if (state.equals(Environment.MEDIA_SHARED)) {
410 /*
411 * Bootstrap UMS enabled state since vold indicates
412 * the volume is shared (runtime restart while ums enabled)
413 */
414 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Shared);
San Mehatfafb0412010-02-18 19:40:04 -0800415 }
San Mehat6a254402010-03-22 10:21:00 -0700416
San Mehat6a965af22010-02-24 17:47:30 -0800417 /*
San Mehat6a254402010-03-22 10:21:00 -0700418 * If UMS was connected on boot, send the connected event
San Mehat6a965af22010-02-24 17:47:30 -0800419 * now that we're up.
420 */
421 if (mSendUmsConnectedOnBoot) {
422 sendUmsIntent(true);
423 mSendUmsConnectedOnBoot = false;
424 }
San Mehatfafb0412010-02-18 19:40:04 -0800425 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700426 Slog.e(TAG, "Boot-time mount exception", ex);
San Mehatfafb0412010-02-18 19:40:04 -0800427 }
San Mehat207e5382010-02-04 20:46:54 -0800428 }
San Mehatfafb0412010-02-18 19:40:04 -0800429 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 }
431 }
432 };
433
San Mehat4270e1e2010-01-29 05:32:19 -0800434 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
435 final IMountServiceListener mListener;
436
437 MountServiceBinderListener(IMountServiceListener listener) {
438 mListener = listener;
Kenny Root02c87302010-07-01 08:10:18 -0700439
San Mehat91c77612010-01-07 10:39:41 -0800440 }
441
San Mehat4270e1e2010-01-29 05:32:19 -0800442 public void binderDied() {
San Mehata5078592010-03-25 09:36:54 -0700443 if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!");
Kenny Roota02b8b02010-08-05 16:14:17 -0700444 synchronized (mListeners) {
San Mehat4270e1e2010-01-29 05:32:19 -0800445 mListeners.remove(this);
446 mListener.asBinder().unlinkToDeath(this, 0);
447 }
448 }
449 }
450
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800451 private void doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800452 // TODO: Add support for multiple share methods
453 if (!method.equals("ums")) {
454 throw new IllegalArgumentException(String.format("Method %s not supported", method));
455 }
456
San Mehat4270e1e2010-01-29 05:32:19 -0800457 try {
458 mConnector.doCommand(String.format(
459 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
460 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -0700461 Slog.e(TAG, "Failed to share/unshare", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800462 }
San Mehat4270e1e2010-01-29 05:32:19 -0800463 }
464
San Mehat207e5382010-02-04 20:46:54 -0800465 private void updatePublicVolumeState(String path, String state) {
San Mehat4270e1e2010-01-29 05:32:19 -0800466 if (!path.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -0700467 Slog.w(TAG, "Multiple volumes not currently supported");
San Mehat4270e1e2010-01-29 05:32:19 -0800468 return;
469 }
San Mehatb1043402010-02-05 08:26:50 -0800470
471 if (mLegacyState.equals(state)) {
San Mehata5078592010-03-25 09:36:54 -0700472 Slog.w(TAG, String.format("Duplicate state transition (%s -> %s)", mLegacyState, state));
San Mehatb1043402010-02-05 08:26:50 -0800473 return;
474 }
Mike Lockwood03559752010-07-19 18:25:03 -0400475 // Update state on PackageManager, but only of real events
476 if (!mEmulateExternalStorage) {
477 if (Environment.MEDIA_UNMOUNTED.equals(state)) {
478 mPms.updateExternalMediaStatus(false, false);
479 } else if (Environment.MEDIA_MOUNTED.equals(state)) {
480 mPms.updateExternalMediaStatus(true, false);
481 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800482 }
San Mehat4270e1e2010-01-29 05:32:19 -0800483 String oldState = mLegacyState;
484 mLegacyState = state;
485
486 synchronized (mListeners) {
487 for (int i = mListeners.size() -1; i >= 0; i--) {
488 MountServiceBinderListener bl = mListeners.get(i);
489 try {
San Mehatb1043402010-02-05 08:26:50 -0800490 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800491 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700492 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800493 mListeners.remove(i);
494 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700495 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800496 }
497 }
498 }
499 }
500
501 /**
502 *
503 * Callback from NativeDaemonConnector
504 */
505 public void onDaemonConnected() {
506 /*
507 * Since we'll be calling back into the NativeDaemonConnector,
508 * we need to do our work in a new thread.
509 */
510 new Thread() {
511 public void run() {
512 /**
513 * Determine media state and UMS detection status
514 */
515 String path = Environment.getExternalStorageDirectory().getPath();
516 String state = Environment.MEDIA_REMOVED;
517
518 try {
519 String[] vols = mConnector.doListCommand(
520 "volume list", VoldResponseCode.VolumeListResult);
521 for (String volstr : vols) {
522 String[] tok = volstr.split(" ");
523 // FMT: <label> <mountpoint> <state>
524 if (!tok[1].equals(path)) {
San Mehata5078592010-03-25 09:36:54 -0700525 Slog.w(TAG, String.format(
San Mehat4270e1e2010-01-29 05:32:19 -0800526 "Skipping unknown volume '%s'",tok[1]));
527 continue;
528 }
529 int st = Integer.parseInt(tok[2]);
530 if (st == VolumeState.NoMedia) {
531 state = Environment.MEDIA_REMOVED;
532 } else if (st == VolumeState.Idle) {
San Mehat207e5382010-02-04 20:46:54 -0800533 state = Environment.MEDIA_UNMOUNTED;
San Mehat4270e1e2010-01-29 05:32:19 -0800534 } else if (st == VolumeState.Mounted) {
535 state = Environment.MEDIA_MOUNTED;
San Mehata5078592010-03-25 09:36:54 -0700536 Slog.i(TAG, "Media already mounted on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800537 } else if (st == VolumeState.Shared) {
538 state = Environment.MEDIA_SHARED;
San Mehata5078592010-03-25 09:36:54 -0700539 Slog.i(TAG, "Media shared on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800540 } else {
541 throw new Exception(String.format("Unexpected state %d", st));
542 }
543 }
544 if (state != null) {
San Mehata5078592010-03-25 09:36:54 -0700545 if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
San Mehat4270e1e2010-01-29 05:32:19 -0800546 updatePublicVolumeState(path, state);
547 }
548 } catch (Exception e) {
San Mehata5078592010-03-25 09:36:54 -0700549 Slog.e(TAG, "Error processing initial volume state", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800550 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
551 }
552
553 try {
San Mehat207e5382010-02-04 20:46:54 -0800554 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800555 notifyShareAvailabilityChange("ums", avail);
556 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700557 Slog.w(TAG, "Failed to get share availability");
San Mehat4270e1e2010-01-29 05:32:19 -0800558 }
San Mehat207e5382010-02-04 20:46:54 -0800559 /*
560 * Now that we've done our initialization, release
561 * the hounds!
562 */
563 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800564 }
565 }.start();
566 }
567
568 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800569 * Callback from NativeDaemonConnector
570 */
571 public boolean onEvent(int code, String raw, String[] cooked) {
572 Intent in = null;
573
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800574 if (DEBUG_EVENTS) {
575 StringBuilder builder = new StringBuilder();
576 builder.append("onEvent::");
577 builder.append(" raw= " + raw);
578 if (cooked != null) {
579 builder.append(" cooked = " );
580 for (String str : cooked) {
581 builder.append(" " + str);
582 }
583 }
San Mehata5078592010-03-25 09:36:54 -0700584 Slog.i(TAG, builder.toString());
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800585 }
San Mehat4270e1e2010-01-29 05:32:19 -0800586 if (code == VoldResponseCode.VolumeStateChange) {
587 /*
588 * One of the volumes we're managing has changed state.
589 * Format: "NNN Volume <label> <path> state changed
590 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
591 */
592 notifyVolumeStateChange(
593 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
594 Integer.parseInt(cooked[10]));
595 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
596 // FMT: NNN Share method <method> now <available|unavailable>
597 boolean avail = false;
598 if (cooked[5].equals("available")) {
599 avail = true;
600 }
601 notifyShareAvailabilityChange(cooked[3], avail);
602 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
603 (code == VoldResponseCode.VolumeDiskRemoved) ||
604 (code == VoldResponseCode.VolumeBadRemoval)) {
605 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
606 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
607 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
608 final String label = cooked[2];
609 final String path = cooked[3];
610 int major = -1;
611 int minor = -1;
612
613 try {
614 String devComp = cooked[6].substring(1, cooked[6].length() -1);
615 String[] devTok = devComp.split(":");
616 major = Integer.parseInt(devTok[0]);
617 minor = Integer.parseInt(devTok[1]);
618 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700619 Slog.e(TAG, "Failed to parse major/minor", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800620 }
621
San Mehat4270e1e2010-01-29 05:32:19 -0800622 if (code == VoldResponseCode.VolumeDiskInserted) {
623 new Thread() {
624 public void run() {
625 try {
626 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800627 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700628 Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
San Mehat4270e1e2010-01-29 05:32:19 -0800629 }
630 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700631 Slog.w(TAG, "Failed to mount media on insertion", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800632 }
633 }
634 }.start();
635 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
636 /*
637 * This event gets trumped if we're already in BAD_REMOVAL state
638 */
639 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
640 return true;
641 }
642 /* Send the media unmounted event first */
San Mehata5078592010-03-25 09:36:54 -0700643 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800644 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
645 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
646 mContext.sendBroadcast(in);
647
San Mehata5078592010-03-25 09:36:54 -0700648 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
San Mehat4270e1e2010-01-29 05:32:19 -0800649 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
650 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
651 } else if (code == VoldResponseCode.VolumeBadRemoval) {
San Mehata5078592010-03-25 09:36:54 -0700652 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800653 /* Send the media unmounted event first */
654 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
655 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
656 mContext.sendBroadcast(in);
657
San Mehata5078592010-03-25 09:36:54 -0700658 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
San Mehat4270e1e2010-01-29 05:32:19 -0800659 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
660 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
661 } else {
San Mehata5078592010-03-25 09:36:54 -0700662 Slog.e(TAG, String.format("Unknown code {%d}", code));
San Mehat4270e1e2010-01-29 05:32:19 -0800663 }
664 } else {
665 return false;
666 }
667
668 if (in != null) {
669 mContext.sendBroadcast(in);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400670 }
671 return true;
San Mehat4270e1e2010-01-29 05:32:19 -0800672 }
673
San Mehat207e5382010-02-04 20:46:54 -0800674 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800675 String vs = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -0700676 if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
San Mehat4270e1e2010-01-29 05:32:19 -0800677
678 Intent in = null;
679
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500680 if (oldState == VolumeState.Shared && newState != oldState) {
San Mehata5078592010-03-25 09:36:54 -0700681 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500682 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
683 Uri.parse("file://" + path)));
684 }
685
San Mehat4270e1e2010-01-29 05:32:19 -0800686 if (newState == VolumeState.Init) {
687 } else if (newState == VolumeState.NoMedia) {
688 // NoMedia is handled via Disk Remove events
689 } else if (newState == VolumeState.Idle) {
690 /*
691 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
692 * if we're in the process of enabling UMS
693 */
694 if (!vs.equals(
695 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
696 Environment.MEDIA_NOFS) && !vs.equals(
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800697 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
San Mehata5078592010-03-25 09:36:54 -0700698 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
San Mehat4270e1e2010-01-29 05:32:19 -0800699 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
700 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
701 }
702 } else if (newState == VolumeState.Pending) {
703 } else if (newState == VolumeState.Checking) {
San Mehata5078592010-03-25 09:36:54 -0700704 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
San Mehat4270e1e2010-01-29 05:32:19 -0800705 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
706 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
707 } else if (newState == VolumeState.Mounted) {
San Mehata5078592010-03-25 09:36:54 -0700708 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800709 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
San Mehat4270e1e2010-01-29 05:32:19 -0800710 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
711 in.putExtra("read-only", false);
712 } else if (newState == VolumeState.Unmounting) {
San Mehat4270e1e2010-01-29 05:32:19 -0800713 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
714 } else if (newState == VolumeState.Formatting) {
715 } else if (newState == VolumeState.Shared) {
San Mehata5078592010-03-25 09:36:54 -0700716 if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800717 /* Send the media unmounted event first */
718 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
719 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
720 mContext.sendBroadcast(in);
721
San Mehata5078592010-03-25 09:36:54 -0700722 if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
San Mehat4270e1e2010-01-29 05:32:19 -0800723 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
724 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
San Mehata5078592010-03-25 09:36:54 -0700725 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
San Mehat4270e1e2010-01-29 05:32:19 -0800726 } else if (newState == VolumeState.SharedMnt) {
San Mehata5078592010-03-25 09:36:54 -0700727 Slog.e(TAG, "Live shared mounts not supported yet!");
San Mehat4270e1e2010-01-29 05:32:19 -0800728 return;
729 } else {
San Mehata5078592010-03-25 09:36:54 -0700730 Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800731 }
732
733 if (in != null) {
734 mContext.sendBroadcast(in);
735 }
736 }
737
San Mehat207e5382010-02-04 20:46:54 -0800738 private boolean doGetShareMethodAvailable(String method) {
Kenny Root85fb2062010-06-01 20:50:21 -0700739 ArrayList<String> rsp;
Kenny Roota80ce062010-06-01 13:23:53 -0700740 try {
Kenny Root85fb2062010-06-01 20:50:21 -0700741 rsp = mConnector.doCommand("share status " + method);
Kenny Roota80ce062010-06-01 13:23:53 -0700742 } catch (NativeDaemonConnectorException ex) {
743 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
744 return false;
745 }
San Mehat207e5382010-02-04 20:46:54 -0800746
747 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700748 String[] tok = line.split(" ");
749 if (tok.length < 3) {
750 Slog.e(TAG, "Malformed response to share status " + method);
751 return false;
752 }
753
San Mehat207e5382010-02-04 20:46:54 -0800754 int code;
755 try {
756 code = Integer.parseInt(tok[0]);
757 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700758 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehat207e5382010-02-04 20:46:54 -0800759 return false;
760 }
761 if (code == VoldResponseCode.ShareStatusResult) {
762 if (tok[2].equals("available"))
763 return true;
764 return false;
765 } else {
San Mehata5078592010-03-25 09:36:54 -0700766 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehat207e5382010-02-04 20:46:54 -0800767 return false;
768 }
769 }
San Mehata5078592010-03-25 09:36:54 -0700770 Slog.e(TAG, "Got an empty response");
San Mehat207e5382010-02-04 20:46:54 -0800771 return false;
772 }
773
774 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800775 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800776
San Mehata5078592010-03-25 09:36:54 -0700777 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
San Mehat207e5382010-02-04 20:46:54 -0800778 try {
779 mConnector.doCommand(String.format("volume mount %s", path));
780 } catch (NativeDaemonConnectorException e) {
781 /*
782 * Mount failed for some reason
783 */
784 Intent in = null;
785 int code = e.getCode();
786 if (code == VoldResponseCode.OpFailedNoMedia) {
787 /*
788 * Attempt to mount but no media inserted
789 */
San Mehatb1043402010-02-05 08:26:50 -0800790 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800791 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
San Mehata5078592010-03-25 09:36:54 -0700792 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
San Mehat207e5382010-02-04 20:46:54 -0800793 /*
794 * Media is blank or does not contain a supported filesystem
795 */
796 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
797 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800798 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800799 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehata5078592010-03-25 09:36:54 -0700800 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
San Mehat207e5382010-02-04 20:46:54 -0800801 /*
802 * Volume consistency check failed
803 */
804 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
805 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800806 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800807 } else {
San Mehatb1043402010-02-05 08:26:50 -0800808 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800809 }
810
811 /*
812 * Send broadcast intent (if required for the failure)
813 */
814 if (in != null) {
815 mContext.sendBroadcast(in);
816 }
817 }
818
819 return rc;
820 }
821
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800822 /*
823 * If force is not set, we do not unmount if there are
824 * processes holding references to the volume about to be unmounted.
825 * If force is set, all the processes holding references need to be
826 * killed via the ActivityManager before actually unmounting the volume.
827 * This might even take a while and might be retried after timed delays
828 * to make sure we dont end up in an instable state and kill some core
829 * processes.
830 */
San Mehatd9709982010-02-18 11:43:03 -0800831 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800832 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800833 return VoldResponseCode.OpFailedVolNotMounted;
834 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800835 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700836 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800837 try {
San Mehatd9709982010-02-18 11:43:03 -0800838 mConnector.doCommand(String.format(
839 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700840 // We unmounted the volume. None of the asec containers are available now.
841 synchronized (mAsecMountSet) {
842 mAsecMountSet.clear();
843 }
San Mehatb1043402010-02-05 08:26:50 -0800844 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800845 } catch (NativeDaemonConnectorException e) {
846 // Don't worry about mismatch in PackageManager since the
847 // call back will handle the status changes any way.
848 int code = e.getCode();
849 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800850 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800851 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
852 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800853 } else {
San Mehatb1043402010-02-05 08:26:50 -0800854 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800855 }
856 }
857 }
858
859 private int doFormatVolume(String path) {
860 try {
861 String cmd = String.format("volume format %s", path);
862 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800863 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800864 } catch (NativeDaemonConnectorException e) {
865 int code = e.getCode();
866 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800867 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800868 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800869 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800870 } else {
San Mehatb1043402010-02-05 08:26:50 -0800871 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800872 }
873 }
874 }
875
San Mehatb1043402010-02-05 08:26:50 -0800876 private boolean doGetVolumeShared(String path, String method) {
877 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700878 ArrayList<String> rsp;
879
880 try {
881 rsp = mConnector.doCommand(cmd);
882 } catch (NativeDaemonConnectorException ex) {
883 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
884 return false;
885 }
San Mehatb1043402010-02-05 08:26:50 -0800886
887 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700888 String[] tok = line.split(" ");
889 if (tok.length < 3) {
890 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
891 return false;
892 }
893
San Mehatb1043402010-02-05 08:26:50 -0800894 int code;
895 try {
896 code = Integer.parseInt(tok[0]);
897 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700898 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800899 return false;
900 }
901 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700902 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800903 } else {
San Mehata5078592010-03-25 09:36:54 -0700904 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800905 return false;
906 }
907 }
San Mehata5078592010-03-25 09:36:54 -0700908 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800909 return false;
910 }
911
San Mehat207e5382010-02-04 20:46:54 -0800912 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800913 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -0700914 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800915 return;
916 }
917
918 synchronized (mListeners) {
919 for (int i = mListeners.size() -1; i >= 0; i--) {
920 MountServiceBinderListener bl = mListeners.get(i);
921 try {
San Mehatb1043402010-02-05 08:26:50 -0800922 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -0800923 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700924 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800925 mListeners.remove(i);
926 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700927 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800928 }
929 }
930 }
931
San Mehat207e5382010-02-04 20:46:54 -0800932 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -0800933 sendUmsIntent(avail);
934 } else {
935 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -0800936 }
San Mehat2fe718a2010-03-11 12:01:49 -0800937
938 final String path = Environment.getExternalStorageDirectory().getPath();
939 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
940 /*
941 * USB mass storage disconnected while enabled
942 */
943 new Thread() {
944 public void run() {
945 try {
946 int rc;
San Mehata5078592010-03-25 09:36:54 -0700947 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -0800948 doShareUnshareVolume(path, "ums", false);
949 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700950 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -0800951 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
952 path, rc));
953 }
954 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700955 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -0800956 }
957 }
958 }.start();
959 }
San Mehat4270e1e2010-01-29 05:32:19 -0800960 }
961
San Mehat6a965af22010-02-24 17:47:30 -0800962 private void sendUmsIntent(boolean c) {
963 mContext.sendBroadcast(
964 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
965 }
966
San Mehat207e5382010-02-04 20:46:54 -0800967 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -0800968 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
969 throw new SecurityException(String.format("Requires %s permission", perm));
970 }
971 }
972
973 /**
San Mehat207e5382010-02-04 20:46:54 -0800974 * Constructs a new MountService instance
975 *
976 * @param context Binder context for this service
977 */
978 public MountService(Context context) {
979 mContext = context;
980
Mike Lockwood03559752010-07-19 18:25:03 -0400981 mEmulateExternalStorage = context.getResources().getBoolean(
982 com.android.internal.R.bool.config_emulateExternalStorage);
983 if (mEmulateExternalStorage) {
984 Slog.d(TAG, "using emulated external storage");
985 mLegacyState = Environment.MEDIA_MOUNTED;
986 }
987
San Mehat207e5382010-02-04 20:46:54 -0800988 // XXX: This will go away soon in favor of IMountServiceObserver
989 mPms = (PackageManagerService) ServiceManager.getService("package");
990
991 mContext.registerReceiver(mBroadcastReceiver,
992 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
993
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400994 mHandlerThread = new HandlerThread("MountService");
995 mHandlerThread.start();
996 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
997
Kenny Roota02b8b02010-08-05 16:14:17 -0700998 // Add OBB Action Handler to MountService thread.
999 mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper());
1000
Marco Nelissenc34ebce2010-02-18 13:39:41 -08001001 /*
1002 * Vold does not run in the simulator, so pretend the connector thread
1003 * ran and did its thing.
1004 */
1005 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
1006 mReady = true;
1007 mUmsEnabling = true;
1008 return;
1009 }
1010
Kenny Root305bcbf2010-09-03 07:56:38 -07001011 /*
1012 * Create the connection to vold with a maximum queue of twice the
1013 * amount of containers we'd ever expect to have. This keeps an
1014 * "asec list" from blocking a thread repeatedly.
1015 */
1016 mConnector = new NativeDaemonConnector(this, "vold",
1017 PackageManagerService.MAX_CONTAINERS * 2, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001018 mReady = false;
Kenny Root305bcbf2010-09-03 07:56:38 -07001019 Thread thread = new Thread(mConnector, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001020 thread.start();
1021 }
1022
1023 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001024 * Exposed API calls below here
1025 */
1026
1027 public void registerListener(IMountServiceListener listener) {
1028 synchronized (mListeners) {
1029 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
1030 try {
1031 listener.asBinder().linkToDeath(bl, 0);
1032 mListeners.add(bl);
1033 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001034 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -08001035 }
1036 }
1037 }
1038
1039 public void unregisterListener(IMountServiceListener listener) {
1040 synchronized (mListeners) {
1041 for(MountServiceBinderListener bl : mListeners) {
1042 if (bl.mListener == listener) {
1043 mListeners.remove(mListeners.indexOf(bl));
1044 return;
1045 }
1046 }
1047 }
1048 }
1049
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001050 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -08001051 validatePermission(android.Manifest.permission.SHUTDOWN);
1052
San Mehata5078592010-03-25 09:36:54 -07001053 Slog.i(TAG, "Shutting down");
San Mehat4270e1e2010-01-29 05:32:19 -08001054
1055 String path = Environment.getExternalStorageDirectory().getPath();
1056 String state = getVolumeState(path);
San Mehat91c77612010-01-07 10:39:41 -08001057
1058 if (state.equals(Environment.MEDIA_SHARED)) {
1059 /*
1060 * If the media is currently shared, unshare it.
1061 * XXX: This is still dangerous!. We should not
1062 * be rebooting at *all* if UMS is enabled, since
1063 * the UMS host could have dirty FAT cache entries
1064 * yet to flush.
1065 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001066 setUsbMassStorageEnabled(false);
San Mehat91c77612010-01-07 10:39:41 -08001067 } else if (state.equals(Environment.MEDIA_CHECKING)) {
1068 /*
1069 * If the media is being checked, then we need to wait for
1070 * it to complete before being able to proceed.
1071 */
1072 // XXX: @hackbod - Should we disable the ANR timer here?
1073 int retries = 30;
1074 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
1075 try {
1076 Thread.sleep(1000);
1077 } catch (InterruptedException iex) {
San Mehata5078592010-03-25 09:36:54 -07001078 Slog.e(TAG, "Interrupted while waiting for media", iex);
San Mehat91c77612010-01-07 10:39:41 -08001079 break;
1080 }
1081 state = Environment.getExternalStorageState();
1082 }
1083 if (retries == 0) {
San Mehata5078592010-03-25 09:36:54 -07001084 Slog.e(TAG, "Timed out waiting for media to check");
San Mehat91c77612010-01-07 10:39:41 -08001085 }
1086 }
1087
1088 if (state.equals(Environment.MEDIA_MOUNTED)) {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001089 // Post a unmount message.
1090 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
1091 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
San Mehat4270e1e2010-01-29 05:32:19 -08001092 }
1093 }
1094
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001095 private boolean getUmsEnabling() {
1096 synchronized (mListeners) {
1097 return mUmsEnabling;
1098 }
1099 }
1100
1101 private void setUmsEnabling(boolean enable) {
1102 synchronized (mListeners) {
Tony Wufc711252010-08-09 16:49:19 +08001103 mUmsEnabling = enable;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001104 }
1105 }
1106
San Mehatb1043402010-02-05 08:26:50 -08001107 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -08001108 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -08001109
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001110 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001111 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001112 }
San Mehatb1043402010-02-05 08:26:50 -08001113 return doGetShareMethodAvailable("ums");
1114 }
1115
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001116 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001117 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001118 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001119
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001120 // TODO: Add support for multiple share methods
1121
1122 /*
1123 * If the volume is mounted and we're enabling then unmount it
1124 */
1125 String path = Environment.getExternalStorageDirectory().getPath();
1126 String vs = getVolumeState(path);
1127 String method = "ums";
1128 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1129 // Override for isUsbMassStorageEnabled()
1130 setUmsEnabling(enable);
1131 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1132 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1133 // Clear override
1134 setUmsEnabling(false);
1135 }
1136 /*
1137 * If we disabled UMS then mount the volume
1138 */
1139 if (!enable) {
1140 doShareUnshareVolume(path, method, enable);
1141 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001142 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001143 " after disabling share method " + method);
1144 /*
1145 * Even though the mount failed, the unshare didn't so don't indicate an error.
1146 * The mountVolume() call will have set the storage state and sent the necessary
1147 * broadcasts.
1148 */
1149 }
1150 }
San Mehatb1043402010-02-05 08:26:50 -08001151 }
1152
1153 public boolean isUsbMassStorageEnabled() {
1154 waitForReady();
1155 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
San Mehat4270e1e2010-01-29 05:32:19 -08001157
San Mehat7fd0fee2009-12-17 07:12:23 -08001158 /**
1159 * @return state of the volume at the specified mount point
1160 */
San Mehat4270e1e2010-01-29 05:32:19 -08001161 public String getVolumeState(String mountPoint) {
San Mehat7fd0fee2009-12-17 07:12:23 -08001162 /*
1163 * XXX: Until we have multiple volume discovery, just hardwire
1164 * this to /sdcard
1165 */
1166 if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -07001167 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
San Mehat7fd0fee2009-12-17 07:12:23 -08001168 throw new IllegalArgumentException();
1169 }
1170
1171 return mLegacyState;
1172 }
1173
San Mehat4270e1e2010-01-29 05:32:19 -08001174 public int mountVolume(String path) {
1175 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001176
San Mehat207e5382010-02-04 20:46:54 -08001177 waitForReady();
1178 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 }
1180
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001181 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001182 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001183 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001185 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001186 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001187 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1188 Environment.MEDIA_REMOVED.equals(volState) ||
1189 Environment.MEDIA_SHARED.equals(volState) ||
1190 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1191 // Media already unmounted or cannot be unmounted.
1192 // TODO return valid return code when adding observer call back.
1193 return;
1194 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001195 UnmountCallBack ucb = new UnmountCallBack(path, force);
1196 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
1198
San Mehat4270e1e2010-01-29 05:32:19 -08001199 public int formatVolume(String path) {
1200 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001201 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001202
San Mehat207e5382010-02-04 20:46:54 -08001203 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 }
1205
San Mehatc1b4ce92010-02-16 17:13:03 -08001206 public int []getStorageUsers(String path) {
1207 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1208 waitForReady();
1209 try {
1210 String[] r = mConnector.doListCommand(
1211 String.format("storage users %s", path),
1212 VoldResponseCode.StorageUsersListResult);
1213 // FMT: <pid> <process name>
1214 int[] data = new int[r.length];
1215 for (int i = 0; i < r.length; i++) {
1216 String []tok = r[i].split(" ");
1217 try {
1218 data[i] = Integer.parseInt(tok[0]);
1219 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001220 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001221 return new int[0];
1222 }
1223 }
1224 return data;
1225 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001226 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001227 return new int[0];
1228 }
1229 }
1230
San Mehatb1043402010-02-05 08:26:50 -08001231 private void warnOnNotMounted() {
1232 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001233 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001234 }
1235 }
1236
San Mehat4270e1e2010-01-29 05:32:19 -08001237 public String[] getSecureContainerList() {
1238 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001239 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001240 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001241
San Mehat4270e1e2010-01-29 05:32:19 -08001242 try {
1243 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1244 } catch (NativeDaemonConnectorException e) {
1245 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 }
1247 }
San Mehat36972292010-01-06 11:06:32 -08001248
San Mehat4270e1e2010-01-29 05:32:19 -08001249 public int createSecureContainer(String id, int sizeMb, String fstype,
1250 String key, int ownerUid) {
1251 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001252 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001253 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001254
San Mehatb1043402010-02-05 08:26:50 -08001255 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001256 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1257 try {
1258 mConnector.doCommand(cmd);
1259 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001260 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001261 }
San Mehata181b212010-02-11 06:50:20 -08001262
1263 if (rc == StorageResultCode.OperationSucceeded) {
1264 synchronized (mAsecMountSet) {
1265 mAsecMountSet.add(id);
1266 }
1267 }
San Mehat4270e1e2010-01-29 05:32:19 -08001268 return rc;
San Mehat36972292010-01-06 11:06:32 -08001269 }
1270
San Mehat4270e1e2010-01-29 05:32:19 -08001271 public int finalizeSecureContainer(String id) {
1272 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001273 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001274
San Mehatb1043402010-02-05 08:26:50 -08001275 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001276 try {
1277 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001278 /*
1279 * Finalization does a remount, so no need
1280 * to update mAsecMountSet
1281 */
San Mehat4270e1e2010-01-29 05:32:19 -08001282 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001283 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001284 }
San Mehat4270e1e2010-01-29 05:32:19 -08001285 return rc;
San Mehat36972292010-01-06 11:06:32 -08001286 }
1287
San Mehatd9709982010-02-18 11:43:03 -08001288 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001289 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001290 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001291 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001292
San Mehatb1043402010-02-05 08:26:50 -08001293 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001294 try {
San Mehatd9709982010-02-18 11:43:03 -08001295 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001296 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001297 int code = e.getCode();
1298 if (code == VoldResponseCode.OpFailedStorageBusy) {
1299 rc = StorageResultCode.OperationFailedStorageBusy;
1300 } else {
1301 rc = StorageResultCode.OperationFailedInternalError;
1302 }
San Mehat02735bc2010-01-26 15:18:08 -08001303 }
San Mehata181b212010-02-11 06:50:20 -08001304
1305 if (rc == StorageResultCode.OperationSucceeded) {
1306 synchronized (mAsecMountSet) {
1307 if (mAsecMountSet.contains(id)) {
1308 mAsecMountSet.remove(id);
1309 }
1310 }
1311 }
1312
San Mehat4270e1e2010-01-29 05:32:19 -08001313 return rc;
San Mehat36972292010-01-06 11:06:32 -08001314 }
1315
San Mehat4270e1e2010-01-29 05:32:19 -08001316 public int mountSecureContainer(String id, String key, int ownerUid) {
1317 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001318 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001319 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001320
San Mehata181b212010-02-11 06:50:20 -08001321 synchronized (mAsecMountSet) {
1322 if (mAsecMountSet.contains(id)) {
1323 return StorageResultCode.OperationFailedStorageMounted;
1324 }
1325 }
1326
San Mehatb1043402010-02-05 08:26:50 -08001327 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001328 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1329 try {
1330 mConnector.doCommand(cmd);
1331 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001332 int code = e.getCode();
1333 if (code != VoldResponseCode.OpFailedStorageBusy) {
1334 rc = StorageResultCode.OperationFailedInternalError;
1335 }
San Mehat02735bc2010-01-26 15:18:08 -08001336 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001337
1338 if (rc == StorageResultCode.OperationSucceeded) {
1339 synchronized (mAsecMountSet) {
1340 mAsecMountSet.add(id);
1341 }
1342 }
San Mehat4270e1e2010-01-29 05:32:19 -08001343 return rc;
San Mehat36972292010-01-06 11:06:32 -08001344 }
1345
San Mehatd9709982010-02-18 11:43:03 -08001346 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001347 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001348 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001349 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001350
San Mehat6cdd9c02010-02-09 14:45:20 -08001351 synchronized (mAsecMountSet) {
1352 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001353 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001354 }
1355 }
1356
San Mehatb1043402010-02-05 08:26:50 -08001357 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001358 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001359 try {
1360 mConnector.doCommand(cmd);
1361 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001362 int code = e.getCode();
1363 if (code == VoldResponseCode.OpFailedStorageBusy) {
1364 rc = StorageResultCode.OperationFailedStorageBusy;
1365 } else {
1366 rc = StorageResultCode.OperationFailedInternalError;
1367 }
San Mehat02735bc2010-01-26 15:18:08 -08001368 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001369
1370 if (rc == StorageResultCode.OperationSucceeded) {
1371 synchronized (mAsecMountSet) {
1372 mAsecMountSet.remove(id);
1373 }
1374 }
San Mehat4270e1e2010-01-29 05:32:19 -08001375 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001376 }
1377
San Mehat6cdd9c02010-02-09 14:45:20 -08001378 public boolean isSecureContainerMounted(String id) {
1379 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1380 waitForReady();
1381 warnOnNotMounted();
1382
1383 synchronized (mAsecMountSet) {
1384 return mAsecMountSet.contains(id);
1385 }
1386 }
1387
San Mehat4270e1e2010-01-29 05:32:19 -08001388 public int renameSecureContainer(String oldId, String newId) {
1389 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001390 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001391 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001392
San Mehata181b212010-02-11 06:50:20 -08001393 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001394 /*
1395 * Because a mounted container has active internal state which cannot be
1396 * changed while active, we must ensure both ids are not currently mounted.
1397 */
1398 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001399 return StorageResultCode.OperationFailedStorageMounted;
1400 }
1401 }
1402
San Mehatb1043402010-02-05 08:26:50 -08001403 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001404 String cmd = String.format("asec rename %s %s", oldId, newId);
1405 try {
1406 mConnector.doCommand(cmd);
1407 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001408 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001409 }
San Mehata181b212010-02-11 06:50:20 -08001410
San Mehat4270e1e2010-01-29 05:32:19 -08001411 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001412 }
1413
San Mehat4270e1e2010-01-29 05:32:19 -08001414 public String getSecureContainerPath(String id) {
1415 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001416 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001417 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001418
San Mehat2d66cef2010-03-23 11:12:52 -07001419 try {
1420 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1421 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001422 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001423 if (code != VoldResponseCode.AsecPathResult) {
1424 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1425 }
1426 return tok[1];
1427 } catch (NativeDaemonConnectorException e) {
1428 int code = e.getCode();
1429 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1430 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001431 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001432 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001433 }
1434 }
San Mehat22dd86e2010-01-12 12:21:18 -08001435 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001436
1437 public void finishMediaUpdate() {
1438 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1439 }
Kenny Root02c87302010-07-01 08:10:18 -07001440
Kenny Roota02b8b02010-08-05 16:14:17 -07001441 private boolean isCallerOwnerOfPackageOrSystem(String packageName) {
Kenny Root02c87302010-07-01 08:10:18 -07001442 final int callerUid = Binder.getCallingUid();
Kenny Roota02b8b02010-08-05 16:14:17 -07001443 return isUidOwnerOfPackageOrSystem(packageName, callerUid);
Kenny Root02c87302010-07-01 08:10:18 -07001444 }
1445
Kenny Roota02b8b02010-08-05 16:14:17 -07001446 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
1447 if (callerUid == android.os.Process.SYSTEM_UID) {
1448 return true;
1449 }
1450
Kenny Root02c87302010-07-01 08:10:18 -07001451 if (packageName == null) {
1452 return false;
1453 }
1454
1455 final int packageUid = mPms.getPackageUid(packageName);
1456
1457 if (DEBUG_OBB) {
1458 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
1459 packageUid + ", callerUid = " + callerUid);
1460 }
1461
1462 return callerUid == packageUid;
1463 }
1464
1465 public String getMountedObbPath(String filename) {
1466 waitForReady();
1467 warnOnNotMounted();
1468
Kenny Root02c87302010-07-01 08:10:18 -07001469 try {
1470 ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename));
1471 String []tok = rsp.get(0).split(" ");
1472 int code = Integer.parseInt(tok[0]);
1473 if (code != VoldResponseCode.AsecPathResult) {
1474 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1475 }
1476 return tok[1];
1477 } catch (NativeDaemonConnectorException e) {
1478 int code = e.getCode();
1479 if (code == VoldResponseCode.OpFailedStorageNotFound) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001480 return null;
Kenny Root02c87302010-07-01 08:10:18 -07001481 } else {
1482 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1483 }
1484 }
1485 }
1486
1487 public boolean isObbMounted(String filename) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001488 synchronized (mObbMounts) {
1489 return mObbPathToStateMap.containsKey(filename);
Kenny Root02c87302010-07-01 08:10:18 -07001490 }
1491 }
1492
Kenny Roota02b8b02010-08-05 16:14:17 -07001493 public void mountObb(String filename, String key, IObbActionListener token) {
Kenny Root02c87302010-07-01 08:10:18 -07001494 waitForReady();
1495 warnOnNotMounted();
1496
Kenny Roota02b8b02010-08-05 16:14:17 -07001497 final ObbState obbState;
1498
1499 synchronized (mObbMounts) {
1500 if (isObbMounted(filename)) {
1501 throw new IllegalArgumentException("OBB file is already mounted");
Kenny Root02c87302010-07-01 08:10:18 -07001502 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001503
1504 if (mObbMounts.containsKey(token)) {
1505 throw new IllegalArgumentException("You may only have one OBB mounted at a time");
1506 }
1507
1508 final int callerUid = Binder.getCallingUid();
1509 obbState = new ObbState(filename, token, callerUid);
1510 addObbState(obbState);
Kenny Root02c87302010-07-01 08:10:18 -07001511 }
1512
Kenny Root02c87302010-07-01 08:10:18 -07001513 try {
Kenny Roota02b8b02010-08-05 16:14:17 -07001514 token.asBinder().linkToDeath(obbState, 0);
1515 } catch (RemoteException rex) {
1516 Slog.e(TAG, "Failed to link to listener death");
Kenny Root02c87302010-07-01 08:10:18 -07001517 }
1518
Kenny Roota02b8b02010-08-05 16:14:17 -07001519 MountObbAction action = new MountObbAction(obbState, key);
1520 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
1521
1522 if (DEBUG_OBB)
1523 Slog.i(TAG, "Send to OBB handler: " + action.toString());
Kenny Root02c87302010-07-01 08:10:18 -07001524 }
1525
Kenny Roota02b8b02010-08-05 16:14:17 -07001526 public void unmountObb(String filename, boolean force, IObbActionListener token) {
1527 final ObbState obbState;
Kenny Root02c87302010-07-01 08:10:18 -07001528
Kenny Roota02b8b02010-08-05 16:14:17 -07001529 synchronized (mObbMounts) {
1530 if (!isObbMounted(filename)) {
1531 throw new IllegalArgumentException("OBB is not mounted");
1532 }
1533 obbState = mObbPathToStateMap.get(filename);
Kenny Root02c87302010-07-01 08:10:18 -07001534 }
1535
Kenny Roota02b8b02010-08-05 16:14:17 -07001536 UnmountObbAction action = new UnmountObbAction(obbState, force);
1537 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root02c87302010-07-01 08:10:18 -07001538
Kenny Roota02b8b02010-08-05 16:14:17 -07001539 if (DEBUG_OBB)
1540 Slog.i(TAG, "Send to OBB handler: " + action.toString());
1541 }
1542
1543 private void addObbState(ObbState obbState) {
1544 synchronized (mObbMounts) {
1545 mObbMounts.put(obbState.token, obbState);
1546 mObbPathToStateMap.put(obbState.filename, obbState);
1547 }
1548 }
1549
1550 private void removeObbState(ObbState obbState) {
1551 synchronized (mObbMounts) {
1552 mObbMounts.remove(obbState.token);
1553 mObbPathToStateMap.remove(obbState.filename);
1554 }
1555 }
1556
1557 private class ObbActionHandler extends Handler {
1558 private boolean mBound = false;
1559 private List<ObbAction> mActions = new LinkedList<ObbAction>();
1560
1561 ObbActionHandler(Looper l) {
1562 super(l);
1563 }
1564
1565 @Override
1566 public void handleMessage(Message msg) {
1567 switch (msg.what) {
1568 case OBB_RUN_ACTION: {
1569 ObbAction action = (ObbAction) msg.obj;
1570
1571 if (DEBUG_OBB)
1572 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
1573
1574 // If a bind was already initiated we don't really
1575 // need to do anything. The pending install
1576 // will be processed later on.
1577 if (!mBound) {
1578 // If this is the only one pending we might
1579 // have to bind to the service again.
1580 if (!connectToService()) {
1581 Slog.e(TAG, "Failed to bind to media container service");
1582 action.handleError();
1583 return;
1584 } else {
1585 // Once we bind to the service, the first
1586 // pending request will be processed.
1587 mActions.add(action);
1588 }
1589 } else {
1590 // Already bound to the service. Just make
1591 // sure we trigger off processing the first request.
1592 if (mActions.size() == 0) {
1593 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1594 }
1595
1596 mActions.add(action);
1597 }
1598 break;
1599 }
1600 case OBB_MCS_BOUND: {
1601 if (DEBUG_OBB)
1602 Slog.i(TAG, "OBB_MCS_BOUND");
1603 if (msg.obj != null) {
1604 mContainerService = (IMediaContainerService) msg.obj;
1605 }
1606 if (mContainerService == null) {
1607 // Something seriously wrong. Bail out
1608 Slog.e(TAG, "Cannot bind to media container service");
1609 for (ObbAction action : mActions) {
1610 // Indicate service bind error
1611 action.handleError();
1612 }
1613 mActions.clear();
1614 } else if (mActions.size() > 0) {
1615 ObbAction action = mActions.get(0);
1616 if (action != null) {
1617 action.execute(this);
1618 }
1619 } else {
1620 // Should never happen ideally.
1621 Slog.w(TAG, "Empty queue");
1622 }
1623 break;
1624 }
1625 case OBB_MCS_RECONNECT: {
1626 if (DEBUG_OBB)
1627 Slog.i(TAG, "OBB_MCS_RECONNECT");
1628 if (mActions.size() > 0) {
1629 if (mBound) {
1630 disconnectService();
1631 }
1632 if (!connectToService()) {
1633 Slog.e(TAG, "Failed to bind to media container service");
1634 for (ObbAction action : mActions) {
1635 // Indicate service bind error
1636 action.handleError();
1637 }
1638 mActions.clear();
1639 }
1640 }
1641 break;
1642 }
1643 case OBB_MCS_UNBIND: {
1644 if (DEBUG_OBB)
1645 Slog.i(TAG, "OBB_MCS_UNBIND");
1646
1647 // Delete pending install
1648 if (mActions.size() > 0) {
1649 mActions.remove(0);
1650 }
1651 if (mActions.size() == 0) {
1652 if (mBound) {
1653 disconnectService();
1654 }
1655 } else {
1656 // There are more pending requests in queue.
1657 // Just post MCS_BOUND message to trigger processing
1658 // of next pending install.
1659 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1660 }
1661 break;
1662 }
1663 case OBB_MCS_GIVE_UP: {
1664 if (DEBUG_OBB)
1665 Slog.i(TAG, "OBB_MCS_GIVE_UP");
1666 mActions.remove(0);
1667 break;
1668 }
1669 }
1670 }
1671
1672 private boolean connectToService() {
1673 if (DEBUG_OBB)
1674 Slog.i(TAG, "Trying to bind to DefaultContainerService");
1675
1676 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
1677 if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) {
1678 mBound = true;
1679 return true;
1680 }
1681 return false;
1682 }
1683
1684 private void disconnectService() {
1685 mContainerService = null;
1686 mBound = false;
1687 mContext.unbindService(mDefContainerConn);
1688 }
1689 }
1690
1691 abstract class ObbAction {
1692 private static final int MAX_RETRIES = 3;
1693 private int mRetries;
1694
1695 ObbState mObbState;
1696
1697 ObbAction(ObbState obbState) {
1698 mObbState = obbState;
1699 }
1700
1701 public void execute(ObbActionHandler handler) {
1702 try {
1703 if (DEBUG_OBB)
1704 Slog.i(TAG, "Starting to execute action: " + this.toString());
1705 mRetries++;
1706 if (mRetries > MAX_RETRIES) {
1707 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
1708 mObbActionHandler.sendEmptyMessage(OBB_MCS_GIVE_UP);
1709 handleError();
1710 return;
1711 } else {
1712 handleExecute();
1713 if (DEBUG_OBB)
1714 Slog.i(TAG, "Posting install MCS_UNBIND");
1715 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1716 }
1717 } catch (RemoteException e) {
1718 if (DEBUG_OBB)
1719 Slog.i(TAG, "Posting install MCS_RECONNECT");
1720 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
1721 } catch (Exception e) {
1722 if (DEBUG_OBB)
1723 Slog.d(TAG, "Error handling OBB action", e);
1724 handleError();
1725 }
1726 }
1727
1728 abstract void handleExecute() throws RemoteException;
1729 abstract void handleError();
1730 }
1731
1732 class MountObbAction extends ObbAction {
1733 private String mKey;
1734
1735 MountObbAction(ObbState obbState, String key) {
1736 super(obbState);
1737 mKey = key;
1738 }
1739
1740 public void handleExecute() throws RemoteException {
1741 ObbInfo obbInfo = mContainerService.getObbInfo(mObbState.filename);
1742 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) {
1743 throw new IllegalArgumentException("Caller package does not match OBB file");
1744 }
1745
1746 if (mKey == null) {
1747 mKey = "none";
1748 }
1749
1750 int rc = StorageResultCode.OperationSucceeded;
1751 String cmd = String.format("obb mount %s %s %d", mObbState.filename, mKey,
1752 mObbState.callerUid);
1753 try {
1754 mConnector.doCommand(cmd);
1755 } catch (NativeDaemonConnectorException e) {
1756 int code = e.getCode();
1757 if (code != VoldResponseCode.OpFailedStorageBusy) {
1758 rc = StorageResultCode.OperationFailedInternalError;
1759 }
1760 }
1761
1762 if (rc == StorageResultCode.OperationSucceeded) {
1763 try {
1764 mObbState.token.onObbResult(mObbState.filename, "mounted");
1765 } catch (RemoteException e) {
1766 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1767 }
Kenny Root02c87302010-07-01 08:10:18 -07001768 } else {
Kenny Roota02b8b02010-08-05 16:14:17 -07001769 Slog.e(TAG, "Couldn't mount OBB file");
1770
1771 // We didn't succeed, so remove this from the mount-set.
1772 removeObbState(mObbState);
Kenny Root02c87302010-07-01 08:10:18 -07001773 }
1774 }
1775
Kenny Roota02b8b02010-08-05 16:14:17 -07001776 public void handleError() {
1777 removeObbState(mObbState);
1778
1779 try {
1780 mObbState.token.onObbResult(mObbState.filename, "error");
1781 } catch (RemoteException e) {
1782 Slog.e(TAG, "Couldn't send back OBB mount error for " + mObbState.filename);
Kenny Root02c87302010-07-01 08:10:18 -07001783 }
1784 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001785
1786 @Override
1787 public String toString() {
1788 StringBuilder sb = new StringBuilder();
1789 sb.append("MountObbAction{");
1790 sb.append("filename=");
1791 sb.append(mObbState.filename);
1792 sb.append(",callerUid=");
1793 sb.append(mObbState.callerUid);
1794 sb.append(",token=");
1795 sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL");
1796 sb.append('}');
1797 return sb.toString();
1798 }
1799 }
1800
1801 class UnmountObbAction extends ObbAction {
1802 private boolean mForceUnmount;
1803
1804 UnmountObbAction(ObbState obbState, boolean force) {
1805 super(obbState);
1806 mForceUnmount = force;
1807 }
1808
1809 public void handleExecute() throws RemoteException {
1810 ObbInfo obbInfo = mContainerService.getObbInfo(mObbState.filename);
1811
1812 if (!isCallerOwnerOfPackageOrSystem(obbInfo.packageName)) {
1813 throw new IllegalArgumentException("Caller package does not match OBB file");
1814 }
1815
1816 int rc = StorageResultCode.OperationSucceeded;
1817 String cmd = String.format("obb unmount %s%s", mObbState.filename,
1818 (mForceUnmount ? " force" : ""));
1819 try {
1820 mConnector.doCommand(cmd);
1821 } catch (NativeDaemonConnectorException e) {
1822 int code = e.getCode();
1823 if (code == VoldResponseCode.OpFailedStorageBusy) {
1824 rc = StorageResultCode.OperationFailedStorageBusy;
1825 } else {
1826 rc = StorageResultCode.OperationFailedInternalError;
1827 }
1828 }
1829
1830 if (rc == StorageResultCode.OperationSucceeded) {
1831 removeObbState(mObbState);
1832
1833 try {
1834 mObbState.token.onObbResult(mObbState.filename, "unmounted");
1835 } catch (RemoteException e) {
1836 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1837 }
1838 } else {
1839 try {
1840 mObbState.token.onObbResult(mObbState.filename, "error");
1841 } catch (RemoteException e) {
1842 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1843 }
1844 }
1845 }
1846
1847 public void handleError() {
1848 removeObbState(mObbState);
1849
1850 try {
1851 mObbState.token.onObbResult(mObbState.filename, "error");
1852 } catch (RemoteException e) {
1853 Slog.e(TAG, "Couldn't send back OBB unmount error for " + mObbState.filename);
1854 }
1855 }
1856
1857 @Override
1858 public String toString() {
1859 StringBuilder sb = new StringBuilder();
1860 sb.append("UnmountObbAction{");
1861 sb.append("filename=");
1862 sb.append(mObbState.filename != null ? mObbState.filename : "null");
1863 sb.append(",force=");
1864 sb.append(mForceUnmount);
1865 sb.append(",callerUid=");
1866 sb.append(mObbState.callerUid);
1867 sb.append(",token=");
1868 sb.append(mObbState.token != null ? mObbState.token.toString() : "null");
1869 sb.append('}');
1870 return sb.toString();
1871 }
Kenny Root02c87302010-07-01 08:10:18 -07001872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873}
1874