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