summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/notification/ZenModeVoiceActivity.java
blob: 270633a9a6fd3969cc73f4ff06a7e4f285449a2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (C) 2015 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 static android.provider.Settings.EXTRA_DO_NOT_DISTURB_MODE_MINUTES;
import static android.provider.Settings.EXTRA_DO_NOT_DISTURB_MODE_ENABLED;

import com.android.settings.R;
import com.android.settings.utils.VoiceSettingsActivity;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.UserHandle;
import android.media.AudioManager;
import android.provider.Settings.Global;
import android.service.notification.Condition;
import android.service.notification.ZenModeConfig;
import android.text.format.DateFormat;
import android.util.Log;

import java.util.Locale;

/**
 * Activity for modifying the Zen mode (Do not disturb) by voice
 * using the Voice Interaction API.
 */
public class ZenModeVoiceActivity extends VoiceSettingsActivity {
    private static final String TAG = "ZenModeVoiceActivity";
    private static final int MINUTES_MS = 60 * 1000;

    @Override
    protected boolean onVoiceSettingInteraction(Intent intent) {
        if (intent.hasExtra(EXTRA_DO_NOT_DISTURB_MODE_ENABLED)) {
            int minutes = intent.getIntExtra(EXTRA_DO_NOT_DISTURB_MODE_MINUTES, -1);
            Condition condition = null;
            int mode = Global.ZEN_MODE_OFF;

            if (intent.getBooleanExtra(EXTRA_DO_NOT_DISTURB_MODE_ENABLED, false)) {
                if (minutes > 0) {
                    condition = ZenModeConfig.toTimeCondition(this, minutes, UserHandle.myUserId());
                }
                mode = Global.ZEN_MODE_ALARMS;
            }
            setZenModeConfig(mode, condition);

            AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            if (audioManager != null) {
                // Show the current Zen Mode setting.
                audioManager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION,
                         AudioManager.ADJUST_SAME,
                         AudioManager.FLAG_SHOW_UI);
            }
            notifySuccess(getChangeSummary(mode, minutes));
        } else {
            Log.v(TAG, "Missing extra android.provider.Settings.EXTRA_DO_NOT_DISTURB_MODE_ENABLED");
            finish();
        }
        return false;
    }

    private void setZenModeConfig(int mode, Condition condition) {
        if (condition != null) {
            NotificationManager.from(this).setZenMode(mode, condition.id, TAG);
        } else {
            NotificationManager.from(this).setZenMode(mode, null, TAG);
        }
     }

    /**
     * Produce a summary of the Zen mode change to be read aloud as TTS.
     */
    private CharSequence getChangeSummary(int mode, int minutes) {
        int indefinite = -1;
        int byMinute = -1;
        int byHour = -1;
        int byTime = -1;

        switch (mode) {
            case Global.ZEN_MODE_ALARMS:
                indefinite = R.string.zen_mode_summary_alarms_only_indefinite;
                byMinute = R.plurals.zen_mode_summary_alarms_only_by_minute;
                byHour = R.plurals.zen_mode_summary_alarms_only_by_hour;
                byTime = R.string.zen_mode_summary_alarms_only_by_time;
                break;
            case Global.ZEN_MODE_OFF:
                indefinite = R.string.zen_mode_summary_always;
                break;
        };

        if (minutes < 0 || mode == Global.ZEN_MODE_OFF) {
            return getString(indefinite);
        }

        long time = System.currentTimeMillis() + minutes * MINUTES_MS;
        String skeleton = DateFormat.is24HourFormat(this, UserHandle.myUserId()) ? "Hm" : "hma";
        String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton);
        CharSequence formattedTime = DateFormat.format(pattern, time);
        Resources res = getResources();

        if (minutes < 60) {
            return res.getQuantityString(byMinute, minutes, minutes, formattedTime);
        } else if (minutes % 60 != 0) {
            return res.getString(byTime, formattedTime);
        } else {
            int hours = minutes / 60;
            return res.getQuantityString(byHour, hours, hours, formattedTime);
        }
    }
}