Prefer SnackBars

This commit is contained in:
Mygod
2018-07-14 23:30:05 +08:00
parent 0142037fa3
commit 89cbd96280
6 changed files with 35 additions and 33 deletions

View File

@@ -10,6 +10,7 @@ import android.os.IBinder
import android.support.customtabs.CustomTabsIntent
import android.support.design.internal.BottomNavigationMenuView
import android.support.design.widget.BottomNavigationView
import android.support.design.widget.Snackbar
import android.support.v4.app.Fragment
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
@@ -32,6 +33,7 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
.build()
}
fun launchUrl(url: Uri) = customTabsIntent.launchUrl(this, url)
fun snackbar(text: CharSequence = "") = Snackbar.make(binding.fragmentHolder, text, Snackbar.LENGTH_LONG)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

View File

@@ -7,7 +7,6 @@ import android.os.Build
import android.os.Bundle
import android.support.v4.content.FileProvider
import android.support.v7.preference.Preference
import android.widget.Toast
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.Routing
import be.mygod.vpnhotspot.net.UpstreamMonitor
@@ -27,10 +26,10 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = SharedPreferenceDataStore(app.pref)
addPreferencesFromResource(R.xml.pref_settings)
val mainActivity = activity as MainActivity
findPreference("service.clean").setOnPreferenceClickListener {
if (Routing.clean() == null) {
Toast.makeText(requireContext(), R.string.root_unavailable, Toast.LENGTH_SHORT).show()
} else app.cleanRoutings()
if (Routing.clean() == null) mainActivity.snackbar().setText(R.string.root_unavailable).show()
else app.cleanRoutings()
true
}
findPreference("misc.logcat").setOnPreferenceClickListener {
@@ -90,7 +89,7 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
true
}
findPreference("misc.source").setOnPreferenceClickListener {
(activity as MainActivity).launchUrl(Uri.parse("https://github.com/Mygod/VPNHotspot"))
mainActivity.launchUrl(Uri.parse("https://github.com/Mygod/VPNHotspot"))
true
}
findPreference("misc.donate").setOnPreferenceClickListener {

View File

@@ -16,11 +16,8 @@ import android.support.v7.widget.RecyclerView
import android.view.WindowManager
import android.widget.EditText
import android.widget.Toast
import be.mygod.vpnhotspot.App
import be.mygod.vpnhotspot.*
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.BR
import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.RepeaterService
import be.mygod.vpnhotspot.databinding.ListitemRepeaterBinding
import be.mygod.vpnhotspot.net.wifi.P2pSupplicantConfiguration
import be.mygod.vpnhotspot.net.wifi.WifiP2pDialog
@@ -104,21 +101,23 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
val binder = binder
val group = binder?.service?.group
val ssid = group?.networkName
val context = parent.requireContext()
val mainActivity = parent.activity as MainActivity
if (ssid != null) {
val wifi = WifiConfiguration()
val conf = P2pSupplicantConfiguration()
wifi.SSID = ssid
wifi.preSharedKey = group.passphrase
if (wifi.preSharedKey == null) wifi.preSharedKey = conf.readPsk()
if (wifi.preSharedKey == null) {
wifi.preSharedKey = conf.readPsk { mainActivity.snackbar(it.message.toString()).show() }
}
if (wifi.preSharedKey != null) {
var dialog: WifiP2pDialog? = null
dialog = WifiP2pDialog(context, DialogInterface.OnClickListener { _, which ->
dialog = WifiP2pDialog(mainActivity, DialogInterface.OnClickListener { _, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> when (conf.update(dialog!!.config!!)) {
true -> App.app.handler.postDelayed(binder::requestGroupUpdate, 1000)
false -> Toast.makeText(context, R.string.noisy_su_failure, Toast.LENGTH_SHORT).show()
null -> Toast.makeText(context, R.string.root_unavailable, Toast.LENGTH_SHORT).show()
true -> app.handler.postDelayed(binder::requestGroupUpdate, 1000)
false -> mainActivity.snackbar().setText(R.string.noisy_su_failure).show()
null -> mainActivity.snackbar().setText(R.string.root_unavailable).show()
}
DialogInterface.BUTTON_NEUTRAL -> binder.resetCredentials()
}
@@ -127,7 +126,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
return
}
}
Toast.makeText(context, R.string.repeater_configure_failure, Toast.LENGTH_LONG).show()
mainActivity.snackbar().setText(R.string.repeater_configure_failure).show()
}
}

View File

@@ -14,8 +14,8 @@ import android.provider.Settings
import android.support.annotation.RequiresApi
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.Toast
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.MainActivity
import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.databinding.ListitemInterfaceBinding
import be.mygod.vpnhotspot.net.TetherType
@@ -43,10 +43,10 @@ abstract class TetherManager private constructor(protected val parent: Tethering
override fun onClick(v: View?) {
val manager = manager!!
val context = manager.parent.requireContext()
if (Build.VERSION.SDK_INT >= 23 && !Settings.System.canWrite(context)) try {
val mainActivity = manager.parent.activity as MainActivity
if (Build.VERSION.SDK_INT >= 23 && !Settings.System.canWrite(mainActivity)) try {
manager.parent.startActivity(Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
Uri.parse("package:${context.packageName}")))
Uri.parse("package:${mainActivity.packageName}")))
return
} catch (exc: ActivityNotFoundException) {
exc.printStackTrace()
@@ -62,7 +62,7 @@ abstract class TetherManager private constructor(protected val parent: Tethering
while (cause != null) {
cause = cause.cause
if (cause != null && cause !is InvocationTargetException) {
Toast.makeText(context, cause.message, Toast.LENGTH_LONG).show()
mainActivity.snackbar(cause.message.toString()).show()
ManageBar.start(itemView.context)
break
}
@@ -90,11 +90,8 @@ abstract class TetherManager private constructor(protected val parent: Tethering
protected abstract fun stop()
override fun onTetheringStarted() = data.notifyChange()
override fun onTetheringFailed() {
app.handler.post {
Toast.makeText(parent.requireContext(), R.string.tethering_manage_failed, Toast.LENGTH_SHORT).show()
}
}
override fun onTetheringFailed() =
(parent.activity as MainActivity).snackbar().setText(R.string.tethering_manage_failed).show()
override fun bindTo(viewHolder: RecyclerView.ViewHolder) {
(viewHolder as ViewHolder).manager = this

View File

@@ -3,7 +3,6 @@ package be.mygod.vpnhotspot.net.wifi
import android.net.wifi.WifiConfiguration
import android.os.Build
import android.util.Log
import android.widget.Toast
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.util.loggerSu
import be.mygod.vpnhotspot.util.noisySu
@@ -28,7 +27,7 @@ class P2pSupplicantConfiguration {
private val content by lazy { loggerSu("cat /data/misc/wifi/p2p_supplicant.conf") }
fun readPsk(): String? {
fun readPsk(handler: ((RuntimeException) -> Unit)? = null): String? {
return try {
val match = pskParser.findAll(content ?: return null).single()
if (match.groups[2] == null && match.groups[3] == null) "" else {
@@ -37,13 +36,13 @@ class P2pSupplicantConfiguration {
result
}
} catch (e: NoSuchElementException) {
Toast.makeText(app, e.message, Toast.LENGTH_LONG).show()
handler?.invoke(e)
null
} catch (e: RuntimeException) {
Crashlytics.log(Log.WARN, TAG, content)
e.printStackTrace()
Crashlytics.logException(e)
Toast.makeText(app, e.message, Toast.LENGTH_LONG).show()
handler?.invoke(e)
null
}
}

View File

@@ -16,14 +16,20 @@
app:title="@string/app_name"
android:id="@+id/toolbar"/>
<FrameLayout
android:id="@+id/fragmentHolder"
<android.support.design.widget.CoordinatorLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"/>
app:layout_constraintTop_toBottomOf="@+id/toolbar">
<FrameLayout
android:id="@+id/fragmentHolder"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"