Skip to content

Commit

Permalink
Always request Bluetooth permission for Improv if 1/2 is granted (#4952)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpelgrom authored Jan 4, 2025
1 parent 5e27a71 commit bd61abc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
downloadFile(downloadFileUrl, downloadFileContentDisposition, downloadFileMimetype)
}
}
private val requestImprovPermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (isGranted) {
presenter.startScanningForImprov()
}
}
private val writeNfcTag = registerForActivityResult(WriteNfcTag()) { messageId ->
sendExternalBusMessage(
ExternalBusMessage(
Expand Down Expand Up @@ -1696,7 +1702,12 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
val dialog = ImprovPermissionDialog()
dialog.show(supportFragmentManager, ImprovPermissionDialog.TAG)
} else {
presenter.startScanningForImprov()
val safePermissionToRequest = presenter.shouldRequestImprovPermission()
if (safePermissionToRequest != null) {
requestImprovPermission.launch(safePermissionToRequest)
} else {
presenter.startScanningForImprov()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ interface WebViewPresenter {
/** @return `true` if the app should prompt the user for Improv permissions before scanning */
suspend fun shouldShowImprovPermissions(): Boolean

/**
* @return Improv permission the app should request directly, without showing a prompt.
* This may occur when one of two Bluetooth related permissions is granted and the other one
* is not. The system should automatically grant this when requested.
* */
fun shouldRequestImprovPermission(): String?

/** @return `true` if the app tried starting scanning or `false` if it was missing permissions */
fun startScanningForImprov(): Boolean
fun stopScanningForImprov(force: Boolean)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.homeassistant.companion.android.webview

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.IntentSender
import android.content.pm.PackageManager
import android.graphics.Color
import android.net.Uri
import android.util.Log
import androidx.activity.result.ActivityResult
import androidx.core.content.ContextCompat
import dagger.hilt.android.qualifiers.ActivityContext
import io.homeassistant.companion.android.common.R as commonR
import io.homeassistant.companion.android.common.data.authentication.SessionState
Expand Down Expand Up @@ -489,6 +492,23 @@ class WebViewPresenterImpl @Inject constructor(
}
}

override fun shouldRequestImprovPermission(): String? {
var returnPermissions = try {
improvRepository.getRequiredPermissions().filter {
ContextCompat.checkSelfPermission(view as Context, it) != PackageManager.PERMISSION_GRANTED
}
} catch (_: Exception) {
// Unable to check, ignore
emptyList<String>()
}
return if (returnPermissions.size == 1 && returnPermissions[0] != Manifest.permission.ACCESS_FINE_LOCATION) {
Log.d(TAG, "Should request Improv permission: $returnPermissions")
returnPermissions[0]
} else {
null
}
}

override fun startScanningForImprov(): Boolean {
if (!improvRepository.hasPermission(view as Context)) {
Log.d(TAG, "Improv scan request ignored because app doesn't have permission")
Expand Down

0 comments on commit bd61abc

Please sign in to comment.