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