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