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