Commit 60e86bd2 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Cleaning

Move loading in Dialog class
Better JSON management
parent f103c9be
...@@ -4,6 +4,9 @@ import android.app.ProgressDialog; ...@@ -4,6 +4,9 @@ import android.app.ProgressDialog;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
import fr.utc.simde.payutc.tools.HTTPRequest; import fr.utc.simde.payutc.tools.HTTPRequest;
/** /**
...@@ -18,8 +21,7 @@ public class FoundationListActivity extends BaseActivity { ...@@ -18,8 +21,7 @@ public class FoundationListActivity extends BaseActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_foundation_list); setContentView(R.layout.activity_foundation_list);
final ProgressDialog loading = ProgressDialog.show(FoundationListActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_authentification), true); dialog.startLoading(FoundationListActivity.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() {
...@@ -34,11 +36,13 @@ public class FoundationListActivity extends BaseActivity { ...@@ -34,11 +36,13 @@ public class FoundationListActivity extends BaseActivity {
@Override @Override
public void run() { public void run() {
HTTPRequest request = nemopaySession.getRequest(); HTTPRequest request = nemopaySession.getRequest();
loading.dismiss(); dialog.stopLoading();
try { try {
if (request.getResponseCode() == 200 && request.isJsonResponse()) if (request.getResponseCode() == 200 && request.isJsonResponse())
return; // setFoundationList(request.getJsonResponse(); Log.d(LOG_TAG, "Liste acquise");
// setFoundationList(request.getJsonResponse();
else else
dialog.errorDialog(getString(R.string.information_collection), getString(R.string.foundation_error_get_list)); dialog.errorDialog(getString(R.string.information_collection), getString(R.string.foundation_error_get_list));
} catch (Exception e) { } catch (Exception e) {
......
...@@ -97,8 +97,7 @@ public class MainActivity extends BaseActivity { ...@@ -97,8 +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() {
...@@ -112,7 +111,7 @@ public class MainActivity extends BaseActivity { ...@@ -112,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();
...@@ -134,10 +133,7 @@ public class MainActivity extends BaseActivity { ...@@ -134,10 +133,7 @@ public class MainActivity extends BaseActivity {
} }
protected void connectWithCAS(final String username, final String password) throws InterruptedException { protected void connectWithCAS(final String username, final String password) throws InterruptedException {
dialog.dismiss(); dialog.startLoading(MainActivity.this, getString(R.string.cas_connection), getString(R.string.cas_in_url));
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getString(R.string.cas_connection), getString(R.string.cas_in_url), true);
loading.setCancelable(false);
new Thread() { new Thread() {
@Override @Override
public void run() { public void run() {
...@@ -159,11 +155,11 @@ public class MainActivity extends BaseActivity { ...@@ -159,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));
} }
}); });
...@@ -181,9 +177,9 @@ public class MainActivity extends BaseActivity { ...@@ -181,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));
} }
} }
...@@ -202,11 +198,10 @@ public class MainActivity extends BaseActivity { ...@@ -202,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));
} }
} }
...@@ -225,7 +220,7 @@ public class MainActivity extends BaseActivity { ...@@ -225,7 +220,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.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));
...@@ -240,13 +235,10 @@ public class MainActivity extends BaseActivity { ...@@ -240,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() {
...@@ -260,7 +252,7 @@ public class MainActivity extends BaseActivity { ...@@ -260,7 +252,7 @@ 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())
......
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,6 +6,7 @@ package fr.utc.simde.payutc.tools; ...@@ -6,6 +6,7 @@ package fr.utc.simde.payutc.tools;
import android.util.Log; import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -171,20 +172,24 @@ public class HTTPRequest { ...@@ -171,20 +172,24 @@ public class HTTPRequest {
this.response = builder.toString(); this.response = builder.toString();
} }
public JSONObject getJsonResponse() throws IOException, JSONException { return new JSONObject(response); }
public String getResponse() throws IOException { return response; }
public Boolean isJsonResponse() { public Boolean isJsonResponse() {
if (request == null)
return null;
return request.getContentType() == "application/json";
}
public JSONObject getJsonResponse() throws IOException, JSONException {
try { try {
new JSONObject(response); return new JSONObject(response);
} }
catch (Exception e) { catch (Exception e) {
return false; return new JSONArray(response);
} }
return true;
} }
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 = "";
......
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