From f2d661e860e84701fbff1c58263ad4e8b2d03b2a Mon Sep 17 00:00:00 2001 From: Stephen Michel Date: Sat, 1 Sep 2018 09:36:53 -0400 Subject: [PATCH 1/5] bump version to 0.2 --- library/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/build.gradle b/library/build.gradle index f464d2d..ea21bc5 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -5,8 +5,8 @@ android { defaultConfig { minSdkVersion 14 targetSdkVersion 27 - versionCode 1 - versionName "0.1" + versionCode 2 + versionName "0.2" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { From 139f6963c8d10bebc0dc3ffbee82db204c96d2b6 Mon Sep 17 00:00:00 2001 From: Stephen Michel Date: Thu, 6 Sep 2018 12:30:27 -0400 Subject: [PATCH 2/5] Use onAlarm callback instead of newAnimation. Fixes #9 --- .../transition/TransitionScheduler.java | 78 ++++++++++++------- 1 file changed, 48 insertions(+), 30 deletions(-) diff --git a/library/src/main/java/org/libreshift/transition/TransitionScheduler.java b/library/src/main/java/org/libreshift/transition/TransitionScheduler.java index da37a50..c4d1c5a 100644 --- a/library/src/main/java/org/libreshift/transition/TransitionScheduler.java +++ b/library/src/main/java/org/libreshift/transition/TransitionScheduler.java @@ -1,6 +1,5 @@ package org.libreshift.transition; -import android.animation.ValueAnimator; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; @@ -11,21 +10,28 @@ import android.os.Build; import android.util.Log; +import java.util.HashSet; +import java.util.Set; + public abstract class TransitionScheduler extends BroadcastReceiver { static final String TAG = "TransitionScheduler"; static final String ACTION_ALARM = "org.libreshift.transition.ACTION_ALARM"; - static final String EXTRA_START = "org.libreshift.transition.EXTRA_START"; + static final String IDS = "IDS"; + static final String START = "_START"; + static final String DURATION = "_DURATION"; + + // One hour; override for a different default + static long DURATION_DEFAULT = 3600000; - abstract ValueAnimator newAnimation(String id); + // It is important to compare the start time to the current time, + // since alarms may be delayed + abstract void onAlarm(String id, long startTime, long duration); // Is UPDATE_CURRENT the right flag? Red Moon was using the constant '0', // which doesn't match the constant of *any* of the PendingIntent flags.. static final int FLAG = PendingIntent.FLAG_UPDATE_CURRENT; - // TODO: Store more than one transition at once. - Transition transition = null; - @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); @@ -33,13 +39,12 @@ public void onReceive(Context context, Intent intent) { switch (action) { case ACTION_ALARM: String id = intent.getDataString(); + String id_start = id + START; + String id_duration = id + DURATION; SharedPreferences prefs = getPrefs(context); - long start = prefs.getLong(EXTRA_START, -1); - if (start == -1) { - ValueAnimator animator = newAnimation(id); - transition = new Transition(context, animator, start); - transition.start(); - } + long start = prefs.getLong(id_start, System.currentTimeMillis()); + long duration = prefs.getLong(id_duration, DURATION_DEFAULT); + onAlarm(id, start, duration); break; case Intent.ACTION_BOOT_COMPLETED: reschedule(context); @@ -49,20 +54,32 @@ public void onReceive(Context context, Intent intent) { } // Passing an id of an alarm that is already scheduled will overwrite that alarm - public void schedule(Context context, String id, long startAtMillis) { + public void schedule(Context context, String id, long startTime, long duration) { // TODO: If there's already an alarm scheduled, should we return the old time? - // TODO: If startAtMillis is in the past, start the transition right away + long now = System.currentTimeMillis(); + long endTime = startTime + duration; + if (now >= endTime) { + return; // TODO: Should we do anything here? + } SharedPreferences prefs = getPrefs(context); - prefs.edit().putLong(id, startAtMillis).apply(); + Set ids = new HashSet<>(prefs.getStringSet(IDS, new HashSet())); + String id_start = id + START; + String id_duration = id + DURATION; + ids.add(id); + prefs.edit() + .putStringSet(IDS, ids) + .putLong(id_start, startTime) + .putLong(id_duration, duration) + .apply(); PendingIntent pi = getIntent(context, id); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); if (am != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - am.setExact(AlarmManager.RTC_WAKEUP, startAtMillis, pi); + am.setExact(AlarmManager.RTC_WAKEUP, startTime, pi); } else { - am.set(AlarmManager.RTC_WAKEUP, startAtMillis, pi); + am.set(AlarmManager.RTC_WAKEUP, startTime, pi); } } } @@ -75,23 +92,24 @@ public void cancel(Context context, String id) { } SharedPreferences prefs = getPrefs(context); - prefs.edit().remove(id).apply(); - - if (transition != null) { - transition.cancel(); - transition = null; - } + Set ids = new HashSet<>(prefs.getStringSet(IDS, new HashSet())); + ids.remove(id); + String id_start = id + START; + String id_duration = id + DURATION; + prefs.edit().putStringSet(IDS, ids).remove(id_start).remove(id_duration).apply(); } void reschedule(Context context) { SharedPreferences prefs = getPrefs(context); - for (String id : prefs.getAll().keySet()) { - Long startTime = prefs.getLong(id, -1); - if (startTime != -1) { - schedule(context, id, startTime); - } else { - Log.e(TAG, "Couldn't get start time for alarm: " + id); - } + Set ids = prefs.getStringSet(IDS, new HashSet()); + + for (String id : ids) { + String id_start = id + START; + String id_duration = id + DURATION; + long startTime = prefs.getLong(id_start, 0); + long duration = prefs.getLong(id_duration, DURATION_DEFAULT); + cancel(context, id); + schedule(context, id, startTime, duration); } } From 3a377922f9acde7344575e9a96359bf1dc288f93 Mon Sep 17 00:00:00 2001 From: Stephen Michel Date: Thu, 6 Sep 2018 12:42:36 -0400 Subject: [PATCH 3/5] add visibility modifiers --- .../org/libreshift/transition/TransitionScheduler.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/library/src/main/java/org/libreshift/transition/TransitionScheduler.java b/library/src/main/java/org/libreshift/transition/TransitionScheduler.java index c4d1c5a..ea8dd12 100644 --- a/library/src/main/java/org/libreshift/transition/TransitionScheduler.java +++ b/library/src/main/java/org/libreshift/transition/TransitionScheduler.java @@ -17,9 +17,9 @@ public abstract class TransitionScheduler extends BroadcastReceiver { static final String TAG = "TransitionScheduler"; static final String ACTION_ALARM = "org.libreshift.transition.ACTION_ALARM"; - static final String IDS = "IDS"; - static final String START = "_START"; - static final String DURATION = "_DURATION"; + private static final String IDS = "IDS"; + private static final String START = "_START"; + private static final String DURATION = "_DURATION"; // One hour; override for a different default static long DURATION_DEFAULT = 3600000; @@ -99,7 +99,7 @@ public void cancel(Context context, String id) { prefs.edit().putStringSet(IDS, ids).remove(id_start).remove(id_duration).apply(); } - void reschedule(Context context) { + public void reschedule(Context context) { SharedPreferences prefs = getPrefs(context); Set ids = prefs.getStringSet(IDS, new HashSet()); @@ -123,5 +123,5 @@ static SharedPreferences getPrefs(Context context) { return context.getSharedPreferences(PREFERENCE, Context.MODE_PRIVATE); } - static final String PREFERENCE = "org.libreshift.transition.preference"; + private static final String PREFERENCE = "org.libreshift.transition.preference"; } From 3e245a809d36c27bf502e9ea91e0ebdb3b8b4e3b Mon Sep 17 00:00:00 2001 From: Stephen Michel Date: Thu, 6 Sep 2018 12:44:06 -0400 Subject: [PATCH 4/5] bump version to 0.3 --- library/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/build.gradle b/library/build.gradle index ea21bc5..7c12b5b 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -5,8 +5,8 @@ android { defaultConfig { minSdkVersion 14 targetSdkVersion 27 - versionCode 2 - versionName "0.2" + versionCode 3 + versionName "0.3" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { From 22c5d94d44b12b9410b2f1666ddbd863005956fc Mon Sep 17 00:00:00 2001 From: Stephen Michel Date: Thu, 6 Sep 2018 12:53:40 -0400 Subject: [PATCH 5/5] Remove questions in comments since I made #11 now --- .../org/libreshift/transition/TransitionScheduler.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/library/src/main/java/org/libreshift/transition/TransitionScheduler.java b/library/src/main/java/org/libreshift/transition/TransitionScheduler.java index ea8dd12..5867022 100644 --- a/library/src/main/java/org/libreshift/transition/TransitionScheduler.java +++ b/library/src/main/java/org/libreshift/transition/TransitionScheduler.java @@ -28,8 +28,6 @@ public abstract class TransitionScheduler extends BroadcastReceiver { // since alarms may be delayed abstract void onAlarm(String id, long startTime, long duration); - // Is UPDATE_CURRENT the right flag? Red Moon was using the constant '0', - // which doesn't match the constant of *any* of the PendingIntent flags.. static final int FLAG = PendingIntent.FLAG_UPDATE_CURRENT; @Override @@ -55,13 +53,12 @@ public void onReceive(Context context, Intent intent) { // Passing an id of an alarm that is already scheduled will overwrite that alarm public void schedule(Context context, String id, long startTime, long duration) { - // TODO: If there's already an alarm scheduled, should we return the old time? - long now = System.currentTimeMillis(); long endTime = startTime + duration; if (now >= endTime) { - return; // TODO: Should we do anything here? + return; } + SharedPreferences prefs = getPrefs(context); Set ids = new HashSet<>(prefs.getStringSet(IDS, new HashSet())); String id_start = id + START;