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