Commit 4f157c29 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Merge branch 'feature/foundationListActivity' into feature/connection

parents 73ca167b 9b39e556
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".BaseActivity" /> <activity android:name=".BaseActivity" />
<activity android:name=".FoundationListActivity" />
</application> </application>
</manifest> </manifest>
\ No newline at end of file
package fr.utc.simde.payutc; package fr.utc.simde.payutc;
import android.os.Bundle;
import android.widget.TextView;
import fr.utc.simde.payutc.tools.CASConnexion; import fr.utc.simde.payutc.tools.CASConnexion;
import fr.utc.simde.payutc.tools.Dialog; import fr.utc.simde.payutc.tools.Dialog;
import fr.utc.simde.payutc.tools.NemopaySession; import fr.utc.simde.payutc.tools.NemopaySession;
......
package fr.utc.simde.payutc;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.json.JSONArray;
import org.json.JSONObject;
import fr.utc.simde.payutc.tools.HTTPRequest;
/**
* Created by Samy on 26/10/2017.
*/
public class FoundationListActivity extends BaseActivity {
private static final String LOG_TAG = "_FoundationListActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_foundation_list);
dialog.startLoading(FoundationListActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_authentification));
new Thread() {
@Override
public void run() {
try {
nemopaySession.getFoundations();
Thread.sleep(100);
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
}
runOnUiThread(new Runnable() {
@Override
public void run() {
HTTPRequest request = nemopaySession.getRequest();
dialog.stopLoading();
try {
if (request.getResponseCode() != 200)
throw new Exception("Malformed JSON");
JsonNode foundationList = request.getJsonResponse();
if (!request.isJsonResponse() || !foundationList.isArray())
throw new Exception("JSON unexpected");
setFoundationList(foundationList);
} catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
dialog.errorDialog(getString(R.string.information_collection), getString(R.string.foundation_error_get_list));
}
}
});
}
}.start();
}
@Override
protected void onIdentification(String idBadge) {}
@Override
protected void onDestroy() {
super.onDestroy();
disconnect();
}
protected void setFoundationList(JsonNode foundationList) throws Exception {
LinearLayout linearLayout = findViewById(R.id.foundationList);
for (final JsonNode foundation : foundationList) {
Button foundationButton = new Button(this);
if (!foundation.has("name") || !foundation.has("fun_id"))
throw new Exception("Unexpected JSON");
foundationButton.setText(foundation.get("name").textValue());
final String idFoundation = foundation.get("fun_id").textValue();
foundationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Log.d(LOG_TAG, idFoundation);
}
});
linearLayout.addView(foundationButton);
}
}
}
...@@ -3,6 +3,7 @@ package fr.utc.simde.payutc; ...@@ -3,6 +3,7 @@ package fr.utc.simde.payutc;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.os.Bundle; import android.os.Bundle;
...@@ -78,6 +79,7 @@ public class MainActivity extends BaseActivity { ...@@ -78,6 +79,7 @@ public class MainActivity extends BaseActivity {
@Override @Override
protected void unregister() { protected void unregister() {
super.unregister(); super.unregister();
((TextView) findViewById(R.id.text_app_registered)).setText(R.string.app_not_registred); ((TextView) findViewById(R.id.text_app_registered)).setText(R.string.app_not_registred);
} }
...@@ -95,9 +97,7 @@ public class MainActivity extends BaseActivity { ...@@ -95,9 +97,7 @@ public class MainActivity extends BaseActivity {
return; return;
} }
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_authentification), true); dialog.startLoading(MainActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_authentification));
loading.setCancelable(false);
new Thread() { new Thread() {
@Override @Override
public void run() { public void run() {
...@@ -111,7 +111,7 @@ public class MainActivity extends BaseActivity { ...@@ -111,7 +111,7 @@ public class MainActivity extends BaseActivity {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
loading.dismiss(); dialog.stopLoading();
if (nemopaySession.isRegistered()) { if (nemopaySession.isRegistered()) {
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
...@@ -128,11 +128,12 @@ public class MainActivity extends BaseActivity { ...@@ -128,11 +128,12 @@ public class MainActivity extends BaseActivity {
}.start(); }.start();
} }
protected void connectWithCAS(final String username, final String password) throws InterruptedException { protected void startFoundationListActivity() {
dialog.dismiss(); MainActivity.this.startActivity(new Intent(MainActivity.this, FoundationListActivity.class));
}
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getString(R.string.cas_connection), getString(R.string.cas_in_url), true); protected void connectWithCAS(final String username, final String password) throws InterruptedException {
loading.setCancelable(false); dialog.startLoading(MainActivity.this, getString(R.string.cas_connection), getString(R.string.cas_in_url));
new Thread() { new Thread() {
@Override @Override
public void run() { public void run() {
...@@ -154,11 +155,11 @@ public class MainActivity extends BaseActivity { ...@@ -154,11 +155,11 @@ public class MainActivity extends BaseActivity {
@Override @Override
public void run() { public void run() {
if (casConnexion.getUrl().equals("")) { if (casConnexion.getUrl().equals("")) {
loading.dismiss(); dialog.stopLoading();
dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_url)); dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_url));
} }
else else
loading.setMessage(getString(R.string.cas_in_connection)); dialog.changeLoading(getString(R.string.cas_in_connection));
} }
}); });
...@@ -176,9 +177,9 @@ public class MainActivity extends BaseActivity { ...@@ -176,9 +177,9 @@ public class MainActivity extends BaseActivity {
@Override @Override
public void run() { public void run() {
if (casConnexion.isConnected()) if (casConnexion.isConnected())
loading.setMessage(getString(R.string.cas_in_service_adding)); dialog.changeLoading(getString(R.string.cas_in_service_adding));
else { else {
loading.dismiss(); dialog.stopLoading();
dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_connection)); dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_connection));
} }
} }
...@@ -197,11 +198,10 @@ public class MainActivity extends BaseActivity { ...@@ -197,11 +198,10 @@ public class MainActivity extends BaseActivity {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
if (casConnexion.isServiceAdded()) if (casConnexion.isServiceAdded())
loading.setMessage(getString(R.string.nemopay_connection)); dialog.changeLoading(getString(R.string.nemopay_connection));
else { else {
loading.dismiss(); dialog.stopLoading();
dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_service_adding)); dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_service_adding));
} }
} }
...@@ -220,14 +220,14 @@ public class MainActivity extends BaseActivity { ...@@ -220,14 +220,14 @@ public class MainActivity extends BaseActivity {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
loading.dismiss(); dialog.stopLoading();
if (!nemopaySession.isConnected()) if (!nemopaySession.isConnected())
dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_service_linking)); dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_service_linking));
else if (!nemopaySession.isRegistered()) else if (!nemopaySession.isRegistered())
keyDialog(); keyDialog();
else else
Toast.makeText(MainActivity.this, "Tout est bon !", Toast.LENGTH_SHORT).show(); startFoundationListActivity();
} }
}); });
} }
...@@ -235,13 +235,10 @@ public class MainActivity extends BaseActivity { ...@@ -235,13 +235,10 @@ public class MainActivity extends BaseActivity {
} }
protected void connectWithBadge(final String idBadge, final String pin) { protected void connectWithBadge(final String idBadge, final String pin) {
dialog.dismiss();
if (!nemopaySession.isRegistered() || nemopaySession.isConnected()) if (!nemopaySession.isRegistered() || nemopaySession.isConnected())
return; return;
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getString(R.string.badge_dialog), getString(R.string.badge_recognization), true); dialog.startLoading(MainActivity.this, getString(R.string.badge_dialog), getString(R.string.badge_recognization));
loading.setCancelable(false);
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
...@@ -255,11 +252,11 @@ public class MainActivity extends BaseActivity { ...@@ -255,11 +252,11 @@ public class MainActivity extends BaseActivity {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
loading.dismiss(); dialog.stopLoading();
try { try {
if (nemopaySession.isConnected()) if (nemopaySession.isConnected())
Toast.makeText(MainActivity.this, "Tout est bon !", Toast.LENGTH_SHORT).show(); startFoundationListActivity();
else if (nemopaySession.getRequest().getResponseCode() == 400) else if (nemopaySession.getRequest().getResponseCode() == 400)
dialog.errorDialog(getString(R.string.badge_dialog), getString(R.string.badge_pin_error_not_recognized)); dialog.errorDialog(getString(R.string.badge_dialog), getString(R.string.badge_pin_error_not_recognized));
else else
......
...@@ -20,8 +20,6 @@ import android.support.v7.app.AlertDialog; ...@@ -20,8 +20,6 @@ import android.support.v7.app.AlertDialog;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import fr.utc.simde.payutc.R;
public abstract class NFCActivity extends Activity { public abstract class NFCActivity extends Activity {
private static final String LOG_TAG = "_NFCActivity"; private static final String LOG_TAG = "_NFCActivity";
private static NfcAdapter NFCAdapter; private static NfcAdapter NFCAdapter;
...@@ -34,9 +32,6 @@ public abstract class NFCActivity extends Activity { ...@@ -34,9 +32,6 @@ public abstract class NFCActivity extends Activity {
NFCAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext()); NFCAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext());
NFCAlertDialog = new AlertDialog.Builder(this); NFCAlertDialog = new AlertDialog.Builder(this);
IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
this.registerReceiver(NFCReceiver, filter);
if (NFCAdapter == null) { if (NFCAdapter == null) {
Toast.makeText(this, R.string.nfc_not_available, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.nfc_not_available, Toast.LENGTH_LONG).show();
NFCAlertDialog NFCAlertDialog
...@@ -53,6 +48,9 @@ public abstract class NFCActivity extends Activity { ...@@ -53,6 +48,9 @@ public abstract class NFCActivity extends Activity {
AlertDialog alertDialog = NFCAlertDialog.create(); AlertDialog alertDialog = NFCAlertDialog.create();
alertDialog.show(); alertDialog.show();
} }
IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
this.registerReceiver(NFCReceiver, filter);
} }
protected abstract void onIdentification(final String idBadge); protected abstract void onIdentification(final String idBadge);
...@@ -100,7 +98,7 @@ public abstract class NFCActivity extends Activity { ...@@ -100,7 +98,7 @@ public abstract class NFCActivity extends Activity {
NFCAdapter.disableForegroundDispatch(this); NFCAdapter.disableForegroundDispatch(this);
} }
public void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
this.unregisterReceiver(NFCReceiver); this.unregisterReceiver(NFCReceiver);
......
...@@ -43,12 +43,19 @@ public class CASConnexion { ...@@ -43,12 +43,19 @@ public class CASConnexion {
} }
public void disconnect() { public void disconnect() {
if (isConnected()) {
new Thread() {
@Override
public void run() {
HTTPRequest request = new HTTPRequest(url + "logout");
request.post();
}
}.start();
}
this.username = ""; this.username = "";
this.location = ""; this.location = "";
this.ticket = ""; this.ticket = "";
HTTPRequest request = new HTTPRequest(this.url + "logout");
request.post();
} }
public Boolean isConnected() { return !this.location.isEmpty(); } public Boolean isConnected() { return !this.location.isEmpty(); }
......
package fr.utc.simde.payutc.tools; package fr.utc.simde.payutc.tools;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context; import android.content.Context;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
...@@ -17,6 +18,7 @@ public class Dialog { ...@@ -17,6 +18,7 @@ public class Dialog {
private static Activity activity; private static Activity activity;
private static AlertDialog alertDialog; private static AlertDialog alertDialog;
private static AlertDialog.Builder alertDialogBuilder; private static AlertDialog.Builder alertDialogBuilder;
private static ProgressDialog loading;
public Dialog(final Activity activity) { public Dialog(final Activity activity) {
this.activity = activity; this.activity = activity;
...@@ -26,10 +28,14 @@ public class Dialog { ...@@ -26,10 +28,14 @@ public class Dialog {
if (this.alertDialog != null) if (this.alertDialog != null)
this.alertDialog.dismiss(); this.alertDialog.dismiss();
if (this.loading != null)
this.loading.dismiss();
this.alertDialog = null; this.alertDialog = null;
this.loading = null;
} }
public Boolean isShowing() { return this.alertDialog != null && this.alertDialog.isShowing(); } public Boolean isShowing() { return (this.alertDialog != null && this.alertDialog.isShowing()) || (this.loading != null && this.loading.isShowing()); }
public void createDialog() { createDialog((EditText) null); } public void createDialog() { createDialog((EditText) null); }
public void createDialog(AlertDialog.Builder alertDialogBuilder) { createDialog(alertDialogBuilder, null); } public void createDialog(AlertDialog.Builder alertDialogBuilder) { createDialog(alertDialogBuilder, null); }
...@@ -77,4 +83,17 @@ public class Dialog { ...@@ -77,4 +83,17 @@ public class Dialog {
createDialog(); createDialog();
} }
public void startLoading(Activity activity, final String title, final String message) {
dismiss();
this.loading = ProgressDialog.show(activity, title, message, true, false);
}
public void changeLoading(final String message) {
this.loading.setMessage(message);
}
public void stopLoading() {
this.loading.dismiss();
this.loading = null;
}
} }
...@@ -6,8 +6,10 @@ package fr.utc.simde.payutc.tools; ...@@ -6,8 +6,10 @@ package fr.utc.simde.payutc.tools;
import android.util.Log; import android.util.Log;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
...@@ -171,20 +173,35 @@ public class HTTPRequest { ...@@ -171,20 +173,35 @@ public class HTTPRequest {
this.response = builder.toString(); this.response = builder.toString();
} }
public JSONObject getJsonResponse() throws IOException, JSONException { return new JSONObject(response); } public Boolean isJsonResponse() throws Exception {
public String getResponse() throws IOException { return response; } if (request == null)
return null;
public Boolean isJsonResponse() { if (request.getContentType().equals("application/json")) {
try { try {
new JSONObject(response); new ObjectMapper().readTree(this.response);
return true;
} catch (IOException e) {
throw new Exception("Malformed JSON");
}
} }
catch (Exception e) { else {
return false; return false;
} }
}
return true; public JsonNode getJsonResponse() throws IOException, JSONException {
try {
return new ObjectMapper().readTree(this.response);
}
catch (Exception e) {
Log.e(LOG_TAG, "error: " + e.getMessage());
return null;
}
} }
public String getResponse() throws IOException { return response; }
protected String args2String(Map<String, String> args) throws UnsupportedEncodingException { return args2String(args, false); } protected String args2String(Map<String, String> args) throws UnsupportedEncodingException { return args2String(args, false); }
protected String args2String(Map<String, String> args, Boolean isGet) throws UnsupportedEncodingException { protected String args2String(Map<String, String> args, Boolean isGet) throws UnsupportedEncodingException {
String data = ""; String data = "";
......
package fr.utc.simde.payutc.tools; package fr.utc.simde.payutc.tools;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.util.Log; import android.util.Log;
import com.fasterxml.jackson.databind.JsonNode;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;