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