Refactored DurationUtils to include DAYS field, and created string resources with days

This commit is contained in:
Phillip Hsu
2016-06-10 20:37:45 -07:00
parent 8d23d8f0b2
commit 0bcbd6b421
4 changed files with 212 additions and 64 deletions
@@ -60,6 +60,13 @@ public class RingtoneActivity extends AppCompatActivity implements RingtoneServi
// This could be the case if we're starting a new instance of this activity after leaving the first launch.
AlarmUtils.removeUpcomingAlarmNotification(this, mAlarm);
// As of API 19, alarms scheduled with AlarmManager.setRepeating() are inexact. The recommended
// workaround is to schedule one-time exact alarms, and reschedule each time after handling
// an alarm delivery.
if (mAlarm.hasRecurrence()) {
AlarmUtils.scheduleAlarm(this, mAlarm);
}
Intent intent = new Intent(this, RingtoneService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
@@ -12,74 +12,44 @@ import java.util.concurrent.TimeUnit;
* Created by Phillip Hsu on 6/6/2016.
*/
public class DurationUtils {
public static final int HOURS = 0;
public static final int MINUTES = 1;
public static final int SECONDS = 2;
public static final int MILLIS = 3;
public static final int DAYS = 0;
public static final int HOURS = 1;
public static final int MINUTES = 2;
public static final int SECONDS = 3;
public static final int MILLIS = 4;
/** Return a string representing the duration, formatted in hours and minutes.
* TODO: Need to adapt this to represent all time fields eventually */
* TODO: Need to adapt this to represent all time fields eventually
* TODO: Since this is primarirly used for alarm set toasts, you should make different methods for
* different use cases. E.g. Timer's duration should have its own method.
* TODO: Then, rename this method to something about alarm toasts. */
public static String toString(Context context, long millis, boolean abbreviate) {
long[] fields = breakdown(millis);
long numHours = fields[0];
long numMins = fields[1];
long numSecs = fields[2]; // only considered for rounding of minutes
long numDays = fields[DAYS];
long numHours = fields[HOURS];
long numMins = fields[MINUTES];
long numSecs = fields[SECONDS]; // only considered for rounding of minutes
if (numSecs >= 31) {
numMins++;
numSecs = 0; // Not totally necessary since it won't be considered any more
if (numMins == 60) {
numHours++;
numMins = 0;
if (numHours == 24) {
numDays++;
numHours = 0;
}
}
}
@StringRes int res;
int res;
if (abbreviate) {
if (numHours == 0) {
if (numMins == 0) {
res = R.string.abbrev_less_than_one_minute;
} else {
res = R.string.abbrev_minutes;
}
} else {
if (numMins == 0) {
res = R.string.abbrev_hours;
} else {
res = R.string.abbrev_hours_and_minutes;
}
}
res = getAbbreviatedStringRes(numDays, numHours, numMins);
} else {
if (numHours == 0) {
if (numMins == 0) {
res = R.string.less_than_one_minute;
} else if (numMins == 1) {
res = R.string.minute;
} else {
res = R.string.minutes;
}
} else {
if (numHours == 1) {
if (numMins == 0) {
res = R.string.hour;
} else if (numMins == 1) {
res = R.string.hour_and_minute;
} else {
res = R.string.hour_and_minutes;
}
} else {
if (numMins == 0) {
res = R.string.hours;
} else if (numMins == 1) {
res = R.string.hours_and_minute;
} else {
res = R.string.hours_and_minutes;
}
}
}
res = getStringRes(numDays, numHours, numMins);
}
return context.getString(res, numHours, numMins);
return context.getString(res, numDays, numHours, numMins);
}
/**
@@ -112,7 +82,8 @@ public class DurationUtils {
* and milliseconds in that order
*/
public static long[] breakdown(long t, @NonNull TimeUnit unit, boolean roundMillis) {
long hours = unit.toHours(t);
long days = unit.toDays(t);
long hours = unit.toHours(t) % 24;
long minutes = unit.toMinutes(t) % 60;
long seconds = unit.toSeconds(t) % 60;
long msecs = unit.toMillis(t) % 1000;
@@ -126,11 +97,136 @@ public class DurationUtils {
if (minutes == 60) {
hours++;
minutes = 0;
if (hours == 24) {
days++;
hours = 0;
}
}
}
}
}
return new long[] { hours, minutes, seconds, msecs };
return new long[] { days, hours, minutes, seconds, msecs };
}
@StringRes
private static int getStringRes(long numDays, long numHours, long numMins) {
int res;
if (numDays == 0) {
if (numHours == 0) {
if (numMins == 0) {
res = R.string.less_than_one_minute;
} else if (numMins == 1) {
res = R.string.minute;
} else {
res = R.string.minutes;
}
} else if (numHours == 1) {
if (numMins == 0) {
res = R.string.hour;
} else if (numMins == 1) {
res = R.string.hour_and_minute;
} else {
res = R.string.hour_and_minutes;
}
} else {
if (numMins == 0) {
res = R.string.hours;
} else if (numMins == 1) {
res = R.string.hours_and_minute;
} else {
res = R.string.hours_and_minutes;
}
}
} else if (numDays == 1) {
if (numHours == 0) {
if (numMins == 0) {
res = R.string.day;
} else if (numMins == 1) {
res = R.string.day_and_minute;
} else {
res = R.string.day_and_minutes;
}
} else if (numHours == 1) {
if (numMins == 0) {
res = R.string.day_and_hour;
} else if (numMins == 1) {
res = R.string.day_hour_and_minute;
} else {
res = R.string.day_hour_and_minutes;
}
} else {
if (numMins == 0) {
res = R.string.day_and_hours;
} else if (numMins == 1) {
res = R.string.day_hours_and_minute;
} else {
res = R.string.day_hours_and_minutes;
}
}
} else {
if (numHours == 0) {
if (numMins == 0) {
res = R.string.days;
} else if (numMins == 1) {
res = R.string.days_and_minute;
} else {
res = R.string.days_and_minutes;
}
} else if (numHours == 1) {
if (numMins == 0) {
res = R.string.days_and_hour;
} else if (numMins == 1) {
res = R.string.days_hour_and_minute;
} else {
res = R.string.days_hour_and_minutes;
}
} else {
if (numMins == 0) {
res = R.string.days_and_hours;
} else if (numMins == 1) {
res = R.string.days_hours_and_minute;
} else {
res = R.string.days_hours_and_minutes;
}
}
}
return res;
}
@StringRes
private static int getAbbreviatedStringRes(long numDays, long numHours, long numMins) {
int res;
if (numDays == 0) {
if (numHours == 0) {
if (numMins == 0) {
res = R.string.abbrev_less_than_one_minute;
} else {
res = R.string.abbrev_minutes;
}
} else {
if (numMins == 0) {
res = R.string.abbrev_hours;
} else {
res = R.string.abbrev_hours_and_minutes;
}
}
} else {
if (numHours == 0) {
if (numMins == 0) {
res = R.string.abbrev_days;
} else {
res = R.string.abbrev_days_and_minutes;
}
} else {
if (numMins == 0) {
res = R.string.abbrev_days_and_hours;
} else {
res = R.string.abbrev_days_hours_and_minutes;
}
}
}
return res;
}
}