From 03336b95f560a845a3f48c561879e321c9104872 Mon Sep 17 00:00:00 2001 From: Jhalak Upadhyay <89909502+Jhalakupadhyay@users.noreply.github.com> Date: Fri, 17 May 2024 01:44:48 +0530 Subject: [PATCH] added the open dialog feature for location permission (#929) --- android/src/main/AndroidManifest.xml | 1 + .../fossasia/badgemagic/ui/DrawerActivity.kt | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index d39b6f11..9eb4e1bc 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ + diff --git a/android/src/main/java/org/fossasia/badgemagic/ui/DrawerActivity.kt b/android/src/main/java/org/fossasia/badgemagic/ui/DrawerActivity.kt index 48690100..bb7a0afc 100644 --- a/android/src/main/java/org/fossasia/badgemagic/ui/DrawerActivity.kt +++ b/android/src/main/java/org/fossasia/badgemagic/ui/DrawerActivity.kt @@ -58,6 +58,8 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + checkLocationPermission() + binding = ActivityDrawerBinding.inflate(layoutInflater) setContentView(binding.root) @@ -380,4 +382,35 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi drawerLayout.closeDrawer(GravityCompat.START) return true } + + private fun hasLocationPermission(): Boolean { + return ContextCompat.checkSelfPermission( + this, Manifest.permission.ACCESS_FINE_LOCATION + ) == PackageManager.PERMISSION_GRANTED + } + private fun checkLocationPermission() { + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || + ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED + ) { + + // Show an explanation to the user why the permission is needed + if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION) || + ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION) + ) { + + AlertDialog.Builder(this) + .setTitle("Location Permission Needed") + .setMessage("This app needs the Location permission to scan for nearby ble devices. Please grant the permission.") + .setPositiveButton("OK") { _, _ -> + } + .create() + .show() + } else { + ActivityCompat.requestPermissions( + this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), + REQUEST_PERMISSION_CODE + ) + } + } + } }