mirror of
https://github.com/awfixers-stuff/OwnDroid.git
synced 2026-03-23 19:15:58 +00:00
88 lines
3.0 KiB
Kotlin
88 lines
3.0 KiB
Kotlin
package com.bintianqi.owndroid
|
|
|
|
import android.app.admin.DevicePolicyManager
|
|
import android.content.ClipData
|
|
import android.content.ClipboardManager
|
|
import android.content.ComponentName
|
|
import android.content.Context
|
|
import android.net.Uri
|
|
import android.widget.Toast
|
|
import androidx.activity.ComponentActivity
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
import androidx.annotation.RequiresApi
|
|
import androidx.annotation.StringRes
|
|
import com.bintianqi.owndroid.dpm.addDeviceAdmin
|
|
import java.io.FileNotFoundException
|
|
import java.io.IOException
|
|
import java.io.InputStream
|
|
import java.text.SimpleDateFormat
|
|
import java.time.Instant
|
|
import java.time.ZoneId
|
|
import java.time.format.DateTimeFormatter
|
|
import java.util.Date
|
|
import java.util.Locale
|
|
|
|
var zhCN = true
|
|
|
|
fun uriToStream(
|
|
context: Context,
|
|
uri: Uri,
|
|
operation: (stream: InputStream)->Unit
|
|
){
|
|
try {
|
|
val stream = context.contentResolver.openInputStream(uri)
|
|
if(stream != null) { operation(stream) }
|
|
stream?.close()
|
|
}
|
|
catch(_: FileNotFoundException) { Toast.makeText(context, R.string.file_not_exist, Toast.LENGTH_SHORT).show() }
|
|
catch(_: IOException) { Toast.makeText(context, R.string.io_exception, Toast.LENGTH_SHORT).show() }
|
|
}
|
|
|
|
fun writeClipBoard(context: Context, string: String):Boolean{
|
|
val clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
|
try {
|
|
clipboardManager.setPrimaryClip(ClipData.newPlainText("", string))
|
|
} catch(_:Exception) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
fun registerActivityResult(context: ComponentActivity) {
|
|
addDeviceAdmin = context.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
|
val dpm = context.applicationContext.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
|
|
if(dpm.isAdminActive(ComponentName(context.applicationContext, Receiver::class.java))) {
|
|
backToHomeStateFlow.value = true
|
|
}
|
|
}
|
|
}
|
|
|
|
fun formatFileSize(bytes: Long): String {
|
|
val kb = 1024
|
|
val mb = kb * 1024
|
|
val gb = mb * 1024
|
|
return when {
|
|
bytes >= gb -> String.format(Locale.US, "%.2f GB", bytes / gb.toDouble())
|
|
bytes >= mb -> String.format(Locale.US, "%.2f MB", bytes / mb.toDouble())
|
|
bytes >= kb -> String.format(Locale.US, "%.2f KB", bytes / kb.toDouble())
|
|
else -> "$bytes bytes"
|
|
}
|
|
}
|
|
|
|
val Boolean.yesOrNo
|
|
@StringRes get() = if(this) R.string.yes else R.string.no
|
|
|
|
@RequiresApi(26)
|
|
fun parseTimestamp(timestamp: Long): String {
|
|
val instant = Instant.ofEpochMilli(timestamp)
|
|
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault())
|
|
return formatter.format(instant)
|
|
}
|
|
|
|
val Long.humanReadableDate: String
|
|
get() = SimpleDateFormat("yyyy/MM/dd", Locale.getDefault()).format(Date(this))
|
|
|
|
fun Context.showOperationResultToast(success: Boolean) {
|
|
Toast.makeText(this, if(success) R.string.success else R.string.failed, Toast.LENGTH_SHORT).show()
|
|
}
|