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