diff --git a/app/src/main/java/net/foucry/pilldroid/AlarmReceiver.java b/app/src/main/java/net/foucry/pilldroid/AlarmReceiver.java index e94aaea..8883956 100644 --- a/app/src/main/java/net/foucry/pilldroid/AlarmReceiver.java +++ b/app/src/main/java/net/foucry/pilldroid/AlarmReceiver.java @@ -65,7 +65,7 @@ public class AlarmReceiver extends BroadcastReceiver { notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); - PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); + PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "PillDroid") .setSmallIcon(R.drawable.ic_pill_alarm) @@ -139,7 +139,7 @@ public class AlarmReceiver extends BroadcastReceiver { PendingIntent alarmIntent; Intent intent = new Intent(context, AlarmReceiver.class); - alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); + alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); diff --git a/app/src/main/java/net/foucry/pilldroid/DBHelper.java b/app/src/main/java/net/foucry/pilldroid/DBHelper.java index bfaf962..5d9954e 100644 --- a/app/src/main/java/net/foucry/pilldroid/DBHelper.java +++ b/app/src/main/java/net/foucry/pilldroid/DBHelper.java @@ -315,6 +315,7 @@ class DBHelper extends SQLiteOpenHelper { public void updateDrug(Drug drug) { Log.d(TAG, "Update Drug == " + drug.toString()); + Log.d(TAG, "drug last_update == " + UtilDate.convertDate(drug.getDateLastUpdate())); // Get reference to writable DB SQLiteDatabase db = this.getWritableDatabase(); diff --git a/app/src/main/java/net/foucry/pilldroid/Drug.java b/app/src/main/java/net/foucry/pilldroid/Drug.java index 7eba859..54a1686 100644 --- a/app/src/main/java/net/foucry/pilldroid/Drug.java +++ b/app/src/main/java/net/foucry/pilldroid/Drug.java @@ -5,6 +5,7 @@ import android.util.Log; import java.io.Serializable; import java.util.Calendar; import java.util.Date; +import java.util.Objects; import static net.foucry.pilldroid.UtilDate.dateAtNoon; import static net.foucry.pilldroid.UtilDate.nbOfDaysBetweenDateAndToday; @@ -148,17 +149,17 @@ public class Drug implements Serializable { } void setDateEndOfStock() { - int numberDayOfPrise; + int numberDayOfTake; if (this.take > 0) { - numberDayOfPrise = (int) Math.floor(this.stock / this.take); + numberDayOfTake = (int) Math.floor(this.stock / this.take); } else { - numberDayOfPrise = 0; + numberDayOfTake = 0; } Date aDate = dateAtNoon(new Date()); Calendar calendar = Calendar.getInstance(); calendar.setTime(aDate); - calendar.add(Calendar.DAY_OF_YEAR, numberDayOfPrise); + calendar.add(Calendar.DAY_OF_YEAR, numberDayOfTake); this.dateEndOfStock = calendar.getTime(); } @@ -175,4 +176,15 @@ public class Drug implements Serializable { setDateLastUpdate(new Date().getTime()); } } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Drug drug = (Drug) o; + return stock == drug.stock && + take == drug.take && + alertThreshold == drug.alertThreshold && + warnThreshold == drug.warnThreshold && + Objects.equals(name, drug.name); + } } \ No newline at end of file diff --git a/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java index d52e90d..f39b211 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugDetailActivity.java @@ -28,6 +28,8 @@ public class DrugDetailActivity extends AppCompatActivity { private static final String TAG = DrugDetailActivity.class.getName(); Drug drug; + + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -109,8 +111,7 @@ public class DrugDetailActivity extends AppCompatActivity { return super.onOptionsItemSelected(item); } - private void getMDrugChanges() - { + private void getMDrugChanges() { Log.d(TAG, "Time to save new values"); DBHelper dbHelper = new DBHelper(this); @@ -142,9 +143,14 @@ public class DrugDetailActivity extends AppCompatActivity { newDrug.setTake(Double.parseDouble(takeValue)); newDrug.setWarnThreshold(Integer.parseInt(warningValue)); newDrug.setAlertThreshold(Integer.parseInt(alertValue)); - newDrug.setDateLastUpdate(new Date().getTime()); newDrug.setDateEndOfStock(); - dbHelper.updateDrug(newDrug); + if (drug.equals(newDrug)) { + Log.d(TAG, "drug and newDrug are Equals"); + } else { + Log.d(TAG, "drug and newDrug are NOT Equals"); + newDrug.setDateLastUpdate(new Date().getTime()); + dbHelper.updateDrug(newDrug); + } } } diff --git a/app/src/main/java/net/foucry/pilldroid/UtilDate.java b/app/src/main/java/net/foucry/pilldroid/UtilDate.java index 6a596a2..1213256 100644 --- a/app/src/main/java/net/foucry/pilldroid/UtilDate.java +++ b/app/src/main/java/net/foucry/pilldroid/UtilDate.java @@ -5,6 +5,7 @@ import android.util.Log; import java.text.DateFormat; import java.text.ParsePosition; import java.text.SimpleDateFormat; +import java.time.LocalTime; import java.util.Calendar; import java.util.Date; import java.util.Locale; @@ -95,7 +96,7 @@ class UtilDate { * @return formatted Date String */ static String convertDate(long dateInMilliseconds) { - DateFormat formatter = new SimpleDateFormat("dd/MM/yy HH:mm:ss", Locale.FRANCE); + DateFormat formatter = new SimpleDateFormat("dd/MM/yy HH:mm:ss", Locale.getDefault()); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(dateInMilliseconds); return formatter.format(calendar.getTime()); diff --git a/build.gradle b/build.gradle index 0948da1..cfe6773 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:7.0.3' + classpath 'com.android.tools.build:gradle:7.0.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files