mirror of
https://github.com/M66B/FairEmail.git
synced 2026-03-29 13:24:52 +02:00
FFSend integration
This commit is contained in:
@@ -23,8 +23,12 @@ import static android.app.Activity.RESULT_OK;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
@@ -41,10 +45,13 @@ import android.widget.TextView;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.documentfile.provider.DocumentFile;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
@@ -54,9 +61,12 @@ import java.nio.charset.StandardCharsets;
|
||||
public class FragmentDialogInsertLink extends FragmentDialogBase {
|
||||
private EditText etLink;
|
||||
private EditText etTitle;
|
||||
private Button btnUpload;
|
||||
private ProgressBar pbUpload;
|
||||
|
||||
private static final int METADATA_CONNECT_TIMEOUT = 10 * 1000; // milliseconds
|
||||
private static final int METADATA_READ_TIMEOUT = 15 * 1000; // milliseconds
|
||||
private static final int REQUEST_FFSEND = 1;
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||
@@ -79,6 +89,8 @@ public class FragmentDialogInsertLink extends FragmentDialogBase {
|
||||
etTitle = view.findViewById(R.id.etTitle);
|
||||
final Button btnMetadata = view.findViewById(R.id.btnMetadata);
|
||||
final ProgressBar pbWait = view.findViewById(R.id.pbWait);
|
||||
btnUpload = view.findViewById(R.id.btnUpload);
|
||||
pbUpload = view.findViewById(R.id.pbUpload);
|
||||
|
||||
etLink.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
@@ -225,6 +237,17 @@ public class FragmentDialogInsertLink extends FragmentDialogBase {
|
||||
}
|
||||
});
|
||||
|
||||
btnUpload.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
||||
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
intent.setType("*/*");
|
||||
startActivityForResult(Helper.getChooser(getContext(), intent), REQUEST_FFSEND);
|
||||
}
|
||||
});
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
String link = (uri == null ? "https://" : uri.toString());
|
||||
etLink.setText(link);
|
||||
@@ -234,7 +257,9 @@ public class FragmentDialogInsertLink extends FragmentDialogBase {
|
||||
etTitle.setText(savedInstanceState.getString("fair:text"));
|
||||
}
|
||||
|
||||
btnUpload.setVisibility(BuildConfig.PLAY_STORE_RELEASE ? View.GONE : View.VISIBLE);
|
||||
pbWait.setVisibility(View.GONE);
|
||||
pbUpload.setVisibility(View.GONE);
|
||||
|
||||
return new AlertDialog.Builder(context)
|
||||
.setView(view)
|
||||
@@ -256,6 +281,76 @@ public class FragmentDialogInsertLink extends FragmentDialogBase {
|
||||
.create();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
try {
|
||||
switch (requestCode) {
|
||||
case REQUEST_FFSEND:
|
||||
if (resultCode == RESULT_OK && data != null)
|
||||
onFFSend(data.getData());
|
||||
break;
|
||||
}
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void onFFSend(Uri uri) {
|
||||
Bundle args = new Bundle();
|
||||
args.putParcelable("uri", uri);
|
||||
|
||||
new SimpleTask<String>() {
|
||||
@Override
|
||||
protected void onPreExecute(Bundle args) {
|
||||
btnUpload.setEnabled(false);
|
||||
pbUpload.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Bundle args) {
|
||||
btnUpload.setEnabled(true);
|
||||
pbUpload.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String onExecute(Context context, Bundle args) throws Throwable {
|
||||
Uri uri = args.getParcelable("uri");
|
||||
if (uri == null)
|
||||
throw new FileNotFoundException("uri");
|
||||
|
||||
if (!"content".equals(uri.getScheme()))
|
||||
throw new FileNotFoundException("content");
|
||||
|
||||
DocumentFile dfile = DocumentFile.fromSingleUri(context, uri);
|
||||
if (dfile == null)
|
||||
throw new FileNotFoundException("dfile");
|
||||
|
||||
args.putString("title", dfile.getName());
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
String server = prefs.getString("ff_send", FFSend.FF_DEFAULT_SERVER);
|
||||
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
try (InputStream is = resolver.openInputStream(uri)) {
|
||||
return FFSend.upload(is, dfile, 10, 60 * 60, server);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onExecuted(Bundle args, String ffsend) {
|
||||
etLink.setText(ffsend);
|
||||
etTitle.setText(args.getString("title"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onException(Bundle args, Throwable ex) {
|
||||
Log.unexpectedError(getParentFragmentManager(), ex);
|
||||
}
|
||||
}.execute(this, args, "ffsend");
|
||||
}
|
||||
|
||||
private static class OpenGraph {
|
||||
private String title;
|
||||
private String description;
|
||||
|
||||
@@ -126,6 +126,8 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
|
||||
private TextView tvVirusTotalPrivacy;
|
||||
private EditText etVirusTotal;
|
||||
private ImageButton ibVirusTotal;
|
||||
private EditText etFFSend;
|
||||
private ImageButton ibFFSend;
|
||||
private SwitchCompat swUpdates;
|
||||
private ImageButton ibChannelUpdated;
|
||||
private SwitchCompat swCheckWeekly;
|
||||
@@ -215,6 +217,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
|
||||
private TextView tvPermissions;
|
||||
|
||||
private Group grpVirusTotal;
|
||||
private Group grpFFSend;
|
||||
private Group grpUpdates;
|
||||
private Group grpTest;
|
||||
private CardView cardDebug;
|
||||
@@ -226,11 +229,11 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
|
||||
private final static String[] RESET_OPTIONS = new String[]{
|
||||
"sort_answers", "shortcuts", "fts",
|
||||
"classification", "class_min_probability", "class_min_difference",
|
||||
"language", "lt_enabled", "deepl_enabled", "vt_enabled", "vt_apikey",
|
||||
"language", "lt_enabled", "deepl_enabled", "vt_enabled", "vt_apikey", "ff_send",
|
||||
"updates", "weekly", "show_changelog",
|
||||
"crash_reports", "cleanup_attachments",
|
||||
"watchdog", "experiments", "main_log", "protocol", "log_level", "debug", "leak_canary", "test1",
|
||||
"test2", "test3", "test4", "test5",
|
||||
"watchdog", "experiments", "main_log", "protocol", "log_level", "debug", "leak_canary",
|
||||
"test1", "test2", "test3", "test4", "test5",
|
||||
"work_manager", // "external_storage",
|
||||
"query_threads", "wal", "sqlite_checkpoints", "sqlite_analyze", "sqlite_auto_vacuum", "sqlite_cache",
|
||||
"chunk_size", "thread_range", "undo_manager",
|
||||
@@ -317,6 +320,8 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
|
||||
tvVirusTotalPrivacy = view.findViewById(R.id.tvVirusTotalPrivacy);
|
||||
etVirusTotal = view.findViewById(R.id.etVirusTotal);
|
||||
ibVirusTotal = view.findViewById(R.id.ibVirusTotal);
|
||||
etFFSend = view.findViewById(R.id.etFFSend);
|
||||
ibFFSend = view.findViewById(R.id.ibFFSend);
|
||||
swUpdates = view.findViewById(R.id.swUpdates);
|
||||
ibChannelUpdated = view.findViewById(R.id.ibChannelUpdated);
|
||||
swCheckWeekly = view.findViewById(R.id.swWeekly);
|
||||
@@ -406,6 +411,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
|
||||
tvPermissions = view.findViewById(R.id.tvPermissions);
|
||||
|
||||
grpVirusTotal = view.findViewById(R.id.grpVirusTotal);
|
||||
grpFFSend = view.findViewById(R.id.grpFFSend);
|
||||
grpUpdates = view.findViewById(R.id.grpUpdates);
|
||||
grpTest = view.findViewById(R.id.grpTest);
|
||||
cardDebug = view.findViewById(R.id.cardDebug);
|
||||
@@ -689,6 +695,35 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
|
||||
}
|
||||
});
|
||||
|
||||
etFFSend.setHint(FFSend.FF_DEFAULT_SERVER);
|
||||
etFFSend.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
String apikey = s.toString().trim();
|
||||
if (TextUtils.isEmpty(apikey))
|
||||
prefs.edit().remove("ff_send").apply();
|
||||
else
|
||||
prefs.edit().putString("ff_send", apikey).apply();
|
||||
}
|
||||
});
|
||||
|
||||
ibFFSend.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Helper.view(v.getContext(), Uri.parse(FFSend.FF_INSTANCES), true);
|
||||
}
|
||||
});
|
||||
|
||||
swUpdates.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
||||
@@ -1632,6 +1667,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
|
||||
});
|
||||
|
||||
grpVirusTotal.setVisibility(BuildConfig.PLAY_STORE_RELEASE ? View.GONE : View.VISIBLE);
|
||||
grpFFSend.setVisibility(BuildConfig.PLAY_STORE_RELEASE ? View.GONE : View.VISIBLE);
|
||||
|
||||
grpUpdates.setVisibility(!BuildConfig.DEBUG &&
|
||||
(Helper.isPlayStoreInstall() || !Helper.hasValidFingerprint(getContext()))
|
||||
@@ -1706,7 +1742,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
|
||||
if ("last_cleanup".equals(key))
|
||||
setLastCleanup(prefs.getLong(key, -1));
|
||||
|
||||
if ("vt_apikey".equals(key))
|
||||
if ("vt_apikey".equals(key) || "ff_send".equals(key))
|
||||
return;
|
||||
|
||||
setOptions();
|
||||
@@ -1853,6 +1889,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
|
||||
swDeepL.setChecked(prefs.getBoolean("deepl_enabled", false));
|
||||
swVirusTotal.setChecked(prefs.getBoolean("vt_enabled", false));
|
||||
etVirusTotal.setText(prefs.getString("vt_apikey", null));
|
||||
etFFSend.setText(prefs.getString("ff_send", null));
|
||||
swUpdates.setChecked(prefs.getBoolean("updates", true));
|
||||
swCheckWeekly.setChecked(prefs.getBoolean("weekly", Helper.hasPlayStore(getContext())));
|
||||
swCheckWeekly.setEnabled(swUpdates.isChecked());
|
||||
|
||||
Reference in New Issue
Block a user