Commit eb954b0f authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Popup creation

Creation of the popup
Auto add date
parent 04b291a0
......@@ -15,6 +15,10 @@ import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import fr.utc.simde.payutc.tools.HTTPRequest;
import fr.utc.simde.payutc.tools.NFCActivity;
......@@ -119,16 +123,20 @@ public class MainActivity extends NFCActivity {
HTTPRequest request = nemopaySession.loginCas(casConnexion.getTicket(), service);
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
Log.e(LOG_TAG, e.getMessage());
}
runOnUiThread(new Runnable() {
@Override
public void run() {
loading.dismiss();
/*
if (casConnexion.isServiceAdded())
loading.setMessage(getResources().getString(R.string.nemopay_connection));*/
if (!nemopaySession.isConnected())
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_service_linking));
else if (!nemopaySession.isRegistered())
keyDialog();
else
Toast.makeText(MainActivity.this, "Tout est bon !", Toast.LENGTH_SHORT).show();
}
});
}
......@@ -233,4 +241,35 @@ public class MainActivity extends NFCActivity {
dialog.createDialog(alertDialogBuilder, usernameInput.getText().toString().isEmpty() ? usernameInput : passwordInput);
}
protected void keyDialog() {
final View usernameView = getLayoutInflater().inflate(R.layout.dialog_key, null);
final EditText keyInput = usernameView.findViewById(R.id.input_key);
final String date = new SimpleDateFormat("yyyy/MM/dd", Locale.FRANCE).format(new Date());
keyInput.setText("Téléphone de " + casConnexion.getUsername() + " - " + date);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder
.setTitle(R.string.key_dialog)
.setView(usernameView)
.setCancelable(false)
.setPositiveButton(R.string.register, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (keyInput.getText().toString().equals("")) {
Toast.makeText(MainActivity.this, R.string.key_name_required, Toast.LENGTH_SHORT).show();
dialog.cancel();
keyDialog();
}
else {
/* AddKey */
Log.d(LOG_TAG, "name: " + keyInput.getText().toString() + (keyInput.getText().toString().matches("^.* - ([0-9]{4})([/-])([0-9]{2})\\2([0-9]{2})$") ? "" : " - " + date));
dialog.cancel();
keyDialog();
}
}
});
dialog.createDialog(alertDialogBuilder, keyInput);
}
}
......@@ -6,6 +6,9 @@ package fr.utc.simde.payutc.tools;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
......@@ -14,6 +17,7 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
......@@ -36,6 +40,20 @@ public class HTTPRequest {
this.cookies = new HashMap<String, String>();
}
public static Map<String, String> jsonToMap(String t) throws JSONException {
Map<String, String> map = new HashMap<String, String>();
JSONObject jObject = new JSONObject(t);
Iterator<?> keys = jObject.keys();
while (keys.hasNext()){
String key = (String) keys.next();
String value = jObject.getString(key);
map.put(key, value);
}
return map;
}
public int get() throws IOException {
String get = args2String(this.getArgs, true);
Log.d(LOG_TAG, "get: " + this.url + get);
......@@ -114,6 +132,7 @@ public class HTTPRequest {
this.response = builder.toString();
}
public Map<String, String> getJsonResponse() throws IOException, JSONException { return jsonToMap(response); }
public String getResponse() throws IOException { return response; }
protected String args2String(Map<String, String> args) throws UnsupportedEncodingException { return args2String(args, false); }
......
......@@ -2,10 +2,18 @@ package fr.utc.simde.payutc.tools;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Created by Samy on 24/10/2017.
......@@ -25,17 +33,32 @@ public class NemopaySession {
public NemopaySession() {
this.session = "";
this.username = "";
this.key = "";
}
public Boolean isConnected() { return !this.session.isEmpty() && !this.username.isEmpty(); }
public Boolean isRegistered() { return !this.key.isEmpty() && !this.session.isEmpty(); }
public HTTPRequest getCasUrl() throws IOException {
return construct("POSS3", "getCasUrl");
}
public HTTPRequest loginCas(final String ticket, final String service) throws IOException {
return construct("POSS3", "loginCas2", new HashMap<String, String>() {{
public HTTPRequest loginCas(final String ticket, final String service) throws Exception {
HTTPRequest request = construct("POSS3", "loginCas2", new HashMap<String, String>() {{
put("ticket", ticket);
put("service", service);
}});
Map<String, String> response = request.getJsonResponse();
if (response.containsKey("sessionid") && response.containsKey("username")) {
this.session = response.get("sessionid");
this.username = response.get("username");
}
else
throw new Exception("Not connected");
return request;
}
protected HTTPRequest construct(final String method, final String service) throws IOException { return construct(method, service, new HashMap<String, String>()); }
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_key_explication"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:text="@string/key_explication"
android:layout_marginBottom="10dp" />
<EditText
android:id="@+id/input_key"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:inputType="textPersonName"
android:hint="@string/key_name" />
</LinearLayout>
\ No newline at end of file
......@@ -5,6 +5,7 @@
<string name="app_not_configured">Par défaut</string>
<string name="ok">Ok</string>
<string name="connexion">Se connecter</string>
<string name="register">Enregistrer</string>
<string name="activate">Activer</string>
<string name="pass">Continuer</string>
<string name="erase">Effacer</string>
......@@ -31,7 +32,12 @@
<string name="cas_error_connection">Login et/ou mot de passe non reconnu par le CAS</string>
<string name="cas_error_service_adding">Une erreur a été détectée lors de l\'ajout du service</string>
<string name="cas_error_service_connection">Erreur lors de la connexion au service depuis le CAS</string>
<string name="cas_error_service_linking">Erreur lors de la connexion entre le CAS et le service</string>
<string name="badge_connection">Connexion via badge</string>
<string name="badge_app_not_registered">Il est nécessaire que l\'application soit enregistrée pour permettre la connexion via badge</string>
<string name="nemopay_connection">Connexion à Nemopay</string>
<string name="key_dialog">Enregistrement de l\'appareil</string>
<string name="key_explication">Pour pouvoir fonctionner, l\'application a besoin d\'être enregistrée sous un nom précis et daté (date automatiquement ajoutée si non détectée). Ceci permettra par la suite de donner des droits à cet appareil</string>
<string name="key_name">Nom - Date</string>
<string name="key_name_required">Nom requis !</string>
</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