Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 android.content.pm; |
| 18 | |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Ng Zhi An | 1033db8 | 2019-01-24 13:45:57 -0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 21 | import android.annotation.UnsupportedAppUsage; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 27 | import android.content.pm.PackageManager.NameNotFoundException; |
| 28 | import android.content.res.Resources; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 29 | import android.content.res.XmlResourceParser; |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 30 | import android.os.Environment; |
| 31 | import android.os.Handler; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 32 | import android.os.UserHandle; |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 33 | import android.os.UserManager; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 34 | import android.util.AtomicFile; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 35 | import android.util.AttributeSet; |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 36 | import android.util.IntArray; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 37 | import android.util.Log; |
| 38 | import android.util.Slog; |
| 39 | import android.util.SparseArray; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 40 | import android.util.Xml; |
| 41 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 42 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | 9e0d81e | 2015-02-10 10:45:55 -0800 | [diff] [blame] | 43 | import com.android.internal.annotations.VisibleForTesting; |
Amith Yamasani | 460a7b4 | 2015-02-06 14:41:40 -0800 | [diff] [blame] | 44 | import com.android.internal.util.ArrayUtils; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 45 | import com.android.internal.util.FastXmlSerializer; |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 46 | |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 47 | import com.google.android.collect.Lists; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 48 | import com.google.android.collect.Maps; |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 49 | |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 50 | import libcore.io.IoUtils; |
| 51 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 52 | import org.xmlpull.v1.XmlPullParser; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 53 | import org.xmlpull.v1.XmlPullParserException; |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 54 | import org.xmlpull.v1.XmlSerializer; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 55 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 56 | import java.io.File; |
| 57 | import java.io.FileDescriptor; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 58 | import java.io.FileOutputStream; |
| 59 | import java.io.IOException; |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 60 | import java.io.InputStream; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 61 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 62 | import java.nio.charset.StandardCharsets; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 63 | import java.util.ArrayList; |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 64 | import java.util.Arrays; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 65 | import java.util.Collection; |
| 66 | import java.util.Collections; |
| 67 | import java.util.List; |
| 68 | import java.util.Map; |
| 69 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 70 | /** |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 71 | * Cache of registered services. This cache is lazily built by interrogating |
| 72 | * {@link PackageManager} on a per-user basis. It's updated as packages are |
| 73 | * added, removed and changed. Users are responsible for calling |
| 74 | * {@link #invalidateCache(int)} when a user is started, since |
| 75 | * {@link PackageManager} broadcasts aren't sent for stopped users. |
| 76 | * <p> |
| 77 | * The services are referred to by type V and are made available via the |
| 78 | * {@link #getServiceInfo} method. |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 79 | * |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 80 | * @hide |
| 81 | */ |
| 82 | public abstract class RegisteredServicesCache<V> { |
| 83 | private static final String TAG = "PackageManager"; |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 84 | private static final boolean DEBUG = false; |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 85 | protected static final String REGISTERED_SERVICES_DIR = "registered_services"; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 86 | |
| 87 | public final Context mContext; |
| 88 | private final String mInterfaceName; |
| 89 | private final String mMetaDataName; |
| 90 | private final String mAttributesName; |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 91 | private final XmlSerializerAndParser<V> mSerializerAndParser; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 92 | |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 93 | protected final Object mServicesLock = new Object(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 94 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 95 | @GuardedBy("mServicesLock") |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 96 | private final SparseArray<UserServices<V>> mUserServices = new SparseArray<UserServices<V>>(2); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 97 | |
| 98 | private static class UserServices<V> { |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 99 | @GuardedBy("mServicesLock") |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 100 | final Map<V, Integer> persistentServices = Maps.newHashMap(); |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 101 | @GuardedBy("mServicesLock") |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 102 | Map<V, ServiceInfo<V>> services = null; |
| 103 | @GuardedBy("mServicesLock") |
| 104 | boolean mPersistentServicesFileDidNotExist = true; |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 105 | @GuardedBy("mServicesLock") |
| 106 | boolean mBindInstantServiceAllowed = false; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 109 | @GuardedBy("mServicesLock") |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 110 | private UserServices<V> findOrCreateUserLocked(int userId) { |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 111 | return findOrCreateUserLocked(userId, true); |
| 112 | } |
| 113 | |
| 114 | @GuardedBy("mServicesLock") |
| 115 | private UserServices<V> findOrCreateUserLocked(int userId, boolean loadFromFileIfNew) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 116 | UserServices<V> services = mUserServices.get(userId); |
| 117 | if (services == null) { |
| 118 | services = new UserServices<V>(); |
| 119 | mUserServices.put(userId, services); |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 120 | if (loadFromFileIfNew && mSerializerAndParser != null) { |
| 121 | // Check if user exists and try loading data from file |
| 122 | // clear existing data if there was an error during migration |
| 123 | UserInfo user = getUser(userId); |
| 124 | if (user != null) { |
| 125 | AtomicFile file = createFileForUser(user.id); |
| 126 | if (file.getBaseFile().exists()) { |
| 127 | if (DEBUG) { |
| 128 | Slog.i(TAG, String.format("Loading u%s data from %s", user.id, file)); |
| 129 | } |
| 130 | InputStream is = null; |
| 131 | try { |
| 132 | is = file.openRead(); |
| 133 | readPersistentServicesLocked(is); |
| 134 | } catch (Exception e) { |
| 135 | Log.w(TAG, "Error reading persistent services for user " + user.id, e); |
| 136 | } finally { |
| 137 | IoUtils.closeQuietly(is); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 142 | } |
| 143 | return services; |
| 144 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 145 | |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 146 | // the listener and handler are synchronized on "this" and must be updated together |
| 147 | private RegisteredServicesCacheListener<V> mListener; |
| 148 | private Handler mHandler; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 149 | |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 150 | @UnsupportedAppUsage |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 151 | public RegisteredServicesCache(Context context, String interfaceName, String metaDataName, |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 152 | String attributeName, XmlSerializerAndParser<V> serializerAndParser) { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 153 | mContext = context; |
| 154 | mInterfaceName = interfaceName; |
| 155 | mMetaDataName = metaDataName; |
| 156 | mAttributesName = attributeName; |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 157 | mSerializerAndParser = serializerAndParser; |
| 158 | |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 159 | migrateIfNecessaryLocked(); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 160 | |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 161 | IntentFilter intentFilter = new IntentFilter(); |
| 162 | intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 163 | intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 164 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 165 | intentFilter.addDataScheme("package"); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 166 | mContext.registerReceiverAsUser(mPackageReceiver, UserHandle.ALL, intentFilter, null, null); |
| 167 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 168 | // Register for events related to sdcard installation. |
| 169 | IntentFilter sdFilter = new IntentFilter(); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 170 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); |
| 171 | sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 172 | mContext.registerReceiver(mExternalReceiver, sdFilter); |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 173 | |
| 174 | // Register for user-related events |
| 175 | IntentFilter userFilter = new IntentFilter(); |
| 176 | sdFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 177 | mContext.registerReceiver(mUserRemovedReceiver, userFilter); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Ng Zhi An | 1033db8 | 2019-01-24 13:45:57 -0800 | [diff] [blame] | 180 | @VisibleForTesting |
| 181 | protected void handlePackageEvent(Intent intent, int userId) { |
Christopher Tate | 0598d70 | 2014-03-11 18:16:46 -0700 | [diff] [blame] | 182 | // Don't regenerate the services map when the package is removed or its |
| 183 | // ASEC container unmounted as a step in replacement. The subsequent |
| 184 | // _ADDED / _AVAILABLE call will regenerate the map in the final state. |
| 185 | final String action = intent.getAction(); |
| 186 | // it's a new-component action if it isn't some sort of removal |
| 187 | final boolean isRemoval = Intent.ACTION_PACKAGE_REMOVED.equals(action) |
| 188 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action); |
| 189 | // if it's a removal, is it part of an update-in-place step? |
| 190 | final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 191 | |
| 192 | if (isRemoval && replacing) { |
| 193 | // package is going away, but it's the middle of an upgrade: keep the current |
| 194 | // state and do nothing here. This clause is intentionally empty. |
| 195 | } else { |
Amith Yamasani | 460a7b4 | 2015-02-06 14:41:40 -0800 | [diff] [blame] | 196 | int[] uids = null; |
Christopher Tate | 0598d70 | 2014-03-11 18:16:46 -0700 | [diff] [blame] | 197 | // either we're adding/changing, or it's a removal without replacement, so |
Amith Yamasani | 460a7b4 | 2015-02-06 14:41:40 -0800 | [diff] [blame] | 198 | // we need to update the set of available services |
| 199 | if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action) |
| 200 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { |
| 201 | uids = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
| 202 | } else { |
| 203 | int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 204 | if (uid > 0) { |
| 205 | uids = new int[] { uid }; |
| 206 | } |
| 207 | } |
| 208 | generateServicesMap(uids, userId); |
Christopher Tate | 0598d70 | 2014-03-11 18:16:46 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 212 | private final BroadcastReceiver mPackageReceiver = new BroadcastReceiver() { |
| 213 | @Override |
| 214 | public void onReceive(Context context, Intent intent) { |
| 215 | final int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 216 | if (uid != -1) { |
Christopher Tate | 0598d70 | 2014-03-11 18:16:46 -0700 | [diff] [blame] | 217 | handlePackageEvent(intent, UserHandle.getUserId(uid)); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 218 | } |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 219 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | private final BroadcastReceiver mExternalReceiver = new BroadcastReceiver() { |
| 223 | @Override |
| 224 | public void onReceive(Context context, Intent intent) { |
| 225 | // External apps can't coexist with multi-user, so scan owner |
Xiaohui Chen | 98404fd | 2015-08-17 16:09:02 -0700 | [diff] [blame] | 226 | handlePackageEvent(intent, UserHandle.USER_SYSTEM); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 227 | } |
| 228 | }; |
| 229 | |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 230 | private final BroadcastReceiver mUserRemovedReceiver = new BroadcastReceiver() { |
| 231 | @Override |
| 232 | public void onReceive(Context context, Intent intent) { |
| 233 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 234 | if (DEBUG) { |
| 235 | Slog.d(TAG, "u" + userId + " removed - cleaning up"); |
| 236 | } |
| 237 | onUserRemoved(userId); |
| 238 | } |
| 239 | }; |
| 240 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 241 | public void invalidateCache(int userId) { |
| 242 | synchronized (mServicesLock) { |
Ng Zhi An | 1033db8 | 2019-01-24 13:45:57 -0800 | [diff] [blame] | 243 | if (DEBUG) { |
| 244 | Slog.d(TAG, "invalidating cache for " + userId + " " + mInterfaceName); |
| 245 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 246 | final UserServices<V> user = findOrCreateUserLocked(userId); |
| 247 | user.services = null; |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 248 | onServicesChangedLocked(userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | public void dump(FileDescriptor fd, PrintWriter fout, String[] args, int userId) { |
| 253 | synchronized (mServicesLock) { |
| 254 | final UserServices<V> user = findOrCreateUserLocked(userId); |
| 255 | if (user.services != null) { |
| 256 | fout.println("RegisteredServicesCache: " + user.services.size() + " services"); |
| 257 | for (ServiceInfo<?> info : user.services.values()) { |
| 258 | fout.println(" " + info); |
| 259 | } |
| 260 | } else { |
| 261 | fout.println("RegisteredServicesCache: services not loaded"); |
| 262 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 266 | public RegisteredServicesCacheListener<V> getListener() { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 267 | synchronized (this) { |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 268 | return mListener; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 272 | public void setListener(RegisteredServicesCacheListener<V> listener, Handler handler) { |
| 273 | if (handler == null) { |
| 274 | handler = new Handler(mContext.getMainLooper()); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 275 | } |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 276 | synchronized (this) { |
| 277 | mHandler = handler; |
| 278 | mListener = listener; |
| 279 | } |
| 280 | } |
| 281 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 282 | private void notifyListener(final V type, final int userId, final boolean removed) { |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 283 | if (DEBUG) { |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 284 | Log.d(TAG, "notifyListener: " + type + " is " + (removed ? "removed" : "added")); |
| 285 | } |
| 286 | RegisteredServicesCacheListener<V> listener; |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 287 | Handler handler; |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 288 | synchronized (this) { |
| 289 | listener = mListener; |
| 290 | handler = mHandler; |
| 291 | } |
| 292 | if (listener == null) { |
| 293 | return; |
| 294 | } |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 295 | |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 296 | final RegisteredServicesCacheListener<V> listener2 = listener; |
| 297 | handler.post(new Runnable() { |
| 298 | public void run() { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 299 | listener2.onServiceChanged(type, userId, removed); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 300 | } |
| 301 | }); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Value type that describes a Service. The information within can be used |
| 306 | * to bind to the service. |
| 307 | */ |
| 308 | public static class ServiceInfo<V> { |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 309 | @UnsupportedAppUsage |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 310 | public final V type; |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 311 | public final ComponentInfo componentInfo; |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 312 | @UnsupportedAppUsage |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 313 | public final ComponentName componentName; |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 314 | @UnsupportedAppUsage |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 315 | public final int uid; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 316 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 317 | /** @hide */ |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 318 | public ServiceInfo(V type, ComponentInfo componentInfo, ComponentName componentName) { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 319 | this.type = type; |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 320 | this.componentInfo = componentInfo; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 321 | this.componentName = componentName; |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 322 | this.uid = (componentInfo != null) ? componentInfo.applicationInfo.uid : -1; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Jeff Hamilton | c42c0dd | 2009-09-03 09:08:30 -0500 | [diff] [blame] | 325 | @Override |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 326 | public String toString() { |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 327 | return "ServiceInfo: " + type + ", " + componentName + ", uid " + uid; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Accessor for the registered authenticators. |
| 333 | * @param type the account type of the authenticator |
| 334 | * @return the AuthenticatorInfo that matches the account type or null if none is present |
| 335 | */ |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 336 | public ServiceInfo<V> getServiceInfo(V type, int userId) { |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 337 | synchronized (mServicesLock) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 338 | // Find user and lazily populate cache |
| 339 | final UserServices<V> user = findOrCreateUserLocked(userId); |
| 340 | if (user.services == null) { |
Amith Yamasani | 460a7b4 | 2015-02-06 14:41:40 -0800 | [diff] [blame] | 341 | generateServicesMap(null, userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 342 | } |
| 343 | return user.services.get(type); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 344 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /** |
| 348 | * @return a collection of {@link RegisteredServicesCache.ServiceInfo} objects for all |
| 349 | * registered authenticators. |
| 350 | */ |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 351 | public Collection<ServiceInfo<V>> getAllServices(int userId) { |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 352 | synchronized (mServicesLock) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 353 | // Find user and lazily populate cache |
| 354 | final UserServices<V> user = findOrCreateUserLocked(userId); |
| 355 | if (user.services == null) { |
Amith Yamasani | 460a7b4 | 2015-02-06 14:41:40 -0800 | [diff] [blame] | 356 | generateServicesMap(null, userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 357 | } |
Jeff Sharkey | a706e2f | 2012-10-16 12:02:42 -0700 | [diff] [blame] | 358 | return Collections.unmodifiableCollection( |
| 359 | new ArrayList<ServiceInfo<V>>(user.services.values())); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 360 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 363 | public void updateServices(int userId) { |
| 364 | if (DEBUG) { |
| 365 | Slog.d(TAG, "updateServices u" + userId); |
| 366 | } |
| 367 | List<ServiceInfo<V>> allServices; |
| 368 | synchronized (mServicesLock) { |
| 369 | final UserServices<V> user = findOrCreateUserLocked(userId); |
| 370 | // If services haven't been initialized yet - no updates required |
| 371 | if (user.services == null) { |
| 372 | return; |
| 373 | } |
| 374 | allServices = new ArrayList<>(user.services.values()); |
| 375 | } |
| 376 | IntArray updatedUids = null; |
| 377 | for (ServiceInfo<V> service : allServices) { |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 378 | long versionCode = service.componentInfo.applicationInfo.versionCode; |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 379 | String pkg = service.componentInfo.packageName; |
| 380 | ApplicationInfo newAppInfo = null; |
| 381 | try { |
| 382 | newAppInfo = mContext.getPackageManager().getApplicationInfoAsUser(pkg, 0, userId); |
| 383 | } catch (NameNotFoundException e) { |
| 384 | // Package uninstalled - treat as null app info |
| 385 | } |
| 386 | // If package updated or removed |
| 387 | if ((newAppInfo == null) || (newAppInfo.versionCode != versionCode)) { |
| 388 | if (DEBUG) { |
| 389 | Slog.d(TAG, "Package " + pkg + " uid=" + service.uid |
| 390 | + " updated. New appInfo: " + newAppInfo); |
| 391 | } |
| 392 | if (updatedUids == null) { |
| 393 | updatedUids = new IntArray(); |
| 394 | } |
| 395 | updatedUids.add(service.uid); |
| 396 | } |
| 397 | } |
| 398 | if (updatedUids != null && updatedUids.size() > 0) { |
| 399 | int[] updatedUidsArray = updatedUids.toArray(); |
| 400 | generateServicesMap(updatedUidsArray, userId); |
| 401 | } |
| 402 | } |
| 403 | |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 404 | /** |
| 405 | * @return whether the binding to service is allowed for instant apps. |
| 406 | */ |
| 407 | public boolean getBindInstantServiceAllowed(int userId) { |
| 408 | mContext.enforceCallingOrSelfPermission( |
| 409 | Manifest.permission.MANAGE_BIND_INSTANT_SERVICE, |
| 410 | "getBindInstantServiceAllowed"); |
| 411 | |
| 412 | synchronized (mServicesLock) { |
| 413 | final UserServices<V> user = findOrCreateUserLocked(userId); |
| 414 | return user.mBindInstantServiceAllowed; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Set whether the binding to service is allowed or not for instant apps. |
| 420 | */ |
| 421 | public void setBindInstantServiceAllowed(int userId, boolean allowed) { |
| 422 | mContext.enforceCallingOrSelfPermission( |
| 423 | Manifest.permission.MANAGE_BIND_INSTANT_SERVICE, |
| 424 | "setBindInstantServiceAllowed"); |
| 425 | |
| 426 | synchronized (mServicesLock) { |
| 427 | final UserServices<V> user = findOrCreateUserLocked(userId); |
| 428 | user.mBindInstantServiceAllowed = allowed; |
| 429 | } |
| 430 | } |
| 431 | |
Fyodor Kupolov | 9e0d81e | 2015-02-10 10:45:55 -0800 | [diff] [blame] | 432 | @VisibleForTesting |
| 433 | protected boolean inSystemImage(int callerUid) { |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 434 | String[] packages = mContext.getPackageManager().getPackagesForUid(callerUid); |
liulvping | bf76ee7 | 2016-09-08 09:16:48 +0800 | [diff] [blame] | 435 | if (packages != null) { |
| 436 | for (String name : packages) { |
| 437 | try { |
| 438 | PackageInfo packageInfo = |
| 439 | mContext.getPackageManager().getPackageInfo(name, 0 /* flags */); |
| 440 | if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 441 | return true; |
| 442 | } |
| 443 | } catch (PackageManager.NameNotFoundException e) { |
| 444 | return false; |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 445 | } |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | return false; |
| 449 | } |
| 450 | |
Fyodor Kupolov | 9e0d81e | 2015-02-10 10:45:55 -0800 | [diff] [blame] | 451 | @VisibleForTesting |
| 452 | protected List<ResolveInfo> queryIntentServices(int userId) { |
| 453 | final PackageManager pm = mContext.getPackageManager(); |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 454 | int flags = PackageManager.GET_META_DATA |
| 455 | | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 456 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE; |
| 457 | synchronized (mServicesLock) { |
| 458 | final UserServices<V> user = findOrCreateUserLocked(userId); |
| 459 | if (user.mBindInstantServiceAllowed) { |
| 460 | flags |= PackageManager.MATCH_INSTANT; |
| 461 | } |
| 462 | } |
| 463 | return pm.queryIntentServicesAsUser(new Intent(mInterfaceName), flags, userId); |
Fyodor Kupolov | 9e0d81e | 2015-02-10 10:45:55 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 466 | /** |
| 467 | * Populate {@link UserServices#services} by scanning installed packages for |
| 468 | * given {@link UserHandle}. |
Amith Yamasani | 460a7b4 | 2015-02-06 14:41:40 -0800 | [diff] [blame] | 469 | * @param changedUids the array of uids that have been affected, as mentioned in the broadcast |
| 470 | * or null to assume that everything is affected. |
| 471 | * @param userId the user for whom to update the services map. |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 472 | */ |
Ng Zhi An | 1033db8 | 2019-01-24 13:45:57 -0800 | [diff] [blame] | 473 | private void generateServicesMap(@Nullable int[] changedUids, int userId) { |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 474 | if (DEBUG) { |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 475 | Slog.d(TAG, "generateServicesMap() for " + userId + ", changed UIDs = " |
| 476 | + Arrays.toString(changedUids)); |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 477 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 478 | |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 479 | synchronized (mServicesLock) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 480 | final UserServices<V> user = findOrCreateUserLocked(userId); |
Ng Zhi An | 1033db8 | 2019-01-24 13:45:57 -0800 | [diff] [blame] | 481 | final boolean cacheInvalid = user.services == null; |
| 482 | if (cacheInvalid) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 483 | user.services = Maps.newHashMap(); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 484 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 485 | |
Ng Zhi An | 1033db8 | 2019-01-24 13:45:57 -0800 | [diff] [blame] | 486 | final ArrayList<ServiceInfo<V>> serviceInfos = new ArrayList<>(); |
| 487 | final List<ResolveInfo> resolveInfos = queryIntentServices(userId); |
| 488 | |
| 489 | for (ResolveInfo resolveInfo : resolveInfos) { |
| 490 | try { |
| 491 | // when changedUids == null, we want to do a rescan of everything, this means |
| 492 | // it's the initial scan, and containsUid will trivially return true |
| 493 | // when changedUids != null, we got here because a package changed, but |
| 494 | // invalidateCache could have been called (thus user.services == null), and we |
| 495 | // should query from PackageManager again |
| 496 | if (!cacheInvalid |
| 497 | && !containsUid( |
| 498 | changedUids, resolveInfo.serviceInfo.applicationInfo.uid)) { |
| 499 | if (DEBUG) { |
| 500 | Slog.d(TAG, "Skipping parseServiceInfo for " + resolveInfo); |
| 501 | } |
| 502 | continue; |
| 503 | } |
| 504 | ServiceInfo<V> info = parseServiceInfo(resolveInfo); |
| 505 | if (info == null) { |
| 506 | Log.w(TAG, "Unable to load service info " + resolveInfo.toString()); |
| 507 | continue; |
| 508 | } |
| 509 | serviceInfos.add(info); |
| 510 | } catch (XmlPullParserException | IOException e) { |
| 511 | Log.w(TAG, "Unable to load service info " + resolveInfo.toString(), e); |
| 512 | } |
| 513 | } |
| 514 | |
Alon Albert | 3fa51e3 | 2010-11-11 09:24:04 -0800 | [diff] [blame] | 515 | StringBuilder changes = new StringBuilder(); |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 516 | boolean changed = false; |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 517 | for (ServiceInfo<V> info : serviceInfos) { |
| 518 | // four cases: |
| 519 | // - doesn't exist yet |
| 520 | // - add, notify user that it was added |
| 521 | // - exists and the UID is the same |
| 522 | // - replace, don't notify user |
| 523 | // - exists, the UID is different, and the new one is not a system package |
| 524 | // - ignore |
| 525 | // - exists, the UID is different, and the new one is a system package |
| 526 | // - add, notify user that it was added |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 527 | Integer previousUid = user.persistentServices.get(info.type); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 528 | if (previousUid == null) { |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 529 | if (DEBUG) { |
| 530 | changes.append(" New service added: ").append(info).append("\n"); |
| 531 | } |
| 532 | changed = true; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 533 | user.services.put(info.type, info); |
| 534 | user.persistentServices.put(info.type, info.uid); |
Ng Zhi An | 1033db8 | 2019-01-24 13:45:57 -0800 | [diff] [blame] | 535 | if (!(user.mPersistentServicesFileDidNotExist && cacheInvalid)) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 536 | notifyListener(info.type, userId, false /* removed */); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 537 | } |
| 538 | } else if (previousUid == info.uid) { |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 539 | if (DEBUG) { |
Alon Albert | 3fa51e3 | 2010-11-11 09:24:04 -0800 | [diff] [blame] | 540 | changes.append(" Existing service (nop): ").append(info).append("\n"); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 541 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 542 | user.services.put(info.type, info); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 543 | } else if (inSystemImage(info.uid) |
| 544 | || !containsTypeAndUid(serviceInfos, info.type, previousUid)) { |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 545 | if (DEBUG) { |
| 546 | if (inSystemImage(info.uid)) { |
| 547 | changes.append(" System service replacing existing: ").append(info) |
| 548 | .append("\n"); |
| 549 | } else { |
| 550 | changes.append(" Existing service replacing a removed service: ") |
| 551 | .append(info).append("\n"); |
| 552 | } |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 553 | } |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 554 | changed = true; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 555 | user.services.put(info.type, info); |
| 556 | user.persistentServices.put(info.type, info.uid); |
| 557 | notifyListener(info.type, userId, false /* removed */); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 558 | } else { |
| 559 | // ignore |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 560 | if (DEBUG) { |
| 561 | changes.append(" Existing service with new uid ignored: ").append(info) |
| 562 | .append("\n"); |
| 563 | } |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
| 567 | ArrayList<V> toBeRemoved = Lists.newArrayList(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 568 | for (V v1 : user.persistentServices.keySet()) { |
Amith Yamasani | 460a7b4 | 2015-02-06 14:41:40 -0800 | [diff] [blame] | 569 | // Remove a persisted service that's not in the currently available services list. |
| 570 | // And only if it is in the list of changedUids. |
| 571 | if (!containsType(serviceInfos, v1) |
| 572 | && containsUid(changedUids, user.persistentServices.get(v1))) { |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 573 | toBeRemoved.add(v1); |
| 574 | } |
| 575 | } |
| 576 | for (V v1 : toBeRemoved) { |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 577 | if (DEBUG) { |
| 578 | changes.append(" Service removed: ").append(v1).append("\n"); |
| 579 | } |
| 580 | changed = true; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 581 | user.persistentServices.remove(v1); |
Amith Yamasani | 460a7b4 | 2015-02-06 14:41:40 -0800 | [diff] [blame] | 582 | user.services.remove(v1); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 583 | notifyListener(v1, userId, true /* removed */); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 584 | } |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 585 | if (DEBUG) { |
Amith Yamasani | 460a7b4 | 2015-02-06 14:41:40 -0800 | [diff] [blame] | 586 | Log.d(TAG, "user.services="); |
| 587 | for (V v : user.services.keySet()) { |
| 588 | Log.d(TAG, " " + v + " " + user.services.get(v)); |
| 589 | } |
| 590 | Log.d(TAG, "user.persistentServices="); |
| 591 | for (V v : user.persistentServices.keySet()) { |
| 592 | Log.d(TAG, " " + v + " " + user.persistentServices.get(v)); |
| 593 | } |
| 594 | } |
| 595 | if (DEBUG) { |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 596 | if (changes.length() > 0) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 597 | Log.d(TAG, "generateServicesMap(" + mInterfaceName + "): " + |
| 598 | serviceInfos.size() + " services:\n" + changes); |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 599 | } else { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 600 | Log.d(TAG, "generateServicesMap(" + mInterfaceName + "): " + |
| 601 | serviceInfos.size() + " services unchanged"); |
| 602 | } |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 603 | } |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 604 | if (changed) { |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 605 | onServicesChangedLocked(userId); |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 606 | writePersistentServicesLocked(user, userId); |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 607 | } |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 611 | protected void onServicesChangedLocked(int userId) { |
| 612 | // Feel free to override |
| 613 | } |
| 614 | |
Amith Yamasani | 460a7b4 | 2015-02-06 14:41:40 -0800 | [diff] [blame] | 615 | /** |
| 616 | * Returns true if the list of changed uids is null (wildcard) or the specified uid |
| 617 | * is contained in the list of changed uids. |
| 618 | */ |
| 619 | private boolean containsUid(int[] changedUids, int uid) { |
| 620 | return changedUids == null || ArrayUtils.contains(changedUids, uid); |
| 621 | } |
| 622 | |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 623 | private boolean containsType(ArrayList<ServiceInfo<V>> serviceInfos, V type) { |
| 624 | for (int i = 0, N = serviceInfos.size(); i < N; i++) { |
| 625 | if (serviceInfos.get(i).type.equals(type)) { |
| 626 | return true; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | return false; |
| 631 | } |
| 632 | |
| 633 | private boolean containsTypeAndUid(ArrayList<ServiceInfo<V>> serviceInfos, V type, int uid) { |
| 634 | for (int i = 0, N = serviceInfos.size(); i < N; i++) { |
| 635 | final ServiceInfo<V> serviceInfo = serviceInfos.get(i); |
| 636 | if (serviceInfo.type.equals(type) && serviceInfo.uid == uid) { |
| 637 | return true; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | return false; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Fyodor Kupolov | 9e0d81e | 2015-02-10 10:45:55 -0800 | [diff] [blame] | 644 | @VisibleForTesting |
| 645 | protected ServiceInfo<V> parseServiceInfo(ResolveInfo service) |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 646 | throws XmlPullParserException, IOException { |
| 647 | android.content.pm.ServiceInfo si = service.serviceInfo; |
| 648 | ComponentName componentName = new ComponentName(si.packageName, si.name); |
| 649 | |
| 650 | PackageManager pm = mContext.getPackageManager(); |
| 651 | |
| 652 | XmlResourceParser parser = null; |
| 653 | try { |
| 654 | parser = si.loadXmlMetaData(pm, mMetaDataName); |
| 655 | if (parser == null) { |
| 656 | throw new XmlPullParserException("No " + mMetaDataName + " meta-data"); |
| 657 | } |
| 658 | |
| 659 | AttributeSet attrs = Xml.asAttributeSet(parser); |
| 660 | |
| 661 | int type; |
| 662 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 663 | && type != XmlPullParser.START_TAG) { |
| 664 | } |
| 665 | |
| 666 | String nodeName = parser.getName(); |
| 667 | if (!mAttributesName.equals(nodeName)) { |
| 668 | throw new XmlPullParserException( |
| 669 | "Meta-data does not start with " + mAttributesName + " tag"); |
| 670 | } |
| 671 | |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 672 | V v = parseServiceAttributes(pm.getResourcesForApplication(si.applicationInfo), |
| 673 | si.packageName, attrs); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 674 | if (v == null) { |
| 675 | return null; |
| 676 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 677 | final android.content.pm.ServiceInfo serviceInfo = service.serviceInfo; |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 678 | return new ServiceInfo<V>(v, serviceInfo, componentName); |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 679 | } catch (NameNotFoundException e) { |
| 680 | throw new XmlPullParserException( |
| 681 | "Unable to load resources for pacakge " + si.packageName); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 682 | } finally { |
| 683 | if (parser != null) parser.close(); |
| 684 | } |
| 685 | } |
| 686 | |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 687 | /** |
| 688 | * Read all sync status back in to the initial engine state. |
| 689 | */ |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 690 | private void readPersistentServicesLocked(InputStream is) |
| 691 | throws XmlPullParserException, IOException { |
| 692 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 693 | parser.setInput(is, StandardCharsets.UTF_8.name()); |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 694 | int eventType = parser.getEventType(); |
| 695 | while (eventType != XmlPullParser.START_TAG |
| 696 | && eventType != XmlPullParser.END_DOCUMENT) { |
| 697 | eventType = parser.next(); |
| 698 | } |
| 699 | String tagName = parser.getName(); |
| 700 | if ("services".equals(tagName)) { |
| 701 | eventType = parser.next(); |
| 702 | do { |
| 703 | if (eventType == XmlPullParser.START_TAG && parser.getDepth() == 2) { |
| 704 | tagName = parser.getName(); |
| 705 | if ("service".equals(tagName)) { |
| 706 | V service = mSerializerAndParser.createFromXml(parser); |
| 707 | if (service == null) { |
| 708 | break; |
| 709 | } |
| 710 | String uidString = parser.getAttributeValue(null, "uid"); |
| 711 | final int uid = Integer.parseInt(uidString); |
| 712 | final int userId = UserHandle.getUserId(uid); |
| 713 | final UserServices<V> user = findOrCreateUserLocked(userId, |
| 714 | false /*loadFromFileIfNew*/) ; |
| 715 | user.persistentServices.put(service, uid); |
| 716 | } |
| 717 | } |
| 718 | eventType = parser.next(); |
| 719 | } while (eventType != XmlPullParser.END_DOCUMENT); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | private void migrateIfNecessaryLocked() { |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 724 | if (mSerializerAndParser == null) { |
| 725 | return; |
| 726 | } |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 727 | File systemDir = new File(getDataDirectory(), "system"); |
| 728 | File syncDir = new File(systemDir, REGISTERED_SERVICES_DIR); |
| 729 | AtomicFile oldFile = new AtomicFile(new File(syncDir, mInterfaceName + ".xml")); |
| 730 | boolean oldFileExists = oldFile.getBaseFile().exists(); |
| 731 | |
| 732 | if (oldFileExists) { |
| 733 | File marker = new File(syncDir, mInterfaceName + ".xml.migrated"); |
| 734 | // if not migrated, perform the migration and add a marker |
| 735 | if (!marker.exists()) { |
| 736 | if (DEBUG) { |
| 737 | Slog.i(TAG, "Marker file " + marker + " does not exist - running migration"); |
| 738 | } |
| 739 | InputStream is = null; |
| 740 | try { |
| 741 | is = oldFile.openRead(); |
| 742 | mUserServices.clear(); |
| 743 | readPersistentServicesLocked(is); |
| 744 | } catch (Exception e) { |
| 745 | Log.w(TAG, "Error reading persistent services, starting from scratch", e); |
| 746 | } finally { |
| 747 | IoUtils.closeQuietly(is); |
| 748 | } |
| 749 | try { |
| 750 | for (UserInfo user : getUsers()) { |
| 751 | UserServices<V> userServices = mUserServices.get(user.id); |
| 752 | if (userServices != null) { |
| 753 | if (DEBUG) { |
| 754 | Slog.i(TAG, "Migrating u" + user.id + " services " |
| 755 | + userServices.persistentServices); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 756 | } |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 757 | writePersistentServicesLocked(userServices, user.id); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 758 | } |
| 759 | } |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 760 | marker.createNewFile(); |
| 761 | } catch (Exception e) { |
| 762 | Log.w(TAG, "Migration failed", e); |
| 763 | } |
| 764 | // Migration is complete and we don't need to keep data for all users anymore, |
| 765 | // It will be loaded from a new location when requested |
| 766 | mUserServices.clear(); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 767 | } |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
| 771 | /** |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 772 | * Writes services of a specified user to the file. |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 773 | */ |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 774 | private void writePersistentServicesLocked(UserServices<V> user, int userId) { |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 775 | if (mSerializerAndParser == null) { |
| 776 | return; |
| 777 | } |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 778 | AtomicFile atomicFile = createFileForUser(userId); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 779 | FileOutputStream fos = null; |
| 780 | try { |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 781 | fos = atomicFile.startWrite(); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 782 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 783 | out.setOutput(fos, StandardCharsets.UTF_8.name()); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 784 | out.startDocument(null, true); |
| 785 | out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 786 | out.startTag(null, "services"); |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 787 | for (Map.Entry<V, Integer> service : user.persistentServices.entrySet()) { |
| 788 | out.startTag(null, "service"); |
| 789 | out.attribute(null, "uid", Integer.toString(service.getValue())); |
| 790 | mSerializerAndParser.writeAsXml(service.getKey(), out); |
| 791 | out.endTag(null, "service"); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 792 | } |
| 793 | out.endTag(null, "services"); |
| 794 | out.endDocument(); |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 795 | atomicFile.finishWrite(fos); |
Fyodor Kupolov | 9e0d81e | 2015-02-10 10:45:55 -0800 | [diff] [blame] | 796 | } catch (IOException e1) { |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 797 | Log.w(TAG, "Error writing accounts", e1); |
| 798 | if (fos != null) { |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 799 | atomicFile.failWrite(fos); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
Fyodor Kupolov | 9e0d81e | 2015-02-10 10:45:55 -0800 | [diff] [blame] | 804 | @VisibleForTesting |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 805 | protected void onUserRemoved(int userId) { |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 806 | synchronized (mServicesLock) { |
| 807 | mUserServices.remove(userId); |
| 808 | } |
Fyodor Kupolov | 259e761 | 2015-02-11 14:13:34 -0800 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | @VisibleForTesting |
| 812 | protected List<UserInfo> getUsers() { |
| 813 | return UserManager.get(mContext).getUsers(true); |
| 814 | } |
| 815 | |
| 816 | @VisibleForTesting |
| 817 | protected UserInfo getUser(int userId) { |
| 818 | return UserManager.get(mContext).getUserInfo(userId); |
| 819 | } |
| 820 | |
| 821 | private AtomicFile createFileForUser(int userId) { |
| 822 | File userDir = getUserSystemDirectory(userId); |
| 823 | File userFile = new File(userDir, REGISTERED_SERVICES_DIR + "/" + mInterfaceName + ".xml"); |
| 824 | return new AtomicFile(userFile); |
| 825 | } |
| 826 | |
| 827 | @VisibleForTesting |
| 828 | protected File getUserSystemDirectory(int userId) { |
| 829 | return Environment.getUserSystemDirectory(userId); |
| 830 | } |
| 831 | |
| 832 | @VisibleForTesting |
| 833 | protected File getDataDirectory() { |
| 834 | return Environment.getDataDirectory(); |
| 835 | } |
| 836 | |
| 837 | @VisibleForTesting |
Fyodor Kupolov | 9e0d81e | 2015-02-10 10:45:55 -0800 | [diff] [blame] | 838 | protected Map<V, Integer> getPersistentServices(int userId) { |
| 839 | return findOrCreateUserLocked(userId).persistentServices; |
| 840 | } |
| 841 | |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 842 | public abstract V parseServiceAttributes(Resources res, |
| 843 | String packageName, AttributeSet attrs); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 844 | } |