Commit 7a7a4a14 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Ginger integration

Make request to Ginger
Manage Ginger key
Manage Nemopay key
parent 7e08fac3
package fr.utc.simde.payutc;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.fasterxml.jackson.databind.JsonNode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import fr.utc.simde.payutc.tools.CASConnexion;
import fr.utc.simde.payutc.tools.Config;
import fr.utc.simde.payutc.tools.Dialog;
import fr.utc.simde.payutc.tools.Ginger;
import fr.utc.simde.payutc.tools.HTTPRequest;
import fr.utc.simde.payutc.tools.NemopaySession;
......@@ -26,11 +37,14 @@ public abstract class BaseActivity extends NFCActivity {
private static final String LOG_TAG = "_BaseActivity";
protected static NemopaySession nemopaySession;
protected static Ginger ginger;
protected static CASConnexion casConnexion;
protected static Config config;
protected static Dialog dialog;
protected static SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -94,7 +108,7 @@ public abstract class BaseActivity extends NFCActivity {
}
}
if (rights.size() == sameRights.size())
if ((rights.size() == sameRights.size()) || (myRightList.has("0") && rights.size() == 0))
runOnUiThread(runnable);
else {
runOnUiThread(new Runnable() {
......@@ -382,11 +396,59 @@ public abstract class BaseActivity extends NFCActivity {
}
protected void startReadCardInfoActivity(final Activity activity) {
hasRights(getString(R.string.user_rights_list_collecting), getResources().getStringArray(R.array.options_rights)[2].split(" "), new Runnable() {
hasRights(getString(R.string.user_rights_list_collecting), new String[]{"STAFF", "POSS3", "GESUSERS"}, new Runnable() {
@Override
public void run() {dialog.startLoading(activity, activity.getResources().getString(R.string.information_collection), activity.getResources().getString(R.string.buyer_info_collecting));
activity.startActivity(new Intent(activity, ReadCardInfoActivity.class));
}
});
}
protected void delKey() {
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.remove("key");
edit.apply();
unregister(BaseActivity.this);
}
protected void setNemopayKey(final String key) {
dialog.startLoading(BaseActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_authentification));
new Thread() {
@Override
public void run() {
try {
nemopaySession.loginApp(key, casConnexion);
Thread.sleep(100);
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
}
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
if (nemopaySession.isRegistered()) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key", key);
editor.apply();
((TextView) findViewById(R.id.text_app_registered)).setText(nemopaySession.getName().substring(0, nemopaySession.getName().length() - (nemopaySession.getName().matches("^.* - ([0-9]{4})([/-])([0-9]{2})\\2([0-9]{2})$") ? 13 : 0)));
}
else
dialog.errorDialog(BaseActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_error_registering));
}
});
}
}.start();
}
protected void setGingerKey(final String key) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key_ginger", key);
editor.apply();
ginger.setKey(key);
}
}
......@@ -36,6 +36,7 @@ import java.util.Map;
import fr.utc.simde.payutc.tools.CASConnexion;
import fr.utc.simde.payutc.tools.Config;
import fr.utc.simde.payutc.tools.Dialog;
import fr.utc.simde.payutc.tools.Ginger;
import fr.utc.simde.payutc.tools.InternetBroadcast;
import fr.utc.simde.payutc.tools.NemopaySession;
......@@ -50,8 +51,6 @@ public class MainActivity extends BaseActivity {
protected static InternetBroadcast internetBroadcast;
private static SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -60,15 +59,20 @@ public class MainActivity extends BaseActivity {
sharedPreferences = getSharedPreferences("payutc", Activity.MODE_PRIVATE);
nemopaySession = new NemopaySession(MainActivity.this);
ginger = new Ginger(MainActivity.this);
casConnexion = new CASConnexion(nemopaySession);
config = new Config(sharedPreferences);
this.internetBroadcast = new InternetBroadcast();
registerReceiver(this.internetBroadcast, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
final String key = sharedPreferences.getString("key", "");
String key = sharedPreferences.getString("key", "");
if (!key.equals(""))
setNemopayKey(key);
key = sharedPreferences.getString("key_ginger", "");
if (!key.equals(""))
setKey(key);
setGingerKey(key);
appNameText = findViewById(R.id.text_app_name);
appConfigText = findViewById(R.id.text_app_config);
......@@ -107,7 +111,7 @@ public class MainActivity extends BaseActivity {
if (!nemopaySession.isRegistered()) {
final String key = sharedPreferences.getString("key", "");
if (!key.equals(""))
setKey(key);
setNemopayKey(key);
}
}
......@@ -150,51 +154,6 @@ public class MainActivity extends BaseActivity {
}
}
protected void delKey() {
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.remove("key");
edit.apply();
unregister(MainActivity.this);
}
protected void setKey(final String key) {
if (nemopaySession.isRegistered()) {
dialog.errorDialog(MainActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_already_registered));
return;
}
dialog.startLoading(MainActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_authentification));
new Thread() {
@Override
public void run() {
try {
nemopaySession.loginApp(key, casConnexion);
Thread.sleep(100);
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
}
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.stopLoading();
if (nemopaySession.isRegistered()) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key", key);
editor.apply();
((TextView) findViewById(R.id.text_app_registered)).setText(nemopaySession.getName().substring(0, nemopaySession.getName().length() - (nemopaySession.getName().matches("^.* - ([0-9]{4})([/-])([0-9]{2})\\2([0-9]{2})$") ? 13 : 0)));
}
else
dialog.errorDialog(MainActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_error_registering));
}
});
}
}.start();
}
protected void connectWithCAS(final String username, final String password) throws InterruptedException {
dialog.startLoading(MainActivity.this, getString(R.string.cas_connection), getString(R.string.cas_in_url));
new Thread() {
......@@ -448,47 +407,47 @@ public class MainActivity extends BaseActivity {
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder
.setTitle(R.string.key_registration)
.setView(keyView)
.setCancelable(false)
.setPositiveButton(R.string.register, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int id) {
if (nameInput.getText().toString().equals("")) {
Toast.makeText(MainActivity.this, R.string.key_name_required, Toast.LENGTH_SHORT).show();
dialogInterface.cancel();
keyDialog();
}
else {
dialogInterface.cancel();
.setTitle(R.string.key_registration)
.setView(keyView)
.setCancelable(false)
.setPositiveButton(R.string.register, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int id) {
if (nameInput.getText().toString().equals("")) {
Toast.makeText(MainActivity.this, R.string.key_name_required, Toast.LENGTH_SHORT).show();
dialogInterface.cancel();
keyDialog();
}
else {
dialogInterface.cancel();
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_registering), true);
loading.setCancelable(false);
new Thread() {
@Override
public void run() {
try {
nemopaySession.registerApp(nameInput.getText().toString() + (nameInput.getText().toString().matches("^.* - ([0-9]{4})([/-])([0-9]{2})\\2([0-9]{2})$") ? "" : " - " + date), descriptionInput.getText().toString(), service);
Thread.sleep(100);
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
}
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_registering), true);
loading.setCancelable(false);
new Thread() {
@Override
public void run() {
try {
nemopaySession.registerApp(nameInput.getText().toString() + (nameInput.getText().toString().matches("^.* - ([0-9]{4})([/-])([0-9]{2})\\2([0-9]{2})$") ? "" : " - " + date), descriptionInput.getText().toString(), service);
Thread.sleep(100);
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
loading.dismiss();
if (nemopaySession.getKey().isEmpty())
dialog.errorDialog(MainActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_error_registering));
else
setNemopayKey(nemopaySession.getKey());
}
});
}
runOnUiThread(new Runnable() {
@Override
public void run() {
loading.dismiss();
if (nemopaySession.getKey().isEmpty())
dialog.errorDialog(MainActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_error_registering));
else
setKey(nemopaySession.getKey());
}
});
}
}.start();
}.start();
}
}
}
});
});
dialog.createDialog(alertDialogBuilder, nameInput);
}
......@@ -504,7 +463,7 @@ public class MainActivity extends BaseActivity {
.setCancelable(false)
.setPositiveButton(R.string.register, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int id) {
setKey(keyInput.getText().toString());
setNemopayKey(keyInput.getText().toString());
}
})
.setNegativeButton(R.string.cancel, null);
......
package fr.utc.simde.payutc.tools;
import android.app.Activity;
import android.util.Log;
import java.util.HashMap;
import java.util.Map;
import fr.utc.simde.payutc.R;
/**
* Created by Samy on 10/11/2017.
*/
public class Ginger {
private static final String LOG_TAG = "_Ginger";
private static final String url = "https://assos.utc.fr/ginger/v1/";
private String key;
private HTTPRequest request;
private String noKey;
private String noRight;
private String serviceText;
private String notFound;
private String badRequest;
private String internalError;
private String errorRequest;
public Ginger(final Activity activity) {
this.key = "";
this.noKey = activity.getString(R.string.ginger_no_key);
this.noRight = activity.getString(R.string.ginger_no_rights);
this.serviceText = activity.getString(R.string.service);
this.notFound = activity.getString(R.string.not_found);
this.badRequest = activity.getString(R.string.bad_request);
this.internalError = activity.getString(R.string.internal_error);
this.errorRequest = activity.getString(R.string.error_request);
}
public void setKey(final String key) { this.key = key; }
public HTTPRequest getRequest() { return this.request; }
public int getInfo(final String login) throws Exception {
return request(
login
);
}
protected int request(final String request) throws Exception { return request(request, new HashMap<String, String>()); }
protected int request(final String request, Map<String, String> getArgs) throws Exception {
Log.d(LOG_TAG, "url: " + url + request);
getArgs.put("key", this.key);
this.request = new HTTPRequest(url + request);
this.request.setGet(getArgs);
int responseCode = this.request.get();
if (responseCode == 200)
return 200;
else if (responseCode == 401)
throw new Exception(this.noKey);
else if (responseCode == 403)
throw new Exception(this.noRight);
else if (responseCode == 404)
throw new Exception("Ginger " + this.notFound);
else if (responseCode == 400)
throw new Exception("Ginger " + this.badRequest);
else if (responseCode == 500 || responseCode == 503) {
throw new Exception("Ginger " + this.internalError);
}
else
throw new Exception("Ginger " + this.errorRequest + " " + responseCode);
}
}
......@@ -7,16 +7,7 @@
<item>Lire un QR Code</item>
<item>Créer une cotisation extérieure</item>
<item>Ajouter une carte</item>
<item>Modifier les clés</item>
</string-array>
<string-array name="options_rights">
<item>STAFF POSS3 GESSALES</item>
<item>GESARTICLE POSS3</item>
<item>STAFF</item>
<item>STAFF POSS3</item>
<item></item>
<item>STAFF POSS3</item>
<item></item>
<item>Modifier la clé Nemopay</item>
<item>Modifier la clé Ginger</item>
</string-array>
</resources>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="user_no_rights">n\'a aucun droit. L\'utilisateur a été automatiquement déconnecté</string>
<string name="ginger_no_rights">La clé donnée n\'a pas les droits</string>
<string name="no_right">Il est nécessaire d\'avoir au moins le droit suivant:</string>
<string name="no_rights">Il est nécessaire d\'avoir au moins les droits suivants:</string>
<string name="no_need_rights">Aucun droit spécifique n\'est nécessaire</string>
......
<resources>
<string name="app_name">PayUTC</string>
<string name="payutc">PayUTC</string>
<string name="nemopay">Nemopay</string>
<string name="ginger">Ginger</string>
<string name="hello">Pour se connecter, clique sur\n le bouton ou scanne ton badge</string>
<string name="ok">Ok</string>
<string name="yes">Oui</string>
......@@ -50,6 +52,7 @@
<string name="tag_id">ID tag</string>
<string name="tag">Numéro de badge</string>
<string name="short_tag">Numéro raccourci</string>
<string name="type">Type</string>
<string name="author">&lt;/&gt; avec ♥ par Samy NASTUZZI</string>
<string name="error_unexpected">Une erreur improbable a eu lieu</string>
......@@ -126,7 +129,9 @@
<string name="can_cancel">Permettre l\'annulation</string>
<string name="cant_cancel">L\'application a été configurée pour ne pas permettre l\'annulation d\'une transaction (contacter un adminstrateur pour annuler)</string>
<string name="read_card_information">Informations de la carte</string>
<string name="read_new_card">Lire une carte</string>
<string name="ginger_information">Informations de la part de Ginger</string>
<string name="ginger_no_key">Aucune clé n\'a été donnée</string>
<string name="print_mode">Mode d\'affichage</string>
<string name="filters">Filtres d\'affichage</string>
......
Supports Markdown
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