medicaments = dbHelper.getAllDrugs();
+
+ 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);
+ }
+
+
@Override
public boolean onStopJob(JobParameters params) {
- mJobHandler.removeMessages(1);
- return false;
+ Log.d(TAG, "Job cancelled before completion");
+ jobCancelled = true;
+ return true;
}
+ /**
+ * Schedule Notification for the delay
+ * @param Context context
+ * @param long delay - date for the notification in millisecond
+ */
+ 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.pharmacy))
+ .setPriority(NotificationCompat.PRIORITY_DEFAULT)
+ .setAutoCancel(true);
-}
+ NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
+ int notificationId = 666;
+ notificationManager.notify(notificationId, builder.build());
+ }
+
+ /**
+ * createNotificationChannelid for android API >= 28
+ */
+ private void createNotificationChannel() {
+
+ 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();
+ }
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/net/foucry/pilldroid/UtilDate.java b/app/src/main/java/net/foucry/pilldroid/UtilDate.java
index 145ff2b..28dd297 100644
--- a/app/src/main/java/net/foucry/pilldroid/UtilDate.java
+++ b/app/src/main/java/net/foucry/pilldroid/UtilDate.java
@@ -15,11 +15,11 @@ import java.util.Locale;
class UtilDate {
private static final String TAG = UtilDate.class.getName();
+
/**
- *
* @param aDate anydate
* @return date the same date as input but at noon (12:00:00)
- *
+ *
* set date time at Noon
*/
static Date dateAtNoon(Date aDate) {
@@ -34,12 +34,12 @@ class UtilDate {
return calendar.getTime();
}
+
/**
- *
* @param days number of days to remove to the ate
* @param date date before day removing
* @return date
- *
+ *
* Substract days to date and return a new date
*/
static Date removeDaysToDate(int days, Date date) {
@@ -51,10 +51,9 @@ class UtilDate {
}
/**
- *
* @param date Date to be converted
* @return String of the converted date
- *
+ *
* Convert a date to a String using a SimpleDateFormat
*/
static String date2String(Date date, DateFormat dateFormat) {
@@ -68,23 +67,21 @@ class UtilDate {
}
/**
- *
- * @param dateString string representing a Date to be conveted
- * @return date Date after convertion
- *
+ * @param dateString string representing a Date to be converted
+ * @return date Date after conversion
+ *
* Convert String date into Date
*/
static Date string2Date(String dateString) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.FRANCE);
ParsePosition pos = new ParsePosition(0);
- return dateFormat.parse(dateString,pos);
+ return dateFormat.parse(dateString, pos);
}
/**
- *
* @param date start date
* @return int numbers of days between date and today
- *
+ *
* Number of days between date (older than today) and today
*/
static int nbOfDaysBetweenDateAndToday(Date date) {
diff --git a/app/src/main/res/values-fr-rFR/strings.xml b/app/src/main/res/values-fr-rFR/strings.xml
index 7cff479..1819058 100644
--- a/app/src/main/res/values-fr-rFR/strings.xml
+++ b/app/src/main/res/values-fr-rFR/strings.xml
@@ -22,9 +22,10 @@
Vue de détail
Scanner un code barre de médicament
Save
- Toggle Flash
- Enter cip 13 here..
+ Allumez/Eteindre le Flash
+ Entrez le code CIP 13 ici…
Enter CIP 13
Saisissez le code CIP13 avec le clavier
Vous devez passer à la pharmacie
-
\ No newline at end of file
+
+