Change random generator

Add variable for random values
This commit is contained in:
Jacques Foucry 2016-11-08 12:09:47 +01:00
parent 74d861b1e4
commit 88322fe1bc
2 changed files with 45 additions and 37 deletions

View file

@ -4,10 +4,6 @@ import android.app.AlarmManager;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
@ -15,7 +11,6 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Message;
import android.os.SystemClock; import android.os.SystemClock;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
@ -30,7 +25,6 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -46,12 +40,10 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Random; import java.util.Random;
import java.util.logging.Handler;
import static net.foucry.pilldroid.NotificationPublisher.NOTIFICATION_ID; import static net.foucry.pilldroid.NotificationPublisher.NOTIFICATION_ID;
import static net.foucry.pilldroid.UtilDate.date2String; import static net.foucry.pilldroid.UtilDate.date2String;
import static net.foucry.pilldroid.UtilDate.tomorrowAtNoonInMillis; import static net.foucry.pilldroid.Utils.intRandomExclusive;
import static net.foucry.pilldroid.Utils.doubleRandomInclusive;
/** /**
* An activity representing a list of Medicaments. This activity * An activity representing a list of Medicaments. This activity
@ -69,6 +61,7 @@ public class MedicamentListActivity extends AppCompatActivity {
*/ */
private boolean mTwoPane; private boolean mTwoPane;
final static Boolean DEMO = true; final static Boolean DEMO = true;
final static Boolean DBDEMO = true;
final static Random random = new Random(); final static Random random = new Random();
/** /**
@ -181,41 +174,51 @@ public class MedicamentListActivity extends AppCompatActivity {
// startService(new Intent(this, TimeService.class)); // startService(new Intent(this, TimeService.class));
if (DEMO) { if (DEMO) {
// Added to drop database each the app is launch.
if (DBDEMO) {
dbHelper.dropDrug();
}
if (dbHelper.getCount() == 0) { if (dbHelper.getCount() == 0) {
// String cis, String cip13, String nom, String mode_administration, // String cis, String cip13, String nom, String mode_administration,
// String presentation,double stock, double prise, int warn, int alert // String presentation,double stock, double prise, int warn, int alert
// Limit for randmon generator
final int min_stock=5;
final int max_stock=50;
final int min_prise=0;
final int max_prise=3;
dbHelper.addDrug(new Medicament("60000011", "3400930000011", "Médicament test 01", "orale", dbHelper.addDrug(new Medicament("60000011", "3400930000011", "Médicament test 01", "orale",
"plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)", "plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)",
doubleRandomInclusive(0, 100), doubleRandomInclusive(0, 10), 14, 7)); intRandomExclusive(min_stock, max_stock), intRandomExclusive(min_prise, max_prise), 14, 7));
dbHelper.addDrug(new Medicament("60000012", "3400930000012", "Médicament test 02", "orale", dbHelper.addDrug(new Medicament("60000012", "3400930000012", "Médicament test 02", "orale",
"plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)", "plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)",
doubleRandomInclusive(0, 100), doubleRandomInclusive(0, 10), 14, 7)); intRandomExclusive(min_stock, max_stock), intRandomExclusive(min_prise, max_prise), 14, 7));
dbHelper.addDrug(new Medicament("60000013", "3400930000013", "Médicament test 03", "orale", dbHelper.addDrug(new Medicament("60000013", "3400930000013", "Médicament test 03", "orale",
"plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)", "plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)",
doubleRandomInclusive(0, 100), doubleRandomInclusive(0, 10), 14, 7)); intRandomExclusive(min_stock, max_stock), intRandomExclusive(min_prise, max_prise), 14, 7));
dbHelper.addDrug(new Medicament("60000014", "3400930000014", "Médicament test 04", "orale", dbHelper.addDrug(new Medicament("60000014", "3400930000014", "Médicament test 04", "orale",
"plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)", "plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)",
doubleRandomInclusive(0, 100), doubleRandomInclusive(0, 10), 14, 7)); intRandomExclusive(min_stock, max_stock), intRandomExclusive(min_prise, max_prise), 14, 7));
dbHelper.addDrug(new Medicament("60000015", "3400930000015", "Médicament test 05", "orale", dbHelper.addDrug(new Medicament("60000015", "3400930000015", "Médicament test 05", "orale",
"plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)", "plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)",
doubleRandomInclusive(0, 100), doubleRandomInclusive(0, 10), 14, 7)); intRandomExclusive(min_stock, max_stock), intRandomExclusive(min_prise, max_prise), 14, 7));
dbHelper.addDrug(new Medicament("60000016", "3400930000016", "Médicament test 06", "orale", dbHelper.addDrug(new Medicament("60000016", "3400930000016", "Médicament test 06", "orale",
"plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)", "plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)",
doubleRandomInclusive(0, 100), doubleRandomInclusive(0, 10), 14, 7)); intRandomExclusive(min_stock, max_stock), intRandomExclusive(min_prise, max_prise), 14, 7));
dbHelper.addDrug(new Medicament("60000017", "3400930000017", "Médicament test 07", "orale", dbHelper.addDrug(new Medicament("60000017", "3400930000017", "Médicament test 07", "orale",
"plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)", "plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)",
doubleRandomInclusive(0, 100), doubleRandomInclusive(0, 10), 14, 7)); intRandomExclusive(min_stock, max_stock), intRandomExclusive(min_prise, max_prise), 14, 7));
dbHelper.addDrug(new Medicament("60000018", "3400930000018", "Médicament test 08", "orale", dbHelper.addDrug(new Medicament("60000018", "3400930000018", "Médicament test 08", "orale",
"plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)", "plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)",
doubleRandomInclusive(0, 100), doubleRandomInclusive(0, 10), 14, 7)); intRandomExclusive(min_stock, max_stock), intRandomExclusive(min_prise, max_prise), 14, 7));
dbHelper.addDrug(new Medicament("60000019", "3400930000019", "Médicament test 09", "orale", dbHelper.addDrug(new Medicament("60000019", "3400930000019", "Médicament test 09", "orale",
"plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)", "plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)",
doubleRandomInclusive(0, 100), doubleRandomInclusive(0, 10), 14, 7)); intRandomExclusive(min_stock, max_stock), intRandomExclusive(min_prise, max_prise), 14, 7));
dbHelper.addDrug(new Medicament("60000010", "3400930000010", "Médicament test 10", "orale", dbHelper.addDrug(new Medicament("60000010", "3400930000010", "Médicament test 10", "orale",
"plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)", "plaquette(s) thermoformée(s) PVC PVDC aluminium de 10 comprimé(s)",
doubleRandomInclusive(0, 100), doubleRandomInclusive(0, 10), 14, 7)); intRandomExclusive(min_stock, max_stock), intRandomExclusive(min_prise, max_prise), 14, 7));
} }
} }
@ -310,12 +313,12 @@ public class MedicamentListActivity extends AppCompatActivity {
if (dateAlerte.getTime() < now.getTime()) if (dateAlerte.getTime() < now.getTime())
{ {
dateSchedule = now.getTime() + 3600000; // If dateAlerte < now we schedule an alert for now + 5 seconds (3600000 pour 1 heure) dateSchedule = now.getTime() + 50000; // If dateAlerte < now we schedule an alert for now + 5 seconds (3600000 pour 1 heure)
} else { } else {
dateSchedule = dateAlerte.getTime(); // If dateAlerte > now we use dateAlerte as scheduleDate dateSchedule = dateAlerte.getTime(); // If dateAlerte > now we use dateAlerte as scheduleDate
} }
long delay = (long) (dateSchedule - now.getTime()); long delay = dateSchedule - now.getTime();
scheduleNotification(getNotification(getString(R.string.notification_text)),delay); scheduleNotification(getNotification(getString(R.string.notification_text)),delay);
Log.d(TAG, "Notification scheduled for "+ UtilDate.convertDate(dateSchedule)); Log.d(TAG, "Notification scheduled for "+ UtilDate.convertDate(dateSchedule));
@ -416,7 +419,7 @@ public class MedicamentListActivity extends AppCompatActivity {
ApplicationInfo applicationInfo = null; ApplicationInfo applicationInfo = null;
try { try {
applicationInfo = packageManager.getApplicationInfo(this.getPackageName(), 0); applicationInfo = packageManager.getApplicationInfo(this.getPackageName(), 0);
} catch (final PackageManager.NameNotFoundException e) {} } catch (final PackageManager.NameNotFoundException ignored) {}
return (String)((applicationInfo != null) ? packageManager.getApplicationLabel(applicationInfo) : "???"); return (String)((applicationInfo != null) ? packageManager.getApplicationLabel(applicationInfo) : "???");
} }
@ -428,11 +431,11 @@ public class MedicamentListActivity extends AppCompatActivity {
private final List<Medicament> mValues; private final List<Medicament> mValues;
public SimpleItemRecyclerViewAdapter(List<Medicament> items) { SimpleItemRecyclerViewAdapter(List<Medicament> items) {
mValues = items; mValues = items;
} }
public void addItem(Medicament scannedMedoc) { void addItem(Medicament scannedMedoc) {
mValues.add(scannedMedoc); mValues.add(scannedMedoc);
notifyDataSetChanged(); notifyDataSetChanged();
dbHelper.addDrug(scannedMedoc); dbHelper.addDrug(scannedMedoc);
@ -507,16 +510,16 @@ public class MedicamentListActivity extends AppCompatActivity {
return mValues.size(); return mValues.size();
} }
public class ViewHolder extends RecyclerView.ViewHolder { class ViewHolder extends RecyclerView.ViewHolder {
public final View mView; final View mView;
public final TextView mIDView; final TextView mIDView;
public final TextView mContentView; final TextView mContentView;
public final TextView mEndOfStock; final TextView mEndOfStock;
public final ImageView mIconView; final ImageView mIconView;
public Medicament mItem; Medicament mItem;
public ViewHolder(View view) { ViewHolder(View view) {
super(view); super(view);
mView = view; mView = view;
mIDView = (TextView) view.findViewById(R.id.cip13); mIDView = (TextView) view.findViewById(R.id.cip13);

View file

@ -22,12 +22,17 @@ public class Utils {
os.write(bytes, 0, count); os.write(bytes, 0, count);
} }
} }
catch(Exception ex){} catch(Exception ignored){}
} }
public static final double doubleRandomInclusive(int min, int max) { /**
double value = Math.floor(min + (max - min) * MedicamentListActivity.random.nextDouble() *4)/4; * Return a random number between twovalues - use to gənerat a false demo DB
* @param min minimal value accepted
return value; * @param max maximum value accepted
* @return
*/
static final int intRandomExclusive(int min, int max) {
Random r = new Random();
return r.nextInt(max - min) +max;
} }
} }