Fix settings not persisted

This commit is contained in:
Mygod
2018-04-21 14:30:00 -07:00
parent 5c3b2363f5
commit 7f81b3ae33
4 changed files with 30 additions and 2 deletions

View File

@@ -15,6 +15,7 @@ import android.widget.Toast
class App : Application() { class App : Application() {
companion object { companion object {
const val ACTION_CLEAN_ROUTINGS = "be.mygod.vpnhotspot.CLEAN_ROUTINGS" const val ACTION_CLEAN_ROUTINGS = "be.mygod.vpnhotspot.CLEAN_ROUTINGS"
private const val KEY_DNS = "service.dns"
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
lateinit var app: App lateinit var app: App
@@ -27,6 +28,8 @@ class App : Application() {
deviceContext = createDeviceProtectedStorageContext() deviceContext = createDeviceProtectedStorageContext()
deviceContext.moveSharedPreferencesFrom(this, PreferenceManager.getDefaultSharedPreferencesName(this)) deviceContext.moveSharedPreferencesFrom(this, PreferenceManager.getDefaultSharedPreferencesName(this))
} else deviceContext = this } else deviceContext = this
// workaround for support lib PreferenceDataStore bug
dns = dns
ServiceNotification.updateNotificationChannels() ServiceNotification.updateNotificationChannels()
} }
@@ -40,5 +43,9 @@ class App : Application() {
val pref: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(deviceContext) } val pref: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(deviceContext) }
val connectivity by lazy { getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager } val connectivity by lazy { getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager }
var dns: String
get() = pref.getString(KEY_DNS, "8.8.8.8")
set(value) = pref.edit().putString(KEY_DNS, value).apply()
fun toast(@StringRes resId: Int) = handler.post { Toast.makeText(this, resId, Toast.LENGTH_SHORT).show() } fun toast(@StringRes resId: Int) = handler.post { Toast.makeText(this, resId, Toast.LENGTH_SHORT).show() }
} }

View File

@@ -10,6 +10,7 @@ import android.support.v4.content.ContextCompat
import android.support.v4.content.FileProvider import android.support.v4.content.FileProvider
import android.support.v4.content.LocalBroadcastManager import android.support.v4.content.LocalBroadcastManager
import android.widget.Toast import android.widget.Toast
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.Routing import be.mygod.vpnhotspot.net.Routing
import com.takisoft.fix.support.v7.preference.PreferenceFragmentCompatDividers import com.takisoft.fix.support.v7.preference.PreferenceFragmentCompatDividers
import java.io.File import java.io.File
@@ -24,6 +25,7 @@ class SettingsPreferenceFragment : PreferenceFragmentCompatDividers() {
} }
override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = SharedPreferenceDataStore(app.pref)
addPreferencesFromResource(R.xml.pref_settings) addPreferencesFromResource(R.xml.pref_settings)
findPreference("service.clean").setOnPreferenceClickListener { findPreference("service.clean").setOnPreferenceClickListener {
if (Routing.clean() == null) { if (Routing.clean() == null) {

View File

@@ -0,0 +1,20 @@
package be.mygod.vpnhotspot
import android.content.SharedPreferences
import android.support.v7.preference.PreferenceDataStore
class SharedPreferenceDataStore(private val pref: SharedPreferences) : PreferenceDataStore() {
override fun getBoolean(key: String?, defValue: Boolean) = pref.getBoolean(key, defValue)
override fun getFloat(key: String?, defValue: Float) = pref.getFloat(key, defValue)
override fun getInt(key: String?, defValue: Int) = pref.getInt(key, defValue)
override fun getLong(key: String?, defValue: Long) = pref.getLong(key, defValue)
override fun getString(key: String?, defValue: String?): String? = pref.getString(key, defValue)
override fun getStringSet(key: String?, defValue: MutableSet<String>?): MutableSet<String>? =
pref.getStringSet(key, defValue)
override fun putBoolean(key: String?, value: Boolean) = pref.edit().putBoolean(key, value).apply()
override fun putFloat(key: String?, value: Float) = pref.edit().putFloat(key, value).apply()
override fun putInt(key: String?, value: Int) = pref.edit().putInt(key, value).apply()
override fun putLong(key: String?, value: Long) = pref.edit().putLong(key, value).apply()
override fun putString(key: String?, value: String?) = pref.edit().putString(key, value).apply()
override fun putStringSet(key: String?, value: MutableSet<String>?) = pref.edit().putStringSet(key, value).apply()
}

View File

@@ -101,8 +101,7 @@ class Routing(val upstream: String?, val downstream: String, ownerAddress: InetA
fun dnsRedirect(dnses: List<InetAddress>): Routing { fun dnsRedirect(dnses: List<InetAddress>): Routing {
val hostAddress = hostAddress.hostAddress val hostAddress = hostAddress.hostAddress
val dns = dnses.firstOrNull { it is Inet4Address }?.hostAddress val dns = dnses.firstOrNull { it is Inet4Address }?.hostAddress ?: app.dns
?: app.pref.getString("service.dns", "8.8.8.8")
debugLog("Routing", "Using $dns from ($dnses)") debugLog("Routing", "Using $dns from ($dnses)")
startScript.add("$IPTABLES -t nat -A PREROUTING -i $downstream -p tcp -d $hostAddress --dport 53 -j DNAT --to-destination $dns") startScript.add("$IPTABLES -t nat -A PREROUTING -i $downstream -p tcp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")
startScript.add("$IPTABLES -t nat -A PREROUTING -i $downstream -p udp -d $hostAddress --dport 53 -j DNAT --to-destination $dns") startScript.add("$IPTABLES -t nat -A PREROUTING -i $downstream -p udp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")