| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 19 | import com.android.internal.app.IMediaContainerService; |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 20 | import com.android.server.am.ActivityManagerService; |
| 21 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 27 | import android.content.ServiceConnection; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 29 | import android.content.res.ObbInfo; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.net.Uri; |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 31 | import android.os.Binder; |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 32 | import android.os.Environment; |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 33 | import android.os.Handler; |
| Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 34 | import android.os.HandlerThread; |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 35 | import android.os.IBinder; |
| Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 36 | import android.os.Looper; |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 37 | import android.os.Message; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 38 | import android.os.RemoteException; |
| Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 39 | import android.os.ServiceManager; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 40 | import android.os.SystemClock; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.os.SystemProperties; |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 42 | import android.os.storage.IMountService; |
| 43 | import android.os.storage.IMountServiceListener; |
| 44 | import android.os.storage.IMountShutdownObserver; |
| 45 | import android.os.storage.IObbActionListener; |
| 46 | import android.os.storage.StorageResultCode; |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 47 | import android.util.Slog; |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 48 | |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 49 | import java.util.ArrayList; |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 50 | import java.util.HashMap; |
| San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 51 | import java.util.HashSet; |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 52 | import java.util.LinkedList; |
| 53 | import java.util.List; |
| 54 | import java.util.Map; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | /** |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 57 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | */ |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 62 | class MountService extends IMountService.Stub |
| 63 | implements INativeDaemonConnectorCallbacks { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 64 | private static final boolean LOCAL_LOGD = false; |
| Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 65 | private static final boolean DEBUG_UNMOUNT = false; |
| 66 | private static final boolean DEBUG_EVENTS = false; |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 67 | private static final boolean DEBUG_OBB = true; |
| 68 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | private static final String TAG = "MountService"; |
| 70 | |
| Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 71 | private static final String VOLD_TAG = "VoldConnector"; |
| 72 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 73 | /* |
| 74 | * Internal vold volume state constants |
| 75 | */ |
| San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 76 | class VolumeState { |
| 77 | public static final int Init = -1; |
| 78 | public static final int NoMedia = 0; |
| 79 | public static final int Idle = 1; |
| 80 | public static final int Pending = 2; |
| 81 | public static final int Checking = 3; |
| 82 | public static final int Mounted = 4; |
| 83 | public static final int Unmounting = 5; |
| 84 | public static final int Formatting = 6; |
| 85 | public static final int Shared = 7; |
| 86 | public static final int SharedMnt = 8; |
| 87 | } |
| 88 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 89 | /* |
| 90 | * Internal vold response code constants |
| 91 | */ |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 92 | class VoldResponseCode { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 93 | /* |
| 94 | * 100 series - Requestion action was initiated; expect another reply |
| 95 | * before proceeding with a new command. |
| 96 | */ |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 97 | public static final int VolumeListResult = 110; |
| 98 | public static final int AsecListResult = 111; |
| San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 99 | public static final int StorageUsersListResult = 112; |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 100 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 101 | /* |
| 102 | * 200 series - Requestion action has been successfully completed. |
| 103 | */ |
| 104 | public static final int ShareStatusResult = 210; |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 105 | public static final int AsecPathResult = 211; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 106 | public static final int ShareEnabledResult = 212; |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 107 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 108 | /* |
| 109 | * 400 series - Command was accepted, but the requested action |
| 110 | * did not take place. |
| 111 | */ |
| 112 | public static final int OpFailedNoMedia = 401; |
| 113 | public static final int OpFailedMediaBlank = 402; |
| 114 | public static final int OpFailedMediaCorrupt = 403; |
| 115 | public static final int OpFailedVolNotMounted = 404; |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 116 | public static final int OpFailedStorageBusy = 405; |
| San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 117 | public static final int OpFailedStorageNotFound = 406; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * 600 series - Unsolicited broadcasts. |
| 121 | */ |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 122 | public static final int VolumeStateChange = 605; |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 123 | public static final int ShareAvailabilityChange = 620; |
| 124 | public static final int VolumeDiskInserted = 630; |
| 125 | public static final int VolumeDiskRemoved = 631; |
| 126 | public static final int VolumeBadRemoval = 632; |
| 127 | } |
| 128 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 129 | private Context mContext; |
| 130 | private NativeDaemonConnector mConnector; |
| 131 | private String mLegacyState = Environment.MEDIA_REMOVED; |
| 132 | private PackageManagerService mPms; |
| 133 | private boolean mUmsEnabling; |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 134 | // Used as a lock for methods that register/unregister listeners. |
| 135 | final private ArrayList<MountServiceBinderListener> mListeners = |
| 136 | new ArrayList<MountServiceBinderListener>(); |
| San Mehat | 6a965af2 | 2010-02-24 17:47:30 -0800 | [diff] [blame] | 137 | private boolean mBooted = false; |
| 138 | private boolean mReady = false; |
| 139 | private boolean mSendUmsConnectedOnBoot = false; |
| Mike Lockwood | 0355975 | 2010-07-19 18:25:03 -0400 | [diff] [blame] | 140 | // true if we should fake MEDIA_MOUNTED state for external storage |
| 141 | private boolean mEmulateExternalStorage = false; |
| Suchi Amalapurapu | fd3530f | 2010-01-18 00:15:59 -0800 | [diff] [blame] | 142 | |
| San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 143 | /** |
| 144 | * Private hash of currently mounted secure containers. |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 145 | * Used as a lock in methods to manipulate secure containers. |
| San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 146 | */ |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 147 | final private HashSet<String> mAsecMountSet = new HashSet<String>(); |
| San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 148 | |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 149 | /** |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 150 | * Mounted OBB tracking information. Used to track the current state of all |
| 151 | * OBBs. |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 152 | */ |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 153 | final private Map<IObbActionListener, ObbState> mObbMounts = new HashMap<IObbActionListener, ObbState>(); |
| 154 | final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>(); |
| 155 | |
| 156 | class ObbState implements IBinder.DeathRecipient { |
| 157 | public ObbState(String filename, IObbActionListener token, int callerUid) { |
| 158 | this.filename = filename; |
| 159 | this.token = token; |
| 160 | this.callerUid = callerUid; |
| 161 | mounted = false; |
| 162 | } |
| 163 | |
| 164 | // OBB source filename |
| 165 | String filename; |
| 166 | |
| 167 | // Token of remote Binder caller |
| 168 | IObbActionListener token; |
| 169 | |
| 170 | // Binder.callingUid() |
| 171 | public int callerUid; |
| 172 | |
| 173 | // Whether this is mounted currently. |
| 174 | boolean mounted; |
| 175 | |
| 176 | @Override |
| 177 | public void binderDied() { |
| 178 | ObbAction action = new UnmountObbAction(this, true); |
| 179 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
| 180 | |
| 181 | removeObbState(this); |
| 182 | |
| 183 | token.asBinder().unlinkToDeath(this, 0); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // OBB Action Handler |
| 188 | final private ObbActionHandler mObbActionHandler; |
| 189 | |
| 190 | // OBB action handler messages |
| 191 | private static final int OBB_RUN_ACTION = 1; |
| 192 | private static final int OBB_MCS_BOUND = 2; |
| 193 | private static final int OBB_MCS_UNBIND = 3; |
| 194 | private static final int OBB_MCS_RECONNECT = 4; |
| 195 | private static final int OBB_MCS_GIVE_UP = 5; |
| 196 | |
| 197 | /* |
| 198 | * Default Container Service information |
| 199 | */ |
| 200 | static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName( |
| 201 | "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService"); |
| 202 | |
| 203 | final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection(); |
| 204 | |
| 205 | class DefaultContainerConnection implements ServiceConnection { |
| 206 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 207 | if (DEBUG_OBB) |
| 208 | Slog.i(TAG, "onServiceConnected"); |
| 209 | IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service); |
| 210 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs)); |
| 211 | } |
| 212 | |
| 213 | public void onServiceDisconnected(ComponentName name) { |
| 214 | if (DEBUG_OBB) |
| 215 | Slog.i(TAG, "onServiceDisconnected"); |
| 216 | } |
| 217 | }; |
| 218 | |
| 219 | // Used in the ObbActionHandler |
| 220 | private IMediaContainerService mContainerService = null; |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 221 | |
| 222 | // Handler messages |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 223 | private static final int H_UNMOUNT_PM_UPDATE = 1; |
| 224 | private static final int H_UNMOUNT_PM_DONE = 2; |
| 225 | private static final int H_UNMOUNT_MS = 3; |
| 226 | private static final int RETRY_UNMOUNT_DELAY = 30; // in ms |
| 227 | private static final int MAX_UNMOUNT_RETRIES = 4; |
| 228 | |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 229 | class UnmountCallBack { |
| 230 | String path; |
| 231 | int retries; |
| 232 | boolean force; |
| 233 | |
| 234 | UnmountCallBack(String path, boolean force) { |
| 235 | retries = 0; |
| 236 | this.path = path; |
| 237 | this.force = force; |
| 238 | } |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 239 | |
| 240 | void handleFinished() { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 241 | if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path); |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 242 | doUnmountVolume(path, true); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | class UmsEnableCallBack extends UnmountCallBack { |
| 247 | String method; |
| 248 | |
| 249 | UmsEnableCallBack(String path, String method, boolean force) { |
| 250 | super(path, force); |
| 251 | this.method = method; |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | void handleFinished() { |
| 256 | super.handleFinished(); |
| 257 | doShareUnshareVolume(path, method, true); |
| 258 | } |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 261 | class ShutdownCallBack extends UnmountCallBack { |
| 262 | IMountShutdownObserver observer; |
| 263 | ShutdownCallBack(String path, IMountShutdownObserver observer) { |
| 264 | super(path, true); |
| 265 | this.observer = observer; |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | void handleFinished() { |
| 270 | int ret = doUnmountVolume(path, true); |
| 271 | if (observer != null) { |
| 272 | try { |
| 273 | observer.onShutDownComplete(ret); |
| 274 | } catch (RemoteException e) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 275 | Slog.w(TAG, "RemoteException when shutting down"); |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 281 | class MountServiceHandler extends Handler { |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 282 | ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>(); |
| Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 283 | boolean mUpdatingStatus = false; |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 284 | |
| Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 285 | MountServiceHandler(Looper l) { |
| 286 | super(l); |
| 287 | } |
| 288 | |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 289 | public void handleMessage(Message msg) { |
| 290 | switch (msg.what) { |
| 291 | case H_UNMOUNT_PM_UPDATE: { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 292 | if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE"); |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 293 | UnmountCallBack ucb = (UnmountCallBack) msg.obj; |
| 294 | mForceUnmounts.add(ucb); |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 295 | if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus); |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 296 | // Register only if needed. |
| Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 297 | if (!mUpdatingStatus) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 298 | if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager"); |
| Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 299 | mUpdatingStatus = true; |
| 300 | mPms.updateExternalMediaStatus(false, true); |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 301 | } |
| 302 | break; |
| 303 | } |
| 304 | case H_UNMOUNT_PM_DONE: { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 305 | if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE"); |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 306 | if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests"); |
| Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 307 | mUpdatingStatus = false; |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 308 | int size = mForceUnmounts.size(); |
| 309 | int sizeArr[] = new int[size]; |
| 310 | int sizeArrN = 0; |
| Suchi Amalapurapu | 7af074a | 2010-04-05 16:46:32 -0700 | [diff] [blame] | 311 | // Kill processes holding references first |
| 312 | ActivityManagerService ams = (ActivityManagerService) |
| 313 | ServiceManager.getService("activity"); |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 314 | for (int i = 0; i < size; i++) { |
| 315 | UnmountCallBack ucb = mForceUnmounts.get(i); |
| 316 | String path = ucb.path; |
| 317 | boolean done = false; |
| 318 | if (!ucb.force) { |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 319 | done = true; |
| 320 | } else { |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 321 | int pids[] = getStorageUsers(path); |
| 322 | if (pids == null || pids.length == 0) { |
| 323 | done = true; |
| 324 | } else { |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 325 | // Eliminate system process here? |
| Suchi Amalapurapu | 7af074a | 2010-04-05 16:46:32 -0700 | [diff] [blame] | 326 | ams.killPids(pids, "unmount media"); |
| 327 | // Confirm if file references have been freed. |
| 328 | pids = getStorageUsers(path); |
| 329 | if (pids == null || pids.length == 0) { |
| 330 | done = true; |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | } |
| Suchi Amalapurapu | 7af074a | 2010-04-05 16:46:32 -0700 | [diff] [blame] | 334 | if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) { |
| 335 | // Retry again |
| 336 | Slog.i(TAG, "Retrying to kill storage users again"); |
| 337 | mHandler.sendMessageDelayed( |
| 338 | mHandler.obtainMessage(H_UNMOUNT_PM_DONE, |
| 339 | ucb.retries++), |
| 340 | RETRY_UNMOUNT_DELAY); |
| 341 | } else { |
| 342 | if (ucb.retries >= MAX_UNMOUNT_RETRIES) { |
| 343 | Slog.i(TAG, "Failed to unmount media inspite of " + |
| 344 | MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now"); |
| 345 | } |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 346 | sizeArr[sizeArrN++] = i; |
| 347 | mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS, |
| 348 | ucb)); |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 349 | } |
| 350 | } |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 351 | // Remove already processed elements from list. |
| 352 | for (int i = (sizeArrN-1); i >= 0; i--) { |
| 353 | mForceUnmounts.remove(sizeArr[i]); |
| 354 | } |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 355 | break; |
| 356 | } |
| 357 | case H_UNMOUNT_MS : { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 358 | if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS"); |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 359 | UnmountCallBack ucb = (UnmountCallBack) msg.obj; |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 360 | ucb.handleFinished(); |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 361 | break; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | }; |
| Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 366 | final private HandlerThread mHandlerThread; |
| 367 | final private Handler mHandler; |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 368 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 369 | private void waitForReady() { |
| 370 | while (mReady == false) { |
| 371 | for (int retries = 5; retries > 0; retries--) { |
| 372 | if (mReady) { |
| 373 | return; |
| 374 | } |
| 375 | SystemClock.sleep(1000); |
| 376 | } |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 377 | Slog.w(TAG, "Waiting too long for mReady!"); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 378 | } |
| San Mehat | 1f6301e | 2010-01-07 22:40:27 -0800 | [diff] [blame] | 379 | } |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 380 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 381 | private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | public void onReceive(Context context, Intent intent) { |
| San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 383 | String action = intent.getAction(); |
| 384 | |
| 385 | if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 386 | mBooted = true; |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 387 | |
| Marco Nelissen | c34ebce | 2010-02-18 13:39:41 -0800 | [diff] [blame] | 388 | /* |
| 389 | * In the simulator, we need to broadcast a volume mounted event |
| 390 | * to make the media scanner run. |
| 391 | */ |
| 392 | if ("simulator".equals(SystemProperties.get("ro.product.device"))) { |
| 393 | notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted); |
| 394 | return; |
| 395 | } |
| San Mehat | fafb041 | 2010-02-18 19:40:04 -0800 | [diff] [blame] | 396 | new Thread() { |
| 397 | public void run() { |
| 398 | try { |
| 399 | String path = Environment.getExternalStorageDirectory().getPath(); |
| San Mehat | 6a25440 | 2010-03-22 10:21:00 -0700 | [diff] [blame] | 400 | String state = getVolumeState(path); |
| 401 | |
| Mike Lockwood | 0355975 | 2010-07-19 18:25:03 -0400 | [diff] [blame] | 402 | if (mEmulateExternalStorage) { |
| 403 | notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Mounted); |
| 404 | } else if (state.equals(Environment.MEDIA_UNMOUNTED)) { |
| San Mehat | fafb041 | 2010-02-18 19:40:04 -0800 | [diff] [blame] | 405 | int rc = doMountVolume(path); |
| 406 | if (rc != StorageResultCode.OperationSucceeded) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 407 | Slog.e(TAG, String.format("Boot-time mount failed (%d)", rc)); |
| San Mehat | fafb041 | 2010-02-18 19:40:04 -0800 | [diff] [blame] | 408 | } |
| San Mehat | 6a25440 | 2010-03-22 10:21:00 -0700 | [diff] [blame] | 409 | } else if (state.equals(Environment.MEDIA_SHARED)) { |
| 410 | /* |
| 411 | * Bootstrap UMS enabled state since vold indicates |
| 412 | * the volume is shared (runtime restart while ums enabled) |
| 413 | */ |
| 414 | notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Shared); |
| San Mehat | fafb041 | 2010-02-18 19:40:04 -0800 | [diff] [blame] | 415 | } |
| San Mehat | 6a25440 | 2010-03-22 10:21:00 -0700 | [diff] [blame] | 416 | |
| San Mehat | 6a965af2 | 2010-02-24 17:47:30 -0800 | [diff] [blame] | 417 | /* |
| San Mehat | 6a25440 | 2010-03-22 10:21:00 -0700 | [diff] [blame] | 418 | * If UMS was connected on boot, send the connected event |
| San Mehat | 6a965af2 | 2010-02-24 17:47:30 -0800 | [diff] [blame] | 419 | * now that we're up. |
| 420 | */ |
| 421 | if (mSendUmsConnectedOnBoot) { |
| 422 | sendUmsIntent(true); |
| 423 | mSendUmsConnectedOnBoot = false; |
| 424 | } |
| San Mehat | fafb041 | 2010-02-18 19:40:04 -0800 | [diff] [blame] | 425 | } catch (Exception ex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 426 | Slog.e(TAG, "Boot-time mount exception", ex); |
| San Mehat | fafb041 | 2010-02-18 19:40:04 -0800 | [diff] [blame] | 427 | } |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 428 | } |
| San Mehat | fafb041 | 2010-02-18 19:40:04 -0800 | [diff] [blame] | 429 | }.start(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | }; |
| 433 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 434 | private final class MountServiceBinderListener implements IBinder.DeathRecipient { |
| 435 | final IMountServiceListener mListener; |
| 436 | |
| 437 | MountServiceBinderListener(IMountServiceListener listener) { |
| 438 | mListener = listener; |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 439 | |
| San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 442 | public void binderDied() { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 443 | if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!"); |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 444 | synchronized (mListeners) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 445 | mListeners.remove(this); |
| 446 | mListener.asBinder().unlinkToDeath(this, 0); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 451 | private void doShareUnshareVolume(String path, String method, boolean enable) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 452 | // TODO: Add support for multiple share methods |
| 453 | if (!method.equals("ums")) { |
| 454 | throw new IllegalArgumentException(String.format("Method %s not supported", method)); |
| 455 | } |
| 456 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 457 | try { |
| 458 | mConnector.doCommand(String.format( |
| 459 | "volume %sshare %s %s", (enable ? "" : "un"), path, method)); |
| 460 | } catch (NativeDaemonConnectorException e) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 461 | Slog.e(TAG, "Failed to share/unshare", e); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 462 | } |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 465 | private void updatePublicVolumeState(String path, String state) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 466 | if (!path.equals(Environment.getExternalStorageDirectory().getPath())) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 467 | Slog.w(TAG, "Multiple volumes not currently supported"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 468 | return; |
| 469 | } |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 470 | |
| 471 | if (mLegacyState.equals(state)) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 472 | Slog.w(TAG, String.format("Duplicate state transition (%s -> %s)", mLegacyState, state)); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 473 | return; |
| 474 | } |
| Mike Lockwood | 0355975 | 2010-07-19 18:25:03 -0400 | [diff] [blame] | 475 | // Update state on PackageManager, but only of real events |
| 476 | if (!mEmulateExternalStorage) { |
| 477 | if (Environment.MEDIA_UNMOUNTED.equals(state)) { |
| 478 | mPms.updateExternalMediaStatus(false, false); |
| 479 | } else if (Environment.MEDIA_MOUNTED.equals(state)) { |
| 480 | mPms.updateExternalMediaStatus(true, false); |
| 481 | } |
| Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 482 | } |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 483 | String oldState = mLegacyState; |
| 484 | mLegacyState = state; |
| 485 | |
| 486 | synchronized (mListeners) { |
| 487 | for (int i = mListeners.size() -1; i >= 0; i--) { |
| 488 | MountServiceBinderListener bl = mListeners.get(i); |
| 489 | try { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 490 | bl.mListener.onStorageStateChanged(path, oldState, state); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 491 | } catch (RemoteException rex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 492 | Slog.e(TAG, "Listener dead"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 493 | mListeners.remove(i); |
| 494 | } catch (Exception ex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 495 | Slog.e(TAG, "Listener failed", ex); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * |
| 503 | * Callback from NativeDaemonConnector |
| 504 | */ |
| 505 | public void onDaemonConnected() { |
| 506 | /* |
| 507 | * Since we'll be calling back into the NativeDaemonConnector, |
| 508 | * we need to do our work in a new thread. |
| 509 | */ |
| 510 | new Thread() { |
| 511 | public void run() { |
| 512 | /** |
| 513 | * Determine media state and UMS detection status |
| 514 | */ |
| 515 | String path = Environment.getExternalStorageDirectory().getPath(); |
| 516 | String state = Environment.MEDIA_REMOVED; |
| 517 | |
| 518 | try { |
| 519 | String[] vols = mConnector.doListCommand( |
| 520 | "volume list", VoldResponseCode.VolumeListResult); |
| 521 | for (String volstr : vols) { |
| 522 | String[] tok = volstr.split(" "); |
| 523 | // FMT: <label> <mountpoint> <state> |
| 524 | if (!tok[1].equals(path)) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 525 | Slog.w(TAG, String.format( |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 526 | "Skipping unknown volume '%s'",tok[1])); |
| 527 | continue; |
| 528 | } |
| 529 | int st = Integer.parseInt(tok[2]); |
| 530 | if (st == VolumeState.NoMedia) { |
| 531 | state = Environment.MEDIA_REMOVED; |
| 532 | } else if (st == VolumeState.Idle) { |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 533 | state = Environment.MEDIA_UNMOUNTED; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 534 | } else if (st == VolumeState.Mounted) { |
| 535 | state = Environment.MEDIA_MOUNTED; |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 536 | Slog.i(TAG, "Media already mounted on daemon connection"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 537 | } else if (st == VolumeState.Shared) { |
| 538 | state = Environment.MEDIA_SHARED; |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 539 | Slog.i(TAG, "Media shared on daemon connection"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 540 | } else { |
| 541 | throw new Exception(String.format("Unexpected state %d", st)); |
| 542 | } |
| 543 | } |
| 544 | if (state != null) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 545 | if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 546 | updatePublicVolumeState(path, state); |
| 547 | } |
| 548 | } catch (Exception e) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 549 | Slog.e(TAG, "Error processing initial volume state", e); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 550 | updatePublicVolumeState(path, Environment.MEDIA_REMOVED); |
| 551 | } |
| 552 | |
| 553 | try { |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 554 | boolean avail = doGetShareMethodAvailable("ums"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 555 | notifyShareAvailabilityChange("ums", avail); |
| 556 | } catch (Exception ex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 557 | Slog.w(TAG, "Failed to get share availability"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 558 | } |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 559 | /* |
| 560 | * Now that we've done our initialization, release |
| 561 | * the hounds! |
| 562 | */ |
| 563 | mReady = true; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 564 | } |
| 565 | }.start(); |
| 566 | } |
| 567 | |
| 568 | /** |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 569 | * Callback from NativeDaemonConnector |
| 570 | */ |
| 571 | public boolean onEvent(int code, String raw, String[] cooked) { |
| 572 | Intent in = null; |
| 573 | |
| Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 574 | if (DEBUG_EVENTS) { |
| 575 | StringBuilder builder = new StringBuilder(); |
| 576 | builder.append("onEvent::"); |
| 577 | builder.append(" raw= " + raw); |
| 578 | if (cooked != null) { |
| 579 | builder.append(" cooked = " ); |
| 580 | for (String str : cooked) { |
| 581 | builder.append(" " + str); |
| 582 | } |
| 583 | } |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 584 | Slog.i(TAG, builder.toString()); |
| Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 585 | } |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 586 | if (code == VoldResponseCode.VolumeStateChange) { |
| 587 | /* |
| 588 | * One of the volumes we're managing has changed state. |
| 589 | * Format: "NNN Volume <label> <path> state changed |
| 590 | * from <old_#> (<old_str>) to <new_#> (<new_str>)" |
| 591 | */ |
| 592 | notifyVolumeStateChange( |
| 593 | cooked[2], cooked[3], Integer.parseInt(cooked[7]), |
| 594 | Integer.parseInt(cooked[10])); |
| 595 | } else if (code == VoldResponseCode.ShareAvailabilityChange) { |
| 596 | // FMT: NNN Share method <method> now <available|unavailable> |
| 597 | boolean avail = false; |
| 598 | if (cooked[5].equals("available")) { |
| 599 | avail = true; |
| 600 | } |
| 601 | notifyShareAvailabilityChange(cooked[3], avail); |
| 602 | } else if ((code == VoldResponseCode.VolumeDiskInserted) || |
| 603 | (code == VoldResponseCode.VolumeDiskRemoved) || |
| 604 | (code == VoldResponseCode.VolumeBadRemoval)) { |
| 605 | // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>) |
| 606 | // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>) |
| 607 | // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>) |
| 608 | final String label = cooked[2]; |
| 609 | final String path = cooked[3]; |
| 610 | int major = -1; |
| 611 | int minor = -1; |
| 612 | |
| 613 | try { |
| 614 | String devComp = cooked[6].substring(1, cooked[6].length() -1); |
| 615 | String[] devTok = devComp.split(":"); |
| 616 | major = Integer.parseInt(devTok[0]); |
| 617 | minor = Integer.parseInt(devTok[1]); |
| 618 | } catch (Exception ex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 619 | Slog.e(TAG, "Failed to parse major/minor", ex); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 620 | } |
| 621 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 622 | if (code == VoldResponseCode.VolumeDiskInserted) { |
| 623 | new Thread() { |
| 624 | public void run() { |
| 625 | try { |
| 626 | int rc; |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 627 | if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 628 | Slog.w(TAG, String.format("Insertion mount failed (%d)", rc)); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 629 | } |
| 630 | } catch (Exception ex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 631 | Slog.w(TAG, "Failed to mount media on insertion", ex); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | }.start(); |
| 635 | } else if (code == VoldResponseCode.VolumeDiskRemoved) { |
| 636 | /* |
| 637 | * This event gets trumped if we're already in BAD_REMOVAL state |
| 638 | */ |
| 639 | if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) { |
| 640 | return true; |
| 641 | } |
| 642 | /* Send the media unmounted event first */ |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 643 | if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 644 | updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED); |
| 645 | in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path)); |
| 646 | mContext.sendBroadcast(in); |
| 647 | |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 648 | if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 649 | updatePublicVolumeState(path, Environment.MEDIA_REMOVED); |
| 650 | in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path)); |
| 651 | } else if (code == VoldResponseCode.VolumeBadRemoval) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 652 | if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 653 | /* Send the media unmounted event first */ |
| 654 | updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED); |
| 655 | in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path)); |
| 656 | mContext.sendBroadcast(in); |
| 657 | |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 658 | if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 659 | updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL); |
| 660 | in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path)); |
| 661 | } else { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 662 | Slog.e(TAG, String.format("Unknown code {%d}", code)); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 663 | } |
| 664 | } else { |
| 665 | return false; |
| 666 | } |
| 667 | |
| 668 | if (in != null) { |
| 669 | mContext.sendBroadcast(in); |
| Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 670 | } |
| 671 | return true; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 672 | } |
| 673 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 674 | private void notifyVolumeStateChange(String label, String path, int oldState, int newState) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 675 | String vs = getVolumeState(path); |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 676 | if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 677 | |
| 678 | Intent in = null; |
| 679 | |
| Mike Lockwood | bf2dd44 | 2010-03-03 06:16:52 -0500 | [diff] [blame] | 680 | if (oldState == VolumeState.Shared && newState != oldState) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 681 | if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent"); |
| Mike Lockwood | bf2dd44 | 2010-03-03 06:16:52 -0500 | [diff] [blame] | 682 | mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED, |
| 683 | Uri.parse("file://" + path))); |
| 684 | } |
| 685 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 686 | if (newState == VolumeState.Init) { |
| 687 | } else if (newState == VolumeState.NoMedia) { |
| 688 | // NoMedia is handled via Disk Remove events |
| 689 | } else if (newState == VolumeState.Idle) { |
| 690 | /* |
| 691 | * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or |
| 692 | * if we're in the process of enabling UMS |
| 693 | */ |
| 694 | if (!vs.equals( |
| 695 | Environment.MEDIA_BAD_REMOVAL) && !vs.equals( |
| 696 | Environment.MEDIA_NOFS) && !vs.equals( |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 697 | Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 698 | if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 699 | updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED); |
| 700 | in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path)); |
| 701 | } |
| 702 | } else if (newState == VolumeState.Pending) { |
| 703 | } else if (newState == VolumeState.Checking) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 704 | if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 705 | updatePublicVolumeState(path, Environment.MEDIA_CHECKING); |
| 706 | in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path)); |
| 707 | } else if (newState == VolumeState.Mounted) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 708 | if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 709 | updatePublicVolumeState(path, Environment.MEDIA_MOUNTED); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 710 | in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path)); |
| 711 | in.putExtra("read-only", false); |
| 712 | } else if (newState == VolumeState.Unmounting) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 713 | in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path)); |
| 714 | } else if (newState == VolumeState.Formatting) { |
| 715 | } else if (newState == VolumeState.Shared) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 716 | if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 717 | /* Send the media unmounted event first */ |
| 718 | updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED); |
| 719 | in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path)); |
| 720 | mContext.sendBroadcast(in); |
| 721 | |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 722 | if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 723 | updatePublicVolumeState(path, Environment.MEDIA_SHARED); |
| 724 | in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path)); |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 725 | if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 726 | } else if (newState == VolumeState.SharedMnt) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 727 | Slog.e(TAG, "Live shared mounts not supported yet!"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 728 | return; |
| 729 | } else { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 730 | Slog.e(TAG, "Unhandled VolumeState {" + newState + "}"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | if (in != null) { |
| 734 | mContext.sendBroadcast(in); |
| 735 | } |
| 736 | } |
| 737 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 738 | private boolean doGetShareMethodAvailable(String method) { |
| Kenny Root | 85fb206 | 2010-06-01 20:50:21 -0700 | [diff] [blame] | 739 | ArrayList<String> rsp; |
| Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 740 | try { |
| Kenny Root | 85fb206 | 2010-06-01 20:50:21 -0700 | [diff] [blame] | 741 | rsp = mConnector.doCommand("share status " + method); |
| Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 742 | } catch (NativeDaemonConnectorException ex) { |
| 743 | Slog.e(TAG, "Failed to determine whether share method " + method + " is available."); |
| 744 | return false; |
| 745 | } |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 746 | |
| 747 | for (String line : rsp) { |
| Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 748 | String[] tok = line.split(" "); |
| 749 | if (tok.length < 3) { |
| 750 | Slog.e(TAG, "Malformed response to share status " + method); |
| 751 | return false; |
| 752 | } |
| 753 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 754 | int code; |
| 755 | try { |
| 756 | code = Integer.parseInt(tok[0]); |
| 757 | } catch (NumberFormatException nfe) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 758 | Slog.e(TAG, String.format("Error parsing code %s", tok[0])); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 759 | return false; |
| 760 | } |
| 761 | if (code == VoldResponseCode.ShareStatusResult) { |
| 762 | if (tok[2].equals("available")) |
| 763 | return true; |
| 764 | return false; |
| 765 | } else { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 766 | Slog.e(TAG, String.format("Unexpected response code %d", code)); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 767 | return false; |
| 768 | } |
| 769 | } |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 770 | Slog.e(TAG, "Got an empty response"); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 771 | return false; |
| 772 | } |
| 773 | |
| 774 | private int doMountVolume(String path) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 775 | int rc = StorageResultCode.OperationSucceeded; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 776 | |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 777 | if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 778 | try { |
| 779 | mConnector.doCommand(String.format("volume mount %s", path)); |
| 780 | } catch (NativeDaemonConnectorException e) { |
| 781 | /* |
| 782 | * Mount failed for some reason |
| 783 | */ |
| 784 | Intent in = null; |
| 785 | int code = e.getCode(); |
| 786 | if (code == VoldResponseCode.OpFailedNoMedia) { |
| 787 | /* |
| 788 | * Attempt to mount but no media inserted |
| 789 | */ |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 790 | rc = StorageResultCode.OperationFailedNoMedia; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 791 | } else if (code == VoldResponseCode.OpFailedMediaBlank) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 792 | if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs"); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 793 | /* |
| 794 | * Media is blank or does not contain a supported filesystem |
| 795 | */ |
| 796 | updatePublicVolumeState(path, Environment.MEDIA_NOFS); |
| 797 | in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path)); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 798 | rc = StorageResultCode.OperationFailedMediaBlank; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 799 | } else if (code == VoldResponseCode.OpFailedMediaCorrupt) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 800 | if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt"); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 801 | /* |
| 802 | * Volume consistency check failed |
| 803 | */ |
| 804 | updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE); |
| 805 | in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path)); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 806 | rc = StorageResultCode.OperationFailedMediaCorrupt; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 807 | } else { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 808 | rc = StorageResultCode.OperationFailedInternalError; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /* |
| 812 | * Send broadcast intent (if required for the failure) |
| 813 | */ |
| 814 | if (in != null) { |
| 815 | mContext.sendBroadcast(in); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | return rc; |
| 820 | } |
| 821 | |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 822 | /* |
| 823 | * If force is not set, we do not unmount if there are |
| 824 | * processes holding references to the volume about to be unmounted. |
| 825 | * If force is set, all the processes holding references need to be |
| 826 | * killed via the ActivityManager before actually unmounting the volume. |
| 827 | * This might even take a while and might be retried after timed delays |
| 828 | * to make sure we dont end up in an instable state and kill some core |
| 829 | * processes. |
| 830 | */ |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 831 | private int doUnmountVolume(String path, boolean force) { |
| San Mehat | 59443a6 | 2010-02-09 13:28:45 -0800 | [diff] [blame] | 832 | if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) { |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 833 | return VoldResponseCode.OpFailedVolNotMounted; |
| 834 | } |
| Kenny Root | aa48540 | 2010-09-14 14:49:41 -0700 | [diff] [blame] | 835 | |
| 836 | /* |
| 837 | * Force a GC to make sure AssetManagers in other threads of the |
| 838 | * system_server are cleaned up. We have to do this since AssetManager |
| 839 | * instances are kept as a WeakReference and it's possible we have files |
| 840 | * open on the external storage. |
| 841 | */ |
| 842 | Runtime.getRuntime().gc(); |
| 843 | |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 844 | // Redundant probably. But no harm in updating state again. |
| Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 845 | mPms.updateExternalMediaStatus(false, false); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 846 | try { |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 847 | mConnector.doCommand(String.format( |
| 848 | "volume unmount %s%s", path, (force ? " force" : ""))); |
| Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 849 | // We unmounted the volume. None of the asec containers are available now. |
| 850 | synchronized (mAsecMountSet) { |
| 851 | mAsecMountSet.clear(); |
| 852 | } |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 853 | return StorageResultCode.OperationSucceeded; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 854 | } catch (NativeDaemonConnectorException e) { |
| 855 | // Don't worry about mismatch in PackageManager since the |
| 856 | // call back will handle the status changes any way. |
| 857 | int code = e.getCode(); |
| 858 | if (code == VoldResponseCode.OpFailedVolNotMounted) { |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 859 | return StorageResultCode.OperationFailedStorageNotMounted; |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 860 | } else if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 861 | return StorageResultCode.OperationFailedStorageBusy; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 862 | } else { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 863 | return StorageResultCode.OperationFailedInternalError; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | private int doFormatVolume(String path) { |
| 869 | try { |
| 870 | String cmd = String.format("volume format %s", path); |
| 871 | mConnector.doCommand(cmd); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 872 | return StorageResultCode.OperationSucceeded; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 873 | } catch (NativeDaemonConnectorException e) { |
| 874 | int code = e.getCode(); |
| 875 | if (code == VoldResponseCode.OpFailedNoMedia) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 876 | return StorageResultCode.OperationFailedNoMedia; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 877 | } else if (code == VoldResponseCode.OpFailedMediaCorrupt) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 878 | return StorageResultCode.OperationFailedMediaCorrupt; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 879 | } else { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 880 | return StorageResultCode.OperationFailedInternalError; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | } |
| 884 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 885 | private boolean doGetVolumeShared(String path, String method) { |
| 886 | String cmd = String.format("volume shared %s %s", path, method); |
| Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 887 | ArrayList<String> rsp; |
| 888 | |
| 889 | try { |
| 890 | rsp = mConnector.doCommand(cmd); |
| 891 | } catch (NativeDaemonConnectorException ex) { |
| 892 | Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method); |
| 893 | return false; |
| 894 | } |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 895 | |
| 896 | for (String line : rsp) { |
| Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 897 | String[] tok = line.split(" "); |
| 898 | if (tok.length < 3) { |
| 899 | Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command"); |
| 900 | return false; |
| 901 | } |
| 902 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 903 | int code; |
| 904 | try { |
| 905 | code = Integer.parseInt(tok[0]); |
| 906 | } catch (NumberFormatException nfe) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 907 | Slog.e(TAG, String.format("Error parsing code %s", tok[0])); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 908 | return false; |
| 909 | } |
| 910 | if (code == VoldResponseCode.ShareEnabledResult) { |
| Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame] | 911 | return "enabled".equals(tok[2]); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 912 | } else { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 913 | Slog.e(TAG, String.format("Unexpected response code %d", code)); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 914 | return false; |
| 915 | } |
| 916 | } |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 917 | Slog.e(TAG, "Got an empty response"); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 918 | return false; |
| 919 | } |
| 920 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 921 | private void notifyShareAvailabilityChange(String method, final boolean avail) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 922 | if (!method.equals("ums")) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 923 | Slog.w(TAG, "Ignoring unsupported share method {" + method + "}"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 924 | return; |
| 925 | } |
| 926 | |
| 927 | synchronized (mListeners) { |
| 928 | for (int i = mListeners.size() -1; i >= 0; i--) { |
| 929 | MountServiceBinderListener bl = mListeners.get(i); |
| 930 | try { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 931 | bl.mListener.onUsbMassStorageConnectionChanged(avail); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 932 | } catch (RemoteException rex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 933 | Slog.e(TAG, "Listener dead"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 934 | mListeners.remove(i); |
| 935 | } catch (Exception ex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 936 | Slog.e(TAG, "Listener failed", ex); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | } |
| 940 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 941 | if (mBooted == true) { |
| San Mehat | 6a965af2 | 2010-02-24 17:47:30 -0800 | [diff] [blame] | 942 | sendUmsIntent(avail); |
| 943 | } else { |
| 944 | mSendUmsConnectedOnBoot = avail; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 945 | } |
| San Mehat | 2fe718a | 2010-03-11 12:01:49 -0800 | [diff] [blame] | 946 | |
| 947 | final String path = Environment.getExternalStorageDirectory().getPath(); |
| 948 | if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) { |
| 949 | /* |
| 950 | * USB mass storage disconnected while enabled |
| 951 | */ |
| 952 | new Thread() { |
| 953 | public void run() { |
| 954 | try { |
| 955 | int rc; |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 956 | Slog.w(TAG, "Disabling UMS after cable disconnect"); |
| San Mehat | 2fe718a | 2010-03-11 12:01:49 -0800 | [diff] [blame] | 957 | doShareUnshareVolume(path, "ums", false); |
| 958 | if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 959 | Slog.e(TAG, String.format( |
| San Mehat | 2fe718a | 2010-03-11 12:01:49 -0800 | [diff] [blame] | 960 | "Failed to remount {%s} on UMS enabled-disconnect (%d)", |
| 961 | path, rc)); |
| 962 | } |
| 963 | } catch (Exception ex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 964 | Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex); |
| San Mehat | 2fe718a | 2010-03-11 12:01:49 -0800 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | }.start(); |
| 968 | } |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 969 | } |
| 970 | |
| San Mehat | 6a965af2 | 2010-02-24 17:47:30 -0800 | [diff] [blame] | 971 | private void sendUmsIntent(boolean c) { |
| 972 | mContext.sendBroadcast( |
| 973 | new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED))); |
| 974 | } |
| 975 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 976 | private void validatePermission(String perm) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 977 | if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) { |
| 978 | throw new SecurityException(String.format("Requires %s permission", perm)); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | /** |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 983 | * Constructs a new MountService instance |
| 984 | * |
| 985 | * @param context Binder context for this service |
| 986 | */ |
| 987 | public MountService(Context context) { |
| 988 | mContext = context; |
| 989 | |
| Mike Lockwood | 0355975 | 2010-07-19 18:25:03 -0400 | [diff] [blame] | 990 | mEmulateExternalStorage = context.getResources().getBoolean( |
| 991 | com.android.internal.R.bool.config_emulateExternalStorage); |
| 992 | if (mEmulateExternalStorage) { |
| 993 | Slog.d(TAG, "using emulated external storage"); |
| 994 | mLegacyState = Environment.MEDIA_MOUNTED; |
| 995 | } |
| 996 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 997 | // XXX: This will go away soon in favor of IMountServiceObserver |
| 998 | mPms = (PackageManagerService) ServiceManager.getService("package"); |
| 999 | |
| 1000 | mContext.registerReceiver(mBroadcastReceiver, |
| 1001 | new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null); |
| 1002 | |
| Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 1003 | mHandlerThread = new HandlerThread("MountService"); |
| 1004 | mHandlerThread.start(); |
| 1005 | mHandler = new MountServiceHandler(mHandlerThread.getLooper()); |
| 1006 | |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1007 | // Add OBB Action Handler to MountService thread. |
| 1008 | mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper()); |
| 1009 | |
| Marco Nelissen | c34ebce | 2010-02-18 13:39:41 -0800 | [diff] [blame] | 1010 | /* |
| 1011 | * Vold does not run in the simulator, so pretend the connector thread |
| 1012 | * ran and did its thing. |
| 1013 | */ |
| 1014 | if ("simulator".equals(SystemProperties.get("ro.product.device"))) { |
| 1015 | mReady = true; |
| 1016 | mUmsEnabling = true; |
| 1017 | return; |
| 1018 | } |
| 1019 | |
| Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 1020 | /* |
| 1021 | * Create the connection to vold with a maximum queue of twice the |
| 1022 | * amount of containers we'd ever expect to have. This keeps an |
| 1023 | * "asec list" from blocking a thread repeatedly. |
| 1024 | */ |
| 1025 | mConnector = new NativeDaemonConnector(this, "vold", |
| 1026 | PackageManagerService.MAX_CONTAINERS * 2, VOLD_TAG); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1027 | mReady = false; |
| Kenny Root | 305bcbf | 2010-09-03 07:56:38 -0700 | [diff] [blame] | 1028 | Thread thread = new Thread(mConnector, VOLD_TAG); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1029 | thread.start(); |
| 1030 | } |
| 1031 | |
| 1032 | /** |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1033 | * Exposed API calls below here |
| 1034 | */ |
| 1035 | |
| 1036 | public void registerListener(IMountServiceListener listener) { |
| 1037 | synchronized (mListeners) { |
| 1038 | MountServiceBinderListener bl = new MountServiceBinderListener(listener); |
| 1039 | try { |
| 1040 | listener.asBinder().linkToDeath(bl, 0); |
| 1041 | mListeners.add(bl); |
| 1042 | } catch (RemoteException rex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1043 | Slog.e(TAG, "Failed to link to listener death"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | public void unregisterListener(IMountServiceListener listener) { |
| 1049 | synchronized (mListeners) { |
| 1050 | for(MountServiceBinderListener bl : mListeners) { |
| 1051 | if (bl.mListener == listener) { |
| 1052 | mListeners.remove(mListeners.indexOf(bl)); |
| 1053 | return; |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 1059 | public void shutdown(final IMountShutdownObserver observer) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1060 | validatePermission(android.Manifest.permission.SHUTDOWN); |
| 1061 | |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1062 | Slog.i(TAG, "Shutting down"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1063 | |
| 1064 | String path = Environment.getExternalStorageDirectory().getPath(); |
| 1065 | String state = getVolumeState(path); |
| San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 1066 | |
| 1067 | if (state.equals(Environment.MEDIA_SHARED)) { |
| 1068 | /* |
| 1069 | * If the media is currently shared, unshare it. |
| 1070 | * XXX: This is still dangerous!. We should not |
| 1071 | * be rebooting at *all* if UMS is enabled, since |
| 1072 | * the UMS host could have dirty FAT cache entries |
| 1073 | * yet to flush. |
| 1074 | */ |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1075 | setUsbMassStorageEnabled(false); |
| San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 1076 | } else if (state.equals(Environment.MEDIA_CHECKING)) { |
| 1077 | /* |
| 1078 | * If the media is being checked, then we need to wait for |
| 1079 | * it to complete before being able to proceed. |
| 1080 | */ |
| 1081 | // XXX: @hackbod - Should we disable the ANR timer here? |
| 1082 | int retries = 30; |
| 1083 | while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) { |
| 1084 | try { |
| 1085 | Thread.sleep(1000); |
| 1086 | } catch (InterruptedException iex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1087 | Slog.e(TAG, "Interrupted while waiting for media", iex); |
| San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 1088 | break; |
| 1089 | } |
| 1090 | state = Environment.getExternalStorageState(); |
| 1091 | } |
| 1092 | if (retries == 0) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1093 | Slog.e(TAG, "Timed out waiting for media to check"); |
| San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | if (state.equals(Environment.MEDIA_MOUNTED)) { |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 1098 | // Post a unmount message. |
| 1099 | ShutdownCallBack ucb = new ShutdownCallBack(path, observer); |
| 1100 | mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb)); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1104 | private boolean getUmsEnabling() { |
| 1105 | synchronized (mListeners) { |
| 1106 | return mUmsEnabling; |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | private void setUmsEnabling(boolean enable) { |
| 1111 | synchronized (mListeners) { |
| Tony Wu | fc71125 | 2010-08-09 16:49:19 +0800 | [diff] [blame] | 1112 | mUmsEnabling = enable; |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1116 | public boolean isUsbMassStorageConnected() { |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1117 | waitForReady(); |
| San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 1118 | |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1119 | if (getUmsEnabling()) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1120 | return true; |
| San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 1121 | } |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1122 | return doGetShareMethodAvailable("ums"); |
| 1123 | } |
| 1124 | |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1125 | public void setUsbMassStorageEnabled(boolean enable) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1126 | waitForReady(); |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1127 | validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1128 | |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1129 | // TODO: Add support for multiple share methods |
| 1130 | |
| 1131 | /* |
| 1132 | * If the volume is mounted and we're enabling then unmount it |
| 1133 | */ |
| 1134 | String path = Environment.getExternalStorageDirectory().getPath(); |
| 1135 | String vs = getVolumeState(path); |
| 1136 | String method = "ums"; |
| 1137 | if (enable && vs.equals(Environment.MEDIA_MOUNTED)) { |
| 1138 | // Override for isUsbMassStorageEnabled() |
| 1139 | setUmsEnabling(enable); |
| 1140 | UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true); |
| 1141 | mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb)); |
| 1142 | // Clear override |
| 1143 | setUmsEnabling(false); |
| 1144 | } |
| 1145 | /* |
| 1146 | * If we disabled UMS then mount the volume |
| 1147 | */ |
| 1148 | if (!enable) { |
| 1149 | doShareUnshareVolume(path, method, enable); |
| 1150 | if (doMountVolume(path) != StorageResultCode.OperationSucceeded) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1151 | Slog.e(TAG, "Failed to remount " + path + |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1152 | " after disabling share method " + method); |
| 1153 | /* |
| 1154 | * Even though the mount failed, the unshare didn't so don't indicate an error. |
| 1155 | * The mountVolume() call will have set the storage state and sent the necessary |
| 1156 | * broadcasts. |
| 1157 | */ |
| 1158 | } |
| 1159 | } |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | public boolean isUsbMassStorageEnabled() { |
| 1163 | waitForReady(); |
| 1164 | return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1165 | } |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1166 | |
| San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 1167 | /** |
| 1168 | * @return state of the volume at the specified mount point |
| 1169 | */ |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1170 | public String getVolumeState(String mountPoint) { |
| San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 1171 | /* |
| 1172 | * XXX: Until we have multiple volume discovery, just hardwire |
| 1173 | * this to /sdcard |
| 1174 | */ |
| 1175 | if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1176 | Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume"); |
| San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 1177 | throw new IllegalArgumentException(); |
| 1178 | } |
| 1179 | |
| 1180 | return mLegacyState; |
| 1181 | } |
| 1182 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1183 | public int mountVolume(String path) { |
| 1184 | validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1185 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1186 | waitForReady(); |
| 1187 | return doMountVolume(path); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1188 | } |
| 1189 | |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 1190 | public void unmountVolume(String path, boolean force) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1191 | validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1192 | waitForReady(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1193 | |
| Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1194 | String volState = getVolumeState(path); |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1195 | if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force); |
| Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1196 | if (Environment.MEDIA_UNMOUNTED.equals(volState) || |
| 1197 | Environment.MEDIA_REMOVED.equals(volState) || |
| 1198 | Environment.MEDIA_SHARED.equals(volState) || |
| 1199 | Environment.MEDIA_UNMOUNTABLE.equals(volState)) { |
| 1200 | // Media already unmounted or cannot be unmounted. |
| 1201 | // TODO return valid return code when adding observer call back. |
| 1202 | return; |
| 1203 | } |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 1204 | UnmountCallBack ucb = new UnmountCallBack(path, force); |
| 1205 | mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1206 | } |
| 1207 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1208 | public int formatVolume(String path) { |
| 1209 | validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1210 | waitForReady(); |
| San Mehat | 5b77dab | 2010-01-26 13:28:50 -0800 | [diff] [blame] | 1211 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1212 | return doFormatVolume(path); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1213 | } |
| 1214 | |
| San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1215 | public int []getStorageUsers(String path) { |
| 1216 | validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1217 | waitForReady(); |
| 1218 | try { |
| 1219 | String[] r = mConnector.doListCommand( |
| 1220 | String.format("storage users %s", path), |
| 1221 | VoldResponseCode.StorageUsersListResult); |
| 1222 | // FMT: <pid> <process name> |
| 1223 | int[] data = new int[r.length]; |
| 1224 | for (int i = 0; i < r.length; i++) { |
| 1225 | String []tok = r[i].split(" "); |
| 1226 | try { |
| 1227 | data[i] = Integer.parseInt(tok[0]); |
| 1228 | } catch (NumberFormatException nfe) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1229 | Slog.e(TAG, String.format("Error parsing pid %s", tok[0])); |
| San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1230 | return new int[0]; |
| 1231 | } |
| 1232 | } |
| 1233 | return data; |
| 1234 | } catch (NativeDaemonConnectorException e) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1235 | Slog.e(TAG, "Failed to retrieve storage users list", e); |
| San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1236 | return new int[0]; |
| 1237 | } |
| 1238 | } |
| 1239 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1240 | private void warnOnNotMounted() { |
| 1241 | if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1242 | Slog.w(TAG, "getSecureContainerList() called when storage not mounted"); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1246 | public String[] getSecureContainerList() { |
| 1247 | validatePermission(android.Manifest.permission.ASEC_ACCESS); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1248 | waitForReady(); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1249 | warnOnNotMounted(); |
| San Mehat | f919cd02 | 2010-02-04 15:10:38 -0800 | [diff] [blame] | 1250 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1251 | try { |
| 1252 | return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult); |
| 1253 | } catch (NativeDaemonConnectorException e) { |
| 1254 | return new String[0]; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | } |
| 1256 | } |
| San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1257 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1258 | public int createSecureContainer(String id, int sizeMb, String fstype, |
| 1259 | String key, int ownerUid) { |
| 1260 | validatePermission(android.Manifest.permission.ASEC_CREATE); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1261 | waitForReady(); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1262 | warnOnNotMounted(); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1263 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1264 | int rc = StorageResultCode.OperationSucceeded; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1265 | String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid); |
| 1266 | try { |
| 1267 | mConnector.doCommand(cmd); |
| 1268 | } catch (NativeDaemonConnectorException e) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1269 | rc = StorageResultCode.OperationFailedInternalError; |
| San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1270 | } |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1271 | |
| 1272 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1273 | synchronized (mAsecMountSet) { |
| 1274 | mAsecMountSet.add(id); |
| 1275 | } |
| 1276 | } |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1277 | return rc; |
| San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1278 | } |
| 1279 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1280 | public int finalizeSecureContainer(String id) { |
| 1281 | validatePermission(android.Manifest.permission.ASEC_CREATE); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1282 | warnOnNotMounted(); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1283 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1284 | int rc = StorageResultCode.OperationSucceeded; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1285 | try { |
| 1286 | mConnector.doCommand(String.format("asec finalize %s", id)); |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1287 | /* |
| 1288 | * Finalization does a remount, so no need |
| 1289 | * to update mAsecMountSet |
| 1290 | */ |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1291 | } catch (NativeDaemonConnectorException e) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1292 | rc = StorageResultCode.OperationFailedInternalError; |
| San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1293 | } |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1294 | return rc; |
| San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1295 | } |
| 1296 | |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1297 | public int destroySecureContainer(String id, boolean force) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1298 | validatePermission(android.Manifest.permission.ASEC_DESTROY); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1299 | waitForReady(); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1300 | warnOnNotMounted(); |
| San Mehat | f919cd02 | 2010-02-04 15:10:38 -0800 | [diff] [blame] | 1301 | |
| Kenny Root | aa48540 | 2010-09-14 14:49:41 -0700 | [diff] [blame] | 1302 | /* |
| 1303 | * Force a GC to make sure AssetManagers in other threads of the |
| 1304 | * system_server are cleaned up. We have to do this since AssetManager |
| 1305 | * instances are kept as a WeakReference and it's possible we have files |
| 1306 | * open on the external storage. |
| 1307 | */ |
| 1308 | Runtime.getRuntime().gc(); |
| 1309 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1310 | int rc = StorageResultCode.OperationSucceeded; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1311 | try { |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1312 | mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : ""))); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1313 | } catch (NativeDaemonConnectorException e) { |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1314 | int code = e.getCode(); |
| 1315 | if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 1316 | rc = StorageResultCode.OperationFailedStorageBusy; |
| 1317 | } else { |
| 1318 | rc = StorageResultCode.OperationFailedInternalError; |
| 1319 | } |
| San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1320 | } |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1321 | |
| 1322 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1323 | synchronized (mAsecMountSet) { |
| 1324 | if (mAsecMountSet.contains(id)) { |
| 1325 | mAsecMountSet.remove(id); |
| 1326 | } |
| 1327 | } |
| 1328 | } |
| 1329 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1330 | return rc; |
| San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1333 | public int mountSecureContainer(String id, String key, int ownerUid) { |
| 1334 | validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1335 | waitForReady(); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1336 | warnOnNotMounted(); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1337 | |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1338 | synchronized (mAsecMountSet) { |
| 1339 | if (mAsecMountSet.contains(id)) { |
| 1340 | return StorageResultCode.OperationFailedStorageMounted; |
| 1341 | } |
| 1342 | } |
| 1343 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1344 | int rc = StorageResultCode.OperationSucceeded; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1345 | String cmd = String.format("asec mount %s %s %d", id, key, ownerUid); |
| 1346 | try { |
| 1347 | mConnector.doCommand(cmd); |
| 1348 | } catch (NativeDaemonConnectorException e) { |
| Kenny Root | f030462 | 2010-03-19 19:20:42 -0700 | [diff] [blame] | 1349 | int code = e.getCode(); |
| 1350 | if (code != VoldResponseCode.OpFailedStorageBusy) { |
| 1351 | rc = StorageResultCode.OperationFailedInternalError; |
| 1352 | } |
| San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1353 | } |
| San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1354 | |
| 1355 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1356 | synchronized (mAsecMountSet) { |
| 1357 | mAsecMountSet.add(id); |
| 1358 | } |
| 1359 | } |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1360 | return rc; |
| San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1361 | } |
| 1362 | |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1363 | public int unmountSecureContainer(String id, boolean force) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1364 | validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1365 | waitForReady(); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1366 | warnOnNotMounted(); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1367 | |
| San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1368 | synchronized (mAsecMountSet) { |
| 1369 | if (!mAsecMountSet.contains(id)) { |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1370 | return StorageResultCode.OperationFailedStorageNotMounted; |
| San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | |
| Kenny Root | aa48540 | 2010-09-14 14:49:41 -0700 | [diff] [blame] | 1374 | /* |
| 1375 | * Force a GC to make sure AssetManagers in other threads of the |
| 1376 | * system_server are cleaned up. We have to do this since AssetManager |
| 1377 | * instances are kept as a WeakReference and it's possible we have files |
| 1378 | * open on the external storage. |
| 1379 | */ |
| 1380 | Runtime.getRuntime().gc(); |
| 1381 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1382 | int rc = StorageResultCode.OperationSucceeded; |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1383 | String cmd = String.format("asec unmount %s%s", id, (force ? " force" : "")); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1384 | try { |
| 1385 | mConnector.doCommand(cmd); |
| 1386 | } catch (NativeDaemonConnectorException e) { |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1387 | int code = e.getCode(); |
| 1388 | if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 1389 | rc = StorageResultCode.OperationFailedStorageBusy; |
| 1390 | } else { |
| 1391 | rc = StorageResultCode.OperationFailedInternalError; |
| 1392 | } |
| San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1393 | } |
| San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1394 | |
| 1395 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1396 | synchronized (mAsecMountSet) { |
| 1397 | mAsecMountSet.remove(id); |
| 1398 | } |
| 1399 | } |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1400 | return rc; |
| San Mehat | 9dba709 | 2010-01-18 06:47:41 -0800 | [diff] [blame] | 1401 | } |
| 1402 | |
| San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1403 | public boolean isSecureContainerMounted(String id) { |
| 1404 | validatePermission(android.Manifest.permission.ASEC_ACCESS); |
| 1405 | waitForReady(); |
| 1406 | warnOnNotMounted(); |
| 1407 | |
| 1408 | synchronized (mAsecMountSet) { |
| 1409 | return mAsecMountSet.contains(id); |
| 1410 | } |
| 1411 | } |
| 1412 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1413 | public int renameSecureContainer(String oldId, String newId) { |
| 1414 | validatePermission(android.Manifest.permission.ASEC_RENAME); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1415 | waitForReady(); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1416 | warnOnNotMounted(); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1417 | |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1418 | synchronized (mAsecMountSet) { |
| San Mehat | 85451ee | 2010-02-24 08:54:18 -0800 | [diff] [blame] | 1419 | /* |
| 1420 | * Because a mounted container has active internal state which cannot be |
| 1421 | * changed while active, we must ensure both ids are not currently mounted. |
| 1422 | */ |
| 1423 | if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) { |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1424 | return StorageResultCode.OperationFailedStorageMounted; |
| 1425 | } |
| 1426 | } |
| 1427 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1428 | int rc = StorageResultCode.OperationSucceeded; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1429 | String cmd = String.format("asec rename %s %s", oldId, newId); |
| 1430 | try { |
| 1431 | mConnector.doCommand(cmd); |
| 1432 | } catch (NativeDaemonConnectorException e) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1433 | rc = StorageResultCode.OperationFailedInternalError; |
| San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1434 | } |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1435 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1436 | return rc; |
| San Mehat | 45f6104 | 2010-01-23 08:12:43 -0800 | [diff] [blame] | 1437 | } |
| 1438 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1439 | public String getSecureContainerPath(String id) { |
| 1440 | validatePermission(android.Manifest.permission.ASEC_ACCESS); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1441 | waitForReady(); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1442 | warnOnNotMounted(); |
| San Mehat | f919cd02 | 2010-02-04 15:10:38 -0800 | [diff] [blame] | 1443 | |
| San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 1444 | try { |
| 1445 | ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id)); |
| 1446 | String []tok = rsp.get(0).split(" "); |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 1447 | int code = Integer.parseInt(tok[0]); |
| San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 1448 | if (code != VoldResponseCode.AsecPathResult) { |
| 1449 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 1450 | } |
| 1451 | return tok[1]; |
| 1452 | } catch (NativeDaemonConnectorException e) { |
| 1453 | int code = e.getCode(); |
| 1454 | if (code == VoldResponseCode.OpFailedStorageNotFound) { |
| 1455 | throw new IllegalArgumentException(String.format("Container '%s' not found", id)); |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 1456 | } else { |
| San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 1457 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 1458 | } |
| 1459 | } |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 1460 | } |
| Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 1461 | |
| 1462 | public void finishMediaUpdate() { |
| 1463 | mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE); |
| 1464 | } |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1465 | |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1466 | private boolean isCallerOwnerOfPackageOrSystem(String packageName) { |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1467 | final int callerUid = Binder.getCallingUid(); |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1468 | return isUidOwnerOfPackageOrSystem(packageName, callerUid); |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1469 | } |
| 1470 | |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1471 | private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) { |
| 1472 | if (callerUid == android.os.Process.SYSTEM_UID) { |
| 1473 | return true; |
| 1474 | } |
| 1475 | |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1476 | if (packageName == null) { |
| 1477 | return false; |
| 1478 | } |
| 1479 | |
| 1480 | final int packageUid = mPms.getPackageUid(packageName); |
| 1481 | |
| 1482 | if (DEBUG_OBB) { |
| 1483 | Slog.d(TAG, "packageName = " + packageName + ", packageUid = " + |
| 1484 | packageUid + ", callerUid = " + callerUid); |
| 1485 | } |
| 1486 | |
| 1487 | return callerUid == packageUid; |
| 1488 | } |
| 1489 | |
| 1490 | public String getMountedObbPath(String filename) { |
| 1491 | waitForReady(); |
| 1492 | warnOnNotMounted(); |
| 1493 | |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1494 | try { |
| 1495 | ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename)); |
| 1496 | String []tok = rsp.get(0).split(" "); |
| 1497 | int code = Integer.parseInt(tok[0]); |
| 1498 | if (code != VoldResponseCode.AsecPathResult) { |
| 1499 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 1500 | } |
| 1501 | return tok[1]; |
| 1502 | } catch (NativeDaemonConnectorException e) { |
| 1503 | int code = e.getCode(); |
| 1504 | if (code == VoldResponseCode.OpFailedStorageNotFound) { |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1505 | return null; |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1506 | } else { |
| 1507 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | public boolean isObbMounted(String filename) { |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1513 | synchronized (mObbMounts) { |
| 1514 | return mObbPathToStateMap.containsKey(filename); |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1515 | } |
| 1516 | } |
| 1517 | |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1518 | public void mountObb(String filename, String key, IObbActionListener token) { |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1519 | waitForReady(); |
| 1520 | warnOnNotMounted(); |
| 1521 | |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1522 | final ObbState obbState; |
| 1523 | |
| 1524 | synchronized (mObbMounts) { |
| 1525 | if (isObbMounted(filename)) { |
| 1526 | throw new IllegalArgumentException("OBB file is already mounted"); |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1527 | } |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1528 | |
| 1529 | if (mObbMounts.containsKey(token)) { |
| 1530 | throw new IllegalArgumentException("You may only have one OBB mounted at a time"); |
| 1531 | } |
| 1532 | |
| 1533 | final int callerUid = Binder.getCallingUid(); |
| 1534 | obbState = new ObbState(filename, token, callerUid); |
| 1535 | addObbState(obbState); |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1538 | try { |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1539 | token.asBinder().linkToDeath(obbState, 0); |
| 1540 | } catch (RemoteException rex) { |
| 1541 | Slog.e(TAG, "Failed to link to listener death"); |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1544 | MountObbAction action = new MountObbAction(obbState, key); |
| 1545 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
| 1546 | |
| 1547 | if (DEBUG_OBB) |
| 1548 | Slog.i(TAG, "Send to OBB handler: " + action.toString()); |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1549 | } |
| 1550 | |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1551 | public void unmountObb(String filename, boolean force, IObbActionListener token) { |
| 1552 | final ObbState obbState; |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1553 | |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1554 | synchronized (mObbMounts) { |
| 1555 | if (!isObbMounted(filename)) { |
| 1556 | throw new IllegalArgumentException("OBB is not mounted"); |
| 1557 | } |
| 1558 | obbState = mObbPathToStateMap.get(filename); |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1561 | UnmountObbAction action = new UnmountObbAction(obbState, force); |
| 1562 | mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action)); |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1563 | |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1564 | if (DEBUG_OBB) |
| 1565 | Slog.i(TAG, "Send to OBB handler: " + action.toString()); |
| 1566 | } |
| 1567 | |
| 1568 | private void addObbState(ObbState obbState) { |
| 1569 | synchronized (mObbMounts) { |
| 1570 | mObbMounts.put(obbState.token, obbState); |
| 1571 | mObbPathToStateMap.put(obbState.filename, obbState); |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | private void removeObbState(ObbState obbState) { |
| 1576 | synchronized (mObbMounts) { |
| 1577 | mObbMounts.remove(obbState.token); |
| 1578 | mObbPathToStateMap.remove(obbState.filename); |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | private class ObbActionHandler extends Handler { |
| 1583 | private boolean mBound = false; |
| 1584 | private List<ObbAction> mActions = new LinkedList<ObbAction>(); |
| 1585 | |
| 1586 | ObbActionHandler(Looper l) { |
| 1587 | super(l); |
| 1588 | } |
| 1589 | |
| 1590 | @Override |
| 1591 | public void handleMessage(Message msg) { |
| 1592 | switch (msg.what) { |
| 1593 | case OBB_RUN_ACTION: { |
| 1594 | ObbAction action = (ObbAction) msg.obj; |
| 1595 | |
| 1596 | if (DEBUG_OBB) |
| 1597 | Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString()); |
| 1598 | |
| 1599 | // If a bind was already initiated we don't really |
| 1600 | // need to do anything. The pending install |
| 1601 | // will be processed later on. |
| 1602 | if (!mBound) { |
| 1603 | // If this is the only one pending we might |
| 1604 | // have to bind to the service again. |
| 1605 | if (!connectToService()) { |
| 1606 | Slog.e(TAG, "Failed to bind to media container service"); |
| 1607 | action.handleError(); |
| 1608 | return; |
| 1609 | } else { |
| 1610 | // Once we bind to the service, the first |
| 1611 | // pending request will be processed. |
| 1612 | mActions.add(action); |
| 1613 | } |
| 1614 | } else { |
| 1615 | // Already bound to the service. Just make |
| 1616 | // sure we trigger off processing the first request. |
| 1617 | if (mActions.size() == 0) { |
| 1618 | mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND); |
| 1619 | } |
| 1620 | |
| 1621 | mActions.add(action); |
| 1622 | } |
| 1623 | break; |
| 1624 | } |
| 1625 | case OBB_MCS_BOUND: { |
| 1626 | if (DEBUG_OBB) |
| 1627 | Slog.i(TAG, "OBB_MCS_BOUND"); |
| 1628 | if (msg.obj != null) { |
| 1629 | mContainerService = (IMediaContainerService) msg.obj; |
| 1630 | } |
| 1631 | if (mContainerService == null) { |
| 1632 | // Something seriously wrong. Bail out |
| 1633 | Slog.e(TAG, "Cannot bind to media container service"); |
| 1634 | for (ObbAction action : mActions) { |
| 1635 | // Indicate service bind error |
| 1636 | action.handleError(); |
| 1637 | } |
| 1638 | mActions.clear(); |
| 1639 | } else if (mActions.size() > 0) { |
| 1640 | ObbAction action = mActions.get(0); |
| 1641 | if (action != null) { |
| 1642 | action.execute(this); |
| 1643 | } |
| 1644 | } else { |
| 1645 | // Should never happen ideally. |
| 1646 | Slog.w(TAG, "Empty queue"); |
| 1647 | } |
| 1648 | break; |
| 1649 | } |
| 1650 | case OBB_MCS_RECONNECT: { |
| 1651 | if (DEBUG_OBB) |
| 1652 | Slog.i(TAG, "OBB_MCS_RECONNECT"); |
| 1653 | if (mActions.size() > 0) { |
| 1654 | if (mBound) { |
| 1655 | disconnectService(); |
| 1656 | } |
| 1657 | if (!connectToService()) { |
| 1658 | Slog.e(TAG, "Failed to bind to media container service"); |
| 1659 | for (ObbAction action : mActions) { |
| 1660 | // Indicate service bind error |
| 1661 | action.handleError(); |
| 1662 | } |
| 1663 | mActions.clear(); |
| 1664 | } |
| 1665 | } |
| 1666 | break; |
| 1667 | } |
| 1668 | case OBB_MCS_UNBIND: { |
| 1669 | if (DEBUG_OBB) |
| 1670 | Slog.i(TAG, "OBB_MCS_UNBIND"); |
| 1671 | |
| 1672 | // Delete pending install |
| 1673 | if (mActions.size() > 0) { |
| 1674 | mActions.remove(0); |
| 1675 | } |
| 1676 | if (mActions.size() == 0) { |
| 1677 | if (mBound) { |
| 1678 | disconnectService(); |
| 1679 | } |
| 1680 | } else { |
| 1681 | // There are more pending requests in queue. |
| 1682 | // Just post MCS_BOUND message to trigger processing |
| 1683 | // of next pending install. |
| 1684 | mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND); |
| 1685 | } |
| 1686 | break; |
| 1687 | } |
| 1688 | case OBB_MCS_GIVE_UP: { |
| 1689 | if (DEBUG_OBB) |
| 1690 | Slog.i(TAG, "OBB_MCS_GIVE_UP"); |
| 1691 | mActions.remove(0); |
| 1692 | break; |
| 1693 | } |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | private boolean connectToService() { |
| 1698 | if (DEBUG_OBB) |
| 1699 | Slog.i(TAG, "Trying to bind to DefaultContainerService"); |
| 1700 | |
| 1701 | Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT); |
| 1702 | if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) { |
| 1703 | mBound = true; |
| 1704 | return true; |
| 1705 | } |
| 1706 | return false; |
| 1707 | } |
| 1708 | |
| 1709 | private void disconnectService() { |
| 1710 | mContainerService = null; |
| 1711 | mBound = false; |
| 1712 | mContext.unbindService(mDefContainerConn); |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | abstract class ObbAction { |
| 1717 | private static final int MAX_RETRIES = 3; |
| 1718 | private int mRetries; |
| 1719 | |
| 1720 | ObbState mObbState; |
| 1721 | |
| 1722 | ObbAction(ObbState obbState) { |
| 1723 | mObbState = obbState; |
| 1724 | } |
| 1725 | |
| 1726 | public void execute(ObbActionHandler handler) { |
| 1727 | try { |
| 1728 | if (DEBUG_OBB) |
| 1729 | Slog.i(TAG, "Starting to execute action: " + this.toString()); |
| 1730 | mRetries++; |
| 1731 | if (mRetries > MAX_RETRIES) { |
| 1732 | Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up"); |
| 1733 | mObbActionHandler.sendEmptyMessage(OBB_MCS_GIVE_UP); |
| 1734 | handleError(); |
| 1735 | return; |
| 1736 | } else { |
| 1737 | handleExecute(); |
| 1738 | if (DEBUG_OBB) |
| 1739 | Slog.i(TAG, "Posting install MCS_UNBIND"); |
| 1740 | mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND); |
| 1741 | } |
| 1742 | } catch (RemoteException e) { |
| 1743 | if (DEBUG_OBB) |
| 1744 | Slog.i(TAG, "Posting install MCS_RECONNECT"); |
| 1745 | mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT); |
| 1746 | } catch (Exception e) { |
| 1747 | if (DEBUG_OBB) |
| 1748 | Slog.d(TAG, "Error handling OBB action", e); |
| 1749 | handleError(); |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | abstract void handleExecute() throws RemoteException; |
| 1754 | abstract void handleError(); |
| 1755 | } |
| 1756 | |
| 1757 | class MountObbAction extends ObbAction { |
| 1758 | private String mKey; |
| 1759 | |
| 1760 | MountObbAction(ObbState obbState, String key) { |
| 1761 | super(obbState); |
| 1762 | mKey = key; |
| 1763 | } |
| 1764 | |
| 1765 | public void handleExecute() throws RemoteException { |
| 1766 | ObbInfo obbInfo = mContainerService.getObbInfo(mObbState.filename); |
| 1767 | if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) { |
| 1768 | throw new IllegalArgumentException("Caller package does not match OBB file"); |
| 1769 | } |
| 1770 | |
| 1771 | if (mKey == null) { |
| 1772 | mKey = "none"; |
| 1773 | } |
| 1774 | |
| 1775 | int rc = StorageResultCode.OperationSucceeded; |
| 1776 | String cmd = String.format("obb mount %s %s %d", mObbState.filename, mKey, |
| 1777 | mObbState.callerUid); |
| 1778 | try { |
| 1779 | mConnector.doCommand(cmd); |
| 1780 | } catch (NativeDaemonConnectorException e) { |
| 1781 | int code = e.getCode(); |
| 1782 | if (code != VoldResponseCode.OpFailedStorageBusy) { |
| 1783 | rc = StorageResultCode.OperationFailedInternalError; |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1788 | try { |
| 1789 | mObbState.token.onObbResult(mObbState.filename, "mounted"); |
| 1790 | } catch (RemoteException e) { |
| 1791 | Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged"); |
| 1792 | } |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1793 | } else { |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1794 | Slog.e(TAG, "Couldn't mount OBB file"); |
| 1795 | |
| 1796 | // We didn't succeed, so remove this from the mount-set. |
| 1797 | removeObbState(mObbState); |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1798 | } |
| 1799 | } |
| 1800 | |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1801 | public void handleError() { |
| 1802 | removeObbState(mObbState); |
| 1803 | |
| 1804 | try { |
| 1805 | mObbState.token.onObbResult(mObbState.filename, "error"); |
| 1806 | } catch (RemoteException e) { |
| 1807 | Slog.e(TAG, "Couldn't send back OBB mount error for " + mObbState.filename); |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1808 | } |
| 1809 | } |
| Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 1810 | |
| 1811 | @Override |
| 1812 | public String toString() { |
| 1813 | StringBuilder sb = new StringBuilder(); |
| 1814 | sb.append("MountObbAction{"); |
| 1815 | sb.append("filename="); |
| 1816 | sb.append(mObbState.filename); |
| 1817 | sb.append(",callerUid="); |
| 1818 | sb.append(mObbState.callerUid); |
| 1819 | sb.append(",token="); |
| 1820 | sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL"); |
| 1821 | sb.append('}'); |
| 1822 | return sb.toString(); |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | class UnmountObbAction extends ObbAction { |
| 1827 | private boolean mForceUnmount; |
| 1828 | |
| 1829 | UnmountObbAction(ObbState obbState, boolean force) { |
| 1830 | super(obbState); |
| 1831 | mForceUnmount = force; |
| 1832 | } |
| 1833 | |
| 1834 | public void handleExecute() throws RemoteException { |
| 1835 | ObbInfo obbInfo = mContainerService.getObbInfo(mObbState.filename); |
| 1836 | |
| 1837 | if (!isCallerOwnerOfPackageOrSystem(obbInfo.packageName)) { |
| 1838 | throw new IllegalArgumentException("Caller package does not match OBB file"); |
| 1839 | } |
| 1840 | |
| 1841 | int rc = StorageResultCode.OperationSucceeded; |
| 1842 | String cmd = String.format("obb unmount %s%s", mObbState.filename, |
| 1843 | (mForceUnmount ? " force" : "")); |
| 1844 | try { |
| 1845 | mConnector.doCommand(cmd); |
| 1846 | } catch (NativeDaemonConnectorException e) { |
| 1847 | int code = e.getCode(); |
| 1848 | if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 1849 | rc = StorageResultCode.OperationFailedStorageBusy; |
| 1850 | } else { |
| 1851 | rc = StorageResultCode.OperationFailedInternalError; |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1856 | removeObbState(mObbState); |
| 1857 | |
| 1858 | try { |
| 1859 | mObbState.token.onObbResult(mObbState.filename, "unmounted"); |
| 1860 | } catch (RemoteException e) { |
| 1861 | Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged"); |
| 1862 | } |
| 1863 | } else { |
| 1864 | try { |
| 1865 | mObbState.token.onObbResult(mObbState.filename, "error"); |
| 1866 | } catch (RemoteException e) { |
| 1867 | Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged"); |
| 1868 | } |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | public void handleError() { |
| 1873 | removeObbState(mObbState); |
| 1874 | |
| 1875 | try { |
| 1876 | mObbState.token.onObbResult(mObbState.filename, "error"); |
| 1877 | } catch (RemoteException e) { |
| 1878 | Slog.e(TAG, "Couldn't send back OBB unmount error for " + mObbState.filename); |
| 1879 | } |
| 1880 | } |
| 1881 | |
| 1882 | @Override |
| 1883 | public String toString() { |
| 1884 | StringBuilder sb = new StringBuilder(); |
| 1885 | sb.append("UnmountObbAction{"); |
| 1886 | sb.append("filename="); |
| 1887 | sb.append(mObbState.filename != null ? mObbState.filename : "null"); |
| 1888 | sb.append(",force="); |
| 1889 | sb.append(mForceUnmount); |
| 1890 | sb.append(",callerUid="); |
| 1891 | sb.append(mObbState.callerUid); |
| 1892 | sb.append(",token="); |
| 1893 | sb.append(mObbState.token != null ? mObbState.token.toString() : "null"); |
| 1894 | sb.append('}'); |
| 1895 | return sb.toString(); |
| 1896 | } |
| Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1897 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1898 | } |
| 1899 | |