Merge "Added footers to zen mode settings"

This commit is contained in:
TreeHugger Robot
2017-12-05 21:34:36 +00:00
committed by Android (Google) Code Review
11 changed files with 882 additions and 9 deletions

View File

@@ -16,6 +16,9 @@
package com.android.settings.notification;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.AlarmManager.AlarmClockInfo;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
@@ -24,8 +27,11 @@ import android.net.Uri;
import android.os.Handler;
import android.os.UserHandle;
import android.provider.Settings;
import android.service.notification.ScheduleCalendar;
import android.service.notification.ZenModeConfig;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.util.Slog;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.core.PreferenceControllerMixin;
@@ -41,12 +47,15 @@ abstract public class AbstractZenModePreferenceController extends
@VisibleForTesting
protected SettingObserver mSettingObserver;
private final String KEY;
final private NotificationManager mNotificationManager;
protected static ZenModeConfigWrapper mZenModeConfigWrapper;
public AbstractZenModePreferenceController(Context context, String key,
Lifecycle lifecycle) {
super(context);
mZenModeConfigWrapper = new ZenModeConfigWrapper(context);
if (lifecycle != null) {
lifecycle.addObserver(this);
}
@@ -79,6 +88,10 @@ abstract public class AbstractZenModePreferenceController extends
return mNotificationManager.getNotificationPolicy();
}
protected ZenModeConfig getZenModeConfig() {
return mNotificationManager.getZenModeConfig();
}
protected int getZenMode() {
return Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.ZEN_MODE, 0);
@@ -117,4 +130,69 @@ abstract public class AbstractZenModePreferenceController extends
}
}
}
/**
* Wrapper for testing compatibility
*/
@VisibleForTesting
static class ZenModeConfigWrapper {
private final Context mContext;
public ZenModeConfigWrapper(Context context) {
mContext = context;
}
protected String getOwnerCaption(String owner) {
return ZenModeConfig.getOwnerCaption(mContext, owner);
}
protected boolean isTimeRule(Uri id) {
return ZenModeConfig.isValidEventConditionId(id) ||
ZenModeConfig.isValidScheduleConditionId(id);
}
protected CharSequence getFormattedTime(long time, int userHandle) {
return ZenModeConfig.getFormattedTime(mContext, time, isToday(time), userHandle);
}
private boolean isToday(long time) {
return ZenModeConfig.isToday(time);
}
protected long parseManualRuleTime(Uri id) {
return ZenModeConfig.tryParseCountdownConditionId(id);
}
protected long parseAutomaticRuleEndTime(Uri id) {
if (ZenModeConfig.isValidEventConditionId(id)) {
// cannot look up end times for events
return Long.MAX_VALUE;
}
if (ZenModeConfig.isValidScheduleConditionId(id)) {
ScheduleCalendar schedule = ZenModeConfig.toScheduleCalendar(id);
long endTimeMs = schedule.getNextChangeTime(System.currentTimeMillis());
// check if automatic rule will end on next alarm
if (schedule.exitAtAlarm()) {
long nextAlarm = getNextAlarm(mContext);
schedule.maybeSetNextAlarm(System.currentTimeMillis(), nextAlarm);
if (schedule.shouldExitForAlarm(endTimeMs)) {
return nextAlarm;
}
}
return endTimeMs;
}
return -1;
}
}
private static long getNextAlarm(Context context) {
final AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
final AlarmClockInfo info = alarms.getNextAlarmClock(ActivityManager.getCurrentUser());
return info != null ? info.getTriggerTime() : 0;
}
}

View File

@@ -0,0 +1,101 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import android.content.Context;
import android.content.ComponentName;
import android.net.Uri;
import android.provider.Settings;
import android.service.notification.ZenModeConfig;
import android.support.v7.preference.Preference;
import android.util.Slog;
import com.android.settings.R;
import com.android.settingslib.core.lifecycle.Lifecycle;
public class ZenModeBehaviorFooterPreferenceController extends AbstractZenModePreferenceController {
protected static final String KEY = "footer_preference";
public ZenModeBehaviorFooterPreferenceController(Context context, Lifecycle lifecycle) {
super(context, KEY, lifecycle);
}
@Override
public boolean isAvailable() {
return isDeprecatedZenMode(getZenMode());
}
@Override
public String getPreferenceKey() {
return KEY;
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
boolean isAvailable = isAvailable();
preference.setVisible(isAvailable);
if (isAvailable) {
preference.setTitle(getFooterText());
}
}
protected String getFooterText() {
ZenModeConfig config = getZenModeConfig();
// DND turned on by manual rule with deprecated zen mode
if (config.manualRule != null &&
isDeprecatedZenMode(config.manualRule.zenMode)) {
final Uri id = config.manualRule.conditionId;
if (config.manualRule.enabler != null) {
// app triggered manual rule
String appOwner = mZenModeConfigWrapper.getOwnerCaption(config.manualRule.enabler);
if (!appOwner.isEmpty()) {
return mContext.getString(R.string.zen_mode_app_set_behavior, appOwner);
}
} else {
return mContext.getString(R.string.zen_mode_qs_set_behavior);
}
}
// DND turned on by an automatic rule with deprecated zen mode
for (ZenModeConfig.ZenRule automaticRule : config.automaticRules.values()) {
if (automaticRule.isAutomaticActive() && isDeprecatedZenMode(automaticRule.zenMode)) {
ComponentName component = automaticRule.component;
if (component != null) {
return mContext.getString(R.string.zen_mode_app_set_behavior,
component.getPackageName());
}
}
}
return mContext.getString(R.string.zen_mode_unknown_app_set_behavior);
}
private boolean isDeprecatedZenMode(int zenMode) {
switch (zenMode) {
case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
case Settings.Global.ZEN_MODE_ALARMS:
return true;
default:
return false;
}
}
}

View File

@@ -48,6 +48,7 @@ public class ZenModeBehaviorSettings extends ZenModeSettingsBase implements Inde
controllers.add(new ZenModeRepeatCallersPreferenceController(context, lifecycle));
controllers.add(new ZenModeScreenOnPreferenceController(context, lifecycle));
controllers.add(new ZenModeScreenOffPreferenceController(context, lifecycle));
controllers.add(new ZenModeBehaviorFooterPreferenceController(context, lifecycle));
return controllers;
}

View File

@@ -64,6 +64,7 @@ public class ZenModeSettings extends ZenModeSettingsBase {
controllers.add(new ZenModeBehaviorPreferenceController(context, lifecycle));
controllers.add(new ZenModeAutomationPreferenceController(context));
controllers.add(new ZenModeButtonPreferenceController(context, lifecycle));
controllers.add(new ZenModeSettingsFooterPreferenceController(context, lifecycle));
return controllers;
}

View File

@@ -0,0 +1,118 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import android.content.Context;
import android.net.Uri;
import android.provider.Settings;
import android.service.notification.ZenModeConfig;
import android.support.v7.preference.Preference;
import com.android.settings.R;
import com.android.settingslib.core.lifecycle.Lifecycle;
public class ZenModeSettingsFooterPreferenceController extends AbstractZenModePreferenceController {
protected static final String KEY = "footer_preference";
public ZenModeSettingsFooterPreferenceController(Context context, Lifecycle lifecycle) {
super(context, KEY, lifecycle);
}
@Override
public boolean isAvailable() {
switch(getZenMode()) {
case Settings.Global.ZEN_MODE_ALARMS:
case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
case Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS:
return true;
case Settings.Global.ZEN_MODE_OFF:
default:
return false;
}
}
@Override
public String getPreferenceKey() {
return KEY;
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
boolean isAvailable = isAvailable();
preference.setVisible(isAvailable);
if (isAvailable) {
preference.setTitle(getFooterText());
}
}
protected String getFooterText() {
ZenModeConfig config = getZenModeConfig();
String footerText = "";
long latestEndTime = -1;
// DND turned on by manual rule
if (config.manualRule != null) {
final Uri id = config.manualRule.conditionId;
if (config.manualRule.enabler != null) {
// app triggered manual rule
String appOwner = mZenModeConfigWrapper.getOwnerCaption(config.manualRule.enabler);
if (!appOwner.isEmpty()) {
footerText = mContext.getString(
R.string.zen_mode_settings_dnd_automatic_rule_app, appOwner);
}
} else {
if (id == null) {
return mContext.getString(
R.string.zen_mode_settings_dnd_manual_indefinite);
} else {
latestEndTime = mZenModeConfigWrapper.parseManualRuleTime(id);
if (latestEndTime > 0) {
final CharSequence formattedTime = mZenModeConfigWrapper.getFormattedTime(
latestEndTime, mContext.getUserId());
footerText = mContext.getString(
R.string.zen_mode_settings_dnd_manual_end_time,
formattedTime);
}
}
}
}
// DND turned on by an automatic rule
for (ZenModeConfig.ZenRule automaticRule : config.automaticRules.values()) {
if (automaticRule.isAutomaticActive()) {
// set footer if 3rd party rule
if (!mZenModeConfigWrapper.isTimeRule(automaticRule.conditionId)) {
return mContext.getString(R.string.zen_mode_settings_dnd_automatic_rule,
automaticRule.name);
} else {
// set footer if automatic rule end time is the latest active rule end time
long endTime = mZenModeConfigWrapper.parseAutomaticRuleEndTime(
automaticRule.conditionId);
if (endTime > latestEndTime) {
latestEndTime = endTime;
footerText = mContext.getString(
R.string.zen_mode_settings_dnd_automatic_rule, automaticRule.name);
}
}
}
}
return footerText;
}
}