Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bookmark: use Storage Access Framework to export bookmarks #248

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions android/app/src/main/kotlin/xyz/project/violet/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package xyz.project.violet

import android.app.Activity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Environment
Expand All @@ -13,6 +15,8 @@ import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugins.GeneratedPluginRegistrant
import java.io.FileInputStream
import java.io.FileOutputStream

class MainActivity : FlutterFragmentActivity() {
private val VOLUME_CHANNEL = "xyz.project.violet/volume"
Expand Down Expand Up @@ -82,6 +86,7 @@ class MainActivity : FlutterFragmentActivity() {
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, MISC_CHANNEL).setMethodCallHandler { call, result ->
when (call.method) {
"finishMainActivity" -> finishMainActivity(call, result)
"exportFile" -> exportFile(call, result)
else -> result.notImplemented()
}
}
Expand All @@ -100,4 +105,80 @@ class MainActivity : FlutterFragmentActivity() {
finish()
result.success(null)
}

private class ExportFileRequest(
val filePath: String,
val call: MethodCall,
val result: MethodChannel.Result,
)

private var nextExportFileRequestCode = 1000001;
private val exportFileRequestMap = hashMapOf<Int, ExportFileRequest>()

private fun exportFile(call: MethodCall, result: MethodChannel.Result) {
val filePath = call.argument<String>("filePath")
val mimeType = call.argument<String>("mimeType")
val fileNameToSaveAs = call.argument<String>("fileNameToSaveAs")

if (filePath == null) {
result.error("noArgument", "filePath", null)
return
}

if (mimeType == null) {
result.error("noArgument", "mimeType", null)
return
}

if (fileNameToSaveAs == null) {
result.error("noArgument", "fileNameToSaveAs", null)
return
}

try {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = mimeType
putExtra(Intent.EXTRA_TITLE, fileNameToSaveAs)
}

exportFileRequestMap[nextExportFileRequestCode] =
ExportFileRequest(filePath, call, result)
startActivityForResult(intent, nextExportFileRequestCode++);
} catch (e: Throwable) {
result.error("exception", e.toString(), e);
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

val request = exportFileRequestMap[requestCode] ?: return

try {
if (resultCode != Activity.RESULT_OK) {
request.result.error("intentResultFail", "resultCode=$resultCode", null)
return
}

try {
val uri = data!!.data!!
val targetFileDescriptor = contentResolver.openFileDescriptor(uri, "w")

val input = FileInputStream(request.filePath)
val output = FileOutputStream(targetFileDescriptor!!.fileDescriptor)

input.copyTo(output)

input.close()
output.close()

request.result.success(null)
} catch (e: Throwable) {
request.result.error("exception", e.toString(), e);
}
} finally {
exportFileRequestMap.remove(requestCode)
}
}
}
58 changes: 34 additions & 24 deletions lib/pages/settings/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import 'package:violet/pages/settings/tag_rebuild_page.dart';
import 'package:violet/pages/settings/tag_selector.dart';
import 'package:violet/pages/settings/version_page.dart';
import 'package:violet/pages/splash/splash_page.dart';
import 'package:violet/platform/misc.dart';
import 'package:violet/server/violet.dart';
import 'package:violet/settings/settings.dart';
import 'package:violet/style/palette.dart';
Expand Down Expand Up @@ -1994,36 +1995,45 @@ class _SettingsPageState extends State<SettingsPage>
title: Text(Translations.of(context).trans('exportingbookmark')),
trailing: const Icon(Icons.keyboard_arrow_right),
onTap: () async {
if (!await Permission.storage.isGranted) {
if (await Permission.storage.request() ==
PermissionStatus.denied) {
flutterToast.showToast(
child: ToastWrapper(
isCheck: false,
msg: Translations.of(context).trans('noauth'),
),
gravity: ToastGravity.BOTTOM,
toastDuration: const Duration(seconds: 4),
);
final dir = Platform.isIOS
? await getApplicationSupportDirectory()
: (await getApplicationDocumentsDirectory());
final bookmarkDatabaseFile = File('${dir.path}/user.db');

return;
}
}
if (Platform.isAndroid) {
await PlatformMiscMethods.instance.exportFile(
bookmarkDatabaseFile.path,
mimeType: 'application/vnd.sqlite3',
fileNameToSaveAs: 'violet-bookmarks.db',
);
} else {
if (!await Permission.storage.isGranted) {
if (await Permission.storage.request() ==
PermissionStatus.denied) {
flutterToast.showToast(
child: ToastWrapper(
isCheck: false,
msg: Translations.of(context).trans('noauth'),
),
gravity: ToastGravity.BOTTOM,
toastDuration: const Duration(seconds: 4),
);

final selectedPath = await FilePicker.platform.getDirectoryPath();
return;
}
}

if (selectedPath == null) {
return;
}
final selectedPath =
await FilePicker.platform.getDirectoryPath();

final db = Platform.isIOS
? await getApplicationSupportDirectory()
: (await getApplicationDocumentsDirectory());
final dbfile = File('${db.path}/user.db');
if (selectedPath == null) {
return;
}

final extpath = '$selectedPath/bookmark.db';
final extpath = '$selectedPath/bookmark.db';

await dbfile.copy(extpath);
await bookmarkDatabaseFile.copy(extpath);
}

flutterToast.showToast(
child: ToastWrapper(
Expand Down
19 changes: 19 additions & 0 deletions lib/platform/misc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,23 @@ class PlatformMiscMethods {

await _methodChannel.invokeMethod('finishMainActivity');
}

Future<void> exportFile(
String filePath, {
required String mimeType,
required String fileNameToSaveAs,
}) async {
if (!Platform.isAndroid) {
throw UnsupportedError('Android only');
}

await _methodChannel.invokeMethod<String>(
'exportFile',
<String, dynamic>{
'filePath': filePath,
'mimeType': mimeType,
'fileNameToSaveAs': fileNameToSaveAs,
},
);
}
}