Refine location permission check for repeater

This commit is contained in:
Mygod
2021-05-19 14:25:02 -04:00
parent 2c89444c63
commit e679974796
4 changed files with 10 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ 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.SoftApConfiguration
import android.net.wifi.p2p.WifiP2pGroup
import android.os.Build
@@ -88,7 +89,11 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
when (binder?.service?.status) {
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)
} else if (parent.requireContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED ||
parent.shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
parent.startRepeater.launch(Manifest.permission.ACCESS_FINE_LOCATION)
} else SmartSnackbar.make(R.string.repeater_missing_location_permissions).shortToast().show()
RepeaterService.Status.ACTIVE -> binder.shutdown()
else -> { }
}

View File

@@ -2,7 +2,6 @@
package be.mygod.vpnhotspot.manage
import android.Manifest
import android.annotation.TargetApi
import android.content.*
import android.os.Build
@@ -12,7 +11,6 @@ import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.appcompat.widget.Toolbar
@@ -126,11 +124,7 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick
@RequiresApi(29)
val startRepeater = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
val activity = requireActivity()
if (granted) activity.startForegroundService(Intent(activity, RepeaterService::class.java))
else if (!activity.shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
Toast.makeText(activity, "Repeater requires LOCATION permission", Toast.LENGTH_SHORT).show()
}
if (granted) requireActivity().startForegroundService(Intent(activity, RepeaterService::class.java))
}
@RequiresApi(26)
val startLocalOnlyHotspot = registerForActivityResult(ActivityResultContracts.RequestPermission()) {

View File

@@ -28,6 +28,7 @@
<string name="repeater_failure_reason_no_service_requests">未添加服务请求</string>
<string name="repeater_failure_reason_unsupported_operation">不支持此操作</string>
<string name="repeater_failure_disconnected">服务不可用,请稍后重试</string>
<string name="repeater_missing_location_permissions">无线中继需要精确位置权限</string>
<string name="tethering_temp_hotspot">临时 WLAN 热点</string>
<string name="tethering_temp_hotspot_location">使用临时热点需要打开位置服务。</string>

View File

@@ -45,6 +45,8 @@
<string name="repeater_failure_reason_no_service_requests">no service requests added</string>
<string name="repeater_failure_reason_unsupported_operation">unsupported operation</string>
<string name="repeater_failure_disconnected">Service unavailable. Try again later</string>
<string name="repeater_missing_location_permissions">Repeater requires permissions for accessing fine
location</string>
<string name="tethering_temp_hotspot">Temporary Wi\u2011Fi hotspot</string>
<string name="tethering_temp_hotspot_location">Temporary hotspot requires location to be turned on.</string>