blob: 32f5e730fb880317a17aecbdab8a7828ffa8b5be [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 }
Kenny Rootaa485402010-09-14 14:49:41 -0700835
836 /*
837 * Force a GC to make sure AssetManagers in other threads of the
838 * system_server are cleaned up. We have to do this since AssetManager
839 * instances are kept as a WeakReference and it's possible we have files
840 * open on the external storage.
841 */
842 Runtime.getRuntime().gc();
843
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800844 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700845 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800846 try {
San Mehatd9709982010-02-18 11:43:03 -0800847 mConnector.doCommand(String.format(
848 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700849 // We unmounted the volume. None of the asec containers are available now.
850 synchronized (mAsecMountSet) {
851 mAsecMountSet.clear();
852 }
San Mehatb1043402010-02-05 08:26:50 -0800853 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800854 } catch (NativeDaemonConnectorException e) {
855 // Don't worry about mismatch in PackageManager since the
856 // call back will handle the status changes any way.
857 int code = e.getCode();
858 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800859 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800860 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
861 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800862 } else {
San Mehatb1043402010-02-05 08:26:50 -0800863 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800864 }
865 }
866 }
867
868 private int doFormatVolume(String path) {
869 try {
870 String cmd = String.format("volume format %s", path);
871 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800872 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800873 } catch (NativeDaemonConnectorException e) {
874 int code = e.getCode();
875 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800876 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800877 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800878 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800879 } else {
San Mehatb1043402010-02-05 08:26:50 -0800880 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800881 }
882 }
883 }
884
San Mehatb1043402010-02-05 08:26:50 -0800885 private boolean doGetVolumeShared(String path, String method) {
886 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700887 ArrayList<String> rsp;
888
889 try {
890 rsp = mConnector.doCommand(cmd);
891 } catch (NativeDaemonConnectorException ex) {
892 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
893 return false;
894 }
San Mehatb1043402010-02-05 08:26:50 -0800895
896 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700897 String[] tok = line.split(" ");
898 if (tok.length < 3) {
899 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
900 return false;
901 }
902
San Mehatb1043402010-02-05 08:26:50 -0800903 int code;
904 try {
905 code = Integer.parseInt(tok[0]);
906 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700907 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800908 return false;
909 }
910 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700911 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800912 } else {
San Mehata5078592010-03-25 09:36:54 -0700913 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800914 return false;
915 }
916 }
San Mehata5078592010-03-25 09:36:54 -0700917 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800918 return false;
919 }
920
San Mehat207e5382010-02-04 20:46:54 -0800921 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800922 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -0700923 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800924 return;
925 }
926
927 synchronized (mListeners) {
928 for (int i = mListeners.size() -1; i >= 0; i--) {
929 MountServiceBinderListener bl = mListeners.get(i);
930 try {
San Mehatb1043402010-02-05 08:26:50 -0800931 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -0800932 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700933 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800934 mListeners.remove(i);
935 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700936 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800937 }
938 }
939 }
940
San Mehat207e5382010-02-04 20:46:54 -0800941 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -0800942 sendUmsIntent(avail);
943 } else {
944 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -0800945 }
San Mehat2fe718a2010-03-11 12:01:49 -0800946
947 final String path = Environment.getExternalStorageDirectory().getPath();
948 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
949 /*
950 * USB mass storage disconnected while enabled
951 */
952 new Thread() {
953 public void run() {
954 try {
955 int rc;
San Mehata5078592010-03-25 09:36:54 -0700956 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -0800957 doShareUnshareVolume(path, "ums", false);
958 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700959 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -0800960 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
961 path, rc));
962 }
963 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700964 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -0800965 }
966 }
967 }.start();
968 }
San Mehat4270e1e2010-01-29 05:32:19 -0800969 }
970
San Mehat6a965af22010-02-24 17:47:30 -0800971 private void sendUmsIntent(boolean c) {
972 mContext.sendBroadcast(
973 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
974 }
975
San Mehat207e5382010-02-04 20:46:54 -0800976 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -0800977 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
978 throw new SecurityException(String.format("Requires %s permission", perm));
979 }
980 }
981
982 /**
San Mehat207e5382010-02-04 20:46:54 -0800983 * Constructs a new MountService instance
984 *
985 * @param context Binder context for this service
986 */
987 public MountService(Context context) {
988 mContext = context;
989
Mike Lockwood03559752010-07-19 18:25:03 -0400990 mEmulateExternalStorage = context.getResources().getBoolean(
991 com.android.internal.R.bool.config_emulateExternalStorage);
992 if (mEmulateExternalStorage) {
993 Slog.d(TAG, "using emulated external storage");
994 mLegacyState = Environment.MEDIA_MOUNTED;
995 }
996
San Mehat207e5382010-02-04 20:46:54 -0800997 // XXX: This will go away soon in favor of IMountServiceObserver
998 mPms = (PackageManagerService) ServiceManager.getService("package");
999
1000 mContext.registerReceiver(mBroadcastReceiver,
1001 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
1002
Daniel Sandler5f27ef42010-03-16 15:42:02 -04001003 mHandlerThread = new HandlerThread("MountService");
1004 mHandlerThread.start();
1005 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
1006
Kenny Roota02b8b02010-08-05 16:14:17 -07001007 // Add OBB Action Handler to MountService thread.
1008 mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper());
1009
Marco Nelissenc34ebce2010-02-18 13:39:41 -08001010 /*
1011 * Vold does not run in the simulator, so pretend the connector thread
1012 * ran and did its thing.
1013 */
1014 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
1015 mReady = true;
1016 mUmsEnabling = true;
1017 return;
1018 }
1019
Kenny Root305bcbf2010-09-03 07:56:38 -07001020 /*
1021 * Create the connection to vold with a maximum queue of twice the
1022 * amount of containers we'd ever expect to have. This keeps an
1023 * "asec list" from blocking a thread repeatedly.
1024 */
1025 mConnector = new NativeDaemonConnector(this, "vold",
1026 PackageManagerService.MAX_CONTAINERS * 2, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001027 mReady = false;
Kenny Root305bcbf2010-09-03 07:56:38 -07001028 Thread thread = new Thread(mConnector, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001029 thread.start();
1030 }
1031
1032 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001033 * Exposed API calls below here
1034 */
1035
1036 public void registerListener(IMountServiceListener listener) {
1037 synchronized (mListeners) {
1038 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
1039 try {
1040 listener.asBinder().linkToDeath(bl, 0);
1041 mListeners.add(bl);
1042 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001043 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -08001044 }
1045 }
1046 }
1047
1048 public void unregisterListener(IMountServiceListener listener) {
1049 synchronized (mListeners) {
1050 for(MountServiceBinderListener bl : mListeners) {
1051 if (bl.mListener == listener) {
1052 mListeners.remove(mListeners.indexOf(bl));
1053 return;
1054 }
1055 }
1056 }
1057 }
1058
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001059 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -08001060 validatePermission(android.Manifest.permission.SHUTDOWN);
1061
San Mehata5078592010-03-25 09:36:54 -07001062 Slog.i(TAG, "Shutting down");
San Mehat4270e1e2010-01-29 05:32:19 -08001063
1064 String path = Environment.getExternalStorageDirectory().getPath();
1065 String state = getVolumeState(path);
San Mehat91c77612010-01-07 10:39:41 -08001066
1067 if (state.equals(Environment.MEDIA_SHARED)) {
1068 /*
1069 * If the media is currently shared, unshare it.
1070 * XXX: This is still dangerous!. We should not
1071 * be rebooting at *all* if UMS is enabled, since
1072 * the UMS host could have dirty FAT cache entries
1073 * yet to flush.
1074 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001075 setUsbMassStorageEnabled(false);
San Mehat91c77612010-01-07 10:39:41 -08001076 } else if (state.equals(Environment.MEDIA_CHECKING)) {
1077 /*
1078 * If the media is being checked, then we need to wait for
1079 * it to complete before being able to proceed.
1080 */
1081 // XXX: @hackbod - Should we disable the ANR timer here?
1082 int retries = 30;
1083 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
1084 try {
1085 Thread.sleep(1000);
1086 } catch (InterruptedException iex) {
San Mehata5078592010-03-25 09:36:54 -07001087 Slog.e(TAG, "Interrupted while waiting for media", iex);
San Mehat91c77612010-01-07 10:39:41 -08001088 break;
1089 }
1090 state = Environment.getExternalStorageState();
1091 }
1092 if (retries == 0) {
San Mehata5078592010-03-25 09:36:54 -07001093 Slog.e(TAG, "Timed out waiting for media to check");
San Mehat91c77612010-01-07 10:39:41 -08001094 }
1095 }
1096
1097 if (state.equals(Environment.MEDIA_MOUNTED)) {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001098 // Post a unmount message.
1099 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
1100 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
San Mehat4270e1e2010-01-29 05:32:19 -08001101 }
1102 }
1103
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001104 private boolean getUmsEnabling() {
1105 synchronized (mListeners) {
1106 return mUmsEnabling;
1107 }
1108 }
1109
1110 private void setUmsEnabling(boolean enable) {
1111 synchronized (mListeners) {
Tony Wufc711252010-08-09 16:49:19 +08001112 mUmsEnabling = enable;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001113 }
1114 }
1115
San Mehatb1043402010-02-05 08:26:50 -08001116 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -08001117 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -08001118
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001119 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001120 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001121 }
San Mehatb1043402010-02-05 08:26:50 -08001122 return doGetShareMethodAvailable("ums");
1123 }
1124
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001125 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001126 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001127 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001128
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001129 // TODO: Add support for multiple share methods
1130
1131 /*
1132 * If the volume is mounted and we're enabling then unmount it
1133 */
1134 String path = Environment.getExternalStorageDirectory().getPath();
1135 String vs = getVolumeState(path);
1136 String method = "ums";
1137 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1138 // Override for isUsbMassStorageEnabled()
1139 setUmsEnabling(enable);
1140 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1141 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1142 // Clear override
1143 setUmsEnabling(false);
1144 }
1145 /*
1146 * If we disabled UMS then mount the volume
1147 */
1148 if (!enable) {
1149 doShareUnshareVolume(path, method, enable);
1150 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001151 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001152 " after disabling share method " + method);
1153 /*
1154 * Even though the mount failed, the unshare didn't so don't indicate an error.
1155 * The mountVolume() call will have set the storage state and sent the necessary
1156 * broadcasts.
1157 */
1158 }
1159 }
San Mehatb1043402010-02-05 08:26:50 -08001160 }
1161
1162 public boolean isUsbMassStorageEnabled() {
1163 waitForReady();
1164 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 }
San Mehat4270e1e2010-01-29 05:32:19 -08001166
San Mehat7fd0fee2009-12-17 07:12:23 -08001167 /**
1168 * @return state of the volume at the specified mount point
1169 */
San Mehat4270e1e2010-01-29 05:32:19 -08001170 public String getVolumeState(String mountPoint) {
San Mehat7fd0fee2009-12-17 07:12:23 -08001171 /*
1172 * XXX: Until we have multiple volume discovery, just hardwire
1173 * this to /sdcard
1174 */
1175 if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -07001176 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
San Mehat7fd0fee2009-12-17 07:12:23 -08001177 throw new IllegalArgumentException();
1178 }
1179
1180 return mLegacyState;
1181 }
1182
San Mehat4270e1e2010-01-29 05:32:19 -08001183 public int mountVolume(String path) {
1184 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001185
San Mehat207e5382010-02-04 20:46:54 -08001186 waitForReady();
1187 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 }
1189
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001190 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001191 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001192 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001194 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001195 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001196 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1197 Environment.MEDIA_REMOVED.equals(volState) ||
1198 Environment.MEDIA_SHARED.equals(volState) ||
1199 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1200 // Media already unmounted or cannot be unmounted.
1201 // TODO return valid return code when adding observer call back.
1202 return;
1203 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001204 UnmountCallBack ucb = new UnmountCallBack(path, force);
1205 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 }
1207
San Mehat4270e1e2010-01-29 05:32:19 -08001208 public int formatVolume(String path) {
1209 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001210 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001211
San Mehat207e5382010-02-04 20:46:54 -08001212 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 }
1214
San Mehatc1b4ce92010-02-16 17:13:03 -08001215 public int []getStorageUsers(String path) {
1216 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1217 waitForReady();
1218 try {
1219 String[] r = mConnector.doListCommand(
1220 String.format("storage users %s", path),
1221 VoldResponseCode.StorageUsersListResult);
1222 // FMT: <pid> <process name>
1223 int[] data = new int[r.length];
1224 for (int i = 0; i < r.length; i++) {
1225 String []tok = r[i].split(" ");
1226 try {
1227 data[i] = Integer.parseInt(tok[0]);
1228 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001229 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001230 return new int[0];
1231 }
1232 }
1233 return data;
1234 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001235 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001236 return new int[0];
1237 }
1238 }
1239
San Mehatb1043402010-02-05 08:26:50 -08001240 private void warnOnNotMounted() {
1241 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001242 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001243 }
1244 }
1245
San Mehat4270e1e2010-01-29 05:32:19 -08001246 public String[] getSecureContainerList() {
1247 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001248 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001249 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001250
San Mehat4270e1e2010-01-29 05:32:19 -08001251 try {
1252 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1253 } catch (NativeDaemonConnectorException e) {
1254 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 }
1256 }
San Mehat36972292010-01-06 11:06:32 -08001257
San Mehat4270e1e2010-01-29 05:32:19 -08001258 public int createSecureContainer(String id, int sizeMb, String fstype,
1259 String key, int ownerUid) {
1260 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001261 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001262 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001263
San Mehatb1043402010-02-05 08:26:50 -08001264 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001265 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1266 try {
1267 mConnector.doCommand(cmd);
1268 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001269 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001270 }
San Mehata181b212010-02-11 06:50:20 -08001271
1272 if (rc == StorageResultCode.OperationSucceeded) {
1273 synchronized (mAsecMountSet) {
1274 mAsecMountSet.add(id);
1275 }
1276 }
San Mehat4270e1e2010-01-29 05:32:19 -08001277 return rc;
San Mehat36972292010-01-06 11:06:32 -08001278 }
1279
San Mehat4270e1e2010-01-29 05:32:19 -08001280 public int finalizeSecureContainer(String id) {
1281 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001282 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001283
San Mehatb1043402010-02-05 08:26:50 -08001284 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001285 try {
1286 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001287 /*
1288 * Finalization does a remount, so no need
1289 * to update mAsecMountSet
1290 */
San Mehat4270e1e2010-01-29 05:32:19 -08001291 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001292 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001293 }
San Mehat4270e1e2010-01-29 05:32:19 -08001294 return rc;
San Mehat36972292010-01-06 11:06:32 -08001295 }
1296
San Mehatd9709982010-02-18 11:43:03 -08001297 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001298 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001299 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001300 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001301
Kenny Rootaa485402010-09-14 14:49:41 -07001302 /*
1303 * Force a GC to make sure AssetManagers in other threads of the
1304 * system_server are cleaned up. We have to do this since AssetManager
1305 * instances are kept as a WeakReference and it's possible we have files
1306 * open on the external storage.
1307 */
1308 Runtime.getRuntime().gc();
1309
San Mehatb1043402010-02-05 08:26:50 -08001310 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001311 try {
San Mehatd9709982010-02-18 11:43:03 -08001312 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001313 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001314 int code = e.getCode();
1315 if (code == VoldResponseCode.OpFailedStorageBusy) {
1316 rc = StorageResultCode.OperationFailedStorageBusy;
1317 } else {
1318 rc = StorageResultCode.OperationFailedInternalError;
1319 }
San Mehat02735bc2010-01-26 15:18:08 -08001320 }
San Mehata181b212010-02-11 06:50:20 -08001321
1322 if (rc == StorageResultCode.OperationSucceeded) {
1323 synchronized (mAsecMountSet) {
1324 if (mAsecMountSet.contains(id)) {
1325 mAsecMountSet.remove(id);
1326 }
1327 }
1328 }
1329
San Mehat4270e1e2010-01-29 05:32:19 -08001330 return rc;
San Mehat36972292010-01-06 11:06:32 -08001331 }
1332
San Mehat4270e1e2010-01-29 05:32:19 -08001333 public int mountSecureContainer(String id, String key, int ownerUid) {
1334 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 Mehata181b212010-02-11 06:50:20 -08001338 synchronized (mAsecMountSet) {
1339 if (mAsecMountSet.contains(id)) {
1340 return StorageResultCode.OperationFailedStorageMounted;
1341 }
1342 }
1343
San Mehatb1043402010-02-05 08:26:50 -08001344 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001345 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1346 try {
1347 mConnector.doCommand(cmd);
1348 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001349 int code = e.getCode();
1350 if (code != VoldResponseCode.OpFailedStorageBusy) {
1351 rc = StorageResultCode.OperationFailedInternalError;
1352 }
San Mehat02735bc2010-01-26 15:18:08 -08001353 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001354
1355 if (rc == StorageResultCode.OperationSucceeded) {
1356 synchronized (mAsecMountSet) {
1357 mAsecMountSet.add(id);
1358 }
1359 }
San Mehat4270e1e2010-01-29 05:32:19 -08001360 return rc;
San Mehat36972292010-01-06 11:06:32 -08001361 }
1362
San Mehatd9709982010-02-18 11:43:03 -08001363 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001364 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001365 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001366 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001367
San Mehat6cdd9c02010-02-09 14:45:20 -08001368 synchronized (mAsecMountSet) {
1369 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001370 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001371 }
1372 }
1373
Kenny Rootaa485402010-09-14 14:49:41 -07001374 /*
1375 * Force a GC to make sure AssetManagers in other threads of the
1376 * system_server are cleaned up. We have to do this since AssetManager
1377 * instances are kept as a WeakReference and it's possible we have files
1378 * open on the external storage.
1379 */
1380 Runtime.getRuntime().gc();
1381
San Mehatb1043402010-02-05 08:26:50 -08001382 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001383 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001384 try {
1385 mConnector.doCommand(cmd);
1386 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001387 int code = e.getCode();
1388 if (code == VoldResponseCode.OpFailedStorageBusy) {
1389 rc = StorageResultCode.OperationFailedStorageBusy;
1390 } else {
1391 rc = StorageResultCode.OperationFailedInternalError;
1392 }
San Mehat02735bc2010-01-26 15:18:08 -08001393 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001394
1395 if (rc == StorageResultCode.OperationSucceeded) {
1396 synchronized (mAsecMountSet) {
1397 mAsecMountSet.remove(id);
1398 }
1399 }
San Mehat4270e1e2010-01-29 05:32:19 -08001400 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001401 }
1402
San Mehat6cdd9c02010-02-09 14:45:20 -08001403 public boolean isSecureContainerMounted(String id) {
1404 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1405 waitForReady();
1406 warnOnNotMounted();
1407
1408 synchronized (mAsecMountSet) {
1409 return mAsecMountSet.contains(id);
1410 }
1411 }
1412
San Mehat4270e1e2010-01-29 05:32:19 -08001413 public int renameSecureContainer(String oldId, String newId) {
1414 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001415 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001416 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001417
San Mehata181b212010-02-11 06:50:20 -08001418 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001419 /*
1420 * Because a mounted container has active internal state which cannot be
1421 * changed while active, we must ensure both ids are not currently mounted.
1422 */
1423 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001424 return StorageResultCode.OperationFailedStorageMounted;
1425 }
1426 }
1427
San Mehatb1043402010-02-05 08:26:50 -08001428 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001429 String cmd = String.format("asec rename %s %s", oldId, newId);
1430 try {
1431 mConnector.doCommand(cmd);
1432 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001433 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001434 }
San Mehata181b212010-02-11 06:50:20 -08001435
San Mehat4270e1e2010-01-29 05:32:19 -08001436 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001437 }
1438
San Mehat4270e1e2010-01-29 05:32:19 -08001439 public String getSecureContainerPath(String id) {
1440 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001441 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001442 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001443
San Mehat2d66cef2010-03-23 11:12:52 -07001444 try {
1445 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1446 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001447 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001448 if (code != VoldResponseCode.AsecPathResult) {
1449 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1450 }
1451 return tok[1];
1452 } catch (NativeDaemonConnectorException e) {
1453 int code = e.getCode();
1454 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1455 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001456 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001457 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001458 }
1459 }
San Mehat22dd86e2010-01-12 12:21:18 -08001460 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001461
1462 public void finishMediaUpdate() {
1463 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1464 }
Kenny Root02c87302010-07-01 08:10:18 -07001465
Kenny Roota02b8b02010-08-05 16:14:17 -07001466 private boolean isCallerOwnerOfPackageOrSystem(String packageName) {
Kenny Root02c87302010-07-01 08:10:18 -07001467 final int callerUid = Binder.getCallingUid();
Kenny Roota02b8b02010-08-05 16:14:17 -07001468 return isUidOwnerOfPackageOrSystem(packageName, callerUid);
Kenny Root02c87302010-07-01 08:10:18 -07001469 }
1470
Kenny Roota02b8b02010-08-05 16:14:17 -07001471 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
1472 if (callerUid == android.os.Process.SYSTEM_UID) {
1473 return true;
1474 }
1475
Kenny Root02c87302010-07-01 08:10:18 -07001476 if (packageName == null) {
1477 return false;
1478 }
1479
1480 final int packageUid = mPms.getPackageUid(packageName);
1481
1482 if (DEBUG_OBB) {
1483 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
1484 packageUid + ", callerUid = " + callerUid);
1485 }
1486
1487 return callerUid == packageUid;
1488 }
1489
1490 public String getMountedObbPath(String filename) {
1491 waitForReady();
1492 warnOnNotMounted();
1493
Kenny Root02c87302010-07-01 08:10:18 -07001494 try {
1495 ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename));
1496 String []tok = rsp.get(0).split(" ");
1497 int code = Integer.parseInt(tok[0]);
1498 if (code != VoldResponseCode.AsecPathResult) {
1499 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1500 }
1501 return tok[1];
1502 } catch (NativeDaemonConnectorException e) {
1503 int code = e.getCode();
1504 if (code == VoldResponseCode.OpFailedStorageNotFound) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001505 return null;
Kenny Root02c87302010-07-01 08:10:18 -07001506 } else {
1507 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1508 }
1509 }
1510 }
1511
1512 public boolean isObbMounted(String filename) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001513 synchronized (mObbMounts) {
1514 return mObbPathToStateMap.containsKey(filename);
Kenny Root02c87302010-07-01 08:10:18 -07001515 }
1516 }
1517
Kenny Roota02b8b02010-08-05 16:14:17 -07001518 public void mountObb(String filename, String key, IObbActionListener token) {
Kenny Root02c87302010-07-01 08:10:18 -07001519 waitForReady();
1520 warnOnNotMounted();
1521
Kenny Roota02b8b02010-08-05 16:14:17 -07001522 final ObbState obbState;
1523
1524 synchronized (mObbMounts) {
1525 if (isObbMounted(filename)) {
1526 throw new IllegalArgumentException("OBB file is already mounted");
Kenny Root02c87302010-07-01 08:10:18 -07001527 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001528
1529 if (mObbMounts.containsKey(token)) {
1530 throw new IllegalArgumentException("You may only have one OBB mounted at a time");
1531 }
1532
1533 final int callerUid = Binder.getCallingUid();
1534 obbState = new ObbState(filename, token, callerUid);
1535 addObbState(obbState);
Kenny Root02c87302010-07-01 08:10:18 -07001536 }
1537
Kenny Root02c87302010-07-01 08:10:18 -07001538 try {
Kenny Roota02b8b02010-08-05 16:14:17 -07001539 token.asBinder().linkToDeath(obbState, 0);
1540 } catch (RemoteException rex) {
1541 Slog.e(TAG, "Failed to link to listener death");
Kenny Root02c87302010-07-01 08:10:18 -07001542 }
1543
Kenny Roota02b8b02010-08-05 16:14:17 -07001544 MountObbAction action = new MountObbAction(obbState, key);
1545 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
1546
1547 if (DEBUG_OBB)
1548 Slog.i(TAG, "Send to OBB handler: " + action.toString());
Kenny Root02c87302010-07-01 08:10:18 -07001549 }
1550
Kenny Roota02b8b02010-08-05 16:14:17 -07001551 public void unmountObb(String filename, boolean force, IObbActionListener token) {
1552 final ObbState obbState;
Kenny Root02c87302010-07-01 08:10:18 -07001553
Kenny Roota02b8b02010-08-05 16:14:17 -07001554 synchronized (mObbMounts) {
1555 if (!isObbMounted(filename)) {
1556 throw new IllegalArgumentException("OBB is not mounted");
1557 }
1558 obbState = mObbPathToStateMap.get(filename);
Kenny Root02c87302010-07-01 08:10:18 -07001559 }
1560
Kenny Roota02b8b02010-08-05 16:14:17 -07001561 UnmountObbAction action = new UnmountObbAction(obbState, force);
1562 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root02c87302010-07-01 08:10:18 -07001563
Kenny Roota02b8b02010-08-05 16:14:17 -07001564 if (DEBUG_OBB)
1565 Slog.i(TAG, "Send to OBB handler: " + action.toString());
1566 }
1567
1568 private void addObbState(ObbState obbState) {
1569 synchronized (mObbMounts) {
1570 mObbMounts.put(obbState.token, obbState);
1571 mObbPathToStateMap.put(obbState.filename, obbState);
1572 }
1573 }
1574
1575 private void removeObbState(ObbState obbState) {
1576 synchronized (mObbMounts) {
1577 mObbMounts.remove(obbState.token);
1578 mObbPathToStateMap.remove(obbState.filename);
1579 }
1580 }
1581
1582 private class ObbActionHandler extends Handler {
1583 private boolean mBound = false;
1584 private List<ObbAction> mActions = new LinkedList<ObbAction>();
1585
1586 ObbActionHandler(Looper l) {
1587 super(l);
1588 }
1589
1590 @Override
1591 public void handleMessage(Message msg) {
1592 switch (msg.what) {
1593 case OBB_RUN_ACTION: {
1594 ObbAction action = (ObbAction) msg.obj;
1595
1596 if (DEBUG_OBB)
1597 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
1598
1599 // If a bind was already initiated we don't really
1600 // need to do anything. The pending install
1601 // will be processed later on.
1602 if (!mBound) {
1603 // If this is the only one pending we might
1604 // have to bind to the service again.
1605 if (!connectToService()) {
1606 Slog.e(TAG, "Failed to bind to media container service");
1607 action.handleError();
1608 return;
1609 } else {
1610 // Once we bind to the service, the first
1611 // pending request will be processed.
1612 mActions.add(action);
1613 }
1614 } else {
1615 // Already bound to the service. Just make
1616 // sure we trigger off processing the first request.
1617 if (mActions.size() == 0) {
1618 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1619 }
1620
1621 mActions.add(action);
1622 }
1623 break;
1624 }
1625 case OBB_MCS_BOUND: {
1626 if (DEBUG_OBB)
1627 Slog.i(TAG, "OBB_MCS_BOUND");
1628 if (msg.obj != null) {
1629 mContainerService = (IMediaContainerService) msg.obj;
1630 }
1631 if (mContainerService == null) {
1632 // Something seriously wrong. Bail out
1633 Slog.e(TAG, "Cannot bind to media container service");
1634 for (ObbAction action : mActions) {
1635 // Indicate service bind error
1636 action.handleError();
1637 }
1638 mActions.clear();
1639 } else if (mActions.size() > 0) {
1640 ObbAction action = mActions.get(0);
1641 if (action != null) {
1642 action.execute(this);
1643 }
1644 } else {
1645 // Should never happen ideally.
1646 Slog.w(TAG, "Empty queue");
1647 }
1648 break;
1649 }
1650 case OBB_MCS_RECONNECT: {
1651 if (DEBUG_OBB)
1652 Slog.i(TAG, "OBB_MCS_RECONNECT");
1653 if (mActions.size() > 0) {
1654 if (mBound) {
1655 disconnectService();
1656 }
1657 if (!connectToService()) {
1658 Slog.e(TAG, "Failed to bind to media container service");
1659 for (ObbAction action : mActions) {
1660 // Indicate service bind error
1661 action.handleError();
1662 }
1663 mActions.clear();
1664 }
1665 }
1666 break;
1667 }
1668 case OBB_MCS_UNBIND: {
1669 if (DEBUG_OBB)
1670 Slog.i(TAG, "OBB_MCS_UNBIND");
1671
1672 // Delete pending install
1673 if (mActions.size() > 0) {
1674 mActions.remove(0);
1675 }
1676 if (mActions.size() == 0) {
1677 if (mBound) {
1678 disconnectService();
1679 }
1680 } else {
1681 // There are more pending requests in queue.
1682 // Just post MCS_BOUND message to trigger processing
1683 // of next pending install.
1684 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1685 }
1686 break;
1687 }
1688 case OBB_MCS_GIVE_UP: {
1689 if (DEBUG_OBB)
1690 Slog.i(TAG, "OBB_MCS_GIVE_UP");
1691 mActions.remove(0);
1692 break;
1693 }
1694 }
1695 }
1696
1697 private boolean connectToService() {
1698 if (DEBUG_OBB)
1699 Slog.i(TAG, "Trying to bind to DefaultContainerService");
1700
1701 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
1702 if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) {
1703 mBound = true;
1704 return true;
1705 }
1706 return false;
1707 }
1708
1709 private void disconnectService() {
1710 mContainerService = null;
1711 mBound = false;
1712 mContext.unbindService(mDefContainerConn);
1713 }
1714 }
1715
1716 abstract class ObbAction {
1717 private static final int MAX_RETRIES = 3;
1718 private int mRetries;
1719
1720 ObbState mObbState;
1721
1722 ObbAction(ObbState obbState) {
1723 mObbState = obbState;
1724 }
1725
1726 public void execute(ObbActionHandler handler) {
1727 try {
1728 if (DEBUG_OBB)
1729 Slog.i(TAG, "Starting to execute action: " + this.toString());
1730 mRetries++;
1731 if (mRetries > MAX_RETRIES) {
1732 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
1733 mObbActionHandler.sendEmptyMessage(OBB_MCS_GIVE_UP);
1734 handleError();
1735 return;
1736 } else {
1737 handleExecute();
1738 if (DEBUG_OBB)
1739 Slog.i(TAG, "Posting install MCS_UNBIND");
1740 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1741 }
1742 } catch (RemoteException e) {
1743 if (DEBUG_OBB)
1744 Slog.i(TAG, "Posting install MCS_RECONNECT");
1745 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
1746 } catch (Exception e) {
1747 if (DEBUG_OBB)
1748 Slog.d(TAG, "Error handling OBB action", e);
1749 handleError();
1750 }
1751 }
1752
1753 abstract void handleExecute() throws RemoteException;
1754 abstract void handleError();
1755 }
1756
1757 class MountObbAction extends ObbAction {
1758 private String mKey;
1759
1760 MountObbAction(ObbState obbState, String key) {
1761 super(obbState);
1762 mKey = key;
1763 }
1764
1765 public void handleExecute() throws RemoteException {
1766 ObbInfo obbInfo = mContainerService.getObbInfo(mObbState.filename);
1767 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) {
1768 throw new IllegalArgumentException("Caller package does not match OBB file");
1769 }
1770
1771 if (mKey == null) {
1772 mKey = "none";
1773 }
1774
1775 int rc = StorageResultCode.OperationSucceeded;
1776 String cmd = String.format("obb mount %s %s %d", mObbState.filename, mKey,
1777 mObbState.callerUid);
1778 try {
1779 mConnector.doCommand(cmd);
1780 } catch (NativeDaemonConnectorException e) {
1781 int code = e.getCode();
1782 if (code != VoldResponseCode.OpFailedStorageBusy) {
1783 rc = StorageResultCode.OperationFailedInternalError;
1784 }
1785 }
1786
1787 if (rc == StorageResultCode.OperationSucceeded) {
1788 try {
1789 mObbState.token.onObbResult(mObbState.filename, "mounted");
1790 } catch (RemoteException e) {
1791 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1792 }
Kenny Root02c87302010-07-01 08:10:18 -07001793 } else {
Kenny Roota02b8b02010-08-05 16:14:17 -07001794 Slog.e(TAG, "Couldn't mount OBB file");
1795
1796 // We didn't succeed, so remove this from the mount-set.
1797 removeObbState(mObbState);
Kenny Root02c87302010-07-01 08:10:18 -07001798 }
1799 }
1800
Kenny Roota02b8b02010-08-05 16:14:17 -07001801 public void handleError() {
1802 removeObbState(mObbState);
1803
1804 try {
1805 mObbState.token.onObbResult(mObbState.filename, "error");
1806 } catch (RemoteException e) {
1807 Slog.e(TAG, "Couldn't send back OBB mount error for " + mObbState.filename);
Kenny Root02c87302010-07-01 08:10:18 -07001808 }
1809 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001810
1811 @Override
1812 public String toString() {
1813 StringBuilder sb = new StringBuilder();
1814 sb.append("MountObbAction{");
1815 sb.append("filename=");
1816 sb.append(mObbState.filename);
1817 sb.append(",callerUid=");
1818 sb.append(mObbState.callerUid);
1819 sb.append(",token=");
1820 sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL");
1821 sb.append('}');
1822 return sb.toString();
1823 }
1824 }
1825
1826 class UnmountObbAction extends ObbAction {
1827 private boolean mForceUnmount;
1828
1829 UnmountObbAction(ObbState obbState, boolean force) {
1830 super(obbState);
1831 mForceUnmount = force;
1832 }
1833
1834 public void handleExecute() throws RemoteException {
1835 ObbInfo obbInfo = mContainerService.getObbInfo(mObbState.filename);
1836
1837 if (!isCallerOwnerOfPackageOrSystem(obbInfo.packageName)) {
1838 throw new IllegalArgumentException("Caller package does not match OBB file");
1839 }
1840
1841 int rc = StorageResultCode.OperationSucceeded;
1842 String cmd = String.format("obb unmount %s%s", mObbState.filename,
1843 (mForceUnmount ? " force" : ""));
1844 try {
1845 mConnector.doCommand(cmd);
1846 } catch (NativeDaemonConnectorException e) {
1847 int code = e.getCode();
1848 if (code == VoldResponseCode.OpFailedStorageBusy) {
1849 rc = StorageResultCode.OperationFailedStorageBusy;
1850 } else {
1851 rc = StorageResultCode.OperationFailedInternalError;
1852 }
1853 }
1854
1855 if (rc == StorageResultCode.OperationSucceeded) {
1856 removeObbState(mObbState);
1857
1858 try {
1859 mObbState.token.onObbResult(mObbState.filename, "unmounted");
1860 } catch (RemoteException e) {
1861 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1862 }
1863 } else {
1864 try {
1865 mObbState.token.onObbResult(mObbState.filename, "error");
1866 } catch (RemoteException e) {
1867 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1868 }
1869 }
1870 }
1871
1872 public void handleError() {
1873 removeObbState(mObbState);
1874
1875 try {
1876 mObbState.token.onObbResult(mObbState.filename, "error");
1877 } catch (RemoteException e) {
1878 Slog.e(TAG, "Couldn't send back OBB unmount error for " + mObbState.filename);
1879 }
1880 }
1881
1882 @Override
1883 public String toString() {
1884 StringBuilder sb = new StringBuilder();
1885 sb.append("UnmountObbAction{");
1886 sb.append("filename=");
1887 sb.append(mObbState.filename != null ? mObbState.filename : "null");
1888 sb.append(",force=");
1889 sb.append(mForceUnmount);
1890 sb.append(",callerUid=");
1891 sb.append(mObbState.callerUid);
1892 sb.append(",token=");
1893 sb.append(mObbState.token != null ? mObbState.token.toString() : "null");
1894 sb.append('}');
1895 return sb.toString();
1896 }
Kenny Root02c87302010-07-01 08:10:18 -07001897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898}
1899