blob: 425e22200d17444ed32eafe3e07a07f05fe8e5db [file] [log] [blame]
John Spurlockd60258f2015-04-30 09:30:52 -04001/*
2 * Copyright (C) 2015 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
17package com.android.server.notification;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.net.Uri;
22import android.service.notification.Condition;
23import android.service.notification.IConditionProvider;
24import android.service.notification.ZenModeConfig;
25import android.util.ArraySet;
26import android.util.Log;
27import android.util.Slog;
28
29import com.android.server.notification.NotificationManagerService.DumpFilter;
30
31import java.io.PrintWriter;
32
33/**
34 * Built-in zen condition provider for calendar event-based conditions.
35 */
36public class EventConditionProvider extends SystemConditionProviderService {
37 private static final String TAG = "ConditionProviders";
38 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
39
40 public static final ComponentName COMPONENT =
41 new ComponentName("android", EventConditionProvider.class.getName());
42 private static final String NOT_SHOWN = "...";
43
44 private final ArraySet<Uri> mSubscriptions = new ArraySet<Uri>();
45
46 private boolean mConnected;
47 private boolean mRegistered;
48
49 public EventConditionProvider() {
50 if (DEBUG) Slog.d(TAG, "new EventConditionProvider()");
51 }
52
53 @Override
54 public ComponentName getComponent() {
55 return COMPONENT;
56 }
57
58 @Override
59 public boolean isValidConditionId(Uri id) {
60 return ZenModeConfig.isValidEventConditionId(id);
61 }
62
63 @Override
64 public void dump(PrintWriter pw, DumpFilter filter) {
65 pw.println(" EventConditionProvider:");
66 pw.print(" mConnected="); pw.println(mConnected);
67 pw.print(" mRegistered="); pw.println(mRegistered);
68 pw.println(" mSubscriptions=");
69 for (Uri conditionId : mSubscriptions) {
70 pw.print(" ");
71 pw.println(conditionId);
72 }
73 }
74
75 @Override
76 public void onConnected() {
77 if (DEBUG) Slog.d(TAG, "onConnected");
78 mConnected = true;
79 }
80
81 @Override
82 public void onDestroy() {
83 super.onDestroy();
84 if (DEBUG) Slog.d(TAG, "onDestroy");
85 mConnected = false;
86 }
87
88 @Override
89 public void onRequestConditions(int relevance) {
90 if (DEBUG) Slog.d(TAG, "onRequestConditions relevance=" + relevance);
91 // does not advertise conditions
92 }
93
94 @Override
95 public void onSubscribe(Uri conditionId) {
96 if (DEBUG) Slog.d(TAG, "onSubscribe " + conditionId);
97 if (!ZenModeConfig.isValidEventConditionId(conditionId)) {
98 notifyCondition(conditionId, Condition.STATE_FALSE, "badCondition");
99 return;
100 }
101 mSubscriptions.add(conditionId);
102 evaluateSubscriptions();
103 }
104
105 @Override
106 public void onUnsubscribe(Uri conditionId) {
107 if (DEBUG) Slog.d(TAG, "onUnsubscribe " + conditionId);
108 if (mSubscriptions.remove(conditionId)) {
109 evaluateSubscriptions();
110 }
111 }
112
113 @Override
114 public void attachBase(Context base) {
115 attachBaseContext(base);
116 }
117
118 @Override
119 public IConditionProvider asInterface() {
120 return (IConditionProvider) onBind(null);
121 }
122
123 private void evaluateSubscriptions() {
124 for (Uri conditionId : mSubscriptions) {
125 notifyCondition(conditionId, Condition.STATE_FALSE, "notImplemented");
126 }
127 }
128
129 private void notifyCondition(Uri conditionId, int state, String reason) {
130 if (DEBUG) Slog.d(TAG, "notifyCondition " + Condition.stateToString(state)
131 + " reason=" + reason);
132 notifyCondition(createCondition(conditionId, state));
133 }
134
135 private Condition createCondition(Uri id, int state) {
136 final String summary = NOT_SHOWN;
137 final String line1 = NOT_SHOWN;
138 final String line2 = NOT_SHOWN;
139 return new Condition(id, summary, line1, line2, 0, state, Condition.FLAG_RELEVANT_ALWAYS);
140 }
141
142}