| 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 | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame^] | 645 | try { |
| 646 | ArrayList<String> rsp = mConnector.doCommand("share status " + method); |
| 647 | } catch (NativeDaemonConnectorException ex) { |
| 648 | Slog.e(TAG, "Failed to determine whether share method " + method + " is available."); |
| 649 | return false; |
| 650 | } |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 651 | |
| 652 | for (String line : rsp) { |
| Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame^] | 653 | String[] tok = line.split(" "); |
| 654 | if (tok.length < 3) { |
| 655 | Slog.e(TAG, "Malformed response to share status " + method); |
| 656 | return false; |
| 657 | } |
| 658 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 659 | int code; |
| 660 | try { |
| 661 | code = Integer.parseInt(tok[0]); |
| 662 | } catch (NumberFormatException nfe) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 663 | Slog.e(TAG, String.format("Error parsing code %s", tok[0])); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 664 | return false; |
| 665 | } |
| 666 | if (code == VoldResponseCode.ShareStatusResult) { |
| 667 | if (tok[2].equals("available")) |
| 668 | return true; |
| 669 | return false; |
| 670 | } else { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 671 | Slog.e(TAG, String.format("Unexpected response code %d", code)); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 672 | return false; |
| 673 | } |
| 674 | } |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 675 | Slog.e(TAG, "Got an empty response"); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 676 | return false; |
| 677 | } |
| 678 | |
| 679 | private int doMountVolume(String path) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 680 | int rc = StorageResultCode.OperationSucceeded; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 681 | |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 682 | if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 683 | try { |
| 684 | mConnector.doCommand(String.format("volume mount %s", path)); |
| 685 | } catch (NativeDaemonConnectorException e) { |
| 686 | /* |
| 687 | * Mount failed for some reason |
| 688 | */ |
| 689 | Intent in = null; |
| 690 | int code = e.getCode(); |
| 691 | if (code == VoldResponseCode.OpFailedNoMedia) { |
| 692 | /* |
| 693 | * Attempt to mount but no media inserted |
| 694 | */ |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 695 | rc = StorageResultCode.OperationFailedNoMedia; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 696 | } else if (code == VoldResponseCode.OpFailedMediaBlank) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 697 | if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs"); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 698 | /* |
| 699 | * Media is blank or does not contain a supported filesystem |
| 700 | */ |
| 701 | updatePublicVolumeState(path, Environment.MEDIA_NOFS); |
| 702 | in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path)); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 703 | rc = StorageResultCode.OperationFailedMediaBlank; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 704 | } else if (code == VoldResponseCode.OpFailedMediaCorrupt) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 705 | if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt"); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 706 | /* |
| 707 | * Volume consistency check failed |
| 708 | */ |
| 709 | updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE); |
| 710 | in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path)); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 711 | rc = StorageResultCode.OperationFailedMediaCorrupt; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 712 | } else { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 713 | rc = StorageResultCode.OperationFailedInternalError; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Send broadcast intent (if required for the failure) |
| 718 | */ |
| 719 | if (in != null) { |
| 720 | mContext.sendBroadcast(in); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | return rc; |
| 725 | } |
| 726 | |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 727 | /* |
| 728 | * If force is not set, we do not unmount if there are |
| 729 | * processes holding references to the volume about to be unmounted. |
| 730 | * If force is set, all the processes holding references need to be |
| 731 | * killed via the ActivityManager before actually unmounting the volume. |
| 732 | * This might even take a while and might be retried after timed delays |
| 733 | * to make sure we dont end up in an instable state and kill some core |
| 734 | * processes. |
| 735 | */ |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 736 | private int doUnmountVolume(String path, boolean force) { |
| San Mehat | 59443a6 | 2010-02-09 13:28:45 -0800 | [diff] [blame] | 737 | if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) { |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 738 | return VoldResponseCode.OpFailedVolNotMounted; |
| 739 | } |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 740 | // Redundant probably. But no harm in updating state again. |
| Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 741 | mPms.updateExternalMediaStatus(false, false); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 742 | try { |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 743 | mConnector.doCommand(String.format( |
| 744 | "volume unmount %s%s", path, (force ? " force" : ""))); |
| Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 745 | // We unmounted the volume. None of the asec containers are available now. |
| 746 | synchronized (mAsecMountSet) { |
| 747 | mAsecMountSet.clear(); |
| 748 | } |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 749 | return StorageResultCode.OperationSucceeded; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 750 | } catch (NativeDaemonConnectorException e) { |
| 751 | // Don't worry about mismatch in PackageManager since the |
| 752 | // call back will handle the status changes any way. |
| 753 | int code = e.getCode(); |
| 754 | if (code == VoldResponseCode.OpFailedVolNotMounted) { |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 755 | return StorageResultCode.OperationFailedStorageNotMounted; |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 756 | } else if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 757 | return StorageResultCode.OperationFailedStorageBusy; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 758 | } else { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 759 | return StorageResultCode.OperationFailedInternalError; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | private int doFormatVolume(String path) { |
| 765 | try { |
| 766 | String cmd = String.format("volume format %s", path); |
| 767 | mConnector.doCommand(cmd); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 768 | return StorageResultCode.OperationSucceeded; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 769 | } catch (NativeDaemonConnectorException e) { |
| 770 | int code = e.getCode(); |
| 771 | if (code == VoldResponseCode.OpFailedNoMedia) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 772 | return StorageResultCode.OperationFailedNoMedia; |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 773 | } else if (code == VoldResponseCode.OpFailedMediaCorrupt) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 774 | return StorageResultCode.OperationFailedMediaCorrupt; |
| 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 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 781 | private boolean doGetVolumeShared(String path, String method) { |
| 782 | String cmd = String.format("volume shared %s %s", path, method); |
| Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame^] | 783 | ArrayList<String> rsp; |
| 784 | |
| 785 | try { |
| 786 | rsp = mConnector.doCommand(cmd); |
| 787 | } catch (NativeDaemonConnectorException ex) { |
| 788 | Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method); |
| 789 | return false; |
| 790 | } |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 791 | |
| 792 | for (String line : rsp) { |
| Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame^] | 793 | String[] tok = line.split(" "); |
| 794 | if (tok.length < 3) { |
| 795 | Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command"); |
| 796 | return false; |
| 797 | } |
| 798 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 799 | int code; |
| 800 | try { |
| 801 | code = Integer.parseInt(tok[0]); |
| 802 | } catch (NumberFormatException nfe) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 803 | Slog.e(TAG, String.format("Error parsing code %s", tok[0])); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 804 | return false; |
| 805 | } |
| 806 | if (code == VoldResponseCode.ShareEnabledResult) { |
| Kenny Root | a80ce06 | 2010-06-01 13:23:53 -0700 | [diff] [blame^] | 807 | return "enabled".equals(tok[2]); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 808 | } else { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 809 | Slog.e(TAG, String.format("Unexpected response code %d", code)); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 810 | return false; |
| 811 | } |
| 812 | } |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 813 | Slog.e(TAG, "Got an empty response"); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 814 | return false; |
| 815 | } |
| 816 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 817 | private void notifyShareAvailabilityChange(String method, final boolean avail) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 818 | if (!method.equals("ums")) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 819 | Slog.w(TAG, "Ignoring unsupported share method {" + method + "}"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 820 | return; |
| 821 | } |
| 822 | |
| 823 | synchronized (mListeners) { |
| 824 | for (int i = mListeners.size() -1; i >= 0; i--) { |
| 825 | MountServiceBinderListener bl = mListeners.get(i); |
| 826 | try { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 827 | bl.mListener.onUsbMassStorageConnectionChanged(avail); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 828 | } catch (RemoteException rex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 829 | Slog.e(TAG, "Listener dead"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 830 | mListeners.remove(i); |
| 831 | } catch (Exception ex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 832 | Slog.e(TAG, "Listener failed", ex); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 837 | if (mBooted == true) { |
| San Mehat | 6a965af2 | 2010-02-24 17:47:30 -0800 | [diff] [blame] | 838 | sendUmsIntent(avail); |
| 839 | } else { |
| 840 | mSendUmsConnectedOnBoot = avail; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 841 | } |
| San Mehat | 2fe718a | 2010-03-11 12:01:49 -0800 | [diff] [blame] | 842 | |
| 843 | final String path = Environment.getExternalStorageDirectory().getPath(); |
| 844 | if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) { |
| 845 | /* |
| 846 | * USB mass storage disconnected while enabled |
| 847 | */ |
| 848 | new Thread() { |
| 849 | public void run() { |
| 850 | try { |
| 851 | int rc; |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 852 | Slog.w(TAG, "Disabling UMS after cable disconnect"); |
| San Mehat | 2fe718a | 2010-03-11 12:01:49 -0800 | [diff] [blame] | 853 | doShareUnshareVolume(path, "ums", false); |
| 854 | if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 855 | Slog.e(TAG, String.format( |
| San Mehat | 2fe718a | 2010-03-11 12:01:49 -0800 | [diff] [blame] | 856 | "Failed to remount {%s} on UMS enabled-disconnect (%d)", |
| 857 | path, rc)); |
| 858 | } |
| 859 | } catch (Exception ex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 860 | Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex); |
| San Mehat | 2fe718a | 2010-03-11 12:01:49 -0800 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | }.start(); |
| 864 | } |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 865 | } |
| 866 | |
| San Mehat | 6a965af2 | 2010-02-24 17:47:30 -0800 | [diff] [blame] | 867 | private void sendUmsIntent(boolean c) { |
| 868 | mContext.sendBroadcast( |
| 869 | new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED))); |
| 870 | } |
| 871 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 872 | private void validatePermission(String perm) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 873 | if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) { |
| 874 | throw new SecurityException(String.format("Requires %s permission", perm)); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | /** |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 879 | * Constructs a new MountService instance |
| 880 | * |
| 881 | * @param context Binder context for this service |
| 882 | */ |
| 883 | public MountService(Context context) { |
| 884 | mContext = context; |
| 885 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 886 | // XXX: This will go away soon in favor of IMountServiceObserver |
| 887 | mPms = (PackageManagerService) ServiceManager.getService("package"); |
| 888 | |
| 889 | mContext.registerReceiver(mBroadcastReceiver, |
| 890 | new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null); |
| 891 | |
| Daniel Sandler | 5f27ef4 | 2010-03-16 15:42:02 -0400 | [diff] [blame] | 892 | mHandlerThread = new HandlerThread("MountService"); |
| 893 | mHandlerThread.start(); |
| 894 | mHandler = new MountServiceHandler(mHandlerThread.getLooper()); |
| 895 | |
| Marco Nelissen | c34ebce | 2010-02-18 13:39:41 -0800 | [diff] [blame] | 896 | /* |
| 897 | * Vold does not run in the simulator, so pretend the connector thread |
| 898 | * ran and did its thing. |
| 899 | */ |
| 900 | if ("simulator".equals(SystemProperties.get("ro.product.device"))) { |
| 901 | mReady = true; |
| 902 | mUmsEnabling = true; |
| 903 | return; |
| 904 | } |
| 905 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 906 | mConnector = new NativeDaemonConnector(this, "vold", 10, "VoldConnector"); |
| 907 | mReady = false; |
| 908 | Thread thread = new Thread(mConnector, NativeDaemonConnector.class.getName()); |
| 909 | thread.start(); |
| 910 | } |
| 911 | |
| 912 | /** |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 913 | * Exposed API calls below here |
| 914 | */ |
| 915 | |
| 916 | public void registerListener(IMountServiceListener listener) { |
| 917 | synchronized (mListeners) { |
| 918 | MountServiceBinderListener bl = new MountServiceBinderListener(listener); |
| 919 | try { |
| 920 | listener.asBinder().linkToDeath(bl, 0); |
| 921 | mListeners.add(bl); |
| 922 | } catch (RemoteException rex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 923 | Slog.e(TAG, "Failed to link to listener death"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | public void unregisterListener(IMountServiceListener listener) { |
| 929 | synchronized (mListeners) { |
| 930 | for(MountServiceBinderListener bl : mListeners) { |
| 931 | if (bl.mListener == listener) { |
| 932 | mListeners.remove(mListeners.indexOf(bl)); |
| 933 | return; |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 939 | public void shutdown(final IMountShutdownObserver observer) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 940 | validatePermission(android.Manifest.permission.SHUTDOWN); |
| 941 | |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 942 | Slog.i(TAG, "Shutting down"); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 943 | |
| 944 | String path = Environment.getExternalStorageDirectory().getPath(); |
| 945 | String state = getVolumeState(path); |
| San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 946 | |
| 947 | if (state.equals(Environment.MEDIA_SHARED)) { |
| 948 | /* |
| 949 | * If the media is currently shared, unshare it. |
| 950 | * XXX: This is still dangerous!. We should not |
| 951 | * be rebooting at *all* if UMS is enabled, since |
| 952 | * the UMS host could have dirty FAT cache entries |
| 953 | * yet to flush. |
| 954 | */ |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 955 | setUsbMassStorageEnabled(false); |
| San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 956 | } else if (state.equals(Environment.MEDIA_CHECKING)) { |
| 957 | /* |
| 958 | * If the media is being checked, then we need to wait for |
| 959 | * it to complete before being able to proceed. |
| 960 | */ |
| 961 | // XXX: @hackbod - Should we disable the ANR timer here? |
| 962 | int retries = 30; |
| 963 | while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) { |
| 964 | try { |
| 965 | Thread.sleep(1000); |
| 966 | } catch (InterruptedException iex) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 967 | Slog.e(TAG, "Interrupted while waiting for media", iex); |
| San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 968 | break; |
| 969 | } |
| 970 | state = Environment.getExternalStorageState(); |
| 971 | } |
| 972 | if (retries == 0) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 973 | Slog.e(TAG, "Timed out waiting for media to check"); |
| San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | |
| 977 | if (state.equals(Environment.MEDIA_MOUNTED)) { |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 978 | // Post a unmount message. |
| 979 | ShutdownCallBack ucb = new ShutdownCallBack(path, observer); |
| 980 | mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb)); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 984 | private boolean getUmsEnabling() { |
| 985 | synchronized (mListeners) { |
| 986 | return mUmsEnabling; |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | private void setUmsEnabling(boolean enable) { |
| 991 | synchronized (mListeners) { |
| 992 | mUmsEnabling = true; |
| 993 | } |
| 994 | } |
| 995 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 996 | public boolean isUsbMassStorageConnected() { |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 997 | waitForReady(); |
| San Mehat | 91c7761 | 2010-01-07 10:39:41 -0800 | [diff] [blame] | 998 | |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 999 | if (getUmsEnabling()) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1000 | return true; |
| San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 1001 | } |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1002 | return doGetShareMethodAvailable("ums"); |
| 1003 | } |
| 1004 | |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1005 | public void setUsbMassStorageEnabled(boolean enable) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1006 | waitForReady(); |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1007 | validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1008 | |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1009 | // TODO: Add support for multiple share methods |
| 1010 | |
| 1011 | /* |
| 1012 | * If the volume is mounted and we're enabling then unmount it |
| 1013 | */ |
| 1014 | String path = Environment.getExternalStorageDirectory().getPath(); |
| 1015 | String vs = getVolumeState(path); |
| 1016 | String method = "ums"; |
| 1017 | if (enable && vs.equals(Environment.MEDIA_MOUNTED)) { |
| 1018 | // Override for isUsbMassStorageEnabled() |
| 1019 | setUmsEnabling(enable); |
| 1020 | UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true); |
| 1021 | mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb)); |
| 1022 | // Clear override |
| 1023 | setUmsEnabling(false); |
| 1024 | } |
| 1025 | /* |
| 1026 | * If we disabled UMS then mount the volume |
| 1027 | */ |
| 1028 | if (!enable) { |
| 1029 | doShareUnshareVolume(path, method, enable); |
| 1030 | if (doMountVolume(path) != StorageResultCode.OperationSucceeded) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1031 | Slog.e(TAG, "Failed to remount " + path + |
| Suchi Amalapurapu | 0eec21d | 2010-02-25 17:07:14 -0800 | [diff] [blame] | 1032 | " after disabling share method " + method); |
| 1033 | /* |
| 1034 | * Even though the mount failed, the unshare didn't so don't indicate an error. |
| 1035 | * The mountVolume() call will have set the storage state and sent the necessary |
| 1036 | * broadcasts. |
| 1037 | */ |
| 1038 | } |
| 1039 | } |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | public boolean isUsbMassStorageEnabled() { |
| 1043 | waitForReady(); |
| 1044 | return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | } |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1046 | |
| San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 1047 | /** |
| 1048 | * @return state of the volume at the specified mount point |
| 1049 | */ |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1050 | public String getVolumeState(String mountPoint) { |
| San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 1051 | /* |
| 1052 | * XXX: Until we have multiple volume discovery, just hardwire |
| 1053 | * this to /sdcard |
| 1054 | */ |
| 1055 | if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1056 | Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume"); |
| San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 1057 | throw new IllegalArgumentException(); |
| 1058 | } |
| 1059 | |
| 1060 | return mLegacyState; |
| 1061 | } |
| 1062 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1063 | public int mountVolume(String path) { |
| 1064 | validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1065 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1066 | waitForReady(); |
| 1067 | return doMountVolume(path); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 1070 | public void unmountVolume(String path, boolean force) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1071 | validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1072 | waitForReady(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | |
| Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1074 | String volState = getVolumeState(path); |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1075 | if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force); |
| Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 1076 | if (Environment.MEDIA_UNMOUNTED.equals(volState) || |
| 1077 | Environment.MEDIA_REMOVED.equals(volState) || |
| 1078 | Environment.MEDIA_SHARED.equals(volState) || |
| 1079 | Environment.MEDIA_UNMOUNTABLE.equals(volState)) { |
| 1080 | // Media already unmounted or cannot be unmounted. |
| 1081 | // TODO return valid return code when adding observer call back. |
| 1082 | return; |
| 1083 | } |
| Suchi Amalapurapu | c42e29e | 2010-02-22 16:03:53 -0800 | [diff] [blame] | 1084 | UnmountCallBack ucb = new UnmountCallBack(path, force); |
| 1085 | mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1086 | } |
| 1087 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1088 | public int formatVolume(String path) { |
| 1089 | validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1090 | waitForReady(); |
| San Mehat | 5b77dab | 2010-01-26 13:28:50 -0800 | [diff] [blame] | 1091 | |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1092 | return doFormatVolume(path); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
| San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1095 | public int []getStorageUsers(String path) { |
| 1096 | validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS); |
| 1097 | waitForReady(); |
| 1098 | try { |
| 1099 | String[] r = mConnector.doListCommand( |
| 1100 | String.format("storage users %s", path), |
| 1101 | VoldResponseCode.StorageUsersListResult); |
| 1102 | // FMT: <pid> <process name> |
| 1103 | int[] data = new int[r.length]; |
| 1104 | for (int i = 0; i < r.length; i++) { |
| 1105 | String []tok = r[i].split(" "); |
| 1106 | try { |
| 1107 | data[i] = Integer.parseInt(tok[0]); |
| 1108 | } catch (NumberFormatException nfe) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1109 | Slog.e(TAG, String.format("Error parsing pid %s", tok[0])); |
| San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1110 | return new int[0]; |
| 1111 | } |
| 1112 | } |
| 1113 | return data; |
| 1114 | } catch (NativeDaemonConnectorException e) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1115 | Slog.e(TAG, "Failed to retrieve storage users list", e); |
| San Mehat | c1b4ce9 | 2010-02-16 17:13:03 -0800 | [diff] [blame] | 1116 | return new int[0]; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1120 | private void warnOnNotMounted() { |
| 1121 | if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
| San Mehat | a507859 | 2010-03-25 09:36:54 -0700 | [diff] [blame] | 1122 | Slog.w(TAG, "getSecureContainerList() called when storage not mounted"); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1126 | public String[] getSecureContainerList() { |
| 1127 | validatePermission(android.Manifest.permission.ASEC_ACCESS); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1128 | waitForReady(); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1129 | warnOnNotMounted(); |
| San Mehat | f919cd02 | 2010-02-04 15:10:38 -0800 | [diff] [blame] | 1130 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1131 | try { |
| 1132 | return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult); |
| 1133 | } catch (NativeDaemonConnectorException e) { |
| 1134 | return new String[0]; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1135 | } |
| 1136 | } |
| San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1137 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1138 | public int createSecureContainer(String id, int sizeMb, String fstype, |
| 1139 | String key, int ownerUid) { |
| 1140 | validatePermission(android.Manifest.permission.ASEC_CREATE); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1141 | waitForReady(); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1142 | warnOnNotMounted(); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1143 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1144 | int rc = StorageResultCode.OperationSucceeded; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1145 | String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid); |
| 1146 | try { |
| 1147 | mConnector.doCommand(cmd); |
| 1148 | } catch (NativeDaemonConnectorException e) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1149 | rc = StorageResultCode.OperationFailedInternalError; |
| San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1150 | } |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1151 | |
| 1152 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1153 | synchronized (mAsecMountSet) { |
| 1154 | mAsecMountSet.add(id); |
| 1155 | } |
| 1156 | } |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1157 | return rc; |
| San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1158 | } |
| 1159 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1160 | public int finalizeSecureContainer(String id) { |
| 1161 | validatePermission(android.Manifest.permission.ASEC_CREATE); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1162 | warnOnNotMounted(); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1163 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1164 | int rc = StorageResultCode.OperationSucceeded; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1165 | try { |
| 1166 | mConnector.doCommand(String.format("asec finalize %s", id)); |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1167 | /* |
| 1168 | * Finalization does a remount, so no need |
| 1169 | * to update mAsecMountSet |
| 1170 | */ |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1171 | } catch (NativeDaemonConnectorException e) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1172 | rc = StorageResultCode.OperationFailedInternalError; |
| San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1173 | } |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1174 | return rc; |
| San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1177 | public int destroySecureContainer(String id, boolean force) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1178 | validatePermission(android.Manifest.permission.ASEC_DESTROY); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1179 | waitForReady(); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1180 | warnOnNotMounted(); |
| San Mehat | f919cd02 | 2010-02-04 15:10:38 -0800 | [diff] [blame] | 1181 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1182 | int rc = StorageResultCode.OperationSucceeded; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1183 | try { |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1184 | mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : ""))); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1185 | } catch (NativeDaemonConnectorException e) { |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1186 | int code = e.getCode(); |
| 1187 | if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 1188 | rc = StorageResultCode.OperationFailedStorageBusy; |
| 1189 | } else { |
| 1190 | rc = StorageResultCode.OperationFailedInternalError; |
| 1191 | } |
| San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1192 | } |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1193 | |
| 1194 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1195 | synchronized (mAsecMountSet) { |
| 1196 | if (mAsecMountSet.contains(id)) { |
| 1197 | mAsecMountSet.remove(id); |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1202 | return rc; |
| San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1203 | } |
| 1204 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1205 | public int mountSecureContainer(String id, String key, int ownerUid) { |
| 1206 | validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1207 | waitForReady(); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1208 | warnOnNotMounted(); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1209 | |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1210 | synchronized (mAsecMountSet) { |
| 1211 | if (mAsecMountSet.contains(id)) { |
| 1212 | return StorageResultCode.OperationFailedStorageMounted; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1216 | int rc = StorageResultCode.OperationSucceeded; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1217 | String cmd = String.format("asec mount %s %s %d", id, key, ownerUid); |
| 1218 | try { |
| 1219 | mConnector.doCommand(cmd); |
| 1220 | } catch (NativeDaemonConnectorException e) { |
| Kenny Root | f030462 | 2010-03-19 19:20:42 -0700 | [diff] [blame] | 1221 | int code = e.getCode(); |
| 1222 | if (code != VoldResponseCode.OpFailedStorageBusy) { |
| 1223 | rc = StorageResultCode.OperationFailedInternalError; |
| 1224 | } |
| San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1225 | } |
| San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1226 | |
| 1227 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1228 | synchronized (mAsecMountSet) { |
| 1229 | mAsecMountSet.add(id); |
| 1230 | } |
| 1231 | } |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1232 | return rc; |
| San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1235 | public int unmountSecureContainer(String id, boolean force) { |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1236 | validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1237 | waitForReady(); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1238 | warnOnNotMounted(); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1239 | |
| San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1240 | synchronized (mAsecMountSet) { |
| 1241 | if (!mAsecMountSet.contains(id)) { |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1242 | return StorageResultCode.OperationFailedStorageNotMounted; |
| San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1246 | int rc = StorageResultCode.OperationSucceeded; |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1247 | String cmd = String.format("asec unmount %s%s", id, (force ? " force" : "")); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1248 | try { |
| 1249 | mConnector.doCommand(cmd); |
| 1250 | } catch (NativeDaemonConnectorException e) { |
| San Mehat | d970998 | 2010-02-18 11:43:03 -0800 | [diff] [blame] | 1251 | int code = e.getCode(); |
| 1252 | if (code == VoldResponseCode.OpFailedStorageBusy) { |
| 1253 | rc = StorageResultCode.OperationFailedStorageBusy; |
| 1254 | } else { |
| 1255 | rc = StorageResultCode.OperationFailedInternalError; |
| 1256 | } |
| San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1257 | } |
| San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1258 | |
| 1259 | if (rc == StorageResultCode.OperationSucceeded) { |
| 1260 | synchronized (mAsecMountSet) { |
| 1261 | mAsecMountSet.remove(id); |
| 1262 | } |
| 1263 | } |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1264 | return rc; |
| San Mehat | 9dba709 | 2010-01-18 06:47:41 -0800 | [diff] [blame] | 1265 | } |
| 1266 | |
| San Mehat | 6cdd9c0 | 2010-02-09 14:45:20 -0800 | [diff] [blame] | 1267 | public boolean isSecureContainerMounted(String id) { |
| 1268 | validatePermission(android.Manifest.permission.ASEC_ACCESS); |
| 1269 | waitForReady(); |
| 1270 | warnOnNotMounted(); |
| 1271 | |
| 1272 | synchronized (mAsecMountSet) { |
| 1273 | return mAsecMountSet.contains(id); |
| 1274 | } |
| 1275 | } |
| 1276 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1277 | public int renameSecureContainer(String oldId, String newId) { |
| 1278 | validatePermission(android.Manifest.permission.ASEC_RENAME); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1279 | waitForReady(); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1280 | warnOnNotMounted(); |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1281 | |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1282 | synchronized (mAsecMountSet) { |
| San Mehat | 85451ee | 2010-02-24 08:54:18 -0800 | [diff] [blame] | 1283 | /* |
| 1284 | * Because a mounted container has active internal state which cannot be |
| 1285 | * changed while active, we must ensure both ids are not currently mounted. |
| 1286 | */ |
| 1287 | if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) { |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1288 | return StorageResultCode.OperationFailedStorageMounted; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1292 | int rc = StorageResultCode.OperationSucceeded; |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1293 | String cmd = String.format("asec rename %s %s", oldId, newId); |
| 1294 | try { |
| 1295 | mConnector.doCommand(cmd); |
| 1296 | } catch (NativeDaemonConnectorException e) { |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1297 | rc = StorageResultCode.OperationFailedInternalError; |
| San Mehat | 02735bc | 2010-01-26 15:18:08 -0800 | [diff] [blame] | 1298 | } |
| San Mehat | a181b21 | 2010-02-11 06:50:20 -0800 | [diff] [blame] | 1299 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1300 | return rc; |
| San Mehat | 45f6104 | 2010-01-23 08:12:43 -0800 | [diff] [blame] | 1301 | } |
| 1302 | |
| San Mehat | 4270e1e | 2010-01-29 05:32:19 -0800 | [diff] [blame] | 1303 | public String getSecureContainerPath(String id) { |
| 1304 | validatePermission(android.Manifest.permission.ASEC_ACCESS); |
| San Mehat | 207e538 | 2010-02-04 20:46:54 -0800 | [diff] [blame] | 1305 | waitForReady(); |
| San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 1306 | warnOnNotMounted(); |
| San Mehat | f919cd02 | 2010-02-04 15:10:38 -0800 | [diff] [blame] | 1307 | |
| San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 1308 | try { |
| 1309 | ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id)); |
| 1310 | String []tok = rsp.get(0).split(" "); |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 1311 | int code = Integer.parseInt(tok[0]); |
| San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 1312 | if (code != VoldResponseCode.AsecPathResult) { |
| 1313 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| 1314 | } |
| 1315 | return tok[1]; |
| 1316 | } catch (NativeDaemonConnectorException e) { |
| 1317 | int code = e.getCode(); |
| 1318 | if (code == VoldResponseCode.OpFailedStorageNotFound) { |
| 1319 | throw new IllegalArgumentException(String.format("Container '%s' not found", id)); |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 1320 | } else { |
| San Mehat | 2d66cef | 2010-03-23 11:12:52 -0700 | [diff] [blame] | 1321 | throw new IllegalStateException(String.format("Unexpected response code %d", code)); |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 1322 | } |
| 1323 | } |
| San Mehat | 22dd86e | 2010-01-12 12:21:18 -0800 | [diff] [blame] | 1324 | } |
| Suchi Amalapurapu | e99bb5f | 2010-03-19 14:36:49 -0700 | [diff] [blame] | 1325 | |
| 1326 | public void finishMediaUpdate() { |
| 1327 | mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE); |
| 1328 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1329 | } |
| 1330 | |