2021-01-22 21:17:23 +01:00
|
|
|
package net.foucry.pilldroid;
|
|
|
|
|
2021-05-07 22:10:26 +02:00
|
|
|
import android.app.AlarmManager;
|
2021-03-01 18:46:15 +01:00
|
|
|
import android.app.NotificationChannel;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
2021-01-22 21:17:23 +01:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2021-05-08 22:31:37 +02:00
|
|
|
import android.icu.util.Calendar;
|
2021-03-01 18:46:15 +01:00
|
|
|
import android.util.Log;
|
2021-01-22 21:17:23 +01:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2021-03-01 18:46:15 +01:00
|
|
|
import androidx.core.app.NotificationCompat;
|
|
|
|
import androidx.core.app.NotificationManagerCompat;
|
|
|
|
|
2021-05-07 22:10:26 +02:00
|
|
|
import java.time.LocalTime;
|
|
|
|
import java.util.Date;
|
2021-03-01 18:46:15 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
2021-01-22 21:17:23 +01:00
|
|
|
public class AlarmReceiver extends BroadcastReceiver {
|
|
|
|
|
2021-05-08 22:31:37 +02:00
|
|
|
private static final String TAG = AlarmManager.class.getName();
|
2021-03-01 18:46:15 +01:00
|
|
|
|
2021-05-07 22:10:26 +02:00
|
|
|
NotificationManager notificationManager;
|
2021-03-01 18:46:15 +01:00
|
|
|
|
2021-01-22 21:17:23 +01:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent)
|
|
|
|
{
|
|
|
|
// Show the toast like in above screen shot
|
2021-03-01 18:46:15 +01:00
|
|
|
Log.d(TAG, "onReceive");
|
|
|
|
|
2021-05-07 22:10:26 +02:00
|
|
|
// If BOOT_COMPLETED is received we launch an alarm in 10 second in order to
|
|
|
|
// start the alarmschedule process.
|
|
|
|
|
2021-05-08 22:31:37 +02:00
|
|
|
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
|
|
|
|
Log.d(TAG, "StartUpBootReceiver BOOT_COMPLETED");
|
2021-05-07 22:10:26 +02:00
|
|
|
scheduleAlarm(context);
|
|
|
|
}
|
|
|
|
|
2021-05-11 17:01:40 +02:00
|
|
|
Toast.makeText(context, "New stock calculated", Toast.LENGTH_LONG).show();
|
2021-03-01 18:46:15 +01:00
|
|
|
createNotificationChannel(context);
|
|
|
|
DBHelper dbHelper = new DBHelper(context);
|
|
|
|
dbHelper.getAllDrugs();
|
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
List<Drug> drugs = dbHelper.getAllDrugs();
|
2021-03-01 18:46:15 +01:00
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
Drug firstDrug = null;
|
2021-03-01 18:46:15 +01:00
|
|
|
|
|
|
|
try {
|
2021-04-12 21:11:50 +02:00
|
|
|
firstDrug = drugs.get(0);
|
2021-03-01 18:46:15 +01:00
|
|
|
}
|
|
|
|
catch (Exception e){
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:11:50 +02:00
|
|
|
if (firstDrug != null) {
|
|
|
|
if (firstDrug.getTake() != 0) {
|
|
|
|
if(firstDrug.getStock() < firstDrug.getAlertThreshold()) {
|
2021-05-07 22:10:26 +02:00
|
|
|
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
2021-03-01 18:46:15 +01:00
|
|
|
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
|
|
|
|
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "PillDroid")
|
|
|
|
.setSmallIcon(R.drawable.ic_pill_alarm)
|
|
|
|
.setContentTitle(context.getString(R.string.app_name))
|
|
|
|
.setContentText(context.getString(R.string.notification_text))
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
|
|
|
.setContentIntent(pendingIntent)
|
|
|
|
.setAutoCancel(true);
|
|
|
|
|
|
|
|
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
|
|
|
|
int notificationId = 666;
|
|
|
|
notificationManager.notify(notificationId, builder.build());
|
|
|
|
} else
|
|
|
|
{
|
2021-04-12 21:11:50 +02:00
|
|
|
double dummy = (firstDrug.getStock() - firstDrug.getAlertThreshold());
|
2021-03-01 18:46:15 +01:00
|
|
|
Log.d(TAG, "no notification scheduled " + dummy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 21:17:23 +01:00
|
|
|
}
|
2021-03-01 18:46:15 +01:00
|
|
|
|
|
|
|
private void createNotificationChannel(Context context) {
|
|
|
|
|
|
|
|
Log.d(TAG, "start create notification channel");
|
|
|
|
CharSequence name = context.getString(R.string.channel_name);
|
|
|
|
String description = context.getString(R.string.channel_description);
|
|
|
|
int importance = NotificationManager.IMPORTANCE_DEFAULT;
|
|
|
|
String CHANNEL_ID = "PillDroid";
|
2021-05-07 22:10:26 +02:00
|
|
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
|
|
|
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
|
|
|
|
channel.setDescription(description);
|
|
|
|
channel.enableLights(true);
|
|
|
|
channel.setLightColor(R.color.led);
|
|
|
|
channel.enableVibration(true);
|
|
|
|
channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
|
|
|
|
// Register the channel with the system; you can't change the importance
|
|
|
|
// or other notification behaviors after this
|
|
|
|
NotificationManager notificationManager = context.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();
|
|
|
|
}
|
2021-03-01 18:46:15 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-07 22:10:26 +02:00
|
|
|
public static void scheduleAlarm(Context context) {
|
2021-05-11 17:01:40 +02:00
|
|
|
Calendar calendar = Calendar.getInstance();
|
2021-05-07 22:10:26 +02:00
|
|
|
Date today;
|
|
|
|
Date tomorrow;
|
|
|
|
|
2021-05-13 17:29:05 +02:00
|
|
|
/*if (BuildConfig.DEBUG) {
|
2021-05-11 17:01:40 +02:00
|
|
|
calendar.add(Calendar.HOUR_OF_DAY, 12);
|
2021-05-07 22:10:26 +02:00
|
|
|
today = calendar.getTime();
|
2021-05-11 17:01:40 +02:00
|
|
|
calendar.add(Calendar.DAY_OF_YEAR, 1);
|
2021-05-08 22:31:37 +02:00
|
|
|
tomorrow = calendar.getTime();
|
2021-05-13 17:29:05 +02:00
|
|
|
} else {*/
|
2021-05-11 17:01:40 +02:00
|
|
|
calendar.set(Calendar.HOUR_OF_DAY, 12);
|
2021-05-07 22:10:26 +02:00
|
|
|
today = calendar.getTime();
|
2021-05-11 17:01:40 +02:00
|
|
|
calendar.add(Calendar.DAY_OF_YEAR, 1);
|
2021-05-08 22:31:37 +02:00
|
|
|
tomorrow = calendar.getTime();
|
2021-05-13 17:29:05 +02:00
|
|
|
// }
|
2021-05-08 22:31:37 +02:00
|
|
|
|
2021-05-07 22:10:26 +02:00
|
|
|
LocalTime todayNow = LocalTime.now();
|
|
|
|
|
|
|
|
if (todayNow.isBefore(LocalTime.NOON)) {
|
|
|
|
// schedule date = today
|
|
|
|
//calendar.setTimeInMillis(dateAtNoon(today).getTime());
|
|
|
|
calendar.setTimeInMillis(today.getTime());
|
|
|
|
} else {
|
|
|
|
// schedule date = tomorrow
|
|
|
|
calendar.setTimeInMillis(tomorrow.getTime());
|
|
|
|
}
|
|
|
|
|
2021-05-11 17:01:40 +02:00
|
|
|
/* if (BuildConfig.DEBUG)
|
|
|
|
{
|
|
|
|
calendar.setTimeInMillis(today.getTime());
|
|
|
|
}*/
|
|
|
|
|
2021-05-07 22:10:26 +02:00
|
|
|
PendingIntent alarmIntent;
|
|
|
|
|
|
|
|
Intent intent = new Intent(context, AlarmReceiver.class);
|
|
|
|
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
|
2021-03-01 18:46:15 +01:00
|
|
|
|
2021-05-07 22:10:26 +02:00
|
|
|
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
|
|
|
|
boolean alarmUp = (PendingIntent.getBroadcast(context, 0, intent,
|
|
|
|
PendingIntent.FLAG_NO_CREATE) != null);
|
|
|
|
if (alarmUp) {
|
|
|
|
Log.d(TAG, "Alarm already active");
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:29:05 +02:00
|
|
|
|
|
|
|
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,(calendar.getTimeInMillis()),
|
|
|
|
AlarmManager.INTERVAL_DAY, alarmIntent);
|
2021-05-07 22:10:26 +02:00
|
|
|
|
|
|
|
Log.d(TAG, "Alarm scheduled for " + UtilDate.convertDate(calendar.getTimeInMillis()));
|
|
|
|
|
|
|
|
Toast.makeText(context, "Alarm scheduled for " + UtilDate.convertDate(calendar.getTimeInMillis()), Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|