• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.settings.applications.appinfo;
18 
19 import static com.android.settings.SettingsActivity.EXTRA_FRAGMENT_ARG_KEY;
20 
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.support.v7.preference.Preference;
24 
25 import com.android.settings.R;
26 import com.android.settings.SettingsPreferenceFragment;
27 import com.android.settings.notification.AppNotificationSettings;
28 import com.android.settings.notification.NotificationBackend;
29 import com.android.settingslib.applications.ApplicationsState;
30 
31 public 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 
AppNotificationPreferenceController(Context context, String key)38     public AppNotificationPreferenceController(Context context, String key) {
39         super(context, key);
40     }
41 
42     @Override
setParentFragment(AppInfoDashboardFragment parent)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
updateState(Preference preference)52     public void updateState(Preference preference) {
53         preference.setSummary(getNotificationSummary(mParent.getAppEntry(), mContext, mBackend));
54     }
55 
56     @Override
getDetailFragmentClass()57     protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() {
58         return AppNotificationSettings.class;
59     }
60 
61     @Override
getArguments()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 
getNotificationSummary(ApplicationsState.AppEntry appEntry, Context context, NotificationBackend backend)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 
getNotificationSummary(NotificationBackend.AppRow appRow, Context context)79     public static CharSequence getNotificationSummary(NotificationBackend.AppRow appRow,
80             Context context) {
81         if (appRow == null) {
82             return "";
83         }
84         if (appRow.banned) {
85             return context.getText(R.string.notifications_disabled);
86         } else if (appRow.channelCount == 0) {
87             return context.getText(R.string.notifications_enabled);
88         } else if (appRow.channelCount == appRow.blockedChannelCount) {
89             return context.getText(R.string.notifications_disabled);
90         } else {
91             if (appRow.blockedChannelCount == 0) {
92                 return context.getText(R.string.notifications_enabled);
93             }
94             return context.getString(R.string.notifications_enabled_with_info,
95                     context.getResources().getQuantityString(R.plurals.notifications_categories_off,
96                             appRow.blockedChannelCount, appRow.blockedChannelCount));
97         }
98     }
99 }
100