Skip to content

Commit

Permalink
Add setting to view config file
Browse files Browse the repository at this point in the history
  • Loading branch information
dhzdhd committed Jan 9, 2025
1 parent ebf2862 commit d431e5e
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 118 deletions.
25 changes: 25 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<uses-permission android:name="android.permission.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.FLAG_GRANT_READ_URI_PERMISSION" />
<uses-permission android:name="android.permission.FLAG_GRANT_WRITE_URI_PERMISSION" />
<application
android:label="syncvault"
android:name="${applicationName}"
Expand Down Expand Up @@ -51,10 +53,33 @@
</intent-filter>
</activity>

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>

<queries>
<intent>
<action android:name="android.support.customtabs.action.CustomTabsService" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="*/*" />
</intent>
<intent>
<action android:name="android.intent.action.EDIT" />
</intent>
</queries>
</manifest>
63 changes: 61 additions & 2 deletions android/app/src/main/kotlin/com/example/syncvault/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ package com.example.syncvault
import android.os.Bundle
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import androidx.core.content.FileProvider
import android.net.Uri
import android.webkit.MimeTypeMap
import android.content.Intent

import java.io.File

import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
Expand All @@ -22,6 +28,54 @@ class MainActivity: FlutterActivity() {
} else {
result.error("UNAVAILABLE", "Native library path not available.", null)
}
} else if (call.method == "getContentUri") {
try {
val filePath = call.argument<String>("filePath")
val file = File(filePath)
val contentUri = FileProvider.getUriForFile(
applicationContext,
"${applicationContext.packageName}.fileprovider",
file
)
println("File path: $filePath")

println("uri path: $contentUri")


// Create intent for viewing text files
val intent = Intent(Intent.ACTION_EDIT).apply {
setDataAndType(contentUri, "text/plain")
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}

// Check if there's an activity to handle this intent
val packageManager = applicationContext.packageManager
val activities = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)

if (activities.isNotEmpty()) {
startActivity(intent)
result.success(contentUri.toString())
} else {
// Try with ACTION_VIEW if EDIT doesn't work
val viewIntent = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(contentUri, "text/plain")
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}

val viewActivities = packageManager.queryIntentActivities(viewIntent, PackageManager.MATCH_DEFAULT_ONLY)
if (viewActivities.isNotEmpty()) {
startActivity(viewIntent)
result.success(contentUri.toString())
} else {
result.error("NO_HANDLER", "No app found to handle this file type. Please install a text editor.", null)
}
}
} catch (e: Exception) {
result.error("ERROR", e.message ?: "Unknown error", null)
}
} else {
result.notImplemented()
}
Expand All @@ -32,4 +86,9 @@ class MainActivity: FlutterActivity() {
val appInfo: ApplicationInfo = applicationContext.packageManager.getApplicationInfo(applicationContext.packageName, 0)
return appInfo.nativeLibraryDir
}
}

private fun getContentUriForFile(filePath: String?): Uri {
val file = File(filePath)
return FileProvider.getUriForFile(this, "$packageName.fileprovider", file)
}
}
19 changes: 19 additions & 0 deletions android/app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path
name="internal_files"
path="." />
<files-path name="files" path="app_flutter/" />
<cache-path
name="internal_cache"
path="." />
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<external-cache-path
name="external_cache"
path="." />
</paths>
1 change: 0 additions & 1 deletion lib/src/home/components/delete_folder_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:syncvault/helpers.dart';
import 'package:syncvault/src/accounts/controllers/folder_controller.dart';
import 'package:syncvault/src/accounts/models/folder_model.dart';

Expand Down
5 changes: 4 additions & 1 deletion lib/src/home/services/rclone.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,11 @@ class RCloneAuthService {
// Wait for process to finish
await process.exitCode;

final match =
RegExp(r'\{.+\}').stringMatch(output.toString()) ?? '';
final Map<String, dynamic> authJson = jsonDecode(
RegExp(r'\{.+\}').stringMatch(output.toString())!,
// FIXME: This probably breaks on android release builds
match,
);
if (authJson
case {
Expand Down
Loading

0 comments on commit d431e5e

Please sign in to comment.