Skip to content

app: Add support for local sync provider #22

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
<!-- 运行前台服务 -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!--
Fix the problem that notifications cannot be allowed on Android 13
https://github.com/siyuan-note/siyuan/issues/7960
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/org/b3log/siyuan/AndroidFileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.b3log.siyuan;

import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.provider.DocumentsContract;

public class AndroidFileUtils {
public static String getPath(final Context context, final Uri uri) {
if (DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
if (split.length != 2) return null;

final String type = split[0];

if ("primary".equalsIgnoreCase(type))
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
}

return null;
}

/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/org/b3log/siyuan/JSAndroid.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ public void changeStatusBarColor(final String color, final int appearanceMode) {
});
}

@JavascriptInterface
public String getLocalFileSystemPath() {
activity.getStoragePermissions();
activity.pickLocalFileSystemFolder();
return activity.getLocalSyncPath();
}

private int parseColor(String str) {
try {
str = str.trim();
Expand Down
47 changes: 46 additions & 1 deletion app/src/main/java/org/b3log/siyuan/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@
import android.content.ClipData;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.LocaleList;
import android.os.Looper;
import android.os.Message;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.Settings;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
Expand Down Expand Up @@ -105,6 +109,7 @@ public class MainActivity extends AppCompatActivity implements com.blankj.utilco
private ValueCallback<Uri[]> uploadMessage;
private static final int REQUEST_SELECT_FILE = 100;
private static final int REQUEST_CAMERA = 101;
private static final int LOCAL_SYNC_FOLDER_CODE = 200;

@Override
public void onNewIntent(final Intent intent) {
Expand Down Expand Up @@ -156,6 +161,32 @@ protected void onCreate(final Bundle savedInstanceState) {
AndroidBug5497Workaround.assistActivity(this);
}

public String getLocalSyncPath() {
SharedPreferences prefs = getSharedPreferences("app_prefs", MODE_PRIVATE);
return prefs.getString("localSyncPath", null);
}

public void getStoragePermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
}

public void pickLocalFileSystemFolder() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, LOCAL_SYNC_FOLDER_CODE);
}

private void saveLocalSyncPath(String path) {
SharedPreferences prefs = getSharedPreferences("app_prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("localSyncPath", path);
editor.apply();
}

private void initUIElements() {
bootLogo = findViewById(R.id.bootLogo);
bootProgressBar = findViewById(R.id.progressBar);
Expand Down Expand Up @@ -573,8 +604,22 @@ private void openCamera() {

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == LOCAL_SYNC_FOLDER_CODE && resultCode == RESULT_OK) {
if (intent != null) {
Uri treeUri = intent.getData();
Uri docUri = DocumentsContract.buildDocumentUriUsingTree(treeUri,
DocumentsContract.getTreeDocumentId(treeUri));
String path = AndroidFileUtils.getPath(this, docUri);
if (path == null || path.isEmpty())
Toast.makeText(this, "Error with selected directory",
Toast.LENGTH_LONG).show();
else
saveLocalSyncPath(path);
}
}

if (null == uploadMessage) {
super.onActivityResult(requestCode, resultCode, intent);
return;
}

Expand Down