Fix lint issues (#618)

* Increase tool version, enable linting again.

* Fix lint errors.

* Ensure ktlint is working.

* Use correct build tools in travis.
This commit is contained in:
Justin Bassett 2020-06-11 13:15:50 -04:00 committed by GitHub
parent 0f92172fee
commit 247cef3c72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 82 additions and 50 deletions

View file

@ -11,7 +11,7 @@ before_install:
android:
components:
- android-29
- build-tools-28.0.3
- build-tools-29.0.2
before_cache:
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock

View file

@ -35,8 +35,8 @@ android {
versionName = "${Config.version}-$vCode"
}
viewBinding {
isEnabled = true
buildFeatures {
viewBinding = true
}
compileOptions {
@ -85,7 +85,6 @@ android {
lintOptions {
disable("MissingTranslation")
isAbortOnError = false
}
}
@ -137,4 +136,4 @@ dependencies {
// This plugin must stay at the bottom
// https://developers.google.com/android/guides/google-services-plugin
apply(plugin = "com.google.gms.google-services")
apply(plugin = "com.google.gms.google-services")

View file

@ -72,7 +72,7 @@ class LocationBroadcastReceiver : BroadcastReceiver() {
}
private fun setupLocationTracking(context: Context) {
if (!PermissionManager.hasLocationPermissions(context)) {
if (!PermissionManager.checkLocationPermission(context)) {
Log.w(TAG, "Not starting location reporting because of permissions.")
return
}
@ -104,6 +104,10 @@ class LocationBroadcastReceiver : BroadcastReceiver() {
}
private fun requestLocationUpdates(context: Context) {
if (!PermissionManager.checkLocationPermission(context)) {
Log.w(TAG, "Not registering for location updates because of permissions.")
return
}
Log.d(TAG, "Registering for location updates.")
val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context)
@ -116,6 +120,11 @@ class LocationBroadcastReceiver : BroadcastReceiver() {
}
private suspend fun requestZoneUpdates(context: Context) {
if (!PermissionManager.checkLocationPermission(context)) {
Log.w(TAG, "Not registering for zone based updates because of permissions.")
return
}
Log.d(TAG, "Registering for zone based location updates")
try {
@ -151,7 +160,10 @@ class LocationBroadcastReceiver : BroadcastReceiver() {
}
if (geofencingEvent.triggeringLocation.accuracy > MINIMUM_ACCURACY) {
Log.w(TAG, "Geofence location accuracy didn't meet requirements, requesting new location.")
Log.w(
TAG,
"Geofence location accuracy didn't meet requirements, requesting new location."
)
requestSingleAccurateLocation(context)
} else {
sendLocationUpdate(geofencingEvent.triggeringLocation)
@ -222,6 +234,10 @@ class LocationBroadcastReceiver : BroadcastReceiver() {
}
private fun requestSingleAccurateLocation(context: Context) {
if (!PermissionManager.checkLocationPermission(context)) {
Log.w(TAG, "Not getting single accurate location because of permissions.")
return
}
val maxRetries = 5
val request = createLocationRequest()
request.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
@ -233,16 +249,26 @@ class LocationBroadcastReceiver : BroadcastReceiver() {
var numberCalls = 0
override fun onLocationResult(locationResult: LocationResult?) {
numberCalls++
Log.d(TAG, "Got single accurate location update: ${locationResult?.lastLocation}")
Log.d(
TAG,
"Got single accurate location update: ${locationResult?.lastLocation}"
)
if (locationResult != null && locationResult.lastLocation.accuracy <= 1) {
Log.d(TAG, "Location accurate enough, all done with high accuracy.")
runBlocking { sendLocationUpdate(locationResult.lastLocation) }
LocationServices.getFusedLocationProviderClient(context).removeLocationUpdates(this)
LocationServices.getFusedLocationProviderClient(context)
.removeLocationUpdates(this)
} else if (numberCalls >= maxRetries) {
Log.d(TAG, "No location was accurate enough, sending our last location anyway")
Log.d(
TAG,
"No location was accurate enough, sending our last location anyway"
)
runBlocking { sendLocationUpdate(locationResult!!.lastLocation) }
} else {
Log.w(TAG, "Location not accurate enough on retry $numberCalls of $maxRetries")
Log.w(
TAG,
"Location not accurate enough on retry $numberCalls of $maxRetries"
)
}
}
},

View file

@ -108,11 +108,11 @@ class AuthenticationFragment : Fragment(), AuthenticationView {
// Fragment is at least paused, can't display alert
return
}
AlertDialog.Builder(context!!)
AlertDialog.Builder(requireContext())
.setTitle(R.string.error_connection_failed)
.setMessage(message)
.setPositiveButton(android.R.string.ok) { _, _ -> }
.show()
fragmentManager?.popBackStack()
parentFragmentManager.popBackStack()
}
}

View file

@ -48,7 +48,7 @@ class DiscoveryFragment : Fragment(), DiscoveryView {
.inject(this)
homeAssistantSearcher = HomeAssistantSearcher(
getSystemService(context!!, NsdManager::class.java)!!,
getSystemService(requireContext(), NsdManager::class.java)!!,
this
)
}
@ -59,7 +59,7 @@ class DiscoveryFragment : Fragment(), DiscoveryView {
savedInstanceState: Bundle?
): View? {
listViewAdapter = object : ArrayAdapter<HomeAssistantInstance>(context!!, R.layout.instance_item, instances) {
listViewAdapter = object : ArrayAdapter<HomeAssistantInstance>(requireContext(), R.layout.instance_item, instances) {
@SuppressLint("InflateParams")
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val v = convertView ?: LayoutInflater.from(context).inflate(

View file

@ -74,7 +74,7 @@ class MobileAppIntegrationFragment : Fragment(), MobileAppIntegrationView {
}
}
val hasLocationPermission = PermissionManager.hasLocationPermissions(context)
val hasLocationPermission = PermissionManager.checkLocationPermission(context)
zoneTracking = findViewById<SwitchCompat>(R.id.location_zone).apply {
setOnCheckedChangeListener { _, isChecked ->
@ -117,7 +117,7 @@ class MobileAppIntegrationFragment : Fragment(), MobileAppIntegrationView {
}
override fun onDestroy() {
PermissionManager.restartLocationTracking(context!!)
PermissionManager.restartLocationTracking(requireContext())
presenter.onFinish()
super.onDestroy()
}
@ -129,7 +129,7 @@ class MobileAppIntegrationFragment : Fragment(), MobileAppIntegrationView {
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (PermissionManager.validateLocationPermissions(requestCode, permissions, grantResults)) {
if (PermissionManager.validateLocationPermissions(requestCode, grantResults)) {
zoneTracking.isEnabled = true
zoneTrackingSummary.isEnabled = true
zoneTracking.isChecked = true

View file

@ -4,6 +4,7 @@ import android.content.Context
import android.util.Log
import io.homeassistant.companion.android.SensorUpdater
import io.homeassistant.companion.android.domain.integration.IntegrationUseCase
import io.homeassistant.companion.android.util.PermissionManager
class AllSensorsUpdaterImpl(
private val integrationUseCase: IntegrationUseCase,
@ -19,7 +20,7 @@ class AllSensorsUpdaterImpl(
NetworkSensorManager()
)
if (integrationUseCase.isBackgroundTrackingEnabled()) {
if (integrationUseCase.isBackgroundTrackingEnabled() && PermissionManager.checkLocationPermission(appContext)) {
sensorManagers.add(GeocodeSensorManager())
}

View file

@ -8,6 +8,7 @@ import com.google.android.gms.tasks.Tasks
import io.homeassistant.companion.android.background.LocationBroadcastReceiver
import io.homeassistant.companion.android.domain.integration.Sensor
import io.homeassistant.companion.android.domain.integration.SensorRegistration
import io.homeassistant.companion.android.util.PermissionManager
class GeocodeSensorManager : SensorManager {
@ -39,6 +40,10 @@ class GeocodeSensorManager : SensorManager {
}
private fun getGeocodedLocation(context: Context): Sensor<Any>? {
if (!PermissionManager.checkLocationPermission(context)) {
Log.w(TAG, "Tried getting gecoded location without permission.")
return null
}
Tasks.await(LocationServices.getFusedLocationProviderClient(context).lastLocation)?.let {
if (it.accuracy > LocationBroadcastReceiver.MINIMUM_ACCURACY)
return null

View file

@ -48,7 +48,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SettingsView {
val onChangeUrlValidator = Preference.OnPreferenceChangeListener { _, newValue ->
val isValid = newValue.toString().isBlank() || newValue.toString().toHttpUrlOrNull() != null
if (!isValid) {
AlertDialog.Builder(activity!!)
AlertDialog.Builder(requireActivity())
.setTitle(R.string.url_invalid)
.setMessage(R.string.url_parse_error)
.setPositiveButton(android.R.string.ok) { _, _ -> }
@ -63,11 +63,11 @@ class SettingsFragment : PreferenceFragmentCompat(), SettingsView {
isValid = true
else {
isValid = true
if (BiometricManager.from(activity!!).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS)
if (BiometricManager.from(requireActivity()).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS)
promptForUnlock()
else {
isValid = false
AlertDialog.Builder(activity!!)
AlertDialog.Builder(requireActivity())
.setTitle(R.string.set_lock_title)
.setMessage(R.string.set_lock_message)
.setPositiveButton(android.R.string.ok) { _, _ -> }
@ -78,7 +78,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SettingsView {
}
val onClickShortcuts = Preference.OnPreferenceClickListener {
fragmentManager!!
parentFragmentManager
.beginTransaction()
.replace(R.id.content, ShortcutsFragment.newInstance())
.addToBackStack(getString(R.string.shortcuts))
@ -111,10 +111,10 @@ class SettingsFragment : PreferenceFragmentCompat(), SettingsView {
}
override fun onLocationSettingChanged() {
if (!PermissionManager.hasLocationPermissions(context!!)) {
if (!PermissionManager.checkLocationPermission(requireContext())) {
PermissionManager.requestLocationPermissions(this)
}
PermissionManager.restartLocationTracking(context!!)
PermissionManager.restartLocationTracking(requireContext())
}
override fun disableInternalConnection() {
@ -132,8 +132,8 @@ class SettingsFragment : PreferenceFragmentCompat(), SettingsView {
override fun onDisplayPreferenceDialog(preference: Preference) {
if (preference is SsidPreference) {
// check if dialog is already showing
val fm = fragmentManager
if (fm == null || fm.findFragmentByTag(SSID_DIALOG_TAG) != null) {
val fm = parentFragmentManager
if (fm.findFragmentByTag(SSID_DIALOG_TAG) != null) {
return
}
val ssidDialog = SsidDialogFragment.newInstance("connection_internal_ssids")
@ -151,8 +151,8 @@ class SettingsFragment : PreferenceFragmentCompat(), SettingsView {
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (PermissionManager.validateLocationPermissions(requestCode, permissions, grantResults)) {
PermissionManager.restartLocationTracking(context!!)
if (PermissionManager.validateLocationPermissions(requestCode, grantResults)) {
PermissionManager.restartLocationTracking(requireContext())
} else {
// If we don't have permissions, don't let them in!
findPreference<SwitchPreference>("location_zone")!!.isChecked = false
@ -161,9 +161,9 @@ class SettingsFragment : PreferenceFragmentCompat(), SettingsView {
}
private fun promptForUnlock() {
val executor = ContextCompat.getMainExecutor(activity!!)
val executor = ContextCompat.getMainExecutor(requireActivity())
val switchLock = findPreference<SwitchPreference>("app_lock")
val biometricPrompt = BiometricPrompt(activity!!, executor,
val biometricPrompt = BiometricPrompt(requireActivity(), executor,
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(
errorCode: Int,
@ -179,8 +179,8 @@ class SettingsFragment : PreferenceFragmentCompat(), SettingsView {
}
})
val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(activity!!.resources.getString(R.string.biometric_title))
.setSubtitle(activity!!.resources.getString(R.string.biometric_message))
.setTitle(requireActivity().resources.getString(R.string.biometric_title))
.setSubtitle(requireActivity().resources.getString(R.string.biometric_message))
.setDeviceCredentialAllowed(true)
.build()

View file

@ -55,23 +55,23 @@ class ShortcutsFragment : Fragment(), ShortcutsView {
!panel.title.isNullOrEmpty() && panel.title !== "lovelace" && panel.title !== "profile"
}.sortedBy { panel -> panel.title }.map { panel ->
panel.title_localized = when (panel.title) {
"calendar" -> context!!.getString(R.string.calendar)
"config" -> context!!.getString(R.string.config)
"developer_tools" -> context!!.getString(R.string.developer_tools)
"history" -> context!!.getString(R.string.history)
"logbook" -> context!!.getString(R.string.logbook)
"mailbox" -> context!!.getString(R.string.mailbox)
"map" -> context!!.getString(R.string.map)
"profile" -> context!!.getString(R.string.profile)
"shopping_list" -> context!!.getString(R.string.shopping_list)
"states" -> context!!.getString(R.string.states)
"calendar" -> requireContext().getString(R.string.calendar)
"config" -> requireContext().getString(R.string.config)
"developer_tools" -> requireContext().getString(R.string.developer_tools)
"history" -> requireContext().getString(R.string.history)
"logbook" -> requireContext().getString(R.string.logbook)
"mailbox" -> requireContext().getString(R.string.mailbox)
"map" -> requireContext().getString(R.string.map)
"profile" -> requireContext().getString(R.string.profile)
"shopping_list" -> requireContext().getString(R.string.shopping_list)
"states" -> requireContext().getString(R.string.states)
else -> panel.title
}
panel
}
recyclerViewAdapter =
ShortcutsRecyclerViewAdapter(panels.toList(), context!!) { onCreateShortcut(it) }
ShortcutsRecyclerViewAdapter(panels.toList(), requireContext()) { onCreateShortcut(it) }
return inflater.inflate(R.layout.fragment_shortcuts, container, false).apply {
findViewById<RecyclerView>(R.id.recycler_view_shortcuts)?.apply {
@ -82,7 +82,7 @@ class ShortcutsFragment : Fragment(), ShortcutsView {
private fun onCreateShortcut(panel: Panel) {
val shortcutManager =
context!!.getSystemService(ShortcutManager::class.java)
requireContext().getSystemService(ShortcutManager::class.java)
if (shortcutManager!!.isRequestPinShortcutSupported) {
val pinShortcutInfo =
ShortcutInfo.Builder(
@ -99,7 +99,7 @@ class ShortcutsFragment : Fragment(), ShortcutsView {
)
.setIntent(
WebViewActivity.newInstance(
context!!,
requireContext(),
panel.url_path
).apply {
this.action = Intent.ACTION_VIEW

View file

@ -31,7 +31,7 @@ class PermissionManager {
/**
* Check if the required location permissions are granted
*/
fun hasLocationPermissions(context: Context): Boolean {
fun checkLocationPermission(context: Context): Boolean {
for (permission in getLocationPermissionArray()) {
if (!hasPermission(context, permission)) {
return false
@ -54,7 +54,6 @@ class PermissionManager {
fun validateLocationPermissions(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
): Boolean {
return requestCode == LOCATION_REQUEST_CODE && arePermissionsGranted(grantResults)

View file

@ -28,6 +28,8 @@ allprojects {
maven(url = Config.Repository.lokalize)
}
apply(plugin = "org.jlleitschuh.gradle.ktlint")
tasks.withType<KotlinCompile>().configureEach {
println("Configuring $name in project ${project.name}...")
kotlinOptions {

View file

@ -2,7 +2,7 @@ object Config {
const val version = "1.9.0"
object Plugin {
const val android = "com.android.tools.build:gradle:3.6.3"
const val android = "com.android.tools.build:gradle:4.0.0"
const val kotlin = "org.jetbrains.kotlin:kotlin-gradle-plugin:${Dependency.Kotlin.version}"
const val google = "com.google.gms:google-services:4.3.3"
const val appDistribution = "com.google.firebase:firebase-appdistribution-gradle:1.4.0"

View file

@ -20,7 +20,7 @@ dependencies {
implementation(Config.Dependency.Kotlin.coroutines)
implementation(Config.Dependency.Google.dagger)
kapt (Config.Dependency.Google.daggerCompiler)
kapt(Config.Dependency.Google.daggerCompiler)
implementation(Config.Dependency.Square.retrofit)
}