|
| 1 | +package protect.card_locker |
| 2 | + |
| 3 | +import android.graphics.Color |
| 4 | +import android.graphics.Typeface |
| 5 | +import android.os.Build |
| 6 | +import android.os.Bundle |
| 7 | +import android.view.View |
| 8 | +import android.widget.FrameLayout |
| 9 | +import android.widget.LinearLayout |
| 10 | +import androidx.appcompat.widget.AppCompatImageView |
| 11 | +import androidx.core.content.ContextCompat |
| 12 | +import androidx.core.graphics.ColorUtils |
| 13 | +import androidx.core.view.WindowInsetsControllerCompat |
| 14 | +import androidx.core.view.children |
| 15 | +import com.google.android.material.color.MaterialColors |
| 16 | +import com.google.android.material.textview.MaterialTextView |
| 17 | +import com.yalantis.ucrop.UCropActivity |
| 18 | + |
| 19 | +class UCropWrapper : UCropActivity() { |
| 20 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 21 | + super.onCreate(savedInstanceState) |
| 22 | + |
| 23 | + Utils.applyWindowInsets(findViewById(android.R.id.content)) |
| 24 | + } |
| 25 | + |
| 26 | + override fun onPostCreate(savedInstanceState: Bundle?) { |
| 27 | + super.onPostCreate(savedInstanceState) |
| 28 | + val darkMode = Utils.isDarkModeEnabled(this) |
| 29 | + // setup status bar to look like the rest of the app |
| 30 | + setupStatusBar(darkMode) |
| 31 | + // find and check views that we wish to color modify |
| 32 | + // for when we update ucrop or switch to another cropper |
| 33 | + checkViews(darkMode) |
| 34 | + // change toolbar font |
| 35 | + changeToolbarFont() |
| 36 | + } |
| 37 | + |
| 38 | + private fun setupStatusBar(darkMode: Boolean) { |
| 39 | + if (window == null) { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + if (Build.VERSION.SDK_INT >= 23) { |
| 44 | + val decorView = window.decorView |
| 45 | + val wic = WindowInsetsControllerCompat(window, decorView) |
| 46 | + wic.isAppearanceLightStatusBars = !darkMode |
| 47 | + } else if (!darkMode) { |
| 48 | + window.statusBarColor = ColorUtils.compositeColors( |
| 49 | + Color.argb(127, 0, 0, 0), |
| 50 | + window.statusBarColor |
| 51 | + ) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private fun checkViews(darkMode: Boolean) { |
| 56 | + var view = findViewById<View?>(com.yalantis.ucrop.R.id.wrapper_controls) |
| 57 | + if (view !is FrameLayout) { |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + val controls = view |
| 62 | + view = findViewById(com.yalantis.ucrop.R.id.wrapper_states) |
| 63 | + if (view !is LinearLayout) { |
| 64 | + return |
| 65 | + } |
| 66 | + val states = view |
| 67 | + controls.children.firstOrNull { it is AppCompatImageView }?.let { |
| 68 | + // everything gathered and are as expected, now perform color patching |
| 69 | + Utils.patchColors(this) |
| 70 | + val colorSurface = MaterialColors.getColor( |
| 71 | + this, |
| 72 | + com.google.android.material.R.attr.colorSurface, |
| 73 | + ContextCompat.getColor( |
| 74 | + this, |
| 75 | + R.color.md_theme_light_surface |
| 76 | + ) |
| 77 | + ) |
| 78 | + val colorOnSurface = MaterialColors.getColor( |
| 79 | + this, |
| 80 | + com.google.android.material.R.attr.colorOnSurface, |
| 81 | + ContextCompat.getColor( |
| 82 | + this, |
| 83 | + R.color.md_theme_light_onSurface |
| 84 | + ) |
| 85 | + ) |
| 86 | + |
| 87 | + val controlsBackgroundImageDrawable = it.background |
| 88 | + controlsBackgroundImageDrawable.mutate() |
| 89 | + controlsBackgroundImageDrawable.setTint( |
| 90 | + if (darkMode) { |
| 91 | + colorOnSurface |
| 92 | + } else { |
| 93 | + colorSurface |
| 94 | + } |
| 95 | + ) |
| 96 | + it.background = controlsBackgroundImageDrawable |
| 97 | + states.setBackgroundColor( |
| 98 | + if (darkMode) { |
| 99 | + colorSurface |
| 100 | + } else { |
| 101 | + colorOnSurface |
| 102 | + } |
| 103 | + ) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private fun changeToolbarFont() { |
| 108 | + val toolbar = findViewById<View?>(com.yalantis.ucrop.R.id.toolbar_title) |
| 109 | + if (toolbar !is MaterialTextView) { |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + val style = intent.getIntExtra(UCROP_TOOLBAR_TYPEFACE_STYLE, -1) |
| 114 | + if (style != -1) { |
| 115 | + toolbar.setTypeface(Typeface.defaultFromStyle(style)) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + internal companion object { |
| 120 | + const val UCROP_TOOLBAR_TYPEFACE_STYLE: String = "ucop_toolbar_typeface_style" |
| 121 | + } |
| 122 | +} |
0 commit comments