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