|
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,7 @@ 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 LOCAL_SYNC_FOLDER_CODE = 200; |
108 | 113 |
|
109 | 114 | @Override |
110 | 115 | public void onNewIntent(final Intent intent) { |
@@ -156,6 +161,32 @@ protected void onCreate(final Bundle savedInstanceState) { |
156 | 161 | AndroidBug5497Workaround.assistActivity(this); |
157 | 162 | } |
158 | 163 |
|
| 164 | + public String getLocalSyncPath() { |
| 165 | + SharedPreferences prefs = getSharedPreferences("app_prefs", MODE_PRIVATE); |
| 166 | + return prefs.getString("localSyncPath", null); |
| 167 | + } |
| 168 | + |
| 169 | + public void getStoragePermissions() { |
| 170 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) { |
| 171 | + Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION); |
| 172 | + Uri uri = Uri.fromParts("package", getPackageName(), null); |
| 173 | + intent.setData(uri); |
| 174 | + startActivity(intent); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + public void pickLocalFileSystemFolder() { |
| 179 | + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); |
| 180 | + startActivityForResult(intent, LOCAL_SYNC_FOLDER_CODE); |
| 181 | + } |
| 182 | + |
| 183 | + private void saveLocalSyncPath(String path) { |
| 184 | + SharedPreferences prefs = getSharedPreferences("app_prefs", MODE_PRIVATE); |
| 185 | + SharedPreferences.Editor editor = prefs.edit(); |
| 186 | + editor.putString("localSyncPath", path); |
| 187 | + editor.apply(); |
| 188 | + } |
| 189 | + |
159 | 190 | private void initUIElements() { |
160 | 191 | bootLogo = findViewById(R.id.bootLogo); |
161 | 192 | bootProgressBar = findViewById(R.id.progressBar); |
@@ -573,8 +604,22 @@ private void openCamera() { |
573 | 604 |
|
574 | 605 | @Override |
575 | 606 | protected void onActivityResult(int requestCode, int resultCode, Intent intent) { |
| 607 | + super.onActivityResult(requestCode, resultCode, intent); |
| 608 | + if (requestCode == LOCAL_SYNC_FOLDER_CODE && resultCode == RESULT_OK) { |
| 609 | + if (intent != null) { |
| 610 | + Uri treeUri = intent.getData(); |
| 611 | + Uri docUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, |
| 612 | + DocumentsContract.getTreeDocumentId(treeUri)); |
| 613 | + String path = AndroidFileUtils.getPath(this, docUri); |
| 614 | + if (path == null || path.isEmpty()) |
| 615 | + Toast.makeText(this, "Error with selected directory", |
| 616 | + Toast.LENGTH_LONG).show(); |
| 617 | + else |
| 618 | + saveLocalSyncPath(path); |
| 619 | + } |
| 620 | + } |
| 621 | + |
576 | 622 | if (null == uploadMessage) { |
577 | | - super.onActivityResult(requestCode, resultCode, intent); |
578 | 623 | return; |
579 | 624 | } |
580 | 625 |
|
|
0 commit comments