diff --git a/APED/.idea/gradle.xml b/APED/.idea/gradle.xml
index 23a89bb..7b3ed48 100644
--- a/APED/.idea/gradle.xml
+++ b/APED/.idea/gradle.xml
@@ -10,8 +10,8 @@
diff --git a/APED/app/src/main/java/com/example/aped/communication/Communicator.java b/APED/app/src/main/java/com/example/aped/communication/Communicator.java
index f10211d..2441268 100644
--- a/APED/app/src/main/java/com/example/aped/communication/Communicator.java
+++ b/APED/app/src/main/java/com/example/aped/communication/Communicator.java
@@ -1,13 +1,24 @@
package com.example.aped.communication;
import android.content.Context;
+import android.graphics.Color;
import android.util.Log;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.TextView;
+import android.widget.Toast;
+import com.android.volley.NoConnectionError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
+import com.android.volley.ServerError;
+import com.android.volley.TimeoutError;
+import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
+import com.example.aped.MainActivity;
+import com.example.aped.R;
import com.example.aped.utils.ExternalStorageHandler;
import org.json.JSONObject;
@@ -29,21 +40,21 @@ public class Communicator {
/** Request queue used for asynchronous requests. **/
private final RequestQueue requestQueue;
/**The context of the application.**/
- private final Context context;
+ private final MainActivity mainActivity;
/**
* Constructor of the Communicator.
* @param pAddress the ipv4 address of the raspberry
* @param pPort the port of the raspberry
- * @param pContext the context of the application
+ * @param pMainActivity the main activity
*/
public Communicator(final String pAddress,
final int pPort,
- final Context pContext) {
+ final MainActivity pMainActivity) {
this.address = pAddress;
this.port = pPort;
- requestQueue = Volley.newRequestQueue(pContext);
- this.context = pContext;
+ requestQueue = Volley.newRequestQueue(pMainActivity);
+ this.mainActivity = pMainActivity;
}
/**
@@ -62,8 +73,7 @@ public class Communicator {
url,
null,
responseListener,
- error -> Log.e("Communicator",
- "Error during READ: " + error.getMessage())
+ error -> handleError(error, "READ")
);
requestQueue.add(request);
}
@@ -86,8 +96,7 @@ public class Communicator {
url,
message,
responseListener,
- error -> Log.e("Communicator",
- "Error during WRITE: " + error.getMessage())
+ error -> handleError(error, "WRITE")
);
requestQueue.add(request);
}
@@ -97,15 +106,14 @@ public class Communicator {
*/
public void uploadXML() throws FileNotFoundException {
File xmlFile = new File(ExternalStorageHandler
- .getExternalPrivateStorageDir(context), "config.xml");
+ .getExternalPrivateStorageDir(mainActivity), "config.xml");
String url = "http://" + address + ":" + port + "/XML/";
FileOutputVolleyRequest request = new FileOutputVolleyRequest(
Request.Method.POST,
url,
xmlFile,
response -> System.out.println(response.toString()),
- error -> Log.e("Communicator",
- "Error during XML UPLOAD" + error.getMessage())
+ error -> handleError(error, "UPLOAD XML")
);
requestQueue.add(request);
}
@@ -122,10 +130,8 @@ public class Communicator {
try {
if (response != null) {
String xmlPath = ExternalStorageHandler
- .getExternalPrivateStorageDir(context);
+ .getExternalPrivateStorageDir(mainActivity);
File xmlFile = new File(xmlPath, "config.xml");
- /*create the default XML config by using the
- default.xml from the assets folder*/
if (xmlFile.exists()) {
xmlFile.delete();
}
@@ -141,9 +147,30 @@ public class Communicator {
+ e.getMessage());
}
},
- error -> Log.e("Communicator",
- "Error during XML DOWNLOAD: " + error.getMessage())
+ error -> handleError(error,"XML DOWNLOAD")
);
requestQueue.add(request);
}
+
+ private void handleError(VolleyError error, String method){
+ if (error instanceof ServerError){
+ Log.e("Communicator","server error during " + method + ": " + error.getMessage());
+ } else if (error instanceof NoConnectionError) {
+ makeToast("NO CONNECTION");
+ } else if (error instanceof TimeoutError) {
+ makeToast("CONNECTION TIMEOUT");
+ } else {
+ Log.e("Communicator","error during " + method + ": " + error.getMessage());
+ }
+ }
+
+ private void makeToast(String message){
+ Toast toast = new Toast(mainActivity);
+ View toastView = mainActivity.getLayoutInflater().inflate(R.layout.volley_toast, (ViewGroup) mainActivity.findViewById(R.id.volleyToastContainer));
+ TextView textView = toastView.findViewById(R.id.text);
+ textView.setText(message);
+ toast.setDuration(Toast.LENGTH_LONG);
+ toast.setView(toastView);
+ toast.show();
+ }
}
diff --git a/APED/app/src/main/res/layout/volley_toast.xml b/APED/app/src/main/res/layout/volley_toast.xml
new file mode 100644
index 0000000..e0f7678
--- /dev/null
+++ b/APED/app/src/main/res/layout/volley_toast.xml
@@ -0,0 +1,27 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file