1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.notification.zen; 18 19 import static android.provider.Settings.EXTRA_DO_NOT_DISTURB_MODE_ENABLED; 20 import static android.provider.Settings.EXTRA_DO_NOT_DISTURB_MODE_MINUTES; 21 22 import android.app.NotificationManager; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.icu.text.MessageFormat; 26 import android.media.AudioManager; 27 import android.os.UserHandle; 28 import android.provider.Settings.Global; 29 import android.service.notification.Condition; 30 import android.service.notification.ZenModeConfig; 31 import android.text.format.DateFormat; 32 import android.util.Log; 33 34 import com.android.settings.R; 35 import com.android.settings.utils.VoiceSettingsActivity; 36 37 import java.util.HashMap; 38 import java.util.Locale; 39 import java.util.Map; 40 41 /** 42 * Activity for modifying the Zen mode (Do not disturb) by voice 43 * using the Voice Interaction API. 44 */ 45 public class ZenModeVoiceActivity extends VoiceSettingsActivity { 46 private static final String TAG = "ZenModeVoiceActivity"; 47 private static final int MINUTES_MS = 60 * 1000; 48 49 @Override onVoiceSettingInteraction(Intent intent)50 protected boolean onVoiceSettingInteraction(Intent intent) { 51 if (intent.hasExtra(EXTRA_DO_NOT_DISTURB_MODE_ENABLED)) { 52 int minutes = intent.getIntExtra(EXTRA_DO_NOT_DISTURB_MODE_MINUTES, -1); 53 Condition condition = null; 54 int mode = Global.ZEN_MODE_OFF; 55 56 if (intent.getBooleanExtra(EXTRA_DO_NOT_DISTURB_MODE_ENABLED, false)) { 57 if (minutes > 0) { 58 condition = ZenModeConfig.toTimeCondition(this, minutes, UserHandle.myUserId()); 59 } 60 mode = Global.ZEN_MODE_ALARMS; 61 } 62 setZenModeConfig(mode, condition); 63 64 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 65 if (audioManager != null) { 66 // Show the current Zen Mode setting. 67 audioManager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, 68 AudioManager.ADJUST_SAME, 69 AudioManager.FLAG_SHOW_UI); 70 } 71 notifySuccess(getChangeSummary(mode, minutes)); 72 } else { 73 Log.v(TAG, "Missing extra android.provider.Settings.EXTRA_DO_NOT_DISTURB_MODE_ENABLED"); 74 finish(); 75 } 76 return false; 77 } 78 setZenModeConfig(int mode, Condition condition)79 private void setZenModeConfig(int mode, Condition condition) { 80 if (condition != null) { 81 NotificationManager.from(this).setZenMode(mode, condition.id, TAG); 82 } else { 83 NotificationManager.from(this).setZenMode(mode, null, TAG); 84 } 85 } 86 87 /** 88 * Produce a summary of the Zen mode change to be read aloud as TTS. 89 */ getChangeSummary(int mode, int minutes)90 private CharSequence getChangeSummary(int mode, int minutes) { 91 int indefinite = -1; 92 93 switch (mode) { 94 case Global.ZEN_MODE_ALARMS: 95 indefinite = R.string.zen_mode_summary_alarms_only_indefinite; 96 break; 97 case Global.ZEN_MODE_OFF: 98 indefinite = R.string.zen_mode_summary_always; 99 break; 100 }; 101 102 if (minutes < 0 || mode == Global.ZEN_MODE_OFF) { 103 return getString(indefinite); 104 } 105 106 long time = System.currentTimeMillis() + minutes * MINUTES_MS; 107 String skeleton = DateFormat.is24HourFormat(this, UserHandle.myUserId()) ? "Hm" : "hma"; 108 String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton); 109 CharSequence formattedTime = DateFormat.format(pattern, time); 110 111 if (minutes < 60) { 112 return buildMessage(R.string.zen_mode_summary_alarms_only_by_minute, minutes, formattedTime); 113 } else if (minutes % 60 != 0) { 114 return getResources().getString(R.string.zen_mode_summary_alarms_only_by_time, formattedTime); 115 } else { 116 int hours = minutes / 60; 117 return buildMessage(R.string.zen_mode_summary_alarms_only_by_hour, hours, formattedTime); 118 } 119 } 120 buildMessage(int resId, int count, CharSequence formattedTime)121 private CharSequence buildMessage(int resId, int count, CharSequence formattedTime) { 122 MessageFormat msgFormat = new MessageFormat( 123 getResources().getString(resId), Locale.getDefault()); 124 Map<String, Object> arguments = new HashMap<>(); 125 arguments.put("count", count); 126 arguments.put("time", formattedTime); 127 return msgFormat.format(arguments); 128 } 129 } 130