blob: 265d61388f0ffe966c3b0eb081a7fce5d2036b7f [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
Kenny Root05105f72010-09-22 17:29:43 -070049import java.io.IOException;
San Mehat22dd86e2010-01-12 12:21:18 -080050import java.util.ArrayList;
Kenny Roota02b8b02010-08-05 16:14:17 -070051import java.util.HashMap;
San Mehat6cdd9c02010-02-09 14:45:20 -080052import java.util.HashSet;
Kenny Roota02b8b02010-08-05 16:14:17 -070053import java.util.LinkedList;
54import java.util.List;
55import java.util.Map;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057/**
San Mehatb1043402010-02-05 08:26:50 -080058 * MountService implements back-end services for platform storage
59 * management.
60 * @hide - Applications should use android.os.storage.StorageManager
61 * to access the MountService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 */
San Mehat22dd86e2010-01-12 12:21:18 -080063class MountService extends IMountService.Stub
64 implements INativeDaemonConnectorCallbacks {
San Mehatb1043402010-02-05 08:26:50 -080065 private static final boolean LOCAL_LOGD = false;
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -080066 private static final boolean DEBUG_UNMOUNT = false;
67 private static final boolean DEBUG_EVENTS = false;
Kenny Root02c87302010-07-01 08:10:18 -070068 private static final boolean DEBUG_OBB = true;
69
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 private static final String TAG = "MountService";
71
Kenny Root305bcbf2010-09-03 07:56:38 -070072 private static final String VOLD_TAG = "VoldConnector";
73
San Mehat4270e1e2010-01-29 05:32:19 -080074 /*
75 * Internal vold volume state constants
76 */
San Mehat7fd0fee2009-12-17 07:12:23 -080077 class VolumeState {
78 public static final int Init = -1;
79 public static final int NoMedia = 0;
80 public static final int Idle = 1;
81 public static final int Pending = 2;
82 public static final int Checking = 3;
83 public static final int Mounted = 4;
84 public static final int Unmounting = 5;
85 public static final int Formatting = 6;
86 public static final int Shared = 7;
87 public static final int SharedMnt = 8;
88 }
89
San Mehat4270e1e2010-01-29 05:32:19 -080090 /*
91 * Internal vold response code constants
92 */
San Mehat22dd86e2010-01-12 12:21:18 -080093 class VoldResponseCode {
San Mehat4270e1e2010-01-29 05:32:19 -080094 /*
95 * 100 series - Requestion action was initiated; expect another reply
96 * before proceeding with a new command.
97 */
San Mehat22dd86e2010-01-12 12:21:18 -080098 public static final int VolumeListResult = 110;
99 public static final int AsecListResult = 111;
San Mehatc1b4ce92010-02-16 17:13:03 -0800100 public static final int StorageUsersListResult = 112;
San Mehat22dd86e2010-01-12 12:21:18 -0800101
San Mehat4270e1e2010-01-29 05:32:19 -0800102 /*
103 * 200 series - Requestion action has been successfully completed.
104 */
105 public static final int ShareStatusResult = 210;
San Mehat22dd86e2010-01-12 12:21:18 -0800106 public static final int AsecPathResult = 211;
San Mehat4270e1e2010-01-29 05:32:19 -0800107 public static final int ShareEnabledResult = 212;
San Mehat22dd86e2010-01-12 12:21:18 -0800108
San Mehat4270e1e2010-01-29 05:32:19 -0800109 /*
110 * 400 series - Command was accepted, but the requested action
111 * did not take place.
112 */
113 public static final int OpFailedNoMedia = 401;
114 public static final int OpFailedMediaBlank = 402;
115 public static final int OpFailedMediaCorrupt = 403;
116 public static final int OpFailedVolNotMounted = 404;
San Mehatd9709982010-02-18 11:43:03 -0800117 public static final int OpFailedStorageBusy = 405;
San Mehat2d66cef2010-03-23 11:12:52 -0700118 public static final int OpFailedStorageNotFound = 406;
San Mehat4270e1e2010-01-29 05:32:19 -0800119
120 /*
121 * 600 series - Unsolicited broadcasts.
122 */
San Mehat22dd86e2010-01-12 12:21:18 -0800123 public static final int VolumeStateChange = 605;
San Mehat22dd86e2010-01-12 12:21:18 -0800124 public static final int ShareAvailabilityChange = 620;
125 public static final int VolumeDiskInserted = 630;
126 public static final int VolumeDiskRemoved = 631;
127 public static final int VolumeBadRemoval = 632;
128 }
129
San Mehat4270e1e2010-01-29 05:32:19 -0800130 private Context mContext;
131 private NativeDaemonConnector mConnector;
132 private String mLegacyState = Environment.MEDIA_REMOVED;
133 private PackageManagerService mPms;
134 private boolean mUmsEnabling;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800135 // Used as a lock for methods that register/unregister listeners.
136 final private ArrayList<MountServiceBinderListener> mListeners =
137 new ArrayList<MountServiceBinderListener>();
San Mehat6a965af22010-02-24 17:47:30 -0800138 private boolean mBooted = false;
139 private boolean mReady = false;
140 private boolean mSendUmsConnectedOnBoot = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800141
San Mehat6cdd9c02010-02-09 14:45:20 -0800142 /**
143 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800144 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800145 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800146 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800147
Kenny Root02c87302010-07-01 08:10:18 -0700148 /**
Kenny Roota02b8b02010-08-05 16:14:17 -0700149 * Mounted OBB tracking information. Used to track the current state of all
150 * OBBs.
Kenny Root02c87302010-07-01 08:10:18 -0700151 */
Kenny Root05105f72010-09-22 17:29:43 -0700152 final private Map<IObbActionListener, List<ObbState>> mObbMounts = new HashMap<IObbActionListener, List<ObbState>>();
Kenny Roota02b8b02010-08-05 16:14:17 -0700153 final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>();
154
155 class ObbState implements IBinder.DeathRecipient {
156 public ObbState(String filename, IObbActionListener token, int callerUid) {
157 this.filename = filename;
158 this.token = token;
159 this.callerUid = callerUid;
160 mounted = false;
161 }
162
163 // OBB source filename
Kenny Root05105f72010-09-22 17:29:43 -0700164 final String filename;
Kenny Roota02b8b02010-08-05 16:14:17 -0700165
166 // Token of remote Binder caller
Kenny Root05105f72010-09-22 17:29:43 -0700167 final IObbActionListener token;
Kenny Roota02b8b02010-08-05 16:14:17 -0700168
169 // Binder.callingUid()
Kenny Root05105f72010-09-22 17:29:43 -0700170 final public int callerUid;
Kenny Roota02b8b02010-08-05 16:14:17 -0700171
172 // Whether this is mounted currently.
173 boolean mounted;
174
175 @Override
176 public void binderDied() {
177 ObbAction action = new UnmountObbAction(this, true);
178 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
179
180 removeObbState(this);
181
182 token.asBinder().unlinkToDeath(this, 0);
183 }
184 }
185
186 // OBB Action Handler
187 final private ObbActionHandler mObbActionHandler;
188
189 // OBB action handler messages
190 private static final int OBB_RUN_ACTION = 1;
191 private static final int OBB_MCS_BOUND = 2;
192 private static final int OBB_MCS_UNBIND = 3;
193 private static final int OBB_MCS_RECONNECT = 4;
194 private static final int OBB_MCS_GIVE_UP = 5;
195
196 /*
197 * Default Container Service information
198 */
199 static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
200 "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService");
201
202 final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection();
203
204 class DefaultContainerConnection implements ServiceConnection {
205 public void onServiceConnected(ComponentName name, IBinder service) {
206 if (DEBUG_OBB)
207 Slog.i(TAG, "onServiceConnected");
208 IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service);
209 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs));
210 }
211
212 public void onServiceDisconnected(ComponentName name) {
213 if (DEBUG_OBB)
214 Slog.i(TAG, "onServiceDisconnected");
215 }
216 };
217
218 // Used in the ObbActionHandler
219 private IMediaContainerService mContainerService = null;
Kenny Root02c87302010-07-01 08:10:18 -0700220
221 // Handler messages
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800222 private static final int H_UNMOUNT_PM_UPDATE = 1;
223 private static final int H_UNMOUNT_PM_DONE = 2;
224 private static final int H_UNMOUNT_MS = 3;
225 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
226 private static final int MAX_UNMOUNT_RETRIES = 4;
227
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800228 class UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700229 final String path;
230 final boolean force;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800231 int retries;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800232
233 UnmountCallBack(String path, boolean force) {
234 retries = 0;
235 this.path = path;
236 this.force = force;
237 }
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800238
239 void handleFinished() {
San Mehata5078592010-03-25 09:36:54 -0700240 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path);
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800241 doUnmountVolume(path, true);
242 }
243 }
244
245 class UmsEnableCallBack extends UnmountCallBack {
Kenny Root05105f72010-09-22 17:29:43 -0700246 final String method;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800247
248 UmsEnableCallBack(String path, String method, boolean force) {
249 super(path, force);
250 this.method = method;
251 }
252
253 @Override
254 void handleFinished() {
255 super.handleFinished();
256 doShareUnshareVolume(path, method, true);
257 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800258 }
259
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800260 class ShutdownCallBack extends UnmountCallBack {
261 IMountShutdownObserver observer;
262 ShutdownCallBack(String path, IMountShutdownObserver observer) {
263 super(path, true);
264 this.observer = observer;
265 }
266
267 @Override
268 void handleFinished() {
269 int ret = doUnmountVolume(path, true);
270 if (observer != null) {
271 try {
272 observer.onShutDownComplete(ret);
273 } catch (RemoteException e) {
San Mehata5078592010-03-25 09:36:54 -0700274 Slog.w(TAG, "RemoteException when shutting down");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800275 }
276 }
277 }
278 }
279
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400280 class MountServiceHandler extends Handler {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800281 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700282 boolean mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800283
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400284 MountServiceHandler(Looper l) {
285 super(l);
286 }
287
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800288 public void handleMessage(Message msg) {
289 switch (msg.what) {
290 case H_UNMOUNT_PM_UPDATE: {
San Mehata5078592010-03-25 09:36:54 -0700291 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800292 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
293 mForceUnmounts.add(ucb);
San Mehata5078592010-03-25 09:36:54 -0700294 if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800295 // Register only if needed.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700296 if (!mUpdatingStatus) {
San Mehata5078592010-03-25 09:36:54 -0700297 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700298 mUpdatingStatus = true;
299 mPms.updateExternalMediaStatus(false, true);
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800300 }
301 break;
302 }
303 case H_UNMOUNT_PM_DONE: {
San Mehata5078592010-03-25 09:36:54 -0700304 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE");
San Mehata5078592010-03-25 09:36:54 -0700305 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700306 mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800307 int size = mForceUnmounts.size();
308 int sizeArr[] = new int[size];
309 int sizeArrN = 0;
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700310 // Kill processes holding references first
311 ActivityManagerService ams = (ActivityManagerService)
312 ServiceManager.getService("activity");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800313 for (int i = 0; i < size; i++) {
314 UnmountCallBack ucb = mForceUnmounts.get(i);
315 String path = ucb.path;
316 boolean done = false;
317 if (!ucb.force) {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800318 done = true;
319 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800320 int pids[] = getStorageUsers(path);
321 if (pids == null || pids.length == 0) {
322 done = true;
323 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800324 // Eliminate system process here?
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700325 ams.killPids(pids, "unmount media");
326 // Confirm if file references have been freed.
327 pids = getStorageUsers(path);
328 if (pids == null || pids.length == 0) {
329 done = true;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800330 }
331 }
332 }
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700333 if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) {
334 // Retry again
335 Slog.i(TAG, "Retrying to kill storage users again");
336 mHandler.sendMessageDelayed(
337 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
338 ucb.retries++),
339 RETRY_UNMOUNT_DELAY);
340 } else {
341 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
342 Slog.i(TAG, "Failed to unmount media inspite of " +
343 MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now");
344 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800345 sizeArr[sizeArrN++] = i;
346 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
347 ucb));
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800348 }
349 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800350 // Remove already processed elements from list.
351 for (int i = (sizeArrN-1); i >= 0; i--) {
352 mForceUnmounts.remove(sizeArr[i]);
353 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800354 break;
355 }
356 case H_UNMOUNT_MS : {
San Mehata5078592010-03-25 09:36:54 -0700357 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800358 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800359 ucb.handleFinished();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800360 break;
361 }
362 }
363 }
364 };
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400365 final private HandlerThread mHandlerThread;
366 final private Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800367
San Mehat207e5382010-02-04 20:46:54 -0800368 private void waitForReady() {
369 while (mReady == false) {
370 for (int retries = 5; retries > 0; retries--) {
371 if (mReady) {
372 return;
373 }
374 SystemClock.sleep(1000);
375 }
San Mehata5078592010-03-25 09:36:54 -0700376 Slog.w(TAG, "Waiting too long for mReady!");
San Mehat207e5382010-02-04 20:46:54 -0800377 }
San Mehat1f6301e2010-01-07 22:40:27 -0800378 }
Kenny Root02c87302010-07-01 08:10:18 -0700379
San Mehat207e5382010-02-04 20:46:54 -0800380 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 public void onReceive(Context context, Intent intent) {
San Mehat91c77612010-01-07 10:39:41 -0800382 String action = intent.getAction();
383
384 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
San Mehat207e5382010-02-04 20:46:54 -0800385 mBooted = true;
San Mehat22dd86e2010-01-12 12:21:18 -0800386
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800387 /*
388 * In the simulator, we need to broadcast a volume mounted event
389 * to make the media scanner run.
390 */
391 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
392 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted);
393 return;
394 }
San Mehatfafb0412010-02-18 19:40:04 -0800395 new Thread() {
396 public void run() {
397 try {
398 String path = Environment.getExternalStorageDirectory().getPath();
San Mehat6a254402010-03-22 10:21:00 -0700399 String state = getVolumeState(path);
400
401 if (state.equals(Environment.MEDIA_UNMOUNTED)) {
San Mehatfafb0412010-02-18 19:40:04 -0800402 int rc = doMountVolume(path);
403 if (rc != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700404 Slog.e(TAG, String.format("Boot-time mount failed (%d)", rc));
San Mehatfafb0412010-02-18 19:40:04 -0800405 }
San Mehat6a254402010-03-22 10:21:00 -0700406 } else if (state.equals(Environment.MEDIA_SHARED)) {
407 /*
408 * Bootstrap UMS enabled state since vold indicates
409 * the volume is shared (runtime restart while ums enabled)
410 */
411 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Shared);
San Mehatfafb0412010-02-18 19:40:04 -0800412 }
San Mehat6a254402010-03-22 10:21:00 -0700413
San Mehat6a965af22010-02-24 17:47:30 -0800414 /*
San Mehat6a254402010-03-22 10:21:00 -0700415 * If UMS was connected on boot, send the connected event
San Mehat6a965af22010-02-24 17:47:30 -0800416 * now that we're up.
417 */
418 if (mSendUmsConnectedOnBoot) {
419 sendUmsIntent(true);
420 mSendUmsConnectedOnBoot = false;
421 }
San Mehatfafb0412010-02-18 19:40:04 -0800422 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700423 Slog.e(TAG, "Boot-time mount exception", ex);
San Mehatfafb0412010-02-18 19:40:04 -0800424 }
San Mehat207e5382010-02-04 20:46:54 -0800425 }
San Mehatfafb0412010-02-18 19:40:04 -0800426 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 }
428 }
429 };
430
San Mehat4270e1e2010-01-29 05:32:19 -0800431 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
432 final IMountServiceListener mListener;
433
434 MountServiceBinderListener(IMountServiceListener listener) {
435 mListener = listener;
Kenny Root02c87302010-07-01 08:10:18 -0700436
San Mehat91c77612010-01-07 10:39:41 -0800437 }
438
San Mehat4270e1e2010-01-29 05:32:19 -0800439 public void binderDied() {
San Mehata5078592010-03-25 09:36:54 -0700440 if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!");
Kenny Roota02b8b02010-08-05 16:14:17 -0700441 synchronized (mListeners) {
San Mehat4270e1e2010-01-29 05:32:19 -0800442 mListeners.remove(this);
443 mListener.asBinder().unlinkToDeath(this, 0);
444 }
445 }
446 }
447
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800448 private void doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800449 // TODO: Add support for multiple share methods
450 if (!method.equals("ums")) {
451 throw new IllegalArgumentException(String.format("Method %s not supported", method));
452 }
453
San Mehat4270e1e2010-01-29 05:32:19 -0800454 try {
455 mConnector.doCommand(String.format(
456 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
457 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -0700458 Slog.e(TAG, "Failed to share/unshare", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800459 }
San Mehat4270e1e2010-01-29 05:32:19 -0800460 }
461
San Mehat207e5382010-02-04 20:46:54 -0800462 private void updatePublicVolumeState(String path, String state) {
San Mehat4270e1e2010-01-29 05:32:19 -0800463 if (!path.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -0700464 Slog.w(TAG, "Multiple volumes not currently supported");
San Mehat4270e1e2010-01-29 05:32:19 -0800465 return;
466 }
San Mehatb1043402010-02-05 08:26:50 -0800467
468 if (mLegacyState.equals(state)) {
San Mehata5078592010-03-25 09:36:54 -0700469 Slog.w(TAG, String.format("Duplicate state transition (%s -> %s)", mLegacyState, state));
San Mehatb1043402010-02-05 08:26:50 -0800470 return;
471 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800472 // Update state on PackageManager
473 if (Environment.MEDIA_UNMOUNTED.equals(state)) {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700474 mPms.updateExternalMediaStatus(false, false);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800475 } else if (Environment.MEDIA_MOUNTED.equals(state)) {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700476 mPms.updateExternalMediaStatus(true, false);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800477 }
San Mehat4270e1e2010-01-29 05:32:19 -0800478 String oldState = mLegacyState;
479 mLegacyState = state;
480
481 synchronized (mListeners) {
482 for (int i = mListeners.size() -1; i >= 0; i--) {
483 MountServiceBinderListener bl = mListeners.get(i);
484 try {
San Mehatb1043402010-02-05 08:26:50 -0800485 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800486 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700487 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800488 mListeners.remove(i);
489 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700490 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800491 }
492 }
493 }
494 }
495
496 /**
497 *
498 * Callback from NativeDaemonConnector
499 */
500 public void onDaemonConnected() {
501 /*
502 * Since we'll be calling back into the NativeDaemonConnector,
503 * we need to do our work in a new thread.
504 */
505 new Thread() {
506 public void run() {
507 /**
508 * Determine media state and UMS detection status
509 */
510 String path = Environment.getExternalStorageDirectory().getPath();
511 String state = Environment.MEDIA_REMOVED;
512
513 try {
514 String[] vols = mConnector.doListCommand(
515 "volume list", VoldResponseCode.VolumeListResult);
516 for (String volstr : vols) {
517 String[] tok = volstr.split(" ");
518 // FMT: <label> <mountpoint> <state>
519 if (!tok[1].equals(path)) {
San Mehata5078592010-03-25 09:36:54 -0700520 Slog.w(TAG, String.format(
San Mehat4270e1e2010-01-29 05:32:19 -0800521 "Skipping unknown volume '%s'",tok[1]));
522 continue;
523 }
524 int st = Integer.parseInt(tok[2]);
525 if (st == VolumeState.NoMedia) {
526 state = Environment.MEDIA_REMOVED;
527 } else if (st == VolumeState.Idle) {
San Mehat207e5382010-02-04 20:46:54 -0800528 state = Environment.MEDIA_UNMOUNTED;
San Mehat4270e1e2010-01-29 05:32:19 -0800529 } else if (st == VolumeState.Mounted) {
530 state = Environment.MEDIA_MOUNTED;
San Mehata5078592010-03-25 09:36:54 -0700531 Slog.i(TAG, "Media already mounted on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800532 } else if (st == VolumeState.Shared) {
533 state = Environment.MEDIA_SHARED;
San Mehata5078592010-03-25 09:36:54 -0700534 Slog.i(TAG, "Media shared on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800535 } else {
536 throw new Exception(String.format("Unexpected state %d", st));
537 }
538 }
539 if (state != null) {
San Mehata5078592010-03-25 09:36:54 -0700540 if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
San Mehat4270e1e2010-01-29 05:32:19 -0800541 updatePublicVolumeState(path, state);
542 }
543 } catch (Exception e) {
San Mehata5078592010-03-25 09:36:54 -0700544 Slog.e(TAG, "Error processing initial volume state", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800545 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
546 }
547
548 try {
San Mehat207e5382010-02-04 20:46:54 -0800549 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800550 notifyShareAvailabilityChange("ums", avail);
551 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700552 Slog.w(TAG, "Failed to get share availability");
San Mehat4270e1e2010-01-29 05:32:19 -0800553 }
San Mehat207e5382010-02-04 20:46:54 -0800554 /*
555 * Now that we've done our initialization, release
556 * the hounds!
557 */
558 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800559 }
560 }.start();
561 }
562
563 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800564 * Callback from NativeDaemonConnector
565 */
566 public boolean onEvent(int code, String raw, String[] cooked) {
567 Intent in = null;
568
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800569 if (DEBUG_EVENTS) {
570 StringBuilder builder = new StringBuilder();
571 builder.append("onEvent::");
572 builder.append(" raw= " + raw);
573 if (cooked != null) {
574 builder.append(" cooked = " );
575 for (String str : cooked) {
576 builder.append(" " + str);
577 }
578 }
San Mehata5078592010-03-25 09:36:54 -0700579 Slog.i(TAG, builder.toString());
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800580 }
San Mehat4270e1e2010-01-29 05:32:19 -0800581 if (code == VoldResponseCode.VolumeStateChange) {
582 /*
583 * One of the volumes we're managing has changed state.
584 * Format: "NNN Volume <label> <path> state changed
585 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
586 */
587 notifyVolumeStateChange(
588 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
589 Integer.parseInt(cooked[10]));
590 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
591 // FMT: NNN Share method <method> now <available|unavailable>
592 boolean avail = false;
593 if (cooked[5].equals("available")) {
594 avail = true;
595 }
596 notifyShareAvailabilityChange(cooked[3], avail);
597 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
598 (code == VoldResponseCode.VolumeDiskRemoved) ||
599 (code == VoldResponseCode.VolumeBadRemoval)) {
600 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
601 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
602 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
603 final String label = cooked[2];
604 final String path = cooked[3];
605 int major = -1;
606 int minor = -1;
607
608 try {
609 String devComp = cooked[6].substring(1, cooked[6].length() -1);
610 String[] devTok = devComp.split(":");
611 major = Integer.parseInt(devTok[0]);
612 minor = Integer.parseInt(devTok[1]);
613 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700614 Slog.e(TAG, "Failed to parse major/minor", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800615 }
616
San Mehat4270e1e2010-01-29 05:32:19 -0800617 if (code == VoldResponseCode.VolumeDiskInserted) {
618 new Thread() {
619 public void run() {
620 try {
621 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800622 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700623 Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
San Mehat4270e1e2010-01-29 05:32:19 -0800624 }
625 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700626 Slog.w(TAG, "Failed to mount media on insertion", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800627 }
628 }
629 }.start();
630 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
631 /*
632 * This event gets trumped if we're already in BAD_REMOVAL state
633 */
634 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
635 return true;
636 }
637 /* Send the media unmounted event first */
San Mehata5078592010-03-25 09:36:54 -0700638 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800639 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
640 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
641 mContext.sendBroadcast(in);
642
San Mehata5078592010-03-25 09:36:54 -0700643 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
San Mehat4270e1e2010-01-29 05:32:19 -0800644 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
645 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
646 } else if (code == VoldResponseCode.VolumeBadRemoval) {
San Mehata5078592010-03-25 09:36:54 -0700647 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800648 /* Send the media unmounted event first */
649 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
650 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
651 mContext.sendBroadcast(in);
652
San Mehata5078592010-03-25 09:36:54 -0700653 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
San Mehat4270e1e2010-01-29 05:32:19 -0800654 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
655 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
656 } else {
San Mehata5078592010-03-25 09:36:54 -0700657 Slog.e(TAG, String.format("Unknown code {%d}", code));
San Mehat4270e1e2010-01-29 05:32:19 -0800658 }
659 } else {
660 return false;
661 }
662
663 if (in != null) {
664 mContext.sendBroadcast(in);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400665 }
666 return true;
San Mehat4270e1e2010-01-29 05:32:19 -0800667 }
668
San Mehat207e5382010-02-04 20:46:54 -0800669 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800670 String vs = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -0700671 if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
San Mehat4270e1e2010-01-29 05:32:19 -0800672
673 Intent in = null;
674
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500675 if (oldState == VolumeState.Shared && newState != oldState) {
San Mehata5078592010-03-25 09:36:54 -0700676 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500677 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
678 Uri.parse("file://" + path)));
679 }
680
San Mehat4270e1e2010-01-29 05:32:19 -0800681 if (newState == VolumeState.Init) {
682 } else if (newState == VolumeState.NoMedia) {
683 // NoMedia is handled via Disk Remove events
684 } else if (newState == VolumeState.Idle) {
685 /*
686 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
687 * if we're in the process of enabling UMS
688 */
689 if (!vs.equals(
690 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
691 Environment.MEDIA_NOFS) && !vs.equals(
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800692 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
San Mehata5078592010-03-25 09:36:54 -0700693 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
San Mehat4270e1e2010-01-29 05:32:19 -0800694 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
695 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
696 }
697 } else if (newState == VolumeState.Pending) {
698 } else if (newState == VolumeState.Checking) {
San Mehata5078592010-03-25 09:36:54 -0700699 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
San Mehat4270e1e2010-01-29 05:32:19 -0800700 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
701 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
702 } else if (newState == VolumeState.Mounted) {
San Mehata5078592010-03-25 09:36:54 -0700703 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800704 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
San Mehat4270e1e2010-01-29 05:32:19 -0800705 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
706 in.putExtra("read-only", false);
707 } else if (newState == VolumeState.Unmounting) {
San Mehat4270e1e2010-01-29 05:32:19 -0800708 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
709 } else if (newState == VolumeState.Formatting) {
710 } else if (newState == VolumeState.Shared) {
San Mehata5078592010-03-25 09:36:54 -0700711 if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800712 /* Send the media unmounted event first */
713 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
714 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
715 mContext.sendBroadcast(in);
716
San Mehata5078592010-03-25 09:36:54 -0700717 if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
San Mehat4270e1e2010-01-29 05:32:19 -0800718 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
719 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
San Mehata5078592010-03-25 09:36:54 -0700720 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
San Mehat4270e1e2010-01-29 05:32:19 -0800721 } else if (newState == VolumeState.SharedMnt) {
San Mehata5078592010-03-25 09:36:54 -0700722 Slog.e(TAG, "Live shared mounts not supported yet!");
San Mehat4270e1e2010-01-29 05:32:19 -0800723 return;
724 } else {
San Mehata5078592010-03-25 09:36:54 -0700725 Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800726 }
727
728 if (in != null) {
729 mContext.sendBroadcast(in);
730 }
731 }
732
San Mehat207e5382010-02-04 20:46:54 -0800733 private boolean doGetShareMethodAvailable(String method) {
Kenny Root85fb2062010-06-01 20:50:21 -0700734 ArrayList<String> rsp;
Kenny Roota80ce062010-06-01 13:23:53 -0700735 try {
Kenny Root85fb2062010-06-01 20:50:21 -0700736 rsp = mConnector.doCommand("share status " + method);
Kenny Roota80ce062010-06-01 13:23:53 -0700737 } catch (NativeDaemonConnectorException ex) {
738 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
739 return false;
740 }
San Mehat207e5382010-02-04 20:46:54 -0800741
742 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700743 String[] tok = line.split(" ");
744 if (tok.length < 3) {
745 Slog.e(TAG, "Malformed response to share status " + method);
746 return false;
747 }
748
San Mehat207e5382010-02-04 20:46:54 -0800749 int code;
750 try {
751 code = Integer.parseInt(tok[0]);
752 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700753 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehat207e5382010-02-04 20:46:54 -0800754 return false;
755 }
756 if (code == VoldResponseCode.ShareStatusResult) {
757 if (tok[2].equals("available"))
758 return true;
759 return false;
760 } else {
San Mehata5078592010-03-25 09:36:54 -0700761 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehat207e5382010-02-04 20:46:54 -0800762 return false;
763 }
764 }
San Mehata5078592010-03-25 09:36:54 -0700765 Slog.e(TAG, "Got an empty response");
San Mehat207e5382010-02-04 20:46:54 -0800766 return false;
767 }
768
769 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800770 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800771
San Mehata5078592010-03-25 09:36:54 -0700772 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
San Mehat207e5382010-02-04 20:46:54 -0800773 try {
774 mConnector.doCommand(String.format("volume mount %s", path));
775 } catch (NativeDaemonConnectorException e) {
776 /*
777 * Mount failed for some reason
778 */
779 Intent in = null;
780 int code = e.getCode();
781 if (code == VoldResponseCode.OpFailedNoMedia) {
782 /*
783 * Attempt to mount but no media inserted
784 */
San Mehatb1043402010-02-05 08:26:50 -0800785 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800786 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
San Mehata5078592010-03-25 09:36:54 -0700787 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
San Mehat207e5382010-02-04 20:46:54 -0800788 /*
789 * Media is blank or does not contain a supported filesystem
790 */
791 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
792 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800793 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800794 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehata5078592010-03-25 09:36:54 -0700795 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
San Mehat207e5382010-02-04 20:46:54 -0800796 /*
797 * Volume consistency check failed
798 */
799 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
800 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800801 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800802 } else {
San Mehatb1043402010-02-05 08:26:50 -0800803 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800804 }
805
806 /*
807 * Send broadcast intent (if required for the failure)
808 */
809 if (in != null) {
810 mContext.sendBroadcast(in);
811 }
812 }
813
814 return rc;
815 }
816
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800817 /*
818 * If force is not set, we do not unmount if there are
819 * processes holding references to the volume about to be unmounted.
820 * If force is set, all the processes holding references need to be
821 * killed via the ActivityManager before actually unmounting the volume.
822 * This might even take a while and might be retried after timed delays
823 * to make sure we dont end up in an instable state and kill some core
824 * processes.
825 */
San Mehatd9709982010-02-18 11:43:03 -0800826 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800827 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800828 return VoldResponseCode.OpFailedVolNotMounted;
829 }
Kenny Rootaa485402010-09-14 14:49:41 -0700830
831 /*
832 * Force a GC to make sure AssetManagers in other threads of the
833 * system_server are cleaned up. We have to do this since AssetManager
834 * instances are kept as a WeakReference and it's possible we have files
835 * open on the external storage.
836 */
837 Runtime.getRuntime().gc();
838
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800839 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700840 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800841 try {
San Mehatd9709982010-02-18 11:43:03 -0800842 mConnector.doCommand(String.format(
843 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700844 // We unmounted the volume. None of the asec containers are available now.
845 synchronized (mAsecMountSet) {
846 mAsecMountSet.clear();
847 }
San Mehatb1043402010-02-05 08:26:50 -0800848 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800849 } catch (NativeDaemonConnectorException e) {
850 // Don't worry about mismatch in PackageManager since the
851 // call back will handle the status changes any way.
852 int code = e.getCode();
853 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800854 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800855 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
856 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800857 } else {
San Mehatb1043402010-02-05 08:26:50 -0800858 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800859 }
860 }
861 }
862
863 private int doFormatVolume(String path) {
864 try {
865 String cmd = String.format("volume format %s", path);
866 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800867 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800868 } catch (NativeDaemonConnectorException e) {
869 int code = e.getCode();
870 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800871 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800872 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800873 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800874 } else {
San Mehatb1043402010-02-05 08:26:50 -0800875 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800876 }
877 }
878 }
879
San Mehatb1043402010-02-05 08:26:50 -0800880 private boolean doGetVolumeShared(String path, String method) {
881 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700882 ArrayList<String> rsp;
883
884 try {
885 rsp = mConnector.doCommand(cmd);
886 } catch (NativeDaemonConnectorException ex) {
887 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
888 return false;
889 }
San Mehatb1043402010-02-05 08:26:50 -0800890
891 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700892 String[] tok = line.split(" ");
893 if (tok.length < 3) {
894 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
895 return false;
896 }
897
San Mehatb1043402010-02-05 08:26:50 -0800898 int code;
899 try {
900 code = Integer.parseInt(tok[0]);
901 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700902 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800903 return false;
904 }
905 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700906 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800907 } else {
San Mehata5078592010-03-25 09:36:54 -0700908 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800909 return false;
910 }
911 }
San Mehata5078592010-03-25 09:36:54 -0700912 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800913 return false;
914 }
915
San Mehat207e5382010-02-04 20:46:54 -0800916 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800917 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -0700918 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800919 return;
920 }
921
922 synchronized (mListeners) {
923 for (int i = mListeners.size() -1; i >= 0; i--) {
924 MountServiceBinderListener bl = mListeners.get(i);
925 try {
San Mehatb1043402010-02-05 08:26:50 -0800926 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -0800927 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700928 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800929 mListeners.remove(i);
930 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700931 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800932 }
933 }
934 }
935
San Mehat207e5382010-02-04 20:46:54 -0800936 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -0800937 sendUmsIntent(avail);
938 } else {
939 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -0800940 }
San Mehat2fe718a2010-03-11 12:01:49 -0800941
942 final String path = Environment.getExternalStorageDirectory().getPath();
943 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
944 /*
945 * USB mass storage disconnected while enabled
946 */
947 new Thread() {
948 public void run() {
949 try {
950 int rc;
San Mehata5078592010-03-25 09:36:54 -0700951 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -0800952 doShareUnshareVolume(path, "ums", false);
953 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700954 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -0800955 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
956 path, rc));
957 }
958 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700959 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -0800960 }
961 }
962 }.start();
963 }
San Mehat4270e1e2010-01-29 05:32:19 -0800964 }
965
San Mehat6a965af22010-02-24 17:47:30 -0800966 private void sendUmsIntent(boolean c) {
967 mContext.sendBroadcast(
968 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
969 }
970
San Mehat207e5382010-02-04 20:46:54 -0800971 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -0800972 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
973 throw new SecurityException(String.format("Requires %s permission", perm));
974 }
975 }
976
977 /**
San Mehat207e5382010-02-04 20:46:54 -0800978 * Constructs a new MountService instance
979 *
980 * @param context Binder context for this service
981 */
982 public MountService(Context context) {
983 mContext = context;
984
San Mehat207e5382010-02-04 20:46:54 -0800985 // XXX: This will go away soon in favor of IMountServiceObserver
986 mPms = (PackageManagerService) ServiceManager.getService("package");
987
988 mContext.registerReceiver(mBroadcastReceiver,
989 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
990
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400991 mHandlerThread = new HandlerThread("MountService");
992 mHandlerThread.start();
993 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
994
Kenny Roota02b8b02010-08-05 16:14:17 -0700995 // Add OBB Action Handler to MountService thread.
996 mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper());
997
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800998 /*
999 * Vold does not run in the simulator, so pretend the connector thread
1000 * ran and did its thing.
1001 */
1002 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
1003 mReady = true;
1004 mUmsEnabling = true;
1005 return;
1006 }
1007
Kenny Root305bcbf2010-09-03 07:56:38 -07001008 /*
1009 * Create the connection to vold with a maximum queue of twice the
1010 * amount of containers we'd ever expect to have. This keeps an
1011 * "asec list" from blocking a thread repeatedly.
1012 */
1013 mConnector = new NativeDaemonConnector(this, "vold",
1014 PackageManagerService.MAX_CONTAINERS * 2, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001015 mReady = false;
Kenny Root305bcbf2010-09-03 07:56:38 -07001016 Thread thread = new Thread(mConnector, VOLD_TAG);
San Mehat207e5382010-02-04 20:46:54 -08001017 thread.start();
1018 }
1019
1020 /**
San Mehat4270e1e2010-01-29 05:32:19 -08001021 * Exposed API calls below here
1022 */
1023
1024 public void registerListener(IMountServiceListener listener) {
1025 synchronized (mListeners) {
1026 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
1027 try {
1028 listener.asBinder().linkToDeath(bl, 0);
1029 mListeners.add(bl);
1030 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -07001031 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -08001032 }
1033 }
1034 }
1035
1036 public void unregisterListener(IMountServiceListener listener) {
1037 synchronized (mListeners) {
1038 for(MountServiceBinderListener bl : mListeners) {
1039 if (bl.mListener == listener) {
1040 mListeners.remove(mListeners.indexOf(bl));
1041 return;
1042 }
1043 }
1044 }
1045 }
1046
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001047 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -08001048 validatePermission(android.Manifest.permission.SHUTDOWN);
1049
San Mehata5078592010-03-25 09:36:54 -07001050 Slog.i(TAG, "Shutting down");
San Mehat4270e1e2010-01-29 05:32:19 -08001051
1052 String path = Environment.getExternalStorageDirectory().getPath();
1053 String state = getVolumeState(path);
San Mehat91c77612010-01-07 10:39:41 -08001054
1055 if (state.equals(Environment.MEDIA_SHARED)) {
1056 /*
1057 * If the media is currently shared, unshare it.
1058 * XXX: This is still dangerous!. We should not
1059 * be rebooting at *all* if UMS is enabled, since
1060 * the UMS host could have dirty FAT cache entries
1061 * yet to flush.
1062 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001063 setUsbMassStorageEnabled(false);
San Mehat91c77612010-01-07 10:39:41 -08001064 } else if (state.equals(Environment.MEDIA_CHECKING)) {
1065 /*
1066 * If the media is being checked, then we need to wait for
1067 * it to complete before being able to proceed.
1068 */
1069 // XXX: @hackbod - Should we disable the ANR timer here?
1070 int retries = 30;
1071 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
1072 try {
1073 Thread.sleep(1000);
1074 } catch (InterruptedException iex) {
San Mehata5078592010-03-25 09:36:54 -07001075 Slog.e(TAG, "Interrupted while waiting for media", iex);
San Mehat91c77612010-01-07 10:39:41 -08001076 break;
1077 }
1078 state = Environment.getExternalStorageState();
1079 }
1080 if (retries == 0) {
San Mehata5078592010-03-25 09:36:54 -07001081 Slog.e(TAG, "Timed out waiting for media to check");
San Mehat91c77612010-01-07 10:39:41 -08001082 }
1083 }
1084
1085 if (state.equals(Environment.MEDIA_MOUNTED)) {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001086 // Post a unmount message.
1087 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
1088 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
San Mehat4270e1e2010-01-29 05:32:19 -08001089 }
1090 }
1091
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001092 private boolean getUmsEnabling() {
1093 synchronized (mListeners) {
1094 return mUmsEnabling;
1095 }
1096 }
1097
1098 private void setUmsEnabling(boolean enable) {
1099 synchronized (mListeners) {
1100 mUmsEnabling = true;
1101 }
1102 }
1103
San Mehatb1043402010-02-05 08:26:50 -08001104 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -08001105 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -08001106
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001107 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001108 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001109 }
San Mehatb1043402010-02-05 08:26:50 -08001110 return doGetShareMethodAvailable("ums");
1111 }
1112
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001113 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001114 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001115 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001116
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001117 // TODO: Add support for multiple share methods
1118
1119 /*
1120 * If the volume is mounted and we're enabling then unmount it
1121 */
1122 String path = Environment.getExternalStorageDirectory().getPath();
1123 String vs = getVolumeState(path);
1124 String method = "ums";
1125 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1126 // Override for isUsbMassStorageEnabled()
1127 setUmsEnabling(enable);
1128 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1129 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1130 // Clear override
1131 setUmsEnabling(false);
1132 }
1133 /*
1134 * If we disabled UMS then mount the volume
1135 */
1136 if (!enable) {
1137 doShareUnshareVolume(path, method, enable);
1138 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001139 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001140 " after disabling share method " + method);
1141 /*
1142 * Even though the mount failed, the unshare didn't so don't indicate an error.
1143 * The mountVolume() call will have set the storage state and sent the necessary
1144 * broadcasts.
1145 */
1146 }
1147 }
San Mehatb1043402010-02-05 08:26:50 -08001148 }
1149
1150 public boolean isUsbMassStorageEnabled() {
1151 waitForReady();
1152 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 }
San Mehat4270e1e2010-01-29 05:32:19 -08001154
San Mehat7fd0fee2009-12-17 07:12:23 -08001155 /**
1156 * @return state of the volume at the specified mount point
1157 */
San Mehat4270e1e2010-01-29 05:32:19 -08001158 public String getVolumeState(String mountPoint) {
San Mehat7fd0fee2009-12-17 07:12:23 -08001159 /*
1160 * XXX: Until we have multiple volume discovery, just hardwire
1161 * this to /sdcard
1162 */
1163 if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -07001164 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
San Mehat7fd0fee2009-12-17 07:12:23 -08001165 throw new IllegalArgumentException();
1166 }
1167
1168 return mLegacyState;
1169 }
1170
San Mehat4270e1e2010-01-29 05:32:19 -08001171 public int mountVolume(String path) {
1172 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001173
San Mehat207e5382010-02-04 20:46:54 -08001174 waitForReady();
1175 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
1177
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001178 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001179 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001180 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001182 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001183 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001184 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1185 Environment.MEDIA_REMOVED.equals(volState) ||
1186 Environment.MEDIA_SHARED.equals(volState) ||
1187 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1188 // Media already unmounted or cannot be unmounted.
1189 // TODO return valid return code when adding observer call back.
1190 return;
1191 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001192 UnmountCallBack ucb = new UnmountCallBack(path, force);
1193 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195
San Mehat4270e1e2010-01-29 05:32:19 -08001196 public int formatVolume(String path) {
1197 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001198 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001199
San Mehat207e5382010-02-04 20:46:54 -08001200 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202
San Mehatc1b4ce92010-02-16 17:13:03 -08001203 public int []getStorageUsers(String path) {
1204 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1205 waitForReady();
1206 try {
1207 String[] r = mConnector.doListCommand(
1208 String.format("storage users %s", path),
1209 VoldResponseCode.StorageUsersListResult);
1210 // FMT: <pid> <process name>
1211 int[] data = new int[r.length];
1212 for (int i = 0; i < r.length; i++) {
1213 String []tok = r[i].split(" ");
1214 try {
1215 data[i] = Integer.parseInt(tok[0]);
1216 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001217 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001218 return new int[0];
1219 }
1220 }
1221 return data;
1222 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001223 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001224 return new int[0];
1225 }
1226 }
1227
San Mehatb1043402010-02-05 08:26:50 -08001228 private void warnOnNotMounted() {
1229 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001230 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001231 }
1232 }
1233
San Mehat4270e1e2010-01-29 05:32:19 -08001234 public String[] getSecureContainerList() {
1235 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001236 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001237 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001238
San Mehat4270e1e2010-01-29 05:32:19 -08001239 try {
1240 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1241 } catch (NativeDaemonConnectorException e) {
1242 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
1244 }
San Mehat36972292010-01-06 11:06:32 -08001245
San Mehat4270e1e2010-01-29 05:32:19 -08001246 public int createSecureContainer(String id, int sizeMb, String fstype,
1247 String key, int ownerUid) {
1248 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001249 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001250 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001251
San Mehatb1043402010-02-05 08:26:50 -08001252 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001253 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1254 try {
1255 mConnector.doCommand(cmd);
1256 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001257 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001258 }
San Mehata181b212010-02-11 06:50:20 -08001259
1260 if (rc == StorageResultCode.OperationSucceeded) {
1261 synchronized (mAsecMountSet) {
1262 mAsecMountSet.add(id);
1263 }
1264 }
San Mehat4270e1e2010-01-29 05:32:19 -08001265 return rc;
San Mehat36972292010-01-06 11:06:32 -08001266 }
1267
San Mehat4270e1e2010-01-29 05:32:19 -08001268 public int finalizeSecureContainer(String id) {
1269 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001270 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001271
San Mehatb1043402010-02-05 08:26:50 -08001272 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001273 try {
1274 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001275 /*
1276 * Finalization does a remount, so no need
1277 * to update mAsecMountSet
1278 */
San Mehat4270e1e2010-01-29 05:32:19 -08001279 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001280 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001281 }
San Mehat4270e1e2010-01-29 05:32:19 -08001282 return rc;
San Mehat36972292010-01-06 11:06:32 -08001283 }
1284
San Mehatd9709982010-02-18 11:43:03 -08001285 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001286 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001287 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001288 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001289
Kenny Rootaa485402010-09-14 14:49:41 -07001290 /*
1291 * Force a GC to make sure AssetManagers in other threads of the
1292 * system_server are cleaned up. We have to do this since AssetManager
1293 * instances are kept as a WeakReference and it's possible we have files
1294 * open on the external storage.
1295 */
1296 Runtime.getRuntime().gc();
1297
San Mehatb1043402010-02-05 08:26:50 -08001298 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001299 try {
San Mehatd9709982010-02-18 11:43:03 -08001300 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001301 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001302 int code = e.getCode();
1303 if (code == VoldResponseCode.OpFailedStorageBusy) {
1304 rc = StorageResultCode.OperationFailedStorageBusy;
1305 } else {
1306 rc = StorageResultCode.OperationFailedInternalError;
1307 }
San Mehat02735bc2010-01-26 15:18:08 -08001308 }
San Mehata181b212010-02-11 06:50:20 -08001309
1310 if (rc == StorageResultCode.OperationSucceeded) {
1311 synchronized (mAsecMountSet) {
1312 if (mAsecMountSet.contains(id)) {
1313 mAsecMountSet.remove(id);
1314 }
1315 }
1316 }
1317
San Mehat4270e1e2010-01-29 05:32:19 -08001318 return rc;
San Mehat36972292010-01-06 11:06:32 -08001319 }
1320
San Mehat4270e1e2010-01-29 05:32:19 -08001321 public int mountSecureContainer(String id, String key, int ownerUid) {
1322 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001323 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001324 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001325
San Mehata181b212010-02-11 06:50:20 -08001326 synchronized (mAsecMountSet) {
1327 if (mAsecMountSet.contains(id)) {
1328 return StorageResultCode.OperationFailedStorageMounted;
1329 }
1330 }
1331
San Mehatb1043402010-02-05 08:26:50 -08001332 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001333 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1334 try {
1335 mConnector.doCommand(cmd);
1336 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001337 int code = e.getCode();
1338 if (code != VoldResponseCode.OpFailedStorageBusy) {
1339 rc = StorageResultCode.OperationFailedInternalError;
1340 }
San Mehat02735bc2010-01-26 15:18:08 -08001341 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001342
1343 if (rc == StorageResultCode.OperationSucceeded) {
1344 synchronized (mAsecMountSet) {
1345 mAsecMountSet.add(id);
1346 }
1347 }
San Mehat4270e1e2010-01-29 05:32:19 -08001348 return rc;
San Mehat36972292010-01-06 11:06:32 -08001349 }
1350
San Mehatd9709982010-02-18 11:43:03 -08001351 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001352 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001353 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001354 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001355
San Mehat6cdd9c02010-02-09 14:45:20 -08001356 synchronized (mAsecMountSet) {
1357 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001358 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001359 }
1360 }
1361
Kenny Rootaa485402010-09-14 14:49:41 -07001362 /*
1363 * Force a GC to make sure AssetManagers in other threads of the
1364 * system_server are cleaned up. We have to do this since AssetManager
1365 * instances are kept as a WeakReference and it's possible we have files
1366 * open on the external storage.
1367 */
1368 Runtime.getRuntime().gc();
1369
San Mehatb1043402010-02-05 08:26:50 -08001370 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001371 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001372 try {
1373 mConnector.doCommand(cmd);
1374 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001375 int code = e.getCode();
1376 if (code == VoldResponseCode.OpFailedStorageBusy) {
1377 rc = StorageResultCode.OperationFailedStorageBusy;
1378 } else {
1379 rc = StorageResultCode.OperationFailedInternalError;
1380 }
San Mehat02735bc2010-01-26 15:18:08 -08001381 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001382
1383 if (rc == StorageResultCode.OperationSucceeded) {
1384 synchronized (mAsecMountSet) {
1385 mAsecMountSet.remove(id);
1386 }
1387 }
San Mehat4270e1e2010-01-29 05:32:19 -08001388 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001389 }
1390
San Mehat6cdd9c02010-02-09 14:45:20 -08001391 public boolean isSecureContainerMounted(String id) {
1392 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1393 waitForReady();
1394 warnOnNotMounted();
1395
1396 synchronized (mAsecMountSet) {
1397 return mAsecMountSet.contains(id);
1398 }
1399 }
1400
San Mehat4270e1e2010-01-29 05:32:19 -08001401 public int renameSecureContainer(String oldId, String newId) {
1402 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001403 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001404 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001405
San Mehata181b212010-02-11 06:50:20 -08001406 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001407 /*
1408 * Because a mounted container has active internal state which cannot be
1409 * changed while active, we must ensure both ids are not currently mounted.
1410 */
1411 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001412 return StorageResultCode.OperationFailedStorageMounted;
1413 }
1414 }
1415
San Mehatb1043402010-02-05 08:26:50 -08001416 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001417 String cmd = String.format("asec rename %s %s", oldId, newId);
1418 try {
1419 mConnector.doCommand(cmd);
1420 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001421 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001422 }
San Mehata181b212010-02-11 06:50:20 -08001423
San Mehat4270e1e2010-01-29 05:32:19 -08001424 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001425 }
1426
San Mehat4270e1e2010-01-29 05:32:19 -08001427 public String getSecureContainerPath(String id) {
1428 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001429 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001430 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001431
San Mehat2d66cef2010-03-23 11:12:52 -07001432 try {
1433 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1434 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001435 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001436 if (code != VoldResponseCode.AsecPathResult) {
1437 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1438 }
1439 return tok[1];
1440 } catch (NativeDaemonConnectorException e) {
1441 int code = e.getCode();
1442 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1443 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001444 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001445 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001446 }
1447 }
San Mehat22dd86e2010-01-12 12:21:18 -08001448 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001449
1450 public void finishMediaUpdate() {
1451 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1452 }
Kenny Root02c87302010-07-01 08:10:18 -07001453
Kenny Roota02b8b02010-08-05 16:14:17 -07001454 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
1455 if (callerUid == android.os.Process.SYSTEM_UID) {
1456 return true;
1457 }
1458
Kenny Root02c87302010-07-01 08:10:18 -07001459 if (packageName == null) {
1460 return false;
1461 }
1462
1463 final int packageUid = mPms.getPackageUid(packageName);
1464
1465 if (DEBUG_OBB) {
1466 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
1467 packageUid + ", callerUid = " + callerUid);
1468 }
1469
1470 return callerUid == packageUid;
1471 }
1472
1473 public String getMountedObbPath(String filename) {
1474 waitForReady();
1475 warnOnNotMounted();
1476
Kenny Root02c87302010-07-01 08:10:18 -07001477 try {
1478 ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename));
1479 String []tok = rsp.get(0).split(" ");
1480 int code = Integer.parseInt(tok[0]);
1481 if (code != VoldResponseCode.AsecPathResult) {
1482 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1483 }
1484 return tok[1];
1485 } catch (NativeDaemonConnectorException e) {
1486 int code = e.getCode();
1487 if (code == VoldResponseCode.OpFailedStorageNotFound) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001488 return null;
Kenny Root02c87302010-07-01 08:10:18 -07001489 } else {
1490 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1491 }
1492 }
1493 }
1494
1495 public boolean isObbMounted(String filename) {
Kenny Roota02b8b02010-08-05 16:14:17 -07001496 synchronized (mObbMounts) {
1497 return mObbPathToStateMap.containsKey(filename);
Kenny Root02c87302010-07-01 08:10:18 -07001498 }
1499 }
1500
Kenny Roota02b8b02010-08-05 16:14:17 -07001501 public void mountObb(String filename, String key, IObbActionListener token) {
Kenny Root02c87302010-07-01 08:10:18 -07001502 waitForReady();
1503 warnOnNotMounted();
1504
Kenny Rootf1121dc2010-09-29 07:30:53 -07001505 if (filename == null) {
1506 throw new IllegalArgumentException("filename cannot be null");
1507 } else if (token == null) {
1508 throw new IllegalArgumentException("token cannot be null");
1509 }
1510
Kenny Roota02b8b02010-08-05 16:14:17 -07001511 final ObbState obbState;
1512
1513 synchronized (mObbMounts) {
1514 if (isObbMounted(filename)) {
1515 throw new IllegalArgumentException("OBB file is already mounted");
Kenny Root02c87302010-07-01 08:10:18 -07001516 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001517
Kenny Roota02b8b02010-08-05 16:14:17 -07001518 final int callerUid = Binder.getCallingUid();
1519 obbState = new ObbState(filename, token, callerUid);
1520 addObbState(obbState);
Kenny Root02c87302010-07-01 08:10:18 -07001521 }
1522
Kenny Root02c87302010-07-01 08:10:18 -07001523 try {
Kenny Roota02b8b02010-08-05 16:14:17 -07001524 token.asBinder().linkToDeath(obbState, 0);
1525 } catch (RemoteException rex) {
1526 Slog.e(TAG, "Failed to link to listener death");
Kenny Root02c87302010-07-01 08:10:18 -07001527 }
1528
Kenny Roota02b8b02010-08-05 16:14:17 -07001529 MountObbAction action = new MountObbAction(obbState, key);
1530 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
1531
1532 if (DEBUG_OBB)
1533 Slog.i(TAG, "Send to OBB handler: " + action.toString());
Kenny Root02c87302010-07-01 08:10:18 -07001534 }
1535
Kenny Roota02b8b02010-08-05 16:14:17 -07001536 public void unmountObb(String filename, boolean force, IObbActionListener token) {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001537 if (filename == null) {
1538 throw new IllegalArgumentException("filename cannot be null");
1539 } else if (token == null) {
1540 throw new IllegalArgumentException("token cannot be null");
1541 }
1542
Kenny Roota02b8b02010-08-05 16:14:17 -07001543 final ObbState obbState;
Kenny Root02c87302010-07-01 08:10:18 -07001544
Kenny Roota02b8b02010-08-05 16:14:17 -07001545 synchronized (mObbMounts) {
1546 if (!isObbMounted(filename)) {
1547 throw new IllegalArgumentException("OBB is not mounted");
1548 }
1549 obbState = mObbPathToStateMap.get(filename);
Kenny Rootf1121dc2010-09-29 07:30:53 -07001550
1551 if (Binder.getCallingUid() != obbState.callerUid) {
1552 throw new SecurityException("caller UID does not match original mount caller UID");
1553 } else if (!token.asBinder().equals(obbState.token.asBinder())) {
1554 throw new SecurityException("caller does not match original mount caller");
1555 }
Kenny Root02c87302010-07-01 08:10:18 -07001556 }
1557
Kenny Roota02b8b02010-08-05 16:14:17 -07001558 UnmountObbAction action = new UnmountObbAction(obbState, force);
1559 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
Kenny Root02c87302010-07-01 08:10:18 -07001560
Kenny Roota02b8b02010-08-05 16:14:17 -07001561 if (DEBUG_OBB)
1562 Slog.i(TAG, "Send to OBB handler: " + action.toString());
1563 }
1564
1565 private void addObbState(ObbState obbState) {
1566 synchronized (mObbMounts) {
Kenny Root05105f72010-09-22 17:29:43 -07001567 List<ObbState> obbStates = mObbMounts.get(obbState.token);
1568 if (obbStates == null) {
1569 obbStates = new ArrayList<ObbState>();
1570 mObbMounts.put(obbState.token, obbStates);
1571 }
1572 obbStates.add(obbState);
Kenny Roota02b8b02010-08-05 16:14:17 -07001573 mObbPathToStateMap.put(obbState.filename, obbState);
1574 }
1575 }
1576
1577 private void removeObbState(ObbState obbState) {
1578 synchronized (mObbMounts) {
Kenny Root05105f72010-09-22 17:29:43 -07001579 final List<ObbState> obbStates = mObbMounts.get(obbState.token);
1580 if (obbStates != null) {
1581 obbStates.remove(obbState);
1582 }
1583 if (obbStates == null || obbStates.isEmpty()) {
1584 mObbMounts.remove(obbState.token);
1585 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001586 mObbPathToStateMap.remove(obbState.filename);
1587 }
1588 }
1589
1590 private class ObbActionHandler extends Handler {
1591 private boolean mBound = false;
1592 private List<ObbAction> mActions = new LinkedList<ObbAction>();
1593
1594 ObbActionHandler(Looper l) {
1595 super(l);
1596 }
1597
1598 @Override
1599 public void handleMessage(Message msg) {
1600 switch (msg.what) {
1601 case OBB_RUN_ACTION: {
1602 ObbAction action = (ObbAction) msg.obj;
1603
1604 if (DEBUG_OBB)
1605 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
1606
1607 // If a bind was already initiated we don't really
1608 // need to do anything. The pending install
1609 // will be processed later on.
1610 if (!mBound) {
1611 // If this is the only one pending we might
1612 // have to bind to the service again.
1613 if (!connectToService()) {
1614 Slog.e(TAG, "Failed to bind to media container service");
1615 action.handleError();
1616 return;
1617 } else {
1618 // Once we bind to the service, the first
1619 // pending request will be processed.
1620 mActions.add(action);
1621 }
1622 } else {
1623 // Already bound to the service. Just make
1624 // sure we trigger off processing the first request.
1625 if (mActions.size() == 0) {
1626 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1627 }
1628
1629 mActions.add(action);
1630 }
1631 break;
1632 }
1633 case OBB_MCS_BOUND: {
1634 if (DEBUG_OBB)
1635 Slog.i(TAG, "OBB_MCS_BOUND");
1636 if (msg.obj != null) {
1637 mContainerService = (IMediaContainerService) msg.obj;
1638 }
1639 if (mContainerService == null) {
1640 // Something seriously wrong. Bail out
1641 Slog.e(TAG, "Cannot bind to media container service");
1642 for (ObbAction action : mActions) {
1643 // Indicate service bind error
1644 action.handleError();
1645 }
1646 mActions.clear();
1647 } else if (mActions.size() > 0) {
1648 ObbAction action = mActions.get(0);
1649 if (action != null) {
1650 action.execute(this);
1651 }
1652 } else {
1653 // Should never happen ideally.
1654 Slog.w(TAG, "Empty queue");
1655 }
1656 break;
1657 }
1658 case OBB_MCS_RECONNECT: {
1659 if (DEBUG_OBB)
1660 Slog.i(TAG, "OBB_MCS_RECONNECT");
1661 if (mActions.size() > 0) {
1662 if (mBound) {
1663 disconnectService();
1664 }
1665 if (!connectToService()) {
1666 Slog.e(TAG, "Failed to bind to media container service");
1667 for (ObbAction action : mActions) {
1668 // Indicate service bind error
1669 action.handleError();
1670 }
1671 mActions.clear();
1672 }
1673 }
1674 break;
1675 }
1676 case OBB_MCS_UNBIND: {
1677 if (DEBUG_OBB)
1678 Slog.i(TAG, "OBB_MCS_UNBIND");
1679
1680 // Delete pending install
1681 if (mActions.size() > 0) {
1682 mActions.remove(0);
1683 }
1684 if (mActions.size() == 0) {
1685 if (mBound) {
1686 disconnectService();
1687 }
1688 } else {
1689 // There are more pending requests in queue.
1690 // Just post MCS_BOUND message to trigger processing
1691 // of next pending install.
1692 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1693 }
1694 break;
1695 }
1696 case OBB_MCS_GIVE_UP: {
1697 if (DEBUG_OBB)
1698 Slog.i(TAG, "OBB_MCS_GIVE_UP");
1699 mActions.remove(0);
1700 break;
1701 }
1702 }
1703 }
1704
1705 private boolean connectToService() {
1706 if (DEBUG_OBB)
1707 Slog.i(TAG, "Trying to bind to DefaultContainerService");
1708
1709 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
1710 if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) {
1711 mBound = true;
1712 return true;
1713 }
1714 return false;
1715 }
1716
1717 private void disconnectService() {
1718 mContainerService = null;
1719 mBound = false;
1720 mContext.unbindService(mDefContainerConn);
1721 }
1722 }
1723
1724 abstract class ObbAction {
1725 private static final int MAX_RETRIES = 3;
1726 private int mRetries;
1727
1728 ObbState mObbState;
1729
1730 ObbAction(ObbState obbState) {
1731 mObbState = obbState;
1732 }
1733
1734 public void execute(ObbActionHandler handler) {
1735 try {
1736 if (DEBUG_OBB)
1737 Slog.i(TAG, "Starting to execute action: " + this.toString());
1738 mRetries++;
1739 if (mRetries > MAX_RETRIES) {
1740 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
1741 mObbActionHandler.sendEmptyMessage(OBB_MCS_GIVE_UP);
1742 handleError();
1743 return;
1744 } else {
1745 handleExecute();
1746 if (DEBUG_OBB)
1747 Slog.i(TAG, "Posting install MCS_UNBIND");
1748 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1749 }
1750 } catch (RemoteException e) {
1751 if (DEBUG_OBB)
1752 Slog.i(TAG, "Posting install MCS_RECONNECT");
1753 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
1754 } catch (Exception e) {
1755 if (DEBUG_OBB)
1756 Slog.d(TAG, "Error handling OBB action", e);
1757 handleError();
1758 }
1759 }
1760
Kenny Root05105f72010-09-22 17:29:43 -07001761 abstract void handleExecute() throws RemoteException, IOException;
Kenny Roota02b8b02010-08-05 16:14:17 -07001762 abstract void handleError();
1763 }
1764
1765 class MountObbAction extends ObbAction {
1766 private String mKey;
1767
1768 MountObbAction(ObbState obbState, String key) {
1769 super(obbState);
1770 mKey = key;
1771 }
1772
Kenny Root05105f72010-09-22 17:29:43 -07001773 public void handleExecute() throws RemoteException, IOException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001774 final ObbInfo obbInfo = mContainerService.getObbInfo(mObbState.filename);
Kenny Root05105f72010-09-22 17:29:43 -07001775 if (obbInfo == null) {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001776 throw new IOException("Couldn't read OBB file: " + mObbState.filename);
Kenny Root05105f72010-09-22 17:29:43 -07001777 }
1778
Kenny Roota02b8b02010-08-05 16:14:17 -07001779 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) {
1780 throw new IllegalArgumentException("Caller package does not match OBB file");
1781 }
1782
1783 if (mKey == null) {
1784 mKey = "none";
1785 }
1786
1787 int rc = StorageResultCode.OperationSucceeded;
1788 String cmd = String.format("obb mount %s %s %d", mObbState.filename, mKey,
1789 mObbState.callerUid);
1790 try {
1791 mConnector.doCommand(cmd);
1792 } catch (NativeDaemonConnectorException e) {
1793 int code = e.getCode();
1794 if (code != VoldResponseCode.OpFailedStorageBusy) {
1795 rc = StorageResultCode.OperationFailedInternalError;
1796 }
1797 }
1798
1799 if (rc == StorageResultCode.OperationSucceeded) {
1800 try {
Kenny Root05105f72010-09-22 17:29:43 -07001801 mObbState.token.onObbResult(mObbState.filename, Environment.MEDIA_MOUNTED);
Kenny Roota02b8b02010-08-05 16:14:17 -07001802 } catch (RemoteException e) {
1803 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1804 }
Kenny Root02c87302010-07-01 08:10:18 -07001805 } else {
Kenny Root05105f72010-09-22 17:29:43 -07001806 Slog.e(TAG, "Couldn't mount OBB file: " + rc);
Kenny Roota02b8b02010-08-05 16:14:17 -07001807
1808 // We didn't succeed, so remove this from the mount-set.
1809 removeObbState(mObbState);
Kenny Root05105f72010-09-22 17:29:43 -07001810
1811 mObbState.token.onObbResult(mObbState.filename, Environment.MEDIA_BAD_REMOVAL);
Kenny Root02c87302010-07-01 08:10:18 -07001812 }
1813 }
1814
Kenny Roota02b8b02010-08-05 16:14:17 -07001815 public void handleError() {
1816 removeObbState(mObbState);
1817
1818 try {
Kenny Root05105f72010-09-22 17:29:43 -07001819 mObbState.token.onObbResult(mObbState.filename, Environment.MEDIA_BAD_REMOVAL);
Kenny Roota02b8b02010-08-05 16:14:17 -07001820 } catch (RemoteException e) {
1821 Slog.e(TAG, "Couldn't send back OBB mount error for " + mObbState.filename);
Kenny Root02c87302010-07-01 08:10:18 -07001822 }
1823 }
Kenny Roota02b8b02010-08-05 16:14:17 -07001824
1825 @Override
1826 public String toString() {
1827 StringBuilder sb = new StringBuilder();
1828 sb.append("MountObbAction{");
1829 sb.append("filename=");
1830 sb.append(mObbState.filename);
1831 sb.append(",callerUid=");
1832 sb.append(mObbState.callerUid);
1833 sb.append(",token=");
1834 sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL");
1835 sb.append('}');
1836 return sb.toString();
1837 }
1838 }
1839
1840 class UnmountObbAction extends ObbAction {
1841 private boolean mForceUnmount;
1842
1843 UnmountObbAction(ObbState obbState, boolean force) {
1844 super(obbState);
1845 mForceUnmount = force;
1846 }
1847
Kenny Root05105f72010-09-22 17:29:43 -07001848 public void handleExecute() throws RemoteException, IOException {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001849 final ObbInfo obbInfo = mContainerService.getObbInfo(mObbState.filename);
Kenny Root05105f72010-09-22 17:29:43 -07001850 if (obbInfo == null) {
Kenny Rootf1121dc2010-09-29 07:30:53 -07001851 throw new IOException("Couldn't read OBB file: " + mObbState.filename);
Kenny Roota02b8b02010-08-05 16:14:17 -07001852 }
1853
1854 int rc = StorageResultCode.OperationSucceeded;
1855 String cmd = String.format("obb unmount %s%s", mObbState.filename,
1856 (mForceUnmount ? " force" : ""));
1857 try {
1858 mConnector.doCommand(cmd);
1859 } catch (NativeDaemonConnectorException e) {
1860 int code = e.getCode();
1861 if (code == VoldResponseCode.OpFailedStorageBusy) {
1862 rc = StorageResultCode.OperationFailedStorageBusy;
1863 } else {
1864 rc = StorageResultCode.OperationFailedInternalError;
1865 }
1866 }
1867
1868 if (rc == StorageResultCode.OperationSucceeded) {
1869 removeObbState(mObbState);
1870
1871 try {
Kenny Root05105f72010-09-22 17:29:43 -07001872 mObbState.token.onObbResult(mObbState.filename, Environment.MEDIA_UNMOUNTED);
Kenny Roota02b8b02010-08-05 16:14:17 -07001873 } catch (RemoteException e) {
1874 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1875 }
1876 } else {
1877 try {
Kenny Root05105f72010-09-22 17:29:43 -07001878 mObbState.token.onObbResult(mObbState.filename, Environment.MEDIA_BAD_REMOVAL);
Kenny Roota02b8b02010-08-05 16:14:17 -07001879 } catch (RemoteException e) {
1880 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1881 }
1882 }
1883 }
1884
1885 public void handleError() {
1886 removeObbState(mObbState);
1887
1888 try {
Kenny Root05105f72010-09-22 17:29:43 -07001889 mObbState.token.onObbResult(mObbState.filename, Environment.MEDIA_BAD_REMOVAL);
Kenny Roota02b8b02010-08-05 16:14:17 -07001890 } catch (RemoteException e) {
1891 Slog.e(TAG, "Couldn't send back OBB unmount error for " + mObbState.filename);
1892 }
1893 }
1894
1895 @Override
1896 public String toString() {
1897 StringBuilder sb = new StringBuilder();
1898 sb.append("UnmountObbAction{");
1899 sb.append("filename=");
1900 sb.append(mObbState.filename != null ? mObbState.filename : "null");
1901 sb.append(",force=");
1902 sb.append(mForceUnmount);
1903 sb.append(",callerUid=");
1904 sb.append(mObbState.callerUid);
1905 sb.append(",token=");
1906 sb.append(mObbState.token != null ? mObbState.token.toString() : "null");
1907 sb.append('}');
1908 return sb.toString();
1909 }
Kenny Root02c87302010-07-01 08:10:18 -07001910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911}
1912