-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.java
More file actions
133 lines (120 loc) · 4.13 KB
/
Logger.java
File metadata and controls
133 lines (120 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.android.mltcode.watchlib.config;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public final class Logger {
private static final String PATH = "/Android/com.android.mltcode/";
private static final String CONFIG_FILE_NAME = ".cofig.txt";
private static boolean isDebug = false;
private static final String LOG = "log";
public static void init(Context context) {
boolean isWriteLog = false;
Config config = readConfig(context);
if(config != null){
isDebug = config.isDebug;
isWriteLog = config.isWriteLog;
}
ConfigUtil.init(context, isWriteLog);
}
public static void d(String tag, String content) {
if (isDebug) {
Log.d(tag, content);
}
ConfigUtil.LogWirte(LOG, "d +" + tag + " :" + content);
}
public static void i(String tag, String content) {
if (isDebug) {
Log.d(tag, content);
}
ConfigUtil.LogWirte(LOG, "i +" + tag + " :" + content);
}
public static void e(String tag, String content) {
if (isDebug) {
Log.d(tag, content);
}
ConfigUtil.LogWirte(LOG, "e +" + tag + " :" + content);
}
public static void w(String tag, String content) {
if (isDebug) {
Log.d(tag, content);
}
ConfigUtil.LogWirte(LOG, "w +" + tag + " :" + content);
}
public static Config readConfig(Context context) {
PackageManager pm = context.getPackageManager();
if (PackageManager.PERMISSION_GRANTED != pm.checkPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
context.getPackageName())) {
return null;
}
File file = createFile();
String str = null;
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String readline = "";
StringBuffer sb = new StringBuffer();
while ((readline = br.readLine()) != null) {
sb.append(readline);
}
br.close();
str = sb.toString();
} catch (Exception e) {
e.printStackTrace();
str = null;
}
Config config;
if (TextUtils.isEmpty(str)) {
config = getDefaultConfig();
} else {
try {
str = new String(Base64.decode(str, Base64.DEFAULT));
config = new Config();
JSONObject jo = new JSONObject(str);
config.isDebug = jo.optBoolean("isDebug", false);
config.isWriteLog = jo.optBoolean("isWriteLog", false);
config.mode = jo.optInt("mode", BusinessConfig.OFFICIAL_MODE);
} catch (JSONException e) {
e.printStackTrace();
config = getDefaultConfig();
}
}
return config;
}
private static Config getDefaultConfig() {
Config config = new Config();
config.isDebug = false;
config.isWriteLog = false;
config.mode = BusinessConfig.OFFICIAL_MODE;
return config;
}
private static File createFile() {
StringBuffer bf = new StringBuffer();
bf.append(Environment.getExternalStorageDirectory().getPath());
bf.append(PATH);
File fileDir = new File(bf.toString());
if (!fileDir.exists()) {
fileDir.mkdirs();
}
bf.append(CONFIG_FILE_NAME);
File file = new File(bf.toString());
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
return file;
}
}