-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show a prompt to request Bluetooth permission
- Loading branch information
Showing
7 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
app/src/main/java/io/bimmergestalt/bclclient/controllers/PermissionsController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.bimmergestalt.bclclient.controllers | ||
|
||
import android.Manifest | ||
import android.app.Activity | ||
import androidx.core.app.ActivityCompat | ||
|
||
class PermissionsController(val activity: Activity) { | ||
companion object { | ||
const val REQUEST_BLUETOOTH = 50 | ||
} | ||
fun promptBluetoothConnect() { | ||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { | ||
ActivityCompat.requestPermissions(activity, | ||
arrayOf(Manifest.permission.BLUETOOTH_CONNECT), | ||
REQUEST_BLUETOOTH) | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
app/src/main/java/io/bimmergestalt/bclclient/fragments/PermissionsFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.bimmergestalt.bclclient.fragments | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import io.bimmergestalt.bclclient.controllers.PermissionsController | ||
import io.bimmergestalt.bclclient.databinding.PermissionsBinding | ||
import io.bimmergestalt.bclclient.models.PermissionsViewModel | ||
|
||
class PermissionsFragment: Fragment() { | ||
val viewModel by viewModels<PermissionsViewModel>() | ||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { | ||
val binding = PermissionsBinding.inflate(inflater, container, false) | ||
|
||
binding.lifecycleOwner = viewLifecycleOwner | ||
binding.controller = PermissionsController(requireActivity()) | ||
binding.viewModel = viewModel | ||
return binding.root | ||
} | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
viewModel.update(requireContext()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
app/src/main/java/io/bimmergestalt/bclclient/models/PermissionsViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.bimmergestalt.bclclient.models | ||
|
||
import android.Manifest | ||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import androidx.core.content.ContextCompat | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
|
||
class PermissionsViewModel: ViewModel() { | ||
private val _supportsBluetoothConnectPermission = MutableLiveData(false) | ||
val supportsBluetoothConnectPermission: LiveData<Boolean> = _supportsBluetoothConnectPermission | ||
private val _hasBluetoothConnectPermission = MutableLiveData(false) | ||
val hasBluetoothConnectPermission: LiveData<Boolean> = _hasBluetoothConnectPermission | ||
|
||
fun update(context: Context) { | ||
_supportsBluetoothConnectPermission.value = android.os.Build.VERSION.SDK_INT >= 31 | ||
_hasBluetoothConnectPermission.value = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { | ||
ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED | ||
} else { | ||
true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto"> | ||
<data class="PermissionsBinding"> | ||
<variable | ||
name="controller" | ||
type="io.bimmergestalt.bclclient.controllers.PermissionsController" /> | ||
<variable | ||
name="viewModel" | ||
type="io.bimmergestalt.bclclient.models.PermissionsViewModel" /> | ||
</data> | ||
<LinearLayout | ||
android:orientation="vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:paddingBottom="16dp" | ||
android:orientation="vertical" | ||
android:gravity="center" | ||
android:visibility="@{viewModel.supportsBluetoothConnectPermission && !viewModel.hasBluetoothConnectPermission}"> | ||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center_vertical"> | ||
<ImageView | ||
android:layout_width="48dp" | ||
android:layout_height="48dp" | ||
android:padding="12dp" | ||
android:onClick="@{() -> controller.promptBluetoothConnect()}" | ||
app:srcCompat="@android:drawable/checkbox_off_background" /> | ||
|
||
<TextView | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:paddingTop="12dp" | ||
android:paddingBottom="12dp" | ||
android:text="@string/lbl_grant_permission_bluetooth_connect" /> | ||
</LinearLayout> | ||
<Button | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/lbl_grant" | ||
android:textSize="12sp" | ||
android:onClick="@{() -> controller.promptBluetoothConnect()}" /> | ||
</LinearLayout> | ||
</LinearLayout> | ||
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters