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 android.service.notification; |
| 18 | |
| 19 | import android.annotation.SdkConstant; |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 20 | import android.annotation.SystemApi; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 21 | import android.app.INotificationManager; |
| 22 | import android.app.Service; |
Julia Reynolds | b0a773f | 2015-08-25 15:01:22 -0400 | [diff] [blame] | 23 | import android.content.ComponentName; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.net.Uri; |
John Spurlock | e86de4c | 2014-04-29 21:48:26 -0400 | [diff] [blame] | 27 | import android.os.Handler; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 28 | import android.os.IBinder; |
John Spurlock | e86de4c | 2014-04-29 21:48:26 -0400 | [diff] [blame] | 29 | import android.os.Message; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 30 | import android.os.ServiceManager; |
| 31 | import android.util.Log; |
| 32 | |
| 33 | /** |
| 34 | * A service that provides conditions about boolean state. |
| 35 | * <p>To extend this class, you must declare the service in your manifest file with |
| 36 | * the {@link android.Manifest.permission#BIND_CONDITION_PROVIDER_SERVICE} permission |
Julia Reynolds | b0a773f | 2015-08-25 15:01:22 -0400 | [diff] [blame] | 37 | * and include an intent filter with the {@link #SERVICE_INTERFACE} action. If you want users to be |
| 38 | * able to create and update conditions for this service to monitor, include the |
Julia Reynolds | c279b99 | 2015-10-30 08:23:51 -0400 | [diff] [blame] | 39 | * {@link #META_DATA_RULE_TYPE} and {@link #META_DATA_CONFIGURATION_ACTIVITY} tags and request the |
| 40 | * {@link android.Manifest.permission#ACCESS_NOTIFICATION_POLICY} permission. For example:</p> |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 41 | * <pre> |
| 42 | * <service android:name=".MyConditionProvider" |
| 43 | * android:label="@string/service_name" |
| 44 | * android:permission="android.permission.BIND_CONDITION_PROVIDER_SERVICE"> |
| 45 | * <intent-filter> |
| 46 | * <action android:name="android.service.notification.ConditionProviderService" /> |
| 47 | * </intent-filter> |
Julia Reynolds | b0a773f | 2015-08-25 15:01:22 -0400 | [diff] [blame] | 48 | * <meta-data |
| 49 | * android:name="android.service.zen.automatic.ruleType" |
| 50 | * android:value="@string/my_condition_rule"> |
| 51 | * </meta-data> |
| 52 | * <meta-data |
Julia Reynolds | b0a773f | 2015-08-25 15:01:22 -0400 | [diff] [blame] | 53 | * android:name="android.service.zen.automatic.configurationActivity" |
| 54 | * android:value="com.my.package/.MyConditionConfigurationActivity"> |
| 55 | * </meta-data> |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 56 | * </service></pre> |
| 57 | * |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 58 | */ |
| 59 | public abstract class ConditionProviderService extends Service { |
| 60 | private final String TAG = ConditionProviderService.class.getSimpleName() |
| 61 | + "[" + getClass().getSimpleName() + "]"; |
| 62 | |
John Spurlock | e86de4c | 2014-04-29 21:48:26 -0400 | [diff] [blame] | 63 | private final H mHandler = new H(); |
| 64 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 65 | private Provider mProvider; |
| 66 | private INotificationManager mNoMan; |
| 67 | |
| 68 | /** |
| 69 | * The {@link Intent} that must be declared as handled by the service. |
| 70 | */ |
| 71 | @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) |
| 72 | public static final String SERVICE_INTERFACE |
| 73 | = "android.service.notification.ConditionProviderService"; |
| 74 | |
Julia Reynolds | b0a773f | 2015-08-25 15:01:22 -0400 | [diff] [blame] | 75 | /** |
| 76 | * The name of the {@code meta-data} tag containing a localized name of the type of zen rules |
| 77 | * provided by this service. |
| 78 | */ |
| 79 | public static final String META_DATA_RULE_TYPE = "android.service.zen.automatic.ruleType"; |
| 80 | |
| 81 | /** |
Julia Reynolds | b0a773f | 2015-08-25 15:01:22 -0400 | [diff] [blame] | 82 | * The name of the {@code meta-data} tag containing the {@link ComponentName} of an activity |
| 83 | * that allows users to configure the conditions provided by this service. |
| 84 | */ |
| 85 | public static final String META_DATA_CONFIGURATION_ACTIVITY = |
| 86 | "android.service.zen.automatic.configurationActivity"; |
| 87 | |
| 88 | /** |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 89 | * The name of the {@code meta-data} tag containing the maximum number of rule instances that |
| 90 | * can be created for this rule type. Omit or enter a value <= 0 to allow unlimited instances. |
| 91 | */ |
| 92 | public static final String META_DATA_RULE_INSTANCE_LIMIT = |
| 93 | "android.service.zen.automatic.ruleInstanceLimit"; |
| 94 | |
| 95 | /** |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 96 | * A String rule id extra passed to {@link #META_DATA_CONFIGURATION_ACTIVITY}. |
Julia Reynolds | b0a773f | 2015-08-25 15:01:22 -0400 | [diff] [blame] | 97 | */ |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 98 | public static final String EXTRA_RULE_ID = "android.content.automatic.ruleId"; |
Julia Reynolds | b0a773f | 2015-08-25 15:01:22 -0400 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Called when this service is connected. |
| 102 | */ |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 103 | abstract public void onConnected(); |
Julia Reynolds | b0a773f | 2015-08-25 15:01:22 -0400 | [diff] [blame] | 104 | |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 105 | @SystemApi |
| 106 | public void onRequestConditions(int relevance) {} |
Julia Reynolds | b0a773f | 2015-08-25 15:01:22 -0400 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Called by the system when there is a new {@link Condition} to be managed by this provider. |
| 110 | * @param conditionId the Uri describing the criteria of the condition. |
| 111 | */ |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 112 | abstract public void onSubscribe(Uri conditionId); |
Julia Reynolds | b0a773f | 2015-08-25 15:01:22 -0400 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * Called by the system when a {@link Condition} has been deleted. |
| 116 | * @param conditionId the Uri describing the criteria of the deleted condition. |
| 117 | */ |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 118 | abstract public void onUnsubscribe(Uri conditionId); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 119 | |
| 120 | private final INotificationManager getNotificationInterface() { |
| 121 | if (mNoMan == null) { |
| 122 | mNoMan = INotificationManager.Stub.asInterface( |
| 123 | ServiceManager.getService(Context.NOTIFICATION_SERVICE)); |
| 124 | } |
| 125 | return mNoMan; |
| 126 | } |
| 127 | |
Julia Reynolds | b0a773f | 2015-08-25 15:01:22 -0400 | [diff] [blame] | 128 | /** |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 129 | * Informs the notification manager that the state of a Condition has changed. Use this method |
| 130 | * to put the system into Do Not Disturb mode or request that it exits Do Not Disturb mode. This |
| 131 | * call will be ignored unless there is an enabled {@link android.app.AutomaticZenRule} owned by |
| 132 | * service that has an {@link android.app.AutomaticZenRule#getConditionId()} equal to this |
| 133 | * {@link Condition#id}. |
Julia Reynolds | b0a773f | 2015-08-25 15:01:22 -0400 | [diff] [blame] | 134 | * @param condition the condition that has changed. |
| 135 | */ |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 136 | public final void notifyCondition(Condition condition) { |
| 137 | if (condition == null) return; |
| 138 | notifyConditions(new Condition[]{ condition }); |
| 139 | } |
| 140 | |
Julia Reynolds | b0a773f | 2015-08-25 15:01:22 -0400 | [diff] [blame] | 141 | /** |
Julia Reynolds | a62496d | 2016-01-29 15:29:16 -0500 | [diff] [blame] | 142 | * Informs the notification manager that the state of one or more Conditions has changed. See |
| 143 | * {@link #notifyCondition(Condition)} for restrictions. |
Julia Reynolds | b0a773f | 2015-08-25 15:01:22 -0400 | [diff] [blame] | 144 | * @param conditions the changed conditions. |
| 145 | */ |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 146 | public final void notifyConditions(Condition... conditions) { |
| 147 | if (!isBound() || conditions == null) return; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 148 | try { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 149 | getNotificationInterface().notifyConditions(getPackageName(), mProvider, conditions); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 150 | } catch (android.os.RemoteException ex) { |
| 151 | Log.v(TAG, "Unable to contact notification manager", ex); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public IBinder onBind(Intent intent) { |
| 157 | if (mProvider == null) { |
| 158 | mProvider = new Provider(); |
| 159 | } |
| 160 | return mProvider; |
| 161 | } |
| 162 | |
| 163 | private boolean isBound() { |
| 164 | if (mProvider == null) { |
| 165 | Log.w(TAG, "Condition provider service not yet bound."); |
| 166 | return false; |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | private final class Provider extends IConditionProvider.Stub { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 172 | @Override |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 173 | public void onConnected() { |
John Spurlock | e86de4c | 2014-04-29 21:48:26 -0400 | [diff] [blame] | 174 | mHandler.obtainMessage(H.ON_CONNECTED).sendToTarget(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 178 | public void onSubscribe(Uri conditionId) { |
John Spurlock | e86de4c | 2014-04-29 21:48:26 -0400 | [diff] [blame] | 179 | mHandler.obtainMessage(H.ON_SUBSCRIBE, conditionId).sendToTarget(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 183 | public void onUnsubscribe(Uri conditionId) { |
John Spurlock | e86de4c | 2014-04-29 21:48:26 -0400 | [diff] [blame] | 184 | mHandler.obtainMessage(H.ON_UNSUBSCRIBE, conditionId).sendToTarget(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | private final class H extends Handler { |
| 189 | private static final int ON_CONNECTED = 1; |
John Spurlock | e86de4c | 2014-04-29 21:48:26 -0400 | [diff] [blame] | 190 | private static final int ON_SUBSCRIBE = 3; |
| 191 | private static final int ON_UNSUBSCRIBE = 4; |
| 192 | |
| 193 | @Override |
| 194 | public void handleMessage(Message msg) { |
| 195 | String name = null; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 196 | try { |
John Spurlock | e86de4c | 2014-04-29 21:48:26 -0400 | [diff] [blame] | 197 | switch(msg.what) { |
| 198 | case ON_CONNECTED: |
| 199 | name = "onConnected"; |
| 200 | onConnected(); |
| 201 | break; |
John Spurlock | e86de4c | 2014-04-29 21:48:26 -0400 | [diff] [blame] | 202 | case ON_SUBSCRIBE: |
| 203 | name = "onSubscribe"; |
| 204 | onSubscribe((Uri)msg.obj); |
| 205 | break; |
| 206 | case ON_UNSUBSCRIBE: |
| 207 | name = "onUnsubscribe"; |
| 208 | onUnsubscribe((Uri)msg.obj); |
| 209 | break; |
| 210 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 211 | } catch (Throwable t) { |
John Spurlock | e86de4c | 2014-04-29 21:48:26 -0400 | [diff] [blame] | 212 | Log.w(TAG, "Error running " + name, t); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |