Add ServiceForegroundConnector
This commit is contained in:
@@ -31,6 +31,7 @@ import be.mygod.vpnhotspot.net.TetherType
|
|||||||
import be.mygod.vpnhotspot.net.TetheringManager
|
import be.mygod.vpnhotspot.net.TetheringManager
|
||||||
import be.mygod.vpnhotspot.net.wifi.P2pSupplicantConfiguration
|
import be.mygod.vpnhotspot.net.wifi.P2pSupplicantConfiguration
|
||||||
import be.mygod.vpnhotspot.net.wifi.WifiP2pDialog
|
import be.mygod.vpnhotspot.net.wifi.WifiP2pDialog
|
||||||
|
import be.mygod.vpnhotspot.util.ServiceForegroundConnector
|
||||||
import be.mygod.vpnhotspot.util.broadcastReceiver
|
import be.mygod.vpnhotspot.util.broadcastReceiver
|
||||||
import be.mygod.vpnhotspot.util.formatAddresses
|
import be.mygod.vpnhotspot.util.formatAddresses
|
||||||
import java.net.NetworkInterface
|
import java.net.NetworkInterface
|
||||||
@@ -177,23 +178,20 @@ class RepeaterFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClickL
|
|||||||
}
|
}
|
||||||
binding.toolbar.inflateMenu(R.menu.repeater)
|
binding.toolbar.inflateMenu(R.menu.repeater)
|
||||||
binding.toolbar.setOnMenuItemClickListener(this)
|
binding.toolbar.setOnMenuItemClickListener(this)
|
||||||
|
ServiceForegroundConnector(this, RepeaterService::class)
|
||||||
return binding.root
|
return binding.root
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStart() {
|
override fun onStart() {
|
||||||
super.onStart()
|
super.onStart()
|
||||||
val context = requireContext()
|
|
||||||
context.bindService(Intent(context, RepeaterService::class.java), this, Context.BIND_AUTO_CREATE)
|
|
||||||
IpNeighbourMonitor.registerCallback(this)
|
IpNeighbourMonitor.registerCallback(this)
|
||||||
context.registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
|
requireContext().registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStop() {
|
override fun onStop() {
|
||||||
val context = requireContext()
|
requireContext().unregisterReceiver(receiver)
|
||||||
context.unregisterReceiver(receiver)
|
|
||||||
IpNeighbourMonitor.unregisterCallback(this)
|
IpNeighbourMonitor.unregisterCallback(this)
|
||||||
onServiceDisconnected(null)
|
onServiceDisconnected(null)
|
||||||
context.unbindService(this)
|
|
||||||
super.onStop()
|
super.onStop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import be.mygod.vpnhotspot.databinding.ListitemManageTetherBinding
|
|||||||
import be.mygod.vpnhotspot.net.TetherType
|
import be.mygod.vpnhotspot.net.TetherType
|
||||||
import be.mygod.vpnhotspot.net.TetheringManager
|
import be.mygod.vpnhotspot.net.TetheringManager
|
||||||
import be.mygod.vpnhotspot.net.wifi.WifiApManager
|
import be.mygod.vpnhotspot.net.wifi.WifiApManager
|
||||||
|
import be.mygod.vpnhotspot.util.ServiceForegroundConnector
|
||||||
import be.mygod.vpnhotspot.util.broadcastReceiver
|
import be.mygod.vpnhotspot.util.broadcastReceiver
|
||||||
import be.mygod.vpnhotspot.util.formatAddresses
|
import be.mygod.vpnhotspot.util.formatAddresses
|
||||||
import java.lang.reflect.InvocationTargetException
|
import java.lang.reflect.InvocationTargetException
|
||||||
@@ -333,18 +334,11 @@ class TetheringFragment : Fragment(), ServiceConnection {
|
|||||||
binding.interfaces.itemAnimator = DefaultItemAnimator()
|
binding.interfaces.itemAnimator = DefaultItemAnimator()
|
||||||
binding.interfaces.adapter = adapter
|
binding.interfaces.adapter = adapter
|
||||||
BluetoothAdapter.getDefaultAdapter()?.getProfileProxy(requireContext(), tetherListener, PAN)
|
BluetoothAdapter.getDefaultAdapter()?.getProfileProxy(requireContext(), tetherListener, PAN)
|
||||||
|
ServiceForegroundConnector(this, if (Build.VERSION.SDK_INT >= 26)
|
||||||
|
listOf(TetheringService::class, LocalOnlyHotspotService::class) else listOf(TetheringService::class))
|
||||||
return binding.root
|
return binding.root
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStart() {
|
|
||||||
super.onStart()
|
|
||||||
val context = requireContext()
|
|
||||||
context.bindService(Intent(context, TetheringService::class.java), this, Context.BIND_AUTO_CREATE)
|
|
||||||
if (Build.VERSION.SDK_INT >= 26) {
|
|
||||||
context.bindService(Intent(context, LocalOnlyHotspotService::class.java), this, Context.BIND_AUTO_CREATE)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
|
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
|
||||||
if (requestCode == START_LOCAL_ONLY_HOTSPOT) @TargetApi(26) {
|
if (requestCode == START_LOCAL_ONLY_HOTSPOT) @TargetApi(26) {
|
||||||
if (grantResults.firstOrNull() == PackageManager.PERMISSION_GRANTED) {
|
if (grantResults.firstOrNull() == PackageManager.PERMISSION_GRANTED) {
|
||||||
@@ -354,11 +348,6 @@ class TetheringFragment : Fragment(), ServiceConnection {
|
|||||||
} else super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
} else super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStop() {
|
|
||||||
requireContext().unbindService(this)
|
|
||||||
super.onStop()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
tetherListener.pan = null
|
tetherListener.pan = null
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package be.mygod.vpnhotspot.util
|
||||||
|
|
||||||
|
import android.app.Service
|
||||||
|
import android.arch.lifecycle.Lifecycle
|
||||||
|
import android.arch.lifecycle.LifecycleObserver
|
||||||
|
import android.arch.lifecycle.LifecycleOwner
|
||||||
|
import android.arch.lifecycle.OnLifecycleEvent
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.ServiceConnection
|
||||||
|
import android.support.v4.app.Fragment
|
||||||
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
|
/**
|
||||||
|
* host also needs to be Context/Fragment and LifecycleOwner.
|
||||||
|
*/
|
||||||
|
class ServiceForegroundConnector(private val host: ServiceConnection, private val classes: List<KClass<out Service>>) :
|
||||||
|
LifecycleObserver {
|
||||||
|
init {
|
||||||
|
(host as LifecycleOwner).lifecycle.addObserver(this)
|
||||||
|
}
|
||||||
|
constructor(host: ServiceConnection, vararg classes: KClass<out Service>) : this(host, classes.toList())
|
||||||
|
|
||||||
|
private val context get() = if (host is Context) host else (host as Fragment).requireContext()
|
||||||
|
|
||||||
|
@OnLifecycleEvent(Lifecycle.Event.ON_START)
|
||||||
|
fun onStart() {
|
||||||
|
val context = context
|
||||||
|
for (clazz in classes) context.bindService(Intent(context, clazz.java), host, Context.BIND_AUTO_CREATE)
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
|
||||||
|
fun onStop() = context.unbindService(host)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user