Skip to content

Instantly share code, notes, and snippets.

View hwd6190128's full-sized avatar
🏠
Working from home

Chang Hung Lun hwd6190128

🏠
Working from home
View GitHub Profile
@hwd6190128
hwd6190128 / Event.kt
Created December 9, 2020 10:29 — forked from JoseAlcerreca/Event.kt
An event wrapper for data that is exposed via a LiveData that represents an event.
/**
* Used as a wrapper for data that is exposed via a LiveData that represents an event.
*/
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
// prefer one
private val immersiveRunnable = Runnable {
immersiveMode(this@BaseActivity)
}
companion object {
private const val INITIAL_HIDE_DELAY = 300L
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
dialog?.window?.setFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
)
activity?.run {
dialog?.window?.decorView?.systemUiVisibility = this.window.decorView.systemUiVisibility
}
dialog?.setOnShowListener {
fun Activity.hasOverlayPermission(): Boolean =
if (Build.VERSION.SDK_INT >= 23) Settings.canDrawOverlays(this) else true
fun Activity.requestOverlayPermission(requestCode: Int) {
if (Build.VERSION.SDK_INT >= 23) {
val intent = Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:$packageName"))
startActivityForResult(intent, requestCode)
}
}
object DateUtils {
internal const val ONE_MINUTE: Long = 60
internal const val HALF_HOUR: Long = 30 * 60 // 1800
internal const val ONE_HOUR: Long = 60 * 60 // 3600
internal const val ONE_DAY: Long = 24 * 60 * 60 // 86400
internal const val HALF_MONTH: Long = ONE_DAY * 15 // 1296000
internal const val ONE_MONTH: Long = ONE_DAY * 30 // 2592000
internal const val HALF_YEAR: Long = ONE_MONTH * 6 // 15552000
internal const val ONE_YEAR: Long = ONE_MONTH * 12 // 31104000
import android.view.View
import android.view.animation.Animation
import android.view.animation.Transformation
/**
* Created by Howard Chang on 2018/10/25.
*/
object AnimUtils {
@hwd6190128
hwd6190128 / Android ResUtils.kt
Last active May 21, 2019 09:36
ResUtils fun of android dev
/**
* Created by Howard Chang on 2018/10/25.
*/
object ResUtils {
fun getStringArray(@ArrayRes id: Int, context: Context): Array<String> {
return context.resources.getStringArray(id)
}
fun getIntArray(@ArrayRes id: Int, context: Context): IntArray {
@hwd6190128
hwd6190128 / Android DeviceUtils.kt
Last active March 8, 2020 21:47
DeviceUtils fun of android dev
/**
* immersiveMode
* set onPostResume, onSystemUiVisibilityChange
*/
fun immersiveMode(activity: Activity) {
activity.window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN)
activity.window
.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
@hwd6190128
hwd6190128 / Android Utils.kt
Last active May 21, 2019 09:37
Utils fun of android dev
val Int.pxToDp: Int
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
val Int.dpToPx: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
val Int.spToPx: Int
get() = (this * Resources.getSystem().displayMetrics.scaledDensity).toInt()

setup adb

Add platform-tools to your path

echo 'export ANDROID_HOME=/Users/$USER/Library/Android/sdk' >> ~/.bash_profile
echo 'export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools' >> ~/.bash_profile

Refresh your bash profile (or restart your terminal app)

source ~/.bash_profile