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

View File

@ -33,70 +33,87 @@ import org.mariotaku.twidere.R;
public class NotificationTypePreference extends DialogPreference implements Constants { public class NotificationTypePreference extends DialogPreference implements Constants {
private final int mDefaultValue; private final int mDefaultValue;
public NotificationTypePreference(final Context context) { public NotificationTypePreference(final Context context) {
this(context, null); this(context, null);
} }
public NotificationTypePreference(final Context context, final AttributeSet attrs) { public NotificationTypePreference(final Context context, final AttributeSet attrs) {
this(context, attrs, android.R.attr.dialogPreferenceStyle); this(context, attrs, android.R.attr.dialogPreferenceStyle);
} }
public NotificationTypePreference(final Context context, final AttributeSet attrs, final int defStyle) { public NotificationTypePreference(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle); super(context, attrs, defStyle);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NotificationTypePreference); final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NotificationTypePreference);
mDefaultValue = a.getInteger(R.styleable.NotificationTypePreference_notificationType, 0); mDefaultValue = a.getInteger(R.styleable.NotificationTypePreference_notificationType, 0);
a.recycle(); a.recycle();
} }
@Override @Override
public void onClick(final DialogInterface dialog, final int which) { public void onClick(final DialogInterface dialog, final int which) {
final Dialog showingDialog = getDialog(); final Dialog showingDialog = getDialog();
if (!(showingDialog instanceof AlertDialog)) return; if (!(showingDialog instanceof AlertDialog)) return;
final AlertDialog alertDialog = (AlertDialog) showingDialog; final AlertDialog alertDialog = (AlertDialog) showingDialog;
final ListView listView = alertDialog.getListView(); final ListView listView = alertDialog.getListView();
if (listView == null) return; if (listView == null) return;
int value = 0; int value = 0;
final int[] flags = getFlags(); final int[] flags = getFlags();
for (int i = 0, j = flags.length; i < j; i++) { for (int i = 0, j = flags.length; i < j; i++) {
if (listView.isItemChecked(i)) { if (listView.isItemChecked(i)) {
value |= flags[i]; value |= flags[i];
} }
} }
persistInt(value); persistInt(value);
} notifyChanged();
}
@Override @Override
public void onPrepareDialogBuilder(final AlertDialog.Builder builder) { public void onPrepareDialogBuilder(final AlertDialog.Builder builder) {
super.onPrepareDialogBuilder(builder); super.onPrepareDialogBuilder(builder);
builder.setPositiveButton(android.R.string.ok, this); builder.setPositiveButton(android.R.string.ok, this);
builder.setNegativeButton(android.R.string.cancel, null); builder.setNegativeButton(android.R.string.cancel, null);
final int value = getPersistedInt(mDefaultValue); final int value = getPersistedInt(mDefaultValue);
builder.setMultiChoiceItems(getEntries(), getCheckedItems(value), null); builder.setMultiChoiceItems(getEntries(), getCheckedItems(value), null);
} }
private boolean[] getCheckedItems(final int value) { private boolean[] getCheckedItems(final int value) {
final int[] flags = getFlags(); final int[] flags = getFlags();
final boolean[] checkedItems = new boolean[flags.length]; final boolean[] checkedItems = new boolean[flags.length];
for (int i = 0, j = flags.length; i < j; i++) { for (int i = 0, j = flags.length; i < j; i++) {
checkedItems[i] = (value & flags[i]) != 0; checkedItems[i] = (value & flags[i]) != 0;
} }
return checkedItems; return checkedItems;
} }
private String[] getEntries() { private String[] getEntries() {
final Context context = getContext(); final Context context = getContext();
final String[] entries = new String[3]; final String[] entries = new String[3];
entries[0] = context.getString(R.string.ringtone); entries[0] = context.getString(R.string.ringtone);
entries[1] = context.getString(R.string.vibration); entries[1] = context.getString(R.string.vibration);
entries[2] = context.getString(R.string.light); entries[2] = context.getString(R.string.light);
return entries; return entries;
} }
private int[] getFlags() { @Override
return new int[] { VALUE_NOTIFICATION_FLAG_RINGTONE, VALUE_NOTIFICATION_FLAG_VIBRATION, public CharSequence getSummary() {
VALUE_NOTIFICATION_FLAG_LIGHT }; 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; if (count == 0) return;
builder.setSmallIcon(R.drawable.ic_stat_notification); builder.setSmallIcon(R.drawable.ic_stat_notification);
builder.setCategory(NotificationCompat.CATEGORY_SOCIAL); builder.setCategory(NotificationCompat.CATEGORY_SOCIAL);
applyNotificationPreferences(builder, pref, pref.getHomeTimelineNotificationType()); applyNotificationPreferences(builder, pref, pref.getMentionsNotificationType());
final Resources resources = context.getResources(); final Resources resources = context.getResources();
final String accountName = DataStoreUtils.getAccountDisplayName(context, accountId, mNameFirst); final String accountName = DataStoreUtils.getAccountDisplayName(context, accountId, mNameFirst);
@ -1434,7 +1434,6 @@ public final class TwidereDataProvider extends ContentProvider implements Consta
notificationDefaults &= ~NotificationCompat.DEFAULT_VIBRATE; notificationDefaults &= ~NotificationCompat.DEFAULT_VIBRATE;
} }
if (AccountPreferences.isNotificationHasRingtone(defaultFlags)) { if (AccountPreferences.isNotificationHasRingtone(defaultFlags)) {
notificationDefaults |= NotificationCompat.DEFAULT_SOUND;
builder.setSound(pref.getNotificationRingtone(), AudioManager.STREAM_NOTIFICATION); builder.setSound(pref.getNotificationRingtone(), AudioManager.STREAM_NOTIFICATION);
} }
} else { } else {

View File

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