From 509511461c76fc84c8e57d1c3d4b0853058221cc Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 31 Jan 2019 17:10:26 +0800 Subject: [PATCH] Add manual MAC lookup --- mobile/src/main/java/be/mygod/vpnhotspot/App.kt | 9 +++++++-- .../mygod/vpnhotspot/client/ClientsFragment.kt | 12 ++++++++---- .../java/be/mygod/vpnhotspot/client/MacLookup.kt | 16 +++++++++++++--- mobile/src/main/res/values-zh-rCN/strings.xml | 2 ++ mobile/src/main/res/values/strings.xml | 2 ++ 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/App.kt b/mobile/src/main/java/be/mygod/vpnhotspot/App.kt index efe4e3e6..6edb99c2 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/App.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/App.kt @@ -3,7 +3,6 @@ package be.mygod.vpnhotspot import android.annotation.SuppressLint import android.app.Application import android.app.UiModeManager -import android.content.SharedPreferences import android.content.res.Configuration import android.net.ConnectivityManager import android.net.wifi.WifiManager @@ -16,6 +15,7 @@ import be.mygod.vpnhotspot.room.AppDatabase import be.mygod.vpnhotspot.util.DeviceStorageApp import be.mygod.vpnhotspot.util.Event0 import be.mygod.vpnhotspot.util.RootSession +import java.util.* class App : Application() { companion object { @@ -48,7 +48,12 @@ class App : Application() { } lateinit var deviceStorage: Application - val pref: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(deviceStorage) } + val english by lazy { + createConfigurationContext(Configuration(resources.configuration).apply { + setLocale(Locale.ENGLISH) + }) + } + val pref by lazy { PreferenceManager.getDefaultSharedPreferences(deviceStorage) } val connectivity by lazy { getSystemService()!! } val uiMode by lazy { getSystemService()!! } val wifi by lazy { getSystemService()!! } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt index 0a23e72c..58005512 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientsFragment.kt @@ -55,6 +55,7 @@ class ClientsFragment : Fragment(), MainScope by MainScope.Supervisor() { setTitle(getString(R.string.clients_nickname_title, arg.mac.macToString())) setPositiveButton(android.R.string.ok, listener) setNegativeButton(android.R.string.cancel, null) + setNeutralButton(R.string.clients_nickname_set_to_vendor, listener) } override fun onCreateDialog(savedInstanceState: Bundle?) = super.onCreateDialog(savedInstanceState).apply { @@ -63,11 +64,14 @@ class ClientsFragment : Fragment(), MainScope by MainScope.Supervisor() { } override fun onClick(dialog: DialogInterface?, which: Int) { - GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED) { - MacLookup.abort(arg.mac) - AppDatabase.instance.clientRecordDao.upsert(arg.mac) { - nickname = this@NicknameDialogFragment.dialog!!.findViewById(android.R.id.edit).text + when (which) { + DialogInterface.BUTTON_POSITIVE -> GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED) { + MacLookup.abort(arg.mac) + AppDatabase.instance.clientRecordDao.upsert(arg.mac) { + nickname = this@NicknameDialogFragment.dialog!!.findViewById(android.R.id.edit).text + } } + DialogInterface.BUTTON_NEUTRAL -> MacLookup.perform(arg.mac, true) } } } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt b/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt index 57b17f2c..1774dbeb 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt @@ -1,8 +1,12 @@ package be.mygod.vpnhotspot.client +import android.content.Context import androidx.annotation.MainThread +import be.mygod.vpnhotspot.App.Companion.app +import be.mygod.vpnhotspot.R import be.mygod.vpnhotspot.room.AppDatabase import be.mygod.vpnhotspot.room.macToString +import be.mygod.vpnhotspot.widget.SmartSnackbar import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job @@ -18,8 +22,12 @@ import java.net.URL * This class generates a default nickname for new clients. */ object MacLookup { - class UnexpectedError(mac: Long, val error: String) : - JSONException("Server returned error for ${mac.macToString()}: $error") + class UnexpectedError(val mac: Long, val error: String) : JSONException("") { + private fun formatMessage(context: Context) = + context.getString(R.string.clients_mac_lookup_unexpected_error, mac.macToString(), error) + override val message get() = formatMessage(app.english) + override fun getLocalizedMessage() = formatMessage(app) + } private val macLookupBusy = mutableMapOf>() private val countryCodeRegex = "[A-Z]{2}".toRegex() // http://en.wikipedia.org/wiki/ISO_3166-1 @@ -31,7 +39,7 @@ object MacLookup { } @MainThread - fun perform(mac: Long) { + fun perform(mac: Long, explicit: Boolean = false) { abort(mac) val conn = URL("https://macvendors.co/api/" + mac.macToString()).openConnection() as HttpURLConnection macLookupBusy[mac] = conn to GlobalScope.launch(Dispatchers.IO) { @@ -50,11 +58,13 @@ object MacLookup { } } catch (e: IOException) { Timber.d(e) + if (explicit) SmartSnackbar.make(e).show() } catch (e: JSONException) { if ((e as? UnexpectedError)?.error == "no result") { // no vendor found, we should not retry in the future AppDatabase.instance.clientRecordDao.upsert(mac) { macLookupPending = false } } else Timber.w(e) + if (explicit) SmartSnackbar.make(e).show() } } } diff --git a/mobile/src/main/res/values-zh-rCN/strings.xml b/mobile/src/main/res/values-zh-rCN/strings.xml index d989ee5a..2fc7fda3 100644 --- a/mobile/src/main/res/values-zh-rCN/strings.xml +++ b/mobile/src/main/res/values-zh-rCN/strings.xml @@ -63,7 +63,9 @@ 拉黑需要为该接口打开服务。 洗白 流量… + 服务器为 %1$s 返回错误:%2$s %s 的昵称 + ← 🏳️‍🌈 厂商 %s 的流量 自 %2$s 以来连接了 %1$s 次 diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml index 5293a179..da14f9c1 100644 --- a/mobile/src/main/res/values/strings.xml +++ b/mobile/src/main/res/values/strings.xml @@ -67,7 +67,9 @@ Turn on service for this interface to block the client. Unblock Stats… + Server returned error for %1$s: %2$s Nickname for %s + ← 🏳️‍🌈 Vendor Stats for %s Connected 1 time since %2$s