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 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 19 | import android.content.ComponentName; |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 20 | import android.content.ContentResolver; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 21 | import android.content.Context; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 22 | import android.net.Uri; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 23 | import android.os.Handler; |
| 24 | import android.os.IBinder; |
| 25 | import android.os.IInterface; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 26 | import android.os.RemoteException; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 27 | import android.os.UserHandle; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 28 | import android.provider.Settings; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 29 | import android.service.notification.Condition; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 30 | import android.service.notification.ConditionProviderService; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 31 | import android.service.notification.IConditionListener; |
| 32 | import android.service.notification.IConditionProvider; |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [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; |
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 { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 46 | private final ArrayList<ConditionRecord> mRecords = new ArrayList<>(); |
| 47 | private final ArrayMap<IBinder, IConditionListener> mListeners = new ArrayMap<>(); |
| 48 | private final ArraySet<String> mSystemConditionProviderNames; |
| 49 | private final ArraySet<SystemConditionProviderService> mSystemConditionProviders |
| 50 | = new ArraySet<>(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 51 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 52 | private Callback mCallback; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 53 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 54 | public ConditionProviders(Context context, Handler handler, UserProfiles userProfiles) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 55 | super(context, handler, new Object(), userProfiles); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 56 | mSystemConditionProviderNames = safeSet(PropConfig.getStringArray(mContext, |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 57 | "system.condition.providers", |
| 58 | R.array.config_system_condition_providers)); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 59 | } |
| 60 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 61 | public void setCallback(Callback callback) { |
| 62 | mCallback = callback; |
| 63 | } |
| 64 | |
| 65 | public boolean isSystemProviderEnabled(String path) { |
| 66 | return mSystemConditionProviderNames.contains(path); |
| 67 | } |
| 68 | |
| 69 | public void addSystemProvider(SystemConditionProviderService service) { |
| 70 | mSystemConditionProviders.add(service); |
| 71 | service.attachBase(mContext); |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 72 | registerService(service.asInterface(), service.getComponent(), UserHandle.USER_SYSTEM); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | public Iterable<SystemConditionProviderService> getSystemProviders() { |
| 76 | return mSystemConditionProviders; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected Config getConfig() { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 81 | final Config c = new Config(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 82 | c.caption = "condition provider"; |
| 83 | c.serviceInterface = ConditionProviderService.SERVICE_INTERFACE; |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 84 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 85 | c.bindPermission = android.Manifest.permission.BIND_CONDITION_PROVIDER_SERVICE; |
| 86 | c.settingsAction = Settings.ACTION_CONDITION_PROVIDER_SETTINGS; |
| 87 | c.clientLabel = R.string.condition_provider_service_binding_label; |
| 88 | return c; |
| 89 | } |
| 90 | |
| 91 | @Override |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 92 | public void dump(PrintWriter pw, DumpFilter filter) { |
| 93 | super.dump(pw, filter); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 94 | synchronized(mMutex) { |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 95 | pw.print(" mRecords("); pw.print(mRecords.size()); pw.println("):"); |
| 96 | for (int i = 0; i < mRecords.size(); i++) { |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 97 | final ConditionRecord r = mRecords.get(i); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 98 | if (filter != null && !filter.matches(r.component)) continue; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 99 | pw.print(" "); pw.println(r); |
| 100 | final String countdownDesc = CountdownConditionProvider.tryParseDescription(r.id); |
| 101 | if (countdownDesc != null) { |
| 102 | pw.print(" ("); pw.print(countdownDesc); pw.println(")"); |
| 103 | } |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 104 | } |
| 105 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 106 | if (filter == null) { |
| 107 | pw.print(" mListeners("); pw.print(mListeners.size()); pw.println("):"); |
| 108 | for (int i = 0; i < mListeners.size(); i++) { |
| 109 | pw.print(" "); pw.println(mListeners.keyAt(i)); |
| 110 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 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) { |
| 153 | // we tried |
| 154 | } |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 155 | if (mCallback != null) { |
| 156 | mCallback.onServiceAdded(info.component); |
| 157 | } |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
| 161 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
| 162 | if (removed == null) return; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 163 | for (int i = mRecords.size() - 1; i >= 0; i--) { |
| 164 | final ConditionRecord r = mRecords.get(i); |
| 165 | if (!r.component.equals(removed.component)) continue; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 166 | mRecords.remove(i); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | public ManagedServiceInfo checkServiceToken(IConditionProvider provider) { |
| 171 | synchronized(mMutex) { |
| 172 | return checkServiceTokenLocked(provider); |
| 173 | } |
| 174 | } |
| 175 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 176 | private Condition[] validateConditions(String pkg, Condition[] conditions) { |
| 177 | if (conditions == null || conditions.length == 0) return null; |
| 178 | final int N = conditions.length; |
| 179 | final ArrayMap<Uri, Condition> valid = new ArrayMap<Uri, Condition>(N); |
| 180 | for (int i = 0; i < N; i++) { |
| 181 | final Uri id = conditions[i].id; |
| 182 | if (!Condition.isValidId(id, pkg)) { |
| 183 | Slog.w(TAG, "Ignoring condition from " + pkg + " for invalid id: " + id); |
| 184 | continue; |
| 185 | } |
| 186 | if (valid.containsKey(id)) { |
| 187 | Slog.w(TAG, "Ignoring condition from " + pkg + " for duplicate id: " + id); |
| 188 | continue; |
| 189 | } |
| 190 | valid.put(id, conditions[i]); |
| 191 | } |
| 192 | if (valid.size() == 0) return null; |
| 193 | if (valid.size() == N) return conditions; |
| 194 | final Condition[] rt = new Condition[valid.size()]; |
| 195 | for (int i = 0; i < rt.length; i++) { |
| 196 | rt[i] = valid.valueAt(i); |
| 197 | } |
| 198 | return rt; |
| 199 | } |
| 200 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 201 | private ConditionRecord getRecordLocked(Uri id, ComponentName component, boolean create) { |
| 202 | if (id == null || component == null) return null; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 203 | final int N = mRecords.size(); |
| 204 | for (int i = 0; i < N; i++) { |
| 205 | final ConditionRecord r = mRecords.get(i); |
| 206 | if (r.id.equals(id) && r.component.equals(component)) { |
| 207 | return r; |
| 208 | } |
| 209 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 210 | if (create) { |
| 211 | final ConditionRecord r = new ConditionRecord(id, component); |
| 212 | mRecords.add(r); |
| 213 | return r; |
| 214 | } |
| 215 | return null; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 216 | } |
| 217 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 218 | public void notifyConditions(String pkg, ManagedServiceInfo info, Condition[] conditions) { |
| 219 | synchronized(mMutex) { |
| 220 | if (DEBUG) Slog.d(TAG, "notifyConditions pkg=" + pkg + " info=" + info + " conditions=" |
| 221 | + (conditions == null ? null : Arrays.asList(conditions))); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 222 | conditions = validateConditions(pkg, conditions); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 223 | if (conditions == null || conditions.length == 0) return; |
| 224 | final int N = conditions.length; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 225 | for (IConditionListener listener : mListeners.values()) { |
| 226 | try { |
| 227 | listener.onConditionsReceived(conditions); |
| 228 | } catch (RemoteException e) { |
| 229 | Slog.w(TAG, "Error sending conditions to listener " + listener, e); |
| 230 | } |
| 231 | } |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 232 | for (int i = 0; i < N; i++) { |
| 233 | final Condition c = conditions[i]; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 234 | final ConditionRecord r = getRecordLocked(c.id, info.component, true /*create*/); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 235 | r.info = info; |
| 236 | r.condition = c; |
Julia Reynolds | 0f50e4e | 2016-02-11 08:54:24 -0500 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | final int N = conditions.length; |
| 240 | for (int i = 0; i < N; i++) { |
| 241 | final Condition c = conditions[i]; |
| 242 | if (mCallback != null) { |
| 243 | mCallback.onConditionChanged(c.id, c); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 248 | public IConditionProvider findConditionProvider(ComponentName component) { |
| 249 | if (component == null) return null; |
| 250 | for (ManagedServiceInfo service : mServices) { |
| 251 | if (component.equals(service.component)) { |
| 252 | return provider(service); |
| 253 | } |
| 254 | } |
| 255 | return null; |
| 256 | } |
| 257 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 258 | public Condition findCondition(ComponentName component, Uri conditionId) { |
| 259 | if (component == null || conditionId == null) return null; |
| 260 | synchronized (mMutex) { |
| 261 | final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/); |
| 262 | return r != null ? r.condition : null; |
| 263 | } |
| 264 | } |
| 265 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 266 | public void ensureRecordExists(ComponentName component, Uri conditionId, |
| 267 | IConditionProvider provider) { |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 268 | // constructed by convention, make sure the record exists... |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 269 | final ConditionRecord r = getRecordLocked(conditionId, component, true /*create*/); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 270 | if (r.info == null) { |
| 271 | // ... and is associated with the in-process service |
| 272 | r.info = checkServiceTokenLocked(provider); |
| 273 | } |
| 274 | } |
| 275 | |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 276 | @Override |
| 277 | protected ArraySet<ComponentName> loadComponentNamesFromSetting(String settingName, |
| 278 | int userId) { |
| 279 | final ContentResolver cr = mContext.getContentResolver(); |
| 280 | String settingValue = Settings.Secure.getStringForUser( |
| 281 | cr, |
| 282 | settingName, |
| 283 | userId); |
| 284 | if (TextUtils.isEmpty(settingValue)) |
| 285 | return null; |
| 286 | String[] packages = settingValue.split(ENABLED_SERVICES_SEPARATOR); |
| 287 | ArraySet<ComponentName> result = new ArraySet<>(packages.length); |
| 288 | for (int i = 0; i < packages.length; i++) { |
| 289 | if (!TextUtils.isEmpty(packages[i])) { |
| 290 | result.addAll(queryPackageForServices(packages[i], userId)); |
| 291 | } |
| 292 | } |
| 293 | return result; |
| 294 | } |
| 295 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 296 | public boolean subscribeIfNecessary(ComponentName component, Uri conditionId) { |
| 297 | synchronized (mMutex) { |
| 298 | final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/); |
| 299 | if (r == null) { |
| 300 | Slog.w(TAG, "Unable to subscribe to " + component + " " + conditionId); |
| 301 | return false; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 302 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 303 | if (r.subscribed) return true; |
| 304 | subscribeLocked(r); |
| 305 | return r.subscribed; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | public void unsubscribeIfNecessary(ComponentName component, Uri conditionId) { |
| 310 | synchronized (mMutex) { |
| 311 | final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/); |
| 312 | if (r == null) { |
| 313 | Slog.w(TAG, "Unable to unsubscribe to " + component + " " + conditionId); |
| 314 | return; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 315 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 316 | if (!r.subscribed) return; |
| 317 | unsubscribeLocked(r);; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 321 | private void subscribeLocked(ConditionRecord r) { |
| 322 | if (DEBUG) Slog.d(TAG, "subscribeLocked " + r); |
| 323 | final IConditionProvider provider = provider(r); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 324 | RemoteException re = null; |
| 325 | if (provider != null) { |
| 326 | try { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 327 | Slog.d(TAG, "Subscribing to " + r.id + " with " + r.component); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 328 | provider.onSubscribe(r.id); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 329 | r.subscribed = true; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 330 | } catch (RemoteException e) { |
| 331 | Slog.w(TAG, "Error subscribing to " + r, e); |
| 332 | re = e; |
| 333 | } |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 334 | } |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 335 | ZenLog.traceSubscribe(r != null ? r.id : null, provider, re); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 336 | } |
| 337 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 338 | @SafeVarargs |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 339 | private static <T> ArraySet<T> safeSet(T... items) { |
| 340 | final ArraySet<T> rt = new ArraySet<T>(); |
| 341 | if (items == null || items.length == 0) return rt; |
| 342 | final int N = items.length; |
| 343 | for (int i = 0; i < N; i++) { |
| 344 | final T item = items[i]; |
| 345 | if (item != null) { |
| 346 | rt.add(item); |
| 347 | } |
| 348 | } |
| 349 | return rt; |
| 350 | } |
| 351 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 352 | private void unsubscribeLocked(ConditionRecord r) { |
| 353 | if (DEBUG) Slog.d(TAG, "unsubscribeLocked " + r); |
| 354 | final IConditionProvider provider = provider(r); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 355 | RemoteException re = null; |
| 356 | if (provider != null) { |
| 357 | try { |
| 358 | provider.onUnsubscribe(r.id); |
| 359 | } catch (RemoteException e) { |
| 360 | Slog.w(TAG, "Error unsubscribing to " + r, e); |
| 361 | re = e; |
| 362 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 363 | r.subscribed = false; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 364 | } |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 365 | ZenLog.traceUnsubscribe(r != null ? r.id : null, provider, re); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | private static IConditionProvider provider(ConditionRecord r) { |
| 369 | return r == null ? null : provider(r.info); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | private static IConditionProvider provider(ManagedServiceInfo info) { |
| 373 | return info == null ? null : (IConditionProvider) info.service; |
| 374 | } |
| 375 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 376 | private static class ConditionRecord { |
| 377 | public final Uri id; |
| 378 | public final ComponentName component; |
| 379 | public Condition condition; |
| 380 | public ManagedServiceInfo info; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 381 | public boolean subscribed; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 382 | |
| 383 | private ConditionRecord(Uri id, ComponentName component) { |
| 384 | this.id = id; |
| 385 | this.component = component; |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public String toString() { |
| 390 | final StringBuilder sb = new StringBuilder("ConditionRecord[id=") |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 391 | .append(id).append(",component=").append(component) |
| 392 | .append(",subscribed=").append(subscribed); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 393 | return sb.append(']').toString(); |
| 394 | } |
| 395 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 396 | |
| 397 | public interface Callback { |
| 398 | void onBootComplete(); |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 399 | void onServiceAdded(ComponentName component); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 400 | void onConditionChanged(Uri id, Condition condition); |
| 401 | void onUserSwitched(); |
| 402 | } |
| 403 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 404 | } |