Skip to content

Commit

Permalink
完善沙盒小工具
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchong211 committed Mar 3, 2022
1 parent 2dd0ad3 commit 47eb0b6
Show file tree
Hide file tree
Showing 48 changed files with 197 additions and 63 deletions.
2 changes: 1 addition & 1 deletion CommonLib/AppStatusLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'com.github.dcendents.android-maven'

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
//buildToolsVersion rootProject.ext.android["buildToolsVersion"]

defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
Expand Down
2 changes: 1 addition & 1 deletion CommonLib/ApplicationLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'com.github.dcendents.android-maven'

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
//buildToolsVersion rootProject.ext.android["buildToolsVersion"]

defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
Expand Down
2 changes: 1 addition & 1 deletion CommonLib/ComponentLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'com.github.dcendents.android-maven'

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
//buildToolsVersion rootProject.ext.android["buildToolsVersion"]

defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
Expand Down
2 changes: 1 addition & 1 deletion CommonLib/ConfigLayer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
//buildToolsVersion rootProject.ext.android["buildToolsVersion"]

defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
Expand Down
3 changes: 1 addition & 2 deletions CommonLib/ToolUtilsLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ apply plugin: 'com.github.dcendents.android-maven'

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
//buildToolsVersion rootProject.ext.android["buildToolsVersion"]
defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
Expand All @@ -32,5 +32,4 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation(rootProject.ext.dependencies["appcompat"])
implementation(rootProject.ext.dependencies["annotation"])
implementation rootProject.ext.dependencies["utilcode"]
}
2 changes: 1 addition & 1 deletion MonitorLib/MonitorAliveLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ apply from: rootProject.projectDir.absolutePath + "/yc.gradle"

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
//buildToolsVersion rootProject.ext.android["buildToolsVersion"]
defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
Expand Down
2 changes: 1 addition & 1 deletion MonitorLib/MonitorAnrLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply from: rootProject.projectDir.absolutePath + "/yc.gradle"

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
//buildToolsVersion rootProject.ext.android["buildToolsVersion"]
defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
Expand Down
2 changes: 1 addition & 1 deletion MonitorLib/MonitorCatonLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ apply from: rootProject.projectDir.absolutePath + "/yc.gradle"

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
//buildToolsVersion rootProject.ext.android["buildToolsVersion"]
defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
Expand Down
2 changes: 1 addition & 1 deletion MonitorLib/MonitorCrashLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply from: rootProject.projectDir.absolutePath + "/yc.gradle"

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
//buildToolsVersion rootProject.ext.android["buildToolsVersion"]
defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
Expand Down
2 changes: 1 addition & 1 deletion MonitorLib/MonitorFileLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply from: rootProject.projectDir.absolutePath + "/yc.gradle"

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
//buildToolsVersion rootProject.ext.android["buildToolsVersion"]
defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void onClick(View v) {
if (mCurDir != null) {
String path = mCurDir.getPath();
boolean copyToClipBoard = AppWindowUtils.copyToClipBoard(getContext(), path);
if (copyToClipBoard){
if (copyToClipBoard) {
Toast.makeText(getContext(), "拷贝成功", Toast.LENGTH_SHORT).show();
}
} else {
Expand All @@ -102,7 +102,7 @@ private void initRecyclerView() {
mFileInfoAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(View view, int i) {
if (mFileList.size()>i && i>=0){
if (mFileList.size() > i && i >= 0) {
File fileInfo = mFileList.get(i);
if (fileInfo.exists() && fileInfo.isFile()) {
//如果是文件,则直接打开文件
Expand Down Expand Up @@ -273,13 +273,13 @@ private List<File> initRootFileInfo(Context context) {
}

/**
* 初始化默认文件。注意:加External和不加的比较
* 初始化默认文件。注意:加External和不加(默认)的比较
* 相同点:1.都可以做app缓存目录。2.app卸载后,两个目录下的数据都会被清空。
* 不同点:1.目录的路径不同。前者的目录存在外部SD卡上的。后者的目录存在app的内部存储上。
* 2.前者的路径在手机里可以直接看到。后者的路径需要root以后,用Root Explorer 文件管理器才能看到。
* 2.前者的路径在手机里可以直接看到。后者的路径需要root以后,用Root Explorer 文件管理器才能看到。
*
* @param context 上下文
* @return
* @return 列表
*/
private List<File> initDefaultRootFileInfos(Context context) {
List<File> fileInfos = new ArrayList<>();
Expand All @@ -289,20 +289,23 @@ private List<File> initDefaultRootFileInfos(Context context) {
fileInfos.add(parentFile);
FileExplorerUtils.logInfo(TAG + parentFile.getPath());
}
//路径:/data/user/0/com.yc.lifehelper

//第二个是缓存文件路径
File externalCacheDir = context.getExternalCacheDir();
if (externalCacheDir != null) {
fileInfos.add(externalCacheDir);
FileExplorerUtils.logInfo(TAG + externalCacheDir.getPath());
}
//路径:/storage/emulated/0/Android/data/com.yc.lifehelper/cache

//第三个是外部file路径
File externalFilesDir = context.getExternalFilesDir((String) null);
if (externalFilesDir != null) {
fileInfos.add(externalFilesDir);
FileExplorerUtils.logInfo(TAG + externalFilesDir.getPath());
}
//路径:/storage/emulated/0/Android/data/com.yc.lifehelper/files
return fileInfos;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public void onClick(View v) {
private void shareFile() {
//分享
if (mFile != null) {
//先把文件转移到外部存储文件
//请求权限
//先把文件转移到外部存储文件
//检查版本是否大于M
if (ContextCompat.checkSelfPermission(mActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
Expand Down
2 changes: 1 addition & 1 deletion MonitorLib/MonitorNetLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ apply from: rootProject.projectDir.absolutePath + "/yc.gradle"

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
//buildToolsVersion rootProject.ext.android["buildToolsVersion"]
defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
Expand Down
2 changes: 1 addition & 1 deletion MonitorLib/MonitorOomLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply from: rootProject.projectDir.absolutePath + "/yc.gradle"

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
//buildToolsVersion rootProject.ext.android["buildToolsVersion"]
defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
Expand Down
153 changes: 143 additions & 10 deletions Read/ReadMeWiki/00.方案实践/07.App磁盘沙盒工具实践.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
- 05.其他设计实践说明
- 5.1 性能设计
- 5.2 稳定性设计
- 5.3 灰度设计
- 5.4 降级设计
- 5.5 异常设计
- 5.3 debug依赖设计


### 01.磁盘沙盒的概述
Expand Down Expand Up @@ -138,11 +136,7 @@


#### 2.4 SD卡外部存储
- 当给设备插入SD卡后,查看其目录:/sdcard/ ---> 依然指向/storage/self/primary,继续来看/storage/:
- 可以看出,多了sdcard1,软链接指向了/storage/77E4-07E7/。
``` java

```
- 当给设备插入SD卡后,查看其目录:/sdcard/ ---> 依然指向/storage/self/primary,继续来看/storage/,可以看出,多了sdcard1,软链接指向了/storage/77E4-07E7/。
- 访问方式,跟获取外部存储-App私有目录方式一样。
``` java
File[] fileList = context.getExternalFilesDirs(null);
Expand Down Expand Up @@ -250,6 +244,42 @@


### 4.2 File文件列表
- 获取文件列表,主要包括,`data/data/包名`目录下的缓存文件。`/sdcard/Android/data/包名`下存储文件。
``` java
/**
* 初始化默认文件。注意:加External和不加(默认)的比较
* 相同点:1.都可以做app缓存目录。2.app卸载后,两个目录下的数据都会被清空。
* 不同点:1.目录的路径不同。前者的目录存在外部SD卡上的。后者的目录存在app的内部存储上。
* 2.前者的路径在手机里可以直接看到。后者的路径需要root以后,用Root Explorer 文件管理器才能看到。
*
* @param context 上下文
* @return 列表
*/
private List<File> initDefaultRootFileInfos(Context context) {
List<File> fileInfos = new ArrayList<>();
//第一个是文件父路径
File parentFile = context.getFilesDir().getParentFile();
if (parentFile != null) {
fileInfos.add(parentFile);
}
//路径:/data/user/0/com.yc.lifehelper

//第二个是缓存文件路径
File externalCacheDir = context.getExternalCacheDir();
if (externalCacheDir != null) {
fileInfos.add(externalCacheDir);
}
//路径:/storage/emulated/0/Android/data/com.yc.lifehelper/cache

//第三个是外部file路径
File externalFilesDir = context.getExternalFilesDir((String) null);
if (externalFilesDir != null) {
fileInfos.add(externalFilesDir);
}
//路径:/storage/emulated/0/Android/data/com.yc.lifehelper/files
return fileInfos;
}
```



Expand Down Expand Up @@ -338,8 +368,36 @@


#### 4.6 分享文件给第三方
- https://www.jianshu.com/p/d71afdf9c90a

- 这里直接说分享内部文件给第三方,大概的思路如下所示:
- 第一步:先判断是否有读取文件的权限,如果没有则申请;如果有则进行第二步;
- 第二步:先把文件转移到外部存储文件,为何要这样操作,主要是解决data/data下目前文件无法直接分享问题,因此需要将目标文件拷贝到外部路径
- 第三步:通过intent发送,FileProvider拿到对应路径的uri,最后调用startActivity进行分享文件。
- 大概的代码如下所示
``` java
if (ContextCompat.checkSelfPermission(mActivity,Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, CODE);
} else {
//先把文件转移到外部存储文件
File srcFile = new File(mFile.getPath());
String newFilePath = AppFileUtils.getFileSharePath() + "/fileShare.txt";
File destFile = new File(newFilePath);
//拷贝文件,将data/data源文件拷贝到新的目标文件路径下
boolean copy = AppFileUtils.copyFile(srcFile, destFile);
if (copy) {
//分享
boolean shareFile = FileShareUtils.shareFile(mActivity, destFile);
if (shareFile) {
Toast.makeText(getContext(), "文件分享成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "文件分享失败", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getContext(), "文件保存失败", Toast.LENGTH_SHORT).show();
}
}
```


#### 4.7 打开图片资源
Expand Down Expand Up @@ -369,6 +427,7 @@
- 这就涉及到了进程间通信。Android进程间通信主要手段是Binder,而四大组件的通信也是依靠Binder,因此我们应用间传递路径可以依靠四大组件。



##### 4.8.2 7.0前后对文件处理方式
- Android 7.0 之前使用,传递路径可以通过Uri
``` java
Expand Down Expand Up @@ -491,6 +550,80 @@
- 2、应用B使用Uri通过IPC获取应用A的FileProvider。
- 3、应用A使用FileProvider通过映射表将Uri转为Path,并构造出文件描述符。
- 4、应用A将文件描述符返回给应用B,应用B就可以读取应用A发送的文件了。
- 整个交互流程图如下
-


### 05.其他设计实践说明
#### 5.1 性能设计
- 这个暂无,因为是小工具,主要是在debug环境下依赖使用。代码逻辑并不复杂,不会影响App的性能。


#### 5.2 稳定性设计
- 修改文件说明
- 目前,针对文本文件,比如缓存的json数据,存储在文本文件中,之前测试说让该工具支持修改属性,考虑到修改json比较复杂,因此这里只是实现可以删除文本文件,或者修改文件名称的功能。
- 针对图片文件,可以打开且进行了图片压缩,仅仅支持删除图片文件操作。
- 针对sp存储的数据,是xml,这里可视化展示sp的数据,目前可以支持修改sp数据,测试童鞋这方便操作简单,提高某些场景的测试效率。
- 为何不支持修改json
- 读取文本文件,是一行行读取,修改数据编辑数据麻烦,而且修改完成后对json数据合法性判断也比较难处理。因此这里暂时不提供修改缓存的json数据,测试如果要看,可以通过分享到外部qq查看文件,或者直接查看,避免脏数据。


#### 5.3 debug依赖设计
- 建议在debug下使用
- 在小工具放到debug包名下,依赖使用。或者在gradle依赖的时候区分也可以。如下所示:
```
//在app包下依赖
apply from: rootProject.file('buildScript/fileExplorer.gradle')
/**
* 沙盒file工具配置脚本
*/
println('gradle file explorer , init start')
if (!isNeedUseExplorer()) {
println('gradle file explorer , not need file explorer')
return
}
println('gradle file isNeedUseExplorer = ture')
dependencies {
// 依赖
implementation('com.github.jacoco:runtime:0.0.23-SNAPSHOT')
}
//过滤,只在debug下使用
def isNeedUseJacoco() {
Map<String, String> map = System.getenv()
if (map == null) {
return false
}
//拿到编译后的 BUILD_TYPE 和 CONFIG。具体看 BuildConfig 生成类的代码
boolean hasBuildType = map.containsKey("BUILD_TYPE")
boolean hasConfig = map.containsKey("CONFIG")
println 'gradle file explorer isNeedUseExplorer hasBuildType =====>' + hasBuildType + ',hasConfig = ' + hasConfig
String buildType = "debug"
String config = "debug"
if (hasBuildType) {
buildType = map.get("BUILD_TYPE")
}
if (hasConfig) {
config = map.get("CONFIG")
}
println 'gradle file explorer isNeedUseExplorer buildType =====>' + buildType + ',config = ' + config
if (buildType.toLowerCase() == "debug" && config.toLowerCase() == "debug" && isNotUserFile()) {
println('gradle file explorer debug used')
return true
}
println('gradle file explorer not use')
//如果是正式包,则不使用沙盒file工具
return false
}
static def isNotUserFile() {
//在debug下默认沙盒file工具,如果你在debug下不想使用沙盒file工具,则设置成false
return true
}
```




Expand Down
2 changes: 1 addition & 1 deletion ServerLib/GsonServer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ apply plugin: 'com.github.dcendents.android-maven'

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
//buildToolsVersion rootProject.ext.android["buildToolsVersion"]
defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
Expand Down
2 changes: 1 addition & 1 deletion ServerLib/HttpServer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ apply plugin: 'com.github.dcendents.android-maven'

android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
//buildToolsVersion rootProject.ext.android["buildToolsVersion"]

defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
Expand Down

0 comments on commit 47eb0b6

Please sign in to comment.