1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.service.notification;
import android.annotation.SdkConstant;
import android.annotation.TestApi;
import android.app.ActivityManager;
import android.app.INotificationManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
/**
* A service that provides conditions about boolean state.
* <p>To extend this class, you must declare the service in your manifest file with
* the {@link android.Manifest.permission#BIND_CONDITION_PROVIDER_SERVICE} permission
* and include an intent filter with the {@link #SERVICE_INTERFACE} action. If you want users to be
* able to create and update conditions for this service to monitor, include the
* {@link #META_DATA_RULE_TYPE} and {@link #META_DATA_CONFIGURATION_ACTIVITY} tags and request the
* {@link android.Manifest.permission#ACCESS_NOTIFICATION_POLICY} permission. For example:</p>
* <pre>
* <service android:name=".MyConditionProvider"
* android:label="@string/service_name"
* android:permission="android.permission.BIND_CONDITION_PROVIDER_SERVICE">
* <intent-filter>
* <action android:name="android.service.notification.ConditionProviderService" />
* </intent-filter>
* <meta-data
* android:name="android.service.zen.automatic.ruleType"
* android:value="@string/my_condition_rule">
* </meta-data>
* <meta-data
* android:name="android.service.zen.automatic.configurationActivity"
* android:value="com.my.package/.MyConditionConfigurationActivity">
* </meta-data>
* </service></pre>
*
* <p> Condition providers cannot be bound by the system on
* {@link ActivityManager#isLowRamDevice() low ram} devices</p>
*
* @deprecated Instead of using an automatically bound service, use
* {@link android.app.NotificationManager#setAutomaticZenRuleState(String, Condition)} to tell the
* system about the state of your rule. In order to maintain a link from
* Settings to your rule configuration screens, provide a configuration activity that handles
* {@link android.app.NotificationManager#ACTION_AUTOMATIC_ZEN_RULE} on your
* {@link android.app.AutomaticZenRule} via
* {@link android.app.AutomaticZenRule#setConfigurationActivity(ComponentName)}.
*/
@Deprecated
public abstract class ConditionProviderService extends Service {
private final String TAG = ConditionProviderService.class.getSimpleName()
+ "[" + getClass().getSimpleName() + "]";
private final H mHandler = new H();
private Provider mProvider;
private INotificationManager mNoMan;
boolean mIsConnected;
/**
* The {@link Intent} that must be declared as handled by the service.
*/
@SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
public static final String SERVICE_INTERFACE
= "android.service.notification.ConditionProviderService";
/**
* The name of the {@code meta-data} tag containing a localized name of the type of zen rules
* provided by this service.
*
* @deprecated see {@link android.app.NotificationManager#META_DATA_AUTOMATIC_RULE_TYPE}.
*/
@Deprecated
public static final String META_DATA_RULE_TYPE = "android.service.zen.automatic.ruleType";
/**
* The name of the {@code meta-data} tag containing the {@link ComponentName} of an activity
* that allows users to configure the conditions provided by this service.
*
* @deprecated see {@link android.app.NotificationManager#ACTION_AUTOMATIC_ZEN_RULE}.
*/
@Deprecated
public static final String META_DATA_CONFIGURATION_ACTIVITY =
"android.service.zen.automatic.configurationActivity";
/**
* The name of the {@code meta-data} tag containing the maximum number of rule instances that
* can be created for this rule type. Omit or enter a value <= 0 to allow unlimited instances.
*
* @deprecated see {@link android.app.NotificationManager#META_DATA_RULE_INSTANCE_LIMIT}.
*/
@Deprecated
public static final String META_DATA_RULE_INSTANCE_LIMIT =
"android.service.zen.automatic.ruleInstanceLimit";
/**
* A String rule id extra passed to {@link #META_DATA_CONFIGURATION_ACTIVITY}.
*
* @deprecated see {@link android.app.NotificationManager#EXTRA_AUTOMATIC_RULE_ID}.
*/
@Deprecated
public static final String EXTRA_RULE_ID = "android.service.notification.extra.RULE_ID";
/**
* Called when this service is connected.
*/
abstract public void onConnected();
public void onRequestConditions(int relevance) {}
/**
* Called by the system when there is a new {@link Condition} to be managed by this provider.
* @param conditionId the Uri describing the criteria of the condition.
*/
abstract public void onSubscribe(Uri conditionId);
/**
* Called by the system when a {@link Condition} has been deleted.
* @param conditionId the Uri describing the criteria of the deleted condition.
*/
abstract public void onUnsubscribe(Uri conditionId);
private final INotificationManager getNotificationInterface() {
if (mNoMan == null) {
mNoMan = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
}
return mNoMan;
}
/**
* Request that the provider be rebound, after a previous call to (@link #requestUnbind).
*
* <p>This method will fail for providers that have not been granted the permission by the user.
*/
public static final void requestRebind(ComponentName componentName) {
INotificationManager noMan = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
try {
noMan.requestBindProvider(componentName);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Request that the provider service be unbound.
*
* <p>This will no longer receive subscription updates and will not be able to update the
* state of conditions until {@link #requestRebind(ComponentName)} is called.
* The service will likely be killed by the system after this call.
*
* <p>The service should wait for the {@link #onConnected()} event before performing this
* operation.
*/
public final void requestUnbind() {
INotificationManager noMan = getNotificationInterface();
try {
noMan.requestUnbindProvider(mProvider);
// Disable future messages.
mIsConnected = false;
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Informs the notification manager that the state of a Condition has changed. Use this method
* to put the system into Do Not Disturb mode or request that it exits Do Not Disturb mode. This
* call will be ignored unless there is an enabled {@link android.app.AutomaticZenRule} owned by
* service that has an {@link android.app.AutomaticZenRule#getConditionId()} equal to this
* {@link Condition#id}.
* @param condition the condition that has changed.
*
* @deprecated see
* {@link android.app.NotificationManager#setAutomaticZenRuleState(String, Condition)}.
*/
@Deprecated
public final void notifyCondition(Condition condition) {
if (condition == null) return;
notifyConditions(new Condition[]{ condition });
}
/**
* Informs the notification manager that the state of one or more Conditions has changed. See
* {@link #notifyCondition(Condition)} for restrictions.
* @param conditions the changed conditions.
*
* @deprecated see
* {@link android.app.NotificationManager#setAutomaticZenRuleState(String, Condition)}.
*/
@Deprecated
public final void notifyConditions(Condition... conditions) {
if (!isBound() || conditions == null) return;
try {
getNotificationInterface().notifyConditions(getPackageName(), mProvider, conditions);
} catch (android.os.RemoteException ex) {
Log.v(TAG, "Unable to contact notification manager", ex);
}
}
@Override
public IBinder onBind(Intent intent) {
if (mProvider == null) {
mProvider = new Provider();
}
return mProvider;
}
/**
* @hide
*/
@TestApi
public boolean isBound() {
if (!mIsConnected) {
Log.w(TAG, "Condition provider service not yet bound.");
}
return mIsConnected;
}
private final class Provider extends IConditionProvider.Stub {
@Override
public void onConnected() {
mIsConnected = true;
mHandler.obtainMessage(H.ON_CONNECTED).sendToTarget();
}
@Override
public void onSubscribe(Uri conditionId) {
mHandler.obtainMessage(H.ON_SUBSCRIBE, conditionId).sendToTarget();
}
@Override
public void onUnsubscribe(Uri conditionId) {
mHandler.obtainMessage(H.ON_UNSUBSCRIBE, conditionId).sendToTarget();
}
}
private final class H extends Handler {
private static final int ON_CONNECTED = 1;
private static final int ON_SUBSCRIBE = 3;
private static final int ON_UNSUBSCRIBE = 4;
@Override
public void handleMessage(Message msg) {
String name = null;
if (!mIsConnected) {
return;
}
try {
switch(msg.what) {
case ON_CONNECTED:
name = "onConnected";
onConnected();
break;
case ON_SUBSCRIBE:
name = "onSubscribe";
onSubscribe((Uri)msg.obj);
break;
case ON_UNSUBSCRIBE:
name = "onUnsubscribe";
onUnsubscribe((Uri)msg.obj);
break;
}
} catch (Throwable t) {
Log.w(TAG, "Error running " + name, t);
}
}
}
}
|