From a66daa3d4f8a403f01eb0da355b6601efe62fefd Mon Sep 17 00:00:00 2001 From: jacques Date: Sat, 15 Aug 2020 22:03:19 +0200 Subject: [PATCH] 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 */