mirror of
https://github.com/fankes/TSBattery.git
synced 2025-09-07 03:06:06 +08:00
Modify add some function in FunctionFactory
This commit is contained in:
@@ -19,7 +19,7 @@
|
|||||||
*
|
*
|
||||||
* This file is Created by fankes on 2022/1/7.
|
* This file is Created by fankes on 2022/1/7.
|
||||||
*/
|
*/
|
||||||
@file:Suppress("DEPRECATION", "unused")
|
@file:Suppress("DEPRECATION", "unused", "DiscouragedApi", "InternalInsetResource")
|
||||||
|
|
||||||
package com.fankes.tsbattery.utils.factory
|
package com.fankes.tsbattery.utils.factory
|
||||||
|
|
||||||
@@ -31,6 +31,7 @@ import android.content.pm.PackageInfo
|
|||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.content.res.Configuration
|
import android.content.res.Configuration
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
|
import android.graphics.drawable.Drawable
|
||||||
import android.net.ConnectivityManager
|
import android.net.ConnectivityManager
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.provider.Settings
|
import android.provider.Settings
|
||||||
@@ -108,12 +109,33 @@ fun Context.version(packageName: String) = safeOfNothing {
|
|||||||
} ?: ""
|
} ?: ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到 APP 名称
|
||||||
|
* @param name APP 包名 - 默认为当前 APP
|
||||||
|
* @return [String]
|
||||||
|
*/
|
||||||
|
fun Context.findAppName(name: String = packageName) =
|
||||||
|
safeOfNothing { packageManager?.getPackageInfo(name, 0)?.applicationInfo?.loadLabel(packageManager).toString() }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到 APP 图标
|
||||||
|
* @param name APP 包名 - 默认为当前 APP
|
||||||
|
* @return [Drawable] or null
|
||||||
|
*/
|
||||||
|
fun Context.findAppIcon(name: String = packageName) =
|
||||||
|
safeOfNull { packageManager?.getPackageInfo(name, 0)?.applicationInfo?.loadIcon(packageManager) }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 网络连接是否正常
|
* 网络连接是否正常
|
||||||
* @return [Boolean] 网络是否连接
|
* @return [Boolean] 网络是否连接
|
||||||
*/
|
*/
|
||||||
val isNetWorkSuccess
|
val isNetWorkSuccess get() = appContext.isNetWorkSuccess
|
||||||
get() = safeOfFalse { appContext.getSystemService<ConnectivityManager>()?.activeNetworkInfo != null }
|
|
||||||
|
/**
|
||||||
|
* 网络连接是否正常
|
||||||
|
* @return [Boolean] 网络是否连接
|
||||||
|
*/
|
||||||
|
val Context.isNetWorkSuccess get() = safeOfFalse { getSystemService<ConnectivityManager>()?.activeNetworkInfo != null }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* dp 转换为 pxInt
|
* dp 转换为 pxInt
|
||||||
@@ -129,11 +151,26 @@ fun Number.dp(context: Context) = dpFloat(context).toInt()
|
|||||||
*/
|
*/
|
||||||
fun Number.dpFloat(context: Context) = toFloat() * context.resources.displayMetrics.density
|
fun Number.dpFloat(context: Context) = toFloat() * context.resources.displayMetrics.density
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取绝对状态栏高度
|
||||||
|
* @return [Int]
|
||||||
|
*/
|
||||||
|
val Context.absoluteStatusBarHeight
|
||||||
|
get() = safeOfNan {
|
||||||
|
resources.getDimensionPixelSize(resources.getIdentifier("status_bar_height", "dimen", "android"))
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 弹出 [Toast]
|
* 弹出 [Toast]
|
||||||
* @param msg 提示内容
|
* @param msg 提示内容
|
||||||
*/
|
*/
|
||||||
fun toast(msg: String) = Toast.makeText(appContext, msg, Toast.LENGTH_SHORT).show()
|
fun toast(msg: String) = appContext.toast(msg)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 弹出 [Toast]
|
||||||
|
* @param msg 提示内容
|
||||||
|
*/
|
||||||
|
fun Context.toast(msg: String) = Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 弹出 [Snackbar]
|
* 弹出 [Snackbar]
|
||||||
|
Reference in New Issue
Block a user