• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
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.content.res.Resources;
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.Locale;
38 
39 /**
40  * Activity for modifying the Zen mode (Do not disturb) by voice
41  * using the Voice Interaction API.
42  */
43 public class ZenModeVoiceActivity extends VoiceSettingsActivity {
44     private static final String TAG = "ZenModeVoiceActivity";
45     private static final int MINUTES_MS = 60 * 1000;
46 
47     @Override
onVoiceSettingInteraction(Intent intent)48     protected boolean onVoiceSettingInteraction(Intent intent) {
49         if (intent.hasExtra(EXTRA_DO_NOT_DISTURB_MODE_ENABLED)) {
50             int minutes = intent.getIntExtra(EXTRA_DO_NOT_DISTURB_MODE_MINUTES, -1);
51             Condition condition = null;
52             int mode = Global.ZEN_MODE_OFF;
53 
54             if (intent.getBooleanExtra(EXTRA_DO_NOT_DISTURB_MODE_ENABLED, false)) {
55                 if (minutes > 0) {
56                     condition = ZenModeConfig.toTimeCondition(this, minutes, UserHandle.myUserId());
57                 }
58                 mode = Global.ZEN_MODE_ALARMS;
59             }
60             setZenModeConfig(mode, condition);
61 
62             AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
63             if (audioManager != null) {
64                 // Show the current Zen Mode setting.
65                 audioManager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION,
66                          AudioManager.ADJUST_SAME,
67                          AudioManager.FLAG_SHOW_UI);
68             }
69             notifySuccess(getChangeSummary(mode, minutes));
70         } else {
71             Log.v(TAG, "Missing extra android.provider.Settings.EXTRA_DO_NOT_DISTURB_MODE_ENABLED");
72             finish();
73         }
74         return false;
75     }
76 
setZenModeConfig(int mode, Condition condition)77     private void setZenModeConfig(int mode, Condition condition) {
78         if (condition != null) {
79             NotificationManager.from(this).setZenMode(mode, condition.id, TAG);
80         } else {
81             NotificationManager.from(this).setZenMode(mode, null, TAG);
82         }
83      }
84 
85     /**
86      * Produce a summary of the Zen mode change to be read aloud as TTS.
87      */
getChangeSummary(int mode, int minutes)88     private CharSequence getChangeSummary(int mode, int minutes) {
89         int indefinite = -1;
90         int byMinute = -1;
91         int byHour = -1;
92         int byTime = -1;
93 
94         switch (mode) {
95             case Global.ZEN_MODE_ALARMS:
96                 indefinite = R.string.zen_mode_summary_alarms_only_indefinite;
97                 byMinute = R.plurals.zen_mode_summary_alarms_only_by_minute;
98                 byHour = R.plurals.zen_mode_summary_alarms_only_by_hour;
99                 byTime = R.string.zen_mode_summary_alarms_only_by_time;
100                 break;
101             case Global.ZEN_MODE_OFF:
102                 indefinite = R.string.zen_mode_summary_always;
103                 break;
104         };
105 
106         if (minutes < 0 || mode == Global.ZEN_MODE_OFF) {
107             return getString(indefinite);
108         }
109 
110         long time = System.currentTimeMillis() + minutes * MINUTES_MS;
111         String skeleton = DateFormat.is24HourFormat(this, UserHandle.myUserId()) ? "Hm" : "hma";
112         String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton);
113         CharSequence formattedTime = DateFormat.format(pattern, time);
114         Resources res = getResources();
115 
116         if (minutes < 60) {
117             return res.getQuantityString(byMinute, minutes, minutes, formattedTime);
118         } else if (minutes % 60 != 0) {
119             return res.getString(byTime, formattedTime);
120         } else {
121             int hours = minutes / 60;
122             return res.getQuantityString(byHour, hours, hours, formattedTime);
123         }
124     }
125 }
126