This commit is contained in:
Mariotaku Lee 2016-02-19 11:50:17 +08:00
parent 473b2fa7be
commit 01cfec55dd
4 changed files with 91 additions and 66 deletions

View File

@ -24,6 +24,7 @@ import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import org.mariotaku.twidere.Constants;
@ -49,7 +50,11 @@ public class AccountPreferences implements Constants {
public int getDefaultNotificationLightColor() {
final ParcelableAccount a = DataStoreUtils.getAccount(mContext, mAccountId);
return a != null ? a.color : mContext.getResources().getColor(R.color.branding_color);
if (a != null) {
return a.color;
} else {
return ContextCompat.getColor(mContext, R.color.branding_color);
}
}
public int getDirectMessagesNotificationType() {
@ -69,9 +74,12 @@ public class AccountPreferences implements Constants {
}
public Uri getNotificationRingtone() {
final Uri def = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final String path = mPreferences.getString(KEY_NOTIFICATION_RINGTONE, null);
return TextUtils.isEmpty(path) ? def : Uri.parse(path);
final String ringtone = mPreferences.getString(KEY_NOTIFICATION_RINGTONE, null);
if (TextUtils.isEmpty(ringtone)) {
return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
} else {
return Uri.parse(ringtone);
}
}
public boolean isAutoRefreshDirectMessagesEnabled() {

View File

@ -33,70 +33,87 @@ import org.mariotaku.twidere.R;
public class NotificationTypePreference extends DialogPreference implements Constants {
private final int mDefaultValue;
private final int mDefaultValue;
public NotificationTypePreference(final Context context) {
this(context, null);
}
public NotificationTypePreference(final Context context) {
this(context, null);
}
public NotificationTypePreference(final Context context, final AttributeSet attrs) {
this(context, attrs, android.R.attr.dialogPreferenceStyle);
}
public NotificationTypePreference(final Context context, final AttributeSet attrs) {
this(context, attrs, android.R.attr.dialogPreferenceStyle);
}
public NotificationTypePreference(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NotificationTypePreference);
mDefaultValue = a.getInteger(R.styleable.NotificationTypePreference_notificationType, 0);
a.recycle();
}
public NotificationTypePreference(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NotificationTypePreference);
mDefaultValue = a.getInteger(R.styleable.NotificationTypePreference_notificationType, 0);
a.recycle();
}
@Override
public void onClick(final DialogInterface dialog, final int which) {
final Dialog showingDialog = getDialog();
if (!(showingDialog instanceof AlertDialog)) return;
final AlertDialog alertDialog = (AlertDialog) showingDialog;
final ListView listView = alertDialog.getListView();
if (listView == null) return;
int value = 0;
final int[] flags = getFlags();
for (int i = 0, j = flags.length; i < j; i++) {
if (listView.isItemChecked(i)) {
value |= flags[i];
}
}
persistInt(value);
}
@Override
public void onClick(final DialogInterface dialog, final int which) {
final Dialog showingDialog = getDialog();
if (!(showingDialog instanceof AlertDialog)) return;
final AlertDialog alertDialog = (AlertDialog) showingDialog;
final ListView listView = alertDialog.getListView();
if (listView == null) return;
int value = 0;
final int[] flags = getFlags();
for (int i = 0, j = flags.length; i < j; i++) {
if (listView.isItemChecked(i)) {
value |= flags[i];
}
}
persistInt(value);
notifyChanged();
}
@Override
public void onPrepareDialogBuilder(final AlertDialog.Builder builder) {
super.onPrepareDialogBuilder(builder);
builder.setPositiveButton(android.R.string.ok, this);
builder.setNegativeButton(android.R.string.cancel, null);
final int value = getPersistedInt(mDefaultValue);
builder.setMultiChoiceItems(getEntries(), getCheckedItems(value), null);
}
@Override
public void onPrepareDialogBuilder(final AlertDialog.Builder builder) {
super.onPrepareDialogBuilder(builder);
builder.setPositiveButton(android.R.string.ok, this);
builder.setNegativeButton(android.R.string.cancel, null);
final int value = getPersistedInt(mDefaultValue);
builder.setMultiChoiceItems(getEntries(), getCheckedItems(value), null);
}
private boolean[] getCheckedItems(final int value) {
final int[] flags = getFlags();
final boolean[] checkedItems = new boolean[flags.length];
for (int i = 0, j = flags.length; i < j; i++) {
checkedItems[i] = (value & flags[i]) != 0;
}
return checkedItems;
}
private boolean[] getCheckedItems(final int value) {
final int[] flags = getFlags();
final boolean[] checkedItems = new boolean[flags.length];
for (int i = 0, j = flags.length; i < j; i++) {
checkedItems[i] = (value & flags[i]) != 0;
}
return checkedItems;
}
private String[] getEntries() {
final Context context = getContext();
final String[] entries = new String[3];
entries[0] = context.getString(R.string.ringtone);
entries[1] = context.getString(R.string.vibration);
entries[2] = context.getString(R.string.light);
return entries;
}
private String[] getEntries() {
final Context context = getContext();
final String[] entries = new String[3];
entries[0] = context.getString(R.string.ringtone);
entries[1] = context.getString(R.string.vibration);
entries[2] = context.getString(R.string.light);
return entries;
}
private int[] getFlags() {
return new int[] { VALUE_NOTIFICATION_FLAG_RINGTONE, VALUE_NOTIFICATION_FLAG_VIBRATION,
VALUE_NOTIFICATION_FLAG_LIGHT };
}
@Override
public CharSequence getSummary() {
final StringBuilder sb = new StringBuilder();
String[] entries = getEntries();
boolean[] states = getCheckedItems(getPersistedInt(mDefaultValue));
for (int i = 0, j = entries.length; i < j; i++) {
if (states[i]) {
if (sb.length() != 0) {
sb.append(", ");
}
sb.append(entries[i]);
}
}
return sb;
}
private int[] getFlags() {
return new int[]{VALUE_NOTIFICATION_FLAG_RINGTONE, VALUE_NOTIFICATION_FLAG_VIBRATION,
VALUE_NOTIFICATION_FLAG_LIGHT};
}
}

View File

@ -1318,7 +1318,7 @@ public final class TwidereDataProvider extends ContentProvider implements Consta
if (count == 0) return;
builder.setSmallIcon(R.drawable.ic_stat_notification);
builder.setCategory(NotificationCompat.CATEGORY_SOCIAL);
applyNotificationPreferences(builder, pref, pref.getHomeTimelineNotificationType());
applyNotificationPreferences(builder, pref, pref.getMentionsNotificationType());
final Resources resources = context.getResources();
final String accountName = DataStoreUtils.getAccountDisplayName(context, accountId, mNameFirst);
@ -1434,7 +1434,6 @@ public final class TwidereDataProvider extends ContentProvider implements Consta
notificationDefaults &= ~NotificationCompat.DEFAULT_VIBRATE;
}
if (AccountPreferences.isNotificationHasRingtone(defaultFlags)) {
notificationDefaults |= NotificationCompat.DEFAULT_SOUND;
builder.setSound(pref.getNotificationRingtone(), AudioManager.STREAM_NOTIFICATION);
}
} else {

View File

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="@string/notifications">
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="@string/notifications">
<org.mariotaku.twidere.preference.NotificationContentPreference
android:key="content_to_notify"