/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings.notification; import android.app.Notification; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.service.notification.NotificationListenerService.Ranking; import android.support.v7.preference.Preference; import android.support.v7.preference.Preference.OnPreferenceChangeListener; import android.support.v7.preference.Preference.OnPreferenceClickListener; import android.support.v7.preference.PreferenceCategory; import android.util.ArrayMap; import android.util.Log; import com.android.internal.logging.MetricsLogger; import com.android.internal.logging.MetricsProto.MetricsEvent; import com.android.settings.AppHeader; import com.android.settings.R; import com.android.settings.Utils; import com.android.settings.applications.AppInfoBase; import com.android.settings.applications.LayoutPreference; import com.android.settings.notification.NotificationBackend.AppRow; import com.android.settingslib.RestrictedLockUtils; import com.android.settingslib.RestrictedSwitchPreference; import com.android.settingslib.RestrictedPreference; import java.util.List; /** These settings are per app, so should not be returned in global search results. */ public class AppNotificationSettings extends NotificationSettingsBase { private static final String TAG = "AppNotificationSettings"; private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); private static final String KEY_BLOCK = "block"; private static final String KEY_CATEGORIES = "categories"; private static final Intent APP_NOTIFICATION_PREFS_CATEGORY_INTENT = new Intent(Intent.ACTION_MAIN) .addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES); private RestrictedSwitchPreference mBlock; private PreferenceCategory mCategories; private AppRow mAppRow; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (mAppRow == null) return; AppHeader.createAppHeader(this, mAppRow.icon, mAppRow.label, mAppRow.pkg, mAppRow.uid, mAppRow.settingsIntent); } @Override protected int getMetricsCategory() { return MetricsEvent.NOTIFICATION_APP_NOTIFICATION; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.app_notification_settings); mCategories = (PreferenceCategory) findPreference(KEY_CATEGORIES); mBlock = (RestrictedSwitchPreference) findPreference(KEY_BLOCK); mImportance = (ImportanceSeekBarPreference) findPreference(KEY_IMPORTANCE); mImportanceReset = (LayoutPreference) findPreference(KEY_IMPORTANCE_RESET); mImportanceTitle = (RestrictedPreference) findPreference(KEY_IMPORTANCE_TITLE); mPriority = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_BYPASS_DND); mSensitive = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_SENSITIVE); mAppRow = mBackend.loadAppRow(mPm, mPkgInfo); // load settings intent ArrayMap rows = new ArrayMap(); rows.put(mAppRow.pkg, mAppRow); collectConfigActivities(rows); // Add topics List topics = mBackend.getTopics(mPkg, mUid); if (topics.size() <= 1) { removeAppPrefs(); setupImportancePrefs(mAppRow.systemApp, mAppRow.appImportance); setupPriorityPref(mAppRow.appBypassDnd); setupSensitivePref(mAppRow.appSensitive); } else { removeTopicPrefs(); setupBlockSwitch(); for (Notification.Topic topic : topics) { RestrictedPreference topicPreference = new RestrictedPreference(getPrefContext()); topicPreference.setDisabledByAdmin(mSuspendedAppsAdmin); topicPreference.setKey(topic.getId()); topicPreference.setTitle(topic.getLabel()); // Create intent for this preference. Bundle topicArgs = new Bundle(); topicArgs.putInt(AppInfoBase.ARG_PACKAGE_UID, mUid); topicArgs.putParcelable(TopicNotificationSettings.ARG_TOPIC, topic); topicArgs.putBoolean(AppHeader.EXTRA_HIDE_INFO_BUTTON, true); topicArgs.putString(AppInfoBase.ARG_PACKAGE_NAME, mPkg); topicArgs.putParcelable(TopicNotificationSettings.ARG_PACKAGE_INFO, mPkgInfo); Intent topicIntent = Utils.onBuildStartFragmentIntent(getActivity(), TopicNotificationSettings.class.getName(), topicArgs, null, R.string.topic_notifications_title, null, false); topicPreference.setIntent(topicIntent); mCategories.addPreference(topicPreference); } } } @Override protected void updateDependents(int progress) { updateDependents(progress == Ranking.IMPORTANCE_NONE); } private void removeTopicPrefs() { setVisible(mImportance, false); setVisible(mImportanceReset, false); setVisible(mImportanceTitle, false); setVisible(mPriority, false); setVisible(mSensitive, false); } private void removeAppPrefs() { setVisible(mBlock, false); setVisible(mCategories, false); } private void updateDependents(boolean banned) { mBlock.setEnabled(!mAppRow.systemApp); mCategories.setEnabled(!banned); } private void setupBlockSwitch() { mBlock.setDisabledByAdmin(mSuspendedAppsAdmin); mBlock.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { final boolean banned = (Boolean) newValue; if (banned) { MetricsLogger.action(getActivity(), MetricsEvent.ACTION_BAN_APP_NOTES, mPkg); } final boolean success = mBackend.setNotificationsBanned(mPkg, mUid, banned); if (success) { updateDependents(banned); } return success; } }); mBlock.setChecked(mAppRow.banned); updateDependents(mAppRow.banned); } private List queryNotificationConfigActivities() { if (DEBUG) Log.d(TAG, "APP_NOTIFICATION_PREFS_CATEGORY_INTENT is " + APP_NOTIFICATION_PREFS_CATEGORY_INTENT); final List resolveInfos = mPm.queryIntentActivities( APP_NOTIFICATION_PREFS_CATEGORY_INTENT, 0 //PackageManager.MATCH_DEFAULT_ONLY ); return resolveInfos; } private void collectConfigActivities(ArrayMap rows) { final List resolveInfos = queryNotificationConfigActivities(); applyConfigActivities(rows, resolveInfos); } private void applyConfigActivities(ArrayMap rows, List resolveInfos) { if (DEBUG) Log.d(TAG, "Found " + resolveInfos.size() + " preference activities" + (resolveInfos.size() == 0 ? " ;_;" : "")); for (ResolveInfo ri : resolveInfos) { final ActivityInfo activityInfo = ri.activityInfo; final ApplicationInfo appInfo = activityInfo.applicationInfo; final AppRow row = rows.get(appInfo.packageName); if (row == null) { if (DEBUG) Log.v(TAG, "Ignoring notification preference activity (" + activityInfo.name + ") for unknown package " + activityInfo.packageName); continue; } if (row.settingsIntent != null) { if (DEBUG) Log.v(TAG, "Ignoring duplicate notification preference activity (" + activityInfo.name + ") for package " + activityInfo.packageName); continue; } row.settingsIntent = new Intent(APP_NOTIFICATION_PREFS_CATEGORY_INTENT) .setClassName(activityInfo.packageName, activityInfo.name); } } }