-
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.
- permissions refactor and some tips recordings for kalinowice
- Loading branch information
Showing
24 changed files
with
160 additions
and
161 deletions.
There are no files selected for viewing
12 changes: 9 additions & 3 deletions
12
app/src/classic/java/pl/marianjureczko/poszukiwacz/permissions/RequirementsForBluetooth.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 |
---|---|---|
@@ -1,27 +1,33 @@ | ||
package pl.marianjureczko.poszukiwacz.permissions | ||
|
||
import android.Manifest | ||
import android.annotation.SuppressLint | ||
import android.os.Build | ||
import androidx.annotation.RequiresApi | ||
import pl.marianjureczko.poszukiwacz.R | ||
|
||
object RequirementsForBluetooth : Requirements { | ||
override fun getPermission(): String = Manifest.permission.BLUETOOTH | ||
override fun getMessage(): Int = R.string.missing_bluetooth_permission | ||
override fun shouldRequestOnThiDevice(): Boolean = true | ||
} | ||
|
||
object RequirementsForBluetoothConnect : Requirements { | ||
@SuppressLint("InlinedApi") | ||
@RequiresApi(Build.VERSION_CODES.S) | ||
override fun getPermission(): String = Manifest.permission.BLUETOOTH_CONNECT | ||
override fun getMessage(): Int = R.string.missing_bluetooth_permission | ||
override fun shouldRequestOnThiDevice(): Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S | ||
} | ||
|
||
object RequirementsForNearbyWifiDevices : Requirements { | ||
@SuppressLint("InlinedApi") | ||
@RequiresApi(Build.VERSION_CODES.TIRAMISU) | ||
override fun getPermission(): String = Manifest.permission.NEARBY_WIFI_DEVICES | ||
override fun getMessage(): Int = R.string.missing_bluetooth_permission | ||
override fun shouldRequestOnThiDevice(): Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU | ||
} | ||
|
||
object RequirementsForBluetoothScan : Requirements { | ||
@RequiresApi(Build.VERSION_CODES.S) | ||
override fun getPermission(): String = Manifest.permission.BLUETOOTH_SCAN | ||
override fun getMessage(): Int = R.string.missing_bluetooth_permission | ||
override fun shouldRequestOnThiDevice(): Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S | ||
} |
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
69 changes: 1 addition & 68 deletions
69
app/src/classic/java/pl/marianjureczko/poszukiwacz/screen/bluetooth/BluetoothState.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 |
---|---|---|
@@ -1,84 +1,17 @@ | ||
package pl.marianjureczko.poszukiwacz.screen.bluetooth | ||
|
||
import android.os.Build | ||
import androidx.compose.runtime.Composable | ||
import com.google.accompanist.permissions.ExperimentalPermissionsApi | ||
import com.google.accompanist.permissions.PermissionState | ||
import com.google.accompanist.permissions.isGranted | ||
import com.google.accompanist.permissions.shouldShowRationale | ||
import pl.marianjureczko.poszukiwacz.model.Route | ||
|
||
@OptIn(ExperimentalPermissionsApi::class) | ||
data class BluetoothState( | ||
val permissions: Permissions = Permissions(), | ||
val route: Route? = null, | ||
val mode: Mode, | ||
val messages: List<String> = emptyList(), | ||
val devices: List<String> = emptyList(), | ||
val deviceIsSelected: Boolean = false, | ||
val permissionToRequestIndex: Int = 0, | ||
) { | ||
fun addMessage(msg: String): BluetoothState = copy(messages = messages + msg) | ||
fun shouldShowDeviceSelection(): Boolean { | ||
return devices.isNotEmpty() && !deviceIsSelected | ||
} | ||
} | ||
|
||
//TODO t: what will happen when permission not granted | ||
@OptIn(ExperimentalPermissionsApi::class) | ||
data class Permissions( | ||
val bluetoothPermission: PermissionState? = null, | ||
val bluetoothScanPermission: PermissionState? = null, | ||
val bluetoothConnectPermission: PermissionState? = null, | ||
val nearbyWifiDevicesPermission: PermissionState? = null, | ||
) { | ||
//TODO t: remove duplication, use canAskForNextPermission fun | ||
//TODO check SDK version | ||
@Composable | ||
fun canAskForBluetoothScanPermission(): Boolean { | ||
return bluetoothPermissionGranted() | ||
&& !scanPermissionGranted() | ||
} | ||
|
||
@Composable | ||
fun canAskForBluetoothConnectPermission(): Boolean { | ||
return sdkRequiringScanPermission() | ||
&& scanPermissionGranted() | ||
&& !connectPermissionGranted() | ||
} | ||
|
||
@Composable | ||
fun canAskForNearbyWifiDevicesPermission(): Boolean { | ||
return sdkRequiringCanAskForNearbyWifiDevices() | ||
&& connectPermissionGranted() | ||
&& !nearbyWifiDevicesPermissionGranted() | ||
} | ||
|
||
@Composable | ||
fun canInitViewModel(): Boolean { | ||
return scanPermissionGranted() | ||
&& (!sdkRequiringScanPermission() || connectPermissionGranted()) | ||
&& (!sdkRequiringScanPermission() || nearbyWifiDevicesPermissionGranted()) | ||
} | ||
|
||
@Composable | ||
private fun bluetoothPermissionGranted() = | ||
bluetoothPermission?.status?.isGranted == true || bluetoothPermission?.status?.shouldShowRationale == true | ||
|
||
@Composable | ||
private fun scanPermissionGranted() = | ||
bluetoothScanPermission?.status?.isGranted == true || bluetoothScanPermission?.status?.shouldShowRationale == true | ||
|
||
@Composable | ||
private fun sdkRequiringScanPermission() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S | ||
|
||
@Composable | ||
private fun sdkRequiringCanAskForNearbyWifiDevices() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU | ||
|
||
@Composable | ||
private fun connectPermissionGranted() = | ||
bluetoothConnectPermission?.status?.isGranted == true || bluetoothConnectPermission?.status?.shouldShowRationale == true | ||
|
||
@Composable | ||
fun nearbyWifiDevicesPermissionGranted() = | ||
nearbyWifiDevicesPermission?.status?.isGranted == true || nearbyWifiDevicesPermission?.status?.shouldShowRationale == 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
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
Oops, something went wrong.