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