Allow users to set notification topic importance.

In the absence of mocks, the silder is shown in the
'normal' position if the user hasn't yet
chosen an importance value.

Bug: 22854014
Change-Id: I51594959412775fe89b29af66ddcb13bafa67255
This commit is contained in:
Julia Reynolds
2015-11-23 08:58:49 -05:00
parent 0501728c03
commit 9dbb20f638
7 changed files with 252 additions and 3 deletions

View File

@@ -0,0 +1,118 @@
/**
* Copyright (C) 2015 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 com.android.settings.R;
import com.android.settings.SeekBarPreference;
import android.content.Context;
import android.service.notification.NotificationListenerService;
import android.support.v7.preference.PreferenceViewHolder;
import android.util.AttributeSet;
import android.widget.SeekBar;
import android.widget.TextView;
/**
* A slider preference that controls notification importance.
**/
public class ImportanceSeekBarPreference extends SeekBarPreference implements
SeekBar.OnSeekBarChangeListener {
private static final String TAG = "ImportanceSeekBarPref";
public static final int IMPORTANCE_PROGRESS_OFFSET = 2;
private Callback mCallback;
private TextView mSummaryTextView;
private String mSummary;
public ImportanceSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setLayoutResource(R.layout.preference_importance_slider);
}
public ImportanceSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public ImportanceSeekBarPreference(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ImportanceSeekBarPreference(Context context) {
this(context, null);
}
public void setCallback(Callback callback) {
mCallback = callback;
}
@Override
public void onBindViewHolder(PreferenceViewHolder view) {
super.onBindViewHolder(view);
mSummaryTextView = (TextView) view.findViewById(com.android.internal.R.id.summary);
}
@Override
public void setProgress(int progress) {
mSummary = getProgressSummary(progress);
super.setProgress(progress);
}
@Override
public void onProgressChanged(SeekBar seekBar, final int progress, boolean fromTouch) {
super.onProgressChanged(seekBar, progress, fromTouch);
if (mSummaryTextView != null) {
mSummaryTextView.setText(getProgressSummary(progress));
}
if (fromTouch) {
mCallback.onImportanceChanged(progress);
}
}
@Override
public CharSequence getSummary() {
return mSummary;
}
private String getProgressSummary(int progress) {
// Map progress 0-4 values to Importance's -2-2.
progress = progress - IMPORTANCE_PROGRESS_OFFSET;
switch (progress) {
case NotificationListenerService.Ranking.IMPORTANCE_NONE:
return getContext().getString(
com.android.internal.R.string.notification_importance_blocked);
case NotificationListenerService.Ranking.IMPORTANCE_LOW:
return getContext().getString(
com.android.internal.R.string.notification_importance_low);
case NotificationListenerService.Ranking.IMPORTANCE_DEFAULT:
return getContext().getString(
com.android.internal.R.string.notification_importance_default);
case NotificationListenerService.Ranking.IMPORTANCE_HIGH:
return getContext().getString(
com.android.internal.R.string.notification_importance_high);
case NotificationListenerService.Ranking.IMPORTANCE_MAX:
return getContext().getString(
com.android.internal.R.string.notification_importance_max);
default:
return "";
}
}
public interface Callback {
void onImportanceChanged(int progress);
}
}

View File

@@ -62,6 +62,7 @@ public class NotificationBackend {
row.priority = getBypassZenMode(row.pkg, row.uid, row.topic);
row.sensitive = getSensitive(row.pkg, row.uid, row.topic);
row.banned = getNotificationsBanned(row.pkg, row.uid);
row.importance = getImportance(row.pkg, row.uid, row.topic);
return row;
}
@@ -128,6 +129,25 @@ public class NotificationBackend {
}
}
public boolean setImportance(String pkg, int uid, Notification.Topic topic, int importance) {
try {
sINM.setTopicImportance(pkg, uid, topic, importance);
return true;
} catch (Exception e) {
Log.w(TAG, "Error calling NoMan", e);
return false;
}
}
public int getImportance(String pkg, int uid, Notification.Topic topic) {
try {
return sINM.getTopicImportance(pkg, uid, topic);
} catch (Exception e) {
Log.w(TAG, "Error calling NoMan", e);
return NotificationListenerService.Ranking.IMPORTANCE_DEFAULT;
}
}
public List<Notification.Topic> getTopics(String pkg, int uid) {
try {
final ParceledListSlice<Notification.Topic> parceledList = sINM.getTopics(pkg, uid);
@@ -156,6 +176,7 @@ public class NotificationBackend {
public Notification.Topic topic;
public boolean priority;
public boolean sensitive;
public int importance;
}
}

View File

@@ -30,14 +30,13 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.UserHandle;
import android.provider.Settings;
import android.service.notification.NotificationListenerService;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.Preference.OnPreferenceChangeListener;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
@@ -50,10 +49,12 @@ public class TopicNotificationSettings extends SettingsPreferenceFragment {
protected static final String ARG_PACKAGE_INFO = "arg_info";
private static final String KEY_BYPASS_DND = "bypass_dnd";
private static final String KEY_SENSITIVE = "sensitive";
private static final String KEY_IMPORTANCE = "importance";
private final NotificationBackend mBackend = new NotificationBackend();
private Context mContext;
private ImportanceSeekBarPreference mImportance;
private SwitchPreference mPriority;
private SwitchPreference mSensitive;
private TopicRow mTopicRow;
@@ -122,11 +123,27 @@ public class TopicNotificationSettings extends SettingsPreferenceFragment {
mIsSystemPackage = Utils.isSystemPackage(pm, info);
addPreferencesFromResource(R.xml.topic_notification_settings);
mImportance = (ImportanceSeekBarPreference) findPreference(KEY_IMPORTANCE);
mPriority = (SwitchPreference) findPreference(KEY_BYPASS_DND);
mSensitive = (SwitchPreference) findPreference(KEY_SENSITIVE);
mTopicRow = mBackend.loadTopicRow(pm, info.applicationInfo, topic);
mImportance.setMax(4);
// TODO: stop defaulting to 'normal' in the UI when there are mocks for this scenario.
int importance =
mTopicRow.importance == NotificationListenerService.Ranking.IMPORTANCE_UNSPECIFIED
? NotificationListenerService.Ranking.IMPORTANCE_DEFAULT
: mTopicRow.importance;
mImportance.setProgress(
importance + ImportanceSeekBarPreference.IMPORTANCE_PROGRESS_OFFSET);
mImportance.setCallback(new ImportanceSeekBarPreference.Callback() {
@Override
public void onImportanceChanged(int progress) {
mBackend.setImportance(mTopicRow.pkg, mTopicRow.uid, mTopicRow.topic,
progress - ImportanceSeekBarPreference.IMPORTANCE_PROGRESS_OFFSET);
}
});
mPriority.setChecked(mTopicRow.priority);
mSensitive.setChecked(mTopicRow.sensitive);
@@ -167,6 +184,7 @@ public class TopicNotificationSettings extends SettingsPreferenceFragment {
setVisible(mPriority, mIsSystemPackage || !banned);
setVisible(mSensitive, mIsSystemPackage || !banned && lockscreenSecure
&& lockscreenNotificationsEnabled && allowPrivate);
setVisible(mImportance, !banned);
}
private void setVisible(Preference p, boolean visible) {