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