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