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; |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 33 | import android.text.TextUtils; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 34 | import android.util.ArrayMap; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 35 | import android.util.ArraySet; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 36 | import android.util.Slog; |
| 37 | |
| 38 | import com.android.internal.R; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 39 | import com.android.internal.annotations.VisibleForTesting; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 40 | import com.android.server.notification.NotificationManagerService.DumpFilter; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 41 | |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 42 | import org.xmlpull.v1.XmlSerializer; |
| 43 | |
| 44 | import java.io.IOException; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 45 | import java.io.PrintWriter; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 46 | import java.util.ArrayList; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 47 | import java.util.Arrays; |
| 48 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 49 | public class ConditionProviders extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 50 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 51 | @VisibleForTesting |
| 52 | static final String TAG_ENABLED_DND_APPS = "dnd_apps"; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 53 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 54 | private final ArrayList<ConditionRecord> mRecords = new ArrayList<>(); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 55 | private final ArraySet<String> mSystemConditionProviderNames; |
| 56 | private final ArraySet<SystemConditionProviderService> mSystemConditionProviders |
| 57 | = new ArraySet<>(); |
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 |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 198 | protected void loadDefaultsFromConfig() { |
| 199 | String defaultDndAccess = mContext.getResources().getString( |
| 200 | R.string.config_defaultDndAccessPackages); |
| 201 | if (defaultDndAccess != null) { |
| 202 | String[] dnds = defaultDndAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR); |
| 203 | for (int i = 0; i < dnds.length; i++) { |
| 204 | if (TextUtils.isEmpty(dnds[i])) { |
| 205 | continue; |
| 206 | } |
| 207 | addDefaultComponentOrPackage(dnds[i]); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | @Override |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 213 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
| 214 | if (removed == null) return; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 215 | for (int i = mRecords.size() - 1; i >= 0; i--) { |
| 216 | final ConditionRecord r = mRecords.get(i); |
| 217 | if (!r.component.equals(removed.component)) continue; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 218 | mRecords.remove(i); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 222 | @Override |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 223 | public void onPackagesChanged(boolean removingPackage, String[] pkgList, int[] uid) { |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 224 | if (removingPackage) { |
| 225 | INotificationManager inm = NotificationManager.getService(); |
| 226 | |
| 227 | if (pkgList != null && (pkgList.length > 0)) { |
| 228 | for (String pkgName : pkgList) { |
| 229 | try { |
| 230 | inm.removeAutomaticZenRules(pkgName); |
| 231 | inm.setNotificationPolicyAccessGranted(pkgName, false); |
| 232 | } catch (Exception e) { |
| 233 | Slog.e(TAG, "Failed to clean up rules for " + pkgName, e); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 238 | super.onPackagesChanged(removingPackage, pkgList, uid); |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 239 | } |
| 240 | |
Julia Reynolds | e5ce071bf | 2017-09-15 14:26:32 -0400 | [diff] [blame] | 241 | @Override |
| 242 | protected boolean isValidEntry(String packageOrComponent, int userId) { |
| 243 | return true; |
| 244 | } |
| 245 | |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 246 | @Override |
| 247 | protected String getRequiredPermission() { |
| 248 | return null; |
| 249 | } |
| 250 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 251 | public ManagedServiceInfo checkServiceToken(IConditionProvider provider) { |
| 252 | synchronized(mMutex) { |
| 253 | return checkServiceTokenLocked(provider); |
| 254 | } |
| 255 | } |
| 256 | |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 257 | private Condition[] removeDuplicateConditions(String pkg, Condition[] conditions) { |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 258 | if (conditions == null || conditions.length == 0) return null; |
| 259 | final int N = conditions.length; |
| 260 | final ArrayMap<Uri, Condition> valid = new ArrayMap<Uri, Condition>(N); |
| 261 | for (int i = 0; i < N; i++) { |
| 262 | final Uri id = conditions[i].id; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 263 | if (valid.containsKey(id)) { |
| 264 | Slog.w(TAG, "Ignoring condition from " + pkg + " for duplicate id: " + id); |
| 265 | continue; |
| 266 | } |
| 267 | valid.put(id, conditions[i]); |
| 268 | } |
| 269 | if (valid.size() == 0) return null; |
| 270 | if (valid.size() == N) return conditions; |
| 271 | final Condition[] rt = new Condition[valid.size()]; |
| 272 | for (int i = 0; i < rt.length; i++) { |
| 273 | rt[i] = valid.valueAt(i); |
| 274 | } |
| 275 | return rt; |
| 276 | } |
| 277 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 278 | private ConditionRecord getRecordLocked(Uri id, ComponentName component, boolean create) { |
| 279 | if (id == null || component == null) return null; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 280 | final int N = mRecords.size(); |
| 281 | for (int i = 0; i < N; i++) { |
| 282 | final ConditionRecord r = mRecords.get(i); |
| 283 | if (r.id.equals(id) && r.component.equals(component)) { |
| 284 | return r; |
| 285 | } |
| 286 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 287 | if (create) { |
| 288 | final ConditionRecord r = new ConditionRecord(id, component); |
| 289 | mRecords.add(r); |
| 290 | return r; |
| 291 | } |
| 292 | return null; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 293 | } |
| 294 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 295 | public void notifyConditions(String pkg, ManagedServiceInfo info, Condition[] conditions) { |
| 296 | synchronized(mMutex) { |
| 297 | if (DEBUG) Slog.d(TAG, "notifyConditions pkg=" + pkg + " info=" + info + " conditions=" |
| 298 | + (conditions == null ? null : Arrays.asList(conditions))); |
Julia Reynolds | 1d6d16d | 2016-03-07 13:51:02 -0500 | [diff] [blame] | 299 | conditions = removeDuplicateConditions(pkg, conditions); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 300 | if (conditions == null || conditions.length == 0) return; |
| 301 | final int N = conditions.length; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 302 | for (int i = 0; i < N; i++) { |
| 303 | final Condition c = conditions[i]; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 304 | final ConditionRecord r = getRecordLocked(c.id, info.component, true /*create*/); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 305 | r.info = info; |
| 306 | r.condition = c; |
Julia Reynolds | 0f50e4e | 2016-02-11 08:54:24 -0500 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | final int N = conditions.length; |
| 310 | for (int i = 0; i < N; i++) { |
| 311 | final Condition c = conditions[i]; |
| 312 | if (mCallback != null) { |
| 313 | mCallback.onConditionChanged(c.id, c); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 318 | public IConditionProvider findConditionProvider(ComponentName component) { |
| 319 | if (component == null) return null; |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 320 | for (ManagedServiceInfo service : getServices()) { |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 321 | if (component.equals(service.component)) { |
| 322 | return provider(service); |
| 323 | } |
| 324 | } |
| 325 | return null; |
| 326 | } |
| 327 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 328 | public Condition findCondition(ComponentName component, Uri conditionId) { |
| 329 | if (component == null || conditionId == null) return null; |
| 330 | synchronized (mMutex) { |
| 331 | final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/); |
| 332 | return r != null ? r.condition : null; |
| 333 | } |
| 334 | } |
| 335 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 336 | public void ensureRecordExists(ComponentName component, Uri conditionId, |
| 337 | IConditionProvider provider) { |
Julia Reynolds | 3ff1fd9 | 2018-08-29 09:57:11 -0400 | [diff] [blame] | 338 | synchronized (mMutex) { |
| 339 | // constructed by convention, make sure the record exists... |
| 340 | final ConditionRecord r = getRecordLocked(conditionId, component, true /*create*/); |
| 341 | if (r.info == null) { |
| 342 | // ... and is associated with the in-process service |
| 343 | r.info = checkServiceTokenLocked(provider); |
| 344 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 348 | public boolean subscribeIfNecessary(ComponentName component, Uri conditionId) { |
| 349 | synchronized (mMutex) { |
| 350 | final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/); |
| 351 | if (r == null) { |
| 352 | Slog.w(TAG, "Unable to subscribe to " + component + " " + conditionId); |
| 353 | return false; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 354 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 355 | if (r.subscribed) return true; |
| 356 | subscribeLocked(r); |
| 357 | return r.subscribed; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | public void unsubscribeIfNecessary(ComponentName component, Uri conditionId) { |
| 362 | synchronized (mMutex) { |
| 363 | final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/); |
| 364 | if (r == null) { |
| 365 | Slog.w(TAG, "Unable to unsubscribe to " + component + " " + conditionId); |
| 366 | return; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 367 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 368 | if (!r.subscribed) return; |
| 369 | unsubscribeLocked(r);; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 373 | private void subscribeLocked(ConditionRecord r) { |
| 374 | if (DEBUG) Slog.d(TAG, "subscribeLocked " + r); |
| 375 | final IConditionProvider provider = provider(r); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 376 | RemoteException re = null; |
| 377 | if (provider != null) { |
| 378 | try { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 379 | Slog.d(TAG, "Subscribing to " + r.id + " with " + r.component); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 380 | provider.onSubscribe(r.id); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 381 | r.subscribed = true; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 382 | } catch (RemoteException e) { |
| 383 | Slog.w(TAG, "Error subscribing to " + r, e); |
| 384 | re = e; |
| 385 | } |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 386 | } |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 387 | ZenLog.traceSubscribe(r != null ? r.id : null, provider, re); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 388 | } |
| 389 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 390 | @SafeVarargs |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 391 | private static <T> ArraySet<T> safeSet(T... items) { |
| 392 | final ArraySet<T> rt = new ArraySet<T>(); |
| 393 | if (items == null || items.length == 0) return rt; |
| 394 | final int N = items.length; |
| 395 | for (int i = 0; i < N; i++) { |
| 396 | final T item = items[i]; |
| 397 | if (item != null) { |
| 398 | rt.add(item); |
| 399 | } |
| 400 | } |
| 401 | return rt; |
| 402 | } |
| 403 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 404 | private void unsubscribeLocked(ConditionRecord r) { |
| 405 | if (DEBUG) Slog.d(TAG, "unsubscribeLocked " + r); |
| 406 | final IConditionProvider provider = provider(r); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 407 | RemoteException re = null; |
| 408 | if (provider != null) { |
| 409 | try { |
| 410 | provider.onUnsubscribe(r.id); |
| 411 | } catch (RemoteException e) { |
| 412 | Slog.w(TAG, "Error unsubscribing to " + r, e); |
| 413 | re = e; |
| 414 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 415 | r.subscribed = false; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 416 | } |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 417 | ZenLog.traceUnsubscribe(r != null ? r.id : null, provider, re); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | private static IConditionProvider provider(ConditionRecord r) { |
| 421 | return r == null ? null : provider(r.info); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | private static IConditionProvider provider(ManagedServiceInfo info) { |
| 425 | return info == null ? null : (IConditionProvider) info.service; |
| 426 | } |
| 427 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 428 | private static class ConditionRecord { |
| 429 | public final Uri id; |
| 430 | public final ComponentName component; |
| 431 | public Condition condition; |
| 432 | public ManagedServiceInfo info; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 433 | public boolean subscribed; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 434 | |
| 435 | private ConditionRecord(Uri id, ComponentName component) { |
| 436 | this.id = id; |
| 437 | this.component = component; |
| 438 | } |
| 439 | |
| 440 | @Override |
| 441 | public String toString() { |
| 442 | final StringBuilder sb = new StringBuilder("ConditionRecord[id=") |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 443 | .append(id).append(",component=").append(component) |
| 444 | .append(",subscribed=").append(subscribed); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 445 | return sb.append(']').toString(); |
| 446 | } |
| 447 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 448 | |
| 449 | public interface Callback { |
| 450 | void onBootComplete(); |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 451 | void onServiceAdded(ComponentName component); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 452 | void onConditionChanged(Uri id, Condition condition); |
| 453 | void onUserSwitched(); |
| 454 | } |
| 455 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 456 | } |