Skip to content

Commit

Permalink
AlarmEventProvider
Browse files Browse the repository at this point in the history
custom ElevationEvent to support an offset in minutes (#537)
  • Loading branch information
forrestguice committed Apr 8, 2023
1 parent 347dc98 commit 24b91eb
Showing 1 changed file with 60 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -576,55 +579,90 @@ 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) {
return context.getString(R.string.sunevent_phrase_gender);
}

@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;
}
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 24b91eb

Please sign in to comment.