mirror of
https://github.com/niushuai233/DevControl.git
synced 2024-10-27 22:43:20 +08:00
feat: app启动时初始化日志相关内容
This commit is contained in:
parent
ba2a1fc643
commit
c8c7e12c8f
@ -15,6 +15,8 @@ android {
|
|||||||
|
|
||||||
multiDexEnabled true
|
multiDexEnabled true
|
||||||
|
|
||||||
|
buildConfigField("String", "APP_NAME", "\"DevControl\"")
|
||||||
|
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,21 +1,141 @@
|
|||||||
package cc.niushuai.project.devcontrol.base;
|
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.GlobalVariables;
|
||||||
|
import cc.niushuai.project.devcontrol.base.util.Keys;
|
||||||
import cc.niushuai.project.devcontrol.db.DB;
|
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 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();
|
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<String> 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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,10 +4,13 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import cc.niushuai.project.devcontrol.BuildConfig;
|
||||||
import cc.niushuai.project.devcontrol.base.ui.BaseActivity;
|
import cc.niushuai.project.devcontrol.base.ui.BaseActivity;
|
||||||
import cc.niushuai.project.devcontrol.db.DB;
|
import cc.niushuai.project.devcontrol.db.DB;
|
||||||
import cc.niushuai.project.devcontrol.db.entity.Device;
|
import cc.niushuai.project.devcontrol.db.entity.Device;
|
||||||
import cc.niushuai.project.devcontrol.vo.DeviceInfo;
|
import cc.niushuai.project.devcontrol.vo.DeviceInfo;
|
||||||
|
import cn.hutool.core.text.StrPool;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
public class GlobalVariables {
|
public class GlobalVariables {
|
||||||
|
|
||||||
@ -16,6 +19,7 @@ public class GlobalVariables {
|
|||||||
* id为key elem为value
|
* id为key elem为value
|
||||||
*/
|
*/
|
||||||
public static final Map<String, DeviceInfo> DEVICE_INFO_MAP = new HashMap<>(16);
|
public static final Map<String, DeviceInfo> 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);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化设备列表到缓存中
|
* 初始化设备列表到缓存中
|
||||||
|
@ -6,6 +6,12 @@ public interface Keys {
|
|||||||
|
|
||||||
String DB_FILE_NAME = "dev_control.db";
|
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 {
|
interface Tag {
|
||||||
String MY_OPEN_HELPER = "MyOpenHelper";
|
String MY_OPEN_HELPER = "MyOpenHelper";
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user