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 |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 124 | public void onBootPhaseAppsCanStart() { |
| 125 | super.onBootPhaseAppsCanStart(); |
John Spurlock | 2f096ed | 2015-05-04 11:58:26 -0400 | [diff] [blame] | 126 | for (int i = 0; i < mSystemConditionProviders.size(); i++) { |
| 127 | mSystemConditionProviders.valueAt(i).onBootComplete(); |
| 128 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 129 | if (mCallback != null) { |
| 130 | mCallback.onBootComplete(); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 131 | } |
John Spurlock | 37bc92c | 2014-11-03 11:01:51 -0500 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 135 | public void onUserSwitched(int user) { |
| 136 | super.onUserSwitched(user); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 137 | if (mCallback != null) { |
| 138 | mCallback.onUserSwitched(); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 139 | } |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 143 | protected void onServiceAdded(ManagedServiceInfo info) { |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 144 | final IConditionProvider provider = provider(info); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 145 | try { |
| 146 | provider.onConnected(); |
| 147 | } catch (RemoteException e) { |
| 148 | // we tried |
| 149 | } |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 150 | if (mCallback != null) { |
| 151 | mCallback.onServiceAdded(info.component); |
| 152 | } |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
| 157 | if (removed == null) return; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 158 | for (int i = mRecords.size() - 1; i >= 0; i--) { |
| 159 | final ConditionRecord r = mRecords.get(i); |
| 160 | if (!r.component.equals(removed.component)) continue; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 161 | mRecords.remove(i); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | public ManagedServiceInfo checkServiceToken(IConditionProvider provider) { |
| 166 | synchronized(mMutex) { |
| 167 | return checkServiceTokenLocked(provider); |
| 168 | } |
| 169 | } |
| 170 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 171 | public void requestConditions(IConditionListener callback, int relevance) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 172 | synchronized(mMutex) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 173 | if (DEBUG) Slog.d(TAG, "requestConditions callback=" + callback |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 174 | + " relevance=" + Condition.relevanceToString(relevance)); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 175 | if (callback == null) return; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 176 | relevance = relevance & (Condition.FLAG_RELEVANT_NOW | Condition.FLAG_RELEVANT_ALWAYS); |
| 177 | if (relevance != 0) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 178 | mListeners.put(callback.asBinder(), callback); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 179 | requestConditionsLocked(relevance); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 180 | } else { |
| 181 | mListeners.remove(callback.asBinder()); |
| 182 | if (mListeners.isEmpty()) { |
| 183 | requestConditionsLocked(0); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 189 | private Condition[] validateConditions(String pkg, Condition[] conditions) { |
| 190 | if (conditions == null || conditions.length == 0) return null; |
| 191 | final int N = conditions.length; |
| 192 | final ArrayMap<Uri, Condition> valid = new ArrayMap<Uri, Condition>(N); |
| 193 | for (int i = 0; i < N; i++) { |
| 194 | final Uri id = conditions[i].id; |
| 195 | if (!Condition.isValidId(id, pkg)) { |
| 196 | Slog.w(TAG, "Ignoring condition from " + pkg + " for invalid id: " + id); |
| 197 | continue; |
| 198 | } |
| 199 | if (valid.containsKey(id)) { |
| 200 | Slog.w(TAG, "Ignoring condition from " + pkg + " for duplicate id: " + id); |
| 201 | continue; |
| 202 | } |
| 203 | valid.put(id, conditions[i]); |
| 204 | } |
| 205 | if (valid.size() == 0) return null; |
| 206 | if (valid.size() == N) return conditions; |
| 207 | final Condition[] rt = new Condition[valid.size()]; |
| 208 | for (int i = 0; i < rt.length; i++) { |
| 209 | rt[i] = valid.valueAt(i); |
| 210 | } |
| 211 | return rt; |
| 212 | } |
| 213 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 214 | private ConditionRecord getRecordLocked(Uri id, ComponentName component, boolean create) { |
| 215 | if (id == null || component == null) return null; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 216 | final int N = mRecords.size(); |
| 217 | for (int i = 0; i < N; i++) { |
| 218 | final ConditionRecord r = mRecords.get(i); |
| 219 | if (r.id.equals(id) && r.component.equals(component)) { |
| 220 | return r; |
| 221 | } |
| 222 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 223 | if (create) { |
| 224 | final ConditionRecord r = new ConditionRecord(id, component); |
| 225 | mRecords.add(r); |
| 226 | return r; |
| 227 | } |
| 228 | return null; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 229 | } |
| 230 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 231 | public void notifyConditions(String pkg, ManagedServiceInfo info, Condition[] conditions) { |
| 232 | synchronized(mMutex) { |
| 233 | if (DEBUG) Slog.d(TAG, "notifyConditions pkg=" + pkg + " info=" + info + " conditions=" |
| 234 | + (conditions == null ? null : Arrays.asList(conditions))); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 235 | conditions = validateConditions(pkg, conditions); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 236 | if (conditions == null || conditions.length == 0) return; |
| 237 | final int N = conditions.length; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 238 | for (IConditionListener listener : mListeners.values()) { |
| 239 | try { |
| 240 | listener.onConditionsReceived(conditions); |
| 241 | } catch (RemoteException e) { |
| 242 | Slog.w(TAG, "Error sending conditions to listener " + listener, e); |
| 243 | } |
| 244 | } |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 245 | for (int i = 0; i < N; i++) { |
| 246 | final Condition c = conditions[i]; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 247 | final ConditionRecord r = getRecordLocked(c.id, info.component, true /*create*/); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 248 | r.info = info; |
| 249 | r.condition = c; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 250 | if (mCallback != null) { |
| 251 | mCallback.onConditionChanged(c.id, c); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 257 | public IConditionProvider findConditionProvider(ComponentName component) { |
| 258 | if (component == null) return null; |
| 259 | for (ManagedServiceInfo service : mServices) { |
| 260 | if (component.equals(service.component)) { |
| 261 | return provider(service); |
| 262 | } |
| 263 | } |
| 264 | return null; |
| 265 | } |
| 266 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 267 | public Condition findCondition(ComponentName component, Uri conditionId) { |
| 268 | if (component == null || conditionId == null) return null; |
| 269 | synchronized (mMutex) { |
| 270 | final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/); |
| 271 | return r != null ? r.condition : null; |
| 272 | } |
| 273 | } |
| 274 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 275 | public void ensureRecordExists(ComponentName component, Uri conditionId, |
| 276 | IConditionProvider provider) { |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 277 | // constructed by convention, make sure the record exists... |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 278 | final ConditionRecord r = getRecordLocked(conditionId, component, true /*create*/); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 279 | if (r.info == null) { |
| 280 | // ... and is associated with the in-process service |
| 281 | r.info = checkServiceTokenLocked(provider); |
| 282 | } |
| 283 | } |
| 284 | |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame^] | 285 | @Override |
| 286 | protected ArraySet<ComponentName> loadComponentNamesFromSetting(String settingName, |
| 287 | int userId) { |
| 288 | final ContentResolver cr = mContext.getContentResolver(); |
| 289 | String settingValue = Settings.Secure.getStringForUser( |
| 290 | cr, |
| 291 | settingName, |
| 292 | userId); |
| 293 | if (TextUtils.isEmpty(settingValue)) |
| 294 | return null; |
| 295 | String[] packages = settingValue.split(ENABLED_SERVICES_SEPARATOR); |
| 296 | ArraySet<ComponentName> result = new ArraySet<>(packages.length); |
| 297 | for (int i = 0; i < packages.length; i++) { |
| 298 | if (!TextUtils.isEmpty(packages[i])) { |
| 299 | result.addAll(queryPackageForServices(packages[i], userId)); |
| 300 | } |
| 301 | } |
| 302 | return result; |
| 303 | } |
| 304 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 305 | public boolean subscribeIfNecessary(ComponentName component, Uri conditionId) { |
| 306 | synchronized (mMutex) { |
| 307 | final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/); |
| 308 | if (r == null) { |
| 309 | Slog.w(TAG, "Unable to subscribe to " + component + " " + conditionId); |
| 310 | return false; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 311 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 312 | if (r.subscribed) return true; |
| 313 | subscribeLocked(r); |
| 314 | return r.subscribed; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | public void unsubscribeIfNecessary(ComponentName component, Uri conditionId) { |
| 319 | synchronized (mMutex) { |
| 320 | final ConditionRecord r = getRecordLocked(conditionId, component, false /*create*/); |
| 321 | if (r == null) { |
| 322 | Slog.w(TAG, "Unable to unsubscribe to " + component + " " + conditionId); |
| 323 | return; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 324 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 325 | if (!r.subscribed) return; |
| 326 | unsubscribeLocked(r);; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 330 | private void subscribeLocked(ConditionRecord r) { |
| 331 | if (DEBUG) Slog.d(TAG, "subscribeLocked " + r); |
| 332 | final IConditionProvider provider = provider(r); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 333 | RemoteException re = null; |
| 334 | if (provider != null) { |
| 335 | try { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 336 | Slog.d(TAG, "Subscribing to " + r.id + " with " + r.component); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 337 | provider.onSubscribe(r.id); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 338 | r.subscribed = true; |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 339 | } catch (RemoteException e) { |
| 340 | Slog.w(TAG, "Error subscribing to " + r, e); |
| 341 | re = e; |
| 342 | } |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 343 | } |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 344 | ZenLog.traceSubscribe(r != null ? r.id : null, provider, re); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 345 | } |
| 346 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 347 | @SafeVarargs |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 348 | private static <T> ArraySet<T> safeSet(T... items) { |
| 349 | final ArraySet<T> rt = new ArraySet<T>(); |
| 350 | if (items == null || items.length == 0) return rt; |
| 351 | final int N = items.length; |
| 352 | for (int i = 0; i < N; i++) { |
| 353 | final T item = items[i]; |
| 354 | if (item != null) { |
| 355 | rt.add(item); |
| 356 | } |
| 357 | } |
| 358 | return rt; |
| 359 | } |
| 360 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 361 | private void unsubscribeLocked(ConditionRecord r) { |
| 362 | if (DEBUG) Slog.d(TAG, "unsubscribeLocked " + r); |
| 363 | final IConditionProvider provider = provider(r); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 364 | RemoteException re = null; |
| 365 | if (provider != null) { |
| 366 | try { |
| 367 | provider.onUnsubscribe(r.id); |
| 368 | } catch (RemoteException e) { |
| 369 | Slog.w(TAG, "Error unsubscribing to " + r, e); |
| 370 | re = e; |
| 371 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 372 | r.subscribed = false; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 373 | } |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 374 | ZenLog.traceUnsubscribe(r != null ? r.id : null, provider, re); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | private static IConditionProvider provider(ConditionRecord r) { |
| 378 | return r == null ? null : provider(r.info); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | private static IConditionProvider provider(ManagedServiceInfo info) { |
| 382 | return info == null ? null : (IConditionProvider) info.service; |
| 383 | } |
| 384 | |
| 385 | private void requestConditionsLocked(int flags) { |
| 386 | for (ManagedServiceInfo info : mServices) { |
| 387 | final IConditionProvider provider = provider(info); |
| 388 | if (provider == null) continue; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 389 | // clear all stored conditions from this provider that we no longer care about |
| 390 | for (int i = mRecords.size() - 1; i >= 0; i--) { |
| 391 | final ConditionRecord r = mRecords.get(i); |
| 392 | if (r.info != info) continue; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 393 | if (r.subscribed) continue; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 394 | mRecords.remove(i); |
| 395 | } |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 396 | try { |
| 397 | provider.onRequestConditions(flags); |
| 398 | } catch (RemoteException e) { |
| 399 | Slog.w(TAG, "Error requesting conditions from " + info.component, e); |
| 400 | } |
| 401 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 402 | } |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 403 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 404 | private static class ConditionRecord { |
| 405 | public final Uri id; |
| 406 | public final ComponentName component; |
| 407 | public Condition condition; |
| 408 | public ManagedServiceInfo info; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 409 | public boolean subscribed; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 410 | |
| 411 | private ConditionRecord(Uri id, ComponentName component) { |
| 412 | this.id = id; |
| 413 | this.component = component; |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | public String toString() { |
| 418 | final StringBuilder sb = new StringBuilder("ConditionRecord[id=") |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 419 | .append(id).append(",component=").append(component) |
| 420 | .append(",subscribed=").append(subscribed); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 421 | return sb.append(']').toString(); |
| 422 | } |
| 423 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 424 | |
| 425 | public interface Callback { |
| 426 | void onBootComplete(); |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 427 | void onServiceAdded(ComponentName component); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 428 | void onConditionChanged(Uri id, Condition condition); |
| 429 | void onUserSwitched(); |
| 430 | } |
| 431 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 432 | } |