Skip to content

Commit

Permalink
qol
Browse files Browse the repository at this point in the history
  • Loading branch information
9001 committed Jan 21, 2022
1 parent 2cde8ea commit 1864307
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 38 deletions.
8 changes: 8 additions & 0 deletions app/src/main/java/me/ocv/partyup/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.ocv.partyup;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.MenuItem;

import androidx.appcompat.app.ActionBar;
Expand All @@ -23,6 +25,12 @@ protected void onCreate(Bundle savedInstanceState) {
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs.getString("on_up_ok", "menu").equals("menu")) {
SharedPreferences.Editor ed = prefs.edit();
ed.putString("on_up_ok", "menu");
ed.commit();
}
}

public static class SettingsFragment extends PreferenceFragmentCompat {
Expand Down
90 changes: 59 additions & 31 deletions app/src/main/java/me/ocv/partyup/XferActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;
import java.util.stream.Collectors;

import me.ocv.partyup.databinding.ActivityXferBinding;
Expand All @@ -40,6 +41,7 @@ public class XferActivity extends AppCompatActivity {
ActivityXferBinding binding;
SharedPreferences prefs;
Intent the_intent;
String password;
String the_msg;
String base_url, full_url, share_url;
Uri src_uri;
Expand Down Expand Up @@ -166,7 +168,7 @@ private void handleSendImage() {
src_size = sz;
}
catch (Exception ex) {
show_msg("Error: " + ex.toString());
show_msg("Error3: " + ex.toString());
return;
}

Expand All @@ -184,7 +186,11 @@ private void do_up() {
upping = true;

try {
base_url = full_url = prefs.getString("server_url", "");
base_url = prefs.getString("server_url", "");
if (!base_url.startsWith("http"))
base_url = "http://" + base_url;

full_url = base_url;
show_msg("Sending to " + base_url + " ...\n\n" + the_desc);

if (!full_url.endsWith("/"))
Expand All @@ -193,13 +199,10 @@ private void do_up() {
if (src_size >= 0 && !src_name.isEmpty())
full_url += URLEncoder.encode(src_name, "UTF-8");

String password = prefs.getString("server_password", "");
password = prefs.getString("server_password", "");
if (password.equals("Default value"))
password = ""; // necessary in the emulator, not on real devices(?)

if (!password.isEmpty())
full_url += "?pw=" + password;

Thread t = new Thread() {
public void run() {
do_up2();
Expand All @@ -208,7 +211,7 @@ public void run() {
t.start();
}
catch (Exception ex) {
show_msg("Error: " + ex.toString());
show_msg("Error1: " + ex.toString());
}
}

Expand All @@ -217,13 +220,26 @@ private void do_up2() {
URL url = new URL(full_url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
if (!password.isEmpty())
conn.setRequestProperty("Authorization", "Basic " + new String(Base64.getEncoder().encode(password.getBytes())));

if (src_uri != null)
do_fileput(conn);
else
do_textmsg(conn);
}
catch (Exception ex) {
tshow_msg("Error: " + ex.toString());
tshow_msg("Error2: " + ex.toString() + "\n\nmaybe wrong password?");
}
}

String read_err(HttpURLConnection conn) {
try {
byte[] buf = new byte[1024];
int n = Math.max(0, conn.getErrorStream().read(buf));
return new String(buf, 0, n, "UTF-8");
} catch (Exception ex) {
return ex.toString();
}
}

Expand All @@ -239,9 +255,7 @@ private void do_textmsg(HttpURLConnection conn) throws Exception {
int rc = conn.getResponseCode();
share_url = "HTTP " + rc;
if (rc >= 300) {
byte[] buf = new byte[1024];
int n = Math.max(0, conn.getErrorStream().read(buf));
tshow_msg("Server error " + rc + ":\n" + new String(buf, 0, n, "UTF-8"));
tshow_msg("Server error " + rc + ":\n" + read_err(conn));
conn.disconnect();
return;
}
Expand Down Expand Up @@ -293,8 +307,7 @@ public void run() {
os.flush();
int rc = conn.getResponseCode();
if (rc >= 300) {
int n = Math.max(0, conn.getErrorStream().read(buf));
tshow_msg("Server error " + rc + ":\n" + new String(buf, 0, n, "UTF-8"));
tshow_msg("Server error " + rc + ":\n" + read_err(conn));
conn.disconnect();
return;
}
Expand Down Expand Up @@ -332,8 +345,15 @@ void onsuccess(Boolean upload) {
show_msg("✅ 👍\n\nCompleted successfully\n\n" + share_url);
((TextView)findViewById(R.id.upper_info)).setGravity(Gravity.CENTER);

if (prefs.getBoolean("autoclose", false)) {
Toast.makeText(getApplicationContext(), "Upload OK", Toast.LENGTH_SHORT).show();
String act = prefs.getString("on_up_ok", "menu");
if (!act.equals("menu")) {
if (act.equals("copy"))
copylink();
else if (act.equals("share"))
sharelink();
else
Toast.makeText(getApplicationContext(), "Upload OK", Toast.LENGTH_SHORT).show();

finishAndRemoveTask();
return;
}
Expand All @@ -359,30 +379,38 @@ public void onClick(View v) {
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ClipboardManager cb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData cd = ClipData.newPlainText("copyparty upload", share_url);
Toast.makeText(getApplicationContext(), "Link copied", Toast.LENGTH_SHORT).show();
copylink();
}
});

btn = (Button)findViewById(R.id.btnShareLink);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent send = new Intent(Intent.ACTION_SEND);
send.setType("text/plain");
send.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
send.putExtra(Intent.EXTRA_SUBJECT, "Uploaded file");
send.putExtra(Intent.EXTRA_TEXT, share_url);
//startActivity(Intent.createChooser(send, "Share file link"));

Intent view = new Intent(Intent.ACTION_VIEW);
view.setData(Uri.parse(share_url));

Intent i = Intent.createChooser(send, "Share file link");
i.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { view });
startActivity(i);
sharelink();
}
});
}

void copylink() {
ClipboardManager cb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData cd = ClipData.newPlainText("copyparty upload", share_url);
Toast.makeText(getApplicationContext(), "Upload OK -- Link copied", Toast.LENGTH_SHORT).show();
}

void sharelink() {
Intent send = new Intent(Intent.ACTION_SEND);
send.setType("text/plain");
send.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
send.putExtra(Intent.EXTRA_SUBJECT, "Uploaded file");
send.putExtra(Intent.EXTRA_TEXT, share_url);
//startActivity(Intent.createChooser(send, "Share file link"));

Intent view = new Intent(Intent.ACTION_VIEW);
view.setData(Uri.parse(share_url));

Intent i = Intent.createChooser(send, "Share file link");
i.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { view });
startActivity(i);
}
}
14 changes: 13 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
<resources>
<string name="app_name">Party UP!</string>
<string name="app_name">Party UP! dbg</string>
<string-array name="konsuccess">
<item>menu</item>
<item>copy</item>
<item>share</item>
<item>exit</item>
</string-array>
<string-array name="vonsuccess">
<item>Show menu (default)</item>
<item>Copy file link</item>
<item>Share file link</item>
<item>Exit app</item>
</string-array>
</resources>
14 changes: 8 additions & 6 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@
</PreferenceCategory>
<PreferenceCategory android:title="Behavior">

<CheckBoxPreference
<SwitchPreference
android:defaultValue="false"
android:key="autosend"
android:title="Just go"
app:summary="Upload right away, don't ask" />
<CheckBoxPreference
android:defaultValue="false"
android:key="autoclose"
android:title="Close on success"
app:summary="Exit if the upload is confirmed OK" />
<ListPreference
android:defaultValue="close"
android:entries="@array/vonsuccess"
android:entryValues="@array/konsuccess"
android:key="on_up_ok"
android:title="On upload success..."
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>

</PreferenceScreen>
2 changes: 2 additions & 0 deletions metadata/en-US/changelogs/10600.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
• new: choose what happens when an upload succeeds
• new: add http:// to the server URL if missing

0 comments on commit 1864307

Please sign in to comment.