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

Remove multiple sensor receivers #746

Merged
merged 2 commits into from
Aug 13, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package io.homeassistant.companion.android.sensors

import android.Manifest
import android.content.Context
import android.location.Address
import android.location.Geocoder
import android.os.Build
import android.util.Log
import com.google.android.gms.location.LocationServices
import com.google.android.gms.tasks.Tasks
import io.homeassistant.companion.android.domain.integration.SensorRegistration
import io.homeassistant.companion.android.util.PermissionManager

class GeocodeSensorManager : SensorManager {

Expand All @@ -19,7 +20,11 @@ class GeocodeSensorManager : SensorManager {
get() = "Geolocation Sensors"

override fun requiredPermissions(): Array<String> {
return PermissionManager.getLocationPermissionArray()
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
} else {
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION)
}
}

override fun getSensorRegistrations(context: Context): List<SensorRegistration<Any>> {
Expand All @@ -28,7 +33,7 @@ class GeocodeSensorManager : SensorManager {

private fun getGeocodedLocation(context: Context): SensorRegistration<Any> {
var address: Address? = null
if (PermissionManager.checkLocationPermission(context)) {
if (checkPermission(context)) {
try {
val locApi = LocationServices.getFusedLocationProviderClient(context)
Tasks.await(locApi.lastLocation)?.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.homeassistant.companion.android.sensors

import android.Manifest
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
Expand All @@ -19,7 +20,6 @@ import io.homeassistant.companion.android.database.AppDatabase
import io.homeassistant.companion.android.domain.integration.IntegrationUseCase
import io.homeassistant.companion.android.domain.integration.SensorRegistration
import io.homeassistant.companion.android.domain.integration.UpdateLocation
import io.homeassistant.companion.android.util.PermissionManager
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -44,6 +44,13 @@ class LocationBroadcastReceiver : BroadcastReceiver(), SensorManager {
const val ID_ZONE_LOCATION = "location_zone"

internal const val TAG = "LocBroadcastReceiver"

fun restartLocationTracking(context: Context) {
val intent = Intent(context, LocationBroadcastReceiver::class.java)
intent.action = ACTION_REQUEST_LOCATION_UPDATES

context.sendBroadcast(intent)
}
}

@Inject
Expand Down Expand Up @@ -76,7 +83,7 @@ class LocationBroadcastReceiver : BroadcastReceiver(), SensorManager {
}

private fun setupLocationTracking(context: Context) {
if (!PermissionManager.checkLocationPermission(context)) {
if (!checkPermission(context)) {
Log.w(TAG, "Not starting location reporting because of permissions.")
return
}
Expand Down Expand Up @@ -110,7 +117,7 @@ class LocationBroadcastReceiver : BroadcastReceiver(), SensorManager {
}

private fun requestLocationUpdates(context: Context) {
if (!PermissionManager.checkLocationPermission(context)) {
if (!checkPermission(context)) {
Log.w(TAG, "Not registering for location updates because of permissions.")
return
}
Expand All @@ -126,7 +133,7 @@ class LocationBroadcastReceiver : BroadcastReceiver(), SensorManager {
}

private suspend fun requestZoneUpdates(context: Context) {
if (!PermissionManager.checkLocationPermission(context)) {
if (!checkPermission(context)) {
Log.w(TAG, "Not registering for zone based updates because of permissions.")
return
}
Expand Down Expand Up @@ -240,7 +247,7 @@ class LocationBroadcastReceiver : BroadcastReceiver(), SensorManager {
}

private fun requestSingleAccurateLocation(context: Context) {
if (!PermissionManager.checkLocationPermission(context)) {
if (!checkPermission(context)) {
Log.w(TAG, "Not getting single accurate location because of permissions.")
return
}
Expand Down Expand Up @@ -286,7 +293,11 @@ class LocationBroadcastReceiver : BroadcastReceiver(), SensorManager {
get() = "Location Sensors"

override fun requiredPermissions(): Array<String> {
return PermissionManager.getLocationPermissionArray()
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
} else {
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION)
}
}

override fun getSensorRegistrations(context: Context): List<SensorRegistration<Any>> {
Expand Down
27 changes: 8 additions & 19 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<uses-feature android:name="android.hardware.sensor" android:required="false"/>
<uses-feature android:name="android.hardware.microphone" android:required="false" />
<uses-feature android:name="android.software.leanback" android:required="false" />
<uses-feature android:name="android.hardware.bluetooth" android:required="false" />

<application
android:name=".HomeAssistantApplication"
Expand Down Expand Up @@ -61,9 +62,14 @@
</receiver>

<!-- Start things like SensorWorker on device boot -->
<receiver android:name=".HomeAssistantBootReceiver">
<receiver android:name=".sensors.SensorReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.app.action.NEXT_ALARM_CLOCK_CHANGED" />
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
</intent-filter>
</receiver>

Expand All @@ -84,27 +90,10 @@
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
<action android:name="io.homeassistant.companion.android.background.REQUEST_ACCURATE_UPDATE" />
</intent-filter>
</receiver>

<receiver android:name=".sensors.NextAlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.app.action.NEXT_ALARM_CLOCK_CHANGED" />
</intent-filter>
</receiver>

<receiver android:name=".sensors.BluetoothReceiver">
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
</intent-filter>
</receiver>

<activity android:name=".launch.LaunchActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import android.telephony.TelephonyManager
import io.homeassistant.companion.android.common.dagger.AppComponent
import io.homeassistant.companion.android.common.dagger.Graph
import io.homeassistant.companion.android.common.dagger.GraphComponentAccessor
import io.homeassistant.companion.android.sensors.BluetoothReceiver
import io.homeassistant.companion.android.sensors.ChargingBroadcastReceiver
import io.homeassistant.companion.android.sensors.PhoneStateReceiver
import io.homeassistant.companion.android.sensors.WifiStateReceiver
import io.homeassistant.companion.android.sensors.SensorReceiver

open class HomeAssistantApplication : Application(), GraphComponentAccessor {

Expand All @@ -23,11 +20,12 @@ open class HomeAssistantApplication : Application(), GraphComponentAccessor {

graph = Graph(this, 0)

val sensorReceiver = SensorReceiver()
// This will cause the sensor to be updated every time the OS broadcasts that a cable was plugged/unplugged.
// This should be nearly instantaneous allowing automations to fire immediately when a phone is plugged
// in or unplugged. Updates will also be triggered when the system reports low battery and when it recovers.
registerReceiver(
ChargingBroadcastReceiver(appComponent.integrationUseCase()), IntentFilter().apply {
sensorReceiver, IntentFilter().apply {
addAction(Intent.ACTION_BATTERY_LOW)
addAction(Intent.ACTION_BATTERY_OKAY)
addAction(Intent.ACTION_POWER_CONNECTED)
Expand All @@ -36,16 +34,22 @@ open class HomeAssistantApplication : Application(), GraphComponentAccessor {
)

// This will trigger an update any time the wifi state has changed
registerReceiver(WifiStateReceiver(), IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION))
registerReceiver(
sensorReceiver,
IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION)
)

// This will cause the phone state sensor to be updated every time the OS broadcasts that a call triggered.
registerReceiver(
PhoneStateReceiver(appComponent.integrationUseCase()), IntentFilter().apply {
sensorReceiver,
IntentFilter().apply {
addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED)
}
)

registerReceiver(BluetoothReceiver(), IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED))
registerReceiver(sensorReceiver,
IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
)
}

override val appComponent: AppComponent
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.homeassistant.companion.android.onboarding.integration

import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
Expand All @@ -25,7 +26,6 @@ import io.homeassistant.companion.android.database.AppDatabase
import io.homeassistant.companion.android.database.sensor.Sensor
import io.homeassistant.companion.android.sensors.LocationBroadcastReceiver
import io.homeassistant.companion.android.sensors.PhoneStateSensorManager
import io.homeassistant.companion.android.util.PermissionManager
import javax.inject.Inject
import kotlinx.android.synthetic.main.fragment_mobile_app_integration.*

Expand All @@ -38,6 +38,9 @@ class MobileAppIntegrationFragment : Fragment(), MobileAppIntegrationView {

private const val BACKGROUND_REQUEST = 99

private const val LOCATION_REQUEST_CODE = 0
private const val PHONE_REQUEST_CODE = 1

fun newInstance(): MobileAppIntegrationFragment {
return MobileAppIntegrationFragment()
}
Expand Down Expand Up @@ -78,11 +81,14 @@ class MobileAppIntegrationFragment : Fragment(), MobileAppIntegrationView {

findViewById<AppCompatButton>(R.id.location_perms).apply {
setOnClickListener {
PermissionManager.requestLocationPermissions(this@MobileAppIntegrationFragment)
this@MobileAppIntegrationFragment.requestPermissions(
LocationBroadcastReceiver().requiredPermissions(),
LOCATION_REQUEST_CODE
)
}
}

val hasLocationPermission = PermissionManager.checkLocationPermission(context)
val hasLocationPermission = LocationBroadcastReceiver().checkPermission(context)

zoneTracking = findViewById<SwitchCompat>(R.id.location_zone).apply {
setOnCheckedChangeListener { _, isChecked ->
Expand All @@ -107,11 +113,11 @@ class MobileAppIntegrationFragment : Fragment(), MobileAppIntegrationView {
// Calls tracking
findViewById<AppCompatButton>(R.id.phone_state_perms).apply {
setOnClickListener {
PermissionManager.requestPhoneStatePermissions(this@MobileAppIntegrationFragment)
this@MobileAppIntegrationFragment.requestPermissions(PhoneStateSensorManager().requiredPermissions(), PHONE_REQUEST_CODE)
}
}

val hasPhoneStatePermission = PermissionManager.checkPhoneStatePermission(context)
val hasPhoneStatePermission = PhoneStateSensorManager().checkPermission(requireContext())

callTracking = findViewById<SwitchCompat>(R.id.call_tracking).apply {
setOnCheckedChangeListener { _, isChecked ->
Expand Down Expand Up @@ -162,7 +168,7 @@ class MobileAppIntegrationFragment : Fragment(), MobileAppIntegrationView {
}

override fun onDestroy() {
PermissionManager.restartLocationTracking(requireContext())
LocationBroadcastReceiver.restartLocationTracking(requireContext())
presenter.onFinish()
super.onDestroy()
}
Expand All @@ -174,8 +180,8 @@ class MobileAppIntegrationFragment : Fragment(), MobileAppIntegrationView {
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)

if (requestCode == PermissionManager.LOCATION_REQUEST_CODE) {
if (PermissionManager.validateLocationPermissions(requestCode, grantResults)) {
if (requestCode == LOCATION_REQUEST_CODE) {
if (grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
zoneTracking.isEnabled = true
zoneTrackingSummary.isEnabled = true
zoneTracking.isChecked = true
Expand All @@ -193,8 +199,8 @@ class MobileAppIntegrationFragment : Fragment(), MobileAppIntegrationView {
requestBackgroundAccess()
}

if (requestCode == PermissionManager.PHONE_STATE_REQUEST_CODE) {
if (PermissionManager.validatePhoneStatePermissions(requestCode, grantResults)) {
if (requestCode == PHONE_REQUEST_CODE) {
if (grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
callTracking.isEnabled = true
callTracking.isChecked = true
callTrackingSummary.isEnabled = true
Expand Down

This file was deleted.