Skip to content

Commit

Permalink
NaturalHourCalculator
Browse files Browse the repository at this point in the history
enforce a 1 second time-out of when querying content providers
  • Loading branch information
forrestguice committed Nov 18, 2024
1 parent c12bb67 commit 4a5e37d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
Copyright (C) 2024 Forrest Guice
This file is part of SuntimesWidget.
SuntimesWidget is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SuntimesWidget is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SuntimesWidget. If not, see <http://www.gnu.org/licenses/>.
*/

package com.forrestguice.suntimes.naturalhour.data;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ExecutorUtils
{
public static boolean runTask(String tag, @NonNull final Callable<Boolean> r, long timeoutAfter)
{
Boolean result = getResult(tag, r, timeoutAfter);
return (result != null && result);
}

@Nullable
public static <T> T getResult(String tag, @NonNull final Callable<T> callable, long timeoutAfter)
{
ExecutorService executor = Executors.newSingleThreadExecutor();
final Future<T> task = executor.submit(callable);
try {
return task.get(timeoutAfter, TimeUnit.MILLISECONDS);

} catch (TimeoutException | InterruptedException | ExecutionException e) {
Log.e(tag, "getResult: failed! " + e);
return null;

} finally {
task.cancel(true);
executor.shutdownNow();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
Copyright (C) 2020 Forrest Guice
Copyright (C) 2020-2024 Forrest Guice
This file is part of Natural Hour.
Natural Hour is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -29,6 +29,7 @@

import java.util.Arrays;
import java.util.Calendar;
import java.util.concurrent.Callable;

public class NaturalHourCalculator
{
Expand Down Expand Up @@ -89,7 +90,7 @@ public boolean queryData(ContentResolver resolver, @NonNull NaturalHourData data
data.dayEnd = daylight[1];

if (querySolsticeEquinox) {
solsticeEquinox = queryEquinoxSolsticeDates(resolver, date);
solsticeEquinox = queryEquinoxSolsticeDatesWithTimeout(resolver, date, MAX_WAIT_MS);
data.solsticeEquinox = solsticeEquinox;
}

Expand All @@ -110,7 +111,7 @@ public long[] queryTwilights(ContentResolver resolver, long date, NaturalHourDat
Double longitude = (useDefaultLocation ? null : data.longitude);
Double altitude = (useDefaultLocation ? null : data.altitude);

return queryTwilight(resolver, date, latitude, longitude, altitude, new String[] {
return queryTwilightWithTimeout(resolver, date, latitude, longitude, altitude, new String[] {
CalculatorProviderContract.COLUMN_SUN_ASTRO_RISE, // 0
CalculatorProviderContract.COLUMN_SUN_NAUTICAL_RISE, // 1
CalculatorProviderContract.COLUMN_SUN_CIVIL_RISE, // 2
Expand All @@ -119,8 +120,7 @@ public long[] queryTwilights(ContentResolver resolver, long date, NaturalHourDat
CalculatorProviderContract.COLUMN_SUN_CIVIL_SET, // 5
CalculatorProviderContract.COLUMN_SUN_NAUTICAL_SET, // 6
CalculatorProviderContract.COLUMN_SUN_ASTRO_SET } // 7

);
, MAX_WAIT_MS);
}

public long[] queryStartEndDay(ContentResolver resolver, long dateMillis, NaturalHourData data)
Expand All @@ -139,19 +139,35 @@ public long[] queryStartEndDay(ContentResolver resolver, long dateMillis, Natura
}

public long[] querySunriseSunset(ContentResolver resolver, long dateMillis, Double latitude, Double longitude, Double altitude) {
return queryTwilight(resolver, dateMillis, latitude, longitude, altitude, new String[] { CalculatorProviderContract.COLUMN_SUN_ACTUAL_RISE, CalculatorProviderContract.COLUMN_SUN_ACTUAL_SET });
return queryTwilightWithTimeout(resolver, dateMillis, latitude, longitude, altitude, new String[] { CalculatorProviderContract.COLUMN_SUN_ACTUAL_RISE, CalculatorProviderContract.COLUMN_SUN_ACTUAL_SET }, MAX_WAIT_MS);
}

public long[] queryCivilTwilight(ContentResolver resolver, long date, Double latitude, Double longitude, Double altitude) {
return queryTwilight(resolver, date, latitude, longitude, altitude, new String[] { CalculatorProviderContract.COLUMN_SUN_CIVIL_RISE, CalculatorProviderContract.COLUMN_SUN_CIVIL_SET });
return queryTwilightWithTimeout(resolver, date, latitude, longitude, altitude, new String[] { CalculatorProviderContract.COLUMN_SUN_CIVIL_RISE, CalculatorProviderContract.COLUMN_SUN_CIVIL_SET }, MAX_WAIT_MS);
}

public long[] queryNauticalTwilight(ContentResolver resolver, long date, Double latitude, Double longitude, Double altitude) {
return queryTwilight(resolver, date, latitude, longitude, altitude, new String[] { CalculatorProviderContract.COLUMN_SUN_NAUTICAL_RISE, CalculatorProviderContract.COLUMN_SUN_NAUTICAL_SET });
return queryTwilightWithTimeout(resolver, date, latitude, longitude, altitude, new String[] { CalculatorProviderContract.COLUMN_SUN_NAUTICAL_RISE, CalculatorProviderContract.COLUMN_SUN_NAUTICAL_SET }, MAX_WAIT_MS);
}

public long[] queryAstroTwilight(ContentResolver resolver, long date, Double latitude, Double longitude, Double altitude) {
return queryTwilight(resolver, date, latitude, longitude, altitude, new String[] { CalculatorProviderContract.COLUMN_SUN_ASTRO_RISE, CalculatorProviderContract.COLUMN_SUN_ASTRO_SET });
return queryTwilightWithTimeout(resolver, date, latitude, longitude, altitude, new String[] { CalculatorProviderContract.COLUMN_SUN_ASTRO_RISE, CalculatorProviderContract.COLUMN_SUN_ASTRO_SET }, MAX_WAIT_MS);
}

public static final long MAX_WAIT_MS = 1000;
public long[] queryTwilightWithTimeout(final ContentResolver resolver, final long date, final Double latitude, final Double longitude, final Double altitude, final String[] projection, long timeoutAfter)
{
long[] result = ExecutorUtils.getResult("queryTwilight", new Callable<long[]>() {
public long[] call() throws Exception {
return queryTwilight(resolver, date, latitude, longitude, altitude, projection);
}
}, timeoutAfter);

if (result == null) {
result = new long[projection.length];
Arrays.fill(result, -1);
}
return result;
}

public long[] queryTwilight(ContentResolver resolver, long date, Double latitude, Double longitude, Double altitude, String[] projection)
Expand Down Expand Up @@ -183,6 +199,20 @@ public long[] queryTwilight(ContentResolver resolver, long date, Double latitude
return retValue;
}

public long[] queryEquinoxSolsticeDatesWithTimeout(final ContentResolver resolver, final long dateMillis, long timeoutAfter)
{
long[] result = ExecutorUtils.getResult("queryEquinox", new Callable<long[]>() {
public long[] call() throws Exception {
return queryEquinoxSolsticeDates(resolver, dateMillis);
}
}, timeoutAfter);

if (result == null) {
result = new long[] {-1, -1, -1, -1};
}
return result;
}

protected long[] solsticeEquinox = new long[] {-1, -1, -1, -1};
public long[] queryEquinoxSolsticeDates(ContentResolver resolver, long dateMillis)
{
Expand Down

0 comments on commit 4a5e37d

Please sign in to comment.