├── .gitignore
├── FileExplorer
├── .gitignore
├── build.gradle
├── proguard-rules.pro
├── src
│ ├── androidTest
│ │ └── java
│ │ │ └── com
│ │ │ └── magicianguo
│ │ │ └── fileexplorer
│ │ │ └── ExampleInstrumentedTest.kt
│ ├── main
│ │ ├── AndroidManifest.xml
│ │ ├── aidl
│ │ │ └── com
│ │ │ │ └── magicianguo
│ │ │ │ └── fileexplorer
│ │ │ │ ├── bean
│ │ │ │ └── BeanFile.aidl
│ │ │ │ └── userservice
│ │ │ │ └── IFileExplorerService.aidl
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── magicianguo
│ │ │ │ └── fileexplorer
│ │ │ │ ├── App.java
│ │ │ │ ├── activity
│ │ │ │ ├── BaseActivity.java
│ │ │ │ ├── MainActivity.java
│ │ │ │ └── SettingActivity.java
│ │ │ │ ├── adapter
│ │ │ │ └── FileListAdapter.java
│ │ │ │ ├── bean
│ │ │ │ └── BeanFile.java
│ │ │ │ ├── constant
│ │ │ │ ├── BundleKey.java
│ │ │ │ ├── PathType.java
│ │ │ │ └── RequestCode.java
│ │ │ │ ├── fragment
│ │ │ │ ├── EmptyFragment.java
│ │ │ │ └── FileListFragment.java
│ │ │ │ ├── observer
│ │ │ │ └── IFileItemClickObserver.java
│ │ │ │ ├── userservice
│ │ │ │ ├── FileExplorerService.java
│ │ │ │ └── FileExplorerServiceManager.java
│ │ │ │ └── util
│ │ │ │ ├── FileTools.java
│ │ │ │ ├── PermissionTools.java
│ │ │ │ ├── SPUtils.java
│ │ │ │ └── ToastUtils.java
│ │ └── res
│ │ │ ├── drawable
│ │ │ ├── bg_file_item.xml
│ │ │ ├── bg_icon_dir.xml
│ │ │ ├── bg_icon_file.xml
│ │ │ └── bg_tv_path.xml
│ │ │ ├── layout
│ │ │ ├── activity_main.xml
│ │ │ ├── activity_setting.xml
│ │ │ ├── fragment_file_list.xml
│ │ │ └── layout_file_list_item.xml
│ │ │ ├── menu
│ │ │ └── menu_main.xml
│ │ │ ├── mipmap
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── navigation
│ │ │ └── nav_main_file_list.xml
│ │ │ └── values
│ │ │ ├── colors.xml
│ │ │ ├── strings.xml
│ │ │ └── themes.xml
│ └── test
│ │ └── java
│ │ └── com
│ │ └── magicianguo
│ │ └── fileexplorer
│ │ └── ExampleUnitTest.kt
├── testks-sign.gradle
└── testks.jks
├── README.md
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── readme_pic
├── android11_data.png
├── android11_data_confirm.png
├── android13_data.png
├── android13_data_request.png
└── android13_data_tips.png
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/*
5 | /.idea/caches
6 | /.idea/libraries
7 | /.idea/modules.xml
8 | /.idea/workspace.xml
9 | /.idea/navEditor.xml
10 | /.idea/assetWizardSettings.xml
11 | .DS_Store
12 | /build
13 | /captures
14 | .externalNativeBuild
15 | .cxx
16 | local.properties
17 |
--------------------------------------------------------------------------------
/FileExplorer/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/FileExplorer/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | id 'org.jetbrains.kotlin.android'
4 | }
5 |
6 | apply from: "testks-sign.gradle"
7 |
8 | android {
9 | namespace 'com.magicianguo.fileexplorer'
10 | compileSdk 34
11 |
12 | defaultConfig {
13 | applicationId "com.magicianguo.fileexplorer"
14 | minSdk 21
15 | targetSdk 34
16 | versionCode 1
17 | versionName "1.2"
18 |
19 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
20 | }
21 | compileOptions {
22 | sourceCompatibility JavaVersion.VERSION_1_8
23 | targetCompatibility JavaVersion.VERSION_1_8
24 | }
25 | kotlinOptions {
26 | jvmTarget = '1.8'
27 | }
28 | buildFeatures {
29 | buildConfig true
30 | aidl true
31 | viewBinding true
32 | }
33 | }
34 |
35 | dependencies {
36 |
37 | implementation 'androidx.core:core-ktx:1.10.1'
38 | implementation 'com.google.android.material:material:1.10.0'
39 |
40 | def shizuku_version = "13.1.5"
41 | implementation "dev.rikka.shizuku:api:$shizuku_version"
42 | // Add this line if you want to support Shizuku
43 | implementation "dev.rikka.shizuku:provider:$shizuku_version"
44 | // 一定要加上这条依赖,SDK自带的documentfile接口有问题
45 | implementation "androidx.documentfile:documentfile:1.0.1"
46 | def nav_version = "2.7.7"
47 | // Java language implementation
48 | implementation "androidx.navigation:navigation-fragment:$nav_version"
49 | implementation "androidx.navigation:navigation-ui:$nav_version"
50 | }
--------------------------------------------------------------------------------
/FileExplorer/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
23 | -keep class com.magicianguo.fileexplorer.bean.BeanFile { *; }
24 | -keep class com.magicianguo.fileexplorer.userservice.IFileExplorerService { *; }
25 | -keep class com.magicianguo.fileexplorer.userservice.FileExplorerService
--------------------------------------------------------------------------------
/FileExplorer/src/androidTest/java/com/magicianguo/fileexplorer/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | //package com.magicianguo.fileexplorer
2 | //
3 | //import androidx.test.platform.app.InstrumentationRegistry
4 | //import androidx.test.ext.junit.runners.AndroidJUnit4
5 | //
6 | //import org.junit.Test
7 | //import org.junit.runner.RunWith
8 | //
9 | //import org.junit.Assert.*
10 | //
11 | ///**
12 | // * Instrumented test, which will execute on an Android device.
13 | // *
14 | // * See [testing documentation](http://d.android.com/tools/testing).
15 | // */
16 | //@RunWith(AndroidJUnit4::class)
17 | //class ExampleInstrumentedTest {
18 | // @Test
19 | // fun useAppContext() {
20 | // // Context of the app under test.
21 | // val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | // assertEquals("com.magicianguo.fileexplorer", appContext.packageName)
23 | // }
24 | //}
--------------------------------------------------------------------------------
/FileExplorer/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
26 |
30 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
49 |
50 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/aidl/com/magicianguo/fileexplorer/bean/BeanFile.aidl:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.bean;
2 |
3 | import com.magicianguo.fileexplorer.bean.BeanFile;
4 |
5 | parcelable BeanFile;
--------------------------------------------------------------------------------
/FileExplorer/src/main/aidl/com/magicianguo/fileexplorer/userservice/IFileExplorerService.aidl:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.userservice;
2 |
3 | import com.magicianguo.fileexplorer.bean.BeanFile;
4 |
5 | interface IFileExplorerService {
6 | List listFiles(String path);
7 | }
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/App.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer;
2 |
3 | import android.app.Activity;
4 | import android.app.Application;
5 | import android.os.Bundle;
6 |
7 | import androidx.annotation.NonNull;
8 | import androidx.annotation.Nullable;
9 |
10 | import java.io.File;
11 | import java.io.FileOutputStream;
12 | import java.util.ArrayList;
13 | import java.util.List;
14 |
15 | public class App extends Application {
16 | private static App sApp;
17 | private static final List sAliveActivityList = new ArrayList<>();
18 |
19 | public static App get() {
20 | return sApp;
21 | }
22 |
23 | @Override
24 | public void onCreate() {
25 | super.onCreate();
26 | sApp = this;
27 | createFile();
28 | registerAliveActivityList();
29 | }
30 |
31 | private void createFile() {
32 | try {
33 | String path = getExternalFilesDir(null).getParent();
34 | File file = new File(path, "test.txt");
35 | if (!file.exists()) {
36 | file.createNewFile();
37 | FileOutputStream fileOutputStream = new FileOutputStream(file);
38 | fileOutputStream.write("Hello world!".getBytes());
39 | fileOutputStream.close();
40 | }
41 | } catch (Exception e) {
42 | e.printStackTrace();
43 | }
44 | }
45 |
46 | public static List getAliveActivityList() {
47 | return sAliveActivityList;
48 | }
49 |
50 | private void registerAliveActivityList() {
51 | registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
52 | @Override
53 | public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
54 | sAliveActivityList.add(activity);
55 | }
56 |
57 | @Override
58 | public void onActivityStarted(@NonNull Activity activity) {
59 | }
60 |
61 | @Override
62 | public void onActivityResumed(@NonNull Activity activity) {
63 | }
64 |
65 | @Override
66 | public void onActivityPaused(@NonNull Activity activity) {
67 | }
68 |
69 | @Override
70 | public void onActivityStopped(@NonNull Activity activity) {
71 | }
72 |
73 | @Override
74 | public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
75 | }
76 |
77 | @Override
78 | public void onActivityDestroyed(@NonNull Activity activity) {
79 | sAliveActivityList.remove(activity);
80 | }
81 | });
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/activity/BaseActivity.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.activity;
2 |
3 | import android.content.Intent;
4 | import android.content.pm.PackageManager;
5 | import android.net.Uri;
6 | import android.os.Build;
7 | import android.os.Bundle;
8 | import android.os.Environment;
9 |
10 | import androidx.annotation.NonNull;
11 | import androidx.annotation.Nullable;
12 | import androidx.appcompat.app.AppCompatActivity;
13 | import androidx.viewbinding.ViewBinding;
14 |
15 | import com.magicianguo.fileexplorer.constant.PathType;
16 | import com.magicianguo.fileexplorer.constant.RequestCode;
17 | import com.magicianguo.fileexplorer.userservice.FileExplorerServiceManager;
18 | import com.magicianguo.fileexplorer.util.FileTools;
19 | import com.magicianguo.fileexplorer.util.PermissionTools;
20 |
21 | import rikka.shizuku.Shizuku;
22 |
23 | public abstract class BaseActivity extends AppCompatActivity
24 | implements Shizuku.OnRequestPermissionResultListener {
25 | protected final String TAG = this.getClass().getSimpleName();
26 | protected VB binding;
27 |
28 | @Override
29 | protected void onCreate(@Nullable Bundle savedInstanceState) {
30 | super.onCreate(savedInstanceState);
31 | binding = onBinding();
32 | setContentView(binding.getRoot());
33 | if (PermissionTools.isShizukuAvailable()) {
34 | Shizuku.addRequestPermissionResultListener(this);
35 | }
36 | }
37 |
38 | @Override
39 | protected void onDestroy() {
40 | super.onDestroy();
41 | if (PermissionTools.isShizukuAvailable()) {
42 | Shizuku.removeRequestPermissionResultListener(this);
43 | }
44 | binding = null;
45 | }
46 |
47 | @Override
48 | public void onRequestPermissionResult(int requestCode, int grantResult) {
49 | if (requestCode == RequestCode.SHIZUKU) {
50 | if (grantResult == PackageManager.PERMISSION_GRANTED) {
51 | FileTools.specialPathReadType = PathType.SHIZUKU;
52 | FileExplorerServiceManager.bindService();
53 | }
54 | }
55 | }
56 |
57 | @Override
58 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
59 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
60 | if (requestCode == RequestCode.STORAGE) {
61 | onStoragePermissionResult(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED);
62 | }
63 | }
64 |
65 | @Override
66 | protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
67 | super.onActivityResult(requestCode, resultCode, data);
68 | if (requestCode == RequestCode.STORAGE) {
69 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
70 | onStoragePermissionResult(Environment.isExternalStorageManager());
71 | }
72 | } else if (requestCode == RequestCode.DOCUMENT) {
73 | Uri uri;
74 | if (data != null && (uri = data.getData()) != null) {
75 | getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
76 | onDocumentPermissionResult(true);
77 | } else {
78 | onDocumentPermissionResult(false);
79 | }
80 | }
81 | }
82 |
83 | protected void onStoragePermissionResult(boolean granted) {
84 | }
85 |
86 | protected void onDocumentPermissionResult(boolean granted) {
87 | }
88 |
89 | protected abstract VB onBinding();
90 | }
91 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/activity/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.activity;
2 |
3 | import android.content.ClipData;
4 | import android.content.ClipDescription;
5 | import android.content.ClipboardManager;
6 | import android.content.Intent;
7 | import android.os.Build;
8 | import android.os.Bundle;
9 | import android.os.Parcelable;
10 | import android.text.TextUtils;
11 | import android.view.Menu;
12 | import android.view.MenuItem;
13 |
14 | import androidx.annotation.NonNull;
15 | import androidx.annotation.Nullable;
16 | import androidx.appcompat.app.AlertDialog;
17 | import androidx.navigation.NavController;
18 | import androidx.navigation.fragment.NavHostFragment;
19 |
20 | import com.magicianguo.fileexplorer.R;
21 | import com.magicianguo.fileexplorer.bean.BeanFile;
22 | import com.magicianguo.fileexplorer.constant.BundleKey;
23 | import com.magicianguo.fileexplorer.constant.PathType;
24 | import com.magicianguo.fileexplorer.databinding.ActivityMainBinding;
25 | import com.magicianguo.fileexplorer.observer.IFileItemClickObserver;
26 | import com.magicianguo.fileexplorer.userservice.FileExplorerServiceManager;
27 | import com.magicianguo.fileexplorer.util.FileTools;
28 | import com.magicianguo.fileexplorer.util.PermissionTools;
29 | import com.magicianguo.fileexplorer.util.SPUtils;
30 | import com.magicianguo.fileexplorer.util.ToastUtils;
31 |
32 | import java.io.File;
33 | import java.util.ArrayList;
34 | import java.util.List;
35 |
36 |
37 | public class MainActivity extends BaseActivity {
38 | private long mLastPressBackTime = 0L;
39 | private String mPathCache = FileTools.ROOT_PATH;
40 | private File mDirectory;
41 |
42 | private NavController mNavController;
43 |
44 | private final IFileItemClickObserver mFileItemClickObserver = new IFileItemClickObserver() {
45 | @Override
46 | public void onClickDir(String path) {
47 | loadPath(path, true);
48 | }
49 | };
50 |
51 | @Override
52 | protected void onCreate(@Nullable Bundle savedInstanceState) {
53 | super.onCreate(savedInstanceState);
54 | initView();
55 | checkStoragePermission();
56 | FileTools.addFileItemClickObserver(mFileItemClickObserver);
57 | }
58 |
59 | @Override
60 | protected void onResume() {
61 | super.onResume();
62 | loadPath(mPathCache, false);
63 | }
64 |
65 | @Override
66 | protected void onDestroy() {
67 | super.onDestroy();
68 | FileTools.removeFileItemClickObserver(mFileItemClickObserver);
69 | }
70 |
71 | private void initView() {
72 | NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fcv_file_list);
73 | if (navHostFragment != null) {
74 | mNavController = navHostFragment.getNavController();
75 | }
76 | binding.btnBack.setOnClickListener(v -> {
77 | if (!FileTools.ROOT_PATH.equals(mDirectory.getPath())) {
78 | mDirectory = mDirectory.getParentFile();
79 | assert mDirectory != null;
80 | mPathCache = mDirectory.getPath();
81 | binding.tvPath.setText(mPathCache);
82 | mNavController.popBackStack();
83 | }
84 | });
85 | binding.tvPath.setOnLongClickListener(v -> {
86 | ClipboardManager manager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
87 | manager.setPrimaryClip(new ClipData(new ClipDescription("", new String[] {ClipDescription.MIMETYPE_TEXT_PLAIN}),
88 | new ClipData.Item(binding.tvPath.getText())));
89 | ToastUtils.shortCall(R.string.toast_path_copied_to_clip);
90 | return true;
91 | });
92 | }
93 |
94 | private void loadPath(String path, boolean isUserClicked) {
95 | if (path == null) {
96 | return;
97 | }
98 | boolean isNavigate = !TextUtils.equals(mPathCache, path);
99 | mPathCache = path;
100 | if (FileTools.shouldRequestUriPermission(path)) {
101 | if (isUserClicked) {
102 | showRequestUriPermissionDialog();
103 | }
104 | } else {
105 | mDirectory = new File(path);
106 | binding.tvPath.setText(mDirectory.getPath());
107 | List list = FileTools.getSortedFileList(path);
108 | Bundle bundle = new Bundle();
109 | bundle.putParcelableArrayList(BundleKey.FILE_LIST, (ArrayList extends Parcelable>) list);
110 | if (!isNavigate) {
111 | mNavController.popBackStack();
112 | }
113 | mNavController.navigate(R.id.fileListFragment, bundle);
114 | }
115 | }
116 |
117 | @Override
118 | protected ActivityMainBinding onBinding() {
119 | return ActivityMainBinding.inflate(getLayoutInflater());
120 | }
121 |
122 | private void showStoragePermissionDialog() {
123 | new AlertDialog.Builder(this)
124 | .setCancelable(false)
125 | .setMessage(R.string.dialog_storage_message)
126 | .setPositiveButton(R.string.dialog_button_request_permission, (dialog, which) -> {
127 | PermissionTools.requestStoragePermission(this);
128 | })
129 | .setNegativeButton(R.string.dialog_button_cancel, (dialog, which) -> {
130 | finish();
131 | }).create().show();
132 | }
133 |
134 | private void showRequestUriPermissionDialog() {
135 | new AlertDialog.Builder(this)
136 | .setCancelable(false)
137 | .setMessage(R.string.dialog_need_uri_permission_message)
138 | .setPositiveButton(R.string.dialog_button_request_permission, (dialog, which) -> {
139 | FileTools.requestUriPermission(this, mPathCache);
140 | })
141 | .setNegativeButton(R.string.dialog_button_cancel, (dialog, which) -> {
142 | }).create().show();
143 | }
144 |
145 | private void checkStoragePermission() {
146 | if (PermissionTools.hasStoragePermission()) {
147 | loadPath(mPathCache, false);
148 | if (!SPUtils.getUseNewDocument()) {
149 | checkShizukuPermission();
150 | }
151 | } else {
152 | showStoragePermissionDialog();
153 | }
154 | }
155 |
156 | private void checkShizukuPermission() {
157 | // 安卓11以下不需要Shizuku,使用File接口就能浏览/sdcard全部文件
158 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
159 | if (PermissionTools.isShizukuAvailable()) {
160 | if (PermissionTools.hasShizukuPermission()) {
161 | FileTools.specialPathReadType = PathType.SHIZUKU;
162 | FileExplorerServiceManager.bindService();
163 | } else {
164 | PermissionTools.requestShizukuPermission();
165 | }
166 | }
167 | }
168 | }
169 |
170 | @Override
171 | protected void onStoragePermissionResult(boolean granted) {
172 | if (granted) {
173 | loadPath(mPathCache, false);
174 | checkShizukuPermission();
175 | } else {
176 | showStoragePermissionDialog();
177 | }
178 | }
179 |
180 | @Override
181 | protected void onDocumentPermissionResult(boolean granted) {
182 | if (granted) {
183 | loadPath(mPathCache, false);
184 | ToastUtils.shortCall(R.string.toast_permission_granted);
185 | } else {
186 | ToastUtils.shortCall(R.string.toast_permission_not_granted);
187 | }
188 | }
189 |
190 | @Override
191 | public void onBackPressed() {
192 | if (!FileTools.ROOT_PATH.equals(mDirectory.getPath())) {
193 | mDirectory = mDirectory.getParentFile();
194 | assert mDirectory != null;
195 | mPathCache = mDirectory.getPath();
196 | binding.tvPath.setText(mPathCache);
197 | super.onBackPressed();
198 | } else {
199 | long time = System.currentTimeMillis();
200 | if (time - mLastPressBackTime < 2000L) {
201 | super.onBackPressed();
202 | finish();
203 | } else {
204 | ToastUtils.shortCall(R.string.toast_press_back_again_to_exit);
205 | }
206 | mLastPressBackTime = time;
207 | }
208 | }
209 |
210 | @Override
211 | public boolean onOptionsItemSelected(@NonNull MenuItem item) {
212 | if (item.getItemId() == R.id.setting) {
213 | startActivity(new Intent(this, SettingActivity.class));
214 | return true;
215 | }
216 | return super.onOptionsItemSelected(item);
217 | }
218 |
219 | @Override
220 | public boolean onCreateOptionsMenu(Menu menu) {
221 | getMenuInflater().inflate(R.menu.menu_main, menu);
222 | return true;
223 | }
224 | }
225 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/activity/SettingActivity.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.activity;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.app.Activity;
5 | import android.os.Bundle;
6 | import android.os.Process;
7 | import android.view.MotionEvent;
8 |
9 | import androidx.appcompat.app.AlertDialog;
10 |
11 | import com.magicianguo.fileexplorer.App;
12 | import com.magicianguo.fileexplorer.databinding.ActivitySettingBinding;
13 | import com.magicianguo.fileexplorer.util.SPUtils;
14 |
15 | public class SettingActivity extends BaseActivity {
16 | @Override
17 | @SuppressLint("ClickableViewAccessibility")
18 | protected void onCreate(Bundle savedInstanceState) {
19 | super.onCreate(savedInstanceState);
20 | binding.swNewDocument.setChecked(SPUtils.getUseNewDocument());
21 | binding.swNewDocument.setOnTouchListener((v, event) -> {
22 | if (event.getAction() == MotionEvent.ACTION_DOWN) {
23 | v.setTag("clicked");
24 | }
25 | return false;
26 | });
27 | binding.swNewDocument.setOnCheckedChangeListener((buttonView, isChecked) -> {
28 | if (!"clicked".equals(buttonView.getTag())) {
29 | return;
30 | }
31 | buttonView.setTag("");
32 | new AlertDialog.Builder(this)
33 | .setMessage("切换后需要重新打开文件管理")
34 | .setPositiveButton("确定", (dialog, which) -> {
35 | buttonView.setChecked(isChecked);
36 | SPUtils.setUseNewDocument(isChecked);
37 | buttonView.postDelayed(() -> {
38 | for (Activity activity : App.getAliveActivityList()) {
39 | activity.finish();
40 | }
41 | Process.killProcess(Process.myPid());
42 | }, 200L);
43 | })
44 | .setNegativeButton("取消", (dialog, which) -> {
45 | buttonView.setChecked(!isChecked);
46 | })
47 | .show();
48 | });
49 | }
50 |
51 | @Override
52 | protected ActivitySettingBinding onBinding() {
53 | return ActivitySettingBinding.inflate(getLayoutInflater());
54 | }
55 | }
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/adapter/FileListAdapter.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.adapter;
2 |
3 | import android.content.pm.PackageManager;
4 | import android.graphics.Color;
5 | import android.graphics.drawable.Drawable;
6 | import android.view.LayoutInflater;
7 | import android.view.ViewGroup;
8 | import android.widget.ImageView;
9 | import android.widget.TextView;
10 |
11 | import androidx.annotation.NonNull;
12 | import androidx.recyclerview.widget.RecyclerView;
13 |
14 | import com.magicianguo.fileexplorer.App;
15 | import com.magicianguo.fileexplorer.R;
16 | import com.magicianguo.fileexplorer.bean.BeanFile;
17 |
18 | import java.util.ArrayList;
19 | import java.util.List;
20 |
21 | public class FileListAdapter extends RecyclerView.Adapter {
22 | private final List mList = new ArrayList<>();
23 | private IItemClickListener mListener;
24 |
25 | private final PackageManager PACKAGE_MANAGER = App.get().getPackageManager();
26 |
27 | public void setListener(IItemClickListener listener) {
28 | mListener = listener;
29 | }
30 |
31 | public void updateList(List list) {
32 | mList.clear();
33 | mList.addAll(list);
34 | notifyDataSetChanged();
35 | }
36 |
37 |
38 | @NonNull
39 | @Override
40 | public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
41 | return new FileListHolder(parent);
42 | }
43 |
44 | @Override
45 | public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
46 | if (holder instanceof FileListHolder) {
47 | FileListHolder fileListHolder = (FileListHolder) holder;
48 | BeanFile file = mList.get(position);
49 | fileListHolder.tvName.setText(file.name);
50 | if (file.isGrantedPath) {
51 | fileListHolder.tvName.setTextColor(Color.BLUE);
52 | } else {
53 | fileListHolder.tvName.setTextColor(Color.BLACK);
54 | }
55 | if (file.isDir) {
56 | fileListHolder.tvIcon.setText(R.string.file_item_icon_txt_dir);
57 | fileListHolder.tvIcon.setBackgroundResource(R.drawable.bg_icon_dir);
58 | } else {
59 | fileListHolder.tvIcon.setText(R.string.file_item_icon_txt_file);
60 | fileListHolder.tvIcon.setBackgroundResource(R.drawable.bg_icon_file);
61 | }
62 | if (file.pathPackageName != null) {
63 | fileListHolder.ivSmall.setImageDrawable(getAppIconDrawable(file));
64 | } else {
65 | fileListHolder.ivSmall.setImageDrawable(null);
66 | }
67 | fileListHolder.itemView.setOnClickListener(v -> {
68 | if (mListener != null && file.isDir) {
69 | mListener.onClickDir(file.path);
70 | }
71 | });
72 | }
73 | }
74 |
75 | @Override
76 | public int getItemCount() {
77 | return mList.size();
78 | }
79 |
80 | private Drawable getAppIconDrawable(BeanFile file) {
81 | try {
82 | return PACKAGE_MANAGER.getApplicationIcon(file.pathPackageName);
83 | } catch (PackageManager.NameNotFoundException e) {
84 | e.printStackTrace();
85 | }
86 | return null;
87 | }
88 |
89 | private static class FileListHolder extends RecyclerView.ViewHolder {
90 | TextView tvIcon = itemView.findViewById(R.id.tv_icon);
91 | ImageView ivSmall = itemView.findViewById(R.id.iv_small_icon);
92 | TextView tvName = itemView.findViewById(R.id.tv_name);
93 |
94 | public FileListHolder(ViewGroup parent) {
95 | super(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_file_list_item, parent, false));
96 | }
97 | }
98 |
99 | public interface IItemClickListener {
100 | void onClickDir(String path);
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/bean/BeanFile.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.bean;
2 |
3 | import android.os.Parcel;
4 | import android.os.Parcelable;
5 |
6 | import androidx.annotation.NonNull;
7 |
8 | public class BeanFile implements Parcelable {
9 | public BeanFile(String name, String path, boolean isDir, boolean isGrantedPath, String pathPackageName) {
10 | this.name = name;
11 | this.path = path;
12 | this.isDir = isDir;
13 | this.isGrantedPath = isGrantedPath;
14 | this.pathPackageName = pathPackageName;
15 | }
16 |
17 | /**
18 | * 文件名
19 | */
20 | public String name;
21 | /**
22 | * 文件路径
23 | */
24 | public String path;
25 | /**
26 | * 是否文件夹
27 | */
28 | public boolean isDir;
29 | /**
30 | * 是否被Document授权的路径
31 | */
32 | public boolean isGrantedPath;
33 | /**
34 | * 如果文件夹名称是应用包名,则将包名保存到该字段
35 | */
36 | public String pathPackageName;
37 |
38 | protected BeanFile(Parcel in) {
39 | name = in.readString();
40 | path = in.readString();
41 | isDir = in.readByte() != 0;
42 | isGrantedPath = in.readByte() != 0;
43 | pathPackageName = in.readString();
44 | }
45 |
46 | public static final Creator CREATOR = new Creator() {
47 | @Override
48 | public BeanFile createFromParcel(Parcel in) {
49 | return new BeanFile(in);
50 | }
51 |
52 | @Override
53 | public BeanFile[] newArray(int size) {
54 | return new BeanFile[size];
55 | }
56 | };
57 |
58 | @Override
59 | public int describeContents() {
60 | return 0;
61 | }
62 |
63 | @Override
64 | public void writeToParcel(@NonNull Parcel dest, int flags) {
65 | dest.writeString(name);
66 | dest.writeString(path);
67 | dest.writeByte((byte) (isDir ? 1 : 0));
68 | dest.writeByte((byte) (isGrantedPath ? 1 : 0));
69 | dest.writeString(pathPackageName);
70 | }
71 |
72 | @NonNull
73 | @Override
74 | public String toString() {
75 | return "BeanFile{" +
76 | "name='" + name + '\'' +
77 | ", path='" + path + '\'' +
78 | ", isDir=" + isDir +
79 | ", isGrantedPath=" + isGrantedPath +
80 | ", pathPackageName='" + pathPackageName + '\'' +
81 | '}';
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/constant/BundleKey.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.constant;
2 |
3 | public interface BundleKey {
4 | String FILE_LIST = "FILE_LIST";
5 | }
6 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/constant/PathType.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.constant;
2 |
3 | import androidx.annotation.IntDef;
4 |
5 | public interface PathType {
6 | /**
7 | * 通过File接口,访问一般路径
8 | */
9 | int FILE = 0;
10 | /**
11 | * 通过Document API 访问特殊路径
12 | */
13 | int DOCUMENT = 1;
14 | /**
15 | * 安卓13及以上,直接用包名展示data、obb下的目录(因为data、obb不能直接授权了,只能对子目录授权)
16 | */
17 | int PACKAGE_NAME = 2;
18 | /**
19 | * 通过Shizuku授权访问特殊路径
20 | */
21 | int SHIZUKU = 3;
22 |
23 | @IntDef({ FILE, DOCUMENT, PACKAGE_NAME, SHIZUKU })
24 | @interface PathType1 {
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/constant/RequestCode.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.constant;
2 |
3 | public interface RequestCode {
4 | int STORAGE = 0;
5 | int DOCUMENT = 1;
6 | int SHIZUKU = 2;
7 | }
8 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/fragment/EmptyFragment.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.fragment;
2 |
3 | import androidx.fragment.app.Fragment;
4 |
5 | public class EmptyFragment extends Fragment {
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/fragment/FileListFragment.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.fragment;
2 |
3 | import android.os.Bundle;
4 | import android.os.Parcelable;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 |
9 | import androidx.annotation.NonNull;
10 | import androidx.annotation.Nullable;
11 | import androidx.fragment.app.Fragment;
12 | import androidx.recyclerview.widget.LinearLayoutManager;
13 | import androidx.recyclerview.widget.RecyclerView;
14 |
15 | import com.magicianguo.fileexplorer.R;
16 | import com.magicianguo.fileexplorer.adapter.FileListAdapter;
17 | import com.magicianguo.fileexplorer.bean.BeanFile;
18 | import com.magicianguo.fileexplorer.constant.BundleKey;
19 | import com.magicianguo.fileexplorer.util.FileTools;
20 |
21 | import java.util.ArrayList;
22 |
23 | public class FileListFragment extends Fragment {
24 | private final FileListAdapter mAdapter = new FileListAdapter();
25 |
26 | @Nullable
27 | @Override
28 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
29 | return inflater.inflate(R.layout.fragment_file_list, container, false);
30 | }
31 |
32 | @Override
33 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
34 | RecyclerView rvList = view.findViewById(R.id.rv_files);
35 | mAdapter.setListener(new FileListAdapter.IItemClickListener() {
36 | @Override
37 | public void onClickDir(String path) {
38 | FileTools.notifyClickDir(path);
39 | }
40 | });
41 | rvList.setLayoutManager(new LinearLayoutManager(requireContext()));
42 | rvList.setAdapter(mAdapter);
43 | Bundle arguments = getArguments();
44 | if (arguments != null) {
45 | ArrayList list = arguments.getParcelableArrayList(BundleKey.FILE_LIST);
46 | mAdapter.updateList(list);
47 | }
48 |
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/observer/IFileItemClickObserver.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.observer;
2 |
3 | public interface IFileItemClickObserver {
4 | void onClickDir(String path);
5 | }
6 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/userservice/FileExplorerService.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.userservice;
2 |
3 | import android.os.RemoteException;
4 | import com.magicianguo.fileexplorer.bean.BeanFile;
5 |
6 | import java.io.File;
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | public class FileExplorerService extends IFileExplorerService.Stub {
11 | private static final String TAG = "FileExplorerService";
12 |
13 | @Override
14 | public List listFiles(String path) throws RemoteException {
15 | List list = new ArrayList<>();
16 | File[] files = new File(path).listFiles();
17 | if (files != null) {
18 | for (File f : files) {
19 | list.add(new BeanFile(f.getName(), f.getPath(), f.isDirectory(), false, f.getName()));
20 | }
21 | }
22 | return list;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/userservice/FileExplorerServiceManager.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.userservice;
2 |
3 | import android.content.ComponentName;
4 | import android.content.ServiceConnection;
5 | import android.os.IBinder;
6 | import android.util.Log;
7 |
8 | import com.magicianguo.fileexplorer.App;
9 | import com.magicianguo.fileexplorer.BuildConfig;
10 | import com.magicianguo.fileexplorer.R;
11 | import com.magicianguo.fileexplorer.util.FileTools;
12 | import com.magicianguo.fileexplorer.util.ToastUtils;
13 |
14 | import rikka.shizuku.Shizuku;
15 |
16 | public class FileExplorerServiceManager {
17 | private static final String TAG = "FileExplorerServiceManager";
18 | private static boolean isBind = false;
19 |
20 | private static final Shizuku.UserServiceArgs USER_SERVICE_ARGS = new Shizuku.UserServiceArgs(
21 | new ComponentName(App.get().getPackageName(), FileExplorerService.class.getName())
22 | ).daemon(false).debuggable(BuildConfig.DEBUG).processNameSuffix("file_explorer_service").version(1);
23 |
24 | private static final ServiceConnection SERVICE_CONNECTION = new ServiceConnection() {
25 | @Override
26 | public void onServiceConnected(ComponentName name, IBinder service) {
27 | Log.d(TAG, "onServiceConnected: ");
28 | isBind = true;
29 | FileTools.iFileExplorerService = IFileExplorerService.Stub.asInterface(service);
30 | ToastUtils.shortCall(R.string.toast_shizuku_connected);
31 | }
32 |
33 | @Override
34 | public void onServiceDisconnected(ComponentName name) {
35 | Log.d(TAG, "onServiceDisconnected: ");
36 | isBind = false;
37 | FileTools.iFileExplorerService = null;
38 | ToastUtils.shortCall(R.string.toast_shizuku_disconnected);
39 | }
40 | };
41 |
42 | public static void bindService() {
43 | Log.d(TAG, "bindService: isBind = " + isBind);
44 | if (!isBind) {
45 | Shizuku.bindUserService(USER_SERVICE_ARGS, SERVICE_CONNECTION);
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/util/FileTools.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.util;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.content.UriPermission;
6 | import android.content.pm.PackageInfo;
7 | import android.content.pm.PackageManager;
8 | import android.net.Uri;
9 | import android.os.Build;
10 | import android.os.Environment;
11 | import android.os.RemoteException;
12 | import android.provider.DocumentsContract;
13 | import android.util.Log;
14 |
15 | import androidx.documentfile.provider.DocumentFile;
16 |
17 | import com.magicianguo.fileexplorer.App;
18 | import com.magicianguo.fileexplorer.R;
19 | import com.magicianguo.fileexplorer.bean.BeanFile;
20 | import com.magicianguo.fileexplorer.constant.PathType;
21 | import com.magicianguo.fileexplorer.constant.RequestCode;
22 | import com.magicianguo.fileexplorer.observer.IFileItemClickObserver;
23 | import com.magicianguo.fileexplorer.userservice.IFileExplorerService;
24 |
25 | import java.io.File;
26 | import java.util.ArrayList;
27 | import java.util.Comparator;
28 | import java.util.List;
29 |
30 | import kotlin.collections.CollectionsKt;
31 |
32 | public class FileTools {
33 | public static final String ROOT_PATH = Environment.getExternalStorageDirectory().getPath();
34 | public static int specialPathReadType = PathType.DOCUMENT;
35 |
36 | private static final PackageManager PACKAGE_MANAGER = App.get().getPackageManager();
37 |
38 | private static final Comparator COMPARATOR = new Comparator() {
39 | @Override
40 | public int compare(BeanFile o1, BeanFile o2) {
41 | String name1 = o1.name;
42 | String name2 = o2.name;
43 | int compareCount = Math.max(name1.length(), name2.length());
44 | for (int i = 0; i < compareCount; i++) {
45 | int code1 = getCharCode(name1, i);
46 | int code2 = getCharCode(name2, i);
47 | if (code1 != code2) {
48 | return code1 - code2;
49 | }
50 | }
51 | return 0;
52 | }
53 |
54 | private int getCharCode(String str, int index) {
55 | if (index >= str.length()) {
56 | return -1;
57 | }
58 | char c = str.charAt(index);
59 | if (Character.isLetter(c)) {
60 | if (Character.isLowerCase(c)) {
61 | return Character.toUpperCase(c);
62 | } else {
63 | return c;
64 | }
65 | } else {
66 | return -1;
67 | }
68 | }
69 | };
70 |
71 | public static IFileExplorerService iFileExplorerService;
72 |
73 | public static List getSortedFileList(String path) {
74 | List fileList = getFileList(path);
75 | CollectionsKt.sortWith(fileList, COMPARATOR);
76 | return fileList;
77 | }
78 |
79 | private static List getFileList(String path) {
80 | int type = getPathType(path);
81 | if (type == PathType.SHIZUKU) {
82 | return getFileListByShizuku(path);
83 | }
84 | if (type == PathType.DOCUMENT) {
85 | return getFileListByDocument(path);
86 | }
87 | if (type == PathType.PACKAGE_NAME) {
88 | return getPackageNameFileList(path);
89 | }
90 | return getFileListByFile(path);
91 | }
92 |
93 | private static List getFileListByFile(String path) {
94 | boolean isPkgNamePath = isDataPath(path) || isObbPath(path);
95 | List list = new ArrayList<>();
96 | File dir = new File(path);
97 | File[] files;
98 | if ((files = dir.listFiles()) != null) {
99 | for (File file : files) {
100 | list.add(new BeanFile(file.getName(), file.getPath(), file.isDirectory(), false, isPkgNamePath ? file.getName() : null));
101 | }
102 | }
103 | return list;
104 | }
105 |
106 | private static List getFileListByDocument(String path) {
107 | Uri pathUri = pathToUri(path);
108 | Log.d("TAG", "getFileListByDocument: pathUri = "+pathUri);
109 | DocumentFile documentFile = DocumentFile.fromTreeUri(App.get(), pathUri);
110 | List list = new ArrayList<>();
111 | if (documentFile != null) {
112 | DocumentFile[] documentFiles = documentFile.listFiles();
113 | for (DocumentFile df : documentFiles) {
114 | String fName = df.getName();
115 | String fPath = path + "/" + fName;
116 | list.add(new BeanFile(fName, fPath, df.isDirectory(), false, getPathPackageName(fName)));
117 | }
118 | }
119 | return list;
120 | }
121 |
122 | private static List getFileListByShizuku(String path) {
123 | try {
124 | return iFileExplorerService.listFiles(path);
125 | } catch (NullPointerException | RemoteException e) {
126 | ToastUtils.longCall(R.string.toast_shizuku_load_file_failed);
127 | e.printStackTrace();
128 | }
129 | return new ArrayList<>();
130 | }
131 |
132 | private static List getPackageNameFileList(String path) {
133 | List list = new ArrayList<>();
134 | List installedPackages = PACKAGE_MANAGER.getInstalledPackages(0);
135 | for (PackageInfo packageInfo : installedPackages) {
136 | String packageName = packageInfo.packageName;
137 | String dirPath = path + "/" + packageName;
138 | File dir = new File(dirPath);
139 | if (dir.exists()) {
140 | list.add(new BeanFile(packageName, dirPath, true, hasUriPermission(dirPath) || isFromMyPackageNamePath(dirPath), packageName));
141 | }
142 | }
143 | return list;
144 | }
145 |
146 | public static boolean shouldRequestUriPermission(String path) {
147 | if (getPathType(path) != PathType.DOCUMENT) {
148 | return false;
149 | }
150 | return !hasUriPermission(path);
151 | }
152 |
153 | @PathType.PathType1
154 | private static int getPathType(String path) {
155 | if (isFromMyPackageNamePath(path)) {
156 | return PathType.FILE;
157 | }
158 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && !SPUtils.getUseNewDocument()) {
159 | if (isDataPath(path) || isObbPath(path)) {
160 | if (specialPathReadType == PathType.SHIZUKU) {
161 | return PathType.SHIZUKU;
162 | } else {
163 | return PathType.PACKAGE_NAME;
164 | }
165 | } else if (isUnderDataPath(path) || isUnderObbPath(path)) {
166 | if (specialPathReadType == PathType.SHIZUKU) {
167 | return PathType.SHIZUKU;
168 | } else {
169 | return PathType.DOCUMENT;
170 | }
171 | } else {
172 | return PathType.FILE;
173 | }
174 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
175 | if (isDataPath(path) || isObbPath(path) || isUnderDataPath(path) || isUnderObbPath(path)) {
176 | if (specialPathReadType == PathType.SHIZUKU) {
177 | return PathType.SHIZUKU;
178 | } else {
179 | return PathType.DOCUMENT;
180 | }
181 | } else {
182 | return PathType.FILE;
183 | }
184 | } else {
185 | return PathType.FILE;
186 | }
187 | }
188 |
189 | private static boolean hasUriPermission(String path) {
190 | List uriPermissions = App.get().getContentResolver().getPersistedUriPermissions();
191 | Log.d("TAG", "hasUriPermission: uriPermissions = " + uriPermissions);
192 | String uriPath = pathToUri(path).getPath();
193 | Log.d("TAG", "hasUriPermission: uriPath = "+uriPath);
194 | for (UriPermission uriPermission : uriPermissions) {
195 | String itemPath = uriPermission.getUri().getPath();
196 | Log.d("TAG", "hasUriPermission: itemPath = " + itemPath);
197 | if (uriPath != null && itemPath != null && (uriPath + "/").contains(itemPath + "/")) {
198 | return true;
199 | }
200 | }
201 | return false;
202 | }
203 |
204 | public static void requestUriPermission(Activity activity, String path) {
205 | Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
206 | intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
207 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
208 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
209 | Uri treeUri = pathToUri(path);
210 | DocumentFile df = DocumentFile.fromTreeUri(activity, treeUri);
211 | if (df != null) {
212 | intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, df.getUri());
213 | }
214 | }
215 | activity.startActivityForResult(intent, RequestCode.DOCUMENT);
216 | }
217 |
218 | private static Uri pathToUri(String path) {
219 | String halfPath = path.replace(ROOT_PATH + "/", "");
220 | String[] segments = halfPath.split("/");
221 | Uri.Builder uriBuilder = new Uri.Builder()
222 | .scheme("content")
223 | .authority("com.android.externalstorage.documents")
224 | .appendPath("tree");
225 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
226 | if (SPUtils.getUseNewDocument()) {
227 | uriBuilder.appendPath("primary:A\u200Bndroid/" + segments[1]);
228 | } else {
229 | uriBuilder.appendPath("primary:Android/" + segments[1] + "/" + segments[2]);
230 | }
231 | } else {
232 | uriBuilder.appendPath("primary:Android/" + segments[1]);
233 | }
234 | uriBuilder.appendPath("document");
235 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && SPUtils.getUseNewDocument()) {
236 | uriBuilder.appendPath("primary:A\u200Bndroid/" + halfPath.replace("Android/", ""));
237 | } else {
238 | uriBuilder.appendPath("primary:" + halfPath);
239 | }
240 | return uriBuilder.build();
241 | }
242 |
243 | private static boolean isFromMyPackageNamePath(String path) {
244 | return (path + "/").contains(ROOT_PATH + "/Android/data/"+ App.get().getPackageName()+"/");
245 | }
246 |
247 | private static boolean isDataPath(String path) {
248 | return (ROOT_PATH + "/Android/data").equals(path);
249 | }
250 |
251 | private static boolean isObbPath(String path) {
252 | return (ROOT_PATH + "/Android/obb").equals(path);
253 | }
254 |
255 | private static boolean isUnderDataPath(String path) {
256 | return path.contains(ROOT_PATH + "/Android/data/");
257 | }
258 |
259 | private static boolean isUnderObbPath(String path) {
260 | return path.contains(ROOT_PATH + "/Android/obb/");
261 | }
262 |
263 | /**
264 | * 如果字符串是应用包名,返回字符串,反之返回null
265 | */
266 | public static String getPathPackageName(String name) {
267 | try {
268 | PACKAGE_MANAGER.getPackageInfo(name, 0);
269 | return name;
270 | } catch (PackageManager.NameNotFoundException ignore) {
271 | }
272 | return null;
273 | }
274 |
275 | private static List sFileItemClickObservers = new ArrayList<>();
276 |
277 | public static void addFileItemClickObserver(IFileItemClickObserver observer) {
278 | sFileItemClickObservers.add(observer);
279 | }
280 |
281 | public static void removeFileItemClickObserver(IFileItemClickObserver observer) {
282 | sFileItemClickObservers.remove(observer);
283 | }
284 |
285 | public static void notifyClickDir(String path) {
286 | for (IFileItemClickObserver observer : sFileItemClickObservers) {
287 | observer.onClickDir(path);
288 | }
289 | }
290 |
291 | }
292 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/util/PermissionTools.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.util;
2 |
3 | import android.Manifest;
4 | import android.app.Activity;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.content.pm.PackageManager;
8 | import android.net.Uri;
9 | import android.os.Build;
10 | import android.os.Environment;
11 | import android.provider.Settings;
12 |
13 | import com.magicianguo.fileexplorer.App;
14 | import com.magicianguo.fileexplorer.constant.RequestCode;
15 |
16 | import rikka.shizuku.Shizuku;
17 |
18 | public class PermissionTools {
19 | private static final String SHIZUKU_PACKAGE_NAME = "moe.shizuku.privileged.api";
20 |
21 | public static boolean hasStoragePermission() {
22 | Context context = App.get();
23 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
24 | return Environment.isExternalStorageManager();
25 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
26 | return context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
27 | PackageManager.PERMISSION_GRANTED;
28 | } else {
29 | return true;
30 | }
31 | }
32 |
33 | public static void requestStoragePermission(Activity activity) {
34 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
35 | Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
36 | .setData(Uri.parse("package:"+activity.getPackageName()));
37 | activity.startActivityForResult(intent, RequestCode.STORAGE);
38 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
39 | activity.requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE } , RequestCode.STORAGE);
40 | }
41 | }
42 |
43 | private static boolean isShizukuInstalled() {
44 | try {
45 | App.get().getPackageManager().getPackageInfo(SHIZUKU_PACKAGE_NAME, 0);
46 | return true;
47 | } catch (PackageManager.NameNotFoundException e) {
48 | e.printStackTrace();
49 | }
50 | return false;
51 | }
52 |
53 | public static boolean isShizukuAvailable() {
54 | return isShizukuInstalled() && Shizuku.pingBinder();
55 | }
56 |
57 | public static boolean hasShizukuPermission() {
58 | return Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED;
59 | }
60 |
61 | /**
62 | * 请求权限。
63 | * @return Shizuku是否可用
64 | */
65 | public static void requestShizukuPermission() {
66 | Shizuku.requestPermission(RequestCode.SHIZUKU);
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/util/SPUtils.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.util;
2 |
3 | import android.content.Context;
4 | import android.content.SharedPreferences;
5 |
6 | import com.magicianguo.fileexplorer.App;
7 |
8 | public class SPUtils {
9 | private static final SharedPreferences SP = App.get().getSharedPreferences("SPUtils", Context.MODE_PRIVATE);
10 | private static final String KEY_USE_NEW_DOCUMENT = "use_new_document";
11 |
12 | public static void setUseNewDocument(boolean use) {
13 | SP.edit().putBoolean(KEY_USE_NEW_DOCUMENT, use).apply();
14 | }
15 |
16 | public static boolean getUseNewDocument() {
17 | return SP.getBoolean(KEY_USE_NEW_DOCUMENT, true);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/java/com/magicianguo/fileexplorer/util/ToastUtils.java:
--------------------------------------------------------------------------------
1 | package com.magicianguo.fileexplorer.util;
2 |
3 | import android.widget.Toast;
4 |
5 | import androidx.annotation.StringRes;
6 |
7 | import com.magicianguo.fileexplorer.App;
8 |
9 | public class ToastUtils {
10 | private static Toast sToast;
11 |
12 | public static void shortCall(@StringRes int resId) {
13 | shortCall(App.get().getString(resId));
14 | }
15 |
16 | public static void shortCall(String text) {
17 | cancelToast();
18 | sToast = Toast.makeText(App.get(), text, Toast.LENGTH_SHORT);
19 | sToast.show();
20 | }
21 |
22 | public static void longCall(@StringRes int resId) {
23 | longCall(App.get().getString(resId));
24 | }
25 |
26 | public static void longCall(String text) {
27 | cancelToast();
28 | sToast = Toast.makeText(App.get(), text, Toast.LENGTH_LONG);
29 | sToast.show();
30 | }
31 |
32 | private static void cancelToast() {
33 | if (sToast != null) {
34 | sToast.cancel();
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/res/drawable/bg_file_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
5 |
6 |
7 |
8 | -
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/res/drawable/bg_icon_dir.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/res/drawable/bg_icon_file.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/res/drawable/bg_tv_path.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
28 |
29 |
38 |
39 |
49 |
50 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/res/layout/activity_setting.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
17 |
18 |
26 |
27 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/res/layout/fragment_file_list.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/res/layout/layout_file_list_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
18 |
19 |
28 |
29 |
44 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/res/mipmap/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MagicianGuo/Android-FileExplorerDemo/a9b0d1771f9e896c8cabe95819c78a577cd6e662/FileExplorer/src/main/res/mipmap/ic_launcher.png
--------------------------------------------------------------------------------
/FileExplorer/src/main/res/mipmap/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MagicianGuo/Android-FileExplorerDemo/a9b0d1771f9e896c8cabe95819c78a577cd6e662/FileExplorer/src/main/res/mipmap/ic_launcher_round.png
--------------------------------------------------------------------------------
/FileExplorer/src/main/res/navigation/nav_main_file_list.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
15 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #000000
4 | #FF000000
5 | #FFFFFFFF
6 | #FFBB00
7 | #999999
8 | @color/white
9 | #EEEEEE
10 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | 文件管理
3 | 文件管理需要存储权限。
4 | 浏览Android/data、Android/obb目录需要授权。
5 | 去授权
6 | 取消
7 | 文件夹
8 | 文件
9 | 授权成功
10 | 授权失败
11 | 再次点击退出应用
12 | Shizuku服务已连接
13 | Shizuku服务已断开
14 | 获取失败,请重新授权Shizuku!
15 | 路径已复制到剪贴板
16 | 设置
17 | 使用新的Document方案\n访问data、obb目录
18 |
--------------------------------------------------------------------------------
/FileExplorer/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
--------------------------------------------------------------------------------
/FileExplorer/src/test/java/com/magicianguo/fileexplorer/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | //package com.magicianguo.fileexplorer
2 | //
3 | //import org.junit.Test
4 | //
5 | //import org.junit.Assert.*
6 | //
7 | ///**
8 | // * Example local unit test, which will execute on the development machine (host).
9 | // *
10 | // * See [testing documentation](http://d.android.com/tools/testing).
11 | // */
12 | //class ExampleUnitTest {
13 | // @Test
14 | // fun addition_isCorrect() {
15 | // assertEquals(4, 2 + 2)
16 | // }
17 | //}
--------------------------------------------------------------------------------
/FileExplorer/testks-sign.gradle:
--------------------------------------------------------------------------------
1 | // 1、在壳工程build.gradle 里加上 apply from: "testks-sign.gradle"
2 | // 2、删除壳工程build.gradle 的 buildTypes 配置,由这里统一配置
3 | android {
4 | signingConfigs {
5 | sign {
6 | storeFile file("testks.jks")
7 | storePassword "testks"
8 | keyAlias "testks"
9 | keyPassword "testks"
10 | }
11 | }
12 | buildTypes {
13 | debug {
14 | signingConfig signingConfigs.sign
15 | }
16 | release {
17 | minifyEnabled true
18 | shrinkResources true
19 | signingConfig signingConfigs.sign
20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | }
--------------------------------------------------------------------------------
/FileExplorer/testks.jks:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MagicianGuo/Android-FileExplorerDemo/a9b0d1771f9e896c8cabe95819c78a577cd6e662/FileExplorer/testks.jks
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 文件管理
2 |
3 | 这是一个简陋的,用于浏览安卓设备/sdcard下文件的应用程序。支持
4 | Android 5.0(API 21)~ Android 14(API 34)。
5 |
6 | ## 不同安卓版本存储权限差异
7 |
8 | ### 1、Android 6.0 之前
9 |
10 | 应用只需要在 AndroidManifest.xml 下声明以下权限即可自动获取存储权限:
11 |
12 | ```xml
13 |
14 |
15 | ```
16 |
17 | ### 2、Android 6.0 起
18 |
19 | 从Android 6.0开始,除了以上操作以外,还需要在代码中动态申请权限。
20 |
21 | ```java
22 | // 检查是否有存储权限
23 | boolean granted = context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
24 | PackageManager.PERMISSION_GRANTED;
25 | ```
26 |
27 | ```java
28 | // 在activity中请求存储权限
29 | requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
30 | ```
31 |
32 | ### 3、Android 10
33 |
34 | Android 10 开始引入了沙盒机制,应用在 sdcard 中默认只能读写私有目录(即/sdcard/Android/data/[应用包名]/),其他目录即便执行前面的操作也无法读写。除非在 AndroidManifest.xml 下声明以下属性:
35 |
36 | ```xml
37 |
40 | ```
41 |
42 | 这样的话就会暂时停用沙盒机制,正常读写/sdcard下文件。
43 |
44 | ### 4、Android 11
45 |
46 | Android 11开始,且应用的目标版本在30及以上,以上的操作也无法再读写sdcard目录。需要声明以下权限:
47 |
48 | ```xml
49 |
50 | ```
51 |
52 | 再动态申请权限:
53 |
54 | ```java
55 | // 检查是否有存储权限
56 | boolean granted = Environment.isExternalStorageManager();
57 | ```
58 |
59 | ```java
60 | // 在activity中请求存储权限
61 | Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
62 | .setData(Uri.parse("package:"+getPackageName()));
63 | startActivityForResult(intent, 0);
64 | ```
65 |
66 | 执行以上操作后,sdcard已能够正常读写。
67 |
68 | 但是,有2个特殊的目录仍然无法读写:
69 |
70 | /sdcard/Android/data 和 /sdcard/Android/obb 。
71 |
72 | 这两个路径需要安卓自带的 DocumentsUI 授权才能访问。
73 |
74 | 首先,最重要的一点,添加 documentfile 依赖(SDK自带的那个版本有问题):
75 |
76 | ```groovy
77 | implementation "androidx.documentfile:documentfile:1.0.1"
78 | ```
79 |
80 | Activity请求授权:
81 |
82 | ```java
83 | Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
84 | intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
85 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
86 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
87 | // 请求Android/data目录的权限,Android/obb目录则把data替换成obb即可。
88 | Uri treeUri = Uri.parse("content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fdata/document/primary%3AAndroid%2Fdata");
89 | DocumentFile df = DocumentFile.fromTreeUri(this, treeUri);
90 | if (df != null) {
91 | intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, df.getUri());
92 | }
93 | }
94 | startActivityForResult(intent, 1);
95 | ```
96 |
97 | 还需要在回调中保存权限:
98 |
99 | ```java
100 | @Override
101 | protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
102 | super.onActivityResult(requestCode, resultCode, data);
103 | Uri uri;
104 | if (data != null && (uri = data.getData()) != null) {
105 | // 授权成功
106 | getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
107 | } else {
108 | // 授权失败
109 | }
110 | }
111 | ```
112 |
113 | 在请求授权时,会跳转到以下界面。点击下方按钮授权即可。
114 |
115 |
116 |
117 |
118 |
119 | 然后,使用接口获取文件列表:
120 |
121 | ```java
122 | // Android 11~12转换路径。例如:/sdcard/Android/data,转换成:
123 | // Uri.parse("content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fdata/document/primary%3AAndroid%2Fdata")
124 |
125 | // 路径 /sdcard/Android/data/com.xxx.yyy,转换成:
126 | // Uri.parse("content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fdata/document/primary%3AAndroid%2Fdata%2Fcom.xxx.yyy")
127 | // 以此类推。
128 | Uri pathUri = pathToUri(path);
129 | DocumentFile documentFile = DocumentFile.fromTreeUri(context, pathUri);
130 | if (documentFile != null) {
131 | DocumentFile[] documentFiles = documentFile.listFiles();
132 | for (DocumentFile df : documentFiles) {
133 | // 文件名
134 | String fName = df.getName();
135 | // 路径
136 | String fPath = path + "/" + fName;
137 | }
138 | }
139 | ```
140 |
141 | ### 5、Android 13
142 |
143 | Android 13 开始,上面提到的授权 Android/data、Android/obb目录的方法失效了。请求授权会出现如下界面:
144 |
145 |
146 |
147 | 点击下方按钮会提示:
148 |
149 |
150 |
151 | 说明安卓13无法再直接授权 Android/data 和 Android/obb 这两个目录了。但也并非无计可施。我们可以授权他们的子目录。
152 |
153 | 我们知道,这个目录下的文件夹名称一般都是应用的包名。我们可以直接用应用包名的路径来请求授权。
154 |
155 | **这里要注意,Android 13和Android 11~12的路径转换规则不一样。**
156 |
157 | ```java
158 | // Android 13转换路径。例如:/sdcard/Android/data/com.xxx.yyy,转换成:
159 | // Uri.parse("content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fdata%2Fcom.xxx.yyy/document/primary%3AAndroid%2Fdata%2Fcom.xxx.yyy")
160 |
161 | // 路径 /sdcard/Android/data/com.xxx.yyy/files,转换成:
162 | // Uri.parse("content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fdata%2Fcom.xxx.yyy/document/primary%3AAndroid%2Fdata%2Fcom.xxx.yyy%2Ffiles")
163 | // 以此类推。
164 | ```
165 |
166 | 请求授权时,出现如下界面。照常授权即可。
167 |
168 |
169 |
170 | ### 6、Android 14
171 |
172 | Android 14对于data、obb目录的授权进一步收紧。在Android 14的后期版本和Android 15预览版中,以上方法已失效(传入uri请求授权只会跳转到sdcard根目录)。这种情况下,想要访问data和obb目录就需要使用Shizuku了。(目前MT、FV就是用这种方法访问的)
173 |
174 | Shizuku的用法可以查阅相关教程,这里不多赘述。请先将Shizuku启动,便于后续使用。
175 |
176 | 首先,添加Shizuku的依赖:
177 |
178 | ```groovy
179 | def shizuku_version = "13.1.5"
180 | implementation "dev.rikka.shizuku:api:$shizuku_version"
181 | // Add this line if you want to support Shizuku
182 | implementation "dev.rikka.shizuku:provider:$shizuku_version"
183 | ```
184 |
185 | AndroidManifest.xml添加以下内容:
186 |
187 | ```xml
188 |
189 |
191 |
192 |
193 |
194 |
195 |
196 |
203 |
206 |
207 |
208 | ```
209 |
210 | 判断Shizuku是否安装:
211 |
212 | ```java
213 | private static boolean isShizukuInstalled() {
214 | try {
215 | context.getPackageManager().getPackageInfo("moe.shizuku.privileged.api", 0);
216 | return true;
217 | } catch (PackageManager.NameNotFoundException e) {
218 | e.printStackTrace();
219 | }
220 | return false;
221 | }
222 | ```
223 |
224 | 判断Shizuku是否可用:
225 |
226 | ```java
227 | boolean available = Shizuku.pingBinder();
228 | ```
229 |
230 | 检查Shizuku是否授权:
231 |
232 | ```java
233 | boolean granted = Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED;
234 | ```
235 |
236 | 请求Shizuku权限:
237 |
238 | ```java
239 | Shizuku.requestPermission(0);
240 | ```
241 |
242 | 监听授权结果:
243 |
244 | ```java
245 | Shizuku.addRequestPermissionResultListener(listener);
246 | Shizuku.removeRequestPermissionResultListener(listener);
247 | ```
248 |
249 | 授权后,自己定义一个aidl文件:
250 |
251 | **IFileExplorerService.aidl**
252 |
253 | ```java
254 | package com.magicianguo.fileexplorer.userservice;
255 |
256 | import com.magicianguo.fileexplorer.bean.BeanFile;
257 |
258 | interface IFileExplorerService {
259 | List listFiles(String path);
260 | }
261 | ```
262 |
263 | **BeanFile.java**
264 |
265 | ```java
266 | public class BeanFile implements Parcelable {
267 | public BeanFile(String name, String path, boolean isDir, boolean isGrantedPath, String pathPackageName) {
268 | this.name = name;
269 | this.path = path;
270 | this.isDir = isDir;
271 | this.isGrantedPath = isGrantedPath;
272 | this.pathPackageName = pathPackageName;
273 | }
274 |
275 | /**
276 | * 文件名
277 | */
278 | public String name;
279 | /**
280 | * 文件路径
281 | */
282 | public String path;
283 | /**
284 | * 是否文件夹
285 | */
286 | public boolean isDir;
287 | /**
288 | * 是否被Document授权的路径
289 | */
290 | public boolean isGrantedPath;
291 | /**
292 | * 如果文件夹名称是应用包名,则将包名保存到该字段
293 | */
294 | public String pathPackageName;
295 |
296 | protected BeanFile(Parcel in) {
297 | name = in.readString();
298 | path = in.readString();
299 | isDir = in.readByte() != 0;
300 | isGrantedPath = in.readByte() != 0;
301 | pathPackageName = in.readString();
302 | }
303 |
304 | public static final Creator CREATOR = new Creator() {
305 | @Override
306 | public BeanFile createFromParcel(Parcel in) {
307 | return new BeanFile(in);
308 | }
309 |
310 | @Override
311 | public BeanFile[] newArray(int size) {
312 | return new BeanFile[size];
313 | }
314 | };
315 |
316 | @Override
317 | public int describeContents() {
318 | return 0;
319 | }
320 |
321 | @Override
322 | public void writeToParcel(@NonNull Parcel dest, int flags) {
323 | dest.writeString(name);
324 | dest.writeString(path);
325 | dest.writeByte((byte) (isDir ? 1 : 0));
326 | dest.writeByte((byte) (isGrantedPath ? 1 : 0));
327 | dest.writeString(pathPackageName);
328 | }
329 | }
330 | ```
331 |
332 | **IFileExplorerService实现类:**
333 |
334 | ```java
335 | public class FileExplorerService extends IFileExplorerService.Stub {
336 |
337 | @Override
338 | public List listFiles(String path) throws RemoteException {
339 | List list = new ArrayList<>();
340 | File[] files = new File(path).listFiles();
341 | if (files != null) {
342 | for (File f : files) {
343 | list.add(new BeanFile(f.getName(), f.getPath(), f.isDirectory(), false, f.getName()));
344 | }
345 | }
346 | return list;
347 | }
348 | }
349 | ```
350 |
351 | 然后使用Shizuku绑定UserService:
352 |
353 | ```java
354 | private static final Shizuku.UserServiceArgs USER_SERVICE_ARGS = new Shizuku.UserServiceArgs(
355 | new ComponentName(packageName, FileExplorerService.class.getName())
356 | ).daemon(false).debuggable(BuildConfig.DEBUG).processNameSuffix("file_explorer_service").version(1);
357 |
358 | public static IFileExplorerService iFileExplorerService;
359 |
360 | private static final ServiceConnection SERVICE_CONNECTION = new ServiceConnection() {
361 | @Override
362 | public void onServiceConnected(ComponentName name, IBinder service) {
363 | Log.d(TAG, "onServiceConnected: ");
364 | iFileExplorerService = IFileExplorerService.Stub.asInterface(service);
365 | }
366 |
367 | @Override
368 | public void onServiceDisconnected(ComponentName name) {
369 | Log.d(TAG, "onServiceDisconnected: ");
370 | iFileExplorerService = null;
371 | }
372 | };
373 |
374 | // 绑定服务
375 | public static void bindService() {
376 | Shizuku.bindUserService(USER_SERVICE_ARGS, SERVICE_CONNECTION);
377 | }
378 | ```
379 |
380 | 绑定之后,调用 aidl 里面的方法来管理文件即可。
381 |
382 | **2024/05/18 更新**
383 |
384 | 目前发现了新的Document授权方式,能够在Android 13、14上直接授权Android/data(obb)目录。
385 |
386 | 例1:Android/data目录请求授权,可使用如下Uri:
387 |
388 | ```java
389 | Uri.Builder uriBuilder = new Uri.Builder()
390 | .scheme("content")
391 | .authority("com.android.externalstorage.documents")
392 | .appendPath("tree")
393 | .appendPath("primary:A\u200Bndroid/data")
394 | .appendPath("document")
395 | .appendPath("primary:A\u200Bndroid/data");
396 | // 相当于 content://com.android.externalstorage.documents/tree/primary%3AA%E2%80%8Bndroid%2Fdata/document/primary%3AA%E2%80%8Bndroid%2Fdata
397 | ```
398 |
399 | 例2:Android/data/com.xxx.yyy目录请求授权,可使用如下Uri:
400 |
401 | ```java
402 | Uri.Builder uriBuilder = new Uri.Builder()
403 | .scheme("content")
404 | .authority("com.android.externalstorage.documents")
405 | .appendPath("tree")
406 | .appendPath("primary:A\u200Bndroid/data")
407 | .appendPath("document")
408 | .appendPath("primary:A\u200Bndroid/data/com.xxx.yyy");
409 | // 相当于 content://com.android.externalstorage.documents/tree/primary%3AA%E2%80%8Bndroid%2Fdata/document/primary%3AA%E2%80%8Bndroid%2Fdata%2Fcom.xxx.yyy
410 | ```
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | plugins {
3 | id 'com.android.application' version '8.2.2' apply false
4 | id 'org.jetbrains.kotlin.android' version '1.9.22' apply false
5 | id 'com.android.library' version '8.2.2' apply false
6 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Kotlin code style for this project: "official" or "obsolete":
19 | kotlin.code.style=official
20 | # Enables namespacing of each library's R class so that its R class includes only the
21 | # resources declared in the library itself and none from the library's dependencies,
22 | # thereby reducing the size of the R class for that library
23 | android.nonTransitiveRClass=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MagicianGuo/Android-FileExplorerDemo/a9b0d1771f9e896c8cabe95819c78a577cd6e662/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Feb 20 16:29:08 CST 2024
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
5 | zipStoreBase=GRADLE_USER_HOME
6 | zipStorePath=wrapper/dists
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/readme_pic/android11_data.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MagicianGuo/Android-FileExplorerDemo/a9b0d1771f9e896c8cabe95819c78a577cd6e662/readme_pic/android11_data.png
--------------------------------------------------------------------------------
/readme_pic/android11_data_confirm.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MagicianGuo/Android-FileExplorerDemo/a9b0d1771f9e896c8cabe95819c78a577cd6e662/readme_pic/android11_data_confirm.png
--------------------------------------------------------------------------------
/readme_pic/android13_data.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MagicianGuo/Android-FileExplorerDemo/a9b0d1771f9e896c8cabe95819c78a577cd6e662/readme_pic/android13_data.png
--------------------------------------------------------------------------------
/readme_pic/android13_data_request.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MagicianGuo/Android-FileExplorerDemo/a9b0d1771f9e896c8cabe95819c78a577cd6e662/readme_pic/android13_data_request.png
--------------------------------------------------------------------------------
/readme_pic/android13_data_tips.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MagicianGuo/Android-FileExplorerDemo/a9b0d1771f9e896c8cabe95819c78a577cd6e662/readme_pic/android13_data_tips.png
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | repositories {
3 | maven {
4 | url "https://maven.aliyun.com/repository/public"
5 | }
6 | google()
7 | mavenCentral()
8 | gradlePluginPortal()
9 | }
10 | }
11 | dependencyResolutionManagement {
12 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
13 | repositories {
14 | maven {
15 | url "https://maven.aliyun.com/repository/public"
16 | }
17 | google()
18 | mavenCentral()
19 | }
20 | }
21 |
22 | rootProject.name = "FileExplorer"
23 | include ':FileExplorer'
24 |
--------------------------------------------------------------------------------