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