Fixed text colors confusion

This commit is contained in:
Phillip Hsu
2016-08-21 02:16:29 -07:00
parent a473f3fd2c
commit 9b7ab3e644
18 changed files with 171 additions and 35 deletions
@@ -54,7 +54,7 @@ public class AlarmActivity extends RingtoneActivity<Alarm> {
@Override
protected int getAutoSilencedDrawable() {
// TODO: correct icon
return R.drawable.ic_half_day_1_black_24dp;
return R.drawable.ic_half_day_1_24dp;
}
@Override
@@ -167,12 +167,13 @@ public class Utils {
return getColorFromThemeAttr(context, R.attr.colorAccent);
}
public static int getTextColorPrimary(Context context) {
public static int getTextColorFromThemeAttr(Context context, int resid) {
// http://stackoverflow.com/a/33839580/5055032
final TypedValue value = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.textColorPrimary, value, true);
TypedArray a = context.obtainStyledAttributes(value.data,
new int[] {android.R.attr.textColorPrimary});
// final TypedValue value = new TypedValue();
// context.getTheme().resolveAttribute(resid, value, true);
// TypedArray a = context.obtainStyledAttributes(value.data,
// new int[] {resid});
TypedArray a = context.getTheme().obtainStyledAttributes(new int[] {resid});
final int color = a.getColor(0/*index*/, 0/*defValue*/);
a.recycle();
return color;
@@ -182,13 +182,21 @@ public class GridSelectorLayout extends FrameLayout implements NumbersGrid.OnNum
@Override
public void onNumberSelected(int number) {
if (getCurrentItemShowing() == HOUR_INDEX && !mIs24HourMode) {
// Change the value before passing it through the callback
int amOrPm = getIsCurrentlyAmOrPm();
if (amOrPm == HALF_DAY_1 && number == 12) {
number = 0;
} else if (amOrPm == HALF_DAY_2 && number != 12) {
number += 12;
if (getCurrentItemShowing() == HOUR_INDEX) {
if (!mIs24HourMode) {
// Change the value before passing it through the callback
int amOrPm = getIsCurrentlyAmOrPm();
if (amOrPm == HALF_DAY_1 && number == 12) {
number = 0;
} else if (amOrPm == HALF_DAY_2 && number != 12) {
number += 12;
}
} else {
// Check if we would be changing half-days with the new value
if (mCurrentHoursOfDay < 12 && number >= 12 || mCurrentHoursOfDay >= 12 && number < 12) {
int newHalfDay = getIsCurrentlyAmOrPm() == HALF_DAY_1 ? HALF_DAY_2 : HALF_DAY_1;
mListener.onValueSelected(AMPM_INDEX, newHalfDay, false);
}
}
}
@@ -22,6 +22,7 @@ import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
@@ -127,6 +128,9 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog implements
private String mMinutePickerDescription;
private String mSelectMinutes;
private int mHalfDayToggleSelectedColor;
private int mHalfDayToggleUnselectedColor;
// ====================================== MY STUFF =============================================
// The padding in dp for the half day icon compound drawable
public static final int HALF_DAY_ICON_PADDING = 8;
@@ -163,6 +167,7 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog implements
// }
updateAmPmDisplay(halfDay);
mTimePicker.setAmOrPm(halfDay);
updateHalfDayTogglesState(halfDay);
}
}
@@ -319,11 +324,13 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog implements
tv1.setText("00 - 11");
// Intrinsic bounds meaning the drawable's own bounds? So 24dp box.
tv1.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.ic_half_day_1_black_24dp, 0, 0, 0);
mThemeDark? R.drawable.ic_half_day_1_dark_24dp : R.drawable.ic_half_day_1_24dp,
0, 0, 0);
tv1.setCompoundDrawablePadding((int) dpToPx(getActivity(), HALF_DAY_ICON_PADDING));
tv2.setText("12 - 23");
tv2.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.ic_half_day_2_black_24dp, 0, 0, 0);
mThemeDark? R.drawable.ic_half_day_2_dark_24dp : R.drawable.ic_half_day_2_24dp,
0, 0, 0);
tv2.setCompoundDrawablePadding((int) dpToPx(getActivity(), HALF_DAY_ICON_PADDING));
} else {
tv1.setText(mAmText);
@@ -452,18 +459,33 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog implements
((TextView) view.findViewById(R.id.separator)).setTextColor(/*mThemeDark? white : timeDisplay*/mUnselectedColor);
((TextView) view.findViewById(R.id.ampm_label)).setTextColor(/*mThemeDark? white : timeDisplay*/mUnselectedColor);
// view.findViewById(R.id.line).setBackgroundColor(mThemeDark? darkLine : line);
// TODO: darkLine is 12% white, but it shows up like 100% white. We have replaced
// darkLine with lightGray for now. Figure out why and come back to replace
// lightGray with darkLine.
view.findViewById(R.id.divider).setBackgroundColor(mThemeDark? lightGray : line);
// mDoneButton.setTextColor(mThemeDark? darkDoneTextColor : doneTextColor);
// The AOSP timepicker originally uses these colors for the CircleView
mTimePicker.setBackgroundColor(mThemeDark? /*lightGray : circleBackground*/ darkGray : white);
// mDoneButton.setBackgroundResource(mThemeDark? darkDoneBackground : doneBackground);
// Set the color on the FAB
// http://stackoverflow.com/a/32031019/5055032
// Color in normal state
mDoneButton.setBackgroundTintList(ColorStateList.valueOf(accentColor));
// Color in pressed state. A ripple expands outwards from the point of contact throughout
// the fab when it is long pressed.
// mDoneButton.setRippleColor(/*your color here*/);
view.findViewById(R.id.half_day_toggles).setBackgroundColor(mThemeDark? /*lightGray : circleBackground*/ darkGray : white);
// Set the color on the half-day toggles
view.findViewById(R.id.half_day_toggles).setBackgroundColor(mThemeDark? /*lightGray : circleBackground*/ darkGray : white);
mHalfDayToggleSelectedColor = accentColor;
// mHalfDayToggleUnselectedColor = Utils.getTextColorFromThemeAttr(getContext(),
// // The colors are in the correct order, which happens to be the reverse of the order
// // used in the NumbersGrids...
// mThemeDark? android.R.attr.textColorPrimaryInverse : android.R.attr.textColorPrimary);
mHalfDayToggleUnselectedColor = ContextCompat.getColor(getContext(),
mThemeDark? R.color.text_color_primary_dark : R.color.text_color_primary_light);
updateHalfDayTogglesState(mTimePicker.getIsCurrentlyAmOrPm());
return view;
}
@@ -497,6 +519,25 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog implements
}
}
/**
* Update the indicator of the toggle buttons to show the given half-day as selected.
* @param halfDay the half-day that should be shown as selected
*/
private void updateHalfDayTogglesState(int halfDay) {
TextView leftHalfDayToggle = (TextView) mLeftHalfDayToggle.getChildAt(0);
TextView rightHalfDayToggle = (TextView) mRightHalfDayToggle.getChildAt(0);
switch (halfDay) {
case HALF_DAY_1:
leftHalfDayToggle.setTextColor(mHalfDayToggleSelectedColor);
rightHalfDayToggle.setTextColor(mHalfDayToggleUnselectedColor);
break;
case HALF_DAY_2:
rightHalfDayToggle.setTextColor(mHalfDayToggleSelectedColor);
leftHalfDayToggle.setTextColor(mHalfDayToggleUnselectedColor);
break;
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
if (mTimePicker != null) {
@@ -533,6 +574,7 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog implements
mTimePicker.setContentDescription(mMinutePickerDescription + ": " + newValue);
} else if (pickerIndex == AMPM_INDEX) {
updateAmPmDisplay(newValue);
updateHalfDayTogglesState(newValue);
} else if (pickerIndex == ENABLE_PICKER_INDEX) {
if (!isTypedTimeFullyLegal()) {
mTypedTimes.clear();
@@ -2,11 +2,13 @@ package com.philliphsu.clock2.editalarm;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayout;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.philliphsu.clock2.R;
import com.philliphsu.clock2.aospdatetimepicker.Utils;
/**
@@ -20,8 +22,11 @@ public abstract class NumbersGrid extends GridLayout implements View.OnClickList
OnNumberSelectedListener mSelectionListener;
View mLastSelectedView;
private final int mDefaultTextColor;
private final int mSelectedTextColor;
// TODO: The half-day buttons in the dialog's layout also need to use this color.
// Consider moving this to either the Dialog class, or move the buttons and the FAB
// to the GridSelectorLayout class and then move these to GridSelectorLayout.
int mDefaultTextColor;
private boolean mIsInitialized;
private int mSelection; // The number selected from this grid
@@ -41,7 +46,8 @@ public abstract class NumbersGrid extends GridLayout implements View.OnClickList
// our subclasses' buttons, if any.
registerClickListeners();
mIsInitialized = false;
mDefaultTextColor = Utils.getTextColorPrimary(context);
// mDefaultTextColor = Utils.getTextColorFromThemeAttr(context, android.R.attr.textColorPrimary);
mDefaultTextColor = ContextCompat.getColor(context, R.color.text_color_primary_light);
mSelectedTextColor = Utils.getThemeAccentColor(context);
// Show the first button as default selected
setIndicator(getChildAt(indexOfDefaultValue()));
@@ -123,8 +129,26 @@ public abstract class NumbersGrid extends GridLayout implements View.OnClickList
return 0;
}
/**
* The default implementation sets the appropriate text color on all of the number buttons
* as determined by {@link #canRegisterClickListener(View)}.
*/
void setTheme(Context context, boolean themeDark) {
// TODO: Change background color and text color
// mDefaultTextColor = Utils.getTextColorFromThemeAttr(context, themeDark?
// // You may think the order should be switched, but this is in fact the correct order.
// // I'm guessing this is sensitive to the background color?
// android.R.attr.textColorPrimary : android.R.attr.textColorPrimaryInverse);
mDefaultTextColor = ContextCompat.getColor(context, themeDark?
R.color.text_color_primary_dark : R.color.text_color_primary_light);
for (int i = 0; i < getChildCount(); i++) {
View v = getChildAt(i);
// Filter out views that aren't number buttons
if (canRegisterClickListener(v)) {
((TextView) v).setTextColor(mDefaultTextColor);
}
}
// Set the indicator again
setIndicator(getChildAt(indexOfDefaultValue()));
}
/**
@@ -60,6 +60,14 @@ public class TwentyFourHourGridItem extends LinearLayout {
setSecondaryText(primary);
}
public TextView getPrimaryTextView() {
return (TextView) getChildAt(0);
}
public TextView getSecondaryTextView() {
return (TextView) getChildAt(1);
}
private void init() {
setOrientation(VERTICAL);
setGravity(Gravity.CENTER);
@@ -1,6 +1,7 @@
package com.philliphsu.clock2.editalarm;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.View;
import com.philliphsu.clock2.R;
@@ -11,11 +12,15 @@ import com.philliphsu.clock2.R;
public class TwentyFourHoursGrid extends NumbersGrid implements View.OnLongClickListener {
private static final String TAG = "TwentyFourHoursGrid";
private int mSecondaryTextColor;
public TwentyFourHoursGrid(Context context) {
super(context);
for (int i = 0; i < getChildCount(); i++) {
getChildAt(i).setOnLongClickListener(this);
}
// mSecondaryTextColor = Utils.getTextColorFromThemeAttr(context, android.R.attr.textColorSecondary);
mSecondaryTextColor = ContextCompat.getColor(context, R.color.text_color_secondary_light);
}
@Override
@@ -56,8 +61,34 @@ public class TwentyFourHoursGrid extends NumbersGrid implements View.OnLongClick
@Override
protected void setIndicator(View view) {
TwentyFourHourGridItem item = (TwentyFourHourGridItem) view;
// Set indicator on the primary TextView
super.setIndicator(item.getChildAt(0));
super.setIndicator(item.getPrimaryTextView());
}
@Override
void setTheme(Context context, boolean themeDark) {
// mDefaultTextColor = Utils.getTextColorFromThemeAttr(context, themeDark?
// // You may think the order should be switched, but this is in fact the correct order.
// // I'm guessing this is sensitive to the background color?
// android.R.attr.textColorPrimary : android.R.attr.textColorPrimaryInverse);
mDefaultTextColor = ContextCompat.getColor(context, themeDark?
R.color.text_color_primary_dark : R.color.text_color_primary_light);
// https://www.reddit.com/r/androiddev/comments/2jpeqd/dealing_with_themematerial_vs_themeholo/
// For pre-21, textColorPrimary == textColorSecondary. Use textColorTertiary instead.
// For 21 and above, textColorSecondary == textColorTertiary == pre-21 textColorTertiary.
// Therefore, use textColorTertiary for both.
// mSecondaryTextColor = Utils.getTextColorFromThemeAttr(context, themeDark?
// // You may think the order should be switched, but this is in fact the correct order.
// // I'm guessing this is sensitive to the background color?
// android.R.attr.textColorSecondary : android.R.attr.textColorSecondaryInverse);
mSecondaryTextColor = ContextCompat.getColor(context, themeDark?
R.color.text_color_secondary_dark : R.color.text_color_secondary_light);
for (int i = 0; i < getChildCount(); i++) {
TwentyFourHourGridItem item = (TwentyFourHourGridItem) getChildAt(i);
item.getPrimaryTextView().setTextColor(mDefaultTextColor);
item.getSecondaryTextView().setTextColor(mSecondaryTextColor);
}
// Set the indicator again
setIndicator(getChildAt(indexOfDefaultValue()));
}
public void swapTexts() {
@@ -93,7 +93,7 @@ public class TimerNotificationService extends Service {
private void showNotification() {
// Base note
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_half_day_1_black_24dp) // TODO: correct icon
.setSmallIcon(R.drawable.ic_half_day_1_24dp) // TODO: correct icon
.setShowWhen(false)
.setOngoing(true);
// TODO: Set content intent so that when clicked, we launch
@@ -69,7 +69,7 @@ public class TimerRingtoneService extends RingtoneService<Timer> {
return new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(getString(R.string.times_up))
.setSmallIcon(R.drawable.ic_half_day_1_black_24dp) // TODO: correct icon
.setSmallIcon(R.drawable.ic_half_day_1_24dp) // TODO: correct icon
.setShowWhen(false) // TODO: Should we show this?
// .setOngoing(true) // foreground notes are ongoing by default
.addAction(R.drawable.ic_add_circle_24dp, // TODO: correct icon
@@ -64,7 +64,7 @@ public class TimesUpActivity extends RingtoneActivity<Timer> {
@Override
protected int getAutoSilencedDrawable() {
// TODO: correct icon
return R.drawable.ic_half_day_1_black_24dp;
return R.drawable.ic_half_day_1_24dp;
}
@Override
@@ -85,7 +85,7 @@ public class TimesUpActivity extends RingtoneActivity<Timer> {
@Override
protected int getLeftButtonDrawable() {
// TODO: correct icon
return R.drawable.ic_half_day_1_black_24dp;
return R.drawable.ic_half_day_1_24dp;
}
@Override