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