Skip to content

Commit 8baf189

Browse files
committed
chore(kotlin): apply Kotlin 2 lint fixes and remove dead code
Migrates constructor parameter annotations to @param: targets, switches unused catch variables to _, replaces edit().apply() chains with the KTX edit {} builder, and drops now-redundant null checks where Kotlin 2's stricter platform-type inference made them unreachable. Also removes the unused BaseRecyclerViewAdapterWithClickHandler implementation.
1 parent 8a28801 commit 8baf189

17 files changed

Lines changed: 148 additions & 191 deletions

File tree

project/app/src/androidTest/kotlin/org/owntracks/android/testutils/BottomSheetSetStateAction.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import org.hamcrest.Matchers
1414
* @property desiredState
1515
* @constructor Create empty Bottom sheet set state action
1616
*/
17-
class BottomSheetSetStateAction(@BottomSheetBehavior.State private val desiredState: Int) :
17+
class BottomSheetSetStateAction(@param:BottomSheetBehavior.State private val desiredState: Int) :
1818
ViewAction {
1919

2020
override fun getConstraints(): Matcher<View> {

project/app/src/main/java/org/owntracks/android/data/waypoints/RoomWaypointsRepo.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ import timber.log.Timber
3636
class RoomWaypointsRepo
3737
@Inject
3838
constructor(
39-
@ApplicationContext private val applicationContext: Context,
40-
@CoroutineScopes.IoDispatcher private val ioDispatcher: CoroutineDispatcher,
41-
@ApplicationScope private val scope: CoroutineScope,
42-
@Named("waypointsMigrationIdlingResource")
39+
@param:ApplicationContext private val applicationContext: Context,
40+
@param:CoroutineScopes.IoDispatcher private val ioDispatcher: CoroutineDispatcher,
41+
@param:ApplicationScope private val scope: CoroutineScope,
42+
@param:Named("waypointsMigrationIdlingResource")
4343
private val migrationIdlingResource: SimpleIdlingResource
4444
) : WaypointsRepo(applicationContext, migrationIdlingResource) {
4545
@Dao

project/app/src/main/java/org/owntracks/android/net/http/HttpMessageProcessorEndpoint.kt

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ import org.owntracks.android.support.interfaces.ConfigurationIncompleteException
3838
import timber.log.Timber
3939

4040
class HttpMessageProcessorEndpoint(
41-
messageProcessor: MessageProcessor,
42-
private val parser: Parser,
43-
private val preferences: Preferences,
44-
private val applicationContext: Context,
45-
private val endpointStateRepo: EndpointStateRepo,
46-
private val caKeyStore: KeyStore,
47-
@ApplicationScope private val scope: CoroutineScope,
48-
@CoroutineScopes.IoDispatcher private val ioDispatcher: CoroutineDispatcher
41+
messageProcessor: MessageProcessor,
42+
private val parser: Parser,
43+
private val preferences: Preferences,
44+
private val applicationContext: Context,
45+
private val endpointStateRepo: EndpointStateRepo,
46+
private val caKeyStore: KeyStore,
47+
@param:ApplicationScope private val scope: CoroutineScope,
48+
@param:CoroutineScopes.IoDispatcher private val ioDispatcher: CoroutineDispatcher
4949
) : MessageProcessorEndpoint(messageProcessor), Preferences.OnPreferenceChangeListener {
5050
override val modeId: ConnectionMode = ConnectionMode.HTTP
5151
internal var httpClientAndConfiguration: HttpClientAndConfiguration? = null
@@ -132,44 +132,35 @@ class HttpMessageProcessorEndpoint(
132132
messageProcessor.onMessageDeliveryFailed(message)
133133
Result.failure(OutgoingMessageSendingException(httpException))
134134
} else {
135-
if (response.body != null) {
136-
try {
137-
val responseString = response.body!!.string()
138-
Timber.d("HTTP response body: ${responseString.take(1000)}")
139-
val responseStream = ByteArrayInputStream(responseString.toByteArray())
140-
val result = parser.fromJson(responseStream)
141-
// TODO apply i18n here
142-
scope.launch {
143-
endpointStateRepo.setState(
144-
EndpointState.IDLE.withMessage(
145-
String.format(
146-
Locale.ROOT,
147-
"Response %d, (%d msgs received)",
148-
response.code,
149-
result.size,
150-
),
151-
),
152-
)
153-
result.forEach { onMessageReceived(it) }
154-
}
155-
return Result.success(Unit)
156-
} catch (e: IOException) {
157-
Timber.w(e, "HTTP response body could not be parsed, ignoring")
135+
try {
136+
val responseString = response.body.string()
137+
Timber.d("HTTP response body: ${responseString.take(1000)}")
138+
val responseStream = ByteArrayInputStream(responseString.toByteArray())
139+
val result = parser.fromJson(responseStream)
140+
// TODO apply i18n here
141+
scope.launch {
158142
endpointStateRepo.setState(
159143
EndpointState.IDLE.withMessage(
160144
String.format(
161-
Locale.ROOT, "Response %d (response not parseable)", response.code),
145+
Locale.ROOT,
146+
"Response %d, (%d msgs received)",
147+
response.code,
148+
result.size,
149+
),
162150
),
163151
)
164-
return Result.success(Unit)
152+
result.forEach { onMessageReceived(it) }
165153
}
166-
} else {
154+
return Result.success(Unit)
155+
} catch (e: IOException) {
156+
Timber.w(e, "HTTP response body could not be parsed, ignoring")
167157
endpointStateRepo.setState(
168158
EndpointState.IDLE.withMessage(
169-
String.format(Locale.ROOT, "Response %d", response.code),
159+
String.format(
160+
Locale.ROOT, "Response %d (response not parseable)", response.code),
170161
),
171162
)
172-
Result.success(Unit)
163+
return Result.success(Unit)
173164
}
174165
}
175166
}

project/app/src/main/java/org/owntracks/android/net/mqtt/MQTTMessageProcessorEndpoint.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ import org.owntracks.android.test.SimpleIdlingResource
6464
import timber.log.Timber
6565

6666
class MQTTMessageProcessorEndpoint(
67-
messageProcessor: MessageProcessor,
68-
private val endpointStateRepo: EndpointStateRepo,
69-
private val scheduler: Scheduler,
70-
private val preferences: Preferences,
71-
private val parser: Parser,
72-
private val caKeyStore: KeyStore,
73-
@ApplicationScope private val scope: CoroutineScope,
74-
@CoroutineScopes.IoDispatcher private val ioDispatcher: CoroutineDispatcher,
75-
@ApplicationContext private val applicationContext: Context,
76-
private val mqttConnectionIdlingResource: SimpleIdlingResource
67+
messageProcessor: MessageProcessor,
68+
private val endpointStateRepo: EndpointStateRepo,
69+
private val scheduler: Scheduler,
70+
private val preferences: Preferences,
71+
private val parser: Parser,
72+
private val caKeyStore: KeyStore,
73+
@param:ApplicationScope private val scope: CoroutineScope,
74+
@param:CoroutineScopes.IoDispatcher private val ioDispatcher: CoroutineDispatcher,
75+
@param:ApplicationContext private val applicationContext: Context,
76+
private val mqttConnectionIdlingResource: SimpleIdlingResource
7777
) :
7878
MessageProcessorEndpoint(messageProcessor),
7979
StatefulServiceMessageProcessor,
@@ -167,7 +167,7 @@ class MQTTMessageProcessorEndpoint(
167167
try {
168168
pingAlarmReceiver?.run(applicationContext::unregisterReceiver)
169169
Timber.d("Unregistered ping alarm receiver")
170-
} catch (e: IllegalArgumentException) {
170+
} catch (_: IllegalArgumentException) {
171171
Timber.d("Ping alarm receiver already unregistered")
172172
}
173173
}
@@ -316,9 +316,9 @@ class MQTTMessageProcessorEndpoint(
316316
this.qos = message.qos
317317
}
318318
.also { Timber.d("Parsed message: $it") })
319-
} catch (e: Parser.EncryptionException) {
319+
} catch (_: Parser.EncryptionException) {
320320
Timber.e("Unable to decrypt received message ${message.id} on $topic")
321-
} catch (e: SerializationException) {
321+
} catch (_: SerializationException) {
322322
Timber.w("Malformed JSON message received ${message.id} on $topic")
323323
}
324324
}

project/app/src/main/java/org/owntracks/android/preferences/Preferences.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import timber.log.Timber
3434
class Preferences
3535
@Inject
3636
constructor(
37-
private val preferencesStore: PreferencesStore,
38-
@Named("importConfigurationIdlingResource")
37+
private val preferencesStore: PreferencesStore,
38+
@param:Named("importConfigurationIdlingResource")
3939
private val importConfigurationIdlingResource: SimpleIdlingResource
4040
) {
4141
val allConfigKeys =
@@ -111,7 +111,7 @@ constructor(
111111
// preference is
112112
// actually an enum
113113
importPreference(it, configValue)
114-
} catch (e: java.lang.IllegalArgumentException) {
114+
} catch (_: java.lang.IllegalArgumentException) {
115115
Timber.w(
116116
"Trying to import wrong type of preference for ${it.name}. " +
117117
"Expected ${it.getter.returnType} but given ${configValue.javaClass}. Ignoring.")
@@ -129,7 +129,7 @@ constructor(
129129
Timber.d("Importing configuration key $key -> $configValue")
130130
try {
131131
importPreference(property, configValue)
132-
} catch (e: java.lang.IllegalArgumentException) {
132+
} catch (_: java.lang.IllegalArgumentException) {
133133
Timber.w(
134134
"Trying to import wrong type of preference for $key. " +
135135
"Expected ${property.getter.returnType} but given ${configValue.javaClass}. Ignoring.")

project/app/src/main/java/org/owntracks/android/preferences/SharedPreferencesStore.kt

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import timber.log.Timber
2828
class SharedPreferencesStore
2929
@Inject
3030
constructor(
31-
@ApplicationContext private val context: Context,
32-
private val notificationManager: NotificationManagerCompat,
33-
private val notificationStash: NotificationsStash
31+
@param:ApplicationContext private val context: Context,
32+
private val notificationManager: NotificationManagerCompat,
33+
private val notificationStash: NotificationsStash
3434
) : PreferencesStore() {
3535

3636
private val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
@@ -53,16 +53,17 @@ constructor(
5353

5454
if (shouldNotify) {
5555
// Delete local files
56-
setOf("tlsClientCrt", "tlsCaCrt").forEach {
56+
setOf("tlsClientCrt", "tlsCaCrt").forEach { preference ->
5757
sharedPreferences
58-
.getString(it, "")
58+
.getString(preference, "")
5959
.also { Timber.i("Deleting legacy cert from app storage: $it") }
6060
.run(context::deleteFile)
6161
}
6262
// Notify user
6363
NotificationCompat.Builder(context, GeocoderProvider.ERROR_NOTIFICATION_CHANNEL_ID)
6464
.setContentTitle(
65-
context.getString(R.string.certificateMigrationRequiredNotificationTitle))
65+
context.getString(R.string.certificateMigrationRequiredNotificationTitle),
66+
)
6667
.setAutoCancel(true)
6768
.setSmallIcon(R.drawable.ic_owntracks_80)
6869
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
@@ -75,24 +76,30 @@ constructor(
7576
Intent(context, PreferencesActivity::class.java)
7677
.addFlags(FLAG_ACTIVITY_NEW_TASK)
7778
.putExtra(START_FRAGMENT_KEY, ConnectionFragment::class.java.name),
78-
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT))
79+
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
80+
),
81+
)
7982
.setStyle(
8083
NotificationCompat.BigTextStyle()
8184
.bigText(
82-
context.getString(R.string.certificateMigrationRequiredNotificationText)))
85+
context.getString(R.string.certificateMigrationRequiredNotificationText),
86+
),
87+
)
8388
.setSilent(true)
8489
.build()
8590
.run {
8691
if (ActivityCompat.checkSelfPermission(
87-
context, Manifest.permission.POST_NOTIFICATIONS) ==
92+
context, Manifest.permission.POST_NOTIFICATIONS,
93+
) ==
8894
PackageManager.PERMISSION_GRANTED) {
8995
notificationManager.notify("CertificateManagementNotification", 0, this).also {
9096
Timber.d("Notifying user of certificate migration")
9197
}
9298
} else {
9399
notificationStash.add(this).also {
94100
Timber.d(
95-
"Notifying user of certificate migration: delaying until user grants notification permissions")
101+
"Notifying user of certificate migration: delaying until user grants notification permissions",
102+
)
96103
}
97104
}
98105
}
@@ -105,7 +112,8 @@ constructor(
105112
private fun migrateToSingleSharedPreferences() {
106113
val oldSharedPreferenceNames =
107114
listOf(
108-
"org.owntracks.android.preferences.private", "org.owntracks.android.preferences.http")
115+
"org.owntracks.android.preferences.private", "org.owntracks.android.preferences.http",
116+
)
109117
with(sharedPreferences.edit()) {
110118
if (sharedPreferences.contains("setupNotCompleted")) {
111119
val oldValue = sharedPreferences.getBoolean("setupNotCompleted", true)
@@ -135,7 +143,7 @@ constructor(
135143
/* Running edit / clear / apply will actually create the preference file, which we don't want to do
136144
if they didn't exist in the first place */
137145
oldSharedPreferenceNames.forEach {
138-
context.getSharedPreferences(it, Context.MODE_PRIVATE).edit().clear().apply()
146+
context.getSharedPreferences(it, Context.MODE_PRIVATE).edit { clear() }
139147
}
140148
}
141149
oldSharedPreferenceNames.forEach {
@@ -154,15 +162,15 @@ constructor(
154162
sharedPreferences.run {
155163
if (contains(key) && getString(key, "") == value) false
156164
else {
157-
edit().putString(key, value).apply()
165+
edit {putString(key, value).apply()}
158166
true
159167
}
160168
}
161169

162170
override fun getString(key: String, default: String): String? =
163171
sharedPreferences.getString(key, default)
164172

165-
override fun remove(key: String) = sharedPreferences.edit().remove(key).apply()
173+
override fun remove(key: String) = sharedPreferences.edit { remove(key) }
166174

167175
override fun getBoolean(key: String, default: Boolean): Boolean =
168176
sharedPreferences.getBoolean(key, default)
@@ -173,7 +181,7 @@ constructor(
173181
sharedPreferences.run {
174182
if (contains(key) && getBoolean(key, false) == value) false
175183
else {
176-
edit().putBoolean(key, value).apply()
184+
edit {putBoolean(key, value).apply()}
177185
true
178186
}
179187
}
@@ -184,7 +192,7 @@ constructor(
184192
sharedPreferences.run {
185193
if (contains(key) && getFloat(key, Float.MIN_VALUE) == value) false
186194
else {
187-
edit().putFloat(key, value).apply()
195+
edit { putFloat(key, value).apply() }
188196
true
189197
}
190198
}
@@ -196,7 +204,7 @@ constructor(
196204
sharedPreferences.run {
197205
if (contains(key) && getInt(key, Int.MIN_VALUE) == value) false
198206
else {
199-
edit().putInt(key, value).apply()
207+
edit { putInt(key, value).apply() }
200208
true
201209
}
202210
}
@@ -205,7 +213,7 @@ constructor(
205213
sharedPreferences.run {
206214
if (contains(key) && getStringSet(key, emptySet()) == values) false
207215
else {
208-
edit().putStringSet(key, values).apply()
216+
edit { putStringSet(key, values).apply() }
209217
true
210218
}
211219
}

project/app/src/main/java/org/owntracks/android/services/BackgroundService.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,8 +742,7 @@ class BackgroundService : LifecycleService(), Preferences.OnPreferenceChangeList
742742
} else {
743743
""
744744
} +
745-
"isInteractive=${powerManager.isInteractive} " +
746-
"isIgnoringBatteryOptimizations=${powerManager.isIgnoringBatteryOptimizations(applicationContext.packageName)}")
745+
"isInteractive=${powerManager.isInteractive} isIgnoringBatteryOptimizations=${powerManager.isIgnoringBatteryOptimizations(applicationContext.packageName)}")
747746
}
748747
}
749748
}

project/app/src/main/java/org/owntracks/android/services/LocationProcessor.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ import timber.log.Timber
3838
class LocationProcessor
3939
@Inject
4040
constructor(
41-
private val messageProcessor: MessageProcessor,
42-
private val preferences: Preferences,
43-
private val locationRepo: LocationRepo,
44-
private val waypointsRepo: WaypointsRepo,
45-
private val deviceMetricsProvider: DeviceMetricsProvider,
46-
private val wifiInfoProvider: WifiInfoProvider,
47-
@ApplicationScope private val scope: CoroutineScope,
48-
@CoroutineScopes.IoDispatcher private val ioDispatcher: CoroutineDispatcher,
49-
@Named("publishResponseMessageIdlingResource")
41+
private val messageProcessor: MessageProcessor,
42+
private val preferences: Preferences,
43+
private val locationRepo: LocationRepo,
44+
private val waypointsRepo: WaypointsRepo,
45+
private val deviceMetricsProvider: DeviceMetricsProvider,
46+
private val wifiInfoProvider: WifiInfoProvider,
47+
@param:ApplicationScope private val scope: CoroutineScope,
48+
@param:CoroutineScopes.IoDispatcher private val ioDispatcher: CoroutineDispatcher,
49+
@param:Named("publishResponseMessageIdlingResource")
5050
private val publishResponseMessageIdlingResource: SimpleIdlingResource,
51-
@Named("mockLocationIdlingResource")
51+
@param:Named("mockLocationIdlingResource")
5252
private val mockLocationIdlingResource: SimpleIdlingResource
5353
) {
5454
private fun locationIsWithAccuracyThreshold(l: Location): Boolean =

0 commit comments

Comments
 (0)