-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathALogActivity.java
326 lines (305 loc) · 12.8 KB
/
ALogActivity.java
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package com.blankj.alog;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.blankj.ALog;
import java.util.ArrayList;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/04/06
* desc : ALog的Demo
* </pre>
*/
public class ALogActivity extends AppCompatActivity
implements View.OnClickListener {
private static final String TAG = "CMJ";
private static final int UPDATE_LOG = 0x01;
private static final int UPDATE_CONSOLE = 0x01 << 1;
private static final int UPDATE_TAG = 0x01 << 2;
private static final int UPDATE_HEAD = 0x01 << 3;
private static final int UPDATE_FILE = 0x01 << 4;
private static final int UPDATE_DIR = 0x01 << 5;
private static final int UPDATE_BORDER = 0x01 << 6;
private static final int UPDATE_SINGLE = 0x01 << 7;
private static final int UPDATE_CONSOLE_FILTER = 0x01 << 8;
private static final int UPDATE_FILE_FILTER = 0x01 << 9;
private static final String JSON = "{\"tools\": [{ \"name\":\"css format\" , \"site\":\"http://tools.w3cschool.cn/code/css\" },{ \"name\":\"JSON format\" , \"site\":\"http://tools.w3cschool.cn/code/JSON\" },{ \"name\":\"pwd check\" , \"site\":\"http://tools.w3cschool.cn/password/my_password_safe\" }]}";
private static final String XML = "<books><book><author>Jack Herrington</author><title>PHP Hacks</title><publisher>O'Reilly</publisher></book><book><author>Jack Herrington</author><title>Podcasting Hacks</title><publisher>O'Reilly</publisher></book></books>";
private static final int[] ONE_D_ARRAY = new int[]{1, 2, 3};
private static final int[][] TWO_D_ARRAY = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
private static final Throwable THROWABLE = new NullPointerException();
private static final Bundle BUNDLE = new Bundle();
private static final Intent INTENT = new Intent();
private static final ArrayList<String> LIST = new ArrayList<>();
private static final String LONG_STR;
static {
StringBuilder sb = new StringBuilder();
sb.append("len = 10400\ncontent = \"");
for (int i = 0; i < 800; ++i) {
sb.append("Hello world. ");
}
sb.append("\"");
LONG_STR = sb.toString();
BUNDLE.putByte("byte", (byte) -1);
BUNDLE.putChar("char", 'c');
BUNDLE.putCharArray("charArray", new char[]{'c', 'h', 'a', 'r', 'A', 'r', 'r', 'a', 'y'});
BUNDLE.putCharSequence("charSequence", "charSequence");
BUNDLE.putCharSequenceArray("charSequenceArray", new CharSequence[]{"char", "Sequence", "Array"});
BUNDLE.putBundle("bundle", BUNDLE);
BUNDLE.putBoolean("boolean", true);
BUNDLE.putInt("int", 1);
BUNDLE.putFloat("float", 1.f);
BUNDLE.putLong("long", 1L);
BUNDLE.putShort("short", (short) 1);
INTENT.setAction("ALog action");
INTENT.addCategory("ALog category");
INTENT.setData(Uri.parse("https://blankj.com"));
INTENT.setType(Intent.ACTION_DIAL);
INTENT.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
INTENT.setPackage("com.blankj.alog");
INTENT.setComponent(new ComponentName("com.blankj.alog", ALogActivity.class.toString()));
INTENT.putExtra("int", 1);
INTENT.putExtra("float", 1f);
INTENT.putExtra("char", 'c');
INTENT.putExtra("string", "string");
INTENT.putExtra("intArray", ONE_D_ARRAY);
ArrayList<String> list = new ArrayList<>();
list.add("ArrayList");
list.add("is");
list.add("serializable");
INTENT.putExtra("serializable", list);
INTENT.putExtra("bundle", BUNDLE);
LIST.add("Hello");
LIST.add("ALog");
}
private TextView tvAboutLog;
private ALog.Config mConfig = ALog.getConfig();
private String dir = "";
private String globalTag = "";
private boolean log = true;
private boolean console = true;
private boolean head = true;
private boolean file = false;
private boolean border = true;
private boolean single = true;
private int consoleFilter = ALog.V;
private int fileFilter = ALog.V;
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
ALog.v("verbose");
ALog.d("debug");
ALog.i("info");
ALog.w("warn");
ALog.e("error");
ALog.a("assert");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alog);
tvAboutLog = findViewById(R.id.tv_about_log);
findViewById(R.id.btn_toggle_log).setOnClickListener(this);
findViewById(R.id.btn_toggle_console).setOnClickListener(this);
findViewById(R.id.btn_toggle_tag).setOnClickListener(this);
findViewById(R.id.btn_toggle_head).setOnClickListener(this);
findViewById(R.id.btn_toggle_border).setOnClickListener(this);
findViewById(R.id.btn_toggle_single).setOnClickListener(this);
findViewById(R.id.btn_toggle_file).setOnClickListener(this);
findViewById(R.id.btn_toggle_dir).setOnClickListener(this);
findViewById(R.id.btn_toggle_conole_filter).setOnClickListener(this);
findViewById(R.id.btn_toggle_file_filter).setOnClickListener(this);
findViewById(R.id.btn_log_no_tag).setOnClickListener(this);
findViewById(R.id.btn_log_with_tag).setOnClickListener(this);
findViewById(R.id.btn_log_in_new_thread).setOnClickListener(this);
findViewById(R.id.btn_log_null).setOnClickListener(this);
findViewById(R.id.btn_log_many_params).setOnClickListener(this);
findViewById(R.id.btn_log_long).setOnClickListener(this);
findViewById(R.id.btn_log_file).setOnClickListener(this);
findViewById(R.id.btn_log_json).setOnClickListener(this);
findViewById(R.id.btn_log_xml).setOnClickListener(this);
findViewById(R.id.btn_log_array).setOnClickListener(this);
findViewById(R.id.btn_log_throwable).setOnClickListener(this);
findViewById(R.id.btn_log_bundle).setOnClickListener(this);
findViewById(R.id.btn_log_intent).setOnClickListener(this);
findViewById(R.id.btn_log_array_list).setOnClickListener(this);
updateAbout(0);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_toggle_log:
updateAbout(UPDATE_LOG);
break;
case R.id.btn_toggle_console:
updateAbout(UPDATE_CONSOLE);
break;
case R.id.btn_toggle_tag:
updateAbout(UPDATE_TAG);
break;
case R.id.btn_toggle_head:
updateAbout(UPDATE_HEAD);
break;
case R.id.btn_toggle_file:
updateAbout(UPDATE_FILE);
break;
case R.id.btn_toggle_dir:
updateAbout(UPDATE_DIR);
break;
case R.id.btn_toggle_border:
updateAbout(UPDATE_BORDER);
break;
case R.id.btn_toggle_single:
updateAbout(UPDATE_SINGLE);
break;
case R.id.btn_toggle_conole_filter:
updateAbout(UPDATE_CONSOLE_FILTER);
break;
case R.id.btn_toggle_file_filter:
updateAbout(UPDATE_FILE_FILTER);
break;
case R.id.btn_log_no_tag:
ALog.v("verbose");
ALog.d("debug");
ALog.i("info");
ALog.w("warn");
ALog.e("error");
ALog.a("assert");
break;
case R.id.btn_log_with_tag:
ALog.vTag("customTag", "verbose");
ALog.dTag("customTag", "debug");
ALog.iTag("customTag", "info");
ALog.wTag("customTag", "warn");
ALog.eTag("customTag", "error");
ALog.aTag("customTag", "assert");
break;
case R.id.btn_log_in_new_thread:
Thread thread = new Thread(mRunnable);
thread.start();
break;
case R.id.btn_log_null:
ALog.v((Object) null);
ALog.d((Object) null);
ALog.i((Object) null);
ALog.w((Object) null);
ALog.e((Object) null);
ALog.a((Object) null);
break;
case R.id.btn_log_many_params:
ALog.v("verbose0", "verbose1");
ALog.vTag("customTag", "verbose0", "verbose1");
ALog.d("debug0", "debug1");
ALog.dTag("customTag", "debug0", "debug1");
ALog.i("info0", "info1");
ALog.iTag("customTag", "info0", "info1");
ALog.w("warn0", "warn1");
ALog.wTag("customTag", "warn0", "warn1");
ALog.e("error0", "error1");
ALog.eTag("customTag", "error0", "error1");
ALog.a("assert0", "assert1");
ALog.aTag("customTag", "assert0", "assert1");
break;
case R.id.btn_log_long:
ALog.d(LONG_STR);
break;
case R.id.btn_log_file:
for (int i = 0; i < 100; i++) {
ALog.file("test0 log to file");
ALog.file(ALog.I, "test0 log to file");
}
break;
case R.id.btn_log_json:
ALog.json(JSON);
ALog.json(ALog.I, JSON);
break;
case R.id.btn_log_xml:
ALog.xml(XML);
ALog.xml(ALog.I, XML);
break;
case R.id.btn_log_array:
ALog.e((Object) ONE_D_ARRAY);
ALog.e((Object) TWO_D_ARRAY);
break;
case R.id.btn_log_throwable:
ALog.e(THROWABLE);
break;
case R.id.btn_log_bundle:
ALog.e(BUNDLE);
break;
case R.id.btn_log_intent:
ALog.e(INTENT);
break;
case R.id.btn_log_array_list:
ALog.e(LIST);
break;
}
}
private void updateAbout(int args) {
switch (args) {
case UPDATE_LOG:
log = !log;
break;
case UPDATE_CONSOLE:
console = !console;
break;
case UPDATE_TAG:
globalTag = globalTag.equals(TAG) ? "" : TAG;
break;
case UPDATE_HEAD:
head = !head;
break;
case UPDATE_FILE:
file = !file;
break;
case UPDATE_DIR:
if (getDir().contains("test")) {
dir = null;
} else {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
dir = Environment.getExternalStorageDirectory().getPath() + System.getProperty("file.separator") + "test";
}
}
break;
case UPDATE_BORDER:
border = !border;
break;
case UPDATE_SINGLE:
single = !single;
break;
case UPDATE_CONSOLE_FILTER:
consoleFilter = consoleFilter == ALog.V ? ALog.W : ALog.V;
break;
case UPDATE_FILE_FILTER:
fileFilter = fileFilter == ALog.V ? ALog.I : ALog.V;
break;
}
mConfig.setLogSwitch(log)
.setConsoleSwitch(console)
.setGlobalTag(globalTag)
.setLogHeadSwitch(head)
.setLog2FileSwitch(file)
.setDir(dir)
.setBorderSwitch(border)
.setSingleTagSwitch(single)
.setConsoleFilter(consoleFilter)
.setFileFilter(fileFilter);
tvAboutLog.setText(mConfig.toString());
}
private String getDir() {
return mConfig.toString().split(System.getProperty("line.separator"))[5].substring(5);
}
@Override
protected void onDestroy() {
ALogApp.getInstance().initALog();
super.onDestroy();
}
}