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

Commit 2c5895d6 authored by Beverly's avatar Beverly
Browse files

Default DND rules show at top and can't be deleted

Test: make ROBOTEST_FILTER=ZenModeAutomaticRulesPreferenceControllerTest RunSettingsRoboTests -j40
Bug: 63077372
Change-Id: I43e7e2a70df5defe931a7ecedc6f946853dab476
parent d7d5754c
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import com.android.settingslib.core.lifecycle.Lifecycle;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

@@ -45,6 +46,7 @@ abstract public class AbstractZenModeAutomaticRulePreferenceController extends
    protected Fragment mParent;
    protected Set<Map.Entry<String, AutomaticZenRule>> mRules;
    protected PackageManager mPm;
    private static List<String> mDefaultRuleIds;

    public AbstractZenModeAutomaticRulePreferenceController(Context context, String key, Fragment
            parent, Lifecycle lifecycle) {
@@ -60,6 +62,13 @@ abstract public class AbstractZenModeAutomaticRulePreferenceController extends
        mRules = getZenModeRules();
    }

    private static List<String> getDefaultRuleIds() {
        if (mDefaultRuleIds == null) {
            mDefaultRuleIds = ZenModeConfig.DEFAULT_RULE_IDS;
        }
        return mDefaultRuleIds;
    }

    private Set<Map.Entry<String, AutomaticZenRule>> getZenModeRules() {
        Map<String, AutomaticZenRule> ruleMap =
                NotificationManager.from(mContext).getAutomaticZenRules();
@@ -99,6 +108,13 @@ abstract public class AbstractZenModeAutomaticRulePreferenceController extends
                @Override
                public int compare(Map.Entry<String, AutomaticZenRule> lhs,
                        Map.Entry<String, AutomaticZenRule> rhs) {
                    // if it's a default rule, should be at the top of automatic rules
                    boolean lhsIsDefaultRule = getDefaultRuleIds().contains(lhs.getKey());
                    boolean rhsIsDefaultRule = getDefaultRuleIds().contains(rhs.getKey());
                    if (lhsIsDefaultRule != rhsIsDefaultRule) {
                        return lhsIsDefaultRule ? -1 : 1;
                    }

                    int byDate = Long.compare(lhs.getValue().getCreationTime(),
                            rhs.getValue().getCreationTime());
                    if (byDate != 0) {
+4 −0
Original line number Diff line number Diff line
@@ -73,6 +73,10 @@ public class ZenRulePreference extends TwoTargetPreference {

    @Override
    protected int getSecondTargetResId() {
        if (mId != null && ZenModeConfig.DEFAULT_RULE_IDS.contains(mId)) {
            return 0;
        }

        return R.layout.zen_rule_widget;
    }

+33 −8
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.settings.notification;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -25,7 +24,6 @@ import static org.mockito.Mockito.when;
import android.app.AutomaticZenRule;
import android.app.Fragment;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.support.v7.preference.PreferenceCategory;
@@ -45,7 +43,9 @@ import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SettingsRobolectricTestRunner.class)
@@ -53,6 +53,9 @@ import java.util.Map;
public class ZenModeAutomaticRulesPreferenceControllerTest {
    private ZenModeAutomaticRulesPreferenceController mController;
    private final String GENERIC_RULE_NAME = "test";
    final String DEFAULT_ID_1 = "DEFAULT_1";
    final String DEFAULT_ID_2 = "DEFAULT_2";
    private final List<String> mDefaultIds = Arrays.asList(DEFAULT_ID_1, DEFAULT_ID_2);

    @Mock
    private ZenModeBackend mBackend;
@@ -66,7 +69,6 @@ public class ZenModeAutomaticRulesPreferenceControllerTest {
    private PreferenceScreen mPreferenceScreen;

    private Context mContext;
    private ContentResolver mContentResolver;

    @Before
    public void setup() {
@@ -75,11 +77,12 @@ public class ZenModeAutomaticRulesPreferenceControllerTest {
        shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);

        mContext = shadowApplication.getApplicationContext();
        mContentResolver = RuntimeEnvironment.application.getContentResolver();
        when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
        mController = new ZenModeAutomaticRulesPreferenceController(mContext, mock(Fragment.class),
                mock(Lifecycle.class));

        ReflectionHelpers.setField(mController, "mBackend", mBackend);
        ReflectionHelpers.setField(mController, "DEFAULT_RULE_IDS", mDefaultIds);

        when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
                mockPref);
@@ -97,7 +100,7 @@ public class ZenModeAutomaticRulesPreferenceControllerTest {

        // check ordering, most recent should be at the bottom/end (ie higher creation time)
        for (int i = 0; i < NUM_RULES; i++) {
            assertEquals(rules[i].getKey(), GENERIC_RULE_NAME + (NUM_RULES - 1 - i));
            assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i].getKey());
        }
    }

@@ -112,7 +115,29 @@ public class ZenModeAutomaticRulesPreferenceControllerTest {

        // check ordering, most recent should be at the bottom/end (ie higher creation time)
        for (int i = 0; i < NUM_RULES; i++) {
            assertEquals(rules[i].getKey(), GENERIC_RULE_NAME + i);
            assertEquals(GENERIC_RULE_NAME + i, rules[i].getKey());
        }
    }

    @Test
    public void updateState_checkRuleOrderingDescending_withDefaultRules() {
        final int NUM_RULES = 4;

        Map<String, AutomaticZenRule> ruleMap = mockAutoZenRulesDecreasingCreationTime(NUM_RULES);
        ruleMap.put(DEFAULT_ID_2, new AutomaticZenRule("DEFAULT_1_NAME", null,
                null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 20));
        ruleMap.put(DEFAULT_ID_1, new AutomaticZenRule("DEFAULT_1_NAME", null,
                null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10));
        when(mNotificationManager.getAutomaticZenRules()).thenReturn(ruleMap);

        Map.Entry<String, AutomaticZenRule>[] rules = mController.sortedRules();
        assertEquals(NUM_RULES + 2, rules.length);

        assertEquals(rules[0].getKey(), DEFAULT_ID_1);
        assertEquals(rules[1].getKey(), DEFAULT_ID_2);
        // NON-DEFAULT RULES check ordering, most recent at the bottom/end
        for (int i = 0; i < NUM_RULES; i++) {
            assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i + 2].getKey());
        }
    }

@@ -138,8 +163,8 @@ public class ZenModeAutomaticRulesPreferenceControllerTest {
        assertEquals(NUM_RULES + 2, rules.length); // inserted 2 rules

        // check ordering of inserted rules
        assertEquals(rules[4].getKey(), insertedRule1);
        assertEquals(rules[2].getKey(), insertedRule2);
        assertEquals(insertedRule1, rules[4].getKey());
        assertEquals(insertedRule2, rules[2].getKey());
    }

    private Map<String, AutomaticZenRule> mockAutoZenRulesAscendingCreationTime(int numRules) {