protected Uri mForeverId;
private int mBucketIndex = -1;
+ @VisibleForTesting
+ protected NotificationManager mNotificationManager;
private AlarmManager mAlarmManager;
private int mUserId;
private boolean mAttached;
}
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);
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);
}
});
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) {
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
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;
@Mock
private Context mContext;
@Mock
+ private Resources mResources;
+ @Mock
private Fragment mFragment;
+ @Mock
+ private NotificationManager mNotificationManager;
private Context mShadowContext;
private LayoutInflater mLayoutInflater;
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);
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,
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