Skip to content

Commit

Permalink
added the open dialog feature for location permission (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhalakupadhyay authored May 16, 2024
1 parent 53e1e71 commit 03336b9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<uses-feature android:name="android.hardware.bluetooth" android:required="true"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
Expand Down
33 changes: 33 additions & 0 deletions android/src/main/java/org/fossasia/badgemagic/ui/DrawerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
)
}
}
}
}

0 comments on commit 03336b9

Please sign in to comment.