Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
Remove some reflection usages in favor of official APIs on API 29
Browse files Browse the repository at this point in the history
  • Loading branch information
saket committed Dec 10, 2019
1 parent e7ab820 commit a7b3c92
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 40 deletions.

This file was deleted.

34 changes: 34 additions & 0 deletions androidApp/src/main/java/press/theme/TextViewCompat.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package press.theme

import android.graphics.drawable.Drawable
import android.os.Build.VERSION
import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources.getDrawable
import press.util.reflect

object TextViewCompat {

fun textSelectionHandles(view: TextView): Array<Drawable> {
if (VERSION.SDK_INT >= 29) {
return arrayOf(view.textSelectHandle!!, view.textSelectHandleLeft!!, view.textSelectHandleRight!!)

} else {
val centerDrawableResId = reflect(TextView::class, "mTextSelectHandleRes").getInt(view)
val leftDrawableResId = reflect(TextView::class, "mTextSelectHandleLeftRes").getInt(view)
val rightDrawableResId = reflect(TextView::class, "mTextSelectHandleRightRes").getInt(view)

val editorField = reflect(TextView::class, "mEditor")
val editor = editorField.get(view)

val centerDrawable = getDrawable(view.context, centerDrawableResId)!!
val leftDrawable = getDrawable(view.context, leftDrawableResId)!!
val rightDrawable = getDrawable(view.context, rightDrawableResId)!!

reflect(editor::class, "mSelectHandleCenter").set(editor, centerDrawable)
reflect(editor::class, "mSelectHandleLeft").set(editor, leftDrawable)
reflect(editor::class, "mSelectHandleRight").set(editor, rightDrawable)

return arrayOf(centerDrawable, leftDrawable, rightDrawable)
}
}
}
10 changes: 3 additions & 7 deletions androidApp/src/main/java/press/theme/ThemedWidgets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import com.jakewharton.rxbinding3.view.attaches
import com.jakewharton.rxbinding3.view.detaches
import press.App
import press.util.onDestroys
import press.util.reflect
import press.widgets.PorterDuffColorFilterWrapper
import press.widgets.findTitleView
import press.widgets.textColor
import me.saket.press.shared.theme.ThemePalette
import me.saket.press.R
import press.widgets.ScrollViewCompat

fun themePalette() = App.component.themePalette()

Expand All @@ -52,7 +52,7 @@ fun themed(view: TextView): TextView = view.apply {

fun themed(view: EditText) = view.apply {
typeface = ResourcesCompat.getFont(context, R.font.work_sans_regular)
val selectionHandleDrawables = EditTextSelectionHandleReflection.find(this)
val selectionHandleDrawables = TextViewCompat.textSelectionHandles(this)

themeAware { palette ->
selectionHandleDrawables.forEach { it.setColorFilter(palette.accentColor, SRC_IN) }
Expand All @@ -61,12 +61,8 @@ fun themed(view: EditText) = view.apply {
}

fun themed(view: ScrollView) = view.apply {
val topEdge = reflect(ScrollView::class, "mEdgeGlowTop").get(view) as EdgeEffect
val bottomEdge = reflect(ScrollView::class, "mEdgeGlowBottom").get(view) as EdgeEffect

themeAware {
topEdge.color = it.accentColor
bottomEdge.color = it.accentColor
ScrollViewCompat.setEdgeEffectColor(view, it.accentColor)
}
}

Expand Down
6 changes: 3 additions & 3 deletions androidApp/src/main/java/press/util/Reflection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ inline fun <reified T : Any> reflect(): Reflection =
Reflection(T::class.java)

class Reflection(private val clazz: Class<out Any>) {
fun field(name: String): Field =
fun field(name: FieldName): Field =
fieldCache.getOrPut(
key = clazz to name,
defaultValue = { clazz.getDeclaredField(name).apply { isAccessible = true } }
)

fun method(name: String, vararg paramTypes: Class<out Any>): Method =
fun method(name: MethodName, vararg paramTypes: Class<out Any>): Method =
methodCache.getOrPut(
key = Triple(clazz, name, paramTypes),
defaultValue = {
Expand All @@ -31,6 +31,6 @@ class Reflection(private val clazz: Class<out Any>) {

companion object {
val fieldCache = mutableMapOf<Pair<Class<out Any>, FieldName>, Field>()
val methodCache = mutableMapOf<Triple<Class<out Any>, String, Array<out Class<out Any>>>, Method>()
val methodCache = mutableMapOf<Triple<Class<out Any>, MethodName, Array<out Class<out Any>>>, Method>()
}
}
23 changes: 23 additions & 0 deletions androidApp/src/main/java/press/widgets/ScrollViewCompat.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package press.widgets

import android.os.Build.VERSION
import android.widget.EdgeEffect
import android.widget.ScrollView
import androidx.annotation.ColorInt
import press.util.reflect

object ScrollViewCompat {

fun setEdgeEffectColor(view: ScrollView, @ColorInt color: Int) {
if (VERSION.SDK_INT >= 29) {
view.topEdgeEffectColor = color
view.bottomEdgeEffectColor = color

} else {
val topEdge = reflect(ScrollView::class, "mEdgeGlowTop").get(view) as EdgeEffect
val bottomEdge = reflect(ScrollView::class, "mEdgeGlowBottom").get(view) as EdgeEffect
topEdge.color = color
bottomEdge.color = color
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- The user never sees this Drawable. See TintedCursorDrawableInterceptor. -->
<size android:width="2dp" />
<solid android:color="#FF0000" />
</shape>
3 changes: 2 additions & 1 deletion androidApp/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<resources>
<color name="default_window_background">#353846</color>

<color name="overscroll">#FFD43D</color>
<!-- The user should never see this color. See ThemeAwareActivity.kt and ThemedWidgets.kt. -->
<color name="overscroll_canary">#FFD43D</color>
</resources>
3 changes: 2 additions & 1 deletion androidApp/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<resources>

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- These colors are applied in Kotlin. The user should never see these. -->
<item name="colorPrimary">#f00</item>
<item name="colorPrimaryDark">#f00</item>
<item name="colorAccent">#f00</item>

<item name="android:editTextStyle">@style/DefaultEditTextStyle</item>
<item name="android:colorEdgeEffect">@color/overscroll</item>
<item name="android:colorEdgeEffect">@color/overscroll_canary</item>

<item name="android:windowBackground">@color/default_window_background</item>
<item name="android:statusBarColor">@color/default_window_background</item>
Expand Down

0 comments on commit a7b3c92

Please sign in to comment.