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