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.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 NotificationBackend.AppRow appRow = 76 backend.loadAppRow(context, context.getPackageManager(), appEntry.info); 77 return getNotificationSummary(appRow, context); 78 } 79 getNotificationSummary(NotificationBackend.AppRow appRow, Context context)80 public static CharSequence getNotificationSummary(NotificationBackend.AppRow appRow, 81 Context context) { 82 if (appRow == null) { 83 return ""; 84 } 85 if (appRow.banned) { 86 return context.getText(R.string.notifications_disabled); 87 } else if (appRow.channelCount == 0) { 88 return NotificationBackend.getSentSummary(context, appRow.sentByApp, false); 89 } else if (appRow.channelCount == appRow.blockedChannelCount) { 90 return context.getText(R.string.notifications_disabled); 91 } else { 92 if (appRow.blockedChannelCount == 0) { 93 return NotificationBackend.getSentSummary(context, appRow.sentByApp, false); 94 } 95 return context.getString(R.string.notifications_enabled_with_info, 96 NotificationBackend.getSentSummary(context, appRow.sentByApp, false), 97 context.getResources().getQuantityString(R.plurals.notifications_categories_off, 98 appRow.blockedChannelCount, appRow.blockedChannelCount)); 99 } 100 } 101 } 102