-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
578 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...p-immunization/src/main/java/org/smartregister/immunization/domain/VaccinationClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.smartregister.immunization.domain; | ||
|
||
import org.joda.time.DateTime; | ||
|
||
/** | ||
* Created by Ephraim Kigamba - ekigamba@ona.io on 2020-02-13 | ||
*/ | ||
|
||
public class VaccinationClient { | ||
|
||
private String baseEntityId; | ||
private DateTime birthDateTime; | ||
|
||
public String getBaseEntityId() { | ||
return baseEntityId; | ||
} | ||
|
||
public void setBaseEntityId(String baseEntityId) { | ||
this.baseEntityId = baseEntityId; | ||
} | ||
|
||
public DateTime getBirthDateTime() { | ||
return birthDateTime; | ||
} | ||
|
||
public void setBirthDateTime(DateTime birthDateTime) { | ||
this.birthDateTime = birthDateTime; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...p-immunization/src/main/java/org/smartregister/immunization/job/DailyExecutionWindow.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.smartregister.immunization.job; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Created by Ephraim Kigamba - ekigamba@ona.io on 2020-02-13 | ||
*/ | ||
class DailyExecutionWindow { | ||
|
||
final long startMs; | ||
final long endMs; | ||
|
||
/** | ||
* Holds the start end time in ms for a job. | ||
* Will wrap around to next day if currentHour < targetHour. | ||
* If the current time is exactly now it will be forced to 60 seconds in the future. | ||
* | ||
* @param currentHour - current currentHour | ||
* @param currentMinute - current currentMinute | ||
* @param targetHour - currentHour we want to start | ||
* @param targetMinute - currentMinute we want to start | ||
* @param windowLengthInMinutes - number of minutes for the execution window | ||
*/ | ||
DailyExecutionWindow(int currentHour, int currentMinute, long targetHour, long targetMinute, long windowLengthInMinutes) { | ||
long hourOffset; | ||
long minuteOffset; | ||
|
||
if (targetHour == currentHour && targetMinute < currentMinute) { | ||
hourOffset = TimeUnit.HOURS.toMillis(23); | ||
} else if (targetHour - currentHour == 1) { // if we are less then an hour ahead, but into the next hour | ||
// move forward to 0 minute of next hour | ||
hourOffset = TimeUnit.MINUTES.toMillis(60 - currentMinute); | ||
currentMinute = 0; | ||
} else if (targetHour >= currentHour) { | ||
hourOffset = TimeUnit.HOURS.toMillis(targetHour - currentHour); | ||
} else { | ||
hourOffset = TimeUnit.HOURS.toMillis((24 + targetHour) - currentHour); | ||
} | ||
|
||
if (targetMinute >= currentMinute) { | ||
minuteOffset = TimeUnit.MINUTES.toMillis(targetMinute - currentMinute); | ||
} else { | ||
minuteOffset = TimeUnit.MINUTES.toMillis((60 + targetMinute) - currentMinute); | ||
} | ||
|
||
this.startMs = Math.max(hourOffset + minuteOffset, 60000); | ||
this.endMs = this.startMs + TimeUnit.MINUTES.toMillis(windowLengthInMinutes); | ||
|
||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...unization/src/main/java/org/smartregister/immunization/job/VaccineSchedulesUpdateJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.smartregister.immunization.job; | ||
|
||
import android.content.Intent; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.VisibleForTesting; | ||
import android.text.TextUtils; | ||
|
||
import com.evernote.android.job.DailyJob; | ||
import com.evernote.android.job.JobRequest; | ||
import com.evernote.android.job.util.support.PersistableBundleCompat; | ||
|
||
import org.smartregister.CoreLibrary; | ||
import org.smartregister.immunization.service.intent.VaccineSchedulesUpdateIntentService; | ||
import org.smartregister.immunization.util.IMConstants; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import timber.log.Timber; | ||
|
||
/** | ||
* This job starts a service which recalculates the alerts periodically(daily) or ad-hoc | ||
* | ||
* Created by Ephraim Kigamba - ekigamba@ona.io on 2020-02-13 | ||
*/ | ||
|
||
public abstract class VaccineSchedulesUpdateJob extends DailyJob { | ||
|
||
public static final String TAG = "VaccineSchedulesUpdateJob"; | ||
public static final String SCHEDULE_ADHOC_TAG = "VaccineSchedulesUpdateAdhocJob"; | ||
|
||
public static void scheduleEverydayAt(@NonNull String jobTag, int hour, int minute) { | ||
JobRequest.Builder jobRequest = new JobRequest.Builder(jobTag); | ||
long startTime = TimeUnit.HOURS.toMillis(hour) + TimeUnit.MINUTES.toMillis(minute); | ||
schedule(jobRequest, startTime, startTime + TimeUnit.MINUTES.toMillis(45)); | ||
} | ||
|
||
/** | ||
* For jobs that need to be started immediately | ||
*/ | ||
public static void scheduleJobImmediately() { | ||
int jobId = startNowOnce(new JobRequest.Builder(SCHEDULE_ADHOC_TAG)); | ||
Timber.d("Scheduling job with name " + SCHEDULE_ADHOC_TAG + " immediately with JOB ID " + jobId); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
protected DailyJobResult onRunDailyJob(@NonNull Params params) { | ||
Intent intent = new Intent(getContext(), VaccineSchedulesUpdateIntentService.class); | ||
intent.putExtra(IMConstants.IntentKey.VaccineScheduleUpdateIntentService.CLIENT_TABLE_NAME, getClientTableName()); | ||
|
||
getContext().startService(intent); | ||
updateLastTimeRun(); | ||
|
||
return DailyJobResult.SUCCESS; | ||
} | ||
|
||
@VisibleForTesting | ||
protected void updateLastTimeRun() { | ||
CoreLibrary.getInstance().context().allSharedPreferences().savePreference(IMConstants.Preference.VACCINE_SCHEDULE_UPDATE_LAST_TIME_RUN, String.valueOf(System.currentTimeMillis())); | ||
} | ||
|
||
public static long getLastTimeRun() { | ||
String timeString = CoreLibrary.getInstance().context().allSharedPreferences().getPreference(IMConstants.Preference.VACCINE_SCHEDULE_UPDATE_LAST_TIME_RUN); | ||
|
||
if (TextUtils.isEmpty(timeString)) { | ||
return 0L; | ||
} else { | ||
try { | ||
return Long.parseLong(timeString); | ||
} catch (NumberFormatException ex) { | ||
Timber.e(ex); | ||
return 0L; | ||
} | ||
} | ||
} | ||
|
||
public static boolean isLastTimeRunLongerThan(long hours) { | ||
long lastTimeMillis = getLastTimeRun(); | ||
|
||
return ((System.currentTimeMillis() - lastTimeMillis)/TimeUnit.HOURS.toMillis(1)) > hours; | ||
} | ||
|
||
|
||
@NonNull | ||
protected abstract String getClientTableName(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.