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