John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2014, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.notification; |
| 18 | |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 19 | import android.app.INotificationManager; |
| 20 | import android.app.NotificationManager; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 21 | import android.content.ComponentName; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 22 | import android.content.Context; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 23 | import android.content.pm.IPackageManager; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 24 | import android.net.Uri; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 25 | import android.os.IBinder; |
| 26 | import android.os.IInterface; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 27 | import android.os.RemoteException; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 28 | import android.os.UserHandle; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 29 | import android.provider.Settings; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 30 | import android.service.notification.Condition; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 31 | import android.service.notification.ConditionProviderService; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 32 | import android.service.notification.IConditionProvider; |
| 33 | import android.util.ArrayMap; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 34 | import android.util.ArraySet; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 35 | import android.util.Slog; |
| 36 | |
| 37 | import com.android.internal.R; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 38 | import com.android.internal.annotations.VisibleForTesting; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 39 | import com.android.server.notification.NotificationManagerService.DumpFilter; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 40 | |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 41 | import org.xmlpull.v1.XmlSerializer; |
| 42 | |
| 43 | import java.io.IOException; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 44 | import java.io.PrintWriter; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 45 | import java.util.ArrayList; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 46 | import java.util.Arrays; |
| 47 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 48 | public class ConditionProviders extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 49 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 50 | @VisibleForTesting |
| 51 | static final String TAG_ENABLED_DND_APPS = "dnd_apps"; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 52 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 53 | private final ArrayList<ConditionRecord> mRecords = new ArrayList<>(); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 54 | private final ArraySet<String> mSystemConditionProviderNames; |
| 55 | private final ArraySet<SystemConditionProviderService> mSystemConditionProviders |
| 56 | = new ArraySet<>(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 57 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 58 | private Callback mCallback; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 59 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 60 | public ConditionProviders(Context context, UserProfiles userProfiles, IPackageManager pm) { |
| 61 | super(context, new Object(), userProfiles, pm); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 62 | mSystemConditionProviderNames = safeSet(PropConfig.getStringArray(mContext, |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 63 | "system.condition.providers", |
| 64 | R.array.config_system_condition_providers)); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 65 | mApprovalLevel = APPROVAL_BY_PACKAGE; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 66 | } |
| 67 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 68 | public void setCallback(Callback callback) { |
| 69 | mCallback = callback; |
| 70 | } |
| 71 | |
| 72 | public boolean isSystemProviderEnabled(String path) { |
| 73 | return mSystemConditionProviderNames.contains(path); |
| 74 | } |
| 75 | |
| 76 | public void addSystemProvider(SystemConditionProviderService service) { |
| 77 | mSystemConditionProviders.add(service); |
| 78 | service.attachBase(mContext); |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 79 | registerService(service.asInterface(), service.getComponent(), UserHandle.USER_SYSTEM); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | public Iterable<SystemConditionProviderService> getSystemProviders() { |
| 83 | return mSystemConditionProviders; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 87 | protected ArrayMap<Boolean, ArrayList<ComponentName>> |
| 88 | resetComponents(String packageName, int userId) { |
| 89 | resetPackage(packageName, userId); |
| 90 | ArrayMap<Boolean, ArrayList<ComponentName>> changes = new ArrayMap<>(); |
| 91 | changes.put(true, new ArrayList<>(0)); |
| 92 | changes.put(false, new ArrayList<>(0)); |
| 93 | return changes; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return true if the passed package is enabled. false otherwise |
| 98 | */ |
| 99 | boolean resetPackage(String packageName, int userId) { |
| 100 | boolean isAllowed = super.isPackageOrComponentAllowed(packageName, userId); |
| 101 | boolean isDefault = super.isDefaultComponentOrPackage(packageName); |
| 102 | if (!isAllowed && isDefault) { |
| 103 | setPackageOrComponentEnabled(packageName, userId, true, true); |
| 104 | } |
| 105 | if (isAllowed && !isDefault) { |
| 106 | setPackageOrComponentEnabled(packageName, userId, true, false); |
| 107 | } |
| 108 | return !isAllowed && isDefault; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | void writeDefaults(XmlSerializer out) throws IOException { |
| 113 | synchronized (mDefaultsLock) { |
| 114 | String defaults = String.join(ENABLED_SERVICES_SEPARATOR, mDefaultPackages); |
| 115 | out.attribute(null, ATT_DEFAULTS, defaults); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | @Override |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 120 | protected Config getConfig() { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 121 | final Config c = new Config(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 122 | c.caption = "condition provider"; |
| 123 | c.serviceInterface = ConditionProviderService.SERVICE_INTERFACE; |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 124 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 125 | c.xmlTag = TAG_ENABLED_DND_APPS; |
Julia Reynolds | 6e839b0 | 2016-04-13 10:01:17 -0400 | [diff] [blame] | 126 | c.secondarySettingName = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 127 | c.bindPermission = android.Manifest.permission.BIND_CONDITION_PROVIDER_SERVICE; |
| 128 | c.settingsAction = Settings.ACTION_CONDITION_PROVIDER_SETTINGS; |
| 129 | c.clientLabel = R.string.condition_provider_service_binding_label; |
| 130 | return c; |
| 131 | } |
| 132 | |
| 133 | @Override |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 134 | public void dump(PrintWriter pw, DumpFilter filter) { |
| 135 | super.dump(pw, filter); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 136 | synchronized(mMutex) { |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 137 | pw.print(" mRecords("); pw.print(mRecords.size()); pw.println("):"); |
| 138 | for (int i = 0; i < mRecords.size(); i++) { |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 139 | final ConditionRecord r = mRecords.get(i); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 140 | if (filter != null && !filter.matches(r.component)) continue; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 141 | pw.print(" "); pw.println(r); |
| 142 | final String countdownDesc = CountdownConditionProvider.tryParseDescription(r.id); |
| 143 | if (countdownDesc != null) { |
| 144 | pw.print(" ("); pw.print(countdownDesc); pw.println(")"); |
| 145 | } |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 146 | } |
| 147 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 148 | pw.print(" mSystemConditionProviders: "); pw.println(mSystemConditionProviderNames); |
| 149 | for (int i = 0; i < mSystemConditionProviders.size(); i++) { |
| 150 | mSystemConditionProviders.valueAt(i).dump(pw, filter); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 151 | } |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 155 | protected IInterface asInterface(IBinder binder) { |
| 156 | return IConditionProvider.Stub.asInterface(binder); |
| 157 | } |
| 158 | |
| 159 | @Override |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 160 | protected boolean checkType(IInterface service) { |
| 161 | return service instanceof IConditionProvider; |
| 162 | } |
| 163 | |
| 164 | @Override |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 165 | public void onBootPhaseAppsCanStart() { |
| 166 | super.onBootPhaseAppsCanStart(); |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame] | 167 | for (int i = 0; i < mSystemConditionProviders.size(); i++) { |
| 168 | mSystemConditionProviders.valueAt(i).onBootComplete(); |
| 169 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 170 | if (mCallback != null) { |
| 171 | mCallback.onBootComplete(); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 172 | } |
John Spurlock | 37bc92c | 2014-11-03 11:01:51 -0500 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 176 | public void onUserSwitched(int user) { |
| 177 | super.onUserSwitched(user); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 178 | if (mCallback != null) { |
| 179 | mCallback.onUserSwitched(); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 180 | } |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 184 | protected void onServiceAdded(ManagedServiceInfo info) { |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 185 | final IConditionProvider provider = provider(info); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 186 | try { |
| 187 | provider.onConnected(); |
| 188 | } catch (RemoteException e) { |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 189 | Slog.e(TAG, "can't connect to service " + info, e); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 190 | // we tried |
| 191 | } |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 192 | if (mCallback != null) { |
| 193 | mCallback.onServiceAdded(info.component); |
| 194 | } |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
| 198 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
| 199 | if (removed == null) return; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 200 | for (int i = mRecords.size() - 1; i >= 0; i--) { |
| 201 | final ConditionRecord r = mRecords.get(i); |
| 202 | if (!r.component.equals(removed.component)) continue; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 203 | mRecords.remove(i); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 207 | @Override |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 208 | public void onPackagesChanged(boolean removingPackage, String[] pkgList, int[] uid) { |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 209 | if (removingPackage) { |
| 210 | INotificationManager inm = NotificationManager.getService(); |
| 211 | |
| 212 | if (pkgList != null && (pkgList.length > 0)) { |
| 213 | for (String pkgName : pkgList) { |
| 214 | try { |
| 215 | inm.removeAutomaticZenRules(pkgName); |
| 216 | inm.setNotificationPolicyAccessGranted(pkgName, false); |
| 217 | } catch (Exception e) { |
| 218 | Slog.e(TAG, "Failed to clean up rules for " + pkgName, e); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 223 | super.onPackagesChanged(removingPackage, pkgList, uid); |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Julia Reynolds | e5ce071bf | 2017-09-15 14:26:32 -0400 | [diff] [blame] | 226 | @Override |
| 227 | protected boolean isValidEntry(String packageOrComponent, int userId) { |
| 228 | return true; |
| 229 | } |
| 230 | |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 231 | @Override |
| 232 | protected String getRequiredPermission() { |
| 233 | return null; |
| 234 | } |
| 235 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 236 | public ManagedServiceInfo checkServiceToken(IConditionProvider provider) { |
| 237 | synchronized(mMutex) { |
| 238 | return checkServiceTokenLocked(provider); |
| 239 | } |
| 240 | } |
| 241 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 242 | private Condition[] removeDuplicateConditions(String pkg, Condition[] conditions) { |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 243 | if (conditions == null || conditions.length == 0) return null; |
| 244 | final int N = conditions.length; |
| 245 | final ArrayMap<Uri, Condition> valid = new ArrayMap<Uri, Condition>(N); |
| 246 | for (int i = 0; i < N; i++) { |
| 247 | final Uri id = conditions[i].id; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 248 | if (valid.containsKey(id)) { |
| 249 | Slog.w(TAG, "Ignoring condition from " + pkg + " for duplicate id: " + id); |
| 250 | continue; |
| 251 | } |
| 252 | valid.put(id, conditions[i]); |
| 253 | } |
| 254 | if (valid.size() == 0) return null; |
| 255 | if (valid.size() == N) return conditions; |
| 256 | final Condition[] rt = new Condition[valid.size()]; |
| 257 | for (int i = 0; i < rt.length; i++) { |
| 258 | rt[i] = valid.valueAt(i); |
| 259 | } |
| 260 | return rt; |
| 261 | } |
| 262 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 263 | private ConditionRecord getRecordLocked(Uri id, ComponentName component, boolean create) { |
| 264 | if (id == null || component == null) return null; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 265 | final int N = mRecords.size(); |
| 266 | for (int i = 0; i < N; i++) { |
| 267 | final ConditionRecord r = mRecords.get(i); |
| 268 | if (r.id.equals(id) && r.component.equals(component)) { |
| 269 | return r; |
| 270 | } |
| 271 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 272 | if (create) { |
| 273 | final ConditionRecord r = new ConditionRecord(id, component); |
| 274 | mRecords.add(r); |
| 275 | return r; |
| 276 | } |
| 277 | return null; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 278 | } |
| 279 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 280 | public void notifyConditions(String pkg, ManagedServiceInfo info, Condition[] conditions) { |
| 281 | synchronized(mMutex) { |
| 282 | if (DEBUG) Slog.d(TAG, "notifyConditions pkg=" + pkg + " info=" + info + " conditions=" |
| 283 | + (conditions == null ? null : Arrays.asList(conditions))); |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 284 | conditions = removeDuplicateConditions(pkg, conditions); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 285 | if (conditions == null || conditions.length == 0) return; |
| 286 | final int N = conditions.length; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 287 | for (int i = 0; i < N; i++) { |
| 288 | final Condition c = conditions[i]; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 289 | final ConditionRecord r = getRecordLocked(c.id, info.component, true /*create*/); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 290 | r.info = info; |
| 291 | r.condition = c; |
Julia Reynolds | 0f50e4e | 2016-02-11 08:54:24 -0500 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | final int N = conditions.length; |
| 295 | for (int i = 0; i < N; i++) { |
| 296 | final Condition c = conditions[i]; |
| 297 | if (mCallback != null) { |
| 298 | mCallback.onConditionChanged(c.id, c); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 303 | public IConditionProvider findConditionProvider(ComponentName component) { |
| 304 | if (component == null) return null; |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 305 | for (ManagedServiceInfo service : getServices()) { |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 306 | if (component.equals(service.component)) { |
| 307 | return provider(service); |
| 308 | } |
| 309 | } |
| 310 | return null; |
| 311 | } |
| 312 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 313 | public Condition findCondition(ComponentName component, Uri conditionId) { |
| 314 | if (component == null || conditionId == null) return null; |
| 315 | synchronized (mMutex) { |
| 316 | final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/); |
| 317 | return r != null ? r.condition : null; |
| 318 | } |
| 319 | } |
| 320 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 321 | public void ensureRecordExists(ComponentName component, Uri conditionId, |
| 322 | IConditionProvider provider) { |
Julia Reynolds | 3ff1fd9 | 2018-08-29 09:57:11 -0400 | [diff] [blame] | 323 | synchronized (mMutex) { |
| 324 | // constructed by convention, make sure the record exists... |
| 325 | final ConditionRecord r = getRecordLocked(conditionId, component, true /*create*/); |
| 326 | if (r.info == null) { |
| 327 | // ... and is associated with the in-process service |
| 328 | r.info = checkServiceTokenLocked(provider); |
| 329 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 333 | public boolean subscribeIfNecessary(ComponentName component, Uri conditionId) { |
| 334 | synchronized (mMutex) { |
| 335 | final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/); |
| 336 | if (r == null) { |
| 337 | Slog.w(TAG, "Unable to subscribe to " + component + " " + conditionId); |
| 338 | return false; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 339 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 340 | if (r.subscribed) return true; |
| 341 | subscribeLocked(r); |
| 342 | return r.subscribed; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | public void unsubscribeIfNecessary(ComponentName component, Uri conditionId) { |
| 347 | synchronized (mMutex) { |
| 348 | final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/); |
| 349 | if (r == null) { |
| 350 | Slog.w(TAG, "Unable to unsubscribe to " + component + " " + conditionId); |
| 351 | return; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 352 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 353 | if (!r.subscribed) return; |
| 354 | unsubscribeLocked(r);; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 358 | private void subscribeLocked(ConditionRecord r) { |
| 359 | if (DEBUG) Slog.d(TAG, "subscribeLocked " + r); |
| 360 | final IConditionProvider provider = provider(r); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 361 | RemoteException re = null; |
| 362 | if (provider != null) { |
| 363 | try { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 364 | Slog.d(TAG, "Subscribing to " + r.id + " with " + r.component); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 365 | provider.onSubscribe(r.id); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 366 | r.subscribed = true; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 367 | } catch (RemoteException e) { |
| 368 | Slog.w(TAG, "Error subscribing to " + r, e); |
| 369 | re = e; |
| 370 | } |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 371 | } |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 372 | ZenLog.traceSubscribe(r != null ? r.id : null, provider, re); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 373 | } |
| 374 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 375 | @SafeVarargs |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 376 | private static <T> ArraySet<T> safeSet(T... items) { |
| 377 | final ArraySet<T> rt = new ArraySet<T>(); |
| 378 | if (items == null || items.length == 0) return rt; |
| 379 | final int N = items.length; |
| 380 | for (int i = 0; i < N; i++) { |
| 381 | final T item = items[i]; |
| 382 | if (item != null) { |
| 383 | rt.add(item); |
| 384 | } |
| 385 | } |
| 386 | return rt; |
| 387 | } |
| 388 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 389 | private void unsubscribeLocked(ConditionRecord r) { |
| 390 | if (DEBUG) Slog.d(TAG, "unsubscribeLocked " + r); |
| 391 | final IConditionProvider provider = provider(r); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 392 | RemoteException re = null; |
| 393 | if (provider != null) { |
| 394 | try { |
| 395 | provider.onUnsubscribe(r.id); |
| 396 | } catch (RemoteException e) { |
| 397 | Slog.w(TAG, "Error unsubscribing to " + r, e); |
| 398 | re = e; |
| 399 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 400 | r.subscribed = false; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 401 | } |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 402 | ZenLog.traceUnsubscribe(r != null ? r.id : null, provider, re); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | private static IConditionProvider provider(ConditionRecord r) { |
| 406 | return r == null ? null : provider(r.info); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | private static IConditionProvider provider(ManagedServiceInfo info) { |
| 410 | return info == null ? null : (IConditionProvider) info.service; |
| 411 | } |
| 412 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 413 | private static class ConditionRecord { |
| 414 | public final Uri id; |
| 415 | public final ComponentName component; |
| 416 | public Condition condition; |
| 417 | public ManagedServiceInfo info; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 418 | public boolean subscribed; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 419 | |
| 420 | private ConditionRecord(Uri id, ComponentName component) { |
| 421 | this.id = id; |
| 422 | this.component = component; |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public String toString() { |
| 427 | final StringBuilder sb = new StringBuilder("ConditionRecord[id=") |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 428 | .append(id).append(",component=").append(component) |
| 429 | .append(",subscribed=").append(subscribed); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 430 | return sb.append(']').toString(); |
| 431 | } |
| 432 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 433 | |
| 434 | public interface Callback { |
| 435 | void onBootComplete(); |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 436 | void onServiceAdded(ComponentName component); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 437 | void onConditionChanged(Uri id, Condition condition); |
| 438 | void onUserSwitched(); |
| 439 | } |
| 440 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 441 | } |