ConditionProviderService.java revision 43b70cdc395d3a6cf3bd0a78b686a7f5d3ed86e0
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 17package android.service.notification; 18 19import android.annotation.SdkConstant; 20import android.app.INotificationManager; 21import android.app.Service; 22import android.content.ComponentName; 23import android.content.Context; 24import android.content.Intent; 25import android.net.Uri; 26import android.os.Handler; 27import android.os.IBinder; 28import android.os.Message; 29import android.os.ServiceManager; 30import android.util.Log; 31 32/** 33 * A service that provides conditions about boolean state. 34 * <p>To extend this class, you must declare the service in your manifest file with 35 * the {@link android.Manifest.permission#BIND_CONDITION_PROVIDER_SERVICE} permission 36 * and include an intent filter with the {@link #SERVICE_INTERFACE} action. If you want users to be 37 * able to create and update conditions for this service to monitor, include the 38 * {@link #META_DATA_RULE_TYPE} and {@link #META_DATA_CONFIGURATION_ACTIVITY} tags and request the 39 * {@link android.Manifest.permission#ACCESS_NOTIFICATION_POLICY} permission. For example:</p> 40 * <pre> 41 * <service android:name=".MyConditionProvider" 42 * android:label="@string/service_name" 43 * android:permission="android.permission.BIND_CONDITION_PROVIDER_SERVICE"> 44 * <intent-filter> 45 * <action android:name="android.service.notification.ConditionProviderService" /> 46 * </intent-filter> 47 * <meta-data 48 * android:name="android.service.zen.automatic.ruleType" 49 * android:value="@string/my_condition_rule"> 50 * </meta-data> 51 * <meta-data 52 * android:name="android.service.zen.automatic.configurationActivity" 53 * android:value="com.my.package/.MyConditionConfigurationActivity"> 54 * </meta-data> 55 * </service></pre> 56 * 57 */ 58public abstract class ConditionProviderService extends Service { 59 private final String TAG = ConditionProviderService.class.getSimpleName() 60 + "[" + getClass().getSimpleName() + "]"; 61 62 private final H mHandler = new H(); 63 64 private Provider mProvider; 65 private INotificationManager mNoMan; 66 67 /** 68 * The {@link Intent} that must be declared as handled by the service. 69 */ 70 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) 71 public static final String SERVICE_INTERFACE 72 = "android.service.notification.ConditionProviderService"; 73 74 /** 75 * The name of the {@code meta-data} tag containing a localized name of the type of zen rules 76 * provided by this service. 77 */ 78 public static final String META_DATA_RULE_TYPE = "android.service.zen.automatic.ruleType"; 79 80 /** 81 * The name of the {@code meta-data} tag containing the {@link ComponentName} of an activity 82 * that allows users to configure the conditions provided by this service. 83 */ 84 public static final String META_DATA_CONFIGURATION_ACTIVITY = 85 "android.service.zen.automatic.configurationActivity"; 86 87 /** 88 * The name of the {@code meta-data} tag containing the maximum number of rule instances that 89 * can be created for this rule type. Omit or enter a value <= 0 to allow unlimited instances. 90 */ 91 public static final String META_DATA_RULE_INSTANCE_LIMIT = 92 "android.service.zen.automatic.ruleInstanceLimit"; 93 94 /** 95 * A String rule id extra passed to {@link #META_DATA_CONFIGURATION_ACTIVITY}. 96 */ 97 public static final String EXTRA_RULE_ID = "android.content.automatic.ruleId"; 98 99 /** 100 * Called when this service is connected. 101 */ 102 abstract public void onConnected(); 103 104 /** 105 * Called when the system wants to know the state of Conditions managed by this provider. 106 * 107 * Implementations should evaluate the state of all subscribed conditions, and provide updates 108 * by calling {@link #notifyCondition(Condition)} or {@link #notifyConditions(Condition...)}. 109 * @param relevance 110 */ 111 abstract public void onRequestConditions(int relevance); 112 113 /** 114 * Called by the system when there is a new {@link Condition} to be managed by this provider. 115 * @param conditionId the Uri describing the criteria of the condition. 116 */ 117 abstract public void onSubscribe(Uri conditionId); 118 119 /** 120 * Called by the system when a {@link Condition} has been deleted. 121 * @param conditionId the Uri describing the criteria of the deleted condition. 122 */ 123 abstract public void onUnsubscribe(Uri conditionId); 124 125 private final INotificationManager getNotificationInterface() { 126 if (mNoMan == null) { 127 mNoMan = INotificationManager.Stub.asInterface( 128 ServiceManager.getService(Context.NOTIFICATION_SERVICE)); 129 } 130 return mNoMan; 131 } 132 133 /** 134 * Informs the notification manager that the state of a Condition has changed. 135 * @param condition the condition that has changed. 136 */ 137 public final void notifyCondition(Condition condition) { 138 if (condition == null) return; 139 notifyConditions(new Condition[]{ condition }); 140 } 141 142 /** 143 * Informs the notification manager that the state of one or more Conditions has changed. 144 * @param conditions the changed conditions. 145 */ 146 public final void notifyConditions(Condition... conditions) { 147 if (!isBound() || conditions == null) return; 148 try { 149 getNotificationInterface().notifyConditions(getPackageName(), mProvider, conditions); 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 { 172 @Override 173 public void onConnected() { 174 mHandler.obtainMessage(H.ON_CONNECTED).sendToTarget(); 175 } 176 177 @Override 178 public void onSubscribe(Uri conditionId) { 179 mHandler.obtainMessage(H.ON_SUBSCRIBE, conditionId).sendToTarget(); 180 } 181 182 @Override 183 public void onUnsubscribe(Uri conditionId) { 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; 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; 196 try { 197 switch(msg.what) { 198 case ON_CONNECTED: 199 name = "onConnected"; 200 onConnected(); 201 break; 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 } 211 } catch (Throwable t) { 212 Log.w(TAG, "Error running " + name, t); 213 } 214 } 215 } 216} 217