parent
1c0f80809b
commit
aa8e511414
@ -1,8 +1,9 @@ |
||||
package com.novel.read |
||||
|
||||
import android.content.Context |
||||
import android.widget.ImageView |
||||
import android.widget.Toast |
||||
|
||||
fun Context.showToast(msg:String){ |
||||
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show() |
||||
} |
||||
} |
||||
|
@ -1,114 +0,0 @@ |
||||
package com.novel.read.utlis; |
||||
|
||||
import android.annotation.SuppressLint; |
||||
|
||||
|
||||
import com.novel.read.constants.Constant; |
||||
|
||||
import java.text.SimpleDateFormat; |
||||
import java.util.Calendar; |
||||
import java.util.Date; |
||||
|
||||
@SuppressLint("SimpleDateFormat") |
||||
public class DateUtli { |
||||
|
||||
private static final int HOUR_OF_DAY = 24; |
||||
private static final int DAY_OF_YESTERDAY = 2; |
||||
private static final int TIME_UNIT = 60; |
||||
|
||||
//将时间转换成日期
|
||||
public static String dateConvert(long time, String pattern) { |
||||
Date date = new Date(time); |
||||
@SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat(pattern); |
||||
return format.format(date); |
||||
} |
||||
|
||||
|
||||
public static String dateConvert(long timesamp, int flag) { |
||||
timesamp = timesamp * 1000; |
||||
String result; |
||||
Calendar todayCalendar = Calendar.getInstance(); |
||||
Calendar otherCalendar = Calendar.getInstance(); |
||||
otherCalendar.setTimeInMillis(timesamp); |
||||
|
||||
String timeFormat = "M月d日"; |
||||
String yearTimeFormat = "yyyy年M月d日"; |
||||
|
||||
boolean yearTemp = todayCalendar.get(Calendar.YEAR) == otherCalendar.get(Calendar.YEAR); |
||||
if (yearTemp) { |
||||
int todayMonth = todayCalendar.get(Calendar.MONTH); |
||||
int otherMonth = otherCalendar.get(Calendar.MONTH); |
||||
if (todayMonth == otherMonth) {//表示是同一个月
|
||||
int temp = todayCalendar.get(Calendar.DATE) - otherCalendar.get(Calendar.DATE); |
||||
switch (temp) { |
||||
case 0: |
||||
result = getHourAndMin(timesamp); |
||||
break; |
||||
case 1: |
||||
if (flag == 1) { |
||||
result = "昨天 "; |
||||
} else { |
||||
result = "昨天 " + getHourAndMin(timesamp); |
||||
} |
||||
break; |
||||
case 2: |
||||
case 3: |
||||
case 4: |
||||
case 5: |
||||
case 6: |
||||
int dayOfMonth = otherCalendar.get(Calendar.WEEK_OF_MONTH); |
||||
int todayOfMonth = todayCalendar.get(Calendar.WEEK_OF_MONTH); |
||||
if (dayOfMonth == todayOfMonth) {//表示是同一周
|
||||
int dayOfWeek = otherCalendar.get(Calendar.DAY_OF_WEEK); |
||||
if (dayOfWeek != 1) {//判断当前是不是星期日 如想显示为:周日 12:09 可去掉此判断
|
||||
result = dayNames[otherCalendar.get(Calendar.DAY_OF_WEEK) - 1]; |
||||
} else { |
||||
result = getTime(timesamp, timeFormat); |
||||
} |
||||
} else { |
||||
result = getTime(timesamp, timeFormat); |
||||
} |
||||
break; |
||||
default: |
||||
result = getTime(timesamp, timeFormat); |
||||
break; |
||||
} |
||||
} else { |
||||
result = getTime(timesamp, timeFormat); |
||||
} |
||||
} else { |
||||
result = getYearTime(timesamp, yearTimeFormat); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* 当天的显示时间格式 |
||||
*/ |
||||
private static String getHourAndMin(long time) { |
||||
SimpleDateFormat format = new SimpleDateFormat("HH:mm"); |
||||
return format.format(new Date(time)); |
||||
} |
||||
|
||||
/** |
||||
* 不同一周的显示时间格式 |
||||
*/ |
||||
private static String getTime(long time, String timeFormat) { |
||||
SimpleDateFormat format = new SimpleDateFormat(timeFormat); |
||||
return format.format(new Date(time)); |
||||
} |
||||
|
||||
/** |
||||
* 不同年的显示时间格式 |
||||
*/ |
||||
private static String getYearTime(long time, String yearTimeFormat) { |
||||
SimpleDateFormat format = new SimpleDateFormat(yearTimeFormat); |
||||
return format.format(new Date(time)); |
||||
} |
||||
|
||||
/** |
||||
* 时间戳格式转换 |
||||
*/ |
||||
private static String[] dayNames = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"}; |
||||
|
||||
} |
@ -0,0 +1,104 @@ |
||||
package com.novel.read.utlis |
||||
|
||||
import android.annotation.SuppressLint |
||||
|
||||
|
||||
import com.novel.read.constants.Constant |
||||
|
||||
import java.text.SimpleDateFormat |
||||
import java.util.Calendar |
||||
import java.util.Date |
||||
|
||||
@SuppressLint("SimpleDateFormat") |
||||
object DateUtli { |
||||
|
||||
private val HOUR_OF_DAY = 24 |
||||
private val DAY_OF_YESTERDAY = 2 |
||||
private val TIME_UNIT = 60 |
||||
|
||||
/** |
||||
* 时间戳格式转换 |
||||
*/ |
||||
private val dayNames = arrayOf("周日", "周一", "周二", "周三", "周四", "周五", "周六") |
||||
|
||||
//将时间转换成日期 |
||||
fun dateConvert(time: Long, pattern: String): String { |
||||
val date = Date(time) |
||||
@SuppressLint("SimpleDateFormat") val format = SimpleDateFormat(pattern) |
||||
return format.format(date) |
||||
} |
||||
|
||||
|
||||
fun dateConvert(timesamp: Long, flag: Int): String { |
||||
var time = timesamp |
||||
time *= 1000 |
||||
val result: String |
||||
val todayCalendar = Calendar.getInstance() |
||||
val otherCalendar = Calendar.getInstance() |
||||
otherCalendar.timeInMillis = time |
||||
|
||||
val timeFormat = "M月d日" |
||||
val yearTimeFormat = "yyyy年M月d日" |
||||
|
||||
val yearTemp = todayCalendar.get(Calendar.YEAR) == otherCalendar.get(Calendar.YEAR) |
||||
if (yearTemp) { |
||||
val todayMonth = todayCalendar.get(Calendar.MONTH) |
||||
val otherMonth = otherCalendar.get(Calendar.MONTH) |
||||
if (todayMonth == otherMonth) {//表示是同一个月 |
||||
when (todayCalendar.get(Calendar.DATE) - otherCalendar.get(Calendar.DATE)) { |
||||
0 -> result = getHourAndMin(time) |
||||
1 -> if (flag == 1) { |
||||
result = "昨天 " |
||||
} else { |
||||
result = "昨天 " + getHourAndMin(time) |
||||
} |
||||
2, 3, 4, 5, 6 -> { |
||||
val dayOfMonth = otherCalendar.get(Calendar.WEEK_OF_MONTH) |
||||
val todayOfMonth = todayCalendar.get(Calendar.WEEK_OF_MONTH) |
||||
if (dayOfMonth == todayOfMonth) {//表示是同一周 |
||||
val dayOfWeek = otherCalendar.get(Calendar.DAY_OF_WEEK) |
||||
if (dayOfWeek != 1) {//判断当前是不是星期日 如想显示为:周日 12:09 可去掉此判断 |
||||
result = dayNames[otherCalendar.get(Calendar.DAY_OF_WEEK) - 1] |
||||
} else { |
||||
result = getTime(time, timeFormat) |
||||
} |
||||
} else { |
||||
result = getTime(time, timeFormat) |
||||
} |
||||
} |
||||
else -> result = getTime(time, timeFormat) |
||||
} |
||||
} else { |
||||
result = getTime(time, timeFormat) |
||||
} |
||||
} else { |
||||
result = getYearTime(time, yearTimeFormat) |
||||
} |
||||
return result |
||||
} |
||||
|
||||
/** |
||||
* 当天的显示时间格式 |
||||
*/ |
||||
private fun getHourAndMin(time: Long): String { |
||||
val format = SimpleDateFormat("HH:mm") |
||||
return format.format(Date(time)) |
||||
} |
||||
|
||||
/** |
||||
* 不同一周的显示时间格式 |
||||
*/ |
||||
private fun getTime(time: Long, timeFormat: String): String { |
||||
val format = SimpleDateFormat(timeFormat) |
||||
return format.format(Date(time)) |
||||
} |
||||
|
||||
/** |
||||
* 不同年的显示时间格式 |
||||
*/ |
||||
private fun getYearTime(time: Long, yearTimeFormat: String): String { |
||||
val format = SimpleDateFormat(yearTimeFormat) |
||||
return format.format(Date(time)) |
||||
} |
||||
|
||||
} |
@ -1,94 +0,0 @@ |
||||
package com.novel.read.utlis; |
||||
|
||||
import android.annotation.SuppressLint; |
||||
import android.content.Context; |
||||
import android.graphics.drawable.Drawable; |
||||
import android.widget.ImageView; |
||||
|
||||
import androidx.annotation.DrawableRes; |
||||
import androidx.annotation.NonNull; |
||||
|
||||
import com.bumptech.glide.Glide; |
||||
import com.bumptech.glide.RequestBuilder; |
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy; |
||||
import com.bumptech.glide.load.resource.bitmap.RoundedCorners; |
||||
import com.bumptech.glide.request.RequestOptions; |
||||
import com.novel.read.R; |
||||
|
||||
|
||||
/** |
||||
* create by 赵利君 on 2018/11/21 |
||||
* describe: |
||||
*/ |
||||
public class GlideImageLoader { |
||||
|
||||
public static void displayImage(Context context, String path, ImageView imageView) { |
||||
Glide.with(context) |
||||
.load(path) |
||||
.into(imageView); |
||||
} |
||||
|
||||
//加载矩形圆角图片
|
||||
public static void displayCornerImage(Context context, String path, ImageView imageView) { |
||||
//设置图片圆角角度
|
||||
RoundedCorners roundedCorners = new RoundedCorners(10); |
||||
RequestOptions options = RequestOptions.bitmapTransform(roundedCorners) |
||||
.placeholder(R.drawable.cover_default) |
||||
.error(R.drawable.cover_default) |
||||
; |
||||
|
||||
|
||||
Glide.with(context) |
||||
.load(path) |
||||
.apply(options) |
||||
// .thumbnail(loadTransform(context,R.drawable.cover_default,5))
|
||||
// .thumbnail(loadTransform(context,R.drawable.cover_default,5))
|
||||
.into(imageView); |
||||
|
||||
} |
||||
private static RequestBuilder<Drawable> loadTransform(Context context, @DrawableRes int placeholderId, float radius) { |
||||
|
||||
return Glide.with(context) |
||||
.load(placeholderId) |
||||
.apply(new RequestOptions().centerCrop() |
||||
.transform(new GlideRoundTransform(context, (int) radius))); |
||||
|
||||
} |
||||
|
||||
|
||||
|
||||
//加载矩形圆角图片 自动设置占位图
|
||||
public static void displayCornerImage(Context context, String path, ImageView imageView,int defaultId) { |
||||
//设置图片圆角角度
|
||||
RoundedCorners roundedCorners = new RoundedCorners(15); |
||||
RequestOptions options = RequestOptions.bitmapTransform(roundedCorners) |
||||
.placeholder(defaultId) |
||||
.error(defaultId); |
||||
Glide.with(context) |
||||
.load(path) |
||||
.apply(options) |
||||
.into(imageView); |
||||
|
||||
} |
||||
|
||||
//加载圆形图片
|
||||
@SuppressLint("CheckResult") |
||||
public static void loadCirCleImage(Context context, String path, ImageView imageView, int width, int height) { |
||||
RequestOptions myOptions = getRequestHeadOptions().circleCrop(); |
||||
myOptions.error(R.mipmap.ic_launcher); |
||||
myOptions.placeholder(R.mipmap.ic_launcher); |
||||
Glide.with(context.getApplicationContext()) |
||||
.load(path) |
||||
.apply(myOptions) |
||||
.into(imageView); |
||||
} |
||||
|
||||
@NonNull |
||||
private static RequestOptions getRequestHeadOptions() { |
||||
return new RequestOptions() |
||||
.error(R.mipmap.ic_launcher)//设置错误图片
|
||||
.placeholder(R.mipmap.ic_launcher) //设置占位图片
|
||||
.diskCacheStrategy(DiskCacheStrategy.ALL); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,88 @@ |
||||
package com.novel.read.utlis |
||||
|
||||
import android.annotation.SuppressLint |
||||
import android.content.Context |
||||
import android.graphics.drawable.Drawable |
||||
import android.widget.ImageView |
||||
|
||||
import androidx.annotation.DrawableRes |
||||
|
||||
import com.bumptech.glide.Glide |
||||
import com.bumptech.glide.RequestBuilder |
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy |
||||
import com.bumptech.glide.load.resource.bitmap.RoundedCorners |
||||
import com.bumptech.glide.request.RequestOptions |
||||
import com.novel.read.R |
||||
|
||||
|
||||
/** |
||||
* create by 赵利君 on 2018/11/21 |
||||
* describe: |
||||
*/ |
||||
object GlideImageLoader { |
||||
|
||||
private//设置错误图片 |
||||
//设置占位图片 |
||||
val requestHeadOptions: RequestOptions |
||||
get() = RequestOptions() |
||||
.error(R.mipmap.ic_launcher) |
||||
.placeholder(R.mipmap.ic_launcher) |
||||
.diskCacheStrategy(DiskCacheStrategy.ALL) |
||||
|
||||
fun displayImage(context: Context, path: String, imageView: ImageView) { |
||||
Glide.with(context) |
||||
.load(path) |
||||
.into(imageView) |
||||
} |
||||
|
||||
//加载矩形圆角图片 |
||||
fun displayCornerImage(context: Context, path: String, imageView: ImageView) { |
||||
//设置图片圆角角度 |
||||
val roundedCorners = RoundedCorners(10) |
||||
val options = RequestOptions.bitmapTransform(roundedCorners) |
||||
.placeholder(R.drawable.cover_default) |
||||
.error(R.drawable.cover_default) |
||||
|
||||
|
||||
Glide.with(context) |
||||
.load(path) |
||||
.apply(options) |
||||
.into(imageView) |
||||
|
||||
} |
||||
|
||||
|
||||
|
||||
//加载矩形圆角图片 自动设置占位图 |
||||
fun displayCornerImage(context: Context, path: String, imageView: ImageView, defaultId: Int) { |
||||
//设置图片圆角角度 |
||||
val roundedCorners = RoundedCorners(15) |
||||
val options = RequestOptions.bitmapTransform(roundedCorners) |
||||
.placeholder(defaultId) |
||||
.error(defaultId) |
||||
Glide.with(context) |
||||
.load(path) |
||||
.apply(options) |
||||
.into(imageView) |
||||
|
||||
} |
||||
|
||||
//加载圆形图片 |
||||
@SuppressLint("CheckResult") |
||||
fun loadCirCleImage( |
||||
context: Context, |
||||
path: String, |
||||
imageView: ImageView, |
||||
width: Int, |
||||
height: Int |
||||
) { |
||||
val myOptions = requestHeadOptions.circleCrop() |
||||
myOptions.error(R.mipmap.ic_launcher) |
||||
myOptions.placeholder(R.mipmap.ic_launcher) |
||||
Glide.with(context.applicationContext) |
||||
.load(path) |
||||
.apply(myOptions) |
||||
.into(imageView) |
||||
} |
||||
|
||||
} |
@ -1,56 +0,0 @@ |
||||
package com.novel.read.utlis; |
||||
|
||||
import android.content.Context; |
||||
import android.content.res.Resources; |
||||
import android.graphics.Bitmap; |
||||
import android.graphics.BitmapShader; |
||||
import android.graphics.Canvas; |
||||
import android.graphics.Paint; |
||||
import android.graphics.RectF; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
|
||||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; |
||||
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; |
||||
|
||||
import java.security.MessageDigest; |
||||
|
||||
public class GlideRoundTransform extends BitmapTransformation { |
||||
private static float radius = 0f; |
||||
|
||||
public GlideRoundTransform(Context context) { |
||||
this(context, 4); |
||||
} |
||||
|
||||
public GlideRoundTransform(Context context, int dp) { |
||||
this.radius = Resources.getSystem().getDisplayMetrics().density * dp; |
||||
} |
||||
|
||||
|
||||
@Override protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) { |
||||
return roundCrop(pool, toTransform); |
||||
} |
||||
|
||||
private static Bitmap roundCrop(BitmapPool pool, Bitmap source) { |
||||
if (source == null) return null; |
||||
|
||||
Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); |
||||
|
||||
Canvas canvas = new Canvas(result); |
||||
Paint paint = new Paint(); |
||||
paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); |
||||
paint.setAntiAlias(true); |
||||
RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight()); |
||||
canvas.drawRoundRect(rectF, radius, radius, paint); |
||||
return result; |
||||
} |
||||
|
||||
public String getId() { |
||||
return getClass().getName() + Math.round(radius); |
||||
} |
||||
|
||||
@Override |
||||
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { |
||||
|
||||
} |
||||
} |
@ -1,21 +0,0 @@ |
||||
package com.novel.read.utlis; |
||||
|
||||
import java.io.Closeable; |
||||
import java.io.IOException; |
||||
|
||||
/** |
||||
* Created by newbiechen on 17-5-11. |
||||
*/ |
||||
|
||||
public class IOUtils { |
||||
|
||||
public static void close(Closeable closeable){ |
||||
if (closeable == null) return; |
||||
try { |
||||
closeable.close(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
//close error
|
||||
} |
||||
} |
||||
} |
@ -0,0 +1,22 @@ |
||||
package com.novel.read.utlis |
||||
|
||||
import java.io.Closeable |
||||
import java.io.IOException |
||||
|
||||
/** |
||||
* Created by zlj |
||||
*/ |
||||
|
||||
object IOUtils { |
||||
|
||||
fun close(closeable: Closeable?) { |
||||
if (closeable == null) return |
||||
try { |
||||
closeable.close() |
||||
} catch (e: IOException) { |
||||
e.printStackTrace() |
||||
//close error |
||||
} |
||||
|
||||
} |
||||
} |
@ -1,222 +0,0 @@ |
||||
package com.novel.read.utlis; |
||||
|
||||
import android.content.Context; |
||||
import android.os.Environment; |
||||
import android.util.Log; |
||||
|
||||
import com.novel.read.base.MyApp; |
||||
|
||||
import java.io.BufferedWriter; |
||||
import java.io.File; |
||||
import java.io.FileWriter; |
||||
import java.io.IOException; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.Calendar; |
||||
import java.util.Date; |
||||
|
||||
/** |
||||
* Created by newbiechen on 17-4-27. |
||||
*/ |
||||
|
||||
public class LogUtils { |
||||
private static Boolean LOG_SWITCH = true; // 日志文件总开关
|
||||
private static Boolean LOG_TO_FILE = false; // 日志写入文件开关
|
||||
private static String LOG_TAG = "IReader"; // 默认的tag
|
||||
private static char LOG_TYPE = 'v';// 输入日志类型,v代表输出所有信息,w则只输出警告...
|
||||
private static int LOG_SAVE_DAYS = 7;// sd卡中日志文件的最多保存天数
|
||||
|
||||
private final static SimpleDateFormat LOG_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 日志的输出格式
|
||||
private final static SimpleDateFormat FILE_SUFFIX = new SimpleDateFormat("yyyy-MM-dd");// 日志文件格式
|
||||
private static String LOG_FILE_PATH; // 日志文件保存路径
|
||||
private static String LOG_FILE_NAME;// 日志文件保存名称
|
||||
|
||||
public static void init(Context context) { // 在Application中初始化
|
||||
LOG_FILE_PATH = Environment.getExternalStorageDirectory().getPath() + File.separator + MyApp.Companion.getContext().getPackageName(); |
||||
LOG_FILE_NAME = "Log"; |
||||
} |
||||
|
||||
/**************************** |
||||
* Warn |
||||
*********************************/ |
||||
public static void w(Object msg) { |
||||
w(LOG_TAG, msg); |
||||
} |
||||
|
||||
public static void w(String tag, Object msg) { |
||||
w(tag, msg, null); |
||||
} |
||||
|
||||
public static void w(String tag, Object msg, Throwable tr) { |
||||
if (msg == null) return; |
||||
log(tag, msg.toString(), tr, 'w'); |
||||
} |
||||
|
||||
/*************************** |
||||
* Error |
||||
********************************/ |
||||
public static void e(Object msg) { |
||||
e(LOG_TAG, msg); |
||||
} |
||||
|
||||
public static void e(String tag, Object msg) { |
||||
e(tag, msg, null); |
||||
} |
||||
|
||||
public static void e(String tag, Object msg, Throwable tr) { |
||||
if (msg == null) return; |
||||
log(tag, msg.toString(), tr, 'e'); |
||||
} |
||||
|
||||
/*************************** |
||||
* Debug |
||||
********************************/ |
||||
public static void d(Object msg) { |
||||
d(LOG_TAG, msg); |
||||
} |
||||
|
||||
public static void d(String tag, Object msg) {// 调试信息
|
||||
d(tag, msg, null); |
||||
} |
||||
|
||||
public static void d(String tag, Object msg, Throwable tr) { |
||||
if (msg == null) return; |
||||
log(tag, msg.toString(), tr, 'd'); |
||||
} |
||||
|
||||
/**************************** |
||||
* Info |
||||
*********************************/ |
||||
public static void i(Object msg) { |
||||
i(LOG_TAG, msg); |
||||
} |
||||
|
||||
public static void i(String tag, Object msg) { |
||||
i(tag, msg, null); |
||||
} |
||||
|
||||
public static void i(String tag, Object msg, Throwable tr) { |
||||
if (msg == null) return; |
||||
log(tag, msg.toString(), tr, 'i'); |
||||
} |
||||
|
||||
/************************** |
||||
* Verbose |
||||
********************************/ |
||||
public static void v(Object msg) { |
||||
v(LOG_TAG, msg); |
||||
} |
||||
|
||||
public static void v(String tag, Object msg) { |
||||
v(tag, msg, null); |
||||
} |
||||
|
||||
public static void v(String tag, Object msg, Throwable tr) { |
||||
if (msg == null) return; |
||||
log(tag, msg.toString(), tr, 'v'); |
||||
} |
||||
|
||||
/** |
||||
* 根据tag, msg和等级,输出日志 |
||||
* |
||||
* @param tag |
||||
* @param msg |
||||
* @param level |
||||
*/ |
||||
private static void log(String tag, String msg, Throwable tr, char level) { |
||||
if (tag == null || msg == null || tr == null) return; |
||||
if (LOG_SWITCH) { |
||||
if ('e' == level && ('e' == LOG_TYPE || 'v' == LOG_TYPE)) { // 输出错误信息
|
||||
Log.e(tag, createMessage(msg), tr); |
||||
} else if ('w' == level && ('w' == LOG_TYPE || 'v' == LOG_TYPE)) { |
||||
Log.w(tag, createMessage(msg), tr); |
||||
} else if ('d' == level && ('d' == LOG_TYPE || 'v' == LOG_TYPE)) { |
||||
Log.d(tag, createMessage(msg), tr); |
||||
} else if ('i' == level && ('d' == LOG_TYPE || 'v' == LOG_TYPE)) { |
||||
Log.i(tag, createMessage(msg), tr); |
||||
} else { |
||||
Log.v(tag, createMessage(msg), tr); |
||||
} |
||||
if (LOG_TO_FILE) |
||||
log2File(String.valueOf(level), tag, msg + tr == null ? "" : "\n" + Log.getStackTraceString(tr)); |
||||
} |
||||
} |
||||
|
||||
private static String getFunctionName() { |
||||
StackTraceElement[] sts = Thread.currentThread().getStackTrace(); |
||||
if (sts == null) { |
||||
return null; |
||||
} |
||||
for (StackTraceElement st : sts) { |
||||
if (st.isNativeMethod()) { |
||||
continue; |
||||
} |
||||
if (st.getClassName().equals(Thread.class.getName())) { |
||||
continue; |
||||
} |
||||
if (st.getFileName().equals("LogUtils.java")) { |
||||
continue; |
||||
} |
||||
return "[" + Thread.currentThread().getName() + "(" |
||||
+ Thread.currentThread().getId() + "): " + st.getFileName() |
||||
+ ":" + st.getLineNumber() + "]"; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private static String createMessage(String msg) { |
||||
String functionName = getFunctionName(); |
||||
String message = (functionName == null ? msg |
||||
: (functionName + " - " + msg)); |
||||
return message; |
||||
} |
||||
|
||||
/** |
||||
* 打开日志文件并写入日志 |
||||
* |
||||
* @return |
||||
**/ |
||||
private synchronized static void log2File(String mylogtype, String tag, String text) { |
||||
Date nowtime = new Date(); |
||||
String date = FILE_SUFFIX.format(nowtime); |
||||
String dateLogContent = LOG_FORMAT.format(nowtime) + ":" + mylogtype + ":" + tag + ":" + text; // 日志输出格式
|
||||
File destDir = new File(LOG_FILE_PATH); |
||||
if (!destDir.exists()) { |
||||
destDir.mkdirs(); |
||||
} |
||||
File file = new File(LOG_FILE_PATH, LOG_FILE_NAME + date); |
||||
try { |
||||
FileWriter filerWriter = new FileWriter(file, true); |
||||
BufferedWriter bufWriter = new BufferedWriter(filerWriter); |
||||
bufWriter.write(dateLogContent); |
||||
bufWriter.newLine(); |
||||
bufWriter.close(); |
||||
filerWriter.close(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 删除指定的日志文件 |
||||
*/ |
||||
public static void delFile() {// 删除日志文件
|
||||
String needDelFiel = FILE_SUFFIX.format(getDateBefore()); |
||||
File file = new File(LOG_FILE_PATH, needDelFiel + LOG_FILE_NAME); |
||||
if (file.exists()) { |
||||
file.delete(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 得到LOG_SAVE_DAYS天前的日期 |
||||
* |
||||
* @return |
||||
*/ |
||||
private static Date getDateBefore() { |
||||
Date nowtime = new Date(); |
||||
Calendar now = Calendar.getInstance(); |
||||
now.setTime(nowtime); |
||||
now.set(Calendar.DATE, now.get(Calendar.DATE) - LOG_SAVE_DAYS); |
||||
return now.getTime(); |
||||
} |
||||
} |
Loading…
Reference in new issue