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