Use RequestPermission contract
This commit is contained in:
@@ -2,11 +2,7 @@ package be.mygod.vpnhotspot.manage
|
||||
|
||||
import android.Manifest
|
||||
import android.annotation.TargetApi
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.ComponentName
|
||||
import android.content.Intent
|
||||
import android.content.ServiceConnection
|
||||
import android.content.pm.PackageManager
|
||||
import android.content.*
|
||||
import android.location.LocationManager
|
||||
import android.os.Build
|
||||
import android.os.IBinder
|
||||
@@ -41,32 +37,7 @@ class LocalOnlyHotspotManager(private val parent: TetheringFragment) : Manager()
|
||||
|
||||
override fun onClick(view: View) {
|
||||
val binder = manager.binder
|
||||
if (binder?.iface != null) binder.stop() else {
|
||||
val context = manager.parent.requireContext()
|
||||
if (context.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
|
||||
manager.parent.requestPermissions(arrayOf(permission), TetheringFragment.START_LOCAL_ONLY_HOTSPOT)
|
||||
return
|
||||
}
|
||||
/**
|
||||
* LOH also requires location to be turned on. Source:
|
||||
* https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/53e0284/service/java/com/android/server/wifi/WifiServiceImpl.java#1204
|
||||
* https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/53e0284/service/java/com/android/server/wifi/WifiSettingsStore.java#228
|
||||
*/
|
||||
if (if (Build.VERSION.SDK_INT < 28) @Suppress("DEPRECATION") {
|
||||
Settings.Secure.getInt(context.contentResolver, Settings.Secure.LOCATION_MODE,
|
||||
Settings.Secure.LOCATION_MODE_OFF) == Settings.Secure.LOCATION_MODE_OFF
|
||||
} else context.getSystemService<LocationManager>()?.isLocationEnabled != true) {
|
||||
try {
|
||||
context.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
|
||||
Toast.makeText(context, R.string.tethering_temp_hotspot_location, Toast.LENGTH_LONG).show()
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
app.logEvent("location_settings") { param("message", e.toString()) }
|
||||
SmartSnackbar.make(R.string.tethering_temp_hotspot_location).show()
|
||||
}
|
||||
return
|
||||
}
|
||||
context.startForegroundService(Intent(context, LocalOnlyHotspotService::class.java))
|
||||
}
|
||||
if (binder?.iface == null) manager.parent.startLocalOnlyHotspot.launch(permission) else binder.stop()
|
||||
}
|
||||
}
|
||||
private inner class Data : be.mygod.vpnhotspot.manage.Data() {
|
||||
@@ -85,6 +56,24 @@ class LocalOnlyHotspotManager(private val parent: TetheringFragment) : Manager()
|
||||
ServiceForegroundConnector(parent, this, LocalOnlyHotspotService::class)
|
||||
}
|
||||
|
||||
/**
|
||||
* LOH also requires location to be turned on. Source:
|
||||
* https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/53e0284/service/java/com/android/server/wifi/WifiServiceImpl.java#1204
|
||||
* https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/53e0284/service/java/com/android/server/wifi/WifiSettingsStore.java#228
|
||||
*/
|
||||
fun start(context: Context) {
|
||||
if (if (Build.VERSION.SDK_INT < 28) @Suppress("DEPRECATION") {
|
||||
Settings.Secure.getInt(context.contentResolver, Settings.Secure.LOCATION_MODE,
|
||||
Settings.Secure.LOCATION_MODE_OFF) == Settings.Secure.LOCATION_MODE_OFF
|
||||
} else context.getSystemService<LocationManager>()?.isLocationEnabled != true) try {
|
||||
context.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
|
||||
Toast.makeText(context, R.string.tethering_temp_hotspot_location, Toast.LENGTH_LONG).show()
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
app.logEvent("location_settings") { param("message", e.toString()) }
|
||||
SmartSnackbar.make(R.string.tethering_temp_hotspot_location).show()
|
||||
} else context.startForegroundService(Intent(context, LocalOnlyHotspotService::class.java))
|
||||
}
|
||||
|
||||
override val type get() = VIEW_TYPE_LOCAL_ONLY_HOTSPOT
|
||||
private val data = Data()
|
||||
internal var binder: LocalOnlyHotspotService.Binder? = null
|
||||
|
||||
@@ -5,7 +5,6 @@ import android.content.ComponentName
|
||||
import android.content.DialogInterface
|
||||
import android.content.Intent
|
||||
import android.content.ServiceConnection
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.wifi.WifiConfiguration
|
||||
import android.net.wifi.p2p.WifiP2pConfig
|
||||
import android.net.wifi.p2p.WifiP2pGroup
|
||||
@@ -83,16 +82,9 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
|
||||
fun toggle() {
|
||||
val binder = binder
|
||||
when (binder?.service?.status) {
|
||||
RepeaterService.Status.IDLE -> {
|
||||
val context = parent.requireContext()
|
||||
if (Build.VERSION.SDK_INT >= 29 && context.checkSelfPermission(
|
||||
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||
parent.requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
|
||||
TetheringFragment.START_REPEATER)
|
||||
return
|
||||
}
|
||||
RepeaterService.Status.IDLE -> if (Build.VERSION.SDK_INT < 29) parent.requireContext().let { context ->
|
||||
ContextCompat.startForegroundService(context, Intent(context, RepeaterService::class.java))
|
||||
}
|
||||
} else parent.startRepeater.launch(Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
RepeaterService.Status.ACTIVE -> binder.shutdown()
|
||||
else -> { }
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package be.mygod.vpnhotspot.manage
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.content.*
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.os.IBinder
|
||||
@@ -10,6 +9,7 @@ import android.view.LayoutInflater
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.appcompat.widget.Toolbar
|
||||
import androidx.core.content.ContextCompat
|
||||
@@ -43,8 +43,6 @@ import java.net.SocketException
|
||||
|
||||
class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClickListener {
|
||||
companion object {
|
||||
const val START_REPEATER = 4
|
||||
const val START_LOCAL_ONLY_HOTSPOT = 1
|
||||
const val REPEATER_WPS = 3
|
||||
const val CONFIGURE_REPEATER = 2
|
||||
const val CONFIGURE_AP = 4
|
||||
@@ -128,6 +126,15 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick
|
||||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) = getItem(position).bindTo(holder)
|
||||
}
|
||||
|
||||
@RequiresApi(29)
|
||||
val startRepeater = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
|
||||
if (granted) requireContext().apply { startForegroundService(Intent(this, RepeaterService::class.java)) }
|
||||
}
|
||||
@RequiresApi(26)
|
||||
val startLocalOnlyHotspot = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
|
||||
if (granted) adapter.localOnlyHotspotManager.start(requireContext())
|
||||
}
|
||||
|
||||
var ifaceLookup: Map<String, NetworkInterface> = emptyMap()
|
||||
var enabledTypes = emptySet<TetherType>()
|
||||
private lateinit var binding: FragmentTetheringBinding
|
||||
@@ -238,22 +245,6 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick
|
||||
}
|
||||
}
|
||||
|
||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
|
||||
when (requestCode) {
|
||||
START_REPEATER -> if (grantResults.firstOrNull() == PackageManager.PERMISSION_GRANTED) @TargetApi(29) {
|
||||
val context = requireContext()
|
||||
context.startForegroundService(Intent(context, RepeaterService::class.java))
|
||||
}
|
||||
START_LOCAL_ONLY_HOTSPOT -> {
|
||||
if (grantResults.firstOrNull() == PackageManager.PERMISSION_GRANTED) @TargetApi(26) {
|
||||
val context = requireContext()
|
||||
context.startForegroundService(Intent(context, LocalOnlyHotspotService::class.java))
|
||||
}
|
||||
}
|
||||
else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
|
||||
binder = service as TetheringService.Binder
|
||||
service.routingsChanged[this] = {
|
||||
|
||||
Reference in New Issue
Block a user