diff --git a/app/src/main/java/com/dp/logcatapp/activities/BaseActivityWithToolbar.kt b/app/src/main/java/com/dp/logcatapp/activities/BaseActivityWithToolbar.kt index d138166..f1a9c75 100644 --- a/app/src/main/java/com/dp/logcatapp/activities/BaseActivityWithToolbar.kt +++ b/app/src/main/java/com/dp/logcatapp/activities/BaseActivityWithToolbar.kt @@ -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") @@ -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) @@ -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) diff --git a/app/src/main/java/com/dp/logcatapp/fragments/base/BaseDialogFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/base/BaseDialogFragment.kt index 9384dd0..aea8a9c 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/base/BaseDialogFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/base/BaseDialogFragment.kt @@ -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, @@ -43,6 +43,7 @@ open class BaseDialogFragment : DialogFragment() { } override fun onDestroyView() { + @Suppress("DEPRECATION") if (dialog != null && retainInstance) dialog?.setDismissMessage(null) super.onDestroyView() diff --git a/app/src/main/java/com/dp/logcatapp/fragments/base/BaseFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/base/BaseFragment.kt index 147c2ae..c335264 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/base/BaseFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/base/BaseFragment.kt @@ -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() { @@ -16,7 +17,7 @@ open class BaseFragment : Fragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - handler = Handler() + handler = mainHandler() } override fun onDestroy() { diff --git a/app/src/main/java/com/dp/logcatapp/util/HandlerCompat.kt b/app/src/main/java/com/dp/logcatapp/util/HandlerCompat.kt new file mode 100644 index 0000000..16cc8ee --- /dev/null +++ b/app/src/main/java/com/dp/logcatapp/util/HandlerCompat.kt @@ -0,0 +1,6 @@ +package com.dp.logcatapp.util + +import android.os.Handler +import android.os.Looper.getMainLooper + +fun mainHandler() = Handler(getMainLooper()) \ No newline at end of file diff --git a/app/src/main/java/com/dp/logcatapp/util/MyExtensions.kt b/app/src/main/java/com/dp/logcatapp/util/MyExtensions.kt index bc35357..32704b3 100644 --- a/app/src/main/java/com/dp/logcatapp/util/MyExtensions.kt +++ b/app/src/main/java/com/dp/logcatapp/util/MyExtensions.kt @@ -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 @@ -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 @@ -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?,