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