Prevent recreating monitor MenuItem

This commit is contained in:
Mygod
2019-04-03 16:32:09 +08:00
parent 10a2b777f2
commit f97afdf8ae
3 changed files with 22 additions and 17 deletions

View File

@@ -25,6 +25,7 @@ import be.mygod.vpnhotspot.net.TetheringManager.localOnlyTetheredIfaces
import be.mygod.vpnhotspot.net.TetheringManager.tetheredIfaces import be.mygod.vpnhotspot.net.TetheringManager.tetheredIfaces
import be.mygod.vpnhotspot.util.ServiceForegroundConnector import be.mygod.vpnhotspot.util.ServiceForegroundConnector
import be.mygod.vpnhotspot.util.broadcastReceiver import be.mygod.vpnhotspot.util.broadcastReceiver
import be.mygod.vpnhotspot.util.isNotGone
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import timber.log.Timber import timber.log.Timber
import java.net.NetworkInterface import java.net.NetworkInterface
@@ -95,20 +96,13 @@ class TetheringFragment : Fragment(), ServiceConnection, MenuItem.OnMenuItemClic
} }
private fun updateMonitorList(canMonitor: List<String> = emptyList()) { private fun updateMonitorList(canMonitor: List<String> = emptyList()) {
val toolbar = requireActivity().toolbar val item = requireActivity().toolbar.menu.findItem(R.id.monitor) ?: return // assuming no longer foreground
val menu = toolbar.menu item.isNotGone = canMonitor.isNotEmpty()
if (canMonitor.isEmpty()) menu.removeItem(R.id.monitor) else {
var item = menu.findItem(R.id.monitor)
if (item == null) {
toolbar.inflateMenu(R.menu.toolbar_monitor)
item = menu.findItem(R.id.monitor)!!
}
item.subMenu.apply { item.subMenu.apply {
clear() clear()
canMonitor.sorted().forEach { add(it).setOnMenuItemClickListener(this@TetheringFragment) } canMonitor.sorted().forEach { add(it).setOnMenuItemClickListener(this@TetheringFragment) }
} }
} }
}
override fun onMenuItemClick(item: MenuItem?): Boolean { override fun onMenuItemClick(item: MenuItem?): Boolean {
ContextCompat.startForegroundService(requireContext(), Intent(context, TetheringService::class.java) ContextCompat.startForegroundService(requireContext(), Intent(context, TetheringService::class.java)
.putExtra(TetheringService.EXTRA_ADD_INTERFACE_MONITOR, item?.title ?: return false)) .putExtra(TetheringService.EXTRA_ADD_INTERFACE_MONITOR, item?.title ?: return false))
@@ -123,9 +117,15 @@ class TetheringFragment : Fragment(), ServiceConnection, MenuItem.OnMenuItemClic
binding.interfaces.adapter = adapter binding.interfaces.adapter = adapter
adapter.update(emptyList(), emptyList(), emptyList()) adapter.update(emptyList(), emptyList(), emptyList())
ServiceForegroundConnector(this, this, TetheringService::class) ServiceForegroundConnector(this, this, TetheringService::class)
requireActivity().toolbar.inflateMenu(R.menu.toolbar_tethering)
return binding.root return binding.root
} }
override fun onDestroyView() {
super.onDestroyView()
requireActivity().toolbar.menu.clear()
}
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (Build.VERSION.SDK_INT >= 27) ManageBar.Data.notifyChange() if (Build.VERSION.SDK_INT >= 27) ManageBar.Data.notifyChange()
@@ -163,9 +163,4 @@ class TetheringFragment : Fragment(), ServiceConnection, MenuItem.OnMenuItemClic
binder = null binder = null
requireContext().unregisterReceiver(receiver) requireContext().unregisterReceiver(receiver)
} }
override fun onDestroy() {
updateMonitorList()
super.onDestroy()
}
} }

View File

@@ -5,6 +5,7 @@ import android.os.Build
import android.text.Spannable import android.text.Spannable
import android.text.SpannableString import android.text.SpannableString
import android.text.SpannableStringBuilder import android.text.SpannableStringBuilder
import android.view.MenuItem
import android.view.View import android.view.View
import android.widget.ImageView import android.widget.ImageView
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
@@ -91,6 +92,13 @@ fun Context.stopAndUnbind(connection: ServiceConnection) {
unbindService(connection) unbindService(connection)
} }
var MenuItem.isNotGone: Boolean
get() = isVisible || isEnabled
set(value) {
isVisible = value
isEnabled = value
}
fun <K, V> MutableMap<K, V>.computeIfAbsentCompat(key: K, value: () -> V) = if (Build.VERSION.SDK_INT >= 24) fun <K, V> MutableMap<K, V>.computeIfAbsentCompat(key: K, value: () -> V) = if (Build.VERSION.SDK_INT >= 24)
computeIfAbsent(key) { value() } else this[key] ?: value().also { put(key, it) } computeIfAbsent(key) { value() } else this[key] ?: value().also { put(key, it) }
fun <K, V> MutableMap<K, V>.putIfAbsentCompat(key: K, value: V) = if (Build.VERSION.SDK_INT >= 24) fun <K, V> MutableMap<K, V>.putIfAbsentCompat(key: K, value: V) = if (Build.VERSION.SDK_INT >= 24)

View File

@@ -4,6 +4,8 @@
<item android:id="@+id/monitor" <item android:id="@+id/monitor"
android:icon="@drawable/ic_image_remove_red_eye" android:icon="@drawable/ic_image_remove_red_eye"
android:title="@string/tethering_monitor" android:title="@string/tethering_monitor"
android:enabled="false"
android:visible="false"
app:showAsAction="always"> app:showAsAction="always">
<menu/> <menu/>
</item> </item>