From 9f84a7d1f5b7fee6fed12c2aef2acba4b5541ce1 Mon Sep 17 00:00:00 2001 From: jacques Date: Fri, 7 May 2021 22:10:26 +0200 Subject: [PATCH] Move scheduleAlarm in AlarmReceiver and call it from onPause. Add the way to launch alarmManager at bootime --- .../net/foucry/pilldroid/AlarmReceiver.java | 113 ++++++++++++++---- .../foucry/pilldroid/DrugListActivity.java | 42 ++++--- 2 files changed, 118 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/AlarmReceiver.java b/app/src/main/java/net/foucry/pilldroid/AlarmReceiver.java index ba37592..04878ec 100644 --- a/app/src/main/java/net/foucry/pilldroid/AlarmReceiver.java +++ b/app/src/main/java/net/foucry/pilldroid/AlarmReceiver.java @@ -1,18 +1,20 @@ package net.foucry.pilldroid; +import android.app.AlarmManager; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; - import android.util.Log; import android.widget.Toast; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; +import java.time.LocalTime; +import java.util.Date; import java.util.List; @@ -20,7 +22,7 @@ public class AlarmReceiver extends BroadcastReceiver { private static final String TAG = DrugDetailFragment.class.getName(); - NotificationManager nm; + NotificationManager notificationManager; @Override public void onReceive(Context context, Intent intent) @@ -28,7 +30,23 @@ public class AlarmReceiver extends BroadcastReceiver { // Show the toast like in above screen shot Log.d(TAG, "onReceive"); - Toast.makeText(context, "New stock calculted", Toast.LENGTH_LONG).show(); + // If BOOT_COMPLETED is received we launch an alarm in 10 second in order to + // start the alarmschedule process. + final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED"; + if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){ + + /*Intent myIntent = new Intent(context, AlarmReceiver.class); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0); + + AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(System.currentTimeMillis()); + calendar.add(Calendar.SECOND, 10); + alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);*/ + scheduleAlarm(context); + } + + Toast.makeText(context, "New stock calculted", Toast.LENGTH_LONG).show(); createNotificationChannel(context); DBHelper dbHelper = new DBHelper(context); dbHelper.getAllDrugs(); @@ -48,7 +66,7 @@ public class AlarmReceiver extends BroadcastReceiver { if (firstDrug != null) { if (firstDrug.getTake() != 0) { if(firstDrug.getStock() < firstDrug.getAlertThreshold()) { - nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0); /* Notification notif = new Notification(R.drawable.ic_pill_alarm, "Crazy About Android...", System.currentTimeMillis()); @@ -83,25 +101,74 @@ public class AlarmReceiver extends BroadcastReceiver { String description = context.getString(R.string.channel_description); int importance = NotificationManager.IMPORTANCE_DEFAULT; String CHANNEL_ID = "PillDroid"; - 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(); + 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(); + } } } + public static void scheduleAlarm(Context context) { + android.icu.util.Calendar calendar = android.icu.util.Calendar.getInstance(); + Date today; + Date tomorrow; -} + if (BuildConfig.DEBUG) { + calendar.add(android.icu.util.Calendar.HOUR, 1); + today = calendar.getTime(); + calendar.add(android.icu.util.Calendar.HOUR, 1); + } else { + calendar.set(android.icu.util.Calendar.HOUR_OF_DAY, 12); + today = calendar.getTime(); + calendar.add(android.icu.util.Calendar.DAY_OF_YEAR, 1); + } + tomorrow = calendar.getTime(); + 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()); + } + + PendingIntent alarmIntent; + + Intent intent = new Intent(context, AlarmReceiver.class); + alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); + + 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"); + } + + /*alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), + AlarmManager.INTERVAL_DAY, alarmIntent);*/ + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), + AlarmManager.INTERVAL_FIFTEEN_MINUTES, alarmIntent); + + Log.d(TAG, "Alarm scheduled for " + UtilDate.convertDate(calendar.getTimeInMillis())); + + Toast.makeText(context, "Alarm scheduled for " + UtilDate.convertDate(calendar.getTimeInMillis()), Toast.LENGTH_SHORT).show(); + } +} \ No newline at end of file diff --git a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java index 5fd23ab..4c3adb4 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java @@ -1,13 +1,10 @@ package net.foucry.pilldroid; -import android.app.AlarmManager; import android.app.NotificationManager; -import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; -import android.icu.util.Calendar; import android.os.Build; import android.os.Bundle; import android.text.Editable; @@ -35,7 +32,6 @@ import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentResult; import java.text.SimpleDateFormat; -import java.time.LocalTime; import java.util.Date; import java.util.List; import java.util.Locale; @@ -60,6 +56,7 @@ public class DrugListActivity extends AppCompatActivity { // TODO: Change DEMO/DBDEMO form static to non-static. In order to create fake data at only at launchtime final Boolean DEMO = false; final Boolean DBDEMO = false; + public final int CUSTOMIZED_REQUEST_CODE = 0x0000ffff; /** @@ -215,7 +212,7 @@ public class DrugListActivity extends AppCompatActivity { public void onPause() { super.onPause(); - scheduleAlarm(); + AlarmReceiver.scheduleAlarm(this); } public void onResume() { @@ -388,14 +385,21 @@ public class DrugListActivity extends AppCompatActivity { /** * scheduleAlarm() */ - public void scheduleAlarm() { - + /*public void scheduleAlarm() { Calendar calendar = Calendar.getInstance(); - calendar.set(Calendar.HOUR_OF_DAY,12); - Date today = calendar.getTime(); - calendar.add(Calendar.DAY_OF_YEAR, 1); - Date tomorrow = calendar.getTime(); + Date today; + Date tomorrow; + if (BuildConfig.DEBUG) { + calendar.add(Calendar.HOUR, 1); + today = calendar.getTime(); + calendar.add(Calendar.HOUR, 1); + } else { + calendar.set(Calendar.HOUR_OF_DAY, 12); + today = calendar.getTime(); + calendar.add(Calendar.DAY_OF_YEAR, 1); + } + tomorrow = calendar.getTime(); LocalTime todayNow = LocalTime.now(); if (todayNow.isBefore(LocalTime.NOON)) { @@ -414,14 +418,24 @@ public class DrugListActivity extends AppCompatActivity { AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); - alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), - AlarmManager.INTERVAL_DAY, alarmIntent); + boolean alarmUp = (PendingIntent.getBroadcast(this, 0, intent, + PendingIntent.FLAG_NO_CREATE) != null); + if (alarmUp) { + Log.d(TAG, "Alarm already active"); + } + + *//*alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), + AlarmManager.INTERVAL_DAY, alarmIntent);*//* + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), + AlarmManager.INTERVAL_FIFTEEN_MINUTES, alarmIntent); Log.d(TAG, "Alarm scheduled for " + UtilDate.convertDate(calendar.getTimeInMillis())); Toast.makeText(getApplicationContext(), "Alarm scheduled for " + UtilDate.convertDate(calendar.getTimeInMillis()), Toast.LENGTH_SHORT).show(); - } + }*/ + + /** * setupRecyclerView (list of drugs * @param recyclerView RecyclerView