diff --git a/app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/AlarmEventProvider.java b/app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/AlarmEventProvider.java index 7c2ebaca9..3e2429ce7 100644 --- a/app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/AlarmEventProvider.java +++ b/app/src/main/java/com/forrestguice/suntimeswidget/alarmclock/AlarmEventProvider.java @@ -1,5 +1,5 @@ /** - Copyright (C) 2021 Forrest Guice + Copyright (C) 2021-2023 Forrest Guice This file is part of SuntimesWidget. SuntimesWidget is free software: you can redistribute it and/or modify @@ -19,15 +19,12 @@ package com.forrestguice.suntimeswidget.alarmclock; import android.content.ContentProvider; -import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; -import android.content.Intent; import android.content.UriMatcher; import android.database.Cursor; import android.database.MatrixCursor; import android.net.Uri; -import android.os.Parcel; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.Log; @@ -543,8 +540,9 @@ public abstract static class ElevationEvent public static final String SUFFIX_RISING = "r"; public static final String SUFFIX_SETTING = "s"; - public ElevationEvent(int angle, boolean rising) { + public ElevationEvent(int angle, int offset, boolean rising) { this.angle = angle; + this.offset = offset; this.rising = rising; } @@ -553,6 +551,11 @@ public int getAngle() { return angle; } + protected int offset; // milliseconds + public int getOffset() { + return offset; + } + protected boolean rising; public boolean isRising() { return rising; @@ -576,22 +579,17 @@ public static final class SunElevationEvent extends ElevationEvent { public static final String NAME_PREFIX = "SUN_"; - public SunElevationEvent(int angle, boolean rising) { - super(angle, rising); - } - - @Override - protected String getEventName(Context context) { // e.g. SUN_-10r (sun elevation @ -10 degrees (rising)) - return NAME_PREFIX + angle + (rising ? SUFFIX_RISING : SUFFIX_SETTING); + public SunElevationEvent(int angle, int offset, boolean rising) { + super(angle, offset, rising); } @Override protected String getEventTitle(Context context) { - return context.getString(R.string.sunevent_title) + " " + (rising ? "rising" : "setting") + " (" + angle + ")"; // TODO: format + return offsetDisplay(context) + context.getString(R.string.sunevent_title) + " " + (rising ? "rising" : "setting") + " (" + angle + ")"; // TODO: format } @Override protected String getEventPhrase(Context context) { - return context.getString(R.string.sunevent_title) + " " + (rising ? "rising" : "setting") + " at " + angle; // TODO: format + return offsetDisplay(context) + context.getString(R.string.sunevent_title) + " " + (rising ? "rising" : "setting") + " at " + angle; // TODO: format } @Override protected String getEventGender(Context context) { @@ -599,32 +597,72 @@ protected String getEventGender(Context context) { } @Override - protected String getEventSummary(Context context) { + protected String getEventSummary(Context context) + { SuntimesUtils utils = new SuntimesUtils(); String angle = utils.formatAsElevation(getAngle(), 0).toString(); - return context.getString(R.string.sunevent_summary_format, context.getString(R.string.sunevent_title), angle.toString()); + if (offset == 0) { + return offsetDisplay(context) + context.getString(R.string.sunevent_summary_format, context.getString(R.string.sunevent_title), angle.toString()); + } else { + return context.getString(R.string.sunevent_summary_format1, offsetDisplay(context), context.getString(R.string.sunevent_title), angle.toString()); + } + } + + private static final SuntimesUtils utils = new SuntimesUtils(); + public String offsetDisplay(Context context) + { + if (offset != 0) + { + SuntimesUtils.initDisplayStrings(context); + String offsetDisplay = utils.timeDeltaLongDisplayString(0, offset, false).getValue(); + return context.getResources().getQuantityString((offset < 0 ? R.plurals.offset_before_plural : R.plurals.offset_after_plural), angle, offsetDisplay); + } else return ""; } public static boolean isElevationEvent(String eventName) { return (eventName != null && (eventName.startsWith(NAME_PREFIX))); } + + /** + * @return e.g. SUN_-10r (@ -10 degrees (rising)), + * SUN_-10|-300000r (5m before @ 10 degrees (rising)) + */ + @Override + protected String getEventName(Context context) { + return getEventName(angle, offset, rising); + } + public static String getEventName(int angle, int offset, @Nullable Boolean rising) { + String name = NAME_PREFIX + + angle + + ((offset != 0) ? "|" + (int)Math.ceil(offset / 1000d / 60d) : ""); + if (rising != null) { + name += (rising ? SUFFIX_RISING : SUFFIX_SETTING); + } + return name; + } + @Nullable public static SunElevationEvent valueOf(String eventName) { if (isElevationEvent(eventName)) { - int angle; + int angle, offsetMinutes = 0; boolean hasSuffix = eventName.endsWith(SUFFIX_RISING) || eventName.endsWith(SUFFIX_SETTING); try { - String angleString = eventName.substring(4, eventName.length() - (hasSuffix ? 1 : 0)); - angle = Integer.parseInt(angleString); + String contentString = eventName.substring(4, eventName.length() - (hasSuffix ? 1 : 0)); + String[] contentParts = contentString.split("\\|"); + + angle = Integer.parseInt(contentParts[0]); + if (contentParts.length > 1) { + offsetMinutes = Integer.parseInt(contentParts[1]); + } } catch (Exception e) { Log.e("ElevationEvent", "createEvent: bad angle: " + e); return null; } boolean rising = eventName.endsWith(SUFFIX_RISING); - return new SunElevationEvent(angle, rising); + return new SunElevationEvent(angle, (offsetMinutes * 60 * 1000), rising); } else return null; } } @@ -643,6 +681,7 @@ public static Calendar updateAlarmTime_sunElevationEvent(Context context, @NonNu eventTime = (event.isRising() ? sunData.sunriseCalendarToday() : sunData.sunsetCalendarToday()); if (eventTime != null) { + eventTime.add(Calendar.MILLISECOND, event.getOffset()); eventTime.set(Calendar.SECOND, 0); alarmTime.setTimeInMillis(eventTime.getTimeInMillis() + offset); } @@ -665,6 +704,7 @@ public static Calendar updateAlarmTime_sunElevationEvent(Context context, @NonNu eventTime = (event.isRising() ? sunData.sunriseCalendarToday() : sunData.sunsetCalendarToday()); if (eventTime != null) { + eventTime.add(Calendar.MILLISECOND, event.getOffset()); eventTime.set(Calendar.SECOND, 0); alarmTime.setTimeInMillis(eventTime.getTimeInMillis() + offset); }