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

Commit 3acf04ca authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "No alarm warning in dnd dialog if alarms allowed"

parents 581576ef 5e65c322
Loading
Loading
Loading
Loading
+22 −5
Original line number Diff line number Diff line
@@ -76,6 +76,8 @@ public class EnableZenModeDialog {
    protected Uri mForeverId;
    private int mBucketIndex = -1;

    @VisibleForTesting
    protected NotificationManager mNotificationManager;
    private AlarmManager mAlarmManager;
    private int mUserId;
    private boolean mAttached;
@@ -98,7 +100,7 @@ public class EnableZenModeDialog {
    }

    public Dialog createDialog() {
        NotificationManager noMan = (NotificationManager) mContext.
        mNotificationManager = (NotificationManager) mContext.
                getSystemService(Context.NOTIFICATION_SERVICE);
        mForeverId =  Condition.newId(mContext).appendPath("forever").build();
        mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
@@ -131,7 +133,8 @@ public class EnableZenModeDialog {
                                    Slog.d(TAG, "Invalid manual condition: " + tag.condition);
                                }
                                // always triggers priority-only dnd with chosen condition
                                noMan.setZenMode(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS,
                                mNotificationManager.setZenMode(
                                        Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS,
                                        getRealConditionId(tag.condition), TAG);
                            }
                        });
@@ -465,7 +468,16 @@ public class EnableZenModeDialog {
        mZenAlarmWarning.setVisibility(warningText == null ? View.GONE : View.VISIBLE);
    }

    private String computeAlarmWarningText(Condition condition) {
    @VisibleForTesting
    protected String computeAlarmWarningText(Condition condition) {
        boolean allowAlarms = (mNotificationManager.getNotificationPolicy().priorityCategories
                & NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS) != 0;

        // don't show alarm warning if alarms are allowed to bypass dnd
        if (allowAlarms) {
            return null;
        }

        final long now = System.currentTimeMillis();
        final long nextAlarm = getNextAlarm();
        if (nextAlarm < now) {
@@ -483,14 +495,19 @@ public class EnableZenModeDialog {
        if (warningRes == 0) {
            return null;
        }

        return mContext.getResources().getString(warningRes, getTime(nextAlarm, now));
    }

    @VisibleForTesting
    protected String getTime(long nextAlarm, long now) {
        final boolean soon = (nextAlarm - now) < 24 * 60 * 60 * 1000;
        final boolean is24 = DateFormat.is24HourFormat(mContext, ActivityManager.getCurrentUser());
        final String skeleton = soon ? (is24 ? "Hm" : "hma") : (is24 ? "EEEHm" : "EEEhma");
        final String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton);
        final CharSequence formattedTime = DateFormat.format(pattern, nextAlarm);
        final int templateRes = soon ? R.string.alarm_template : R.string.alarm_template_far;
        final String template = mContext.getResources().getString(templateRes, formattedTime);
        return mContext.getResources().getString(warningRes, template);
        return mContext.getResources().getString(templateRes, formattedTime);
    }

    // used as the view tag on condition rows
+50 −0
Original line number Diff line number Diff line
@@ -17,15 +17,22 @@
package com.android.settingslib.notification;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.app.Fragment;
import android.app.NotificationManager;
import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.service.notification.Condition;
import android.view.LayoutInflater;
@@ -46,7 +53,11 @@ public class EnableZenModeDialogTest {
    @Mock
    private Context mContext;
    @Mock
    private Resources mResources;
    @Mock
    private Fragment mFragment;
    @Mock
    private NotificationManager mNotificationManager;

    private Context mShadowContext;
    private LayoutInflater mLayoutInflater;
@@ -58,6 +69,7 @@ public class EnableZenModeDialogTest {
        MockitoAnnotations.initMocks(this);
        mShadowContext = RuntimeEnvironment.application;
        when(mContext.getApplicationContext()).thenReturn(mContext);
        when(mContext.getResources()).thenReturn(mResources);
        when(mFragment.getContext()).thenReturn(mShadowContext);
        mLayoutInflater = LayoutInflater.from(mShadowContext);

@@ -67,6 +79,10 @@ public class EnableZenModeDialogTest {
        mController.mForeverId =  Condition.newId(mContext).appendPath("forever").build();
        when(mContext.getString(com.android.internal.R.string.zen_mode_forever))
                .thenReturn("testSummary");
        NotificationManager.Policy alarmsEnabledPolicy = new NotificationManager.Policy(
                NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS, 0, 0, 0);
        doReturn(alarmsEnabledPolicy).when(mNotificationManager).getNotificationPolicy();
        mController.mNotificationManager = mNotificationManager;
        mController.getContentView();

        // these methods use static calls to ZenModeConfig which would normally fail in robotests,
@@ -141,4 +157,38 @@ public class EnableZenModeDialogTest {
        assertFalse(mController.getConditionTagAt(
                EnableZenModeDialog.COUNTDOWN_ALARM_CONDITION_INDEX).rb.isChecked());
    }

    @Test
    public void testNoAlarmWarning() {
        // setup alarm
        long now = System.currentTimeMillis();
        doReturn(now + 100000).when(mController).getNextAlarm();
        doReturn("").when(mController).getTime(anyLong(), anyLong());

        // allow alarms
        when(mNotificationManager.getNotificationPolicy()).thenReturn(
                new NotificationManager.Policy(
                        NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS, 0, 0, 0));

        // alarm warning should be null
        assertNull(mController.computeAlarmWarningText(null));
    }

    @Test
    public void testAlarmWarning() {
        // setup alarm
        long now = System.currentTimeMillis();
        doReturn(now + 1000000).when(mController).getNextAlarm();
        doReturn("").when(mController).getTime(anyLong(), anyLong());

        // don't allow alarms to bypass dnd
        when(mNotificationManager.getNotificationPolicy()).thenReturn(
                new NotificationManager.Policy(0, 0, 0, 0));

        // return a string if mResources is asked to retrieve a string
        when(mResources.getString(anyInt(), anyString())).thenReturn("");

        // alarm warning should NOT be null
        assertNotNull(mController.computeAlarmWarningText(null));
    }
}
 No newline at end of file