Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2047ce82 authored by John Spurlock's avatar John Spurlock Committed by Android (Google) Code Review
Browse files

Merge "Zen: Serialize notify conditions calls, defer subscription." into mnc-dev

parents 84260768 a7d92b19
Loading
Loading
Loading
Loading
+8 −8
Original line number Diff line number Diff line
@@ -1622,16 +1622,16 @@ public class NotificationManagerService extends SystemService {
        }

        @Override
        public void notifyConditions(String pkg, IConditionProvider provider,
                Condition[] conditions) {
        public void notifyConditions(final String pkg, IConditionProvider provider,
                final Condition[] conditions) {
            final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider);
            checkCallerIsSystemOrSameApp(pkg);
            final long identity = Binder.clearCallingIdentity();
            try {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mConditionProviders.notifyConditions(pkg, info, conditions);
            } finally {
                Binder.restoreCallingIdentity(identity);
                }
            });
        }

        @Override
+13 −5
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@ public class ScheduleConditionProvider extends SystemConditionProviderService {

    private boolean mConnected;
    private boolean mRegistered;
    private long mNextAlarmTime;

    public ScheduleConditionProvider() {
        if (DEBUG) Slog.d(TAG, "new " + SIMPLE_NAME + "()");
@@ -84,6 +85,12 @@ public class ScheduleConditionProvider extends SystemConditionProviderService {
            pw.print(meetsSchedule(conditionId, now) ? "* " : "  ");
            pw.println(conditionId);
        }
        pw.print("      mNextAlarmTime="); pw.print(mNextAlarmTime);
        if (mNextAlarmTime > 0) {
            pw.printf(" (%s, in %s, now=%s)", ts(mNextAlarmTime),
                    formatDuration(mNextAlarmTime - now), ts(now));
        }
        pw.println();
    }

    @Override
@@ -141,7 +148,7 @@ public class ScheduleConditionProvider extends SystemConditionProviderService {
    private void evaluateSubscriptions() {
        setRegistered(!mSubscriptions.isEmpty());
        final long now = System.currentTimeMillis();
        long nextAlarmTime = 0;
        mNextAlarmTime = 0;
        for (Uri conditionId : mSubscriptions) {
            final ScheduleCalendar cal = toScheduleCalendar(conditionId);
            if (cal != null && cal.isInSchedule(now)) {
@@ -152,13 +159,13 @@ public class ScheduleConditionProvider extends SystemConditionProviderService {
            if (cal != null) {
                final long nextChangeTime = cal.getNextChangeTime(now);
                if (nextChangeTime > 0 && nextChangeTime > now) {
                    if (nextAlarmTime == 0 || nextChangeTime < nextAlarmTime) {
                        nextAlarmTime = nextChangeTime;
                    if (mNextAlarmTime == 0 || nextChangeTime < mNextAlarmTime) {
                        mNextAlarmTime = nextChangeTime;
                    }
                }
            }
        }
        updateAlarm(now, nextAlarmTime);
        updateAlarm(now, mNextAlarmTime);
    }

    private void updateAlarm(long now, long time) {
@@ -209,7 +216,8 @@ public class ScheduleConditionProvider extends SystemConditionProviderService {
    }

    private void notifyCondition(Uri conditionId, int state, String reason) {
        if (DEBUG) Slog.d(TAG, "notifyCondition " + Condition.stateToString(state)
        if (DEBUG) Slog.d(TAG, "notifyCondition " + conditionId
                + " " + Condition.stateToString(state)
                + " reason=" + reason);
        notifyCondition(createCondition(conditionId, state));
    }
+15 −11
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@ public class ZenModeConditions implements ConditionProviders.Callback {
        mConditionProviders.requestConditions(callback, relevance);
    }

    public void evaluateConfig(ZenModeConfig config) {
    public void evaluateConfig(ZenModeConfig config, boolean processSubscriptione) {
        if (config == null) return;
        if (config.manualRule != null && config.manualRule.condition != null
                && !config.manualRule.isTrueOrUnknown()) {
@@ -71,20 +71,22 @@ public class ZenModeConditions implements ConditionProviders.Callback {
            config.manualRule = null;
        }
        final ArraySet<Uri> current = new ArraySet<>();
        evaluateRule(config.manualRule, current);
        evaluateRule(config.manualRule, current, processSubscriptione);
        for (ZenRule automaticRule : config.automaticRules.values()) {
            evaluateRule(automaticRule, current);
            evaluateRule(automaticRule, current, processSubscriptione);
            updateSnoozing(automaticRule);
        }
        final int N = mSubscriptions.size();
        for (int i = N - 1; i >= 0; i--) {
            final Uri id = mSubscriptions.keyAt(i);
            final ComponentName component = mSubscriptions.valueAt(i);
            if (processSubscriptione) {
                if (!current.contains(id)) {
                    mConditionProviders.unsubscribeIfNecessary(component, id);
                    mSubscriptions.removeAt(i);
                }
            }
        }
        mFirstEvaluation = false;
    }

@@ -122,7 +124,7 @@ public class ZenModeConditions implements ConditionProviders.Callback {
        }
    }

    private void evaluateRule(ZenRule rule, ArraySet<Uri> current) {
    private void evaluateRule(ZenRule rule, ArraySet<Uri> current, boolean processSubscriptions) {
        if (rule == null || rule.conditionId == null) return;
        final Uri id = rule.conditionId;
        boolean isSystemCondition = false;
@@ -148,12 +150,14 @@ public class ZenModeConditions implements ConditionProviders.Callback {
        if (current != null) {
            current.add(id);
        }
        if (processSubscriptions) {
            if (mConditionProviders.subscribeIfNecessary(rule.component, rule.conditionId)) {
                mSubscriptions.put(rule.conditionId, rule.component);
            } else {
                if (DEBUG) Log.d(TAG, "zmc failed to subscribe");
            }
        }
    }

    private boolean isAutomaticActive(ComponentName component) {
        if (component == null) return false;
+2 −1
Original line number Diff line number Diff line
@@ -264,7 +264,7 @@ public class ZenModeHelper {
            Log.w(TAG, "Invalid config in setConfig; " + config);
            return false;
        }
        mConditions.evaluateConfig(config);  // may modify config
        mConditions.evaluateConfig(config, false /*processSubscriptions*/);  // may modify config
        if (config.equals(mConfig)) return true;
        if (DEBUG) Log.d(TAG, "setConfig reason=" + reason, new Throwable());
        ZenLog.traceConfig(reason, config);
@@ -280,6 +280,7 @@ public class ZenModeHelper {
        if (!evaluateZenMode(reason, setRingerMode)) {
            applyRestrictions();  // evaluateZenMode will also apply restrictions if changed
        }
        mConditions.evaluateConfig(config, true /*processSubscriptions*/);
        return true;
    }