Commit 337d8a47 authored by djamel's avatar djamel

test QRCode en fonction des clés fournis dans les réglages

parent 34d378e5
......@@ -63,9 +63,7 @@
</extensions>
</Objective-C-extensions>
<XML>
<option name="XML_KEEP_LINE_BREAKS" value="false" />
<option name="XML_ALIGN_ATTRIBUTES" value="false" />
<option name="XML_SPACE_INSIDE_EMPTY_TAG" value="true" />
<option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" />
</XML>
<codeStyleSettings language="XML">
<option name="FORCE_REARRANGE_MODE" value="1" />
......@@ -79,7 +77,7 @@
<match>
<AND>
<NAME>xmlns:android</NAME>
<XML_NAMESPACE />
<XML_NAMESPACE>Namespace:</XML_NAMESPACE>
</AND>
</match>
</rule>
......@@ -89,7 +87,7 @@
<match>
<AND>
<NAME>xmlns:.*</NAME>
<XML_NAMESPACE />
<XML_NAMESPACE>Namespace:</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
......
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="AndroidLintDeprecated" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="AndroidLintRtlHardcoded" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="LoggerInitializedWithForeignClass" enabled="false" level="WARNING" enabled_by_default="false">
<option name="loggerClassName" value="org.apache.log4j.Logger,org.slf4j.LoggerFactory,org.apache.commons.logging.LogFactory,java.util.logging.Logger" />
<option name="loggerFactoryMethodName" value="getLogger,getLogger,getLog,getLogger" />
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="PROJECT_PROFILE" value="Project Default" />
<option name="USE_PROJECT_PROFILE" value="true" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
......@@ -10,13 +10,16 @@
android:theme="@style/AppTheme" >
<activity
android:name=".Read_QR_Code"
android:label="@string/app_name" >
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Settings"
android:label="@string/settings">
</activity>
</application>
</manifest>
package djamelfel.gala;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by djamel on 17/10/15.
*/
public class Key_List implements Parcelable {
int id;
String key;
public Key_List(int id, String key) {
this.id = id;
this.key = key;
}
protected Key_List(Parcel in) {
this.id = in.readInt();
this.key = in.readString();
}
public int getId() {
return this.id;
}
public String getKey() {
return this.key;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.key);
}
public static final Creator<Key_List> CREATOR = new Creator<Key_List>() {
@Override
public Key_List createFromParcel(Parcel in) {
return new Key_List(in);
}
@Override
public Key_List[] newArray(int size) {
return new Key_List[size];
}
};
}
package djamelfel.gala;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.net.Uri;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class Read_QR_Code extends ActionBarActivity {
static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
private ArrayList<Key_List> key_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read__qr__code);
Intent intent = getIntent();
if (intent != null) {
key_list = intent.getParcelableArrayListExtra("key_list");
}
}
//product qr code mode
public void scanQR(View v) {
if (key_list == null) {
display(getString(R.string.error_lib_empty), false);
return;
}
try {
//start the scanning activity from the com.google.zxing.client.android.SCAN intent
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException anfe) {
//on catch, show the download dialog
showDialog(Read_QR_Code.this, "Auncun lecteur de QRCode n'a été trouvé", "Télécharger" +
" un scanner ?", "Oui", "Non").show();
}
}
//alert dialog for downloadDialog
private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence
message, CharSequence buttonYes, CharSequence buttonNo) {
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
......@@ -61,39 +82,102 @@ public class Read_QR_Code extends ActionBarActivity {
return downloadDialog.show();
}
//on ActivityResult method
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
//get the extras that are returned from the intent
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Toast toast = Toast.makeText(this, "Content:" + contents + " Format:" + format,
Toast.LENGTH_LONG);
toast.show();
if (requestCode == 0) if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
validateTicket(contents);
}
}
public void validateTicket(String result) {
String str[] = result.split(" ");
Boolean found = false;
Iterator<Key_List> itr = key_list.iterator();
while (itr.hasNext()) {
Key_List key = itr.next();
if (key.getId() == Integer.parseInt(str[1])) {
String hmac = hmacDigest(str[0] + " " + str[1] + " " + str[2], key.getKey(),
"HmacSHA1");
if(str[3].equals(hmac.substring(0, 8).toUpperCase())) {
display("youpi", true);
found = true;
}
return;
}
}
if (!found) {
display("Baddd", false);
}
}
public void display(String msg, boolean success) {
LayoutInflater inflater = getLayoutInflater();
View layout;
if(success) {
layout = inflater.inflate(R.layout.toast_success,
(ViewGroup) findViewById(R.id.toast_success));
}
else {
layout = inflater.inflate(R.layout.toast_failure,
(ViewGroup) findViewById(R.id.toast_failure));
}
TextView text = (TextView)layout.findViewById(R.id.text);
text.setTextSize(20);
text.setText(msg.toUpperCase());
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_read__qr__code, menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_read__qr__code, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(Read_QR_Code.this, Settings.class);
if (key_list != null)
if (!key_list.isEmpty())
intent.putParcelableArrayListExtra("key_list", key_list);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
public static String hmacDigest(String msg, String keyString, String algo) {
String digest = null;
try {
SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), algo);
Mac mac = Mac.getInstance(algo);
mac.init(key);
byte[] bytes = mac.doFinal(msg.getBytes("ASCII"));
StringBuffer hash = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
hash.append('0');
}
hash.append(hex);
}
digest = hash.toString();
} catch (UnsupportedEncodingException e) {
} catch (InvalidKeyException e) {
} catch (NoSuchAlgorithmException e) {
}
return digest;
}
}
package djamelfel.gala;
import android.content.Intent;
import android.database.MatrixCursor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Iterator;
public class Settings extends ActionBarActivity implements View.OnClickListener {
private ArrayList<Key_List> key_list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Intent intent = getIntent();
if (intent != null) {
Log.i("intent", "variable");
key_list = intent.getParcelableArrayListExtra("key_list");
}
if (key_list != null) {
updateView();
} else {
key_list = new ArrayList<Key_List>();
}
findViewById(R.id.new_key).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.new_key:
EditText id = (EditText)findViewById(R.id.id);
EditText key = (EditText)findViewById(R.id.key);
String idS = id.getText().toString();
String keyS = key.getText().toString();
if (idS.isEmpty() || keyS.isEmpty()) {
Toast.makeText(getApplicationContext(), R.string.empty_text_area, Toast
.LENGTH_LONG).show();
}
else {
key_list.add(new Key_List(Integer.parseInt(idS), keyS));
id.setText("");
key.setText("");
updateView();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.retour) {
Intent intent = new Intent(Settings.this, Read_QR_Code.class);
intent.putParcelableArrayListExtra("key_list", key_list);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
public void updateView() {
String[] columns = new String[]{"_id", "id", "key"};
MatrixCursor matrixCursor = new MatrixCursor(columns);
startManagingCursor(matrixCursor);
Iterator<Key_List> itr = key_list.iterator();
while(itr.hasNext()) {
Key_List kl = itr.next();
matrixCursor.addRow(new Object[]{
0, kl.getId(), kl.getKey()
});
}
String[] from = new String[]{"id", "key"};
int[] to = new int[]{R.id.textViewCol1, R.id.textViewCol2};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row_item,
matrixCursor, from, to, 0);
ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
}
}
......@@ -14,7 +14,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_margin="20dp"
android:text="Scan"
android:text="@string/scan"
android:textColor="#000000"
android:textSize="30dp" />
......@@ -26,7 +26,7 @@
android:layout_margin="10dp"
android:gravity="center"
android:onClick="scanQR"
android:text="Scanner Code"
android:text="@string/scanner"
android:textSize="18dp" >
</Button>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="478dp"
android:id="@+id/listView"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/listView"
android:weightSum="5">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/id"
android:layout_weight="1"
android:numeric="integer"
android:layout_below="@+id/listView"
android:hint="id"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/key"
android:layout_weight="3"
android:layout_toRightOf="@id/id"
android:autoText="false"
android:hint="key"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/new_key"
android:layout_weight="1"
android:id="@+id/new_key"
android:layout_toRightOf="@id/key"/>
</LinearLayout>
</RelativeLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<TextView
android:id="@+id/textViewCol1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_weight="1"
android:gravity="center"
android:padding="4dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textViewCol2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_failure"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="#ddff0000" >
<TextView android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF" />
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_success"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="#dd2fff00" >
<TextView android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF" />
</LinearLayout>
\ No newline at end of file
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".Read_QR_Code">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:title="@string/settings"/>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".Settings">
<item android:id="@+id/retour"
android:title="@string/retour" />
</menu>
\ No newline at end of file
<resources>
<string name="app_name">Gala</string>
<string name="settings">Settings</string>
<string name="action_settings">Settings</string>
<string name="new_key">Nouvel clé</string>
<string name="empty_text_area">veuillez saisir les champs</string>
<string name="retour">retour</string>
<string name="scan">Scan</string>
<string name="scanner">Scanner Code</string>
<string name="error_lib_empty">Veuillez entrer au moins une clé valide</string>
</resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment