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.content.Context;
20import android.content.res.Resources;
21import android.os.Bundle;
22import android.preference.Preference;
23import android.preference.PreferenceScreen;
24import android.util.SparseArray;
25
26import com.android.internal.logging.MetricsLogger;
27import com.android.settings.R;
28import com.android.settings.search.BaseSearchIndexProvider;
29import com.android.settings.search.Indexable;
30import com.android.settings.search.SearchIndexableRaw;
31
32import java.util.ArrayList;
33import java.util.List;
34
35public class ZenModeSettings extends ZenModeSettingsBase implements Indexable {
36    private static final String KEY_PRIORITY_SETTINGS = "priority_settings";
37    private static final String KEY_AUTOMATION_SETTINGS = "automation_settings";
38
39    private Preference mPrioritySettings;
40
41    @Override
42    public void onCreate(Bundle savedInstanceState) {
43        super.onCreate(savedInstanceState);
44
45        addPreferencesFromResource(R.xml.zen_mode_settings);
46        final PreferenceScreen root = getPreferenceScreen();
47
48        mPrioritySettings = root.findPreference(KEY_PRIORITY_SETTINGS);
49        if (!isScheduleSupported(mContext)) {
50            removePreference(KEY_AUTOMATION_SETTINGS);
51        }
52    }
53
54    @Override
55    public void onResume() {
56        super.onResume();
57        updateControls();
58    }
59
60    @Override
61    protected int getMetricsCategory() {
62        return MetricsLogger.NOTIFICATION_ZEN_MODE;
63    }
64
65    @Override
66    protected void onZenModeChanged() {
67        updateControls();
68    }
69
70    @Override
71    protected void onZenModeConfigChanged() {
72        updateControls();
73    }
74
75    private void updateControls() {
76        updatePrioritySettingsSummary();
77    }
78
79    private void updatePrioritySettingsSummary() {
80        final boolean callers = mConfig.allowCalls || mConfig.allowRepeatCallers;
81        String s = getResources().getString(R.string.zen_mode_alarms);
82        s = appendLowercase(s, mConfig.allowReminders, R.string.zen_mode_reminders);
83        s = appendLowercase(s, mConfig.allowEvents, R.string.zen_mode_events);
84        s = appendLowercase(s, callers, R.string.zen_mode_selected_callers);
85        s = appendLowercase(s, mConfig.allowMessages, R.string.zen_mode_selected_messages);
86        mPrioritySettings.setSummary(s);
87    }
88
89    private String appendLowercase(String s, boolean condition, int resId) {
90        if (condition) {
91            return getResources().getString(R.string.join_many_items_middle, s,
92                    getResources().getString(resId).toLowerCase());
93        }
94        return s;
95    }
96
97    private static SparseArray<String> allKeyTitles(Context context) {
98        final SparseArray<String> rt = new SparseArray<String>();
99        rt.put(R.string.zen_mode_priority_settings_title, KEY_PRIORITY_SETTINGS);
100        rt.put(R.string.zen_mode_automation_settings_title, KEY_AUTOMATION_SETTINGS);
101        return rt;
102    }
103
104    @Override
105    protected int getHelpResource() {
106        return R.string.help_uri_interruptions;
107    }
108
109    // Enable indexing of searchable data
110    public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
111        new BaseSearchIndexProvider() {
112
113            @Override
114            public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
115                final SparseArray<String> keyTitles = allKeyTitles(context);
116                final int N = keyTitles.size();
117                final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>(N);
118                final Resources res = context.getResources();
119                for (int i = 0; i < N; i++) {
120                    final SearchIndexableRaw data = new SearchIndexableRaw(context);
121                    data.key = keyTitles.valueAt(i);
122                    data.title = res.getString(keyTitles.keyAt(i));
123                    data.screenTitle = res.getString(R.string.zen_mode_settings_title);
124                    result.add(data);
125                }
126                return result;
127            }
128
129            @Override
130            public List<String> getNonIndexableKeys(Context context) {
131                final ArrayList<String> rt = new ArrayList<String>();
132                if (!isScheduleSupported(context)) {
133                    rt.add(KEY_AUTOMATION_SETTINGS);
134                }
135                return rt;
136            }
137        };
138}
139