2016-10-18 16:29:07 +02:00
|
|
|
package net.foucry.pilldroid;
|
|
|
|
|
2020-09-11 17:21:01 +02:00
|
|
|
import android.app.NotificationChannel;
|
|
|
|
import android.app.NotificationManager;
|
2016-10-18 16:29:07 +02:00
|
|
|
import android.app.job.JobParameters;
|
|
|
|
import android.app.job.JobService;
|
2020-09-11 17:21:01 +02:00
|
|
|
import android.icu.util.Calendar;
|
2016-10-18 16:29:07 +02:00
|
|
|
import android.util.Log;
|
|
|
|
|
2020-09-11 17:21:01 +02:00
|
|
|
import androidx.core.app.NotificationCompat;
|
|
|
|
import androidx.core.app.NotificationManagerCompat;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
|
2016-10-18 16:29:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by jacques on 17/09/16.
|
|
|
|
*/
|
2020-09-11 17:21:01 +02:00
|
|
|
|
2016-10-18 16:29:07 +02:00
|
|
|
public class PillDroidJobService extends JobService {
|
2020-09-11 17:21:01 +02:00
|
|
|
private static final String TAG = JobService.class.getName();
|
|
|
|
private boolean jobCancelled = false;
|
2020-09-12 19:05:42 +02:00
|
|
|
private String CHANNEL_ID = "PillDroid";
|
2020-09-11 17:21:01 +02:00
|
|
|
private DBHelper dbHelper = new DBHelper(this);
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onStartJob(JobParameters params) {
|
|
|
|
Log.d(TAG, "Job started");
|
|
|
|
createNotificationChannel();
|
|
|
|
doBackgroundWork(params);
|
2020-08-15 22:04:34 +02:00
|
|
|
|
2020-09-11 17:21:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grab sorted list of medicaments
|
|
|
|
* test dateAlert of the first of the list
|
|
|
|
* if dateAlert < now
|
|
|
|
* schedule notification
|
2020-09-11 21:46:03 +02:00
|
|
|
* @param params JobParameters
|
2020-09-11 17:21:01 +02:00
|
|
|
*/
|
|
|
|
private void doBackgroundWork(final JobParameters params) {
|
2020-08-15 22:04:34 +02:00
|
|
|
|
2020-09-11 17:21:01 +02:00
|
|
|
if (jobCancelled) {
|
|
|
|
return;
|
2020-08-15 22:04:34 +02:00
|
|
|
}
|
2020-09-11 17:21:01 +02:00
|
|
|
List<Medicament> medicaments = dbHelper.getAllDrugs();
|
2020-08-15 22:04:34 +02:00
|
|
|
|
2020-09-11 17:21:01 +02:00
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
Date now = calendar.getTime();
|
|
|
|
|
|
|
|
long dateSchedule;
|
|
|
|
|
|
|
|
Medicament firstMedicament = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
firstMedicament = medicaments.get(0);
|
|
|
|
}
|
|
|
|
catch (Exception ignored){}
|
|
|
|
|
|
|
|
if (firstMedicament != null) {
|
|
|
|
Date dateAlert = UtilDate.removeDaysToDate(firstMedicament.getAlertThreshold(), firstMedicament.getDateEndOfStock());
|
|
|
|
|
|
|
|
if (dateAlert.getTime() < now.getTime()) {
|
|
|
|
dateSchedule = now.getTime() + 120000; // If dateAlert < now we schedule an alert for now + 120 seconds
|
|
|
|
} else {
|
|
|
|
dateSchedule = dateAlert.getTime(); // If dateAlert > now we use dateAlert as scheduleDate
|
|
|
|
}
|
|
|
|
|
|
|
|
long delay = dateSchedule - now.getTime();
|
|
|
|
scheduleNotification(delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.d(TAG, "Job finished");
|
|
|
|
jobFinished(params, false);
|
2016-10-18 16:29:07 +02:00
|
|
|
}
|
|
|
|
|
2020-09-11 17:21:01 +02:00
|
|
|
|
2016-10-18 16:29:07 +02:00
|
|
|
@Override
|
|
|
|
public boolean onStopJob(JobParameters params) {
|
2020-09-11 17:21:01 +02:00
|
|
|
Log.d(TAG, "Job cancelled before completion");
|
|
|
|
jobCancelled = true;
|
|
|
|
return true;
|
2016-10-18 16:29:07 +02:00
|
|
|
}
|
|
|
|
|
2020-09-11 17:21:01 +02:00
|
|
|
/**
|
|
|
|
* Schedule Notification for the delay
|
2020-09-11 21:46:03 +02:00
|
|
|
* @param delay long - date for the notification in millisecond
|
2020-09-11 17:21:01 +02:00
|
|
|
*/
|
|
|
|
private void scheduleNotification(long delay) {
|
|
|
|
Log.d(TAG, "scheduleNotification delay == " + delay);
|
|
|
|
createNotificationChannel();
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
|
|
|
|
.setSmallIcon(R.drawable.ic_pill)
|
|
|
|
.setContentTitle(getString(R.string.app_name))
|
|
|
|
.setContentText(getString(R.string.notification_text))
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
|
|
|
.setAutoCancel(true);
|
|
|
|
|
|
|
|
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
|
|
|
|
int notificationId = 666;
|
|
|
|
notificationManager.notify(notificationId, builder.build());
|
|
|
|
|
|
|
|
}
|
2020-08-21 10:34:12 +02:00
|
|
|
|
2020-09-11 17:21:01 +02:00
|
|
|
/**
|
|
|
|
* createNotificationChannelid for android API >= 28
|
|
|
|
*/
|
|
|
|
private void createNotificationChannel() {
|
2020-08-21 10:34:12 +02:00
|
|
|
|
2020-09-11 17:21:01 +02:00
|
|
|
Log.d(TAG, "start create notification channel");
|
|
|
|
CharSequence name = getString(R.string.channel_name);
|
|
|
|
String description = getString(R.string.channel_description);
|
|
|
|
int importance = NotificationManager.IMPORTANCE_DEFAULT;
|
|
|
|
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
|
|
|
|
channel.setDescription(description);
|
|
|
|
// Register the channel with the system; you can't change the importance
|
|
|
|
// or other notification behaviors after this
|
|
|
|
NotificationManager notificationManager = getSystemService(NotificationManager.class);
|
|
|
|
try {
|
|
|
|
notificationManager.createNotificationChannel(channel);
|
|
|
|
} catch (Exception e) {
|
|
|
|
// This will catch any exception, because they are all descended from Exception
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
//At the level Exception Class handle the error in Exception Table
|
|
|
|
// Exception Create That Error Object and throw it
|
|
|
|
//E.g: FileNotFoundException ,etc
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|