Lecture du code barre et du datamatrix

This commit is contained in:
Jacques Foucry 2016-06-10 18:45:42 +02:00
parent b19edd2fbf
commit a4bc0dc8ee
2 changed files with 59 additions and 26 deletions

View file

@ -1,10 +1,11 @@
package net.foucry.pilldroid; package net.foucry.pilldroid;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; 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.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
@ -15,6 +16,7 @@ import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.SimpleCursorAdapter; import android.widget.SimpleCursorAdapter;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Collections; import java.util.Collections;
@ -49,6 +51,8 @@ public class MedicamentListActivity extends AppCompatActivity {
} }
private static DBHelper dbHelper; private static DBHelper dbHelper;
private static DBMedoc dbMedoc;
private SimpleCursorAdapter drugAdapter; private SimpleCursorAdapter drugAdapter;
private List<Medicament> medicaments; private List<Medicament> medicaments;
@ -58,6 +62,7 @@ public class MedicamentListActivity extends AppCompatActivity {
setContentView(R.layout.activity_medicament_list); setContentView(R.layout.activity_medicament_list);
dbHelper = new DBHelper(this); dbHelper = new DBHelper(this);
dbMedoc = new DBMedoc(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
@ -66,18 +71,18 @@ public class MedicamentListActivity extends AppCompatActivity {
toolbar.setTitle(getTitle()); toolbar.setTitle(getTitle());
} }
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); /*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() { fab.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
/* Snackbar.make(view, "Will be used to add a drug to the list", Snackbar.LENGTH_LONG) *//* Snackbar.make(view, "Will be used to add a drug to the list", Snackbar.LENGTH_LONG)
.setAction("Action", null).show(); */ .setAction("Action", null).show(); *//*
Intent intent = new Intent("com.google.zxing.client.android.SCAN"); Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "CODE_128"); intent.putExtra("SCAN_MODE", "CODE_128");
//intent.putExtra("SCAN_FORMATS", "EAN_13,DATA_MATRIX"); //intent.putExtra("SCAN_FORMATS", "EAN_13,DATA_MATRIX");
startActivityForResult(intent, 0); startActivityForResult(intent, 0);
} }
}); });*/
if (DEMO) { 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) { private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getApplicationContext())); recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getApplicationContext()));
recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(medicaments)); recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(medicaments));
@ -247,27 +300,6 @@ public class MedicamentListActivity extends AppCompatActivity {
public String toString() { public String toString() {
return super.toString() + " '" + mContentView.getText() + "'"; 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
}
}
}
} }
} }
} }

View file

@ -125,4 +125,5 @@
<string name="result_wifi">Found WLAN Configuration</string> <string name="result_wifi">Found WLAN Configuration</string>
<string name="sbc_name">Google Book Search</string> <string name="sbc_name">Google Book Search</string>
<string name="wifi_changing_network">Requesting connection to network\u2026</string> <string name="wifi_changing_network">Requesting connection to network\u2026</string>
<string name="msgNotFound">Médicament introuvable dans la base de données</string>
</resources> </resources>