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