mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
camera permissions routine
This commit is contained in:
parent
14af474b71
commit
062dbbc47d
3 changed files with 28 additions and 7 deletions
|
|
@ -42,6 +42,7 @@ import com.geeksville.android.Logging
|
||||||
import com.geeksville.android.ServiceClient
|
import com.geeksville.android.ServiceClient
|
||||||
import com.geeksville.concurrent.handledLaunch
|
import com.geeksville.concurrent.handledLaunch
|
||||||
import com.geeksville.mesh.android.getBackgroundPermissions
|
import com.geeksville.mesh.android.getBackgroundPermissions
|
||||||
|
import com.geeksville.mesh.android.getCameraPermissions
|
||||||
import com.geeksville.mesh.android.getMissingPermissions
|
import com.geeksville.mesh.android.getMissingPermissions
|
||||||
import com.geeksville.mesh.database.entity.Packet
|
import com.geeksville.mesh.database.entity.Packet
|
||||||
import com.geeksville.mesh.databinding.ActivityMainBinding
|
import com.geeksville.mesh.databinding.ActivityMainBinding
|
||||||
|
|
@ -247,6 +248,8 @@ class MainActivity : AppCompatActivity(), Logging,
|
||||||
return getMissingPermissions(perms)
|
return getMissingPermissions(perms)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Ask the user to grant camera permission */
|
||||||
|
fun requestCameraPermission() = requestPermission(getCameraPermissions(), false)
|
||||||
|
|
||||||
/** Ask the user to grant background location permission */
|
/** Ask the user to grant background location permission */
|
||||||
fun requestBackgroundPermission() = requestPermission(getBackgroundPermissions(), false)
|
fun requestBackgroundPermission() = requestPermission(getBackgroundPermissions(), false)
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,18 @@ fun Context.getMissingPermissions(perms: List<String>) = perms.filter {
|
||||||
) != PackageManager.PERMISSION_GRANTED
|
) != PackageManager.PERMISSION_GRANTED
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Camera permission (or empty if we already have what we need)
|
||||||
|
*/
|
||||||
|
fun Context.getCameraPermissions(): List<String> {
|
||||||
|
val perms = mutableListOf(Manifest.permission.CAMERA)
|
||||||
|
|
||||||
|
return getMissingPermissions(perms)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return true if the user already has camera permission */
|
||||||
|
fun Context.hasCameraPermission() = getCameraPermissions().isEmpty()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A list of missing background location permissions (or empty if we already have what we need)
|
* A list of missing background location permissions (or empty if we already have what we need)
|
||||||
*/
|
*/
|
||||||
|
|
@ -41,4 +53,4 @@ fun Context.getBackgroundPermissions(): List<String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return true if the user already has background location permission */
|
/** @return true if the user already has background location permission */
|
||||||
fun Context.hasBackgroundPermission() = getBackgroundPermissions().isEmpty()
|
fun Context.hasBackgroundPermission() = getBackgroundPermissions().isEmpty()
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,9 @@ import com.geeksville.android.Logging
|
||||||
import com.geeksville.android.hideKeyboard
|
import com.geeksville.android.hideKeyboard
|
||||||
import com.geeksville.mesh.AppOnlyProtos
|
import com.geeksville.mesh.AppOnlyProtos
|
||||||
import com.geeksville.mesh.ChannelProtos
|
import com.geeksville.mesh.ChannelProtos
|
||||||
|
import com.geeksville.mesh.MainActivity
|
||||||
import com.geeksville.mesh.R
|
import com.geeksville.mesh.R
|
||||||
|
import com.geeksville.mesh.android.hasCameraPermission
|
||||||
import com.geeksville.mesh.databinding.ChannelFragmentBinding
|
import com.geeksville.mesh.databinding.ChannelFragmentBinding
|
||||||
import com.geeksville.mesh.model.Channel
|
import com.geeksville.mesh.model.Channel
|
||||||
import com.geeksville.mesh.model.ChannelOption
|
import com.geeksville.mesh.model.ChannelOption
|
||||||
|
|
@ -208,12 +210,16 @@ class ChannelFragment : ScreenFragment("Channel"), Logging {
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.scanButton.setOnClickListener {
|
binding.scanButton.setOnClickListener {
|
||||||
val zxingScan = IntentIntegrator.forSupportFragment(this)
|
if ((requireActivity() as MainActivity).hasCameraPermission()) {
|
||||||
zxingScan.setCameraId(0)
|
val zxingScan = IntentIntegrator.forSupportFragment(this)
|
||||||
zxingScan.setPrompt("")
|
zxingScan.setCameraId(0)
|
||||||
zxingScan.setBeepEnabled(false)
|
zxingScan.setPrompt("")
|
||||||
zxingScan.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE)
|
zxingScan.setBeepEnabled(false)
|
||||||
zxingScan.initiateScan()
|
zxingScan.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE)
|
||||||
|
zxingScan.initiateScan()
|
||||||
|
} else {
|
||||||
|
(requireActivity() as MainActivity).requestCameraPermission()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: Do not use setOnCheckedChanged here because we don't want to be called when we programmatically disable editing
|
// Note: Do not use setOnCheckedChanged here because we don't want to be called when we programmatically disable editing
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue