mirror of
https://github.com/jfoucry/Pilldroid.git
synced 2024-11-22 04:29:22 +01:00
Move scheduleAlarm in AlarmReceiver and call it from onPause.
Add the way to launch alarmManager at bootime
This commit is contained in:
parent
c4a2a968e0
commit
9f84a7d1f5
2 changed files with 118 additions and 37 deletions
|
@ -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,6 +30,22 @@ public class AlarmReceiver extends BroadcastReceiver {
|
|||
// Show the toast like in above screen shot
|
||||
Log.d(TAG, "onReceive");
|
||||
|
||||
// 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);
|
||||
|
@ -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,6 +101,7 @@ public class AlarmReceiver extends BroadcastReceiver {
|
|||
String description = context.getString(R.string.channel_description);
|
||||
int importance = NotificationManager.IMPORTANCE_DEFAULT;
|
||||
String CHANNEL_ID = "PillDroid";
|
||||
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);
|
||||
|
@ -103,5 +122,53 @@ public class AlarmReceiver extends BroadcastReceiver {
|
|||
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();
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue