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