Refine AutoCompleteEditTextPreferenceDialogFragment

This commit is contained in:
Mygod
2019-07-16 18:02:37 +08:00
parent f79e20fe11
commit cb5e8ee420
5 changed files with 45 additions and 84 deletions

View File

@@ -15,8 +15,9 @@ import be.mygod.vpnhotspot.net.monitor.FallbackUpstreamMonitor
import be.mygod.vpnhotspot.net.monitor.IpMonitor
import be.mygod.vpnhotspot.net.monitor.UpstreamMonitor
import be.mygod.vpnhotspot.net.wifi.WifiDoubleLock
import be.mygod.vpnhotspot.preference.AlwaysAutoCompleteEditTextPreferenceDialogFragmentCompat
import be.mygod.vpnhotspot.preference.AlwaysAutoCompleteEditTextPreferenceDialogFragment
import be.mygod.vpnhotspot.preference.SharedPreferenceDataStore
import be.mygod.vpnhotspot.preference.SummaryFallbackProvider
import be.mygod.vpnhotspot.util.RootSession
import be.mygod.vpnhotspot.util.launchUrl
import be.mygod.vpnhotspot.widget.SmartSnackbar
@@ -40,6 +41,8 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
RoutingManager.masqueradeMode = RoutingManager.masqueradeMode
preferenceManager.preferenceDataStore = SharedPreferenceDataStore(app.pref)
addPreferencesFromResource(R.xml.pref_settings)
SummaryFallbackProvider(findPreference(UpstreamMonitor.KEY)!!)
SummaryFallbackProvider(findPreference(FallbackUpstreamMonitor.KEY)!!)
findPreference<SwitchPreference>("system.enableTetherOffload")!!.apply {
if (Build.VERSION.SDK_INT >= 27) {
isChecked = TetherOffloadManager.enabled
@@ -167,7 +170,7 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
override fun onDisplayPreferenceDialog(preference: Preference) {
when (preference.key) {
UpstreamMonitor.KEY, FallbackUpstreamMonitor.KEY ->
AlwaysAutoCompleteEditTextPreferenceDialogFragmentCompat().apply {
AlwaysAutoCompleteEditTextPreferenceDialogFragment().apply {
setArguments(preference.key, try {
NetworkInterface.getNetworkInterfaces().asSequence()
.filter {

View File

@@ -1,25 +0,0 @@
package be.mygod.vpnhotspot.preference
import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import androidx.preference.EditTextPreference
import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.widget.AlwaysAutoCompleteEditText
open class AlwaysAutoCompleteEditTextPreference @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = R.attr.editTextPreferenceStyle,
defStyleRes: Int = 0) : EditTextPreference(context, attrs, defStyleAttr, defStyleRes) {
val editText = AlwaysAutoCompleteEditText(context, attrs)
init {
editText.id = android.R.id.edit
SummaryFallbackProvider(this)
}
override fun setText(text: String?) {
val oldText = getText()
super.setText(text)
if (!TextUtils.equals(text, oldText)) notifyChanged()
}
}

View File

@@ -0,0 +1,38 @@
package be.mygod.vpnhotspot.preference
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.core.os.bundleOf
import androidx.preference.EditTextPreferenceDialogFragmentCompat
import be.mygod.vpnhotspot.widget.AlwaysAutoCompleteEditText
class AlwaysAutoCompleteEditTextPreferenceDialogFragment : EditTextPreferenceDialogFragmentCompat() {
companion object {
private const val ARG_SUGGESTIONS = "suggestions"
}
fun setArguments(key: String, suggestions: Array<String>) {
arguments = bundleOf(ARG_KEY to key, ARG_SUGGESTIONS to suggestions)
}
private lateinit var editText: AlwaysAutoCompleteEditText
override fun onCreateDialogView(context: Context) = super.onCreateDialogView(context).apply {
editText = AlwaysAutoCompleteEditText(context).apply { id = android.R.id.edit }
val oldEditText = findViewById<View>(android.R.id.edit)!!
val container = oldEditText.parent as ViewGroup
container.removeView(oldEditText)
container.addView(editText, oldEditText.layoutParams)
}
override fun onBindDialogView(view: View) {
super.onBindDialogView(view)
editText.hint = (preference.summaryProvider as SummaryFallbackProvider).fallback
arguments?.getStringArray(ARG_SUGGESTIONS)?.let { suggestions ->
editText.setAdapter(ArrayAdapter(view.context, android.R.layout.select_dialog_item, suggestions))
}
editText.clearFocus() // having focus is buggy currently
}
}

View File

@@ -1,55 +0,0 @@
package be.mygod.vpnhotspot.preference
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.appcompat.widget.AppCompatAutoCompleteTextView
import androidx.core.os.bundleOf
import androidx.preference.PreferenceDialogFragmentCompat
class AlwaysAutoCompleteEditTextPreferenceDialogFragmentCompat : PreferenceDialogFragmentCompat() {
companion object {
private const val ARG_SUGGESTIONS = "suggestions"
}
fun setArguments(key: String, suggestions: Array<String>) {
arguments = bundleOf(Pair(ARG_KEY, key), Pair(ARG_SUGGESTIONS, suggestions))
}
private lateinit var editText: AppCompatAutoCompleteTextView
private val editTextPreference get() = this.preference as AlwaysAutoCompleteEditTextPreference
override fun onBindDialogView(view: View) {
super.onBindDialogView(view)
editText = editTextPreference.editText
editText.hint = (editTextPreference.summaryProvider as SummaryFallbackProvider).fallback
editText.setText(this.editTextPreference.text)
val text = editText.text
if (text != null) editText.setSelection(text.length, text.length)
val suggestions = arguments?.getStringArray(ARG_SUGGESTIONS)
if (suggestions != null)
editText.setAdapter(ArrayAdapter(view.context, android.R.layout.select_dialog_item, suggestions))
val oldParent = editText.parent as? ViewGroup?
if (oldParent === view) return
oldParent?.removeView(editText)
val oldEditText = view.findViewById<View>(android.R.id.edit) ?: return
val container = oldEditText.parent as? ViewGroup? ?: return
container.removeView(oldEditText)
container.addView(editText, oldEditText.layoutParams)
}
override fun needInputMethod(): Boolean = true
override fun onDialogClosed(positiveResult: Boolean) {
if (positiveResult) {
val value = this.editText.text.toString()
if (this.editTextPreference.callChangeListener(value)) {
this.editTextPreference.text = value
}
}
}
}

View File

@@ -7,12 +7,12 @@
app:summary="@string/settings_service_clean_summary"/>
<PreferenceCategory
app:title="@string/settings_upstream">
<be.mygod.vpnhotspot.preference.AlwaysAutoCompleteEditTextPreference
<EditTextPreference
app:key="service.upstream"
app:icon="@drawable/ic_action_settings_ethernet"
app:title="@string/settings_service_upstream"
app:summary="@string/settings_service_upstream_auto"/>
<be.mygod.vpnhotspot.preference.AlwaysAutoCompleteEditTextPreference
<EditTextPreference
app:key="service.upstream.fallback"
app:icon="@drawable/ic_action_settings_input_component"
app:title="@string/settings_upstream_fallback"