From c8c7e12c8ff962fbaed9317bbf61d3063c5ad608 Mon Sep 17 00:00:00 2001 From: niushuai233 Date: Wed, 26 Oct 2022 15:21:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20app=E5=90=AF=E5=8A=A8=E6=97=B6=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E6=97=A5=E5=BF=97=E7=9B=B8=E5=85=B3=E5=86=85?= =?UTF-8?q?=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 2 + .../niushuai/project/devcontrol/base/App.java | 130 +++++++++++++++++- .../devcontrol/base/util/GlobalVariables.java | 4 + .../project/devcontrol/base/util/Keys.java | 6 + 4 files changed, 137 insertions(+), 5 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 2277a92..1246cdc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -15,6 +15,8 @@ android { multiDexEnabled true + buildConfigField("String", "APP_NAME", "\"DevControl\"") + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/App.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/App.java index ac051df..07f53cd 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/App.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/App.java @@ -1,21 +1,141 @@ package cc.niushuai.project.devcontrol.base; -import android.content.Context; +import android.Manifest; +import android.app.Activity; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Build; +import android.os.Environment; +import android.provider.Settings; +import androidx.core.app.ActivityCompat; + +import java.io.File; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import cc.niushuai.project.devcontrol.BuildConfig; import cc.niushuai.project.devcontrol.base.util.GlobalVariables; +import cc.niushuai.project.devcontrol.base.util.Keys; import cc.niushuai.project.devcontrol.db.DB; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.text.StrPool; +import cn.hutool.core.util.StrUtil; public class App { - public static void init(Context context) { + public static void init(Activity activity) { + // 检查root权限 + checkRoot(activity); + // 申请权限 + requestPermissions(activity); // 初始化数据库 - initDb(context); + initDb(activity); // 重建设备信息缓存 GlobalVariables.initDeviceInfoMap(); + // 初始化日志相关内容 + initLog(activity); } - private static void initDb(Context context) { - DB.getInstance().init(context); + /** + * 检查root权限 + * + * @param activity + * @author niushuai + * @date: 2022/10/26 15:02 + */ + private static void checkRoot(Activity activity) { + + } + + /** + * 请求权限 + * + * @param activity + * @author niushuai + * @date: 2022/10/26 14:58 + */ + private static void requestPermissions(Activity activity) { + // 所需权限列表 + String[] permissions = { + Manifest.permission.READ_EXTERNAL_STORAGE, + Manifest.permission.WRITE_EXTERNAL_STORAGE, + Manifest.permission.MANAGE_EXTERNAL_STORAGE, + }; + + // 待重新申请权限列表 + List permissionsList = new ArrayList<>(); + for (String perm : permissions) { + if (PackageManager.PERMISSION_GRANTED != activity.checkSelfPermission(perm)) { + // 进入到这里代表没有权限 + permissionsList.add(perm); + } + } + + // 重新申请权限 + if (!permissionsList.isEmpty()) { + String[] strings = new String[permissionsList.size()]; + activity.requestPermissions(permissionsList.toArray(strings), 0); + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + if (!Environment.isExternalStorageManager()) { + Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION); + activity.startActivity(intent); + return; + } + } + + } + + /** + * 初始化数据库文件 + * + * @param activity + * @author niushuai + * @date: 2022/10/26 14:58 + */ + private static void initDb(Activity activity) { + DB.getInstance().init(activity); + } + + /** + * 初始化日志内容 + * + * @author niushuai + * @date: 2022/10/26 15:00 + */ + private static void initLog(Activity activity) { + + String rootPath = getRootPath(); + + if (!FileUtil.exist(rootPath)) { + // 不存在的时候创建文件 + boolean mkdirs = new File(rootPath).mkdirs(); + System.out.println(mkdirs); + } + + // 内存存储日志根目录 + GlobalVariables.LOG_ROOT_PATH = rootPath; + } + + private static String getRootPath() { + String rootPath = ""; + + String externalStorageState = Environment.getExternalStorageState(); + if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) { + // 如果存在外置存储 + rootPath = StrUtil.join(StrPool.SLASH, Environment.getExternalStorageDirectory().getAbsolutePath(), BuildConfig.APP_NAME); + } else { + // 否则就写到/data/data里 + rootPath = StrUtil.join(StrPool.SLASH, Keys.LOG_ROOT_FILE_PATH, BuildConfig.APPLICATION_ID); + } + + return StrUtil.join(StrPool.SLASH, rootPath, Keys.LOG_LOG_FOLDER); + } + } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/GlobalVariables.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/GlobalVariables.java index 32602e1..a5d85f7 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/GlobalVariables.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/GlobalVariables.java @@ -4,10 +4,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import cc.niushuai.project.devcontrol.BuildConfig; import cc.niushuai.project.devcontrol.base.ui.BaseActivity; import cc.niushuai.project.devcontrol.db.DB; import cc.niushuai.project.devcontrol.db.entity.Device; import cc.niushuai.project.devcontrol.vo.DeviceInfo; +import cn.hutool.core.text.StrPool; +import cn.hutool.core.util.StrUtil; public class GlobalVariables { @@ -16,6 +19,7 @@ public class GlobalVariables { * id为key elem为value */ public static final Map DEVICE_INFO_MAP = new HashMap<>(16); + public static String LOG_ROOT_PATH = StrUtil.join(StrPool.SLASH, Keys.LOG_ROOT_FILE_PATH, BuildConfig.APPLICATION_ID, Keys.LOG_LOG_FOLDER); /** * 初始化设备列表到缓存中 diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Keys.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Keys.java index cea6a35..d7dd4a9 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Keys.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Keys.java @@ -6,6 +6,12 @@ public interface Keys { String DB_FILE_NAME = "dev_control.db"; + String LOG_ROOT_FILE_PATH = "/data/data"; + + String LOG_LOG_FOLDER = "log/"; + + String LOG_SUFFIX = ".log"; + interface Tag { String MY_OPEN_HELPER = "MyOpenHelper"; }