update readme

This commit is contained in:
luyao 2019-07-16 16:55:35 +08:00
parent f1decbeb5b
commit 314f640b9c
5 changed files with 187 additions and 64 deletions

175
README.md
View File

@ -11,91 +11,162 @@
### [权限相关](https://github.com/lulululbj/AndroidUtilCodeKTX/blob/master/ktx/src/main/java/luyao/util/ktx/ext/permission/PermissionExt.kt) [[Demo]](https://github.com/lulululbj/AndroidUtilCodeKTX/blob/master/app/src/main/java/luyao/util/ktx/ui/PermissionExtActivity.kt)
```kotlin
fun FragmentActivity.request() :权限请求
FragmentActivity.request(vararg permissions: String, callbacks: PermissionsCallbackDSL.() -> Unit)
```
### [AppExt](https://github.com/lulululbj/AndroidUtilCodeKTX/blob/master/ktx/src/main/java/luyao/util/ktx/ext/AppExt.kt)
```kotlin
fun Context.getVersionName() : 获取应用版本名称
fun Context.getVersionCode() :获取应用版本号
fun Context.getAppSignature() :获取应用签名
fun Context.getAppInfo() :根据安装包获取应用信息
fun Context.getAppInfos()
Context.versionName: String
Context.versionCode: Long
Context.getAppInfo(apkPath: String): AppInfo
Context.getAppInfos(apkFolderPath: String): List<AppInfo>
Context.getAppSignature(packageName: String = this.packageName): ByteArray?
```
### [CommonExt](https://github.com/lulululbj/AndroidUtilCodeKTX/blob/master/ktx/src/main/java/luyao/util/ktx/ext/CommonExt.kt)
```kotlin
fun Context.dp2px() : dp 转 px
fun Context.px2dp() px 转 dp
fun View.dp2px() dp 转 px
fun View.px2dp() px 转 dp
fun Context.getWidth() :获取屏幕宽度
fun Context.getHeight() :获取屏幕高度
fun Context.copyToClipboard() :复制到粘贴板
Context.dp2px(dp: Float): Int
Context.px2dp(px: Float): Int
View.dp2px(dp: Float): Int
View.px2dp(px: Float): Int
Context.screenWidth
Context.screenHeight
Context.copyToClipboard(label: String, text: String)
```
### [HashExt](https://github.com/lulululbj/AndroidUtilCodeKTX/blob/master/ktx/src/main/java/luyao/util/ktx/ext/HashExt.kt) [[Demo]](https://github.com/lulululbj/AndroidUtilCodeKTX/blob/master/ktx/src/test/java/luyao/util/ktx/ext/HashExtKtTest.kt)
```kotlin
fun ByteArray.hash() :获取字节数组哈希值
fun String.hash() :获取字符串哈希值
fun File.hash() :获取文件哈希值
fun ByteArray.md5Bytes()
fun ByteArray.md5() :获取字节数组 md5 值
fun String.md5() :获取字符串 md5 值
fun ByteArray.sha1Bytes()
fun ByteArray.sha1() :获取字节数组 sha1 值
fun String.sha1() :获取字符串 sha1 值
fun ByteArray.sha224Bytes()
fun ByteArray.sha224() :获取字节数组 sha224 值
fun String.sha224() :获取字符串 sha224 值
fun ByteArray.sha256Bytes()
fun ByteArray.sha256() :获取字节数组 sha256 值
fun String.sha256() :获取字符串 sha256 值
fun ByteArray.sha384Bytes()
fun ByteArray.sha384() :获取字节数组 sha384 值
fun String.sha384() :获取字符串 sha384 值
fun ByteArray.sha512Bytes()
fun ByteArray.sha512() :获取字节数组 sha512 值
fun String.sha512() :获取字符串 sha512 值
ByteArray.hash(algorithm: Hash): String
String.hash(algorithm: Hash, charset: Charset = Charset.forName("utf-8")): String
ByteArray.md5Bytes(): ByteArray
ByteArray.md5(): String
String.md5(charset: Charset = Charset.forName("utf-8")): String
ByteArray.sha1Bytes(): ByteArray
ByteArray.sha1(): String
String.sha1(charset: Charset = Charset.forName("utf-8")): String
ByteArray.sha224Bytes(): ByteArray
ByteArray.sha224(): String
String.sha224(charset: Charset = Charset.forName("utf-8")): String
ByteArray.sha256Bytes(): ByteArray
ByteArray.sha256(): String
String.sha256(charset: Charset = Charset.forName("utf-8")): String
ByteArray.sha384Bytes(): ByteArray
ByteArray.sha384(): String
String.sha384(charset: Charset = Charset.forName("utf-8")): String
ByteArray.sha512Bytes(): ByteArray
ByteArray.sha512(): String
String.sha512(charset: Charset = Charset.forName("utf-8")): String
File.hash(algorithm: Hash = Hash.SHA1): String
```
### [AesExt](https://github.com/lulululbj/AndroidUtilCodeKTX/blob/master/ktx/src/main/java/luyao/util/ktx/ext/AesExt.kt) [[Demo]](https://github.com/lulululbj/AndroidUtilCodeKTX/blob/master/ktx/src/test/java/luyao/util/ktx/ext/AesExtKtTest.kt)
```kotlin
fun ByteArray.aesEncrypt() :加密字节数组,默认 CFB
fun ByteArray.aesDecrypt() :解密字节数组,默认 CFB
fun File.aesEncrypt() 加密文件CFB 流加密模式
fun File.aesDecrypt() 解密文件CFB 流加密模式
ByteArray.aesEncrypt(key: ByteArray, iv: ByteArray, cipherAlgotirhm: String = AES_CFB_NOPADDING): ByteArray
ByteArray.aesDecrypt(key: ByteArray, iv: ByteArray, cipherAlgotirhm: String = AES_CFB_NOPADDING): ByteArray
File.aesEncrypt(key: ByteArray, iv: ByteArray, destFilePath: String): File?
File.aesDecrypt(key: ByteArray, iv: ByteArray, destFilePath: String): File?
```
### [IntentExt](https://github.com/lulululbj/AndroidUtilCodeKTX/blob/master/ktx/src/main/java/luyao/util/ktx/ext/IntentExt.kt) [[Demo]](https://github.com/lulululbj/AndroidUtilCodeKTX/blob/master/app/src/main/java/luyao/util/ktx/ui/IntentExtActivity.kt)
```kotlin
fun Context.goToAppInfoPage() : 跳转到应用信息页面
fun Context.goToDateAndTimePage() : 跳转到日期和时间设置页面
fun Context.goToLanguagePage() : 跳转到语言设置页面
fun Context.goToAccessibilitySetting() : 跳转到无障碍服务设置页面
fun Context.openApp() : 打开 app
fun Context.openInAppStore() : 在应用商店打开 app
fun Context.installApk() : 安装 apk
fun Context.openBrowser(url:String) :打开浏览器跳转到指定网页
fun Context.uninstallApp() : 卸载 app
Context.getAppInfoIntent(packageName: String = this.packageName): Intent
Context.goToAppInfoPage(packageName: String = this.packageName)
Context.getDateAndTimeIntent(): Intent
Context.goToDateAndTimePage()
Context.getLanguageIntent()
Context.goToLanguagePage()
Context.getInstallIntent(apkFile: File): Intent?
Context.goToAccessibilitySetting()
Context.installApk(apkFile: File)
Context.openBrowser(url: String)
Context.openInAppStore(packageName: String = this.packageName)
Context.openApp(packageName: String)
Context.uninstallApp(packageName: String)
```
### [LogExt]()
```kotlin
String.logv(tag: String = TAG)
String.logd(tag: String = TAG)
String.logi(tag: String = TAG)
String.logw(tag: String = TAG)
String.loge(tag: String = TAG)
```
### [ToastExt](https://github.com/lulululbj/AndroidUtilCodeKTX/blob/master/ktx/src/main/java/luyao/util/ktx/ext/ToastExt.kt) [[Demo]](https://github.com/lulululbj/AndroidUtilCodeKTX/blob/master/app/src/main/java/luyao/util/ktx/ui/ToastExtActivity.kt)
```kotlin
fun Context.toast()
fun Context.longToast()
fun Any.toast()
fun Any.longToast()
Context.toast(content: String, duration: Int = Toast.LENGTH_SHORT)
Context.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_SHORT)
Context.longToast(content: String)
Context.longToast(@StringRes id: Int)
Any.toast(context: Context, content: String, duration: Int = Toast.LENGTH_SHORT)
Any.toast(context: Context, @StringRes id: Int, duration: Int=Toast.LENGTH_SHORT)
Any.longToast(context: Context, content: String)
Any.longToast(context: Context, @StringRes id: Int)
```
### [SharedPreferencesExt]()
```kotlin
fun <T> Context.putSpValue(key: String, value: T, name: String = packageName)
fun <T> Activity.putSpValue(key: String, value: T, name: String = packageName)
fun <T> Context.getSpValue(key: String, default: T, name: String = packageName ): T
fun <T> Activity.getSpValue(key: String, default: T, name: String = packageName): T
```
### [SystemServiceExt]()
```kotlin
val Context.windowManager
val Context.clipboardManager
val Context.layoutInflater
val Context.activityManager
val Context.powerManager
val Context.alarmManager
val Context.notificationManager
val Context.keyguardManager
val Context.locationManager
val Context.searchManager
val Context.storageManager
val Context.vibrator
val Context.connectivityManager
val Context.wifiManager
val Context.audioManager
val Context.mediaRouter
val Context.telephonyManager
val Context.sensorManager
val Context.subscriptionManager
val Context.carrierConfigManager
val Context.inputMethodManager
val Context.uiModeManager
val Context.downloadManager
val Context.batteryManager
val Context.jobScheduler
```
### [TransformExt](https://github.com/lulululbj/AndroidUtilCodeKTX/blob/master/ktx/src/main/java/luyao/util/ktx/ext/TransformExt.kt)
```kotlin
fun ByteArray.toHexString() : 字节数组转十六进制字符串
ByteArray.toHexString() : 字节数组转十六进制字符串
```
### [ViewExt]()
```kotlin
View.visible()
View.invisible()
View.gone()
var View.isVisible: Boolean
var View.isInvisible: Boolean
var View.isGone: Boolean
View.setPadding(@Px size: Int)
View.postDelayed(delayInMillis: Long, crossinline action: () -> Unit): Runnable
View.toBitmap(config: Bitmap.Config = Bitmap.Config.ARGB_8888): Bitmap
```

View File

@ -5,15 +5,25 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import androidx.fragment.app.Fragment
import java.io.Serializable
/**
* Created by luyao
* on 2019/7/9 14:17
*/
inline fun <reified T : Activity> Activity.startKtxActivity(flags: Int? = null, extra: Bundle? = null): Unit =
inline fun <reified T : Activity> Activity.startKtxActivity(flags: Int? = null, extra: Bundle? = null) =
startActivity(getIntent<T>(flags, extra))
inline fun <reified T : Activity> Fragment.startKtxActivity(flags: Int? = null, extra: Bundle? = null) =
activity?.let {
startActivity(activity?.getIntent<T>(flags, extra))
}
inline fun <reified T : Activity> Context.startKtxActivity(flags: Int? = null, extra: Bundle? = null) =
startActivity(getIntent<T>(flags, extra))
inline fun <reified T : Activity> Activity.startKtxActivity(
flags: Int? = null,
extra: Bundle? = null,
@ -21,6 +31,22 @@ inline fun <reified T : Activity> Activity.startKtxActivity(
): Unit =
startActivity(getIntent<T>(flags, extra, arrayListOf(value)))
inline fun <reified T : Activity> Fragment.startKtxActivity(
flags: Int? = null,
extra: Bundle? = null,
value: Pair<String, Any>?
) =
activity?.let {
startActivity(activity?.getIntent<T>(flags, extra, arrayListOf(value)))
}
inline fun <reified T : Activity> Context.startKtxActivity(
flags: Int? = null,
extra: Bundle? = null,
value: Pair<String, Any>?
): Unit =
startActivity(getIntent<T>(flags, extra, arrayListOf(value)))
inline fun <reified T : Activity> Activity.startKtxActivity(
flags: Int? = null,
extra: Bundle? = null,
@ -28,6 +54,22 @@ inline fun <reified T : Activity> Activity.startKtxActivity(
): Unit =
startActivity(getIntent<T>(flags, extra, values))
inline fun <reified T : Activity> Fragment.startKtxActivity(
flags: Int? = null,
extra: Bundle? = null,
values: List<Pair<String, Any>?>?
) =
activity?.let {
startActivity(activity?.getIntent<T>(flags, extra, values))
}
inline fun <reified T : Activity> Context.startKtxActivity(
flags: Int? = null,
extra: Bundle? = null,
values: List<Pair<String, Any>?>?
) =
startActivity(getIntent<T>(flags, extra, values))
inline fun <reified T : Activity> Activity.startKtxActivityForResult(
requestCode: Int,
flags: Int? = null,
@ -35,13 +77,22 @@ inline fun <reified T : Activity> Activity.startKtxActivityForResult(
): Unit =
startActivityForResult(getIntent<T>(flags, extra), requestCode)
inline fun <reified T : Context> Activity.getIntent(flags: Int?, extra: Bundle?): Intent =
inline fun <reified T : Activity> Fragment.startKtxActivityForResult(
requestCode: Int,
flags: Int? = null,
extra: Bundle? = null
) =
activity?.let {
startActivityForResult(activity?.getIntent<T>(flags, extra), requestCode)
}
inline fun <reified T : Context> Context.getIntent(flags: Int?, extra: Bundle?): Intent =
Intent(this, T::class.java).apply {
flags?.let { setFlags(flags) }
extra?.let { putExtras(extra) }
}
inline fun <reified T : Context> Activity.getIntent(
inline fun <reified T : Context> Context.getIntent(
flags: Int?,
extra: Bundle?,
pairs: List<Pair<String, Any>?>?
@ -83,4 +134,5 @@ inline fun <reified T : Context> Activity.getIntent(
}
}
}
}
}

View File

@ -37,7 +37,7 @@ val Context.screenWidth
val Context.screenHeight
get() = resources.displayMetrics.heightPixels
fun Context.copyToClipboard(text: String, label: String) {
fun Context.copyToClipboard(label: String, text: String) {
val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clipData = ClipData.newPlainText(label, text)
cm.primaryClip = clipData

View File

@ -13,11 +13,11 @@ private enum class LEVEL {
V, D, I, W, E
}
fun String.logV(tag: String = TAG) = log(LEVEL.V, tag, this)
fun String.logD(tag: String = TAG) = log(LEVEL.D, tag, this)
fun String.logI(tag: String = TAG) = log(LEVEL.I, tag, this)
fun String.logW(tag: String = TAG) = log(LEVEL.W, tag, this)
fun String.logE(tag: String = TAG) = log(LEVEL.E, tag, this)
fun String.logv(tag: String = TAG) = log(LEVEL.V, tag, this)
fun String.logd(tag: String = TAG) = log(LEVEL.D, tag, this)
fun String.logi(tag: String = TAG) = log(LEVEL.I, tag, this)
fun String.logw(tag: String = TAG) = log(LEVEL.W, tag, this)
fun String.loge(tag: String = TAG) = log(LEVEL.E, tag, this)
private fun log(level: LEVEL, tag: String, message: String) {
when (level) {

View File

@ -30,7 +30,7 @@ fun Activity.sp(name: String = packageName, mode: Int = Context.MODE_PRIVATE): S
getSharedPreferences(name, mode)
fun <T> Context.putSpValue(name: String = packageName, key: String, value: T) = sp(name).edit {
fun <T> Context.putSpValue(key: String, value: T, name: String = packageName) = sp(name).edit {
when (value) {
is Long -> putLong(key, value)
is String -> putString(key, value)
@ -52,7 +52,7 @@ fun <T> Activity.putSpValue(key: String, value: T, name: String = packageName) =
}
}
fun <T> Context.getSpValue(key: String, default: T, name: String = packageName ): T = sp(name).run {
fun <T> Context.getSpValue(key: String, default: T, name: String = packageName): T = sp(name).run {
val result = when (default) {
is Long -> getLong(key, default)
is String -> getString(key, default)