Commit 239277fb authored by Anon's avatar Anon

Handle permissions

parent bcf06692
......@@ -23,7 +23,7 @@
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
......
package com.github.antweb.donkey
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
private const val TAG = "MainActivity"
private const val REQUEST_CODE_PERMISSIONS = 1
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (ConnectionService.hasDevice()) {
val locationPermission =
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
val storagePermission =
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
if (locationPermission != PackageManager.PERMISSION_GRANTED ||
storagePermission != PackageManager.PERMISSION_GRANTED
) {
showPermissionView()
} else if (ConnectionService.hasDevice()) {
showConnectedView()
} else {
showNotConnectedView()
}
}
private fun showPermissionView() {
setContentView(R.layout.activity_main_no_permissions)
val buttonRequest = findViewById<Button>(R.id.button_request_permissions)
buttonRequest.setOnClickListener {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_EXTERNAL_STORAGE),
REQUEST_CODE_PERMISSIONS
)
}
}
private fun showConnectedView() {
setContentView(R.layout.activity_main_connected)
......@@ -43,4 +70,10 @@ class MainActivity : AppCompatActivity() {
startActivity(intent)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
// The lazy way out...
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_label_no_permissions"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_request_permissions"
android:text="@string/main_button_grant_permissions"/>
</LinearLayout>
\ No newline at end of file
......@@ -5,8 +5,10 @@
<string name="send_activity_title">Send File</string>
<string name="main_activity_title">@string/app_name</string>
<string name="main_label_no_permissions">card10 needs location and storage permissions in order to scan for Bluetooth devices and send files.</string>
<string name="main_label_not_connected">You are currently not connected to your card10.</string>
<string name="main_label_status">You are connected to %1$s (%2$s)</string>
<string name="main_button_grant_permissions">Grant Permissions</string>
<string name="main_button_connect">Connect</string>
<string name="main_button_send">Send File</string>
</resources>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment