Use switches instead of checkboxes

This commit is contained in:
Moez Bhatti
2015-12-07 13:14:44 -05:00
parent a126dcf70c
commit 1b4d3a2c66
45 changed files with 108 additions and 173 deletions
@@ -12,7 +12,7 @@ import android.widget.LinearLayout;
import com.moez.QKSMS.R;
import com.moez.QKSMS.ui.ThemeManager;
import com.moez.QKSMS.ui.settings.SettingsFragment;
import com.moez.QKSMS.ui.view.QKCheckBoxPreference;
import com.moez.QKSMS.ui.view.QKSwitchPreference;
import com.moez.QKSMS.ui.view.QKTextView;
public class BubblePreferenceDialog extends QKDialog {
@@ -52,7 +52,7 @@ public class BubblePreferenceDialog extends QKDialog {
Log.d(TAG, preference.getKey());
switch (preference.getKey()) {
case SettingsFragment.BUBBLES_NEW:
ThemeManager.setBubbleStyleNew(((QKCheckBoxPreference) preference).isChecked());
ThemeManager.setBubbleStyleNew(((QKSwitchPreference) preference).isChecked());
in1.setBackgroundResource(ThemeManager.getReceivedBubbleRes());
in1.getBackground().setColorFilter(ThemeManager.getReceivedBubbleColor(), PorterDuff.Mode.MULTIPLY);
in1.setOnColorBackground(ThemeManager.getReceivedBubbleColor() == ThemeManager.getColor());
@@ -71,7 +71,7 @@ public class BubblePreferenceDialog extends QKDialog {
return true;
case SettingsFragment.COLOUR_RECEIVED:
ThemeManager.setReceivedBubbleColored(((QKCheckBoxPreference) preference).isChecked());
ThemeManager.setReceivedBubbleColored(((QKSwitchPreference) preference).isChecked());
in1.getBackground().setColorFilter(ThemeManager.getReceivedBubbleColor(), PorterDuff.Mode.MULTIPLY);
in1.setOnColorBackground(ThemeManager.getReceivedBubbleColor() == ThemeManager.getColor());
in2.getBackground().setColorFilter(ThemeManager.getReceivedBubbleColor(), PorterDuff.Mode.MULTIPLY);
@@ -79,7 +79,7 @@ public class BubblePreferenceDialog extends QKDialog {
return true;
case SettingsFragment.COLOUR_SENT:
ThemeManager.setSentBubbleColored(((QKCheckBoxPreference) preference).isChecked());
ThemeManager.setSentBubbleColored(((QKSwitchPreference) preference).isChecked());
out1.getBackground().setColorFilter(ThemeManager.getSentBubbleColor(), PorterDuff.Mode.MULTIPLY);
out1.setOnColorBackground(ThemeManager.getSentBubbleColor() == ThemeManager.getColor());
out2.getBackground().setColorFilter(ThemeManager.getSentBubbleColor(), PorterDuff.Mode.MULTIPLY);
@@ -91,11 +91,11 @@ public class BubblePreferenceDialog extends QKDialog {
};
LinearLayout prefsLayout = (LinearLayout) view.findViewById(R.id.prefs);
prefsLayout.addView(new QKCheckBoxPreference(mContext, onPreferenceClickListener, SettingsFragment.BUBBLES_NEW,
prefsLayout.addView(new QKSwitchPreference(mContext, onPreferenceClickListener, SettingsFragment.BUBBLES_NEW,
prefs, true, R.string.pref_bubble_style_new, 0).getView());
prefsLayout.addView(new QKCheckBoxPreference(mContext, onPreferenceClickListener, SettingsFragment.COLOUR_RECEIVED,
prefsLayout.addView(new QKSwitchPreference(mContext, onPreferenceClickListener, SettingsFragment.COLOUR_RECEIVED,
prefs, false, R.string.pref_color_received, 0).getView());
prefsLayout.addView(new QKCheckBoxPreference(mContext, onPreferenceClickListener, SettingsFragment.COLOUR_SENT,
prefsLayout.addView(new QKSwitchPreference(mContext, onPreferenceClickListener, SettingsFragment.COLOUR_SENT,
prefs, true, R.string.pref_color_sent, 0).getView());
setTitle(R.string.pref_bubbles);
@@ -15,7 +15,7 @@ import com.moez.QKSMS.common.FontManager;
import com.moez.QKSMS.common.utils.Units;
import com.moez.QKSMS.ui.MainActivity;
import com.moez.QKSMS.ui.settings.SettingsFragment;
import com.moez.QKSMS.ui.view.QKCheckBoxPreference;
import com.moez.QKSMS.ui.view.QKSwitchPreference;
import com.moez.QKSMS.ui.view.QKPreference;
import com.moez.QKSMS.ui.view.QKRingtonePreference;
import com.moez.QKSMS.ui.view.colorpicker.ColorPickerDialog;
@@ -87,25 +87,25 @@ public class ConversationNotificationSettingsDialog extends QKDialog implements
LinearLayout list = new LinearLayout(getActivity());
list.setOrientation(LinearLayout.VERTICAL);
list.addView(new QKCheckBoxPreference(getActivity(), this, SettingsFragment.NOTIFICATION_LED,
list.addView(new QKSwitchPreference(getActivity(), this, SettingsFragment.NOTIFICATION_LED,
mConversationPrefs.getConversationPrefs(), mConversationPrefs.getNotificationLedEnabled(), R.string.pref_led, 0).getView());
list.addView(new QKPreference(getActivity(), this, SettingsFragment.NOTIFICATION_LED_COLOR,
R.string.pref_theme_led, 0).getView());
list.addView(new QKCheckBoxPreference(getActivity(), this, SettingsFragment.WAKE,
list.addView(new QKSwitchPreference(getActivity(), this, SettingsFragment.WAKE,
mConversationPrefs.getConversationPrefs(), mConversationPrefs.getWakePhoneEnabled(), R.string.pref_wake, R.string.pref_wake_summary).getView());
list.addView(new QKCheckBoxPreference(getActivity(), this, SettingsFragment.NOTIFICATION_TICKER,
list.addView(new QKSwitchPreference(getActivity(), this, SettingsFragment.NOTIFICATION_TICKER,
mConversationPrefs.getConversationPrefs(), mConversationPrefs.getTickerEnabled(), R.string.pref_ticker, R.string.pref_ticker_summary).getView());
list.addView(new QKCheckBoxPreference(getActivity(), this, SettingsFragment.NOTIFICATION_VIBRATE,
list.addView(new QKSwitchPreference(getActivity(), this, SettingsFragment.NOTIFICATION_VIBRATE,
mConversationPrefs.getConversationPrefs(), mConversationPrefs.getVibrateEnabled(), R.string.pref_vibration, R.string.pref_vibration_summary).getView());
list.addView(new QKRingtonePreference(getActivity(), this, SettingsFragment.NOTIFICATION_TONE,
R.string.pref_ringtone, R.string.pref_ringtone_summary).getView());
list.addView(new QKCheckBoxPreference(getActivity(), this, SettingsFragment.NOTIFICATION_CALL_BUTTON,
list.addView(new QKSwitchPreference(getActivity(), this, SettingsFragment.NOTIFICATION_CALL_BUTTON,
mConversationPrefs.getConversationPrefs(), mConversationPrefs.getCallButtonEnabled(), R.string.pref_notification_call, R.string.pref_notification_call_summary).getView());
setCustomView(list);
@@ -1,102 +0,0 @@
package com.moez.QKSMS.ui.view;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import com.moez.QKSMS.interfaces.LiveView;
import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.ui.ThemeManager;
import com.moez.QKSMS.ui.settings.SettingsFragment;
public class QKCheckBox extends android.widget.CheckBox implements LiveView {
public static final String TAG = "QKCheckBox";
private static final int ALPHA_ENABLED = 255;
private static final int ALPHA_DISABLED = 128;
private static final int ANIMATION_FRAME_START = 0;
private static final int ANIMATION_FRAME_COMPLETE = 15;
private static final int ANIMATION_DURATION = 200; // ms
private Drawable mButtonDrawable;
public QKCheckBox(Context context) { this(context, null); }
public QKCheckBox(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.checkboxStyle); }
public QKCheckBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setDrawable(ANIMATION_FRAME_COMPLETE);
// This disables an annoying double ripple effect on Lollipop+ android versions.
// But, we can't just always disable the background, because that makes the CheckBox
// invisible for older Android versions (i.e. 4.1.2, perhaps older)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setBackground(null);
}
// Register this view for live updates.
LiveViewManager.registerView(this);
LiveViewManager.registerPreference(this, SettingsFragment.THEME);
}
/**
* Sets the button drawable, updating the color of it according to the theme color.
* @param drawable
*/
@Override
public void setButtonDrawable(Drawable drawable) {
mButtonDrawable = drawable;
mButtonDrawable.setColorFilter(ThemeManager.getColor(), PorterDuff.Mode.MULTIPLY);
super.setButtonDrawable(drawable);
}
@Override
public void refresh() {
// Refresh the button drawable with the new theme color.
setButtonDrawable(mButtonDrawable);
}
@Override
public void setChecked(boolean checked) {
boolean animate = checked != isChecked();
super.setChecked(checked);
if (animate) {
ObjectAnimator checkBoxAnimator = ObjectAnimator.ofInt(
this, "drawable", ANIMATION_FRAME_START, ANIMATION_FRAME_COMPLETE
);
checkBoxAnimator.setDuration(ANIMATION_DURATION);
checkBoxAnimator.start();
}
}
private void setDrawable(int number) {
// TODO: Preload the drawables as constants instead of loading them this way? Might be faster
Resources res = getContext().getResources();
// i.e. btn_check_to_off_mtrl_003, or btn_check_to_on_mtrl_013.
// the "%03d" part just means "insert this number, padding it so that it's always three
// digits"
String name = String.format("btn_check_to_%s_mtrl_%03d", isChecked() ? "on" : "off", number);
int id = res.getIdentifier(name, "drawable", getContext().getPackageName());
try {
setButtonDrawable(ContextCompat.getDrawable(getContext(), id));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onDraw(Canvas canvas) {
mButtonDrawable.mutate().setAlpha(isEnabled() ? ALPHA_ENABLED : ALPHA_DISABLED);
super.onDraw(canvas);
}
}
@@ -0,0 +1,40 @@
package com.moez.QKSMS.ui.view;
import android.content.Context;
import android.support.v7.widget.SwitchCompat;
import android.util.AttributeSet;
import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.interfaces.LiveView;
import com.moez.QKSMS.ui.settings.SettingsFragment;
public class QKSwitch extends SwitchCompat implements LiveView {
public QKSwitch(Context context) {
super(context);
init();
}
public QKSwitch(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
// Register this view for live updates.
LiveViewManager.registerView(this);
LiveViewManager.registerPreference(this, SettingsFragment.THEME);
refresh();
}
@Override
public void refresh() {
//getThumbDrawable().setColorFilter(ThemeManager.getColor(), PorterDuff.Mode.MULTIPLY);
//getTrackDrawable().setColorFilter(ThemeManager.getColor(), PorterDuff.Mode.MULTIPLY);
}
@Override
public void setChecked(boolean checked) {
super.setChecked(checked);
}
}
@@ -2,7 +2,7 @@ package com.moez.QKSMS.ui.view;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.CheckBoxPreference;
import android.preference.SwitchPreference;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
@@ -12,15 +12,15 @@ import com.moez.QKSMS.R;
* Regular android preferences don't have basic functionality when you manually add them to views
* other than preferencegroups, this just cleans up some boilerplate code to set ours up
*/
public class QKCheckBoxPreference extends CheckBoxPreference {
public class QKSwitchPreference extends SwitchPreference {
private SharedPreferences mPrefs;
private OnPreferenceClickListener mOnPreferenceClickListener;
private boolean mDefaultValue;
private QKCheckBox mCheckBox;
private QKSwitch mCheckBox;
public QKCheckBoxPreference(Context context, OnPreferenceClickListener onPreferenceClickListener,
String key, SharedPreferences prefs, boolean defaultValue, int title, int summary) {
public QKSwitchPreference(Context context, OnPreferenceClickListener onPreferenceClickListener,
String key, SharedPreferences prefs, boolean defaultValue, int title, int summary) {
super(context);
mPrefs = prefs;
mOnPreferenceClickListener = onPreferenceClickListener;
@@ -29,7 +29,7 @@ public class QKCheckBoxPreference extends CheckBoxPreference {
setEnabled(true);
mDefaultValue = prefs.getBoolean(key, defaultValue);
setLayoutResource(R.layout.list_item_preference);
setWidgetLayoutResource(R.layout.view_checkbox);
setWidgetLayoutResource(R.layout.view_switch);
if (title != 0) setTitle(title);
if (summary != 0) setSummary(summary);
}
@@ -45,17 +45,14 @@ public class QKCheckBoxPreference extends CheckBoxPreference {
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
mCheckBox = (QKCheckBox) view.findViewById(android.R.id.checkbox);
mCheckBox = (QKSwitch) view.findViewById(android.R.id.checkbox);
mCheckBox.setChecked(mDefaultValue);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPrefs.edit().putBoolean(getKey(), !mCheckBox.isChecked()).apply();
mCheckBox.setChecked(!mCheckBox.isChecked());
if (mOnPreferenceClickListener != null) {
mOnPreferenceClickListener.onPreferenceClick(QKCheckBoxPreference.this);
}
view.setOnClickListener(v -> {
mPrefs.edit().putBoolean(getKey(), !mCheckBox.isChecked()).apply();
mCheckBox.setChecked(!mCheckBox.isChecked());
if (mOnPreferenceClickListener != null) {
mOnPreferenceClickListener.onPreferenceClick(QKSwitchPreference.this);
}
});