Browse Source

Merge remote-tracking branch 'origin/master'

# Conflicts:
#	app/src/main/AndroidManifest.xml
#	app/src/main/java/com/convenient/android/lib/MainActivity.kt
#	app/src/main/res/layout/activity_main.xml
liuxiaolong 2 years ago
parent
commit
27429dbd40

+ 3 - 2
app/src/main/AndroidManifest.xml

@@ -21,14 +21,15 @@
             android:name=".MediaSampleActivity"
             android:exported="false" />
         <activity
-            android:name=".MainActivity"
+            android:name="com.convenient.android.lib.MainActivity"
             android:exported="true">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
-
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+
+        <activity android:name=".DateUtilActivity" />
     </application>
 
 </manifest>

+ 38 - 0
app/src/main/java/com/convenient/android/lib/DateUtilActivity.kt

@@ -0,0 +1,38 @@
+package com.convenient.android.lib
+
+import android.os.Bundle
+import androidx.lifecycle.lifecycleScope
+import com.convenient.android.common.base.viewbinding.BaseBindingActivity
+import com.convenient.android.common.utils.date.*
+import com.convenient.android.lib.databinding.ActivityDateBinding
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+
+/**
+ * @author: hubowen
+ * @date: 2022/8/3
+ * @description:
+ */
+class DateUtilActivity : BaseBindingActivity<ActivityDateBinding>(ActivityDateBinding::inflate) {
+
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        binding.apply {
+
+            lifecycleScope.launch(Dispatchers.IO) {
+                while (true) {
+                    delay(1000)
+                    withContext(Dispatchers.Main) {
+                        idDateActivityTvTime.text = System.currentTimeMillis().toDate(DateUtils.DateFormat.DEFAULT_PATTERN.dateFormat)
+                    }
+                }
+            }
+
+            idDateActivityTvContent.text =
+                "${System.currentTimeMillis().toDate(DateUtils.DateFormat.DEFAULT_PATTERN.dateFormat)}\n" +
+                        "${year("年")}${month(isAbbreviation = true)}${day("日")}${hour("时")}${minute("分")}${second("秒")}${week(isAbbreviation = true)}"
+        }
+    }
+}

+ 7 - 48
app/src/main/java/com/convenient/android/lib/MainActivity.kt

@@ -1,59 +1,18 @@
 package com.convenient.android.lib
 
-import android.Manifest
-import android.app.Activity
-import android.content.Intent
-import android.net.Uri
-import android.os.Build
-import androidx.appcompat.app.AppCompatActivity
 import android.os.Bundle
-import android.os.Environment
-import android.provider.Settings
-import androidx.activity.result.ActivityResultLauncher
-import androidx.activity.result.contract.ActivityResultContracts
-import androidx.activity.result.registerForActivityResult
-import com.convenient.android.common.utils.ToastUtil
+import com.convenient.android.common.base.viewbinding.BaseBindingActivity
+import com.convenient.android.common.extension.clickWithTrigger
+import com.convenient.android.common.extension.readyGo
 import com.convenient.android.lib.databinding.ActivityMainBinding
-import java.io.Serializable
-import java.security.Permission
-
-class MainActivity : AppCompatActivity() {
-
-    val permissionLaunch = registerForActivityResult(ActivityResultContracts.RequestPermission()){
-        ToastUtil.showToast(this, "存储权限获取成功")
-    }
-
-    val androidRStoragePermissionLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
-        if (it.resultCode == Activity.RESULT_OK && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
-            if (Environment.isExternalStorageManager()){
-                ToastUtil.showToast(this, "访问所有文件权限获取成功")
-            }
-        }
-    }
 
+class MainActivity : BaseBindingActivity<ActivityMainBinding>(ActivityMainBinding::inflate) {
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
-
-        val binding = ActivityMainBinding.inflate(layoutInflater)
-        setContentView(binding.root)
-
-        binding.btnStorgePermission.setOnClickListener {
-            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
-                if (Environment.isExternalStorageManager().not()){
-                    androidRStoragePermissionLauncher.launch(Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, Uri.parse("package:${packageName}")))
-                }
-            }else{
-                permissionLaunch.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
+        binding.apply {
+            etText.clickWithTrigger {
+                readyGo(DateUtilActivity::class.java)
             }
         }
-
-        binding.btnQueryMediaStore.setOnClickListener {
-
-            startActivity(Intent(this, MediaSampleActivity::class.java))
-        }
-
     }
-
-
-
 }

+ 36 - 0
app/src/main/res/layout/activity_date.xml

@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <androidx.appcompat.widget.AppCompatTextView
+        android:id="@+id/id_date_activity_tv_date"
+        android:layout_width="match_parent"
+        android:layout_height="50dp"
+        android:gravity="center"
+        android:text="DateUtil"
+        android:textSize="14sp"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+
+    <androidx.appcompat.widget.AppCompatTextView
+        android:id="@+id/id_date_activity_tv_time"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:gravity="center"
+        android:textSize="14sp"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintRight_toRightOf="parent"
+        app:layout_constraintTop_toBottomOf="@+id/id_date_activity_tv_date" />
+
+    <androidx.appcompat.widget.AppCompatTextView
+        android:id="@+id/id_date_activity_tv_content"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="10dp"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintRight_toRightOf="parent"
+        app:layout_constraintTop_toBottomOf="@+id/id_date_activity_tv_time" />
+
+</androidx.constraintlayout.widget.ConstraintLayout>

+ 4 - 19
app/src/main/res/layout/activity_main.xml

@@ -6,27 +6,12 @@
     android:layout_height="match_parent"
     tools:context=".MainActivity">
 
-
-    <Button
-        android:id="@+id/btn_storge_permission"
-        android:layout_width="0dp"
+    <androidx.appcompat.widget.AppCompatButton
+        android:id="@+id/et_text"
+        android:text="测试"
+        android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:layout_marginStart="16dp"
-        android:layout_marginTop="16dp"
-        android:layout_marginEnd="16dp"
-        android:text="申请存储权限"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent" />
-
-    <Button
-        android:id="@+id/btn_query_media_store"
-        android:layout_width="0dp"
-        android:layout_height="wrap_content"
-        android:layout_marginTop="4dp"
-        android:text="媒体文件测试"
-        app:layout_constraintEnd_toEndOf="@+id/btn_storge_permission"
-        app:layout_constraintStart_toStartOf="@+id/btn_storge_permission"
-        app:layout_constraintTop_toBottomOf="@+id/btn_storge_permission" />
-
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 116 - 0
lib_common/src/main/java/com/convenient/android/common/utils/date/DateExtension.kt

@@ -0,0 +1,116 @@
+package com.convenient.android.common.utils.date
+
+import java.util.*
+
+/**
+ * @author: hubowen
+ * @date: 2022/8/3
+ * @description:
+ */
+
+/**
+ * @param: format 时间格式
+ * @description: 时间戳转换成默认时间格式
+ */
+fun Long.toDate(format: String): String = DateUtils.getFormatDate(this, format)
+
+/**
+ * @param: singleDateFormat 单独时间格式
+ * @description: 获得当前时间信息
+ */
+fun getSingleDate(singleDateFormat: DateUtils.SingleDateFormat): Int {
+    Calendar.getInstance().apply {
+        return when (singleDateFormat) {
+            DateUtils.SingleDateFormat.YEAR -> get(Calendar.YEAR)
+            DateUtils.SingleDateFormat.MONTH -> get(Calendar.MONTH) + 1
+            DateUtils.SingleDateFormat.DAY -> get(Calendar.DAY_OF_MONTH)
+            DateUtils.SingleDateFormat.HOUR -> get(Calendar.HOUR_OF_DAY)
+            DateUtils.SingleDateFormat.MINUTE -> get(Calendar.MINUTE)
+            DateUtils.SingleDateFormat.SECOND -> get(Calendar.SECOND)
+            DateUtils.SingleDateFormat.WEEK -> get(Calendar.DAY_OF_WEEK) - 1
+        }
+    }
+}
+
+/**
+ * @param: endStr 结尾字符
+ * @description: 获得当前年份
+ */
+fun year(endStr: String? = ""): String = "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.YEAR)}$endStr"
+
+/**
+ * @param: endStr 结尾字符
+ * @param: isAbbreviation 是否简写
+ * @description: 获得当前月份
+ */
+fun month(endStr: String? = "", isAbbreviation: Boolean = false): String {
+    fun getAbbreviation(month: Int): String {
+        return when (month) {
+            1 -> "Jan"
+            2 -> "Feb"
+            3 -> "Mar"
+            4 -> "Apr"
+            5 -> "May"
+            6 -> "Jun"
+            7 -> "Jul"
+            8 -> "Aug"
+            9 -> "Sept"
+            10 -> "Oct"
+            11 -> "Nov"
+            else -> "Dec"
+        }
+    }
+
+    return when (isAbbreviation) {
+        true -> "${getAbbreviation(getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.MONTH))}$endStr"
+        else -> "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.MONTH)}$endStr"
+    }
+}
+
+/**
+ * @param: endStr 结尾字符
+ * @description: 获得当前日期
+ */
+fun day(endStr: String? = ""): String = "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.DAY)}$endStr"
+
+/**
+ * @param: endStr 结尾字符
+ * @description: 获得当前小时
+ */
+fun hour(endStr: String? = ""): String = "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.HOUR)}$endStr"
+
+/**
+ * @param: endStr 结尾字符
+ * @description: 获得当前分钟
+ */
+fun minute(endStr: String? = ""): String = "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.MINUTE)}$endStr"
+
+/**
+ * @param: endStr 结尾字符
+ * @description: 获得当前秒数
+ */
+fun second(endStr: String? = ""): String = "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.SECOND)}$endStr"
+
+/**
+ * @param: endStr 结尾字符
+ * @param: isAbbreviation 是否简写
+ * @description: 获得当前星期
+ */
+fun week(endStr: String? = "", isAbbreviation: Boolean = false): String {
+    fun getAbbreviation(week: Int): String {
+        return when (week) {
+            1 -> "Mon"
+            2 -> "Tues"
+            3 -> "Wed"
+            4 -> "Thur"
+            5 -> "Fri"
+            6 -> "Sat"
+            else -> "Sun"
+        }
+    }
+
+    return when (isAbbreviation) {
+        true -> "${getAbbreviation(getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.WEEK))}$endStr"
+        else -> "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.WEEK)}$endStr"
+    }
+}

+ 18 - 155
lib_common/src/main/java/com/convenient/android/common/utils/date/DateUtils.kt

@@ -10,10 +10,21 @@ import java.util.*
  * description: 时间相关的工具类
  */
 object DateUtils {
+    //默认的时间格式
+    enum class DateFormat(val dateFormat: String) {
+        DEFAULT_PATTERN("yyyy-MM-dd HH:mm:ss"),
+    }
 
-
-    const val DATE_DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss"
-
+    //默认单独时间格式
+    enum class SingleDateFormat {
+        YEAR,
+        MONTH,
+        DAY,
+        HOUR,
+        MINUTE,
+        SECOND,
+        WEEK
+    }
 
     /**
      * 获取格式化的时间
@@ -21,155 +32,11 @@ object DateUtils {
      * @param pattern 格式
      * @param locale 区域
      */
-    fun getFormatDate(time: Long, pattern: String, locale: Locale = Locale.getDefault()): String {
-        val sdf = SimpleDateFormat(pattern, locale)
-        return sdf.format(Date(time))
-    }
-
-    /**
-     * 获取当前格式化后的时间
-     */
-    fun getTime(pattern: String = DATE_DEFAULT_PATTERN): String {
-        return getFormatDate(System.currentTimeMillis(), pattern)
-    }
-
-
-    /**
-     * 返回指定的时间毫秒数
-     * @param date 时间
-     * @param pattern 传入的时间对应格式
-     * @description 时间格式例如:yyyy-MM-dd HH:mm:ss
-     */
-    fun getMillionSeconds(date: String, pattern: String): Long {
-        val sdf = SimpleDateFormat(pattern)
-        var millionSeconds: Long = 0
-        millionSeconds = try {
-            if (date.isEmpty()) {
-                return millionSeconds
-            }
-            // 毫秒
-            sdf.parse(date).time
-        } catch (e: Exception) {
-            e.printStackTrace()
-            return millionSeconds
-        }
-        return millionSeconds
-    }
-
-
-    /**
-     * @return
-     * @方法说明:判断用户的设备时区是否为东八区(中国)
-     * @方法名称:isInEasternEightZones
-     * @返回值:boolean
-     */
-    fun isInEasternEightZones(): Boolean {
-        return TimeZone.getDefault() === TimeZone.getTimeZone("GMT+08")
-    }
-
-
-    /**
-     * @param date
-     * @param oldZone
-     * @param newZone
-     * @return
-     * @方法说明:根据不同时区,转换时间 2014年7月31日
-     * @方法名称:transformTime
-     * @返回值:Date
-     */
-    fun transformTime(date: Date, oldZone: TimeZone, newZone: TimeZone): Date? {
-        val timeOffset = (oldZone.getOffset(date.time) - newZone.getOffset(date.time))
-        return Date(date.time - timeOffset)
-    }
+    fun getFormatDate(time: Long, pattern: String = DateFormat.DEFAULT_PATTERN.dateFormat, locale: Locale = Locale.getDefault()) = SimpleDateFormat(pattern, locale).format(Date(time))
 
+    fun isBefore(src: Date, destDate: Date): Boolean = src.before(destDate)
 
-    /**
-     * 获取星期几
-     * @return 1-7 对应星期一 - 星期日
-     */
-    fun getDayOfWeek(date: Date) : Int{
-        val weeks = arrayOf(7, 1, 2, 3, 4, 5,6)
-
-        val cal = Calendar.getInstance()
-        cal.time = date
-        var week_index = cal[Calendar.DAY_OF_WEEK] - 1
-        if (week_index < 0) {
-            week_index = 0
-        }
-        return weeks[week_index]
-    }
-
-
-    /**
-     * 将系统时间转成英文字符串 星期-日期-月份
-     *
-     * @return Friday 29, Sept.
-     */
-    fun systemTimeToEnglishString(): String {
-        val dataTime: String = getTime("yyyy-MM-dd")
-        val split = dataTime.split("-".toRegex()).toTypedArray()
-        var month = split[1]
-        val day = split[2]
-        var dayOfWeek: Int = getDayOfWeek(Date())
-        var week = ""
-        when (dayOfWeek) {
-            7 -> {
-                week = "Sunday"
-                week = "Sun"
-            }
-            1 -> {
-                week = "Monday"
-                week = "Mon"
-            }
-            2 -> {
-                week = "Tuesday"
-                week = "Tues"
-            }
-            3 -> {
-                week = "Wednesday"
-                week = "Wed"
-            }
-            4 -> {
-                week = "Thursday"
-                week = "Thur"
-            }
-            5 -> {
-                week = "Friday"
-                week = "Fri"
-            }
-            6 -> {
-                week = "Saturday"
-                week = "Sat"
-            }
-            else -> {}
-        }
-        when (month) {
-            "01" -> month = "Jan"
-            "02" -> month = "Feb"
-            "03" -> month = "Mar"
-            "04" -> month = "Apr"
-            "05" -> month = "May"
-            "06" -> month = "Jun"
-            "07" -> month = "Jul"
-            "08" -> month = "Aug"
-            "09" -> month = "Sept"
-            "10" -> month = "Oct"
-            "11" -> month = "Nov"
-            "12" -> month = "Dec"
-            else -> {}
-        }
-        return "$week , $month.$day"
-    }
-
-
-    fun isBefore(src : Date, destDate: Date) : Boolean{
-        return src.before(destDate)
-    }
-
-
-    fun isAfter(src: Date, destDate: Date) : Boolean{
-        return src.after(destDate)
-    }
+    fun isAfter(src: Date, destDate: Date): Boolean = src.after(destDate)
 
     /**
      * 传入的时间是否介于一段时间内
@@ -177,9 +44,5 @@ object DateUtils {
      * @param startDate 介于的开始时间
      * @param endDate 介于的结束时间
      */
-    fun timeBetweenDate(time : Date, startDate: Date, endDate: Date) : Boolean{
-        return isBefore(time, endDate) && isAfter(time, startDate)
-    }
-
-
+    fun timeBetweenDate(time: Date, startDate: Date, endDate: Date): Boolean = isBefore(time, endDate) && isAfter(time, startDate)
 }