AppNotificationPreferenceController.java revision 78756b18804461bc155423d0f96b3c0eab515b43
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.applications.appinfo;
18
19import static com.android.settings.SettingsActivity.EXTRA_FRAGMENT_ARG_KEY;
20
21import android.content.Context;
22import android.os.Bundle;
23import android.support.v7.preference.Preference;
24
25import com.android.settings.R;
26import com.android.settings.SettingsPreferenceFragment;
27import com.android.settings.notification.AppNotificationSettings;
28import com.android.settings.notification.NotificationBackend;
29import com.android.settingslib.applications.ApplicationsState;
30
31public class AppNotificationPreferenceController extends AppInfoPreferenceControllerBase {
32
33    private String mChannelId = null;
34
35    // Used for updating notification preference.
36    private final NotificationBackend mBackend = new NotificationBackend();
37
38    public AppNotificationPreferenceController(Context context, String key) {
39        super(context, key);
40    }
41
42    @Override
43    public void setParentFragment(AppInfoDashboardFragment parent) {
44        super.setParentFragment(parent);
45        if (parent != null && parent.getActivity() != null
46                && parent.getActivity().getIntent() != null) {
47            mChannelId = parent.getActivity().getIntent().getStringExtra(EXTRA_FRAGMENT_ARG_KEY);
48        }
49    }
50
51    @Override
52    public void updateState(Preference preference) {
53        preference.setSummary(getNotificationSummary(mParent.getAppEntry(), mContext, mBackend));
54    }
55
56    @Override
57    protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() {
58        return AppNotificationSettings.class;
59    }
60
61    @Override
62    protected Bundle getArguments() {
63        Bundle bundle = null;
64        if (mChannelId != null) {
65            bundle = new Bundle();
66            bundle.putString(EXTRA_FRAGMENT_ARG_KEY, mChannelId);
67        }
68        return bundle;
69    }
70
71
72    private CharSequence getNotificationSummary(ApplicationsState.AppEntry appEntry,
73            Context context, NotificationBackend backend) {
74        NotificationBackend.AppRow appRow =
75                backend.loadAppRow(context, context.getPackageManager(), appEntry.info);
76        return getNotificationSummary(appRow, context);
77    }
78
79    public static CharSequence getNotificationSummary(NotificationBackend.AppRow appRow,
80            Context context) {
81        if (appRow == null) {
82            return "";
83        }
84        if (appRow.banned || appRow.channelCount == appRow.blockedChannelCount) {
85            return context.getString(R.string.notifications_disabled);
86        } else {
87            if (appRow.blockedChannelCount == 0) {
88                return context.getString(R.string.notifications_enabled);
89            }
90            return context.getString(R.string.notifications_enabled_with_info,
91                    context.getResources().getQuantityString(R.plurals.notifications_categories_off,
92                            appRow.blockedChannelCount, appRow.blockedChannelCount));
93        }
94    }
95}
96