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