version:2.3
fix:增加上传日志功能,优化一键加速功能 update:
This commit is contained in:
211
app/src/main/java/com/aoleyun/sn/rlog/LogDBManager.java
Normal file
211
app/src/main/java/com/aoleyun/sn/rlog/LogDBManager.java
Normal file
@@ -0,0 +1,211 @@
|
||||
package com.aoleyun.sn.rlog;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import com.aoleyun.sn.database.DBSqlBuilder;
|
||||
import com.aoleyun.sn.utils.Utils;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class LogDBManager {
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private static LogDBManager sInstance;
|
||||
private Context mContext;
|
||||
private LogDBHelper mDBHelper;
|
||||
|
||||
private static final String TABLE_NAME = "logtable";
|
||||
private static String TAG = LogDBManager.class.getSimpleName();
|
||||
|
||||
private LogDBManager(Context context) {
|
||||
if (context == null) {
|
||||
throw new RuntimeException("Context is NULL");
|
||||
}
|
||||
mContext = context;
|
||||
mDBHelper = new LogDBHelper(context);
|
||||
}
|
||||
|
||||
public static void init(Context context) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new LogDBManager(context);
|
||||
}
|
||||
}
|
||||
|
||||
public static LogDBManager getInstance() {
|
||||
if (sInstance == null) {
|
||||
throw new IllegalStateException("You must be init LogDBManager first");
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
public LogDBHelper getDBHelper() {
|
||||
if (mDBHelper == null) {
|
||||
mDBHelper = new LogDBHelper(mContext);
|
||||
}
|
||||
return mDBHelper;
|
||||
}
|
||||
|
||||
private static final String LOG_CODE_REBOOT = "1001";
|
||||
private static final String LOG_CODE_NETWORK = "1002";
|
||||
private static final String LOG_CODE_NETCHANGE = "1003";
|
||||
|
||||
private static final String LOG_EVENT_REBOOT = "设备重启";
|
||||
private static final String LOG_EVENT_NETWORK = "首次联网";
|
||||
private static final String LOG_EVENT_NETCHANGE = "网络改变";
|
||||
public static final String LOG_EVENT_BATTERY = "充电改变";
|
||||
|
||||
public void creatRebootLog(String operate, String content) {
|
||||
LogBean logBean = new LogBean();
|
||||
logBean.setCode(LOG_CODE_REBOOT);
|
||||
logBean.setTimestamp(System.currentTimeMillis());
|
||||
logBean.setEvent(LOG_EVENT_REBOOT);
|
||||
logBean.setOperate(operate);
|
||||
logBean.setContent(content);
|
||||
saveLog(logBean);
|
||||
}
|
||||
|
||||
public void creatNetworktLog(String operate, String content) {
|
||||
LogBean logBean = new LogBean();
|
||||
logBean.setCode(LOG_CODE_NETWORK);
|
||||
logBean.setTimestamp(System.currentTimeMillis());
|
||||
logBean.setEvent(LOG_EVENT_NETWORK);
|
||||
logBean.setOperate(operate);
|
||||
logBean.setContent(content);
|
||||
saveLog(logBean);
|
||||
}
|
||||
|
||||
public void creatNetChangeLog(String operate, String content) {
|
||||
LogBean logBean = new LogBean();
|
||||
logBean.setCode(LOG_CODE_NETCHANGE);
|
||||
logBean.setTimestamp(System.currentTimeMillis());
|
||||
logBean.setEvent(LOG_EVENT_NETCHANGE);
|
||||
logBean.setOperate(operate);
|
||||
logBean.setContent(content);
|
||||
saveLog(logBean);
|
||||
}
|
||||
|
||||
public void creatOtherLog(String event, String operate, String content) {
|
||||
LogBean logBean = new LogBean();
|
||||
logBean.setCode("0000");
|
||||
logBean.setTimestamp(System.currentTimeMillis());
|
||||
logBean.setEvent(event);
|
||||
logBean.setOperate(operate);
|
||||
logBean.setContent(content);
|
||||
saveLog(logBean);
|
||||
}
|
||||
|
||||
|
||||
public void saveLog(LogBean logBean) {
|
||||
SQLiteDatabase db = mDBHelper.getWritableDatabase();
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("code", logBean.getCode());
|
||||
values.put("timestamp", logBean.getTimestamp());
|
||||
values.put("event", logBean.getEvent());
|
||||
values.put("operate", logBean.getOperate());
|
||||
values.put("content", logBean.getContent());
|
||||
db.insert(TABLE_NAME, null, values);
|
||||
}
|
||||
|
||||
public void delete(Integer id) {
|
||||
SQLiteDatabase db = mDBHelper.getWritableDatabase();
|
||||
db.delete(TABLE_NAME, "id=?", new String[]{id.toString()});
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
SQLiteDatabase db = mDBHelper.getWritableDatabase();
|
||||
db.delete(TABLE_NAME, "", null);
|
||||
}
|
||||
|
||||
public void update(LogBean logBean) {
|
||||
SQLiteDatabase db = mDBHelper.getWritableDatabase();
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("code", logBean.getCode());
|
||||
values.put("timestamp", logBean.getTimestamp());
|
||||
values.put("event", logBean.getEvent());
|
||||
values.put("operate", logBean.getOperate());
|
||||
values.put("content", logBean.getContent());
|
||||
db.update(TABLE_NAME, values, "id=?", new String[]{logBean.getId().toString()});
|
||||
}
|
||||
|
||||
private long aWeekDayTime = 1000 * 60 * 60 * 24 * 7;
|
||||
|
||||
public List<LogBean> getLogList() {
|
||||
List<LogBean> logBeanList = new ArrayList<>();
|
||||
SQLiteDatabase db = mDBHelper.getReadableDatabase();
|
||||
long time = System.currentTimeMillis() - aWeekDayTime;
|
||||
String sql = "select * from " + TABLE_NAME + " where" + " timestamp > " + time;
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
cursor = db.rawQuery(sql, null);
|
||||
while (cursor.moveToNext()) {
|
||||
LogBean logBean = new LogBean();
|
||||
logBean.setId(cursor.getInt(cursor.getColumnIndex("id")));
|
||||
logBean.setCode(cursor.getString(cursor.getColumnIndex("code")));
|
||||
logBean.setTimestamp(cursor.getLong(cursor.getColumnIndex("timestamp")));
|
||||
logBean.setEvent(cursor.getString(cursor.getColumnIndex("event")));
|
||||
logBean.setOperate(cursor.getString(cursor.getColumnIndex("operate")));
|
||||
logBean.setContent(cursor.getString(cursor.getColumnIndex("content")));
|
||||
logBeanList.add(logBean);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
db.close();
|
||||
}
|
||||
return logBeanList;
|
||||
}
|
||||
|
||||
public String saveToFile() {
|
||||
List<LogBean> logBeanList = getLogList();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd-HH_mm_ss");
|
||||
Date date = new Date(System.currentTimeMillis());
|
||||
String fileName = sdf.format(date) + " log.txt";
|
||||
String filePath = ContextCompat.getExternalFilesDirs(mContext, Environment.DIRECTORY_DOCUMENTS)[0].getAbsolutePath()
|
||||
+ File.separator + fileName;
|
||||
File logFile = new File(filePath);
|
||||
if (logFile.exists()) {
|
||||
logFile.delete();
|
||||
}
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (LogBean logBean : logBeanList) {
|
||||
stringBuilder.append(logBean.toLogString()).append("\n");
|
||||
}
|
||||
byte[] bs = stringBuilder.toString().getBytes();
|
||||
try {
|
||||
OutputStream out = new FileOutputStream(logFile);
|
||||
InputStream is = new ByteArrayInputStream(bs);
|
||||
byte[] buff = new byte[1024];
|
||||
int len = 0;
|
||||
while ((len = is.read(buff)) != -1) {
|
||||
out.write(buff, 0, len);
|
||||
}
|
||||
is.close();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Log.e(TAG, "saveToFile: " + logFile.getAbsolutePath());
|
||||
return logFile.getAbsolutePath();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user