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