From a4bc0dc8ee692c3bd3fc0d9580c0b19b4900251e Mon Sep 17 00:00:00 2001 From: Jacques Foucry Date: Fri, 10 Jun 2016 18:45:42 +0200 Subject: [PATCH] Lecture du code barre et du datamatrix --- .../pilldroid/MedicamentListActivity.java | 84 +++++++++++++------ app/src/main/res/values/strings.xml | 1 + 2 files changed, 59 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java index 5bfe26a..2418b41 100644 --- a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java @@ -1,10 +1,11 @@ package net.foucry.pilldroid; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; -import android.support.design.widget.FloatingActionButton; +import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; @@ -15,6 +16,7 @@ import android.view.ViewGroup; import android.widget.ImageView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; +import android.widget.Toast; import java.text.SimpleDateFormat; import java.util.Collections; @@ -49,6 +51,8 @@ public class MedicamentListActivity extends AppCompatActivity { } private static DBHelper dbHelper; + private static DBMedoc dbMedoc; + private SimpleCursorAdapter drugAdapter; private List medicaments; @@ -58,6 +62,7 @@ public class MedicamentListActivity extends AppCompatActivity { setContentView(R.layout.activity_medicament_list); dbHelper = new DBHelper(this); + dbMedoc = new DBMedoc(this); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); @@ -66,18 +71,18 @@ public class MedicamentListActivity extends AppCompatActivity { toolbar.setTitle(getTitle()); } - FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); + /*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - /* Snackbar.make(view, "Will be used to add a drug to the list", Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); */ + *//* Snackbar.make(view, "Will be used to add a drug to the list", Snackbar.LENGTH_LONG) + .setAction("Action", null).show(); *//* Intent intent = new Intent("com.google.zxing.client.android.SCAN"); intent.putExtra("SCAN_MODE", "CODE_128"); //intent.putExtra("SCAN_FORMATS", "EAN_13,DATA_MATRIX"); startActivityForResult(intent, 0); } - }); + });*/ if (DEMO) { @@ -142,6 +147,54 @@ public class MedicamentListActivity extends AppCompatActivity { } } + public void scanNow(View view) { + Intent intent = new Intent("com.google.zxing.client.android.SCAN"); + //intent.putExtra("SCAN_MODE", "CODE_128"); + intent.putExtra("SCAN_FORMATS", "CODE_18,DATA_MATRIX"); + startActivityForResult(intent, 0); + } + + public void onActivityResult(int requestCode, int resultCode, Intent intent) { + Context context = getApplicationContext(); + String cip13 = null; + if (requestCode == 0) { + if (resultCode == RESULT_OK) { + String contents = intent.getStringExtra("SCAN_RESULT"); + String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); + Log.i(Constants.TAG, "Format:" + format); + Log.i(Constants.TAG, "Content:" + contents); + + // Handle successful scan + if (format.equals("CODE_128")) { //CODE_128 + cip13 = contents; + } else + { + cip13 = contents.substring(4,17); + } + Medicament scannedMedoc = dbMedoc.getMedocByCIP13(cip13); + + if (scannedMedoc != null) { + Toast.makeText(context, "Medicament found in database", Toast.LENGTH_LONG).show(); + } else + { + AlertDialog.Builder dlg = new AlertDialog.Builder(this); + dlg.setTitle(context.getString(R.string.app_name)); + dlg.setMessage(context.getString(R.string.msgNotFound)); + dlg.setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + // nothing to do to just dismiss dialog + } + }); + + } + } else if (resultCode == RESULT_CANCELED) { + // Handle cancel + Toast.makeText(context, "Scan annulé", Toast.LENGTH_LONG).show(); + } + } + } + private void setupRecyclerView(@NonNull RecyclerView recyclerView) { recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getApplicationContext())); recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(medicaments)); @@ -247,27 +300,6 @@ public class MedicamentListActivity extends AppCompatActivity { public String toString() { return super.toString() + " '" + mContentView.getText() + "'"; } - - public void scanNow(View view) { - Intent intent = new Intent("com.google.zxing.client.android.SCAN"); - intent.putExtra("SCAN_MODE", "CODE_128"); - //intent.putExtra("SCAN_FORMATS", "EAN_13,DATA_MATRIX"); - startActivityForResult(intent, 0); - } - - public void onActivityResult(int requestCode, int resultCode, Intent intent) { - if (requestCode == 0) { - if (resultCode == RESULT_OK) { - String contents = intent.getStringExtra("SCAN_RESULT"); - String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); - Log.i("Prout", format); - Log.i("Prout", contents); - // Handle successful scan - } else if (resultCode == RESULT_CANCELED) { - // Handle cancel - } - } - } } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0b0ce68..cb93174 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -125,4 +125,5 @@ Found WLAN Configuration Google Book Search Requesting connection to network\u2026 + Médicament introuvable dans la base de données