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