Remote dialog
This commit is contained in:
@@ -122,7 +122,7 @@ public class MainActivity extends NextcloudMapsStyledActivity {
|
||||
|
||||
// Check dialog
|
||||
RemoteDialog rd = new RemoteDialog(this);
|
||||
rd.check();
|
||||
rd.run();
|
||||
|
||||
boolean showMap = SettingsManager.isGeofavoriteListShownAsMap(this);
|
||||
if (showMap)
|
||||
|
||||
@@ -1,4 +1,99 @@
|
||||
package it.danieleverducci.nextcloudmaps.utils;
|
||||
|
||||
import static androidx.core.content.ContextCompat.startActivity;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import it.danieleverducci.nextcloudmaps.R;
|
||||
|
||||
public class RemoteDialog {
|
||||
|
||||
private Activity context;
|
||||
|
||||
public RemoteDialog(Activity context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Thread t = new Thread(() -> {
|
||||
HttpURLConnection urlConnection = null;
|
||||
try {
|
||||
URL url = new URL("https://git.ichibi.eu/penguin86/nextcloud-maps-client/raw/branch/remote-dialog/remote-dialog/data_prod.json");
|
||||
urlConnection = (HttpURLConnection) url.openConnection();
|
||||
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
|
||||
StringBuilder textBuilder = new StringBuilder();
|
||||
try (Reader reader = new BufferedReader(new InputStreamReader
|
||||
(in, StandardCharsets.UTF_8))) {
|
||||
int c = 0;
|
||||
while ((c = reader.read()) != -1) {
|
||||
textBuilder.append((char) c);
|
||||
}
|
||||
}
|
||||
showDialog(new JSONObject(textBuilder.toString()));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (urlConnection != null)
|
||||
urlConnection.disconnect();
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
|
||||
private void showDialog(JSONObject jo) {
|
||||
try {
|
||||
String title = jo.getString("title");
|
||||
String content = jo.getString("content");
|
||||
JSONObject button1 = jo.getJSONObject("button1");
|
||||
JSONObject button2 = jo.getJSONObject("button2");
|
||||
String button1text = button1.getString("text");
|
||||
String button1action = button1.getString("action");
|
||||
String button2text = button2.getString("text");
|
||||
String button2action = button2.getString("action");
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
||||
builder.setMessage(content)
|
||||
.setTitle(title)
|
||||
.setPositiveButton(button1text, (dialog, id) -> {
|
||||
openBrowser(button1action);
|
||||
})
|
||||
.setNegativeButton(button2text, (dialog, id) -> {
|
||||
openBrowser(button2action);
|
||||
})
|
||||
.setNeutralButton(android.R.string.cancel, (dialog, id) -> dialog.dismiss());
|
||||
context.runOnUiThread(() -> {
|
||||
AlertDialog ad = builder.create();
|
||||
ad.show();
|
||||
});
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void openBrowser(String url) {
|
||||
Intent i = new Intent(Intent.ACTION_VIEW);
|
||||
i.setData(Uri.parse(url));
|
||||
context.startActivity(i);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user