From f4ce4742e63daeec4d58349d180f45603550704f Mon Sep 17 00:00:00 2001 From: Quyunshuo Date: Tue, 16 Mar 2021 09:59:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/utils/AndroidBugFixUtils.kt | 47 +++++++++++ .../com/quyunshuo/base/utils/DateUtils.kt | 83 +++++++++++++++++-- 2 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 Lib_Base/src/main/java/com/quyunshuo/base/utils/AndroidBugFixUtils.kt diff --git a/Lib_Base/src/main/java/com/quyunshuo/base/utils/AndroidBugFixUtils.kt b/Lib_Base/src/main/java/com/quyunshuo/base/utils/AndroidBugFixUtils.kt new file mode 100644 index 0000000..cb71c8e --- /dev/null +++ b/Lib_Base/src/main/java/com/quyunshuo/base/utils/AndroidBugFixUtils.kt @@ -0,0 +1,47 @@ +package com.quyunshuo.base.utils + +import android.app.Activity +import android.content.Context +import android.view.View +import android.view.inputmethod.InputMethodManager +import com.quyunshuo.base.BaseApplication +import java.lang.reflect.Field + +/** + * 解决 Android 自身的 Bug + * + * @author Qu Yunshuo + * @since 2020/10/22 + */ +class AndroidBugFixUtils { + + /** + * 解决 InputMethodManager 造成的内存泄露 + * + * 使用方式: + * ``` + * override fun onDestroy() { + * AndroidBugFixUtils().fixSoftInputLeaks(this) + * super.onDestroy() + * } + * ``` + */ + fun fixSoftInputLeaks(activity: Activity) { + val imm = + BaseApplication.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + val leakViews = arrayOf("mLastSrvView", "mCurRootView", "mServedView", "mNextServedView") + for (leakView in leakViews) { + try { + val leakViewField: Field = + InputMethodManager::class.java.getDeclaredField(leakView) ?: continue + if (!leakViewField.isAccessible) leakViewField.isAccessible = true + val view: Any? = leakViewField.get(imm) + if (view !is View) continue + if (view.rootView == activity.window.decorView.rootView) { + leakViewField.set(imm, null) + } + } catch (t: Throwable) { + } + } + } +} \ No newline at end of file diff --git a/Lib_Base/src/main/java/com/quyunshuo/base/utils/DateUtils.kt b/Lib_Base/src/main/java/com/quyunshuo/base/utils/DateUtils.kt index d42557b..50a5528 100644 --- a/Lib_Base/src/main/java/com/quyunshuo/base/utils/DateUtils.kt +++ b/Lib_Base/src/main/java/com/quyunshuo/base/utils/DateUtils.kt @@ -5,16 +5,40 @@ import java.text.SimpleDateFormat import java.util.* /** - * @Author: QuYunShuo - * @Time: 2020/9/8 - * @Class: DateUtils - * @Remark: 时间工具类 + * 时间工具类 + * ________________________________________________________________________________________ + * |字母 |日期或时间元素 | 表示 | 示例 | + * |:--:|:--------------------:|:-----------------:|:------------------------------------:| + * |G |Era 标志符 | Text | AD | + * |y |年 | Year | 1996; 96 | + * |M |年中的月份 | Month | July; Jul; 07 | + * |w |年中的周数 | Number | 27 | + * |W |月份中的周数 | Number | 2 | + * |D |年中的天数 | Number | 189 | + * |d |月份中的天数 | Number | 10 | + * |F |月份中的星期 | Number | 2 | + * |E |星期中的天数 | Text | Tuesday; Tue | + * |a |Am/pm 标记 | Text | PM | + * |H |一天中的小时数(0-23) | Number | 0 | + * |k |一天中的小时数(1-24) | Number | 24 | + * |K |am/pm 中的小时数(0-11) | Number | 0 | + * |h |am/pm 中的小时数(1-12) | Number | 12 | + * |m |小时中的分钟数 | Number | 30 | + * |s |分钟中的秒数 | Number | 55 | + * |S |毫秒数 | Number | 978 | + * |z |时区 | General time zone | Pacific Standard Time; PST; GMT-08:00| + * |Z |时区 | RFC 822 time zone | -0800 | + *  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ + * @author Qu Yunshuo + * @since 2020/9/8 */ object DateUtils { + /** * 获取时间格式化String * @param timestamp 时间戳 * @param dateFormat 日期格式 + * @return 格式化后的字符串 */ fun getDateFormatString(timestamp: Long, dateFormat: String): String = SimpleDateFormat(dateFormat, Locale.CHINESE).format(Date(timestamp)) @@ -22,15 +46,15 @@ object DateUtils { /** * 将固定格式[dateFormat]的时间字符串[dateString]转换为时间值 */ - fun getDateStringToDate(dateString: String, dateFormat: String): Long { + fun getDateStringToDate(dateString: String, dateFormat: String): Long? { val simpleDateFormat = SimpleDateFormat(dateFormat, Locale.CHINESE) - var date = Date() + var date: Date? = null try { date = simpleDateFormat.parse(dateString) } catch (e: ParseException) { e.printStackTrace() } - return date.time + return date?.time } /** @@ -114,4 +138,49 @@ object DateUtils { ) return calendar.time.time } + + /** + * String 转化 Calendar + * @param string String + * @param format String + */ + fun stringToCalendar(string: String, format: String): Calendar? { + val sdf = SimpleDateFormat(format, Locale.CHINESE) + var calendar: Calendar + try { + val date: Date = sdf.parse(string) ?: return null + calendar = Calendar.getInstance() + calendar.time = date + } catch (e: ParseException) { + e.printStackTrace() + calendar = Calendar.getInstance() + } + return calendar + } + + /** + * String 转化Date + * @param str String + * @param format String + * @return Date + */ + fun strToDate(str: String, format: String): Date? { + val sdf = SimpleDateFormat(format, Locale.CHINESE) + return try { + sdf.parse(str) + } catch (e: ParseException) { + e.printStackTrace() + null + } + } + + /** + * 判断两个时间是否是同一天 + * @param cal1 Calendar + * @param cal2 Calendar + * @return Boolean + */ + fun isSameDay(cal1: Calendar, cal2: Calendar): Boolean { + return cal1[0] == cal2[0] && cal1[1] == cal2[1] && cal1[6] == cal2[6] + } } \ No newline at end of file