Skip to content

Instantly share code, notes, and snippets.

@trfiladelfo
Forked from hwd6190128/Android DeviceUtils.kt
Created March 8, 2020 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trfiladelfo/b37cd800b03dd7bcad7be6866b6d36d3 to your computer and use it in GitHub Desktop.
Save trfiladelfo/b37cd800b03dd7bcad7be6866b6d36d3 to your computer and use it in GitHub Desktop.
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) {
activity.window.decorView.systemUiVisibility =
(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
or View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
} else {
activity.window.decorView.systemUiVisibility =
(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}
}
/**
* Check network is available
*/
fun hasNetworkConnection(context: Context): Boolean {
val mConnectivityManager =
context!!.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
val networkCapabilities =
mConnectivityManager.getNetworkCapabilities(mConnectivityManager.activeNetwork)
networkCapabilities != null && networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
} else {
val activeNetworkInfo = mConnectivityManager.activeNetworkInfo
!activeNetworkInfo.isConnected
}
}
fun showKeyboard(editor: EditText, mContext: Context) {
val imm = mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(editor, InputMethodManager.SHOW_FORCED)
}
fun hideKeyboard(activity: Activity?, view: View? = null) {
if (activity != null) {
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val windowToken: IBinder?
if (view != null) {
windowToken = view.windowToken
} else {
var v = activity.currentFocus
if (v == null) {
v = View(activity)
}
windowToken = v.windowToken
}
// hide
if (windowToken != null) {
imm.hideSoftInputFromWindow(windowToken, 0)
}
// disable auto focus
if (activity.window != null) {
activity.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
}
}
}
fun isTablet(context: Context): Boolean {
return context.resources.configuration.screenLayout and
Configuration.SCREENLAYOUT_SIZE_MASK >= Configuration.SCREENLAYOUT_SIZE_LARGE
}
fun is7Tablet(act: Activity): Boolean {
val metrics = DisplayMetrics()
act.windowManager.defaultDisplay.getMetrics(metrics)
val widthInInches = metrics.widthPixels / metrics.xdpi
val heightInInches = metrics.heightPixels / metrics.ydpi
val sizeInInches = Math.sqrt(Math.pow(widthInInches.toDouble(), 2.0) + Math.pow(heightInInches.toDouble(), 2.0))
//0.5" buffer for 7" devices
return sizeInInches in 6.5..7.5
}
fun is10Tablet(act: Activity): Boolean {
val metrics = DisplayMetrics()
act.windowManager.defaultDisplay.getMetrics(metrics)
val widthInInches = metrics.widthPixels / metrics.xdpi
val heightInInches = metrics.heightPixels / metrics.ydpi
val sizeInInches = Math.sqrt(Math.pow(widthInInches.toDouble(), 2.0) + Math.pow(heightInInches.toDouble(), 2.0))
//0.5" buffer for 7" devices
return sizeInInches > 7.5
}
fun getDeviceHeight(context: Context = ShenZhuanApplicationLike.applicationContext()): Int {
val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val display = wm.defaultDisplay
val size = Point()
display?.getSize(size)
return size.y
}
fun getDeviceWidth(context: Context = ShenZhuanApplicationLike.applicationContext()): Int {
val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val display = wm.defaultDisplay
val size = Point()
display?.getSize(size)
return size.x
}
fun getDisplaySize(context: Context): Point {
val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val display = wm.defaultDisplay
val size = Point()
display?.getSize(size)
return size
}
fun getNavBarHeight(context: Context): Int {
val res = context.resources
val resId = res.getIdentifier("navigation_bar_height", "dimen", "android")
if (resId > 0) {
return res.getDimensionPixelSize(resId)
}
return 0
}
fun getStatusBarHeight(context: Context): Int {
val res = context.resources
val resId = res.getIdentifier("status_bar_height", "dimen", "android")
if (resId > 0) {
return res.getDimensionPixelSize(resId)
}
return 0
}
fun adjustFontScale(activity: Activity) {
val configuration = activity.resources.configuration
if (configuration.fontScale > 1.0f) {
configuration.fontScale = 1.0f
val metrics = activity.resources.displayMetrics
val wm = activity.getSystemService(Context.WINDOW_SERVICE) as WindowManager
wm.defaultDisplay.getMetrics(metrics)
metrics.scaledDensity = configuration.fontScale * metrics.density
activity.resources.updateConfiguration(configuration, metrics)
activity.baseContext.resources.updateConfiguration(configuration, metrics)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment