Commit 425c34d7 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Merge branch 'feature/connection' into develop

parents 54d95874 741a5f80
...@@ -14,14 +14,10 @@ import android.widget.EditText; ...@@ -14,14 +14,10 @@ import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import org.json.JSONException;
import java.io.IOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import fr.utc.simde.payutc.tools.HTTPRequest;
import fr.utc.simde.payutc.tools.NFCActivity; import fr.utc.simde.payutc.tools.NFCActivity;
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;
...@@ -63,8 +59,8 @@ public class MainActivity extends NFCActivity { ...@@ -63,8 +59,8 @@ public class MainActivity extends NFCActivity {
public boolean onLongClick(View v) { public boolean onLongClick(View v) {
if (!nemopaySession.isRegistered()) if (!nemopaySession.isRegistered())
addKeyDialog(); addKeyDialog();
else // A supprimer = embêtant si les clés sont réinitialisées else
delKey(); unregister();
return false; return false;
} }
...@@ -73,7 +69,7 @@ public class MainActivity extends NFCActivity { ...@@ -73,7 +69,7 @@ public class MainActivity extends NFCActivity {
usernameButton.setOnClickListener(new View.OnClickListener() { usernameButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
connectDialog(); casDialog();
} }
}); });
} }
...@@ -84,10 +80,25 @@ public class MainActivity extends NFCActivity { ...@@ -84,10 +80,25 @@ public class MainActivity extends NFCActivity {
badgeDialog(idBadge); badgeDialog(idBadge);
} }
protected void disconnect() {
nemopaySession.disconnect();
casConnexion.disconnect();
}
protected void unregister() {
nemopaySession.unregister();
disconnect();
((TextView) findViewById(R.id.text_app_registered)).setText(R.string.app_not_registred);
dialog.errorDialog(getResources().getString(R.string.key_registration), getResources().getString(R.string.key_remove_temp));
}
protected void delKey() { protected void delKey() {
SharedPreferences.Editor edit = sharedPreferences.edit(); SharedPreferences.Editor edit = sharedPreferences.edit();
edit.remove("key"); edit.remove("key");
edit.apply(); edit.apply();
unregister();
} }
protected void setKey(final String key) { protected void setKey(final String key) {
...@@ -250,7 +261,7 @@ public class MainActivity extends NFCActivity { ...@@ -250,7 +261,7 @@ public class MainActivity extends NFCActivity {
} }
if (nemopaySession.isConnected()) { if (nemopaySession.isConnected()) {
dialog.errorDialog(getResources().getString(R.string.badge_connection), getResources().getString(R.string.already_connected)); dialog.errorDialog(getResources().getString(R.string.badge_connection), getResources().getString(R.string.already_connected) + " " + nemopaySession.getUsername());
return; return;
} }
...@@ -285,9 +296,9 @@ public class MainActivity extends NFCActivity { ...@@ -285,9 +296,9 @@ public class MainActivity extends NFCActivity {
dialog.createDialog(alertDialogBuilder, pinInput); dialog.createDialog(alertDialogBuilder, pinInput);
} }
protected void connectDialog() { protected void casDialog() {
if (nemopaySession.isConnected()) { if (nemopaySession.isConnected()) {
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.already_connected)); dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.already_connected) + " " + nemopaySession.getUsername());
return; return;
} }
...@@ -310,7 +321,7 @@ public class MainActivity extends NFCActivity { ...@@ -310,7 +321,7 @@ public class MainActivity extends NFCActivity {
Toast.makeText(MainActivity.this, R.string.username_and_password_required, Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this, R.string.username_and_password_required, Toast.LENGTH_SHORT).show();
dialogInterface.cancel(); dialogInterface.cancel();
connectDialog(); casDialog();
} }
else { else {
try { try {
...@@ -399,7 +410,8 @@ public class MainActivity extends NFCActivity { ...@@ -399,7 +410,8 @@ public class MainActivity extends NFCActivity {
public void onClick(DialogInterface dialogInterface, int id) { public void onClick(DialogInterface dialogInterface, int id) {
setKey(keyInput.getText().toString()); setKey(keyInput.getText().toString());
} }
}); })
.setNegativeButton(R.string.cancel, null);
dialog.createDialog(alertDialogBuilder, keyInput); dialog.createDialog(alertDialogBuilder, keyInput);
} }
......
...@@ -59,6 +59,12 @@ public class CASConnexion { ...@@ -59,6 +59,12 @@ public class CASConnexion {
throw new RuntimeException("Not Connected"); throw new RuntimeException("Not Connected");
} }
public void disconnect() {
this.username = "";
this.location = "";
this.ticket = "";
}
public Boolean isConnected() { return !this.location.isEmpty(); } public Boolean isConnected() { return !this.location.isEmpty(); }
public void addService(final String service) throws Exception { public void addService(final String service) throws Exception {
......
...@@ -78,7 +78,12 @@ public class HTTPRequest { ...@@ -78,7 +78,12 @@ public class HTTPRequest {
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse()); Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse());
} }
catch (Exception e) { catch (Exception e) {
Log.e(LOG_TAG, e.getMessage()); try {
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", error: " + e.getMessage());
}
catch (Exception e2) {
Log.e(LOG_TAG, e.getMessage());
}
} }
return getResponseCode(); return getResponseCode();
...@@ -115,7 +120,12 @@ public class HTTPRequest { ...@@ -115,7 +120,12 @@ public class HTTPRequest {
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse()); Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse());
} }
catch (Exception e) { catch (Exception e) {
Log.e(LOG_TAG, e.getMessage()); try {
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", error: " + e.getMessage());
}
catch (Exception e2) {
Log.e(LOG_TAG, e.getMessage());
}
} }
return getResponseCode(); return getResponseCode();
...@@ -179,6 +189,17 @@ public class HTTPRequest { ...@@ -179,6 +189,17 @@ public class HTTPRequest {
public Map<String, String> getJsonResponse() throws IOException, JSONException { return jsonToMap(response); } public Map<String, String> getJsonResponse() throws IOException, JSONException { return jsonToMap(response); }
public String getResponse() throws IOException { return response; } public String getResponse() throws IOException { return response; }
public Boolean isJsonResponse() {
try {
jsonToMap(response);
}
catch (Exception e) {
return false;
}
return true;
}
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 = "";
......
...@@ -50,8 +50,22 @@ public class NemopaySession { ...@@ -50,8 +50,22 @@ public class NemopaySession {
public Boolean isConnected() { return !this.session.isEmpty() && !this.username.isEmpty(); } public Boolean isConnected() { return !this.session.isEmpty() && !this.username.isEmpty(); }
public Boolean isRegistered() { return !this.name.isEmpty() && !this.key.isEmpty() && !this.session.isEmpty(); } public Boolean isRegistered() { return !this.name.isEmpty() && !this.key.isEmpty() && !this.session.isEmpty(); }
public void disconnect() {
this.session = "";
this.username = "";
}
public void unregister() {
this.name = "";
this.key = "";
disconnect();
}
public String getName() { return this.name; } public String getName() { return this.name; }
public String getKey() { return this.key; } public String getKey() { return this.key; }
public String getUsername() { return username; }
public HTTPRequest getRequest() { return this.request; }
public void getCasUrl() throws IOException { public void getCasUrl() throws IOException {
request("POSS3", "getCasUrl"); request("POSS3", "getCasUrl");
...@@ -64,7 +78,7 @@ public class NemopaySession { ...@@ -64,7 +78,7 @@ public class NemopaySession {
put("app_desc", description); put("app_desc", description);
}}); }});
if (request.getResponseCode() == 200) if (request.getResponseCode() == 200 && request.isJsonResponse())
this.key = request.getJsonResponse().get("app_key"); this.key = request.getJsonResponse().get("app_key");
this.request = request; this.request = request;
...@@ -75,7 +89,11 @@ public class NemopaySession { ...@@ -75,7 +89,11 @@ public class NemopaySession {
put("key", key); put("key", key);
}}); }});
Map<String, String> response = request.getJsonResponse(); Map<String, String> response;
if (request.getResponseCode() == 200 && request.isJsonResponse())
response = request.getJsonResponse();
else
throw new Exception("Not authentified");
if (response.containsKey("sessionid") && response.containsKey("name")) { if (response.containsKey("sessionid") && response.containsKey("name")) {
this.session = response.get("sessionid"); this.session = response.get("sessionid");
...@@ -83,7 +101,7 @@ public class NemopaySession { ...@@ -83,7 +101,7 @@ public class NemopaySession {
this.key = key; this.key = key;
} }
else else
throw new Exception("Not authentified"); throw new Exception("No correct informations");
} }
public void loginBadge(final String idBadge, final String pin) throws Exception { public void loginBadge(final String idBadge, final String pin) throws Exception {
...@@ -92,15 +110,18 @@ public class NemopaySession { ...@@ -92,15 +110,18 @@ public class NemopaySession {
put("pin", pin); put("pin", pin);
}}); }});
Map<String, String> response = request.getJsonResponse(); Map<String, String> response;
if (request.getResponseCode() == 200 && request.isJsonResponse())
response = request.getJsonResponse();
else
throw new Exception("Not connected");
if (response.containsKey("sessionid") && response.containsKey("username")) { if (response.containsKey("sessionid") && response.containsKey("username")) {
this.session = response.get("sessionid"); this.session = response.get("sessionid");
this.username = response.get("username"); this.username = response.get("username");
} }
else { else
throw new Exception("Not connected"); throw new Exception("No correct informations");
}
} }
public void loginCas(final String ticket, final String service) throws Exception { public void loginCas(final String ticket, final String service) throws Exception {
...@@ -109,18 +130,20 @@ public class NemopaySession { ...@@ -109,18 +130,20 @@ public class NemopaySession {
put("service", service); put("service", service);
}}); }});
Map<String, String> response = request.getJsonResponse(); Map<String, String> response;
if (request.getResponseCode() == 200 && request.isJsonResponse())
response = request.getJsonResponse();
else
throw new Exception("Not connected");
if (response.containsKey("sessionid") && response.containsKey("username")) { if (response.containsKey("sessionid") && response.containsKey("username")) {
this.session = response.get("sessionid"); this.session = response.get("sessionid");
this.username = response.get("username"); this.username = response.get("username");
} }
else else
throw new Exception("Not connected"); throw new Exception("No correct informations");
} }
public HTTPRequest getRequest() { return this.request; }
protected void request(final String method, final String service) throws IOException { request(method, service, new HashMap<String, String>()); } protected void request(final String method, final String service) throws IOException { request(method, service, new HashMap<String, String>()); }
protected void request(final String method, final String service, final Map<String, String> postArgs) throws IOException { protected void request(final String method, final String service, final Map<String, String> postArgs) throws IOException {
HTTPRequest request = new HTTPRequest(url + method + "/" + service); HTTPRequest request = new HTTPRequest(url + method + "/" + service);
......
...@@ -50,5 +50,6 @@ ...@@ -50,5 +50,6 @@
<string name="key_description">Description</string> <string name="key_description">Description</string>
<string name="key_name_required">Nom requis !</string> <string name="key_name_required">Nom requis !</string>
<string name="key_add_explication">Il est possible d\'ajouter directement une clé d\'application si celle-ci est connue</string> <string name="key_add_explication">Il est possible d\'ajouter directement une clé d\'application si celle-ci est connue</string>
<string name="already_connected">Déjà connecté !</string> <string name="key_remove_temp">L\'application a été désauthentifiée temporairement. Il suffit de relancer l\'application pour qu\'elle soit de nouveau connectée</string>
<string name="already_connected">Déjà connecté sous</string>
</resources> </resources>
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