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