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 @@
</intent-filter>
</activity>
<activity android:name=".BaseActivity" />
<activity android:name=".FoundationListActivity" />
</application>
</manifest>
\ No newline at end of file
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.Dialog;
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;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
......@@ -78,6 +79,7 @@ public class MainActivity extends BaseActivity {
@Override
protected void unregister() {
super.unregister();
((TextView) findViewById(R.id.text_app_registered)).setText(R.string.app_not_registred);
}
......@@ -95,9 +97,7 @@ public class MainActivity extends BaseActivity {
return;
}
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_authentification), true);
loading.setCancelable(false);
dialog.startLoading(MainActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_authentification));
new Thread() {
@Override
public void run() {
......@@ -111,7 +111,7 @@ public class MainActivity extends BaseActivity {
runOnUiThread(new Runnable() {
@Override
public void run() {
loading.dismiss();
dialog.stopLoading();
if (nemopaySession.isRegistered()) {
SharedPreferences.Editor editor = sharedPreferences.edit();
......@@ -128,11 +128,12 @@ public class MainActivity extends BaseActivity {
}.start();
}
protected void connectWithCAS(final String username, final String password) throws InterruptedException {
dialog.dismiss();
protected void startFoundationListActivity() {
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);
loading.setCancelable(false);
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() {
@Override
public void run() {
......@@ -154,11 +155,11 @@ public class MainActivity extends BaseActivity {
@Override
public void run() {
if (casConnexion.getUrl().equals("")) {
loading.dismiss();
dialog.stopLoading();
dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_url));
}
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 {
@Override
public void run() {
if (casConnexion.isConnected())
loading.setMessage(getString(R.string.cas_in_service_adding));
dialog.changeLoading(getString(R.string.cas_in_service_adding));
else {
loading.dismiss();
dialog.stopLoading();
dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_connection));
}
}
......@@ -197,11 +198,10 @@ public class MainActivity extends BaseActivity {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (casConnexion.isServiceAdded())
loading.setMessage(getString(R.string.nemopay_connection));
dialog.changeLoading(getString(R.string.nemopay_connection));
else {
loading.dismiss();
dialog.stopLoading();
dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_service_adding));
}
}
......@@ -220,14 +220,14 @@ public class MainActivity extends BaseActivity {
runOnUiThread(new Runnable() {
@Override
public void run() {
loading.dismiss();
dialog.stopLoading();
if (!nemopaySession.isConnected())
dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_service_linking));
else if (!nemopaySession.isRegistered())
keyDialog();
else
Toast.makeText(MainActivity.this, "Tout est bon !", Toast.LENGTH_SHORT).show();
startFoundationListActivity();
}
});
}
......@@ -235,13 +235,10 @@ public class MainActivity extends BaseActivity {
}
protected void connectWithBadge(final String idBadge, final String pin) {
dialog.dismiss();
if (!nemopaySession.isRegistered() || nemopaySession.isConnected())
return;
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getString(R.string.badge_dialog), getString(R.string.badge_recognization), true);
loading.setCancelable(false);
dialog.startLoading(MainActivity.this, getString(R.string.badge_dialog), getString(R.string.badge_recognization));
new Thread(new Runnable() {
@Override
public void run() {
......@@ -255,11 +252,11 @@ public class MainActivity extends BaseActivity {
runOnUiThread(new Runnable() {
@Override
public void run() {
loading.dismiss();
dialog.stopLoading();
try {
if (nemopaySession.isConnected())
Toast.makeText(MainActivity.this, "Tout est bon !", Toast.LENGTH_SHORT).show();
startFoundationListActivity();
else if (nemopaySession.getRequest().getResponseCode() == 400)
dialog.errorDialog(getString(R.string.badge_dialog), getString(R.string.badge_pin_error_not_recognized));
else
......
......@@ -20,8 +20,6 @@ import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;
import fr.utc.simde.payutc.R;
public abstract class NFCActivity extends Activity {
private static final String LOG_TAG = "_NFCActivity";
private static NfcAdapter NFCAdapter;
......@@ -34,9 +32,6 @@ public abstract class NFCActivity extends Activity {
NFCAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext());
NFCAlertDialog = new AlertDialog.Builder(this);
IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
this.registerReceiver(NFCReceiver, filter);
if (NFCAdapter == null) {
Toast.makeText(this, R.string.nfc_not_available, Toast.LENGTH_LONG).show();
NFCAlertDialog
......@@ -53,6 +48,9 @@ public abstract class NFCActivity extends Activity {
AlertDialog alertDialog = NFCAlertDialog.create();
alertDialog.show();
}
IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
this.registerReceiver(NFCReceiver, filter);
}
protected abstract void onIdentification(final String idBadge);
......@@ -100,7 +98,7 @@ public abstract class NFCActivity extends Activity {
NFCAdapter.disableForegroundDispatch(this);
}
public void onDestroy() {
protected void onDestroy() {
super.onDestroy();
this.unregisterReceiver(NFCReceiver);
......
......@@ -43,12 +43,19 @@ public class CASConnexion {
}
public void disconnect() {
if (isConnected()) {
new Thread() {
@Override
public void run() {
HTTPRequest request = new HTTPRequest(url + "logout");
request.post();
}
}.start();
}
this.username = "";
this.location = "";
this.ticket = "";
HTTPRequest request = new HTTPRequest(this.url + "logout");
request.post();
}
public Boolean isConnected() { return !this.location.isEmpty(); }
......
package fr.utc.simde.payutc.tools;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.inputmethod.InputMethodManager;
......@@ -17,6 +18,7 @@ public class Dialog {
private static Activity activity;
private static AlertDialog alertDialog;
private static AlertDialog.Builder alertDialogBuilder;
private static ProgressDialog loading;
public Dialog(final Activity activity) {
this.activity = activity;
......@@ -26,10 +28,14 @@ public class Dialog {
if (this.alertDialog != null)
this.alertDialog.dismiss();
if (this.loading != null)
this.loading.dismiss();
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(AlertDialog.Builder alertDialogBuilder) { createDialog(alertDialogBuilder, null); }
......@@ -77,4 +83,17 @@ public class Dialog {
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;
import android.util.Log;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
......@@ -171,20 +173,35 @@ public class HTTPRequest {
this.response = builder.toString();
}
public JSONObject getJsonResponse() throws IOException, JSONException { return new JSONObject(response); }
public String getResponse() throws IOException { return response; }
public Boolean isJsonResponse() throws Exception {
if (request == null)
return null;
public Boolean isJsonResponse() {
if (request.getContentType().equals("application/json")) {
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 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, Boolean isGet) throws UnsupportedEncodingException {
String data = "";
......
package fr.utc.simde.payutc.tools;
import android.app.Activity;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.util.Log;
import com.fasterxml.jackson.databind.JsonNode;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import fr.utc.simde.payutc.R;
import static java.lang.System.in;
/**
* Created by Samy on 24/10/2017.
*/
......@@ -57,8 +50,10 @@ public class NemopaySession {
public Boolean isRegistered() { return !this.name.isEmpty() && !this.key.isEmpty() && !this.session.isEmpty(); }
public void disconnect() {
this.session = "";
this.username = "";
if (!isRegistered())
this.session = "";
}
public void unregister() {
......@@ -73,19 +68,42 @@ public class NemopaySession {
public String getUsername() { return username; }
public HTTPRequest getRequest() { return this.request; }
public int getFoundations() throws IOException, JSONException {
return request(
"POSS3",
"getFundations",
new String[]{
"sale"
}
);
}
public int getCASUrl() throws IOException {
return request("POSS3", "getCasUrl");
return request(
"POSS3",
"getCasUrl"
);
}
public int registerApp(final String name, final String description, final String service) throws IOException, JSONException {
int reponseCode = request("KEY", "registerApplication", new HashMap<String, String>() {{
public int registerApp(final String name, final String description, final String service) throws Exception {
int reponseCode = request(
"KEY",
"registerApplication",
new HashMap<String, String>() {{
put("app_url", service);
put("app_name", name);
put("app_desc", description);
}});
}}
);
if (reponseCode == 200 && this.request.isJsonResponse())
this.key = this.request.getJsonResponse().getString("app_key");
if (reponseCode != 200 || !this.request.isJsonResponse())
throw new Exception("Not created");
JsonNode response = this.request.getJsonResponse();
if (response.has("app_key"))
this.key = response.get("app_key").textValue();
else
throw new Exception("Unexpected JSON");
return reponseCode;
}
......@@ -93,78 +111,91 @@ public class NemopaySession {
public int loginApp(final String key, CASConnexion casConnexion) throws Exception {
int reponseCode = loginApp(key);
JSONObject response = getRequest().getJsonResponse();
if (response.has("config") && ((JSONObject) response.get("config")).has("cas_url"))
casConnexion.setUrl(((JSONObject) response.get("config")).getString("cas_url"));
JsonNode response = this.request.getJsonResponse();
if (response.has("config") && response.get("config").has("cas_url"))
casConnexion.setUrl(response.get("config").get("cas_url").textValue());
else
throw new Exception("No correct informations");
throw new Exception("Unexpected JSON");
return reponseCode;
}
public int loginApp(final String key) throws Exception {
int reponseCode = request("POSS3", "loginApp", new HashMap<String, String>() {{
int reponseCode = request(
"POSS3",
"loginApp",
new HashMap<String, String>() {{
put("key", key);
}});
}}
);
JSONObject response;
JsonNode response;
if (reponseCode == 200 && this.request.isJsonResponse())
response = this.request.getJsonResponse();
else
throw new Exception("Not authentified");
if (response.has("sessionid") && response.has("name")) {
this.session = response.getString("sessionid");
this.name = response.getString("name");
this.session = response.get("sessionid").textValue();
this.name = response.get("name").textValue();
this.key = key;
}
else
throw new Exception("No correct informations");
throw new Exception("Unexpected JSON");
return reponseCode;
}
public int loginBadge(final String idBadge, final String pin) throws Exception {
int reponseCode = request("POSS3", "loginBadge2", new HashMap<String, String>() {{
int reponseCode = request(
"POSS3",
"loginBadge2",
new HashMap<String, String>() {{
put("badge_id", idBadge);
put("pin", pin);
}}, new String[]{
}},
new String[]{
"sale"
});
}
);
JSONObject response;
JsonNode response;
if (reponseCode == 200 && this.request.isJsonResponse())
response = this.request.getJsonResponse();
else
throw new Exception("Not connected");
if (response.has("sessionid") && response.has("username")) {
this.session = response.getString("sessionid");
this.username = response.getString("username");
this.session = response.get("sessionid").textValue();
this.username = response.get("username").textValue();
}