From b2287c99d4e7bea066c147ad95f9d02cf59e4dda Mon Sep 17 00:00:00 2001 From: jacques Date: Sun, 19 Jul 2020 20:01:52 +0200 Subject: [PATCH 01/19] Minor changes in debug --- .../main/java/net/foucry/pilldroid/MedicamentListActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java index a5e53fb..b1e9c68 100644 --- a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java @@ -408,7 +408,7 @@ public class MedicamentListActivity extends AppCompatActivity { } private void scheduleNotification(Notification notification, long delay) { - Log.i(TAG, "scheduleNotification delay == " + 30000); + Log.d(TAG, "scheduleNotification delay == " + 30000); Intent notificationIntent = new Intent(this, NotificationPublisher.class); notificationIntent.putExtra(NOTIFICATION_ID, 1); From 0afb8bf34eab1a84394974a2ebafc898e0b3efdf Mon Sep 17 00:00:00 2001 From: jacques Date: Sun, 19 Jul 2020 20:02:53 +0200 Subject: [PATCH 02/19] New NotificationPublisher for android >= 28; Optimize import. --- .../pilldroid/NotificationPublisher.java | 70 +++++++++++++++---- 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java index 55a832b..a42974a 100644 --- a/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java +++ b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java @@ -1,35 +1,75 @@ package net.foucry.pilldroid; -import android.app.Notification; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.os.VibrationEffect; -import android.os.Vibrator; import android.util.Log; +import androidx.core.app.NotificationCompat; + /** * Created by jfoucry on 6/23/16. + * Publish notification */ + + public class NotificationPublisher extends BroadcastReceiver { private static String TAG = NotificationPublisher.class.getName(); public static String NOTIFICATION_ID = "notification-id"; public static String NOTIFICATION = "notification"; + /** + * onReceive notification + * @param Context context + * @param Intent intent + */ public void onReceive(Context context, Intent intent) { - Log.i(TAG, "Receive notification"); + Log.d(TAG, "Receive notification"); - NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - - Notification notification = intent.getParcelableExtra(NOTIFICATION); - int id = intent.getIntExtra(NOTIFICATION_ID,0); - if (notificationManager != null) { - notificationManager.notify(id, notification); - } - Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); - assert vibrator != null; - vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)); + String channel_id = createNotificationChannel(context); + NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channel_id) + .setSmallIcon(R.drawable.ic_pill) + .setContentTitle(context.getString(R.string.app_name)) + .setContentText(context.getString(R.string.pharmacy)) + .setStyle(new NotificationCompat.BigTextStyle() + .bigText(context.getString(R.string.pharmacy))) + .setPriority(NotificationCompat.PRIORITY_DEFAULT); } -} + + /** + * createNotificationChannelid for android API >= 28 + * @param Context context + * @return String channel_id + */ + public static String createNotificationChannel(Context context) { + + // The id of the channel. + String channelId = "Channel_id"; + + // The user-visible name of the channel. + CharSequence channelName = context.getString(R.string.app_name); + // The user-visible description of the channel. + String channelDescription = "Pilldroid Alert"; + int channelImportance = NotificationManager.IMPORTANCE_DEFAULT; + // int channelLockscreenVisibility = Notification.; + + // Initializes NotificationChannel. + NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, + channelImportance); + notificationChannel.setDescription(channelDescription); + notificationChannel.enableVibration(true); + // notificationChannel.setLockscreenVisibility(channelLockscreenVisibility); + + // Adds NotificationChannel to system. Attempting to create an existing notification + // channel with its original values performs no operation, so it's safe to perform the + // below sequence. + NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + assert notificationManager != null; + notificationManager.createNotificationChannel(notificationChannel); + + return channelId; + } +} \ No newline at end of file From f3145bd74c063a8734b68f6fc3bf6bb67045337a Mon Sep 17 00:00:00 2001 From: jacques Date: Sun, 19 Jul 2020 20:04:12 +0200 Subject: [PATCH 03/19] Change in debug code; Optimize import. --- .../main/java/net/foucry/pilldroid/PillDroidJobService.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java b/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java index c913ac6..f85e486 100644 --- a/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java +++ b/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java @@ -2,10 +2,9 @@ package net.foucry.pilldroid; import android.app.job.JobParameters; import android.app.job.JobService; -import android.os.Message; import android.os.Handler; +import android.os.Message; import android.util.Log; -import android.widget.Toast; /** @@ -27,7 +26,7 @@ public class PillDroidJobService extends JobService { @Override public boolean onStartJob (JobParameters params) { - Log.i(TAG, "on Start Job: " + params.getJobId()); + Log.d(TAG, "on Start Job: " + params.getJobId()); mJobHandler.sendMessage(Message.obtain(mJobHandler, 1,params)); return false; } From 4b80c35e89f40a52609d4cd0a2c28f0ff5b27724 Mon Sep 17 00:00:00 2001 From: jacques Date: Sun, 19 Jul 2020 20:08:55 +0200 Subject: [PATCH 04/19] Add and fix strings and translations --- app/src/main/res/values-fr-rFR/strings.xml | 7 ++++--- app/src/main/res/values/strings.xml | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/src/main/res/values-fr-rFR/strings.xml b/app/src/main/res/values-fr-rFR/strings.xml index 756ec48..a85baad 100644 --- a/app/src/main/res/values-fr-rFR/strings.xml +++ b/app/src/main/res/values-fr-rFR/strings.xml @@ -17,12 +17,13 @@ Stock courant Éteindre le flash Allumer le flash - Vous devez passer à la pharmacie Utilisez le clavier 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 + Vous devez passer à la pharmacie + Entrez le code CIP 13 avec le clavier \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index abb7fea..99d908b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,7 +7,6 @@ Drug found in the database About Help - You have to go to the pharmacy Turn Flash On Turn Flash Off Name @@ -22,7 +21,8 @@ Scan a drug barcode Save Input Cip13 with the keyboard - Toggle Flash + Toggle flash Enter cip 13 here.. Enter CIP 13 + You must go to the pharmacy From 2e6a52283e4ddf716ba5c7fe7c73cee74952cca1 Mon Sep 17 00:00:00 2001 From: jacques Date: Mon, 20 Jul 2020 18:08:04 +0200 Subject: [PATCH 05/19] Fix bug forget to save prise value. --- app/src/main/java/net/foucry/pilldroid/DBHelper.java | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/net/foucry/pilldroid/DBHelper.java b/app/src/main/java/net/foucry/pilldroid/DBHelper.java index e6d7d94..24ab493 100644 --- a/app/src/main/java/net/foucry/pilldroid/DBHelper.java +++ b/app/src/main/java/net/foucry/pilldroid/DBHelper.java @@ -276,6 +276,7 @@ class DBHelper extends SQLiteOpenHelper { values.put(KEY_ADMIN, medicament.getMode_administration()); values.put(KEY_PRES, medicament.getPresentation()); values.put(KEY_STOCK, medicament.getStock()); + values.put(KEY_PRISE, medicament.getPrise()); Log.d(TAG, "values are " +values.toString()); // Update row From 613a38269b356987166969064d46271325a402aa Mon Sep 17 00:00:00 2001 From: jacques Date: Mon, 20 Jul 2020 18:08:53 +0200 Subject: [PATCH 06/19] Fix bug recalcultate user medicament list --- .../foucry/pilldroid/MedicamentListActivity.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java index b1e9c68..5ee8ebe 100644 --- a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java @@ -227,7 +227,6 @@ public class MedicamentListActivity extends AppCompatActivity { public void onPause() { super.onPause(); - newStockCalculation(); } @@ -266,7 +265,7 @@ public class MedicamentListActivity extends AppCompatActivity { } long delay = dateSchedule - now.getTime(); - scheduleNotification(getNotification(getString(R.string.notification_text)), delay); + scheduleNotification(getNotification(getString(R.string.pharmacy)), delay); Log.d(TAG, "Notification scheduled for " + UtilDate.convertDate(dateSchedule)); } @@ -274,6 +273,9 @@ public class MedicamentListActivity extends AppCompatActivity { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { + + Log.d(TAG, "REQUEST_CODE = " + requestCode + " RESULT_CODE = " + resultCode); + if (requestCode != CUSTOMIZED_REQUEST_CODE && requestCode != IntentIntegrator.REQUEST_CODE) { // This is important, otherwise the result will not be passed to the fragment super.onActivityResult(requestCode, resultCode, data); @@ -408,14 +410,14 @@ public class MedicamentListActivity extends AppCompatActivity { } private void scheduleNotification(Notification notification, long delay) { - Log.d(TAG, "scheduleNotification delay == " + 30000); + Log.d(TAG, "scheduleNotification delay == " + delay); Intent notificationIntent = new Intent(this, NotificationPublisher.class); notificationIntent.putExtra(NOTIFICATION_ID, 1); notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); - long futureInMillis = SystemClock.elapsedRealtime() + 30000; + long futureInMillis = SystemClock.elapsedRealtime() + delay; AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); if (alarmManager != null) { alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); @@ -423,7 +425,7 @@ public class MedicamentListActivity extends AppCompatActivity { } private Notification getNotification(String content) { - Log.i(TAG, "getNotification"); + Log.d(TAG, "getNotification"); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle(getAppName()) @@ -524,8 +526,7 @@ public class MedicamentListActivity extends AppCompatActivity { Context context = v.getContext(); Intent intent = new Intent(context, MedicamentDetailActivity.class); intent.putExtra("medicament", medicamentCourant); - int requestCode =1; - startActivityForResult(intent, requestCode); + startActivityForResult(intent, CUSTOMIZED_REQUEST_CODE); } } }); From 2a3dab3398c9f9b427b2700ba7f6a1f9f7060e7a Mon Sep 17 00:00:00 2001 From: jacques Date: Mon, 20 Jul 2020 18:09:10 +0200 Subject: [PATCH 07/19] Add log --- .../main/java/net/foucry/pilldroid/NotificationPublisher.java | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java index a42974a..0da2eca 100644 --- a/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java +++ b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java @@ -46,6 +46,7 @@ public class NotificationPublisher extends BroadcastReceiver { */ public static String createNotificationChannel(Context context) { + Log.d(TAG, "start create notification channel"); // The id of the channel. String channelId = "Channel_id"; From a36febfd5e48b1782095b0bb38baf04472bf7a94 Mon Sep 17 00:00:00 2001 From: jacques Date: Tue, 21 Jul 2020 17:33:41 +0200 Subject: [PATCH 08/19] Refactor notifications. --- .../pilldroid/MedicamentListActivity.java | 63 +++++++------------ .../pilldroid/NotificationPublisher.java | 32 +++++++++- 2 files changed, 51 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java index 5ee8ebe..acd2d16 100644 --- a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java @@ -1,22 +1,12 @@ package net.foucry.pilldroid; import android.app.AlarmManager; -import android.app.Notification; 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.graphics.BitmapFactory; import android.os.Bundle; import android.os.SystemClock; -import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; -import androidx.core.app.NotificationCompat; -import androidx.appcompat.app.AppCompatActivity; -import androidx.recyclerview.widget.RecyclerView; -import androidx.appcompat.widget.Toolbar; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; @@ -29,6 +19,16 @@ import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.recyclerview.widget.RecyclerView; + +import com.google.zxing.client.android.Intents; +import com.google.zxing.integration.android.IntentIntegrator; +import com.google.zxing.integration.android.IntentResult; + import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Collections; @@ -37,10 +37,6 @@ import java.util.Date; import java.util.List; import java.util.Locale; -import com.google.zxing.client.android.Intents; -import com.google.zxing.integration.android.IntentIntegrator; -import com.google.zxing.integration.android.IntentResult; - import static net.foucry.pilldroid.NotificationPublisher.NOTIFICATION_ID; import static net.foucry.pilldroid.UtilDate.date2String; import static net.foucry.pilldroid.Utils.intRandomExclusive; @@ -55,7 +51,6 @@ import static net.foucry.pilldroid.Utils.intRandomExclusive; */ public class MedicamentListActivity extends AppCompatActivity { - private static final String CHANNEL_ID = "MedicamentCHANEL"; /** * Whether or not the activity is in two-pane mode, i.e. running on a tablet * device. @@ -265,7 +260,7 @@ public class MedicamentListActivity extends AppCompatActivity { } long delay = dateSchedule - now.getTime(); - scheduleNotification(getNotification(getString(R.string.pharmacy)), delay); + scheduleNotification(this, delay); Log.d(TAG, "Notification scheduled for " + UtilDate.convertDate(dateSchedule)); } @@ -409,12 +404,20 @@ public class MedicamentListActivity extends AppCompatActivity { recyclerView.setAdapter(mAdapter); } - private void scheduleNotification(Notification notification, long delay) { + /** + * Schedule Notification for the delay + * @param Context context + * @param long delay - date for the notification in milliseconds + */ + private void scheduleNotification(Context context, long delay) { Log.d(TAG, "scheduleNotification delay == " + delay); - Intent notificationIntent = new Intent(this, NotificationPublisher.class); + Intent notificationIntent = new Intent(context, NotificationPublisher.class); notificationIntent.putExtra(NOTIFICATION_ID, 1); - notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification); + notificationIntent.putExtra(NotificationPublisher.KEY_MESSAGE, getString(R.string.pharmacy)); + notificationIntent.putExtra((NotificationPublisher.KEY_TITLE, getString(R.string.app_name)); + notificationIntent.putExtra(NotificationPublisher.KEY_SOUND, true); + PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); long futureInMillis = SystemClock.elapsedRealtime() + delay; @@ -424,28 +427,6 @@ public class MedicamentListActivity extends AppCompatActivity { } } - private Notification getNotification(String content) { - Log.d(TAG, "getNotification"); - - NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) - .setContentTitle(getAppName()) - .setContentText(content) - .setSmallIcon(R.drawable.ic_pill) - .setAutoCancel(true) - .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), - R.mipmap.ic_launcher)); - return builder.build(); - } - - private String getAppName() { - PackageManager packageManager = getApplicationContext().getPackageManager(); - ApplicationInfo applicationInfo = null; - try { - applicationInfo = packageManager.getApplicationInfo(this.getPackageName(), 0); - } catch (final PackageManager.NameNotFoundException ignored) {} - return (String)((applicationInfo != null) ? packageManager.getApplicationLabel(applicationInfo) : "???"); - } - /** * SimpleItemRecyclerViewAdapter */ diff --git a/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java index 0da2eca..0724610 100644 --- a/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java +++ b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java @@ -5,6 +5,8 @@ import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.media.RingtoneManager; +import android.net.Uri; import android.util.Log; import androidx.core.app.NotificationCompat; @@ -17,26 +19,50 @@ import androidx.core.app.NotificationCompat; public class NotificationPublisher extends BroadcastReceiver { - private static String TAG = NotificationPublisher.class.getName(); - public static String NOTIFICATION_ID = "notification-id"; - public static String NOTIFICATION = "notification"; + private static final String TAG = NotificationPublisher.class.getName(); + + public static String NOTIFICATION_ID = "notification_id"; + public static String KEY_MESSAGE = "key_message"; + public static String KEY_TITLE = "key_title"; + public static String KEY_EXPAND = "key_expand"; + public static String KEY_SOUND = "key_sound"; + public static String KEY_MULTIPLE = "key_multiple"; + public static String APP_NAME = "PillDroid"; /** * onReceive notification * @param Context context * @param Intent intent */ + @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "Receive notification"); + NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + + int notificationId = intent.getIntExtra(NOTIFICATION_ID, 0); + String message = intent.getStringExtra(KEY_MESSAGE); + String title = intent.getStringExtra(KEY_TITLE); + boolean isEnabledExpand = intent.getBooleanExtra(KEY_EXPAND, false); + boolean isEnableSound = intent.getBooleanExtra(KEY_SOUND, false); + boolean isEnabledMultiple = intent.getBooleanExtra(KEY_MULTIPLE, false); + String channel_id = createNotificationChannel(context); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channel_id) .setSmallIcon(R.drawable.ic_pill) .setContentTitle(context.getString(R.string.app_name)) .setContentText(context.getString(R.string.pharmacy)) + .setChannelId(APP_NAME) .setStyle(new NotificationCompat.BigTextStyle() .bigText(context.getString(R.string.pharmacy))) .setPriority(NotificationCompat.PRIORITY_DEFAULT); + + if (isEnableSound) { + Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); + builder.setSound(alarmSound); + } + + notificationManager.notify(notificationId, builder.build()); } /** From f9a0e078b221d5aac42f46ea447ca9040293bb31 Mon Sep 17 00:00:00 2001 From: jacques Date: Tue, 21 Jul 2020 17:36:05 +0200 Subject: [PATCH 09/19] fix typo --- .../main/java/net/foucry/pilldroid/MedicamentListActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java index acd2d16..2817e90 100644 --- a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java @@ -415,7 +415,7 @@ public class MedicamentListActivity extends AppCompatActivity { Intent notificationIntent = new Intent(context, NotificationPublisher.class); notificationIntent.putExtra(NOTIFICATION_ID, 1); notificationIntent.putExtra(NotificationPublisher.KEY_MESSAGE, getString(R.string.pharmacy)); - notificationIntent.putExtra((NotificationPublisher.KEY_TITLE, getString(R.string.app_name)); + notificationIntent.putExtra(NotificationPublisher.KEY_TITLE, getString(R.string.app_name)); notificationIntent.putExtra(NotificationPublisher.KEY_SOUND, true); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); From d39a13f4539f20998ac3d8b7b48aca8959a1ed38 Mon Sep 17 00:00:00 2001 From: jacques Date: Sat, 15 Aug 2020 21:58:49 +0200 Subject: [PATCH 10/19] Add RECEIVE_BOOT_COMPLETE permission --- app/src/main/AndroidManifest.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2646367..e5c87dd 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ package="net.foucry.pilldroid"> + Date: Sat, 15 Aug 2020 22:03:19 +0200 Subject: [PATCH 11/19] Refector: - change dbHelper.getAllDrugs to return a sorted of medicaments; - remove unused import; Change onPause to call scheduleJob Add scheduleJob to use JobService --- .../java/net/foucry/pilldroid/DBHelper.java | 28 ++++++++-- .../pilldroid/MedicamentListActivity.java | 53 ++++++++++++++----- 2 files changed, 64 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/DBHelper.java b/app/src/main/java/net/foucry/pilldroid/DBHelper.java index 24ab493..c4a89c4 100644 --- a/app/src/main/java/net/foucry/pilldroid/DBHelper.java +++ b/app/src/main/java/net/foucry/pilldroid/DBHelper.java @@ -7,6 +7,8 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; +import java.util.Collections; +import java.util.Comparator; import java.util.LinkedList; import java.util.List; @@ -34,6 +36,7 @@ class DBHelper extends SQLiteOpenHelper { private static final String KEY_SEUIL_ALERT = "alerte"; private static DBHelper sInstance; + List medicaments = new LinkedList(); private static final String TAG = DBHelper.class.getName(); @@ -213,16 +216,15 @@ class DBHelper extends SQLiteOpenHelper { /** * - * @return a List of All medicaments presents in database + * @return a Sorted and updated by dateEndOfStock List of All medicaments presents in database */ List getAllDrugs() { - List medicaments = new LinkedList(); // Build the query String query = "SELECT * FROM " + TABLE_DRUG; - // Get reference to readable DB (tutorial parle de writable, mais bof... on verra) + // Get reference to readable DB SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(query, null); @@ -252,6 +254,21 @@ class DBHelper extends SQLiteOpenHelper { } cursor.close(); + + Medicament currentMedicament = null; + for (int position = 0 ; position < getCount() ; position++ ) { + currentMedicament = getItem(position); + currentMedicament.newStock(currentMedicament.getStock()); + updateDrug(currentMedicament); + } + + Collections.sort(medicaments, new Comparator() { + @Override + public int compare(Medicament lhs, Medicament rhs) { + return lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock()); + } + }); + Log.d(TAG, "getAllDrugs " + medicaments.toString()); return medicaments; @@ -327,6 +344,11 @@ class DBHelper extends SQLiteOpenHelper { mCount.close(); return count; } + + public Medicament getItem(int position) { + return medicaments.get(position); + } + boolean isMedicamentExist(String cip13) { boolean value = false; try { diff --git a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java index 2817e90..dfbb74e 100644 --- a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java @@ -3,8 +3,12 @@ package net.foucry.pilldroid; import android.app.AlarmManager; import android.app.NotificationManager; import android.app.PendingIntent; +import android.app.job.JobInfo; +import android.app.job.JobScheduler; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.icu.util.Calendar; import android.os.Bundle; import android.os.SystemClock; import android.util.Log; @@ -30,14 +34,12 @@ import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentResult; import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Collections; -import java.util.Comparator; + import java.util.Date; import java.util.List; import java.util.Locale; -import static net.foucry.pilldroid.NotificationPublisher.NOTIFICATION_ID; + import static net.foucry.pilldroid.UtilDate.date2String; import static net.foucry.pilldroid.Utils.intRandomExclusive; @@ -107,18 +109,18 @@ public class MedicamentListActivity extends AppCompatActivity { } medicaments = dbHelper.getAllDrugs(); - Collections.sort(medicaments, new Comparator() { +/* Collections.sort(medicaments, new Comparator() { @Override public int compare(Medicament lhs, Medicament rhs) { return lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock()); } - }); + });*/ - for (int position = 0 ; position < this.getCount() ; position++ ) { +/* for (int position = 0 ; position < this.getCount() ; position++ ) { currentMedicament = this.getItem(position); currentMedicament.newStock(currentMedicament.getStock()); dbHelper.updateDrug(currentMedicament); - } + }*/ View mRecyclerView = findViewById(R.id.medicament_list); assert mRecyclerView != null; @@ -222,7 +224,7 @@ public class MedicamentListActivity extends AppCompatActivity { public void onPause() { super.onPause(); - newStockCalculation(); + scheduleJob(); } /** scanNow @@ -237,7 +239,7 @@ public class MedicamentListActivity extends AppCompatActivity { /** * Calculation of newStock */ - public void newStockCalculation() { +/* public void newStockCalculation() { Calendar calendar = Calendar.getInstance(); Date now = calendar.getTime(); @@ -264,7 +266,7 @@ public class MedicamentListActivity extends AppCompatActivity { Log.d(TAG, "Notification scheduled for " + UtilDate.convertDate(dateSchedule)); } - } + }*/ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { @@ -404,11 +406,11 @@ public class MedicamentListActivity extends AppCompatActivity { recyclerView.setAdapter(mAdapter); } - /** +/* *//** * Schedule Notification for the delay * @param Context context * @param long delay - date for the notification in milliseconds - */ + *//* private void scheduleNotification(Context context, long delay) { Log.d(TAG, "scheduleNotification delay == " + delay); @@ -425,8 +427,31 @@ public class MedicamentListActivity extends AppCompatActivity { if (alarmManager != null) { alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); } - } + }*/ + public void scheduleJob() { + Calendar calendar = Calendar.getInstance(); + Date now = calendar.getTime(); + + ComponentName componentName = new ComponentName(this, PillDroidJobService.class); + JobInfo info = new JobInfo.Builder(123, componentName) + .setPersisted(false) + .setMinimumLatency(60 *1000) + .build(); + // .setPeriodic(5 * 60 * 1000) + JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); + int resultCode = scheduler.schedule(info); + if (resultCode == JobScheduler.RESULT_SUCCESS) { + Log.d(TAG, ("Job scheduled " + cal(now.getTime() + 60000))); + } else { + Log.d(TAG, "Job scheduling failed"); + } + } + public void cancelJob(View v) { + JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); + scheduler.cancel(123); + Log.d(TAG, "Job cancelled"); + } /** * SimpleItemRecyclerViewAdapter */ From f92c861764a6c2547e0dae28c76f3459f8822d33 Mon Sep 17 00:00:00 2001 From: jacques Date: Sat, 15 Aug 2020 22:04:03 +0200 Subject: [PATCH 12/19] Small refactoring, add setAutoCancel(true) --- .../main/java/net/foucry/pilldroid/NotificationPublisher.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java index 0724610..a59a367 100644 --- a/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java +++ b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java @@ -55,6 +55,7 @@ public class NotificationPublisher extends BroadcastReceiver { .setChannelId(APP_NAME) .setStyle(new NotificationCompat.BigTextStyle() .bigText(context.getString(R.string.pharmacy))) + .setAutoCancel(true) .setPriority(NotificationCompat.PRIORITY_DEFAULT); if (isEnableSound) { @@ -62,6 +63,7 @@ public class NotificationPublisher extends BroadcastReceiver { builder.setSound(alarmSound); } + notificationManager.notify(notificationId, builder.build()); } From c4175e06e3afeec2fd4ab2ca03712a4ca118706b Mon Sep 17 00:00:00 2001 From: jacques Date: Sat, 15 Aug 2020 22:04:34 +0200 Subject: [PATCH 13/19] New version of JobService implemtattion. --- .../foucry/pilldroid/PillDroidJobService.java | 103 ++++++++++++++---- 1 file changed, 83 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java b/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java index f85e486..45e1155 100644 --- a/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java +++ b/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java @@ -1,42 +1,105 @@ package net.foucry.pilldroid; +import android.app.AlarmManager; +import android.app.PendingIntent; import android.app.job.JobParameters; import android.app.job.JobService; -import android.os.Handler; -import android.os.Message; +import android.content.Context; +import android.content.Intent; +import android.icu.util.Calendar; +import android.os.SystemClock; import android.util.Log; +import java.util.Date; +import java.util.List; + +import static net.foucry.pilldroid.NotificationPublisher.NOTIFICATION_ID; + /** * Created by jacques on 17/09/16. */ + public class PillDroidJobService extends JobService { - private static final String TAG = "JobService"; + private static final String TAG = JobService.class.getName(); + private boolean jobCancelled = false; - private Handler mJobHandler = new Handler(new Handler.Callback() { - @Override - public boolean handleMessage(Message msg) { -// Toast.makeText( getApplicationContext(), "PillDroid - Calcul nouveau stocks", Toast.LENGTH_SHORT).show(); -// MedicamentListActivity.newStockCalculation(getApplicationContext()); - - jobFinished( (JobParameters) msg.obj,false); - return true; - } - }); + private List medicaments = null; + private DBHelper dbHelper = new DBHelper(this); @Override - public boolean onStartJob (JobParameters params) { - Log.d(TAG, "on Start Job: " + params.getJobId()); - mJobHandler.sendMessage(Message.obtain(mJobHandler, 1,params)); - return false; + public boolean onStartJob(JobParameters params) { + Log.d(TAG, "Job started"); + doBackgroundWork(params); + + return true; } + private void doBackgroundWork(final JobParameters params) { + + if (jobCancelled) { + return; + } + 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(getApplicationContext(), 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 milliseconds + * @param context + */ + private void scheduleNotification(Context context, long delay) { + Log.d(TAG, "scheduleNotification delay == " + delay); -} + Intent notificationIntent = new Intent(context, NotificationPublisher.class); + notificationIntent.putExtra(NOTIFICATION_ID, 1); + notificationIntent.putExtra(NotificationPublisher.KEY_MESSAGE, getString(R.string.pharmacy)); + notificationIntent.putExtra(NotificationPublisher.KEY_TITLE, getString(R.string.app_name)); + notificationIntent.putExtra(NotificationPublisher.KEY_SOUND, true); + PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); + + long futureInMillis = SystemClock.elapsedRealtime() + delay; + AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); + if (alarmManager != null) { + alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); + } + } +} \ No newline at end of file From 2ef64e31e3d46aa4935fe40d7c785096f485b5ec Mon Sep 17 00:00:00 2001 From: jacques Date: Fri, 21 Aug 2020 10:34:12 +0200 Subject: [PATCH 14/19] change schedule notification methode --- .../pilldroid/MedicamentListActivity.java | 9 +-- .../pilldroid/NotificationPublisher.java | 6 +- .../foucry/pilldroid/PillDroidJobService.java | 60 +++++++++++++++---- app/src/main/res/values/strings.xml | 2 + 4 files changed, 55 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java index dfbb74e..4375c2b 100644 --- a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java @@ -1,8 +1,6 @@ package net.foucry.pilldroid; -import android.app.AlarmManager; import android.app.NotificationManager; -import android.app.PendingIntent; import android.app.job.JobInfo; import android.app.job.JobScheduler; import android.content.ComponentName; @@ -10,7 +8,6 @@ import android.content.Context; import android.content.Intent; import android.icu.util.Calendar; import android.os.Bundle; -import android.os.SystemClock; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; @@ -34,12 +31,10 @@ import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentResult; import java.text.SimpleDateFormat; - import java.util.Date; import java.util.List; import java.util.Locale; - import static net.foucry.pilldroid.UtilDate.date2String; import static net.foucry.pilldroid.Utils.intRandomExclusive; @@ -63,6 +58,7 @@ public class MedicamentListActivity extends AppCompatActivity { final Boolean DEMO = false; final Boolean DBDEMO = false; public final int CUSTOMIZED_REQUEST_CODE = 0x0000ffff; + public String CHANNEL_ID = null; @Override public void onStart() { @@ -99,7 +95,6 @@ public class MedicamentListActivity extends AppCompatActivity { public void constructMedsList() { - Medicament currentMedicament; dbHelper = new DBHelper(getApplicationContext()); if (!(medicaments == null)) { @@ -442,7 +437,7 @@ public class MedicamentListActivity extends AppCompatActivity { JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); int resultCode = scheduler.schedule(info); if (resultCode == JobScheduler.RESULT_SUCCESS) { - Log.d(TAG, ("Job scheduled " + cal(now.getTime() + 60000))); + Log.d(TAG, ("Job scheduled " + (now.getTime() + 60000))); } else { Log.d(TAG, "Job scheduling failed"); } diff --git a/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java index a59a367..3638183 100644 --- a/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java +++ b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java @@ -38,8 +38,6 @@ public class NotificationPublisher extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Log.d(TAG, "Receive notification"); - NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - int notificationId = intent.getIntExtra(NOTIFICATION_ID, 0); String message = intent.getStringExtra(KEY_MESSAGE); String title = intent.getStringExtra(KEY_TITLE); @@ -63,6 +61,10 @@ public class NotificationPublisher extends BroadcastReceiver { builder.setSound(alarmSound); } + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); + +// notificationId is a unique int for each notification that you must define + notificationManager.notify(notificationId, builder.build()); notificationManager.notify(notificationId, builder.build()); } diff --git a/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java b/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java index 45e1155..5030680 100644 --- a/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java +++ b/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java @@ -1,15 +1,23 @@ package net.foucry.pilldroid; import android.app.AlarmManager; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.PendingIntent; import android.app.job.JobParameters; import android.app.job.JobService; import android.content.Context; import android.content.Intent; import android.icu.util.Calendar; +import android.media.RingtoneManager; +import android.net.Uri; +import android.os.Build; import android.os.SystemClock; import android.util.Log; +import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; + import java.util.Date; import java.util.List; @@ -24,7 +32,7 @@ public class PillDroidJobService extends JobService { private static final String TAG = JobService.class.getName(); private boolean jobCancelled = false; - private List medicaments = null; + private String CHANNEL_ID = null; private DBHelper dbHelper = new DBHelper(this); @Override @@ -35,12 +43,19 @@ public class PillDroidJobService extends JobService { return true; } + /** + * Grab sorted list of medicaments + * test dateAlert of the first of the list + * if dateAlert < now + * schedule notification + * @param JobParameters params + */ private void doBackgroundWork(final JobParameters params) { if (jobCancelled) { return; } - medicaments = dbHelper.getAllDrugs(); + List medicaments = dbHelper.getAllDrugs(); Calendar calendar = Calendar.getInstance(); Date now = calendar.getTime(); @@ -64,6 +79,7 @@ public class PillDroidJobService extends JobService { } long delay = dateSchedule - now.getTime(); + createNotificationChannel(); scheduleNotification(getApplicationContext(), delay); } @@ -88,18 +104,36 @@ public class PillDroidJobService extends JobService { private void scheduleNotification(Context context, long delay) { Log.d(TAG, "scheduleNotification delay == " + delay); - Intent notificationIntent = new Intent(context, NotificationPublisher.class); - notificationIntent.putExtra(NOTIFICATION_ID, 1); - notificationIntent.putExtra(NotificationPublisher.KEY_MESSAGE, getString(R.string.pharmacy)); - notificationIntent.putExtra(NotificationPublisher.KEY_TITLE, getString(R.string.app_name)); - notificationIntent.putExtra(NotificationPublisher.KEY_SOUND, true); + 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); - PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); + int notificationId = 666; + notificationManager.notify(notificationId, builder.build()); + + } + + /** + * createNotificationChannelid for android API >= 28 + * @param Context context + * @return String channel_id + */ + 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); + notificationManager.createNotificationChannel(channel); - long futureInMillis = SystemClock.elapsedRealtime() + delay; - AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); - if (alarmManager != null) { - alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); - } } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 99d908b..f05765e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -25,4 +25,6 @@ Enter cip 13 here.. Enter CIP 13 You must go to the pharmacy + pilldroidChannel + PillDroid NotificationChannel From bde9c1b78f4ebcbb0bfbaa8d71150df2f5b96cb8 Mon Sep 17 00:00:00 2001 From: jacques Date: Fri, 11 Sep 2020 11:28:10 +0200 Subject: [PATCH 15/19] Update gradle for new version --- app/build.gradle | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 5f71d96..d950c41 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -48,11 +48,12 @@ dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') testImplementation 'junit:junit:4.13' - implementation 'androidx.appcompat:appcompat:1.0.0' - implementation 'androidx.recyclerview:recyclerview:1.0.0' - implementation 'com.google.android.material:material:1.0.0' + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'androidx.recyclerview:recyclerview:1.1.0' + implementation 'com.google.android.material:material:1.2.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'com.journeyapps:zxing-android-embedded:4.1.0' - implementation 'androidx.appcompat:appcompat:1.0.0' + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation "com.android.support:support-compat:28.0.0" } From 6dcdcd3ae7fc1cf7e44f74a197b8c1020039f7a9 Mon Sep 17 00:00:00 2001 From: jacques Date: Fri, 11 Sep 2020 11:31:41 +0200 Subject: [PATCH 16/19] Cleanning import; remove not used method and variables; add mendatory method; --- .../pilldroid/MedicamentListActivity.java | 92 +++---------------- 1 file changed, 11 insertions(+), 81 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java index 4375c2b..e247285 100644 --- a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java @@ -53,12 +53,11 @@ public class MedicamentListActivity extends AppCompatActivity { * device. */ - // TODO: Change DEMO/DBDEMO form statci to non-static. In order to create fake data at only at launchtime + // TODO: Change DEMO/DBDEMO form static to non-static. In order to create fake data at only at lunchtime private boolean mTwoPane; final Boolean DEMO = false; final Boolean DBDEMO = false; public final int CUSTOMIZED_REQUEST_CODE = 0x0000ffff; - public String CHANNEL_ID = null; @Override public void onStart() { @@ -85,10 +84,6 @@ public class MedicamentListActivity extends AppCompatActivity { private SimpleItemRecyclerViewAdapter mAdapter; - public int getCount() { - return medicaments.size(); - } - public Medicament getItem(int position) { return medicaments.get(position); } @@ -104,19 +99,6 @@ public class MedicamentListActivity extends AppCompatActivity { } medicaments = dbHelper.getAllDrugs(); -/* Collections.sort(medicaments, new Comparator() { - @Override - public int compare(Medicament lhs, Medicament rhs) { - return lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock()); - } - });*/ - -/* for (int position = 0 ; position < this.getCount() ; position++ ) { - currentMedicament = this.getItem(position); - currentMedicament.newStock(currentMedicament.getStock()); - dbHelper.updateDrug(currentMedicament); - }*/ - View mRecyclerView = findViewById(R.id.medicament_list); assert mRecyclerView != null; setupRecyclerView((RecyclerView) mRecyclerView); @@ -222,6 +204,10 @@ public class MedicamentListActivity extends AppCompatActivity { scheduleJob(); } + public void onResume() { + super.onResume(); + } + /** scanNow * * @param view @@ -231,38 +217,6 @@ public class MedicamentListActivity extends AppCompatActivity { new IntentIntegrator(this).setOrientationLocked(false).setCaptureActivity(CustomScannerActivity.class).initiateScan(); } - /** - * Calculation of newStock - */ -/* public void newStockCalculation() { - 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() + 50000; // If dateAlert < now we schedule an alert for now + 5 seconds (3600000 pour 1 heure)[in prod define delay] - } else { - dateSchedule = dateAlert.getTime(); // If dateAlert > now we use dateAlert as scheduleDate - } - - long delay = dateSchedule - now.getTime(); - scheduleNotification(this, delay); - - Log.d(TAG, "Notification scheduled for " + UtilDate.convertDate(dateSchedule)); - } - }*/ - @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { @@ -401,50 +355,26 @@ public class MedicamentListActivity extends AppCompatActivity { recyclerView.setAdapter(mAdapter); } -/* *//** - * Schedule Notification for the delay - * @param Context context - * @param long delay - date for the notification in milliseconds - *//* - private void scheduleNotification(Context context, long delay) { - Log.d(TAG, "scheduleNotification delay == " + delay); - - Intent notificationIntent = new Intent(context, NotificationPublisher.class); - notificationIntent.putExtra(NOTIFICATION_ID, 1); - notificationIntent.putExtra(NotificationPublisher.KEY_MESSAGE, getString(R.string.pharmacy)); - notificationIntent.putExtra(NotificationPublisher.KEY_TITLE, getString(R.string.app_name)); - notificationIntent.putExtra(NotificationPublisher.KEY_SOUND, true); - - PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); - - long futureInMillis = SystemClock.elapsedRealtime() + delay; - AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); - if (alarmManager != null) { - alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); - } - }*/ - public void scheduleJob() { Calendar calendar = Calendar.getInstance(); Date now = calendar.getTime(); ComponentName componentName = new ComponentName(this, PillDroidJobService.class); - JobInfo info = new JobInfo.Builder(123, componentName) - .setPersisted(false) - .setMinimumLatency(60 *1000) + JobInfo info = new JobInfo.Builder(24560, componentName) + .setPersisted(true) + .setPeriodic(15 *60 *1000) .build(); - // .setPeriodic(5 * 60 * 1000) JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); int resultCode = scheduler.schedule(info); if (resultCode == JobScheduler.RESULT_SUCCESS) { - Log.d(TAG, ("Job scheduled " + (now.getTime() + 60000))); + Log.d(TAG, ("Job scheduled " + UtilDate.convertDate(now.getTime()+15 * 60*1000))); } else { Log.d(TAG, "Job scheduling failed"); } } public void cancelJob(View v) { JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); - scheduler.cancel(123); + scheduler.cancel(24560); Log.d(TAG, "Job cancelled"); } /** @@ -465,7 +395,7 @@ public class MedicamentListActivity extends AppCompatActivity { notifyDataSetChanged(); dbHelper.addDrug(scannedMedoc); } else { - Toast.makeText(getApplicationContext(), "aleready in the database", Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(), "already in the database", Toast.LENGTH_SHORT).show(); } } From ad889b8a94ab39ea095deb42817955b6a3f5b8d2 Mon Sep 17 00:00:00 2001 From: jacques Date: Fri, 11 Sep 2020 11:32:06 +0200 Subject: [PATCH 17/19] Add import --- .../java/net/foucry/pilldroid/NotificationPublisher.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java index 3638183..aacec21 100644 --- a/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java +++ b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java @@ -10,6 +10,7 @@ import android.net.Uri; import android.util.Log; import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; /** * Created by jfoucry on 6/23/16. @@ -61,11 +62,9 @@ public class NotificationPublisher extends BroadcastReceiver { builder.setSound(alarmSound); } - NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); - -// notificationId is a unique int for each notification that you must define - notificationManager.notify(notificationId, builder.build()); + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); + // notificationId is a unique int for each notification that you must define notificationManager.notify(notificationId, builder.build()); } From f27abe2d8bd2643d6b0e717925704d62445b8eaa Mon Sep 17 00:00:00 2001 From: jacques Date: Fri, 11 Sep 2020 11:33:08 +0200 Subject: [PATCH 18/19] Cleanning import; Fix CHANNEL_ID bug (at least) --- .../foucry/pilldroid/PillDroidJobService.java | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java b/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java index 5030680..db8c8fa 100644 --- a/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java +++ b/app/src/main/java/net/foucry/pilldroid/PillDroidJobService.java @@ -1,18 +1,10 @@ package net.foucry.pilldroid; -import android.app.AlarmManager; import android.app.NotificationChannel; import android.app.NotificationManager; -import android.app.PendingIntent; import android.app.job.JobParameters; import android.app.job.JobService; -import android.content.Context; -import android.content.Intent; import android.icu.util.Calendar; -import android.media.RingtoneManager; -import android.net.Uri; -import android.os.Build; -import android.os.SystemClock; import android.util.Log; import androidx.core.app.NotificationCompat; @@ -21,8 +13,6 @@ import androidx.core.app.NotificationManagerCompat; import java.util.Date; import java.util.List; -import static net.foucry.pilldroid.NotificationPublisher.NOTIFICATION_ID; - /** * Created by jacques on 17/09/16. @@ -31,13 +21,14 @@ import static net.foucry.pilldroid.NotificationPublisher.NOTIFICATION_ID; public class PillDroidJobService extends JobService { private static final String TAG = JobService.class.getName(); private boolean jobCancelled = false; - - private String CHANNEL_ID = null; + private String CHANNEL_ID = "pillDroid"; private DBHelper dbHelper = new DBHelper(this); + @Override public boolean onStartJob(JobParameters params) { Log.d(TAG, "Job started"); + createNotificationChannel(); doBackgroundWork(params); return true; @@ -79,8 +70,7 @@ public class PillDroidJobService extends JobService { } long delay = dateSchedule - now.getTime(); - createNotificationChannel(); - scheduleNotification(getApplicationContext(), delay); + scheduleNotification(delay); } Log.d(TAG, "Job finished"); @@ -98,12 +88,11 @@ public class PillDroidJobService extends JobService { /** * Schedule Notification for the delay * @param Context context - * @param long delay - date for the notification in milliseconds - * @param context + * @param long delay - date for the notification in millisecond */ - private void scheduleNotification(Context context, long delay) { + 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)) @@ -119,8 +108,6 @@ public class PillDroidJobService extends JobService { /** * createNotificationChannelid for android API >= 28 - * @param Context context - * @return String channel_id */ private void createNotificationChannel() { @@ -133,7 +120,15 @@ public class PillDroidJobService extends JobService { // Register the channel with the system; you can't change the importance // or other notification behaviors after this NotificationManager notificationManager = getSystemService(NotificationManager.class); - notificationManager.createNotificationChannel(channel); - + 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 From c9c5d7e9f314ee95f5e2590491f7c1e6f7b487eb Mon Sep 17 00:00:00 2001 From: jacques Date: Fri, 11 Sep 2020 11:34:05 +0200 Subject: [PATCH 19/19] Minor change in comments --- .../java/net/foucry/pilldroid/UtilDate.java | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) 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) {