big change: move the json loading and random server selection to main PieCannon class (so that it has a reason to live :)) and implement loading the json file in the android app.

I deliberated between re-using the json file in the android app and using a sqlite database or some such thing, given that a UI for editing these will have to be implemented at some point. I figure the json file is more config than data, and probably should be as consistent as possible between platforms.

For now at least this makes the app usable for me, without having to hard-code my server credentials.
This commit is contained in:
2020-10-30 06:52:53 -05:00
parent 432cfa4d21
commit da39586c4d
19 changed files with 225 additions and 74 deletions

View File

@@ -1,12 +1,22 @@
package net.monarchpass.piecannon;
import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Main extends Activity {
@Override
public void onCreate (final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final File serversJson = new File(getExternalFilesDir(null), "servers.json");
final TextView text = (TextView)findViewById(R.id.welcome);
text.append("\n---\n");
text.append(serversJson.exists() ? "servers.json file found at: " : "Please place a servers.json file at: ");
text.append("\n");
text.append(serversJson.getPath());
}
}

View File

@@ -10,6 +10,7 @@ import android.content.ContentResolver;
import com.google.common.io.ByteSource;
import java.net.URI;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.StringWriter;
@@ -42,25 +43,34 @@ public class Send extends Activity {
public void uploadImage (final Uri imageUri) {
// handle image
final TextView text = (TextView)findViewById(R.id.text);
text.setText(imageUri.toString());
text.append("\n---\nReceived file url: \n");
text.append(imageUri.toString());
final ContentResolver resolver = getContentResolver();
final Server testServer = new TestServer();
final PieCannon cannon = new PieCannon();
try
{
cannon.loadServersFrom(new File(getExternalFilesDir(null), "servers.json"));
}
catch (final Exception exception)
{
logException(exception);
return;
}
executor.submit(() -> {
try
{
final URI uploaded = testServer.upload(createFileNameForUri(imageUri, resolver), new ContentByteSource(resolver, imageUri));
final URI uploaded = cannon.selectServer().upload(createFileNameForUri(imageUri, resolver), new ContentByteSource(resolver, imageUri));
runOnUiThread(() -> {
text.setText(uploaded.toString());
text.append("\n---\nUploaded file to url: \n");
text.append(uploaded.toString());
shareUploadUrl(uploaded);
});
}
catch (final Exception exception) {
final StringWriter buf = new StringWriter();
final PrintWriter out = new PrintWriter(buf);
exception.printStackTrace(out);
runOnUiThread(() -> text.setText(buf.toString()));
logException(exception);
}
});
}
@@ -87,6 +97,17 @@ public class Send extends Activity {
throw new RuntimeException("Could not construct a file name");
}
private void logException (final Exception exception) {
final StringWriter buf = new StringWriter();
final PrintWriter out = new PrintWriter(buf);
exception.printStackTrace(out);
runOnUiThread(() -> {
final TextView text = (TextView)findViewById(R.id.text);
text.append("\n---\n");
text.append(buf.toString());
});
}
@AllArgsConstructor
public static class ContentByteSource extends ByteSource {
private final ContentResolver resolver;

View File

@@ -1,18 +0,0 @@
package net.monarchpass.piecannon;
import net.monarchpass.piecannon.impl.SftpServer;
import java.net.URI;
public class TestServer extends SftpServer {
public TestServer () {
super(
"Test Server",
"",
22,
"",
"",
"",
URI.create("")
);
}
}

View File

@@ -5,4 +5,9 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.monarchpass.piecannon.Main">
<TextView android:id="@+id/welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Pie Cannon! There is no GUI for editing servers yet." />
</ScrollView>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="net.monarchpass.piecannon.Send">
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pie Cannon at work!" />
</ScrollView>