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