1/*
2 * Copyright (C) 2017 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 static android.app.NotificationChannel.USER_LOCKED_IMPORTANCE;
20import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
21import static android.app.NotificationManager.IMPORTANCE_HIGH;
22import static android.app.NotificationManager.IMPORTANCE_LOW;
23import static android.app.NotificationManager.IMPORTANCE_MAX;
24import static android.app.NotificationManager.IMPORTANCE_MIN;
25
26import android.content.Context;
27import android.provider.SearchIndexableResource;
28import android.support.v7.preference.Preference;
29import android.support.v7.preference.PreferenceScreen;
30import android.text.TextUtils;
31import android.util.Log;
32
33import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
34import com.android.settings.R;
35import com.android.settings.search.BaseSearchIndexProvider;
36import com.android.settings.search.Indexable;
37import com.android.settings.widget.RadioButtonPreference;
38
39import java.util.ArrayList;
40import java.util.List;
41
42public class ChannelImportanceSettings extends NotificationSettingsBase
43        implements RadioButtonPreference.OnClickListener, Indexable {
44    private static final String TAG = "NotiImportance";
45
46    private static final String KEY_IMPORTANCE_HIGH = "importance_high";
47    private static final String KEY_IMPORTANCE_DEFAULT = "importance_default";
48    private static final String KEY_IMPORTANCE_LOW = "importance_low";
49    private static final String KEY_IMPORTANCE_MIN = "importance_min";
50
51    List<RadioButtonPreference> mImportances = new ArrayList<>();
52
53    @Override
54    public int getMetricsCategory() {
55        return MetricsEvent.NOTIFICATION_CHANNEL_IMPORTANCE;
56    }
57
58    @Override
59    public void onResume() {
60        super.onResume();
61        if (mUid < 0 || TextUtils.isEmpty(mPkg) || mPkgInfo == null || mChannel == null) {
62            Log.w(TAG, "Missing package or uid or packageinfo or channel");
63            finish();
64            return;
65        }
66        createPreferenceHierarchy();
67    }
68
69    @Override
70    void setupBadge() {}
71
72    @Override
73    void updateDependents(boolean banned) {}
74
75    @Override
76    public void onPause() {
77        super.onPause();
78    }
79
80    private PreferenceScreen createPreferenceHierarchy() {
81        PreferenceScreen root = getPreferenceScreen();
82        if (root != null) {
83            root.removeAll();
84        }
85        addPreferencesFromResource(R.xml.notification_importance);
86        root = getPreferenceScreen();
87
88        for (int i = 0; i < root.getPreferenceCount(); i++) {
89            Preference pref = root.getPreference(i);
90            if (pref instanceof RadioButtonPreference) {
91                RadioButtonPreference radioPref = (RadioButtonPreference) pref;
92                radioPref.setOnClickListener(this);
93                mImportances.add(radioPref);
94            }
95        }
96
97        switch (mChannel.getImportance()) {
98            case IMPORTANCE_MIN:
99                updateRadioButtons(KEY_IMPORTANCE_MIN);
100                break;
101            case IMPORTANCE_LOW:
102                updateRadioButtons(KEY_IMPORTANCE_LOW);
103                break;
104            case IMPORTANCE_DEFAULT:
105                updateRadioButtons(KEY_IMPORTANCE_DEFAULT);
106                break;
107            case IMPORTANCE_HIGH:
108            case IMPORTANCE_MAX:
109                updateRadioButtons(KEY_IMPORTANCE_HIGH);
110                break;
111        }
112
113        return root;
114    }
115
116    private void updateRadioButtons(String selectionKey) {
117        for (RadioButtonPreference pref : mImportances) {
118            if (selectionKey.equals(pref.getKey())) {
119                pref.setChecked(true);
120            } else {
121                pref.setChecked(false);
122            }
123        }
124    }
125
126    @Override
127    public void onRadioButtonClicked(RadioButtonPreference clicked) {
128        switch (clicked.getKey()) {
129            case KEY_IMPORTANCE_HIGH:
130                mChannel.setImportance(IMPORTANCE_HIGH);
131                break;
132            case KEY_IMPORTANCE_DEFAULT:
133                mChannel.setImportance(IMPORTANCE_DEFAULT);
134                break;
135            case KEY_IMPORTANCE_LOW:
136                mChannel.setImportance(IMPORTANCE_LOW);
137                break;
138            case KEY_IMPORTANCE_MIN:
139                mChannel.setImportance(IMPORTANCE_MIN);
140                break;
141        }
142        updateRadioButtons(clicked.getKey());
143        mChannel.lockFields(USER_LOCKED_IMPORTANCE);
144        mBackend.updateChannel(mAppRow.pkg, mAppRow.uid, mChannel);
145    }
146
147    // This page exists per notification channel; should not be included
148    // in search
149    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
150            new BaseSearchIndexProvider() {
151                @Override
152                public List<SearchIndexableResource> getXmlResourcesToIndex(
153                        Context context, boolean enabled) {
154                    return null;
155                }
156            };
157}
158