Added button to export classifier data

This commit is contained in:
M66B 2022-09-09 18:04:27 +02:00
parent b1993a32c1
commit 1e4121d608
3 changed files with 86 additions and 2 deletions

View File

@ -19,9 +19,11 @@ package eu.faircode.email;
Copyright 2018-2022 by Marcel Bokhorst (M66B)
*/
import android.app.Activity;
import android.app.ActivityManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
@ -79,7 +81,10 @@ import androidx.lifecycle.Observer;
import androidx.preference.PreferenceManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.text.NumberFormat;
@ -203,6 +208,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
private EditText etKeywords;
private SwitchCompat swTestIab;
private Button btnImportProviders;
private Button btnExportClassifier;
private TextView tvProcessors;
private TextView tvMemoryClass;
private TextView tvMemoryUsage;
@ -229,7 +235,8 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
private NumberFormat NF = NumberFormat.getNumberInstance();
private final static long MIN_FILE_SIZE = 1024 * 1024L;
private static final int REQUEST_CLASSIFIER = 1;
private static final long MIN_FILE_SIZE = 1024 * 1024L;
private final static String[] RESET_OPTIONS = new String[]{
"sort_answers", "shortcuts", "fts",
@ -405,6 +412,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
etKeywords = view.findViewById(R.id.etKeywords);
swTestIab = view.findViewById(R.id.swTestIab);
btnImportProviders = view.findViewById(R.id.btnImportProviders);
btnExportClassifier = view.findViewById(R.id.btnExportClassifier);
tvProcessors = view.findViewById(R.id.tvProcessors);
tvMemoryClass = view.findViewById(R.id.tvMemoryClass);
tvMemoryUsage = view.findViewById(R.id.tvMemoryUsage);
@ -1433,6 +1441,13 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
}
});
btnExportClassifier.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onExportClassifier(v.getContext());
}
});
btnGC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -1813,6 +1828,22 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
super.onDestroyView();
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
switch (requestCode) {
case REQUEST_CLASSIFIER:
if (resultCode == Activity.RESULT_OK && data != null)
onHandleExportClassifier(data);
break;
}
} catch (Throwable ex) {
Log.e(ex);
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if ("last_cleanup".equals(key))
@ -2276,6 +2307,48 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
}.execute(this, new Bundle(), "permissions");
}
private void onExportClassifier(Context context) {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TITLE, "classifier.json");
Helper.openAdvanced(intent);
startActivityForResult(intent, REQUEST_CLASSIFIER);
}
private void onHandleExportClassifier(Intent intent) {
Bundle args = new Bundle();
args.putParcelable("uri", intent.getData());
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
Uri uri = args.getParcelable("uri");
ContentResolver resolver = context.getContentResolver();
File file = MessageClassifier.getFile(context, false);
try (OutputStream os = resolver.openOutputStream(uri)) {
try (InputStream is = new FileInputStream(file)) {
Helper.copy(is, os);
}
}
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
ToastEx.makeText(getContext(), R.string.title_setup_exported, Toast.LENGTH_LONG).show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "classifier");
}
private static class StorageData {
private long hused;
private long hmax;

View File

@ -1736,6 +1736,16 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swTestIab" />
<Button
android:id="@+id/btnExportClassifier"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_advanced_export_classifier"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnImportProviders" />
<TextView
android:id="@+id/tvProcessors"
android:layout_width="0dp"
@ -1745,7 +1755,7 @@
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnImportProviders" />
app:layout_constraintTop_toBottomOf="@id/btnExportClassifier" />
<TextView
android:id="@+id/tvMemoryClass"

View File

@ -816,6 +816,7 @@
<string name="title_advanced_global_keywords" translatable="false">Global keywords</string>
<string name="title_advanced_test_iab" translatable="false">Test IAB</string>
<string name="title_advanced_import_providers" translatable="false">Import providers</string>
<string name="title_advanced_export_classifier" translatable="false">Export classifier</string>
<string name="title_advanced_processors" translatable="false">Processors: %1$d</string>
<string name="title_advanced_memory_class" translatable="false">Memory class: %1$s/%2$s Total: %3$s</string>
<string name="title_advanced_memory_usage" translatable="false">Memory usage: %1$s/%2$s Native: %3$s</string>