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