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