blob: d604886ed1d3821ed90550f4a293bbd2b8559b97 [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
San Mehat4270e1e2010-01-29 05:32:19 -080071 /*
72 * Internal vold volume state constants
73 */
San Mehat7fd0fee2009-12-17 07:12:23 -080074 class VolumeState {
75 public static final int Init = -1;
76 public static final int NoMedia = 0;
77 public static final int Idle = 1;
78 public static final int Pending = 2;
79 public static final int Checking = 3;
80 public static final int Mounted = 4;
81 public static final int Unmounting = 5;
82 public static final int Formatting = 6;
83 public static final int Shared = 7;
84 public static final int SharedMnt = 8;
85 }
86
San Mehat4270e1e2010-01-29 05:32:19 -080087 /*
88 * Internal vold response code constants
89 */
San Mehat22dd86e2010-01-12 12:21:18 -080090 class VoldResponseCode {
San Mehat4270e1e2010-01-29 05:32:19 -080091 /*
92 * 100 series - Requestion action was initiated; expect another reply
93 * before proceeding with a new command.
94 */
San Mehat22dd86e2010-01-12 12:21:18 -080095 public static final int VolumeListResult = 110;
96 public static final int AsecListResult = 111;
San Mehatc1b4ce92010-02-16 17:13:03 -080097 public static final int StorageUsersListResult = 112;
San Mehat22dd86e2010-01-12 12:21:18 -080098
San Mehat4270e1e2010-01-29 05:32:19 -080099 /*
100 * 200 series - Requestion action has been successfully completed.
101 */
102 public static final int ShareStatusResult = 210;
San Mehat22dd86e2010-01-12 12:21:18 -0800103 public static final int AsecPathResult = 211;
San Mehat4270e1e2010-01-29 05:32:19 -0800104 public static final int ShareEnabledResult = 212;
San Mehat22dd86e2010-01-12 12:21:18 -0800105
San Mehat4270e1e2010-01-29 05:32:19 -0800106 /*
107 * 400 series - Command was accepted, but the requested action
108 * did not take place.
109 */
110 public static final int OpFailedNoMedia = 401;
111 public static final int OpFailedMediaBlank = 402;
112 public static final int OpFailedMediaCorrupt = 403;
113 public static final int OpFailedVolNotMounted = 404;
San Mehatd9709982010-02-18 11:43:03 -0800114 public static final int OpFailedStorageBusy = 405;
San Mehat2d66cef2010-03-23 11:12:52 -0700115 public static final int OpFailedStorageNotFound = 406;
San Mehat4270e1e2010-01-29 05:32:19 -0800116
117 /*
118 * 600 series - Unsolicited broadcasts.
119 */
San Mehat22dd86e2010-01-12 12:21:18 -0800120 public static final int VolumeStateChange = 605;
San Mehat22dd86e2010-01-12 12:21:18 -0800121 public static final int ShareAvailabilityChange = 620;
122 public static final int VolumeDiskInserted = 630;
123 public static final int VolumeDiskRemoved = 631;
124 public static final int VolumeBadRemoval = 632;
125 }
126
San Mehat4270e1e2010-01-29 05:32:19 -0800127 private Context mContext;
128 private NativeDaemonConnector mConnector;
129 private String mLegacyState = Environment.MEDIA_REMOVED;
130 private PackageManagerService mPms;
131 private boolean mUmsEnabling;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800132 // Used as a lock for methods that register/unregister listeners.
133 final private ArrayList<MountServiceBinderListener> mListeners =
134 new ArrayList<MountServiceBinderListener>();
San Mehat6a965af22010-02-24 17:47:30 -0800135 private boolean mBooted = false;
136 private boolean mReady = false;
137 private boolean mSendUmsConnectedOnBoot = false;
Mike Lockwood03559752010-07-19 18:25:03 -0400138 // true if we should fake MEDIA_MOUNTED state for external storage
139 private boolean mEmulateExternalStorage = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800140
San Mehat6cdd9c02010-02-09 14:45:20 -0800141 /**
142 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800143 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800144 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800145 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800146
Kenny Root02c87302010-07-01 08:10:18 -0700147 /**
Kenny Roota02b8b02010-08-05 16:14:17 -0700148 * Mounted OBB tracking information. Used to track the current state of all
149 * OBBs.
Kenny Root02c87302010-07-01 08:10:18 -0700150 */
Kenny Roota02b8b02010-08-05 16:14:17 -0700151 final private Map<IObbActionListener, ObbState> mObbMounts = new HashMap<IObbActionListener, ObbState>();
152 final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>();
153
154 class ObbState implements IBinder.DeathRecipient {
155 public ObbState(String filename, IObbActionListener token, int callerUid) {
156 this.filename = filename;
157 this.token = token;
158 this.callerUid = callerUid;
159 mounted = false;
160 }
161
162 // OBB source filename
163 String filename;
164
165 // Token of remote Binder caller
166 IObbActionListener token;
167
168 // Binder.callingUid()
169 public int callerUid;
170
171 // Whether this is mounted currently.
172 boolean mounted;
173
174 @Override
175 public void binderDied() {
176 ObbAction action = new UnmountObbAction(this, true);
177 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
178
179 removeObbState(this);
180
181 token.asBinder().unlinkToDeath(this, 0);
182 }
183 }
184
185 // OBB Action Handler
186 final private ObbActionHandler mObbActionHandler;
187
188 // OBB action handler messages
189 private static final int OBB_RUN_ACTION = 1;
190 private static final int OBB_MCS_BOUND = 2;
191 private static final int OBB_MCS_UNBIND = 3;
192 private static final int OBB_MCS_RECONNECT = 4;
193 private static final int OBB_MCS_GIVE_UP = 5;
194
195 /*
196 * Default Container Service information
197 */
198 static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
199 "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService");
200
201 final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection();
202
203 class DefaultContainerConnection implements ServiceConnection {
204 public void onServiceConnected(ComponentName name, IBinder service) {
205 if (DEBUG_OBB)
206 Slog.i(TAG, "onServiceConnected");
207 IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service);
208 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs));
209 }
210
211 public void onServiceDisconnected(ComponentName name) {
212 if (DEBUG_OBB)
213 Slog.i(TAG, "onServiceDisconnected");
214 }
215 };
216
217 // Used in the ObbActionHandler
218 private IMediaContainerService mContainerService = null;
Kenny Root02c87302010-07-01 08:10:18 -0700219
220 // Handler messages
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800221 private static final int H_UNMOUNT_PM_UPDATE = 1;
222 private static final int H_UNMOUNT_PM_DONE = 2;
223 private static final int H_UNMOUNT_MS = 3;
224 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
225 private static final int MAX_UNMOUNT_RETRIES = 4;
226
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800227 class UnmountCallBack {
228 String path;
229 int retries;
230 boolean force;
231
232 UnmountCallBack(String path, boolean force) {
233 retries = 0;
234 this.path = path;
235 this.force = force;
236 }
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800237
238 void handleFinished() {
San Mehata5078592010-03-25 09:36:54 -0700239 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path);
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800240 doUnmountVolume(path, true);
241 }
242 }
243
244 class UmsEnableCallBack extends UnmountCallBack {
245 String method;
246
247 UmsEnableCallBack(String path, String method, boolean force) {
248 super(path, force);
249 this.method = method;
250 }
251
252 @Override
253 void handleFinished() {
254 super.handleFinished();
255 doShareUnshareVolume(path, method, true);
256 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800257 }
258
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800259 class ShutdownCallBack extends UnmountCallBack {
260 IMountShutdownObserver observer;
261 ShutdownCallBack(String path, IMountShutdownObserver observer) {
262 super(path, true);
263 this.observer = observer;
264 }
265
266 @Override
267 void handleFinished() {
268 int ret = doUnmountVolume(path, true);
269 if (observer != null) {
270 try {
271 observer.onShutDownComplete(ret);
272 } catch (RemoteException e) {
San Mehata5078592010-03-25 09:36:54 -0700273 Slog.w(TAG, "RemoteException when shutting down");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800274 }
275 }
276 }
277 }
278
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400279 class MountServiceHandler extends Handler {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800280 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700281 boolean mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800282
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400283 MountServiceHandler(Looper l) {
284 super(l);
285 }
286
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800287 public void handleMessage(Message msg) {
288 switch (msg.what) {
289 case H_UNMOUNT_PM_UPDATE: {
San Mehata5078592010-03-25 09:36:54 -0700290 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800291 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
292 mForceUnmounts.add(ucb);
San Mehata5078592010-03-25 09:36:54 -0700293 if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800294 // Register only if needed.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700295 if (!mUpdatingStatus) {
San Mehata5078592010-03-25 09:36:54 -0700296 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700297 mUpdatingStatus = true;
298 mPms.updateExternalMediaStatus(false, true);
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800299 }
300 break;
301 }
302 case H_UNMOUNT_PM_DONE: {
San Mehata5078592010-03-25 09:36:54 -0700303 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE");
San Mehata5078592010-03-25 09:36:54 -0700304 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700305 mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800306 int size = mForceUnmounts.size();
307 int sizeArr[] = new int[size];
308 int sizeArrN = 0;
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700309 // Kill processes holding references first
310 ActivityManagerService ams = (ActivityManagerService)
311 ServiceManager.getService("activity");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800312 for (int i = 0; i < size; i++) {
313 UnmountCallBack ucb = mForceUnmounts.get(i);
314 String path = ucb.path;
315 boolean done = false;
316 if (!ucb.force) {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800317 done = true;
318 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800319 int pids[] = getStorageUsers(path);
320 if (pids == null || pids.length == 0) {
321 done = true;
322 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800323 // Eliminate system process here?
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700324 ams.killPids(pids, "unmount media");
325 // Confirm if file references have been freed.
326 pids = getStorageUsers(path);
327 if (pids == null || pids.length == 0) {
328 done = true;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800329 }
330 }
331 }
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700332 if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) {
333 // Retry again
334 Slog.i(TAG, "Retrying to kill storage users again");
335 mHandler.sendMessageDelayed(
336 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
337 ucb.retries++),
338 RETRY_UNMOUNT_DELAY);
339 } else {
340 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
341 Slog.i(TAG, "Failed to unmount media inspite of " +
342 MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now");
343 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800344 sizeArr[sizeArrN++] = i;
345 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
346 ucb));
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800347 }
348 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800349 // Remove already processed elements from list.
350 for (int i = (sizeArrN-1); i >= 0; i--) {
351 mForceUnmounts.remove(sizeArr[i]);
352 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800353 break;
354 }
355 case H_UNMOUNT_MS : {
San Mehata5078592010-03-25 09:36:54 -0700356 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800357 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800358 ucb.handleFinished();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800359 break;
360 }
361 }
362 }
363 };
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400364 final private HandlerThread mHandlerThread;
365 final private Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800366
San Mehat207e5382010-02-04 20:46:54 -0800367 private void waitForReady() {
368 while (mReady == false) {
369 for (int retries = 5; retries > 0; retries--) {
370 if (mReady) {
371 return;
372 }
373 SystemClock.sleep(1000);
374 }
San Mehata5078592010-03-25 09:36:54 -0700375 Slog.w(TAG, "Waiting too long for mReady!");
San Mehat207e5382010-02-04 20:46:54 -0800376 }
San Mehat1f6301e2010-01-07 22:40:27 -0800377 }
Kenny Root02c87302010-07-01 08:10:18 -0700378
San Mehat207e5382010-02-04 20:46:54 -0800379 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 public void onReceive(Context context, Intent intent) {
San Mehat91c77612010-01-07 10:39:41 -0800381 String action = intent.getAction();
382
383 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
San Mehat207e5382010-02-04 20:46:54 -0800384 mBooted = true;
San Mehat22dd86e2010-01-12 12:21:18 -0800385
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800386 /*
387 * In the simulator, we need to broadcast a volume mounted event
388 * to make the media scanner run.
389 */
390 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
391 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted);
392 return;
393 }
San Mehatfafb0412010-02-18 19:40:04 -0800394 new Thread() {
395 public void run() {
396 try {
397 String path = Environment.getExternalStorageDirectory().getPath();
San Mehat6a254402010-03-22 10:21:00 -0700398 String state = getVolumeState(path);
399
Mike Lockwood03559752010-07-19 18:25:03 -0400400 if (mEmulateExternalStorage) {
401 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Mounted);
402 } else if (state.equals(Environment.MEDIA_UNMOUNTED)) {
San Mehatfafb0412010-02-18 19:40:04 -0800403 int rc = doMountVolume(path);
404 if (rc != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700405 Slog.e(TAG, String.format("Boot-time mount failed (%d)", rc));
San Mehatfafb0412010-02-18 19:40:04 -0800406 }
San Mehat6a254402010-03-22 10:21:00 -0700407 } else if (state.equals(Environment.MEDIA_SHARED)) {
408 /*
409 * Bootstrap UMS enabled state since vold indicates
410 * the volume is shared (runtime restart while ums enabled)
411 */
412 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Shared);
San Mehatfafb0412010-02-18 19:40:04 -0800413 }
San Mehat6a254402010-03-22 10:21:00 -0700414
San Mehat6a965af22010-02-24 17:47:30 -0800415 /*
San Mehat6a254402010-03-22 10:21:00 -0700416 * If UMS was connected on boot, send the connected event
San Mehat6a965af22010-02-24 17:47:30 -0800417 * now that we're up.
418 */
419 if (mSendUmsConnectedOnBoot) {
420 sendUmsIntent(true);
421 mSendUmsConnectedOnBoot = false;
422 }
San Mehatfafb0412010-02-18 19:40:04 -0800423 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700424 Slog.e(TAG, "Boot-time mount exception", ex);
San Mehatfafb0412010-02-18 19:40:04 -0800425 }
San Mehat207e5382010-02-04 20:46:54 -0800426 }
San Mehatfafb0412010-02-18 19:40:04 -0800427 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429 }
430 };
431
San Mehat4270e1e2010-01-29 05:32:19 -0800432 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
433 final IMountServiceListener mListener;
434
435 MountServiceBinderListener(IMountServiceListener listener) {
436 mListener = listener;
Kenny Root02c87302010-07-01 08:10:18 -0700437
San Mehat91c77612010-01-07 10:39:41 -0800438 }
439
San Mehat4270e1e2010-01-29 05:32:19 -0800440 public void binderDied() {
San Mehata5078592010-03-25 09:36:54 -0700441 if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!");
Kenny Roota02b8b02010-08-05 16:14:17 -0700442 synchronized (mListeners) {
San Mehat4270e1e2010-01-29 05:32:19 -0800443 mListeners.remove(this);
444 mListener.asBinder().unlinkToDeath(this, 0);
445 }
446 }
447 }
448
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800449 private void doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800450 // TODO: Add support for multiple share methods
451 if (!method.equals("ums")) {
452 throw new IllegalArgumentException(String.format("Method %s not supported", method));
453 }
454
San Mehat4270e1e2010-01-29 05:32:19 -0800455 try {
456 mConnector.doCommand(String.format(
457 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
458 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -0700459 Slog.e(TAG, "Failed to share/unshare", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800460 }
San Mehat4270e1e2010-01-29 05:32:19 -0800461 }
462
San Mehat207e5382010-02-04 20:46:54 -0800463 private void updatePublicVolumeState(String path, String state) {
San Mehat4270e1e2010-01-29 05:32:19 -0800464 if (!path.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -0700465 Slog.w(TAG, "Multiple volumes not currently supported");
San Mehat4270e1e2010-01-29 05:32:19 -0800466 return;
467 }
San Mehatb1043402010-02-05 08:26:50 -0800468
469 if (mLegacyState.equals(state)) {
San Mehata5078592010-03-25 09:36:54 -0700470 Slog.w(TAG, String.format("Duplicate state transition (%s -> %s)", mLegacyState, state));
San Mehatb1043402010-02-05 08:26:50 -0800471 return;
472 }
Mike Lockwood03559752010-07-19 18:25:03 -0400473 // Update state on PackageManager, but only of real events
474 if (!mEmulateExternalStorage) {
475 if (Environment.MEDIA_UNMOUNTED.equals(state)) {
476 mPms.updateExternalMediaStatus(false, false);
477 } else if (Environment.MEDIA_MOUNTED.equals(state)) {
478 mPms.updateExternalMediaStatus(true, false);
479 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800480 }
San Mehat4270e1e2010-01-29 05:32:19 -0800481 String oldState = mLegacyState;
482 mLegacyState = state;
483
484 synchronized (mListeners) {
485 for (int i = mListeners.size() -1; i >= 0; i--) {
486 MountServiceBinderListener bl = mListeners.get(i);
487 try {
San Mehatb1043402010-02-05 08:26:50 -0800488 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800489 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700490 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800491 mListeners.remove(i);
492 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700493 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800494 }
495 }
496 }
497 }
498
499 /**
500 *
501 * Callback from NativeDaemonConnector
502 */
503 public void onDaemonConnected() {
504 /*
505 * Since we'll be calling back into the NativeDaemonConnector,
506 * we need to do our work in a new thread.
507 */
508 new Thread() {
509 public void run() {
510 /**
511 * Determine media state and UMS detection status
512 */
513 String path = Environment.getExternalStorageDirectory().getPath();
514 String state = Environment.MEDIA_REMOVED;
515
516 try {
517 String[] vols = mConnector.doListCommand(
518 "volume list", VoldResponseCode.VolumeListResult);
519 for (String volstr : vols) {
520 String[] tok = volstr.split(" ");
521 // FMT: <label> <mountpoint> <state>
522 if (!tok[1].equals(path)) {
San Mehata5078592010-03-25 09:36:54 -0700523 Slog.w(TAG, String.format(
San Mehat4270e1e2010-01-29 05:32:19 -0800524 "Skipping unknown volume '%s'",tok[1]));
525 continue;
526 }
527 int st = Integer.parseInt(tok[2]);
528 if (st == VolumeState.NoMedia) {
529 state = Environment.MEDIA_REMOVED;
530 } else if (st == VolumeState.Idle) {
San Mehat207e5382010-02-04 20:46:54 -0800531 state = Environment.MEDIA_UNMOUNTED;
San Mehat4270e1e2010-01-29 05:32:19 -0800532 } else if (st == VolumeState.Mounted) {
533 state = Environment.MEDIA_MOUNTED;
San Mehata5078592010-03-25 09:36:54 -0700534 Slog.i(TAG, "Media already mounted on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800535 } else if (st == VolumeState.Shared) {
536 state = Environment.MEDIA_SHARED;
San Mehata5078592010-03-25 09:36:54 -0700537 Slog.i(TAG, "Media shared on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800538 } else {
539 throw new Exception(String.format("Unexpected state %d", st));
540 }
541 }
542 if (state != null) {
San Mehata5078592010-03-25 09:36:54 -0700543 if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
San Mehat4270e1e2010-01-29 05:32:19 -0800544 updatePublicVolumeState(path, state);
545 }
546 } catch (Exception e) {
San Mehata5078592010-03-25 09:36:54 -0700547 Slog.e(TAG, "Error processing initial volume state", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800548 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
549 }
550
551 try {
San Mehat207e5382010-02-04 20:46:54 -0800552 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800553 notifyShareAvailabilityChange("ums", avail);
554 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700555 Slog.w(TAG, "Failed to get share availability");
San Mehat4270e1e2010-01-29 05:32:19 -0800556 }
San Mehat207e5382010-02-04 20:46:54 -0800557 /*
558 * Now that we've done our initialization, release
559 * the hounds!
560 */
561 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800562 }
563 }.start();
564 }
565
566 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800567 * Callback from NativeDaemonConnector
568 */
569 public boolean onEvent(int code, String raw, String[] cooked) {
570 Intent in = null;
571
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800572 if (DEBUG_EVENTS) {
573 StringBuilder builder = new StringBuilder();
574 builder.append("onEvent::");
575 builder.append(" raw= " + raw);
576 if (cooked != null) {
577 builder.append(" cooked = " );
578 for (String str : cooked) {
579 builder.append(" " + str);
580 }
581 }
San Mehata5078592010-03-25 09:36:54 -0700582 Slog.i(TAG, builder.toString());
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800583 }
San Mehat4270e1e2010-01-29 05:32:19 -0800584 if (code == VoldResponseCode.VolumeStateChange) {
585 /*
586 * One of the volumes we're managing has changed state.
587 * Format: "NNN Volume <label> <path> state changed
588 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
589 */
590 notifyVolumeStateChange(
591 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
592 Integer.parseInt(cooked[10]));
593 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
594 // FMT: NNN Share method <method> now <available|unavailable>
595 boolean avail = false;
596 if (cooked[5].equals("available")) {
597 avail = true;
598 }
599 notifyShareAvailabilityChange(cooked[3], avail);
600 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
601 (code == VoldResponseCode.VolumeDiskRemoved) ||
602 (code == VoldResponseCode.VolumeBadRemoval)) {
603 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
604 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
605 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
606 final String label = cooked[2];
607 final String path = cooked[3];
608 int major = -1;
609 int minor = -1;
610
611 try {
612 String devComp = cooked[6].substring(1, cooked[6].length() -1);
613 String[] devTok = devComp.split(":");
614 major = Integer.parseInt(devTok[0]);
615 minor = Integer.parseInt(devTok[1]);
616 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700617 Slog.e(TAG, "Failed to parse major/minor", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800618 }
619
San Mehat4270e1e2010-01-29 05:32:19 -0800620 if (code == VoldResponseCode.VolumeDiskInserted) {
621 new Thread() {
622 public void run() {
623 try {
624 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800625 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700626 Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
San Mehat4270e1e2010-01-29 05:32:19 -0800627 }
628 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700629 Slog.w(TAG, "Failed to mount media on insertion", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800630 }
631 }
632 }.start();
633 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
634 /*
635 * This event gets trumped if we're already in BAD_REMOVAL state
636 */
637 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
638 return true;
639 }
640 /* Send the media unmounted event first */
San Mehata5078592010-03-25 09:36:54 -0700641 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800642 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
643 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
644 mContext.sendBroadcast(in);
645
San Mehata5078592010-03-25 09:36:54 -0700646 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
San Mehat4270e1e2010-01-29 05:32:19 -0800647 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
648 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
649 } else if (code == VoldResponseCode.VolumeBadRemoval) {
San Mehata5078592010-03-25 09:36:54 -0700650 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800651 /* Send the media unmounted event first */
652 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
653 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
654 mContext.sendBroadcast(in);
655
San Mehata5078592010-03-25 09:36:54 -0700656 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
San Mehat4270e1e2010-01-29 05:32:19 -0800657 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
658 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
659 } else {
San Mehata5078592010-03-25 09:36:54 -0700660 Slog.e(TAG, String.format("Unknown code {%d}", code));
San Mehat4270e1e2010-01-29 05:32:19 -0800661 }
662 } else {
663 return false;
664 }
665
666 if (in != null) {
667 mContext.sendBroadcast(in);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400668 }
669 return true;
San Mehat4270e1e2010-01-29 05:32:19 -0800670 }
671
San Mehat207e5382010-02-04 20:46:54 -0800672 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800673 String vs = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -0700674 if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
San Mehat4270e1e2010-01-29 05:32:19 -0800675
676 Intent in = null;
677
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500678 if (oldState == VolumeState.Shared && newState != oldState) {
San Mehata5078592010-03-25 09:36:54 -0700679 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500680 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
681 Uri.parse("file://" + path)));
682 }
683
San Mehat4270e1e2010-01-29 05:32:19 -0800684 if (newState == VolumeState.Init) {
685 } else if (newState == VolumeState.NoMedia) {
686 // NoMedia is handled via Disk Remove events
687 } else if (newState == VolumeState.Idle) {
688 /*
689 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
690 * if we're in the process of enabling UMS
691 */
692 if (!vs.equals(
693 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
694 Environment.MEDIA_NOFS) && !vs.equals(
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800695 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
San Mehata5078592010-03-25 09:36:54 -0700696 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
San Mehat4270e1e2010-01-29 05:32:19 -0800697 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
698 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
699 }
700 } else if (newState == VolumeState.Pending) {
701 } else if (newState == VolumeState.Checking) {
San Mehata5078592010-03-25 09:36:54 -0700702 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
San Mehat4270e1e2010-01-29 05:32:19 -0800703 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
704 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
705 } else if (newState == VolumeState.Mounted) {
San Mehata5078592010-03-25 09:36:54 -0700706 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800707 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
San Mehat4270e1e2010-01-29 05:32:19 -0800708 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
709 in.putExtra("read-only", false);
710 } else if (newState == VolumeState.Unmounting) {
San Mehat4270e1e2010-01-29 05:32:19 -0800711 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
712 } else if (newState == VolumeState.Formatting) {
713 } else if (newState == VolumeState.Shared) {
San Mehata5078592010-03-25 09:36:54 -0700714 if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800715 /* Send the media unmounted event first */
716 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
717 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
718 mContext.sendBroadcast(in);
719
San Mehata5078592010-03-25 09:36:54 -0700720 if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
San Mehat4270e1e2010-01-29 05:32:19 -0800721 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
722 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
San Mehata5078592010-03-25 09:36:54 -0700723 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
San Mehat4270e1e2010-01-29 05:32:19 -0800724 } else if (newState == VolumeState.SharedMnt) {
San Mehata5078592010-03-25 09:36:54 -0700725 Slog.e(TAG, "Live shared mounts not supported yet!");
San Mehat4270e1e2010-01-29 05:32:19 -0800726 return;
727 } else {
San Mehata5078592010-03-25 09:36:54 -0700728 Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800729 }
730
731 if (in != null) {
732 mContext.sendBroadcast(in);
733 }
734 }
735
San Mehat207e5382010-02-04 20:46:54 -0800736 private boolean doGetShareMethodAvailable(String method) {
Kenny Root85fb2062010-06-01 20:50:21 -0700737 ArrayList<String> rsp;
Kenny Roota80ce062010-06-01 13:23:53 -0700738 try {
Kenny Root85fb2062010-06-01 20:50:21 -0700739 rsp = mConnector.doCommand("share status " + method);
Kenny Roota80ce062010-06-01 13:23:53 -0700740 } catch (NativeDaemonConnectorException ex) {
741 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
742 return false;
743 }
San Mehat207e5382010-02-04 20:46:54 -0800744
745 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700746 String[] tok = line.split(" ");
747 if (tok.length < 3) {
748 Slog.e(TAG, "Malformed response to share status " + method);
749 return false;
750 }
751
San Mehat207e5382010-02-04 20:46:54 -0800752 int code;
753 try {
754 code = Integer.parseInt(tok[0]);
755 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700756 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehat207e5382010-02-04 20:46:54 -0800757 return false;
758 }
759 if (code == VoldResponseCode.ShareStatusResult) {
760 if (tok[2].equals("available"))
761 return true;
762 return false;
763 } else {
San Mehata5078592010-03-25 09:36:54 -0700764 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehat207e5382010-02-04 20:46:54 -0800765 return false;
766 }
767 }
San Mehata5078592010-03-25 09:36:54 -0700768 Slog.e(TAG, "Got an empty response");
San Mehat207e5382010-02-04 20:46:54 -0800769 return false;
770 }
771
772 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800773 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800774
San Mehata5078592010-03-25 09:36:54 -0700775 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
San Mehat207e5382010-02-04 20:46:54 -0800776 try {
777 mConnector.doCommand(String.format("volume mount %s", path));
778 } catch (NativeDaemonConnectorException e) {
779 /*
780 * Mount failed for some reason
781 */
782 Intent in = null;
783 int code = e.getCode();
784 if (code == VoldResponseCode.OpFailedNoMedia) {
785 /*
786 * Attempt to mount but no media inserted
787 */
San Mehatb1043402010-02-05 08:26:50 -0800788 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800789 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
San Mehata5078592010-03-25 09:36:54 -0700790 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
San Mehat207e5382010-02-04 20:46:54 -0800791 /*
792 * Media is blank or does not contain a supported filesystem
793 */
794 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
795 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800796 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800797 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehata5078592010-03-25 09:36:54 -0700798 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
San Mehat207e5382010-02-04 20:46:54 -0800799 /*
800 * Volume consistency check failed
801 */
802 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
803 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800804 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800805 } else {
San Mehatb1043402010-02-05 08:26:50 -0800806 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800807 }
808
809 /*
810 * Send broadcast intent (if required for the failure)
811 */
812 if (in != null) {
813 mContext.sendBroadcast(in);
814 }
815 }
816
817 return rc;
818 }
819
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800820 /*
821 * If force is not set, we do not unmount if there are
822 * processes holding references to the volume about to be unmounted.
823 * If force is set, all the processes holding references need to be
824 * killed via the ActivityManager before actually unmounting the volume.
825 * This might even take a while and might be retried after timed delays
826 * to make sure we dont end up in an instable state and kill some core
827 * processes.
828 */
San Mehatd9709982010-02-18 11:43:03 -0800829 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800830 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800831 return VoldResponseCode.OpFailedVolNotMounted;
832 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800833 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700834 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800835 try {
San Mehatd9709982010-02-18 11:43:03 -0800836 mConnector.doCommand(String.format(
837 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700838 // We unmounted the volume. None of the asec containers are available now.
839 synchronized (mAsecMountSet) {
840 mAsecMountSet.clear();
841 }
San Mehatb1043402010-02-05 08:26:50 -0800842 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800843 } catch (NativeDaemonConnectorException e) {
844 // Don't worry about mismatch in PackageManager since the
845 // call back will handle the status changes any way.
846 int code = e.getCode();
847 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800848 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800849 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
850 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800851 } else {
San Mehatb1043402010-02-05 08:26:50 -0800852 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800853 }
854 }
855 }
856
857 private int doFormatVolume(String path) {
858 try {
859 String cmd = String.format("volume format %s", path);
860 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800861 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800862 } catch (NativeDaemonConnectorException e) {
863 int code = e.getCode();
864 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800865 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800866 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800867 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800868 } else {
San Mehatb1043402010-02-05 08:26:50 -0800869 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800870 }
871 }
872 }
873
San Mehatb1043402010-02-05 08:26:50 -0800874 private boolean doGetVolumeShared(String path, String method) {
875 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700876 ArrayList<String> rsp;
877
878 try {
879 rsp = mConnector.doCommand(cmd);
880 } catch (NativeDaemonConnectorException ex) {
881 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
882 return false;
883 }
San Mehatb1043402010-02-05 08:26:50 -0800884
885 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700886 String[] tok = line.split(" ");
887 if (tok.length < 3) {
888 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
889 return false;
890 }
891
San Mehatb1043402010-02-05 08:26:50 -0800892 int code;
893 try {
894 code = Integer.parseInt(tok[0]);
895 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700896 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800897 return false;
898 }
899 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700900 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800901 } else {
San Mehata5078592010-03-25 09:36:54 -0700902 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800903 return false;
904 }
905 }
San Mehata5078592010-03-25 09:36:54 -0700906 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800907 return false;
908 }
909
San Mehat207e5382010-02-04 20:46:54 -0800910 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800911 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -0700912 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800913 return;
914 }
915
916 synchronized (mListeners) {
917 for (int i = mListeners.size() -1; i >= 0; i--) {
918 MountServiceBinderListener bl = mListeners.get(i);
919 try {
San Mehatb1043402010-02-05 08:26:50 -0800920 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -0800921 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700922 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800923 mListeners.remove(i);
924 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700925 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800926 }
927 }
928 }
929
San Mehat207e5382010-02-04 20:46:54 -0800930 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -0800931 sendUmsIntent(avail);
932 } else {
933 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -0800934 }
San Mehat2fe718a2010-03-11 12:01:49 -0800935
936 final String path = Environment.getExternalStorageDirectory().getPath();
937 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
938 /*
939 * USB mass storage disconnected while enabled
940 */
941 new Thread() {
942 public void run() {
943 try {
944 int rc;
San Mehata5078592010-03-25 09:36:54 -0700945 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -0800946 doShareUnshareVolume(path, "ums", false);
947 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700948 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -0800949 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
950 path, rc));
951 }
952 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700953 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -0800954 }
955 }
956 }.start();
957 }
San Mehat4270e1e2010-01-29 05:32:19 -0800958 }
959
San Mehat6a965af22010-02-24 17:47:30 -0800960 private void sendUmsIntent(boolean c) {
961 mContext.sendBroadcast(
962 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
963 }
964
San Mehat207e5382010-02-04 20:46:54 -0800965 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -0800966 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
967 throw new SecurityException(String.format("Requires %s permission", perm));
968 }
969 }
970
971 /**
San Mehat207e5382010-02-04 20:46:54 -0800972 * Constructs a new MountService instance
973 *
974 * @param context Binder context for this service
975 */
976 public MountService(Context context) {
977 mContext = context;
978
Mike Lockwood03559752010-07-19 18:25:03 -0400979 mEmulateExternalStorage = context.getResources().getBoolean(
980 com.android.internal.R.bool.config_emulateExternalStorage);
981 if (mEmulateExternalStorage) {
982 Slog.d(TAG, "using emulated external storage");
983 mLegacyState = Environment.MEDIA_MOUNTED;
984 }
985
San Mehat207e5382010-02-04 20:46:54 -0800986 // XXX: This will go away soon in favor of IMountServiceObserver
987 mPms = (PackageManagerService) ServiceManager.getService("package");
988
989 mContext.registerReceiver(mBroadcastReceiver,
990 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
991
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400992 mHandlerThread = new HandlerThread("MountService");
993 mHandlerThread.start();
994 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
995
Kenny Roota02b8b02010-08-05 16:14:17 -0700996 // Add OBB Action Handler to MountService thread.
997 mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper());
998
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800999 /*
1000 * Vold does not run in the simulator, so pretend the connector thread
1001 * ran and did its thing.
1002 */
1003 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
1004 mReady = true;
1005 mUmsEnabling = true;
1006 return;
1007 }
1008
San Mehat207e5382010-02-04 20:46:54 -08001009 mConnector = new NativeDaemonConnector(this, "vold", 10, "VoldConnector");
1010 mReady = false;
1011 Thread thread = new Thread(mConnector, NativeDaemonConnector.class.getName());
1012 thread.start();
1013 }
1014
1015 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001016 * Exposed API calls below here
1017 */
1018
1019 public void registerListener(IMountServiceListener listener) {
1020 synchronized (mListeners) {
1021 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
1022 try {
1023 listener.asBinder().linkToDeath(bl, 0);
1024 mListeners.add(bl);
1025 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001026 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -08001027 }
1028 }
1029 }
1030
1031 public void unregisterListener(IMountServiceListener listener) {
1032 synchronized (mListeners) {
1033 for(MountServiceBinderListener bl : mListeners) {
1034 if (bl.mListener == listener) {
1035 mListeners.remove(mListeners.indexOf(bl));
1036 return;
1037 }
1038 }
1039 }
1040 }
1041
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001042 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -08001043 validatePermission(android.Manifest.permission.SHUTDOWN);
1044
San Mehata5078592010-03-25 09:36:54 -07001045 Slog.i(TAG, "Shutting down");
San Mehat4270e1e2010-01-29 05:32:19 -08001046
1047 String path = Environment.getExternalStorageDirectory().getPath();
1048 String state = getVolumeState(path);
San Mehat91c77612010-01-07 10:39:41 -08001049
1050 if (state.equals(Environment.MEDIA_SHARED)) {
1051 /*
1052 * If the media is currently shared, unshare it.
1053 * XXX: This is still dangerous!. We should not
1054 * be rebooting at *all* if UMS is enabled, since
1055 * the UMS host could have dirty FAT cache entries
1056 * yet to flush.
1057 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001058 setUsbMassStorageEnabled(false);
San Mehat91c77612010-01-07 10:39:41 -08001059 } else if (state.equals(Environment.MEDIA_CHECKING)) {
1060 /*
1061 * If the media is being checked, then we need to wait for
1062 * it to complete before being able to proceed.
1063 */
1064 // XXX: @hackbod - Should we disable the ANR timer here?
1065 int retries = 30;
1066 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
1067 try {
1068 Thread.sleep(1000);
1069 } catch (InterruptedException iex) {
San Mehata5078592010-03-25 09:36:54 -07001070 Slog.e(TAG, "Interrupted while waiting for media", iex);
San Mehat91c77612010-01-07 10:39:41 -08001071 break;
1072 }
1073 state = Environment.getExternalStorageState();
1074 }
1075 if (retries == 0) {
San Mehata5078592010-03-25 09:36:54 -07001076 Slog.e(TAG, "Timed out waiting for media to check");
San Mehat91c77612010-01-07 10:39:41 -08001077 }
1078 }
1079
1080 if (state.equals(Environment.MEDIA_MOUNTED)) {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001081 // Post a unmount message.
1082 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
1083 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
San Mehat4270e1e2010-01-29 05:32:19 -08001084 }
1085 }
1086
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001087 private boolean getUmsEnabling() {
1088 synchronized (mListeners) {
1089 return mUmsEnabling;
1090 }
1091 }
1092
1093 private void setUmsEnabling(boolean enable) {
1094 synchronized (mListeners) {
Tony Wufc711252010-08-09 16:49:19 +08001095 mUmsEnabling = enable;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001096 }
1097 }
1098
San Mehatb1043402010-02-05 08:26:50 -08001099 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -08001100 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -08001101
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001102 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001103 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001104 }
San Mehatb1043402010-02-05 08:26:50 -08001105 return doGetShareMethodAvailable("ums");
1106 }
1107
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001108 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001109 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001110 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001111
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001112 // TODO: Add support for multiple share methods
1113
1114 /*
1115 * If the volume is mounted and we're enabling then unmount it
1116 */
1117 String path = Environment.getExternalStorageDirectory().getPath();
1118 String vs = getVolumeState(path);
1119 String method = "ums";
1120 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1121 // Override for isUsbMassStorageEnabled()
1122 setUmsEnabling(enable);
1123 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1124 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1125 // Clear override
1126 setUmsEnabling(false);
1127 }
1128 /*
1129 * If we disabled UMS then mount the volume
1130 */
1131 if (!enable) {
1132 doShareUnshareVolume(path, method, enable);
1133 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001134 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001135 " after disabling share method " + method);
1136 /*
1137 * Even though the mount failed, the unshare didn't so don't indicate an error.
1138 * The mountVolume() call will have set the storage state and sent the necessary
1139 * broadcasts.
1140 */
1141 }
1142 }
San Mehatb1043402010-02-05 08:26:50 -08001143 }
1144
1145 public boolean isUsbMassStorageEnabled() {
1146 waitForReady();
1147 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 }
San Mehat4270e1e2010-01-29 05:32:19 -08001149
San Mehat7fd0fee2009-12-17 07:12:23 -08001150 /**
1151 * @return state of the volume at the specified mount point
1152 */
San Mehat4270e1e2010-01-29 05:32:19 -08001153 public String getVolumeState(String mountPoint) {
San Mehat7fd0fee2009-12-17 07:12:23 -08001154 /*
1155 * XXX: Until we have multiple volume discovery, just hardwire
1156 * this to /sdcard
1157 */
1158 if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -07001159 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
San Mehat7fd0fee2009-12-17 07:12:23 -08001160 throw new IllegalArgumentException();
1161 }
1162
1163 return mLegacyState;
1164 }
1165
San Mehat4270e1e2010-01-29 05:32:19 -08001166 public int mountVolume(String path) {
1167 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001168
San Mehat207e5382010-02-04 20:46:54 -08001169 waitForReady();
1170 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
1172
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001173 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001174 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001175 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001177 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001178 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001179 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1180 Environment.MEDIA_REMOVED.equals(volState) ||
1181 Environment.MEDIA_SHARED.equals(volState) ||
1182 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1183 // Media already unmounted or cannot be unmounted.
1184 // TODO return valid return code when adding observer call back.
1185 return;
1186 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001187 UnmountCallBack ucb = new UnmountCallBack(path, force);
1188 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190
San Mehat4270e1e2010-01-29 05:32:19 -08001191 public int formatVolume(String path) {
1192 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001193 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001194
San Mehat207e5382010-02-04 20:46:54 -08001195 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 }
1197
San Mehatc1b4ce92010-02-16 17:13:03 -08001198 public int []getStorageUsers(String path) {
1199 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1200 waitForReady();
1201 try {
1202 String[] r = mConnector.doListCommand(
1203 String.format("storage users %s", path),
1204 VoldResponseCode.StorageUsersListResult);
1205 // FMT: <pid> <process name>
1206 int[] data = new int[r.length];
1207 for (int i = 0; i < r.length; i++) {
1208 String []tok = r[i].split(" ");
1209 try {
1210 data[i] = Integer.parseInt(tok[0]);
1211 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001212 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001213 return new int[0];
1214 }
1215 }
1216 return data;
1217 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001218 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001219 return new int[0];
1220 }
1221 }
1222
San Mehatb1043402010-02-05 08:26:50 -08001223 private void warnOnNotMounted() {
1224 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001225 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001226 }
1227 }
1228
San Mehat4270e1e2010-01-29 05:32:19 -08001229 public String[] getSecureContainerList() {
1230 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001231 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001232 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001233
San Mehat4270e1e2010-01-29 05:32:19 -08001234 try {
1235 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1236 } catch (NativeDaemonConnectorException e) {
1237 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 }
1239 }
San Mehat36972292010-01-06 11:06:32 -08001240
San Mehat4270e1e2010-01-29 05:32:19 -08001241 public int createSecureContainer(String id, int sizeMb, String fstype,
1242 String key, int ownerUid) {
1243 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001244 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001245 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001246
San Mehatb1043402010-02-05 08:26:50 -08001247 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001248 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1249 try {
1250 mConnector.doCommand(cmd);
1251 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001252 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001253 }
San Mehata181b212010-02-11 06:50:20 -08001254
1255 if (rc == StorageResultCode.OperationSucceeded) {
1256 synchronized (mAsecMountSet) {
1257 mAsecMountSet.add(id);
1258 }
1259 }
San Mehat4270e1e2010-01-29 05:32:19 -08001260 return rc;
San Mehat36972292010-01-06 11:06:32 -08001261 }
1262
San Mehat4270e1e2010-01-29 05:32:19 -08001263 public int finalizeSecureContainer(String id) {
1264 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001265 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001266
San Mehatb1043402010-02-05 08:26:50 -08001267 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001268 try {
1269 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001270 /*
1271 * Finalization does a remount, so no need
1272 * to update mAsecMountSet
1273 */
San Mehat4270e1e2010-01-29 05:32:19 -08001274 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001275 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001276 }
San Mehat4270e1e2010-01-29 05:32:19 -08001277 return rc;
San Mehat36972292010-01-06 11:06:32 -08001278 }
1279
San Mehatd9709982010-02-18 11:43:03 -08001280 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001281 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001282 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001283 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001284
San Mehatb1043402010-02-05 08:26:50 -08001285 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001286 try {
San Mehatd9709982010-02-18 11:43:03 -08001287 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001288 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001289 int code = e.getCode();
1290 if (code == VoldResponseCode.OpFailedStorageBusy) {
1291 rc = StorageResultCode.OperationFailedStorageBusy;
1292 } else {
1293 rc = StorageResultCode.OperationFailedInternalError;
1294 }
San Mehat02735bc2010-01-26 15:18:08 -08001295 }
San Mehata181b212010-02-11 06:50:20 -08001296
1297 if (rc == StorageResultCode.OperationSucceeded) {
1298 synchronized (mAsecMountSet) {
1299 if (mAsecMountSet.contains(id)) {
1300 mAsecMountSet.remove(id);
1301 }
1302 }
1303 }
1304
San Mehat4270e1e2010-01-29 05:32:19 -08001305 return rc;
San Mehat36972292010-01-06 11:06:32 -08001306 }
1307
San Mehat4270e1e2010-01-29 05:32:19 -08001308 public int mountSecureContainer(String id, String key, int ownerUid) {
1309 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001310 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001311 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001312
San Mehata181b212010-02-11 06:50:20 -08001313 synchronized (mAsecMountSet) {
1314 if (mAsecMountSet.contains(id)) {
1315 return StorageResultCode.OperationFailedStorageMounted;
1316 }
1317 }
1318
San Mehatb1043402010-02-05 08:26:50 -08001319 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001320 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1321 try {
1322 mConnector.doCommand(cmd);
1323 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001324 int code = e.getCode();
1325 if (code != VoldResponseCode.OpFailedStorageBusy) {
1326 rc = StorageResultCode.OperationFailedInternalError;
1327 }
San Mehat02735bc2010-01-26 15:18:08 -08001328 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001329
1330 if (rc == StorageResultCode.OperationSucceeded) {
1331 synchronized (mAsecMountSet) {
1332 mAsecMountSet.add(id);
1333 }
1334 }
San Mehat4270e1e2010-01-29 05:32:19 -08001335 return rc;
San Mehat36972292010-01-06 11:06:32 -08001336 }
1337
San Mehatd9709982010-02-18 11:43:03 -08001338 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001339 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001340 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001341 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001342
San Mehat6cdd9c02010-02-09 14:45:20 -08001343 synchronized (mAsecMountSet) {
1344 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001345 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001346 }
1347 }
1348
San Mehatb1043402010-02-05 08:26:50 -08001349 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001350 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001351 try {
1352 mConnector.doCommand(cmd);
1353 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001354 int code = e.getCode();
1355 if (code == VoldResponseCode.OpFailedStorageBusy) {
1356 rc = StorageResultCode.OperationFailedStorageBusy;
1357 } else {
1358 rc = StorageResultCode.OperationFailedInternalError;
1359 }
San Mehat02735bc2010-01-26 15:18:08 -08001360 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001361
1362 if (rc == StorageResultCode.OperationSucceeded) {
1363 synchronized (mAsecMountSet) {
1364 mAsecMountSet.remove(id);
1365 }
1366 }
San Mehat4270e1e2010-01-29 05:32:19 -08001367 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001368 }
1369
San Mehat6cdd9c02010-02-09 14:45:20 -08001370 public boolean isSecureContainerMounted(String id) {
1371 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1372 waitForReady();
1373 warnOnNotMounted();
1374
1375 synchronized (mAsecMountSet) {
1376 return mAsecMountSet.contains(id);
1377 }
1378 }
1379
San Mehat4270e1e2010-01-29 05:32:19 -08001380 public int renameSecureContainer(String oldId, String newId) {
1381 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001382 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001383 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001384
San Mehata181b212010-02-11 06:50:20 -08001385 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001386 /*
1387 * Because a mounted container has active internal state which cannot be
1388 * changed while active, we must ensure both ids are not currently mounted.
1389 */
1390 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001391 return StorageResultCode.OperationFailedStorageMounted;
1392 }
1393 }
1394
San Mehatb1043402010-02-05 08:26:50 -08001395 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001396 String cmd = String.format("asec rename %s %s", oldId, newId);
1397 try {
1398 mConnector.doCommand(cmd);
1399 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001400 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001401 }
San Mehata181b212010-02-11 06:50:20 -08001402
San Mehat4270e1e2010-01-29 05:32:19 -08001403 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001404 }
1405
San Mehat4270e1e2010-01-29 05:32:19 -08001406 public String getSecureContainerPath(String id) {
1407 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001408 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001409 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001410
San Mehat2d66cef2010-03-23 11:12:52 -07001411 try {
1412 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1413 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001414 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001415 if (code != VoldResponseCode.AsecPathResult) {
1416 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1417 }
1418 return tok[1];
1419 } catch (NativeDaemonConnectorException e) {
1420 int code = e.getCode();
1421 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1422 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001423 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001424 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001425 }
1426 }
San Mehat22dd86e2010-01-12 12:21:18 -08001427 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001428
1429 public void finishMediaUpdate() {
1430 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1431 }
Kenny Root02c87302010-07-01 08:10:18 -07001432
Kenny Roota02b8b02010-08-05 16:14:17 -07001433 private boolean isCallerOwnerOfPackageOrSystem(String packageName) {
Kenny Root02c87302010-07-01 08:10:18 -07001434 final int callerUid = Binder.getCallingUid();
Kenny Roota02b8b02010-08-05 16:14:17 -07001435 return isUidOwnerOfPackageOrSystem(packageName, callerUid);
Kenny Root02c87302010-07-01 08:10:18 -07001436 }
1437
Kenny Roota02b8b02010-08-05 16:14:17 -07001438 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
1439 if (callerUid == android.os.Process.SYSTEM_UID) {
1440 return true;
1441 }
1442
Kenny Root02c87302010-07-01 08:10:18 -07001443 if (packageName == null) {
1444 return false;
1445 }
1446
1447 final int packageUid = mPms.getPackageUid(packageName);
1448
1449 if (DEBUG_OBB) {
1450 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
1451 packageUid + ", callerUid = " + callerUid);
1452 }
1453
1454 return callerUid == packageUid;
1455 }
1456
1457 public String getMountedObbPath(String filename) {
1458 waitForReady();
1459 warnOnNotMounted();
1460
Kenny Root02c87302010-07-01 08:10:18 -07001461 try {
1462 ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename));
1463 String []tok = rsp.get(0).split(" ");
1464 int code = Integer.parseInt(tok[0]);
1465 if (code != VoldResponseCode.AsecPathResult) {
1466 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1467 }
1468 return tok[1];
1469 } catch (NativeDaemonConnectorException e) {
1470 int code = e.getCode();
1471 if (code == VoldResponseCode.OpFailedStorageNotFound) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001472 return null;
Kenny Root02c87302010-07-01 08:10:18 -07001473 } else {
1474 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1475 }
1476 }
1477 }
1478
1479 public boolean isObbMounted(String filename) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001480 synchronized (mObbMounts) {
1481 return mObbPathToStateMap.containsKey(filename);
Kenny Root02c87302010-07-01 08:10:18 -07001482 }
1483 }
1484
Kenny Roota02b8b02010-08-05 16:14:17 -07001485 public void mountObb(String filename, String key, IObbActionListener token) {
Kenny Root02c87302010-07-01 08:10:18 -07001486 waitForReady();
1487 warnOnNotMounted();
1488
Kenny Roota02b8b02010-08-05 16:14:17 -07001489 final ObbState obbState;
1490
1491 synchronized (mObbMounts) {
1492 if (isObbMounted(filename)) {
1493 throw new IllegalArgumentException("OBB file is already mounted");
Kenny Root02c87302010-07-01 08:10:18 -07001494 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001495
1496 if (mObbMounts.containsKey(token)) {
1497 throw new IllegalArgumentException("You may only have one OBB mounted at a time");
1498 }
1499
1500 final int callerUid = Binder.getCallingUid();
1501 obbState = new ObbState(filename, token, callerUid);
1502 addObbState(obbState);
Kenny Root02c87302010-07-01 08:10:18 -07001503 }
1504
Kenny Root02c87302010-07-01 08:10:18 -07001505 try {
Kenny Roota02b8b02010-08-05 16:14:17 -07001506 token.asBinder().linkToDeath(obbState, 0);
1507 } catch (RemoteException rex) {
1508 Slog.e(TAG, "Failed to link to listener death");
Kenny Root02c87302010-07-01 08:10:18 -07001509 }
1510
Kenny Roota02b8b02010-08-05 16:14:17 -07001511 MountObbAction action = new MountObbAction(obbState, key);
1512 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
1513
1514 if (DEBUG_OBB)
1515 Slog.i(TAG, "Send to OBB handler: " + action.toString());
Kenny Root02c87302010-07-01 08:10:18 -07001516 }
1517
Kenny Roota02b8b02010-08-05 16:14:17 -07001518 public void unmountObb(String filename, boolean force, IObbActionListener token) {
1519 final ObbState obbState;
Kenny Root02c87302010-07-01 08:10:18 -07001520
Kenny Roota02b8b02010-08-05 16:14:17 -07001521 synchronized (mObbMounts) {
1522 if (!isObbMounted(filename)) {
1523 throw new IllegalArgumentException("OBB is not mounted");
1524 }
1525 obbState = mObbPathToStateMap.get(filename);
Kenny Root02c87302010-07-01 08:10:18 -07001526 }
1527
Kenny Roota02b8b02010-08-05 16:14:17 -07001528 UnmountObbAction action = new UnmountObbAction(obbState, force);
1529 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root02c87302010-07-01 08:10:18 -07001530
Kenny Roota02b8b02010-08-05 16:14:17 -07001531 if (DEBUG_OBB)
1532 Slog.i(TAG, "Send to OBB handler: " + action.toString());
1533 }
1534
1535 private void addObbState(ObbState obbState) {
1536 synchronized (mObbMounts) {
1537 mObbMounts.put(obbState.token, obbState);
1538 mObbPathToStateMap.put(obbState.filename, obbState);
1539 }
1540 }
1541
1542 private void removeObbState(ObbState obbState) {
1543 synchronized (mObbMounts) {
1544 mObbMounts.remove(obbState.token);
1545 mObbPathToStateMap.remove(obbState.filename);
1546 }
1547 }
1548
1549 private class ObbActionHandler extends Handler {
1550 private boolean mBound = false;
1551 private List<ObbAction> mActions = new LinkedList<ObbAction>();
1552
1553 ObbActionHandler(Looper l) {
1554 super(l);
1555 }
1556
1557 @Override
1558 public void handleMessage(Message msg) {
1559 switch (msg.what) {
1560 case OBB_RUN_ACTION: {
1561 ObbAction action = (ObbAction) msg.obj;
1562
1563 if (DEBUG_OBB)
1564 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
1565
1566 // If a bind was already initiated we don't really
1567 // need to do anything. The pending install
1568 // will be processed later on.
1569 if (!mBound) {
1570 // If this is the only one pending we might
1571 // have to bind to the service again.
1572 if (!connectToService()) {
1573 Slog.e(TAG, "Failed to bind to media container service");
1574 action.handleError();
1575 return;
1576 } else {
1577 // Once we bind to the service, the first
1578 // pending request will be processed.
1579 mActions.add(action);
1580 }
1581 } else {
1582 // Already bound to the service. Just make
1583 // sure we trigger off processing the first request.
1584 if (mActions.size() == 0) {
1585 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1586 }
1587
1588 mActions.add(action);
1589 }
1590 break;
1591 }
1592 case OBB_MCS_BOUND: {
1593 if (DEBUG_OBB)
1594 Slog.i(TAG, "OBB_MCS_BOUND");
1595 if (msg.obj != null) {
1596 mContainerService = (IMediaContainerService) msg.obj;
1597 }
1598 if (mContainerService == null) {
1599 // Something seriously wrong. Bail out
1600 Slog.e(TAG, "Cannot bind to media container service");
1601 for (ObbAction action : mActions) {
1602 // Indicate service bind error
1603 action.handleError();
1604 }
1605 mActions.clear();
1606 } else if (mActions.size() > 0) {
1607 ObbAction action = mActions.get(0);
1608 if (action != null) {
1609 action.execute(this);
1610 }
1611 } else {
1612 // Should never happen ideally.
1613 Slog.w(TAG, "Empty queue");
1614 }
1615 break;
1616 }
1617 case OBB_MCS_RECONNECT: {
1618 if (DEBUG_OBB)
1619 Slog.i(TAG, "OBB_MCS_RECONNECT");
1620 if (mActions.size() > 0) {
1621 if (mBound) {
1622 disconnectService();
1623 }
1624 if (!connectToService()) {
1625 Slog.e(TAG, "Failed to bind to media container service");
1626 for (ObbAction action : mActions) {
1627 // Indicate service bind error
1628 action.handleError();
1629 }
1630 mActions.clear();
1631 }
1632 }
1633 break;
1634 }
1635 case OBB_MCS_UNBIND: {
1636 if (DEBUG_OBB)
1637 Slog.i(TAG, "OBB_MCS_UNBIND");
1638
1639 // Delete pending install
1640 if (mActions.size() > 0) {
1641 mActions.remove(0);
1642 }
1643 if (mActions.size() == 0) {
1644 if (mBound) {
1645 disconnectService();
1646 }
1647 } else {
1648 // There are more pending requests in queue.
1649 // Just post MCS_BOUND message to trigger processing
1650 // of next pending install.
1651 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1652 }
1653 break;
1654 }
1655 case OBB_MCS_GIVE_UP: {
1656 if (DEBUG_OBB)
1657 Slog.i(TAG, "OBB_MCS_GIVE_UP");
1658 mActions.remove(0);
1659 break;
1660 }
1661 }
1662 }
1663
1664 private boolean connectToService() {
1665 if (DEBUG_OBB)
1666 Slog.i(TAG, "Trying to bind to DefaultContainerService");
1667
1668 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
1669 if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) {
1670 mBound = true;
1671 return true;
1672 }
1673 return false;
1674 }
1675
1676 private void disconnectService() {
1677 mContainerService = null;
1678 mBound = false;
1679 mContext.unbindService(mDefContainerConn);
1680 }
1681 }
1682
1683 abstract class ObbAction {
1684 private static final int MAX_RETRIES = 3;
1685 private int mRetries;
1686
1687 ObbState mObbState;
1688
1689 ObbAction(ObbState obbState) {
1690 mObbState = obbState;
1691 }
1692
1693 public void execute(ObbActionHandler handler) {
1694 try {
1695 if (DEBUG_OBB)
1696 Slog.i(TAG, "Starting to execute action: " + this.toString());
1697 mRetries++;
1698 if (mRetries > MAX_RETRIES) {
1699 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
1700 mObbActionHandler.sendEmptyMessage(OBB_MCS_GIVE_UP);
1701 handleError();
1702 return;
1703 } else {
1704 handleExecute();
1705 if (DEBUG_OBB)
1706 Slog.i(TAG, "Posting install MCS_UNBIND");
1707 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1708 }
1709 } catch (RemoteException e) {
1710 if (DEBUG_OBB)
1711 Slog.i(TAG, "Posting install MCS_RECONNECT");
1712 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
1713 } catch (Exception e) {
1714 if (DEBUG_OBB)
1715 Slog.d(TAG, "Error handling OBB action", e);
1716 handleError();
1717 }
1718 }
1719
1720 abstract void handleExecute() throws RemoteException;
1721 abstract void handleError();
1722 }
1723
1724 class MountObbAction extends ObbAction {
1725 private String mKey;
1726
1727 MountObbAction(ObbState obbState, String key) {
1728 super(obbState);
1729 mKey = key;
1730 }
1731
1732 public void handleExecute() throws RemoteException {
1733 ObbInfo obbInfo = mContainerService.getObbInfo(mObbState.filename);
1734 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) {
1735 throw new IllegalArgumentException("Caller package does not match OBB file");
1736 }
1737
1738 if (mKey == null) {
1739 mKey = "none";
1740 }
1741
1742 int rc = StorageResultCode.OperationSucceeded;
1743 String cmd = String.format("obb mount %s %s %d", mObbState.filename, mKey,
1744 mObbState.callerUid);
1745 try {
1746 mConnector.doCommand(cmd);
1747 } catch (NativeDaemonConnectorException e) {
1748 int code = e.getCode();
1749 if (code != VoldResponseCode.OpFailedStorageBusy) {
1750 rc = StorageResultCode.OperationFailedInternalError;
1751 }
1752 }
1753
1754 if (rc == StorageResultCode.OperationSucceeded) {
1755 try {
1756 mObbState.token.onObbResult(mObbState.filename, "mounted");
1757 } catch (RemoteException e) {
1758 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1759 }
Kenny Root02c87302010-07-01 08:10:18 -07001760 } else {
Kenny Roota02b8b02010-08-05 16:14:17 -07001761 Slog.e(TAG, "Couldn't mount OBB file");
1762
1763 // We didn't succeed, so remove this from the mount-set.
1764 removeObbState(mObbState);
Kenny Root02c87302010-07-01 08:10:18 -07001765 }
1766 }
1767
Kenny Roota02b8b02010-08-05 16:14:17 -07001768 public void handleError() {
1769 removeObbState(mObbState);
1770
1771 try {
1772 mObbState.token.onObbResult(mObbState.filename, "error");
1773 } catch (RemoteException e) {
1774 Slog.e(TAG, "Couldn't send back OBB mount error for " + mObbState.filename);
Kenny Root02c87302010-07-01 08:10:18 -07001775 }
1776 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001777
1778 @Override
1779 public String toString() {
1780 StringBuilder sb = new StringBuilder();
1781 sb.append("MountObbAction{");
1782 sb.append("filename=");
1783 sb.append(mObbState.filename);
1784 sb.append(",callerUid=");
1785 sb.append(mObbState.callerUid);
1786 sb.append(",token=");
1787 sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL");
1788 sb.append('}');
1789 return sb.toString();
1790 }
1791 }
1792
1793 class UnmountObbAction extends ObbAction {
1794 private boolean mForceUnmount;
1795
1796 UnmountObbAction(ObbState obbState, boolean force) {
1797 super(obbState);
1798 mForceUnmount = force;
1799 }
1800
1801 public void handleExecute() throws RemoteException {
1802 ObbInfo obbInfo = mContainerService.getObbInfo(mObbState.filename);
1803
1804 if (!isCallerOwnerOfPackageOrSystem(obbInfo.packageName)) {
1805 throw new IllegalArgumentException("Caller package does not match OBB file");
1806 }
1807
1808 int rc = StorageResultCode.OperationSucceeded;
1809 String cmd = String.format("obb unmount %s%s", mObbState.filename,
1810 (mForceUnmount ? " force" : ""));
1811 try {
1812 mConnector.doCommand(cmd);
1813 } catch (NativeDaemonConnectorException e) {
1814 int code = e.getCode();
1815 if (code == VoldResponseCode.OpFailedStorageBusy) {
1816 rc = StorageResultCode.OperationFailedStorageBusy;
1817 } else {
1818 rc = StorageResultCode.OperationFailedInternalError;
1819 }
1820 }
1821
1822 if (rc == StorageResultCode.OperationSucceeded) {
1823 removeObbState(mObbState);
1824
1825 try {
1826 mObbState.token.onObbResult(mObbState.filename, "unmounted");
1827 } catch (RemoteException e) {
1828 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1829 }
1830 } else {
1831 try {
1832 mObbState.token.onObbResult(mObbState.filename, "error");
1833 } catch (RemoteException e) {
1834 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1835 }
1836 }
1837 }
1838
1839 public void handleError() {
1840 removeObbState(mObbState);
1841
1842 try {
1843 mObbState.token.onObbResult(mObbState.filename, "error");
1844 } catch (RemoteException e) {
1845 Slog.e(TAG, "Couldn't send back OBB unmount error for " + mObbState.filename);
1846 }
1847 }
1848
1849 @Override
1850 public String toString() {
1851 StringBuilder sb = new StringBuilder();
1852 sb.append("UnmountObbAction{");
1853 sb.append("filename=");
1854 sb.append(mObbState.filename != null ? mObbState.filename : "null");
1855 sb.append(",force=");
1856 sb.append(mForceUnmount);
1857 sb.append(",callerUid=");
1858 sb.append(mObbState.callerUid);
1859 sb.append(",token=");
1860 sb.append(mObbState.token != null ? mObbState.token.toString() : "null");
1861 sb.append('}');
1862 return sb.toString();
1863 }
Kenny Root02c87302010-07-01 08:10:18 -07001864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865}
1866