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