1/*
2 * Copyright (C) 2014 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
17package com.android.settings.notification;
18
19import android.app.NotificationManager;
20import android.app.NotificationManager.Policy;
21import android.os.Bundle;
22import android.support.v7.preference.Preference;
23import android.support.v7.preference.PreferenceScreen;
24import android.view.Menu;
25import android.view.MenuInflater;
26import android.view.MenuItem;
27
28import com.android.internal.logging.MetricsProto.MetricsEvent;
29import com.android.settings.R;
30import com.android.settings.SettingsActivity;
31
32public class ZenModeSettings extends ZenModeSettingsBase {
33    private static final String KEY_PRIORITY_SETTINGS = "priority_settings";
34    private static final String KEY_VISUAL_SETTINGS = "visual_interruptions_settings";
35
36    private Preference mPrioritySettings;
37    private Preference mVisualSettings;
38    private Policy mPolicy;
39
40    @Override
41    public void onCreate(Bundle savedInstanceState) {
42        super.onCreate(savedInstanceState);
43
44        addPreferencesFromResource(R.xml.zen_mode_settings);
45        final PreferenceScreen root = getPreferenceScreen();
46
47        mPrioritySettings = root.findPreference(KEY_PRIORITY_SETTINGS);
48        mVisualSettings = root.findPreference(KEY_VISUAL_SETTINGS);
49        mPolicy = NotificationManager.from(mContext).getNotificationPolicy();
50    }
51
52    @Override
53    public void onResume() {
54        super.onResume();
55        if (isUiRestricted()) {
56            return;
57        }
58    }
59
60    @Override
61    protected int getMetricsCategory() {
62        return MetricsEvent.NOTIFICATION_ZEN_MODE;
63    }
64
65    @Override
66    protected void onZenModeChanged() {
67        updateControls();
68    }
69
70    @Override
71    protected void onZenModeConfigChanged() {
72        mPolicy = NotificationManager.from(mContext).getNotificationPolicy();
73        updateControls();
74    }
75
76    private void updateControls() {
77        updatePrioritySettingsSummary();
78        updateVisualSettingsSummary();
79    }
80
81    private void updatePrioritySettingsSummary() {
82        String s = getResources().getString(R.string.zen_mode_alarms);
83        s = appendLowercase(s, isCategoryEnabled(mPolicy, Policy.PRIORITY_CATEGORY_REMINDERS),
84                R.string.zen_mode_reminders);
85        s = appendLowercase(s, isCategoryEnabled(mPolicy, Policy.PRIORITY_CATEGORY_EVENTS),
86                R.string.zen_mode_events);
87        if (isCategoryEnabled(mPolicy, Policy.PRIORITY_CATEGORY_MESSAGES)) {
88            if (mPolicy.priorityMessageSenders == Policy.PRIORITY_SENDERS_ANY) {
89                s = appendLowercase(s, true, R.string.zen_mode_all_messages);
90            } else {
91                s = appendLowercase(s, true, R.string.zen_mode_selected_messages);
92            }
93        }
94        if (isCategoryEnabled(mPolicy, Policy.PRIORITY_CATEGORY_CALLS)) {
95            if (mPolicy.priorityCallSenders == Policy.PRIORITY_SENDERS_ANY) {
96                s = appendLowercase(s, true, R.string.zen_mode_all_callers);
97            } else {
98                s = appendLowercase(s, true, R.string.zen_mode_selected_callers);
99            }
100        } else if (isCategoryEnabled(mPolicy, Policy.PRIORITY_CATEGORY_REPEAT_CALLERS)) {
101            s = appendLowercase(s, true, R.string.zen_mode_repeat_callers);
102        }
103        mPrioritySettings.setSummary(s);
104    }
105
106    private void updateVisualSettingsSummary() {
107        String s = getString(R.string.zen_mode_all_visual_interruptions);
108        if (isEffectSuppressed(Policy.SUPPRESSED_EFFECT_SCREEN_ON)
109                && isEffectSuppressed(Policy.SUPPRESSED_EFFECT_SCREEN_OFF)) {
110            s = getString(R.string.zen_mode_no_visual_interruptions);
111        } else if (isEffectSuppressed(Policy.SUPPRESSED_EFFECT_SCREEN_ON)) {
112            s = getString(R.string.zen_mode_screen_on_visual_interruptions);
113        } else if (isEffectSuppressed(Policy.SUPPRESSED_EFFECT_SCREEN_OFF)) {
114            s = getString(R.string.zen_mode_screen_off_visual_interruptions);
115        }
116        mVisualSettings.setSummary(s);
117    }
118
119    private boolean isEffectSuppressed(int effect) {
120        return (mPolicy.suppressedVisualEffects & effect) != 0;
121    }
122
123    private boolean isCategoryEnabled(Policy policy, int categoryType) {
124        return (policy.priorityCategories & categoryType) != 0;
125    }
126
127    private String appendLowercase(String s, boolean condition, int resId) {
128        if (condition) {
129            return getResources().getString(R.string.join_many_items_middle, s,
130                    getResources().getString(resId).toLowerCase());
131        }
132        return s;
133    }
134
135    @Override
136    protected int getHelpResource() {
137        return R.string.help_uri_interruptions;
138    }
139}
140