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