Skip to content

Commit

Permalink
Merge pull request #708 from forrestguice/events
Browse files Browse the repository at this point in the history
events
  • Loading branch information
forrestguice authored Jun 3, 2023
2 parents ec8d9c5 + 8be3af5 commit 82d2abf
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,13 @@ private void onPickTheme(int requestCode, int resultCode, Intent data)
}
}

private void onManageEvents(int requestCode, int resultCode, Intent data)
private void onManageEvents(int requestCode, int resultCode, @Nullable Intent data)
{
boolean adapterModified = data.getBooleanExtra(ActionListActivity.ADAPTER_MODIFIED, false);
boolean adapterModified = ((data != null) && data.getBooleanExtra(ActionListActivity.ADAPTER_MODIFIED, false));

if (resultCode == RESULT_OK)
{
String eventID = data.getStringExtra(EventListActivity.SELECTED_EVENTID);
String eventID = ((data != null) ? data.getStringExtra(EventListActivity.SELECTED_EVENTID) : null);
if (eventID != null) {
EventSettings.setShown(context, eventID, true);
adapterModified = true;
Expand Down
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 @@ -175,9 +172,16 @@ private Cursor queryEvents(@Nullable String eventID, @NonNull Uri uri, @Nullable

// list all custom events
List<EventSettings.EventAlias> events1 = EventSettings.loadEvents(context, EventType.SUN_ELEVATION);
for (EventSettings.EventAlias event : events1) {
retValue.addRow(createRow(context, event, true, columns, selection, selectionArgs));
retValue.addRow(createRow(context, event, false, columns, selection, selectionArgs));
for (EventSettings.EventAlias event : events1)
{
Object[] row1 = createRow(context, event, true, columns, selection, selectionArgs);
if (row1 != null) {
retValue.addRow(row1);
}
Object[] row2 = createRow(context, event, false, columns, selection, selectionArgs);
if (row2 != null) {
retValue.addRow(row2);
}
}

} else {
Expand Down Expand Up @@ -211,7 +215,7 @@ private void addRowsToCursor(Context context, MatrixCursor retValue, String even
return;
}

switch(type)
switch (type)
{
case DATE:
try {
Expand All @@ -230,7 +234,10 @@ private void addRowsToCursor(Context context, MatrixCursor retValue, String even
}
boolean rising = suffix.equals(ElevationEvent.SUFFIX_RISING);
EventSettings.EventAlias alias = EventSettings.loadEvent(context, aliasID);
retValue.addRow(createRow(context, alias, rising, columns, selection, selectionArgs));
Object[] row = createRow(context, alias, rising, columns, selection, selectionArgs);
if (row != null) {
retValue.addRow(row);
}
break;

case SUN_ELEVATION:
Expand Down Expand Up @@ -313,6 +320,7 @@ private Object[] createRow(@NonNull Context context, @NonNull SolarEvents event,
/**
* createRow( EventAlias )
*/
@Nullable
private Object[] createRow(@NonNull Context context, EventSettings.EventAlias event, boolean rising, String[] columns, @Nullable String selection, @Nullable String[] selectionArgs)
{
Uri uri = Uri.parse(event.getUri() + (rising ? ElevationEvent.SUFFIX_RISING : ElevationEvent.SUFFIX_SETTING));
Expand All @@ -321,6 +329,13 @@ private Object[] createRow(@NonNull Context context, EventSettings.EventAlias ev
cursor.moveToFirst();
}

if (cursor != null && cursor.isAfterLast())
{
Log.w("AlarmEventProvider", "null result for " + event.getID());
cursor.close();
return null;
}

Object[] row = new Object[columns.length];
for (int i=0; i<columns.length; i++)
{
Expand Down Expand Up @@ -543,16 +558,22 @@ 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(double angle, int offset, boolean rising) {
this.angle = angle;
this.offset = offset;
this.rising = rising;
}

protected int angle;
public int getAngle() {
protected double angle;
public double getAngle() {
return angle;
}

protected int offset; // milliseconds
public int getOffset() {
return offset;
}

protected boolean rising;
public boolean isRising() {
return rising;
Expand All @@ -576,63 +597,99 @@ 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(double 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());
String angle = utils.formatAsElevation(getAngle(), 1).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), (int)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(double 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;
double angle;
int 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 = Double.parseDouble(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;
}
}

@Nullable
public static Calendar updateAlarmTime_sunElevationEvent(Context context, @NonNull SunElevationEvent event, @NonNull Location location, long offset, boolean repeating, ArrayList<Integer> repeatingDays, Calendar now)
{
SuntimesRiseSetData sunData = getData_sunElevationEvent(context, event.getAngle(), location);
SuntimesRiseSetData sunData = getData_sunElevationEvent(context, event.getAngle(), event.getOffset(), location);

Calendar alarmTime = Calendar.getInstance();
Calendar eventTime;
Expand Down Expand Up @@ -673,11 +730,12 @@ public static Calendar updateAlarmTime_sunElevationEvent(Context context, @NonNu
return eventTime;
}

private static SuntimesRiseSetData getData_sunElevationEvent(Context context, int angle, @NonNull Location location)
private static SuntimesRiseSetData getData_sunElevationEvent(Context context, double angle, int offset, @NonNull Location location)
{
SuntimesRiseSetData sunData = new SuntimesRiseSetData(context, 0);
sunData.setLocation(location);
sunData.setAngle(angle);
sunData.setOffset(offset);
sunData.setTodayIs(Calendar.getInstance());
return sunData;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
Copyright (C) 2014-2022 Forrest Guice
Copyright (C) 2014-2023 Forrest Guice
This file is part of SuntimesWidget.
SuntimesWidget is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -90,6 +90,7 @@ public void setDataMode(WidgetSettings.RiseSetDataMode value)
EventSettings.EventAlias alias = ((WidgetSettings.EventAliasTimeMode) dataMode).getEvent();
AlarmEventProvider.SunElevationEvent event = AlarmEventProvider.SunElevationEvent.valueOf(Uri.parse(alias.getUri()).getLastPathSegment());
this.angle = (event == null ? null : event.getAngle());
this.offset = (event == null ? 0 : event.getOffset());
}
WidgetSettings.TimeMode mode = dataMode.getTimeMode();
this.timeMode = ((mode != null) ? mode : WidgetSettings.PREF_DEF_GENERAL_TIMEMODE);
Expand All @@ -101,14 +102,25 @@ public WidgetSettings.RiseSetDataMode dataMode() {
/**
* Property: sun angle (overrides time mode)
*/
protected Integer angle = null;
public Integer angle() {
protected Double angle = null;
public Double angle() {
return angle;
}
public void setAngle( int value ) {
public void setAngle( double value ) {
angle = value;
}

/**
* property: offset
*/
protected int offset = 0;
public void setOffset(int millis) {
offset = millis;
}
public int getOffset() {
return offset;
}

/**
* Property: compare mode
*/
Expand Down Expand Up @@ -254,6 +266,7 @@ protected void initFromOther( SuntimesRiseSetData other, int layoutID )
this.compareMode = other.compareMode();
this.timeMode = other.timeMode();
this.angle = other.angle;
this.offset = other.offset;

this.sunriseCalendarToday = other.sunriseCalendarToday();
this.sunsetCalendarToday = other.sunsetCalendarToday();
Expand Down Expand Up @@ -406,6 +419,13 @@ public void calculate()
}
}

if (offset != 0) {
sunriseCalendarToday.add(Calendar.MILLISECOND, offset);
sunsetCalendarToday.add(Calendar.MILLISECOND, offset);
sunriseCalendarOther.add(Calendar.MILLISECOND, offset);
sunsetCalendarOther.add(Calendar.MILLISECOND, offset);
}

dayLengthToday = determineDayLength(sunriseCalendarToday, sunsetCalendarToday);
dayLengthOther = determineDayLength(sunriseCalendarOther, sunsetCalendarOther);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
Copyright (C) 2014-2018 Forrest Guice
Copyright (C) 2014-2024 Forrest Guice
This file is part of SuntimesWidget.
SuntimesWidget is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -158,6 +158,7 @@ protected void initFromOther(SuntimesRiseSetData2 other, int layoutID )
this.compareMode = other.compareMode();
this.timeMode = other.timeMode();
this.angle = other.angle;
this.offset = other.offset;

this.dayLengthToday = other.dayLengthToday();
this.dayLengthOther = other.dayLengthOther();
Expand Down Expand Up @@ -247,6 +248,10 @@ public void calculate()
{
sunrise[i] = calculator.getSunriseCalendarForDate(calendar[i], angle);
sunset[i] = calculator.getSunsetCalendarForDate(calendar[i], angle);
if (offset != 0) {
sunrise[i].add(Calendar.MILLISECOND, offset);
sunset[i].add(Calendar.MILLISECOND, offset);
}
continue;
}

Expand Down Expand Up @@ -292,6 +297,10 @@ public void calculate()
sunset[i] = calculator.getOfficialSunsetCalendarForDate(calendar[i]);
break;
}
if (offset != 0) {
sunrise[i].add(Calendar.MILLISECOND, offset);
sunset[i].add(Calendar.MILLISECOND, offset);
}
}

int i = indexOfOther();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@ public long getTropicalYearLength(Calendar date) {
}

@Override
public Calendar getSunriseCalendarForDate( Calendar date, int angle ) {
public Calendar getSunriseCalendarForDate( Calendar date, double angle ) {
return null; // TODO: supported by this lib?
}

@Override
public Calendar getSunsetCalendarForDate( Calendar date, int angle ) {
public Calendar getSunsetCalendarForDate( Calendar date, double angle ) {
return null; // TODO: supported by this lib?
}

Expand Down
Loading

0 comments on commit 82d2abf

Please sign in to comment.