Migrate view overrides to Factory2

This commit is contained in:
Suphon Thanakornpakapong committed 2021-10-20 08:24:35 +07:00
1 parent bb336ea973
commit a7c69072b7
3 files changed
+44 -83

No files matched your search

@@ -18,7 +18,6 @@ package app.lawnchair
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewTreeObserver
import androidx.activity.OnBackPressedDispatcher
import androidx.activity.OnBackPressedDispatcherOwner
@@ -66,14 +65,11 @@ class LawnchairLauncher : QuickstepLauncher(), LifecycleOwner,
private val prefs by lazy { PreferenceManager.getInstance(this) }
var allAppsScrimColor = 0
private val customLayoutInflater by lazy {
LawnchairLayoutInflater(super.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater, this)
}
private val themeProvider by lazy { ThemeProvider.INSTANCE.get(this) }
private lateinit var colorScheme: ColorScheme
override fun onCreate(savedInstanceState: Bundle?) {
layoutInflater.factory2 = LawnchairLayoutFactory(this)
savedStateRegistryController.performRestore(savedInstanceState)
super.onCreate(savedInstanceState)
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
@@ -219,13 +215,6 @@ class LawnchairLauncher : QuickstepLauncher(), LifecycleOwner,
return defaultOverlay
}
override fun getSystemService(name: String): Any? {
if (name == Context.LAYOUT_INFLATER_SERVICE) {
return customLayoutInflater
}
return super.getSystemService(name)
}
private fun restartIfPending() {
when {
sRestartFlags and FLAG_RESTART != 0 -> lawnchairApp.restart(false)
@@ -0,0 +1,43 @@
package app.lawnchair
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
import app.lawnchair.font.FontManager
import com.android.launcher3.BubbleTextView
import com.android.launcher3.views.DoubleShadowBubbleTextView
class LawnchairLayoutFactory(context: Context) : LayoutInflater.Factory2 {
private val fontManager by lazy { FontManager.INSTANCE.get(context) }
private val constructorMap = mapOf<String, (Context, AttributeSet) -> View>(
"Button" to ::Button,
"TextView" to ::TextView,
BubbleTextView::class.java.name to ::BubbleTextView,
DoubleShadowBubbleTextView::class.java.name to ::DoubleShadowBubbleTextView,
)
override fun onCreateView(
parent: View?,
name: String,
context: Context,
attrs: AttributeSet
): View? {
val view = constructorMap[name]?.let { it(context, attrs) }
if (view is TextView) {
try {
fontManager.overrideFont(view, attrs)
} catch (e: Exception) {
}
}
return view
}
override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? {
return onCreateView(null, name, context, attrs)
}
}
@@ -1,71 +0,0 @@
/*
* Copyright 2021, Lawnchair
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.lawnchair
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import app.lawnchair.font.FontManager
class LawnchairLayoutInflater(original: LayoutInflater, newContext: Context) : LayoutInflater(original, newContext) {
private val fontManager by lazy { FontManager.INSTANCE.get(context) }
override fun cloneInContext(newContext: Context): LayoutInflater {
return LawnchairLayoutInflater(this, newContext)
}
private fun onViewCreated(view: View, attrs: AttributeSet?) {
if (view is TextView) {
fontManager.overrideFont(view, attrs)
}
}
override fun onCreateView(parent: View?, name: String?, attrs: AttributeSet?): View {
val view = super.onCreateView(parent, name, attrs)
if (view != null) {
onViewCreated(view, attrs)
}
return view
}
@Throws(ClassNotFoundException::class)
override fun onCreateView(name: String, attrs: AttributeSet): View {
for (prefix in sClassPrefixList) {
try {
val view = createView(name, prefix, attrs)
if (view != null) {
return view
}
} catch (e: ClassNotFoundException) {
// In this case we want to let the base class take a crack
// at it.
}
}
return super.onCreateView(name, attrs)
}
companion object {
@JvmStatic
private val sClassPrefixList = arrayOf("android.widget.", "android.webkit.", "android.app.")
}
}