Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for permissions and notify if they are missing when command_bluetooth is used #2205

Merged
merged 1 commit into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.homeassistant.companion.android.notifications

import android.Manifest
import android.app.ActivityManager
import android.app.Notification
import android.app.NotificationChannel
Expand All @@ -9,6 +10,7 @@ import android.bluetooth.BluetoothManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
Expand Down Expand Up @@ -547,6 +549,17 @@ class MessagingManager @Inject constructor(
}
COMMAND_BLUETOOTH -> {
val bluetoothAdapter = context.getSystemService<BluetoothManager>()?.adapter
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
when (PackageManager.PERMISSION_GRANTED) {
ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) -> {
Log.d(TAG, "We have proper bluetooth permissions proceeding with command")
}
else -> {
Log.e(TAG, "Missing Bluetooth permissions, notifying user to grant permissions")
notifyMissingPermission(data[MESSAGE].toString())
}
}
}
if (title == TURN_OFF)
bluetoothAdapter?.disable()
if (title == TURN_ON)
Expand Down Expand Up @@ -1310,6 +1323,15 @@ class MessagingManager @Inject constructor(
context.startActivity(intent)
}

private fun navigateAppDetails() {
val intent = Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:${context.packageName}")
)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
}

private fun getKeyEvent(key: String): Int {
return when (key) {
MEDIA_FAST_FORWARD -> KeyEvent.KEYCODE_MEDIA_FAST_FORWARD
Expand Down Expand Up @@ -1513,6 +1535,7 @@ class MessagingManager @Inject constructor(
COMMAND_MEDIA -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
requestNotificationPermission()
}
COMMAND_BLUETOOTH -> navigateAppDetails()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@
<string name="message_some_installed">The Wear app is installed on some of your wear devices: (%1$s)\n\nClick the button below to install the app on the other devices.\n\nNote: Currently the Wear OS app requires you to be enrolled in the beta for the phone app. If the button does not work then please join the beta: https://play.google.com/apps/testing/io.homeassistant.companion.android</string>
<string name="mfa_title">Two-factor\nAuthentication</string>
<string name="mfa_hint">Code</string>
<string name="missing_command_permission">Please open the Home Assistant app and send the command again in order to grant the proper permissions.</string>
<string name="missing_command_permission">Please open the Home Assistant app and send the command again in order to grant the proper permissions. You will be taken to a page to either grant the Home Assistant app the permission, or you will need to select Permissions from the details page and then grant the missing permission. For command_bluetooth the name of the permission is Nearby devices.</string>
<string name="areas">Areas</string>
<string name="more_entities">More entities</string>
<string name="need_help">Need Help?</string>
Expand Down