|
23 | 23 | import android.content.ClipData;
|
24 | 24 | import android.content.ContentValues;
|
25 | 25 | import android.content.Intent;
|
| 26 | +import android.content.SharedPreferences; |
26 | 27 | import android.content.pm.PackageManager;
|
27 | 28 | import android.graphics.Bitmap;
|
28 | 29 | import android.graphics.Color;
|
29 | 30 | import android.net.Uri;
|
30 | 31 | import android.os.Build;
|
31 | 32 | import android.os.Bundle;
|
| 33 | +import android.os.Environment; |
32 | 34 | import android.os.Handler;
|
33 | 35 | import android.os.LocaleList;
|
34 | 36 | import android.os.Looper;
|
35 | 37 | import android.os.Message;
|
| 38 | +import android.provider.DocumentsContract; |
36 | 39 | import android.provider.MediaStore;
|
| 40 | +import android.provider.Settings; |
37 | 41 | import android.util.Log;
|
38 | 42 | import android.view.DragEvent;
|
39 | 43 | import android.view.View;
|
@@ -105,6 +109,8 @@ public class MainActivity extends AppCompatActivity implements com.blankj.utilco
|
105 | 109 | private ValueCallback<Uri[]> uploadMessage;
|
106 | 110 | private static final int REQUEST_SELECT_FILE = 100;
|
107 | 111 | private static final int REQUEST_CAMERA = 101;
|
| 112 | + private static final int REQUEST_MANAGE_ALL_CODE = 200; |
| 113 | + private static final int FOLDER_PICKER_CODE = 202; |
108 | 114 |
|
109 | 115 | @Override
|
110 | 116 | public void onNewIntent(final Intent intent) {
|
@@ -156,6 +162,32 @@ protected void onCreate(final Bundle savedInstanceState) {
|
156 | 162 | AndroidBug5497Workaround.assistActivity(this);
|
157 | 163 | }
|
158 | 164 |
|
| 165 | + public String getLocalSyncPath() { |
| 166 | + SharedPreferences prefs = getSharedPreferences("app_prefs", MODE_PRIVATE); |
| 167 | + return prefs.getString("localSyncPath", null); |
| 168 | + } |
| 169 | + |
| 170 | + public void getStoragePermissions() { |
| 171 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) { |
| 172 | + Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION); |
| 173 | + Uri uri = Uri.fromParts("package", getPackageName(), null); |
| 174 | + intent.setData(uri); |
| 175 | + startActivityForResult(intent, REQUEST_MANAGE_ALL_CODE); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + public void pickLocalFileSystemFolder() { |
| 180 | + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); |
| 181 | + startActivityForResult(intent, FOLDER_PICKER_CODE); |
| 182 | + } |
| 183 | + |
| 184 | + private void saveLocalSyncPath(String path) { |
| 185 | + SharedPreferences prefs = getSharedPreferences("app_prefs", MODE_PRIVATE); |
| 186 | + SharedPreferences.Editor editor = prefs.edit(); |
| 187 | + editor.putString("localSyncPath", path); |
| 188 | + editor.apply(); |
| 189 | + } |
| 190 | + |
159 | 191 | private void initUIElements() {
|
160 | 192 | bootLogo = findViewById(R.id.bootLogo);
|
161 | 193 | bootProgressBar = findViewById(R.id.progressBar);
|
@@ -573,8 +605,24 @@ private void openCamera() {
|
573 | 605 |
|
574 | 606 | @Override
|
575 | 607 | protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
| 608 | + super.onActivityResult(requestCode, resultCode, intent); |
| 609 | + if (requestCode == FOLDER_PICKER_CODE && resultCode == RESULT_OK) { |
| 610 | + if (intent != null) { |
| 611 | + Uri treeUri = intent.getData(); |
| 612 | + Uri docUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, |
| 613 | + DocumentsContract.getTreeDocumentId(treeUri)); |
| 614 | + Log.i("Logseq/FolderPicker", "Got uri " + docUri); |
| 615 | + String path = AndroidFileUtils.getPath(this, docUri); |
| 616 | + Log.i("picker", "Convert to path " + AndroidFileUtils.getPath(this, docUri)); |
| 617 | + if (path == null || path.isEmpty()) |
| 618 | + Toast.makeText(this, "Error with selected directory", |
| 619 | + Toast.LENGTH_LONG).show(); |
| 620 | + else |
| 621 | + saveLocalSyncPath(path); |
| 622 | + } |
| 623 | + } |
| 624 | + |
576 | 625 | if (null == uploadMessage) {
|
577 |
| - super.onActivityResult(requestCode, resultCode, intent); |
578 | 626 | return;
|
579 | 627 | }
|
580 | 628 |
|
|
0 commit comments