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