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;
|
2020-12-26 21:52:33 +01:00
|
|
|
import android.app.PendingIntent;
|
2016-10-18 16:29:07 +02:00
|
|
|
import android.app.job.JobParameters;
|
|
|
|
import android.app.job.JobService;
|
2020-12-26 21:52:33 +01:00
|
|
|
import android.content.Intent;
|
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;
|
2020-12-26 21:52:33 +01:00
|
|
|
import androidx.core.app.NotificationManagerCompat;
|
2020-09-11 17:21:01 +02:00
|
|
|
|
|
|
|
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-12-23 09:48:08 +01:00
|
|
|
private final DBHelper dbHelper = new DBHelper(this);
|
2020-09-11 17:21:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onStartJob(JobParameters params) {
|
|
|
|
Log.d(TAG, "Job started");
|
2020-12-26 21:52:33 +01:00
|
|
|
createNotificationChannel();
|
2020-09-11 17:21:01 +02:00
|
|
|
doBackgroundWork(params);
|
2020-08-15 22:04:34 +02:00
|
|
|
|
2020-12-07 11:29:07 +01:00
|
|
|
return false;
|
2020-09-11 17:21:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-27 14:17:38 +02:00
|
|
|
Log.d(TAG,"background job");
|
2020-09-11 17:21:01 +02:00
|
|
|
if (jobCancelled) {
|
|
|
|
return;
|
2020-08-15 22:04:34 +02:00
|
|
|
}
|
2021-04-12 21:11:50 +02:00
|
|
|
List<Drug> drugs = dbHelper.getAllDrugs();
|
2020-08-15 22:04:34 +02:00
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
Drug firstDrug = null;
|
2020-09-11 17:21:01 +02:00
|
|
|
|
|
|
|
try {
|
2021-04-12 21:11:50 +02:00
|
|
|
firstDrug = drugs.get(0);
|
2020-09-11 17:21:01 +02:00
|
|
|
}
|
2020-09-19 18:01:42 +02:00
|
|
|
catch (Exception e){
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2020-09-11 17:21:01 +02:00
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
if (firstDrug != null) {
|
|
|
|
if (firstDrug.getTake() != 0) {
|
|
|
|
if(firstDrug.getStock() < firstDrug.getAlertThreshold()) {
|
2021-01-09 11:19:18 +01:00
|
|
|
scheduleNotification();
|
|
|
|
} else
|
|
|
|
{
|
2021-04-12 21:11:50 +02:00
|
|
|
double dummy = (firstDrug.getStock() - firstDrug.getAlertThreshold());
|
2021-01-09 11:19:18 +01:00
|
|
|
Log.d(TAG, "no notification scheduled " + dummy);
|
|
|
|
}
|
2020-09-22 11:06:01 +02:00
|
|
|
}
|
2020-09-11 17:21:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Log.d(TAG, "Job finished");
|
2021-01-09 11:19:18 +01:00
|
|
|
jobFinished(params, true);
|
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;
|
2020-12-07 11:29:07 +01:00
|
|
|
return false;
|
2016-10-18 16:29:07 +02:00
|
|
|
}
|
|
|
|
|
2020-09-11 17:21:01 +02:00
|
|
|
/**
|
|
|
|
* Schedule Notification for the delay
|
|
|
|
*/
|
2021-03-01 18:52:37 +01:00
|
|
|
void scheduleNotification() {
|
2020-09-27 14:17:38 +02:00
|
|
|
Log.d(TAG, "schedule notification");
|
2020-12-26 21:52:33 +01:00
|
|
|
createNotificationChannel();
|
2021-04-12 21:11:50 +02:00
|
|
|
Intent intent = new Intent(this, DrugListActivity.class);
|
2020-09-19 18:01:42 +02:00
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,0);
|
2020-12-26 21:52:33 +01:00
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "PillDroid")
|
2020-12-23 09:48:08 +01:00
|
|
|
.setSmallIcon(R.drawable.ic_pill_alarm)
|
2020-09-11 17:21:01 +02:00
|
|
|
.setContentTitle(getString(R.string.app_name))
|
|
|
|
.setContentText(getString(R.string.notification_text))
|
2020-12-28 19:35:20 +01:00
|
|
|
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
2020-09-19 18:01:42 +02:00
|
|
|
.setContentIntent(pendingIntent)
|
2020-09-11 17:21:01 +02:00
|
|
|
.setAutoCancel(true);
|
|
|
|
|
|
|
|
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
|
|
|
|
int notificationId = 666;
|
2020-12-26 21:52:33 +01:00
|
|
|
notificationManager.notify(notificationId, builder.build());
|
2020-09-11 17:21:01 +02:00
|
|
|
}
|
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;
|
2020-12-07 11:29:07 +01:00
|
|
|
String CHANNEL_ID = "PillDroid";
|
2021-05-07 22:12:55 +02:00
|
|
|
NotificationChannel channel;
|
|
|
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
|
|
|
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();
|
|
|
|
}
|
2020-09-11 17:21:01 +02:00
|
|
|
}
|
|
|
|
}
|
2022-01-31 21:30:19 +01:00
|
|
|
}
|