Skip to content

Commit

Permalink
Fix some deprecated stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
darshanparajuli committed Mar 13, 2021
1 parent 6d320c1 commit f6738e6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.core.app.TaskStackBuilder
import androidx.core.view.ViewCompat
import androidx.preference.PreferenceManager
import com.dp.logcatapp.R
import com.dp.logcatapp.util.mainHandler
import com.dp.logcatapp.util.setTheme

@SuppressLint("Registered")
Expand All @@ -29,7 +30,7 @@ abstract class BaseActivityWithToolbar : AppCompatActivity() {

lateinit var toolbar: Toolbar
private set
protected val handler = Handler()
protected val handler = mainHandler()

override fun onCreate(savedInstanceState: Bundle?) {
PreferenceManager.setDefaultValues(this, R.xml.settings, false)
Expand All @@ -56,6 +57,7 @@ abstract class BaseActivityWithToolbar : AppCompatActivity() {
supportActionBar?.title = getToolbarTitle()
}

@Suppress("DEPRECATION")
private fun setAppBarPaddingForKitkat(viewGroup: ViewGroup) {
val dm = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(dm)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.dp.logcatapp.fragments.base

import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import androidx.fragment.app.DialogFragment
import com.dp.logcatapp.util.mainHandler

open class BaseDialogFragment : DialogFragment() {
private val handler = Handler()
private val handler = mainHandler()

override fun onCreateAnimation(
transit: Int,
Expand Down Expand Up @@ -43,6 +43,7 @@ open class BaseDialogFragment : DialogFragment() {
}

override fun onDestroyView() {
@Suppress("DEPRECATION")
if (dialog != null && retainInstance)
dialog?.setDismissMessage(null)
super.onDestroyView()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Handler
import android.os.Looper
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.dp.logcatapp.util.mainHandler

open class BaseFragment : Fragment() {

Expand All @@ -16,7 +17,7 @@ open class BaseFragment : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
handler = Handler()
handler = mainHandler()
}

override fun onDestroy() {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/dp/logcatapp/util/HandlerCompat.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.dp.logcatapp.util

import android.os.Handler
import android.os.Looper.getMainLooper

fun mainHandler() = Handler(getMainLooper())
14 changes: 10 additions & 4 deletions app/src/main/java/com/dp/logcatapp/util/MyExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.os.Build.VERSION.SDK_INT
import android.provider.OpenableColumns
import android.util.TypedValue
import android.view.Display
Expand Down Expand Up @@ -99,7 +100,7 @@ fun Context.showToast(
length: Int = Toast.LENGTH_SHORT
) {
val toast = Toast.makeText(this, msg, length)
if (Build.VERSION.SDK_INT <= 25) {
if (SDK_INT <= 25) {
try {
val field = View::class.java.getDeclaredField("mContext")
field.isAccessible = true
Expand All @@ -118,15 +119,20 @@ private class ToastViewContextWrapper(base: Context) : ContextWrapper(base) {
private class ToastViewApplicationContextWrapper(base: Context) : ContextWrapper(base) {
override fun getSystemService(name: String): Any {
return if (name == Context.WINDOW_SERVICE) {
ToastWindowManager(baseContext.getSystemService(name) as WindowManager)
ToastWindowManager(baseContext, baseContext.getSystemService(name) as WindowManager)
} else {
super.getSystemService(name)
}
}
}

private class ToastWindowManager(val base: WindowManager) : WindowManager {
override fun getDefaultDisplay(): Display = base.defaultDisplay
private class ToastWindowManager(
private val context: Context,
private val base: WindowManager
) : WindowManager {
@Suppress("DEPRECATION")
override fun getDefaultDisplay(): Display =
if (SDK_INT >= 30) context.display!! else base.defaultDisplay

override fun addView(
view: View?,
Expand Down

0 comments on commit f6738e6

Please sign in to comment.