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