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

Commit 99f963ea authored by Chris Wren's avatar Chris Wren
Browse files

Add people signals to Zen mode.

Depends-On: I51fcf689ddbee7715e3387b865f18a715887c943
Change-Id: I7c91dec1afeb54505426c4da59ec4d072a60c240
parent 4f0e39a9
Loading
Loading
Loading
Loading
+31 −2
Original line number Diff line number Diff line
@@ -41,12 +41,18 @@ public class ZenModeConfig implements Parcelable {
    public static final String SLEEP_MODE_NIGHTS = "nights";
    public static final String SLEEP_MODE_WEEKNIGHTS = "weeknights";

    public static final int SOURCE_ANYONE = 0;
    public static final int SOURCE_CONTACT = 1;
    public static final int SOURCE_STAR = 2;
    public static final int MAX_SOURCE = SOURCE_STAR;

    private static final int XML_VERSION = 1;
    private static final String ZEN_TAG = "zen";
    private static final String ZEN_ATT_VERSION = "version";
    private static final String ALLOW_TAG = "allow";
    private static final String ALLOW_ATT_CALLS = "calls";
    private static final String ALLOW_ATT_MESSAGES = "messages";
    private static final String ALLOW_ATT_FROM = "from";
    private static final String SLEEP_TAG = "sleep";
    private static final String SLEEP_ATT_MODE = "mode";

@@ -61,6 +67,7 @@ public class ZenModeConfig implements Parcelable {

    public boolean allowCalls;
    public boolean allowMessages;
    public int allowFrom = SOURCE_ANYONE;

    public String sleepMode;
    public int sleepStartHour;
@@ -92,6 +99,7 @@ public class ZenModeConfig implements Parcelable {
            conditionIds = new Uri[len];
            source.readTypedArray(conditionIds, Uri.CREATOR);
        }
        allowFrom = source.readInt();
    }

    @Override
@@ -120,6 +128,7 @@ public class ZenModeConfig implements Parcelable {
        } else {
            dest.writeInt(0);
        }
        dest.writeInt(allowFrom);
    }

    @Override
@@ -127,6 +136,7 @@ public class ZenModeConfig implements Parcelable {
        return new StringBuilder(ZenModeConfig.class.getSimpleName()).append('[')
            .append("allowCalls=").append(allowCalls)
            .append(",allowMessages=").append(allowMessages)
            .append(",allowFrom=").append(sourceToString(allowFrom))
            .append(",sleepMode=").append(sleepMode)
            .append(",sleepStart=").append(sleepStartHour).append('.').append(sleepStartMinute)
            .append(",sleepEnd=").append(sleepEndHour).append('.').append(sleepEndMinute)
@@ -137,6 +147,19 @@ public class ZenModeConfig implements Parcelable {
            .append(']').toString();
    }

    public static String sourceToString(int source) {
        switch (source) {
            case SOURCE_ANYONE:
                return "anyone";
            case SOURCE_CONTACT:
                return "contacts";
            case SOURCE_STAR:
                return "stars";
            default:
                return "UNKNOWN";
        }
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof ZenModeConfig)) return false;
@@ -144,6 +167,7 @@ public class ZenModeConfig implements Parcelable {
        final ZenModeConfig other = (ZenModeConfig) o;
        return other.allowCalls == allowCalls
                && other.allowMessages == allowMessages
                && other.allowFrom == allowFrom
                && Objects.equals(other.sleepMode, sleepMode)
                && other.sleepStartHour == sleepStartHour
                && other.sleepStartMinute == sleepStartMinute
@@ -155,8 +179,8 @@ public class ZenModeConfig implements Parcelable {

    @Override
    public int hashCode() {
        return Objects.hash(allowCalls, allowMessages, sleepMode, sleepStartHour,
                sleepStartMinute, sleepEndHour, sleepEndMinute,
        return Objects.hash(allowCalls, allowMessages, allowFrom, sleepMode,
                sleepStartHour, sleepStartMinute, sleepEndHour, sleepEndMinute,
                Arrays.hashCode(conditionComponents), Arrays.hashCode(conditionIds));
    }

@@ -191,6 +215,10 @@ public class ZenModeConfig implements Parcelable {
                if (ALLOW_TAG.equals(tag)) {
                    rt.allowCalls = safeBoolean(parser, ALLOW_ATT_CALLS, false);
                    rt.allowMessages = safeBoolean(parser, ALLOW_ATT_MESSAGES, false);
                    rt.allowFrom = safeInt(parser, ALLOW_ATT_FROM, SOURCE_ANYONE);
                    if (rt.allowFrom < SOURCE_ANYONE || rt.allowFrom > MAX_SOURCE) {
                        throw new IndexOutOfBoundsException("bad source in config:" + rt.allowFrom);
                    }
                } else if (SLEEP_TAG.equals(tag)) {
                    final String mode = parser.getAttributeValue(null, SLEEP_ATT_MODE);
                    rt.sleepMode = (SLEEP_MODE_NIGHTS.equals(mode)
@@ -224,6 +252,7 @@ public class ZenModeConfig implements Parcelable {
        out.startTag(null, ALLOW_TAG);
        out.attribute(null, ALLOW_ATT_CALLS, Boolean.toString(allowCalls));
        out.attribute(null, ALLOW_ATT_MESSAGES, Boolean.toString(allowMessages));
        out.attribute(null, ALLOW_ATT_FROM, Integer.toString(allowFrom));
        out.endTag(null, ALLOW_TAG);

        out.startTag(null, SLEEP_TAG);
+14 −3
Original line number Diff line number Diff line
@@ -47,9 +47,20 @@ public class ValidateNotificationPeople implements NotificationSignalExtractor {
    private static final int MAX_PEOPLE = 10;
    private static final int PEOPLE_CACHE_SIZE = 200;

    private static final float NONE = 0f;
    private static final float VALID_CONTACT = 0.5f;
    private static final float STARRED_CONTACT = 1f;
    /** Indicates that the notification does not reference any valid contacts. */
    static final float NONE = 0f;

    /**
     * Affinity will be equal to or greater than this value on notifications
     * that reference a valid contact.
     */
    static final float VALID_CONTACT = 0.5f;

    /**
     * Affinity will be equal to or greater than this value on notifications
     * that reference a starred contact.
     */
    static final float STARRED_CONTACT = 1f;

    protected boolean mEnabled;
    private Context mContext;
+22 −2
Original line number Diff line number Diff line
@@ -77,11 +77,13 @@ public class ZenModeHelper {
    // temporary, until we update apps to provide metadata
    private static final Set<String> CALL_PACKAGES = new HashSet<String>(Arrays.asList(
            "com.google.android.dialer",
            "com.android.phone"
            "com.android.phone",
            "com.android.example.notificationshowcase"
            ));
    private static final Set<String> MESSAGE_PACKAGES = new HashSet<String>(Arrays.asList(
            "com.google.android.talk",
            "com.android.mms"
            "com.android.mms",
            "com.android.example.notificationshowcase"
            ));
    private static final Set<String> ALARM_PACKAGES = new HashSet<String>(Arrays.asList(
            "com.google.android.deskclock"
@@ -131,6 +133,10 @@ public class ZenModeHelper {
            if (isAlarm(record)) {
                return false;
            }
            // audience has veto power over all following rules
            if (!audienceMatches(record)) {
                return true;
            }
            if (isCall(record)) {
                return !mConfig.allowCalls;
            }
@@ -245,6 +251,20 @@ public class ZenModeHelper {
        return MESSAGE_PACKAGES.contains(record.sbn.getPackageName());
    }

    private boolean audienceMatches(NotificationRecord record) {
        switch (mConfig.allowFrom) {
            case ZenModeConfig.SOURCE_ANYONE:
                return true;
            case ZenModeConfig.SOURCE_CONTACT:
                return record.getContactAffinity() >= ValidateNotificationPeople.VALID_CONTACT;
            case ZenModeConfig.SOURCE_STAR:
                return record.getContactAffinity() >= ValidateNotificationPeople.STARRED_CONTACT;
            default:
                Slog.w(TAG, "Encountered unknown source: " + mConfig.allowFrom);
                return true;
        }
    }

    private void updateAlarms() {
        updateAlarm(ACTION_ENTER_ZEN, REQUEST_CODE_ENTER,
                mConfig.sleepStartHour, mConfig.sleepStartMinute);