();
+
+ @Override
+ public String toString() {
+ final StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < mFiredAlarms.size(); i++) {
+ if (i > 0) sb.append(',');
+ sb.append(mTracker.formatAlarmDebug(mFiredAlarms.valueAt(i)));
+ }
+ return sb.toString();
+ }
+
+ public void add(long firedAlarm) {
+ mFiredAlarms.add(firedAlarm);
+ }
+
+ public void clear() {
+ mFiredAlarms.clear();
+ }
+
+ public boolean findBefore(long time) {
+ for (int i = 0; i < mFiredAlarms.size(); i++) {
+ if (mFiredAlarms.valueAt(i) < time) {
+ return true;
+ }
+ }
+ return false;
+ }
}
}
diff --git a/services/core/java/com/android/server/notification/NextAlarmConditionProvider.java b/services/core/java/com/android/server/notification/NextAlarmConditionProvider.java
index 35bbaa0534651..1634c657b36ca 100644
--- a/services/core/java/com/android/server/notification/NextAlarmConditionProvider.java
+++ b/services/core/java/com/android/server/notification/NextAlarmConditionProvider.java
@@ -25,6 +25,8 @@ import android.service.notification.Condition;
import android.service.notification.ConditionProviderService;
import android.service.notification.IConditionProvider;
import android.service.notification.ZenModeConfig;
+import android.text.TextUtils;
+import android.util.ArraySet;
import android.util.Log;
import android.util.Slog;
import android.util.TimeUtils;
@@ -38,10 +40,7 @@ import java.io.PrintWriter;
* Built-in zen condition provider for alarm-clock-based conditions.
*
* If the user's next alarm is within a lookahead threshold (config, default 12hrs), advertise
- * it as an exit condition for zen mode (unless the built-in downtime condition is also available).
- *
- *
When this next alarm is selected as the active exit condition, follow subsequent changes
- * to the user's next alarm, assuming it remains within the 12-hr window.
+ * it as an exit condition for zen mode.
*
*
The next alarm is defined as {@link AlarmManager#getNextAlarmClock(int)}, which does not
* survive a reboot. Maintain the illusion of a consistent next alarm value by holding on to
@@ -55,20 +54,22 @@ public class NextAlarmConditionProvider extends ConditionProviderService {
private static final long MINUTES = 60 * SECONDS;
private static final long HOURS = 60 * MINUTES;
- private static final String NEXT_ALARM_PATH = "next_alarm";
+ private static final long BAD_CONDITION = -1;
+
public static final ComponentName COMPONENT =
new ComponentName("android", NextAlarmConditionProvider.class.getName());
private final Context mContext = this;
+ private final NextAlarmTracker mTracker;
+ private final ArraySet mSubscriptions = new ArraySet();
- private NextAlarmTracker mTracker;
private boolean mConnected;
private long mLookaheadThreshold;
- private Callback mCallback;
- private Uri mCurrentSubscription;
+ private boolean mRequesting;
- public NextAlarmConditionProvider() {
+ public NextAlarmConditionProvider(NextAlarmTracker tracker) {
if (DEBUG) Slog.d(TAG, "new NextAlarmConditionProvider()");
+ mTracker = tracker;
}
public void dump(PrintWriter pw, DumpFilter filter) {
@@ -76,20 +77,16 @@ public class NextAlarmConditionProvider extends ConditionProviderService {
pw.print(" mConnected="); pw.println(mConnected);
pw.print(" mLookaheadThreshold="); pw.print(mLookaheadThreshold);
pw.print(" ("); TimeUtils.formatDuration(mLookaheadThreshold, pw); pw.println(")");
- pw.print(" mCurrentSubscription="); pw.println(mCurrentSubscription);
- }
-
- public void setCallback(Callback callback) {
- mCallback = callback;
+ pw.print(" mSubscriptions="); pw.println(mSubscriptions);
+ pw.print(" mRequesting="); pw.println(mRequesting);
}
@Override
public void onConnected() {
if (DEBUG) Slog.d(TAG, "onConnected");
- mLookaheadThreshold = mContext.getResources()
- .getInteger(R.integer.config_next_alarm_condition_lookahead_threshold_hrs) * HOURS;
+ mLookaheadThreshold = PropConfig.getInt(mContext, "nextalarm.condition.lookahead",
+ R.integer.config_next_alarm_condition_lookahead_threshold_hrs) * HOURS;
mConnected = true;
- mTracker = mCallback.getNextAlarmTracker();
mTracker.addCallback(mTrackerCallback);
}
@@ -103,34 +100,27 @@ public class NextAlarmConditionProvider extends ConditionProviderService {
@Override
public void onRequestConditions(int relevance) {
- if (!mConnected || (relevance & Condition.FLAG_RELEVANT_NOW) == 0) return;
-
- final AlarmClockInfo nextAlarm = mTracker.getNextAlarm();
- if (nextAlarm == null) return; // no next alarm
- if (mCallback != null && mCallback.isInDowntime()) return; // prefer downtime condition
- if (!isWithinLookaheadThreshold(nextAlarm)) return; // alarm not within window
-
- // next alarm exists, and is within the configured lookahead threshold
- notifyCondition(newConditionId(), nextAlarm, Condition.STATE_TRUE, "request");
+ if (DEBUG) Slog.d(TAG, "onRequestConditions relevance=" + relevance);
+ if (!mConnected) return;
+ mRequesting = (relevance & Condition.FLAG_RELEVANT_NOW) != 0;
+ mTracker.evaluate();
}
@Override
public void onSubscribe(Uri conditionId) {
if (DEBUG) Slog.d(TAG, "onSubscribe " + conditionId);
- if (!isNextAlarmCondition(conditionId)) {
+ if (tryParseNextAlarmCondition(conditionId) == BAD_CONDITION) {
notifyCondition(conditionId, null, Condition.STATE_FALSE, "badCondition");
return;
}
- mCurrentSubscription = conditionId;
+ mSubscriptions.add(conditionId);
mTracker.evaluate();
}
@Override
public void onUnsubscribe(Uri conditionId) {
if (DEBUG) Slog.d(TAG, "onUnsubscribe " + conditionId);
- if (conditionId != null && conditionId.equals(mCurrentSubscription)) {
- mCurrentSubscription = null;
- }
+ mSubscriptions.remove(conditionId);
}
public void attachBase(Context base) {
@@ -157,43 +147,72 @@ public class NextAlarmConditionProvider extends ConditionProviderService {
formattedAlarm, 0, state, Condition.FLAG_RELEVANT_NOW));
}
- private Uri newConditionId() {
+ private Uri newConditionId(AlarmClockInfo nextAlarm) {
return new Uri.Builder().scheme(Condition.SCHEME)
.authority(ZenModeConfig.SYSTEM_AUTHORITY)
- .appendPath(NEXT_ALARM_PATH)
+ .appendPath(ZenModeConfig.NEXT_ALARM_PATH)
.appendPath(Integer.toString(mTracker.getCurrentUserId()))
+ .appendPath(Long.toString(nextAlarm.getTriggerTime()))
.build();
}
- private boolean isNextAlarmCondition(Uri conditionId) {
+ private long tryParseNextAlarmCondition(Uri conditionId) {
return conditionId != null && conditionId.getScheme().equals(Condition.SCHEME)
&& conditionId.getAuthority().equals(ZenModeConfig.SYSTEM_AUTHORITY)
- && conditionId.getPathSegments().size() == 2
- && conditionId.getPathSegments().get(0).equals(NEXT_ALARM_PATH)
+ && conditionId.getPathSegments().size() == 3
+ && conditionId.getPathSegments().get(0).equals(ZenModeConfig.NEXT_ALARM_PATH)
&& conditionId.getPathSegments().get(1)
- .equals(Integer.toString(mTracker.getCurrentUserId()));
+ .equals(Integer.toString(mTracker.getCurrentUserId()))
+ ? tryParseLong(conditionId.getPathSegments().get(2), BAD_CONDITION)
+ : BAD_CONDITION;
+ }
+
+ private static long tryParseLong(String value, long defValue) {
+ if (TextUtils.isEmpty(value)) return defValue;
+ try {
+ return Long.valueOf(value);
+ } catch (NumberFormatException e) {
+ return defValue;
+ }
}
private void onEvaluate(AlarmClockInfo nextAlarm, long wakeupTime, boolean booted) {
final boolean withinThreshold = isWithinLookaheadThreshold(nextAlarm);
- if (DEBUG) Slog.d(TAG, "onEvaluate mCurrentSubscription=" + mCurrentSubscription
+ final long nextAlarmTime = nextAlarm != null ? nextAlarm.getTriggerTime() : 0;
+ if (DEBUG) Slog.d(TAG, "onEvaluate mSubscriptions=" + mSubscriptions
+ + " nextAlarmTime=" + mTracker.formatAlarmDebug(nextAlarmTime)
+ " nextAlarmWakeup=" + mTracker.formatAlarmDebug(wakeupTime)
+ " withinThreshold=" + withinThreshold
+ " booted=" + booted);
- if (mCurrentSubscription == null) return; // no one cares
- if (!booted) {
- // we don't know yet
- notifyCondition(mCurrentSubscription, nextAlarm, Condition.STATE_UNKNOWN, "!booted");
- return;
+
+ ArraySet conditions = mSubscriptions;
+ if (mRequesting && nextAlarm != null && withinThreshold) {
+ final Uri id = newConditionId(nextAlarm);
+ if (!conditions.contains(id)) {
+ conditions = new ArraySet(conditions);
+ conditions.add(id);
+ }
}
- if (!withinThreshold) {
- // next alarm outside threshold or in the past, condition = false
- notifyCondition(mCurrentSubscription, nextAlarm, Condition.STATE_FALSE, "!within");
- mCurrentSubscription = null;
- return;
+ for (Uri conditionId : conditions) {
+ final long time = tryParseNextAlarmCondition(conditionId);
+ if (time == BAD_CONDITION) {
+ notifyCondition(conditionId, nextAlarm, Condition.STATE_FALSE, "badCondition");
+ } else if (!booted) {
+ // we don't know yet
+ if (mSubscriptions.contains(conditionId)) {
+ notifyCondition(conditionId, nextAlarm, Condition.STATE_UNKNOWN, "!booted");
+ }
+ } else if (time != nextAlarmTime) {
+ // next alarm changed since subscription, consider obsolete
+ notifyCondition(conditionId, nextAlarm, Condition.STATE_FALSE, "changed");
+ } else if (!withinThreshold) {
+ // next alarm outside threshold or in the past, condition = false
+ notifyCondition(conditionId, nextAlarm, Condition.STATE_FALSE, "!within");
+ } else {
+ // next alarm within threshold and in the future, condition = true
+ notifyCondition(conditionId, nextAlarm, Condition.STATE_TRUE, "within");
+ }
}
- // next alarm in the future and within threshold, condition = true
- notifyCondition(mCurrentSubscription, nextAlarm, Condition.STATE_TRUE, "within");
}
private final NextAlarmTracker.Callback mTrackerCallback = new NextAlarmTracker.Callback() {
@@ -202,9 +221,4 @@ public class NextAlarmConditionProvider extends ConditionProviderService {
NextAlarmConditionProvider.this.onEvaluate(nextAlarm, wakeupTime, booted);
}
};
-
- public interface Callback {
- boolean isInDowntime();
- NextAlarmTracker getNextAlarmTracker();
- }
}
diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java
index aec20bc553168..bb999168509ff 100644
--- a/services/core/java/com/android/server/notification/NotificationManagerService.java
+++ b/services/core/java/com/android/server/notification/NotificationManagerService.java
@@ -1551,6 +1551,12 @@ public class NotificationManagerService extends SystemService {
MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS,
MATCHES_CALL_FILTER_TIMEOUT_AFFINITY);
}
+
+ @Override
+ public boolean isSystemConditionProviderEnabled(String path) {
+ enforceSystemOrSystemUI("INotificationManager.isSystemConditionProviderEnabled");
+ return mConditionProviders.isSystemConditionProviderEnabled(path);
+ }
};
private String[] getActiveNotificationKeys(INotificationListener token) {
diff --git a/services/core/java/com/android/server/notification/PropConfig.java b/services/core/java/com/android/server/notification/PropConfig.java
new file mode 100644
index 0000000000000..97bf90d0a92c8
--- /dev/null
+++ b/services/core/java/com/android/server/notification/PropConfig.java
@@ -0,0 +1,33 @@
+/*
+ * 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 com.android.server.notification;
+
+import android.content.Context;
+import android.os.SystemProperties;
+
+public class PropConfig {
+ private static final String UNSET = "UNSET";
+
+ public static int getInt(Context context, String propName, int resId) {
+ return SystemProperties.getInt(propName, context.getResources().getInteger(resId));
+ }
+
+ public static String[] getStringArray(Context context, String propName, int resId) {
+ final String prop = SystemProperties.get(propName, UNSET);
+ return !UNSET.equals(prop) ? prop.split(",") : context.getResources().getStringArray(resId);
+ }
+}
diff --git a/services/core/java/com/android/server/notification/ZenLog.java b/services/core/java/com/android/server/notification/ZenLog.java
index 69601594b629c..dda0b37d11c1a 100644
--- a/services/core/java/com/android/server/notification/ZenLog.java
+++ b/services/core/java/com/android/server/notification/ZenLog.java
@@ -89,12 +89,12 @@ public class ZenLog {
ringerModeToString(ringerModeExternalOut));
}
- public static void traceDowntime(int downtimeMode, int day, ArraySet days) {
- append(TYPE_DOWNTIME, zenModeToString(downtimeMode) + ",day=" + day + ",days=" + days);
+ public static void traceDowntimeAutotrigger(String result) {
+ append(TYPE_DOWNTIME, result);
}
- public static void traceSetZenMode(int mode, String reason) {
- append(TYPE_SET_ZEN_MODE, zenModeToString(mode) + "," + reason);
+ public static void traceSetZenMode(int zenMode, String reason) {
+ append(TYPE_SET_ZEN_MODE, zenModeToString(zenMode) + "," + reason);
}
public static void traceUpdateZenMode(int fromMode, int toMode) {
diff --git a/services/core/java/com/android/server/notification/ZenModeHelper.java b/services/core/java/com/android/server/notification/ZenModeHelper.java
index 5ceb5030a060c..012e22f2eb420 100644
--- a/services/core/java/com/android/server/notification/ZenModeHelper.java
+++ b/services/core/java/com/android/server/notification/ZenModeHelper.java
@@ -108,6 +108,10 @@ public class ZenModeHelper implements AudioManagerInternal.RingerModeDelegate {
mCallbacks.add(callback);
}
+ public void removeCallback(Callback callback) {
+ mCallbacks.remove(callback);
+ }
+
public void onSystemReady() {
mAudioManager = LocalServices.getService(AudioManagerInternal.class);
if (mAudioManager != null) {
@@ -208,9 +212,9 @@ public class ZenModeHelper implements AudioManagerInternal.RingerModeDelegate {
return mZenMode;
}
- public void setZenMode(int zenModeValue, String reason) {
- ZenLog.traceSetZenMode(zenModeValue, reason);
- Global.putInt(mContext.getContentResolver(), Global.ZEN_MODE, zenModeValue);
+ public void setZenMode(int zenMode, String reason) {
+ ZenLog.traceSetZenMode(zenMode, reason);
+ Global.putInt(mContext.getContentResolver(), Global.ZEN_MODE, zenMode);
}
public void updateZenMode() {