From 7e30ddf26d4a041539977d2fd79a064c93daed40 Mon Sep 17 00:00:00 2001 From: Mygod Date: Fri, 5 Jan 2018 00:53:55 +0800 Subject: [PATCH] Refine finding default Wi-Fi interface --- mobile/src/main/java/be/mygod/vpnhotspot/App.kt | 17 ++++++++++++++++- .../be/mygod/vpnhotspot/SettingsFragment.kt | 8 +++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/App.kt b/mobile/src/main/java/be/mygod/vpnhotspot/App.kt index 6facaf52..2dbc1529 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/App.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/App.kt @@ -3,8 +3,11 @@ package be.mygod.vpnhotspot import android.app.Application import android.app.NotificationChannel import android.app.NotificationManager +import android.content.SharedPreferences +import android.content.res.Resources import android.os.Build import android.preference.PreferenceManager +import java.net.NetworkInterface class App : Application() { companion object { @@ -17,7 +20,19 @@ class App : Application() { if (Build.VERSION.SDK_INT >= 26) getSystemService(NotificationManager::class.java) .createNotificationChannel(NotificationChannel(HotspotService.CHANNEL, "Hotspot Service", NotificationManager.IMPORTANCE_LOW)) + pref = PreferenceManager.getDefaultSharedPreferences(this) + val wifiRegexes = resources.getStringArray(Resources.getSystem() + .getIdentifier("config_tether_wifi_regexs", "array", "android")) + .map { it.toPattern() } + wifiInterfaces = NetworkInterface.getNetworkInterfaces().asSequence() + .map { it.name } + .filter { ifname -> wifiRegexes.any { it.matcher(ifname).matches() } } + .sorted().toList().toTypedArray() + val wifiInterface = wifiInterfaces.singleOrNull() + if (wifiInterface != null && pref.getString(HotspotService.KEY_WIFI, null) == null) + pref.edit().putString(HotspotService.KEY_WIFI, wifiInterface).apply() } - val pref by lazy { PreferenceManager.getDefaultSharedPreferences(this) } + lateinit var pref: SharedPreferences + lateinit var wifiInterfaces: Array } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/SettingsFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/SettingsFragment.kt index d32b5899..cd5fa1ea 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/SettingsFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/SettingsFragment.kt @@ -11,6 +11,7 @@ import android.support.customtabs.CustomTabsIntent import android.support.v4.content.ContextCompat import android.support.v4.content.LocalBroadcastManager import android.support.v7.preference.Preference +import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.preference.AlwaysAutoCompleteEditTextPreferenceDialogFragmentCompat import com.takisoft.fix.support.v7.preference.PreferenceFragmentCompatDividers import java.net.NetworkInterface @@ -56,11 +57,8 @@ class SettingsFragment : PreferenceFragmentCompatDividers(), ServiceConnection { .filter { it.isUp && !it.isLoopback && it.interfaceAddresses.isNotEmpty() } .map { it.name }.sorted().toList().toTypedArray())) HotspotService.KEY_WIFI -> displayPreferenceDialog( - AlwaysAutoCompleteEditTextPreferenceDialogFragmentCompat(), HotspotService.KEY_WIFI, - Bundle().put(AlwaysAutoCompleteEditTextPreferenceDialogFragmentCompat.KEY_SUGGESTIONS, - NetworkInterface.getNetworkInterfaces().asSequence() - .filter { !it.isLoopback } // wlan0 is down in airplane mode - .map { it.name }.sorted().toList().toTypedArray())) + AlwaysAutoCompleteEditTextPreferenceDialogFragmentCompat(), HotspotService.KEY_WIFI, Bundle() + .put(AlwaysAutoCompleteEditTextPreferenceDialogFragmentCompat.KEY_SUGGESTIONS, app.wifiInterfaces)) else -> super.onDisplayPreferenceDialog(preference) }