From d504f9873422c9d13b69c6b9fc08b757c3219d6b Mon Sep 17 00:00:00 2001 From: Phillip Hsu Date: Mon, 5 Sep 2016 15:10:55 -0700 Subject: [PATCH] Use DateFormatSymbols to get weekday labels for the device locale. Remove corresponding string resources. --- .../com/philliphsu/clock2/DaysOfWeek.java | 37 ++++++++----------- .../philliphsu/clock2/DaysOfWeekHelper.java | 12 ------ app/src/main/res/values/strings.xml | 7 ---- 3 files changed, 16 insertions(+), 40 deletions(-) delete mode 100644 app/src/main/java/com/philliphsu/clock2/DaysOfWeekHelper.java diff --git a/app/src/main/java/com/philliphsu/clock2/DaysOfWeek.java b/app/src/main/java/com/philliphsu/clock2/DaysOfWeek.java index cc1b095..3e5711c 100644 --- a/app/src/main/java/com/philliphsu/clock2/DaysOfWeek.java +++ b/app/src/main/java/com/philliphsu/clock2/DaysOfWeek.java @@ -6,12 +6,13 @@ import android.util.Log; import com.philliphsu.clock2.util.AlarmUtils; +import java.text.DateFormatSymbols; import java.util.Arrays; /** * Created by Phillip Hsu on 5/30/2016. */ -public class DaysOfWeek implements DaysOfWeekHelper { +public final class DaysOfWeek { private static final String TAG = "DaysOfWeek"; // DAY_OF_WEEK constants in Calendar class not zero-based public static final int SUNDAY = 0; @@ -24,25 +25,12 @@ public class DaysOfWeek implements DaysOfWeekHelper { public static final int NUM_DAYS = 7; private static final int[] DAYS = new int[NUM_DAYS]; - private static final int[] LABELS_RES = new int[NUM_DAYS]; - - static { - // TODO: use `new DateFormatSymbols().getShortWeekdays()` to set texts - LABELS_RES[SUNDAY] = R.string.sun; - LABELS_RES[MONDAY] = R.string.mon; - LABELS_RES[TUESDAY] = R.string.tue; - LABELS_RES[WEDNESDAY] = R.string.wed; - LABELS_RES[THURSDAY] = R.string.thu; - LABELS_RES[FRIDAY] = R.string.fri; - LABELS_RES[SATURDAY] = R.string.sat; - } - - private static Context sAppContext; + private static final String[] LABELS = new DateFormatSymbols().getShortWeekdays(); + private static DaysOfWeek sInstance; private static int sLastPreferredFirstDay; public static DaysOfWeek getInstance(Context context) { - sAppContext = context.getApplicationContext(); int preferredFirstDay = AlarmUtils.firstDayOfWeek(context); if (sInstance == null || preferredFirstDay != sLastPreferredFirstDay) { sLastPreferredFirstDay = preferredFirstDay; @@ -52,19 +40,26 @@ public class DaysOfWeek implements DaysOfWeekHelper { return sInstance; } - /** @param weekDay the day constant as defined in this class */ - public static String getLabel(int weekDay) { - return sAppContext.getString(LABELS_RES[weekDay]); + /** + * @param weekday the zero-based index of the week day you would like to get the label for. + + */ + public static String getLabel(int weekday) { + // This array was returned from DateFormatSymbols.getShortWeekdays(). + // We are supposed to use the constants in the Calendar class as indices, but the previous + // implementation of this method used our own zero-based indices. For backward compatibility, + // we add one to the index passed in to match up with the values of the Calendar constants. + return LABELS[weekday + 1]; } - @Override + /** @return the week day at {@code position} within the user-defined week */ public int weekDayAt(int position) { if (position < 0 || position > 6) throw new ArrayIndexOutOfBoundsException("Ordinal day out of range"); return DAYS[position]; } - @Override + /** @return the position of {@code weekDay} within the user-defined week */ public int positionOf(int weekDay) { if (weekDay < SUNDAY || weekDay > SATURDAY) throw new ArrayIndexOutOfBoundsException("Week day ("+weekDay+") out of range"); diff --git a/app/src/main/java/com/philliphsu/clock2/DaysOfWeekHelper.java b/app/src/main/java/com/philliphsu/clock2/DaysOfWeekHelper.java deleted file mode 100644 index d6a7a12..0000000 --- a/app/src/main/java/com/philliphsu/clock2/DaysOfWeekHelper.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.philliphsu.clock2; - -/** - * Created by Phillip Hsu on 6/9/2016. - */ -public interface DaysOfWeekHelper { - /** @return the week day at {@code position} within the user-defined week */ - int weekDayAt(int position); - - /** @return the position of the {@code weekDay} within the user-defined week */ - int positionOf(int weekDay); -} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b8eec26..e563869 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -75,13 +75,6 @@ Settings - Sun - Mon - Tue - Wed - Thu - Fri - Sat Every day 0