Fix settings not persisted
This commit is contained in:
@@ -15,6 +15,7 @@ import android.widget.Toast
|
||||
class App : Application() {
|
||||
companion object {
|
||||
const val ACTION_CLEAN_ROUTINGS = "be.mygod.vpnhotspot.CLEAN_ROUTINGS"
|
||||
private const val KEY_DNS = "service.dns"
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
lateinit var app: App
|
||||
@@ -27,6 +28,8 @@ class App : Application() {
|
||||
deviceContext = createDeviceProtectedStorageContext()
|
||||
deviceContext.moveSharedPreferencesFrom(this, PreferenceManager.getDefaultSharedPreferencesName(this))
|
||||
} else deviceContext = this
|
||||
// workaround for support lib PreferenceDataStore bug
|
||||
dns = dns
|
||||
ServiceNotification.updateNotificationChannels()
|
||||
}
|
||||
|
||||
@@ -40,5 +43,9 @@ class App : Application() {
|
||||
val pref: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(deviceContext) }
|
||||
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() }
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import android.support.v4.content.ContextCompat
|
||||
import android.support.v4.content.FileProvider
|
||||
import android.support.v4.content.LocalBroadcastManager
|
||||
import android.widget.Toast
|
||||
import be.mygod.vpnhotspot.App.Companion.app
|
||||
import be.mygod.vpnhotspot.net.Routing
|
||||
import com.takisoft.fix.support.v7.preference.PreferenceFragmentCompatDividers
|
||||
import java.io.File
|
||||
@@ -24,6 +25,7 @@ class SettingsPreferenceFragment : PreferenceFragmentCompatDividers() {
|
||||
}
|
||||
|
||||
override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
preferenceManager.preferenceDataStore = SharedPreferenceDataStore(app.pref)
|
||||
addPreferencesFromResource(R.xml.pref_settings)
|
||||
findPreference("service.clean").setOnPreferenceClickListener {
|
||||
if (Routing.clean() == null) {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
@@ -101,8 +101,7 @@ class Routing(val upstream: String?, val downstream: String, ownerAddress: InetA
|
||||
|
||||
fun dnsRedirect(dnses: List<InetAddress>): Routing {
|
||||
val hostAddress = hostAddress.hostAddress
|
||||
val dns = dnses.firstOrNull { it is Inet4Address }?.hostAddress
|
||||
?: app.pref.getString("service.dns", "8.8.8.8")
|
||||
val dns = dnses.firstOrNull { it is Inet4Address }?.hostAddress ?: app.dns
|
||||
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 udp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")
|
||||
|
||||
Reference in New Issue
Block a user