Skip to content

Commit

Permalink
Use gradle plugin for ktlint
Browse files Browse the repository at this point in the history
and auto fix format issues.

Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed Apr 14, 2024
1 parent 19b00a4 commit b959515
Show file tree
Hide file tree
Showing 69 changed files with 865 additions and 580 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ trim_trailing_whitespace = true
insert_final_newline = true

[*.{java,kt,xml}]
max_line_length = 120
max_line_length = 140
ktlint_code_style = android_studio

[*.yml]
indent_size = 2
2 changes: 0 additions & 2 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions mobile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ buildscript {
classpath 'com.github.bjoernq:unmockplugin:0.7.9'
classpath "com.mikepenz.aboutlibraries.plugin:aboutlibraries-plugin:$about_libraries_version"
classpath "de.jjohannes.gradle:missing-metadata-guava:31.1.1"
classpath "org.jlleitschuh.gradle:ktlint-gradle:12.1.0"
}
}

Expand All @@ -34,6 +35,7 @@ apply plugin: "kotlin-parcelize"
apply plugin: 'de.mobilej.unmock'
apply plugin: "com.mikepenz.aboutlibraries.plugin"
apply plugin: "de.jjohannes.missing-metadata-guava"
apply plugin: "org.jlleitschuh.gradle.ktlint"
if (!isFoss) {
apply plugin: "com.google.gms.google-services"
apply plugin: "com.google.firebase.crashlytics"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ object CloudMessagingHelper {

fun isPollingBuild() = true

fun needsPollingForNotifications(context: Context) =
context.getPrefs().getBoolean(PrefKeys.FOSS_NOTIFICATIONS_ENABLED, false)
fun needsPollingForNotifications(context: Context) = context.getPrefs().getBoolean(PrefKeys.FOSS_NOTIFICATIONS_ENABLED, false)

suspend fun pollForNotifications(context: Context) {
NotificationPoller.checkForNewNotifications(context)
Expand Down
20 changes: 11 additions & 9 deletions mobile/src/foss/java/org/openhab/habdroid/ui/MapViewHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,18 @@ object MapViewHelper {
setMultiTouchControls(false)
setDestroyMode(false)
overlays.add(CopyrightOverlay(itemView.context))
overlays.add(MapEventsOverlay(object : MapEventsReceiver {
override fun singleTapConfirmedHelper(p: GeoPoint): Boolean {
openPopup()
return true
}
overlays.add(
MapEventsOverlay(object : MapEventsReceiver {
override fun singleTapConfirmedHelper(p: GeoPoint): Boolean {
openPopup()
return true
}

override fun longPressHelper(p: GeoPoint): Boolean {
return false
}
}))
override fun longPressHelper(p: GeoPoint): Boolean {
return false
}
})
)
mapOverlay.setColorFilter(if (context.isDarkModeActive()) TilesOverlay.INVERT_COLORS else null)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ object CloudMessagingHelper {

fun onNotificationSelected(context: Context, intent: Intent) {
val notificationId = intent.getIntExtra(
NotificationHelper.EXTRA_NOTIFICATION_ID, -1)
NotificationHelper.EXTRA_NOTIFICATION_ID,
-1
)
if (notificationId >= 0) {
FcmRegistrationWorker.scheduleHideNotification(context, notificationId)
}
Expand Down Expand Up @@ -72,7 +74,8 @@ object CloudMessagingHelper {
)
// Cloud connection failed
cloudFailure != null && cloudFailure !is NotACloudServerException -> {
val message = context.getString(R.string.push_notification_status_http_error,
val message = context.getString(
R.string.push_notification_status_http_error,
context.getHumanReadableErrorMessage(
if (cloudFailure is HttpClient.HttpException) cloudFailure.originalUrl else "",
if (cloudFailure is HttpClient.HttpException) cloudFailure.statusCode else 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete
val manufacturer = Build.MANUFACTURER
val model = Build.MODEL

val actualModel = if (model.lowercase(Locale.ROOT).startsWith(manufacturer.lowercase(Locale.ROOT)))
model else "$manufacturer $model"
val actualModel = if (model.lowercase(Locale.ROOT).startsWith(manufacturer.lowercase(Locale.ROOT))) {
model
} else {
"$manufacturer $model"
}

// Capitalize returned value
val first = actualModel.elementAtOrNull(0)
Expand Down Expand Up @@ -103,7 +106,6 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete
sendHideNotificationRequest(id, connection.messagingSenderId)
return Result.success()
}

}
else -> Log.e(TAG, "Invalid action '$action'")
}
Expand All @@ -119,15 +121,21 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete
// HttpException is thrown by our HTTP code, IOException can be thrown by FCM
@Throws(HttpClient.HttpException::class, IOException::class)
private suspend fun registerFcm(connection: CloudConnection) {
val token = FirebaseInstanceId.getInstance().getToken(connection.messagingSenderId,
FirebaseMessaging.INSTANCE_ID_SCOPE)
val token = FirebaseInstanceId.getInstance().getToken(
connection.messagingSenderId,
FirebaseMessaging.INSTANCE_ID_SCOPE
)
val deviceName = deviceName + if (Util.isFlavorBeta) " (${context.getString(R.string.beta)})" else ""
val deviceId = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID) +
if (Util.isFlavorBeta) "-beta" else ""
if (Util.isFlavorBeta) "-beta" else ""

val regUrl = String.format(Locale.US,
"addAndroidRegistration?deviceId=%s&deviceModel=%s&regId=%s",
deviceId, URLEncoder.encode(deviceName, "UTF-8"), token)
val regUrl = String.format(
Locale.US,
"addAndroidRegistration?deviceId=%s&deviceModel=%s&regId=%s",
deviceId,
URLEncoder.encode(deviceName, "UTF-8"),
token
)

Log.d(TAG, "Register device at openHAB-cloud with URL: $regUrl")
connection.httpClient.get(regUrl).close()
Expand All @@ -137,9 +145,9 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete
private fun sendHideNotificationRequest(notificationId: Int, senderId: String) {
val fcm = FirebaseMessaging.getInstance()
val message = RemoteMessage.Builder("$senderId@gcm.googleapis.com")
.addData("type", "hideNotification")
.addData("notificationId", notificationId.toString())
.build()
.addData("type", "hideNotification")
.addData("notificationId", notificationId.toString())
.build()
fcm.send(message)
}

Expand All @@ -158,7 +166,7 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete
companion object {
internal fun wrap(context: Context, intent: Intent, id: Int): PendingIntent {
val wrapped = Intent(context, ProxyReceiver::class.java)
.putExtra("intent", intent)
.putExtra("intent", intent)
return PendingIntent.getBroadcast(
context,
id,
Expand Down Expand Up @@ -209,8 +217,11 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete

val workRequest = OneTimeWorkRequest.Builder(FcmRegistrationWorker::class.java)
.setConstraints(constraints)
.setBackoffCriteria(BackoffPolicy.LINEAR,
WorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MILLISECONDS)
.setBackoffCriteria(
BackoffPolicy.LINEAR,
WorkRequest.MIN_BACKOFF_MILLIS,
TimeUnit.MILLISECONDS
)
.setInputData(data)
.build()

Expand Down
6 changes: 3 additions & 3 deletions mobile/src/full/java/org/openhab/habdroid/ui/MapViewHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ fun GoogleMap.applyPositionAndLabel(widget: Widget, zoomLevel: Float, allowDrag:

fun GoogleMap.setMarker(position: LatLng, item: Item, label: CharSequence?, canDrag: Boolean) {
val marker = MarkerOptions()
.draggable(canDrag)
.position(position)
.title(label?.toString())
.draggable(canDrag)
.position(position)
.title(label?.toString())
addMarker(marker)?.tag = item
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ class BackgroundTasksManager : BroadcastReceiver() {
PrefKeys.SEND_WIFI_SSID,
PrefKeys.SEND_DND_MODE
)

// These package names must be added to the manifest as well
private val IGNORED_PACKAGES_FOR_ALARM = listOf(
"net.dinglisch.android.taskerm",
Expand Down Expand Up @@ -337,7 +338,8 @@ class BackgroundTasksManager : BroadcastReceiver() {
// These broadcasts are already defined in the manifest, so we only need them on Android 8+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (prefs.isItemUpdatePrefEnabled(PrefKeys.SEND_BATTERY_LEVEL) ||
prefs.isItemUpdatePrefEnabled(PrefKeys.SEND_CHARGING_STATE)) {
prefs.isItemUpdatePrefEnabled(PrefKeys.SEND_CHARGING_STATE)
) {
addAction(Intent.ACTION_POWER_CONNECTED)
addAction(Intent.ACTION_POWER_DISCONNECTED)
addAction(Intent.ACTION_BATTERY_LOW)
Expand All @@ -354,7 +356,8 @@ class BackgroundTasksManager : BroadcastReceiver() {
}
// This broadcast is only sent to registered receivers, so we need that in any case
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
prefs.isItemUpdatePrefEnabled(PrefKeys.SEND_DND_MODE)) {
prefs.isItemUpdatePrefEnabled(PrefKeys.SEND_DND_MODE)
) {
addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED)
}
}
Expand Down Expand Up @@ -494,12 +497,19 @@ class BackgroundTasksManager : BroadcastReceiver() {
PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS
)

Log.d(TAG, "Scheduling periodic workers with $repeatInterval repeat interval. Currently running:" +
" notCharging $isNotChargingWorkerRunning, charging $isChargingWorkerRunning")
Log.d(
TAG,
"Scheduling periodic workers with $repeatInterval repeat interval. Currently running:" +
" notCharging $isNotChargingWorkerRunning, charging $isChargingWorkerRunning"
)

val notChargingWorkRequest = PeriodicWorkRequest.Builder(PeriodicItemUpdateWorker::class.java,
repeatInterval, TimeUnit.MILLISECONDS,
flexInterval, TimeUnit.MILLISECONDS)
val notChargingWorkRequest = PeriodicWorkRequest.Builder(
PeriodicItemUpdateWorker::class.java,
repeatInterval,
TimeUnit.MILLISECONDS,
flexInterval,
TimeUnit.MILLISECONDS
)
.setConstraints(Constraints(requiredNetworkType = NetworkType.CONNECTED))
.addTag(WORKER_TAG_PERIODIC_TRIGGER)
.addTag(WORKER_TAG_PERIODIC_TRIGGER_NOT_CHARGING)
Expand All @@ -511,9 +521,13 @@ class BackgroundTasksManager : BroadcastReceiver() {
requiresCharging = true
)

val chargingWorkRequest = PeriodicWorkRequest.Builder(PeriodicItemUpdateWorker::class.java,
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS, TimeUnit.MILLISECONDS,
PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS, TimeUnit.MILLISECONDS)
val chargingWorkRequest = PeriodicWorkRequest.Builder(
PeriodicItemUpdateWorker::class.java,
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS,
TimeUnit.MILLISECONDS,
PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS,
TimeUnit.MILLISECONDS
)
.setConstraints(chargingConstraints)
.addTag(WORKER_TAG_PERIODIC_TRIGGER)
.addTag(WORKER_TAG_PERIODIC_TRIGGER_CHARGING)
Expand Down Expand Up @@ -661,8 +675,11 @@ class BackgroundTasksManager : BroadcastReceiver() {
)
val workRequest = OneTimeWorkRequest.Builder(ItemUpdateWorker::class.java)
.setConstraints(constraints)
.setBackoffCriteria(if (isImportant) BackoffPolicy.LINEAR else BackoffPolicy.EXPONENTIAL,
WorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MILLISECONDS)
.setBackoffCriteria(
if (isImportant) BackoffPolicy.LINEAR else BackoffPolicy.EXPONENTIAL,
WorkRequest.MIN_BACKOFF_MILLIS,
TimeUnit.MILLISECONDS
)
.addTag(primaryTag)
.addTag(WORKER_TAG_ITEM_UPLOADS)
.addTag(
Expand Down Expand Up @@ -757,7 +774,8 @@ class BackgroundTasksManager : BroadcastReceiver() {
val plugged = batteryStatus?.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)
Log.d(TAG, "EXTRA_STATUS is $status, EXTRA_PLUGGED is $plugged")
val state = if (status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL) {
status == BatteryManager.BATTERY_STATUS_FULL
) {
when (plugged) {
BatteryManager.BATTERY_PLUGGED_USB -> "USB"
BatteryManager.BATTERY_PLUGGED_AC -> "AC"
Expand All @@ -774,7 +792,9 @@ class BackgroundTasksManager : BroadcastReceiver() {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val requiredPermissions = getRequiredPermissionsForTask(PrefKeys.SEND_WIFI_SSID)
// TODO: Replace deprecated function
@Suppress("DEPRECATION") val ssidToSend = wifiManager.connectionInfo.let { info ->

@Suppress("DEPRECATION")
val ssidToSend = wifiManager.connectionInfo.let { info ->
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
!LocationManagerCompat.isLocationEnabled(locationManager) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,18 @@ class EventListenerService : Service() {
getString(R.string.send_device_info_foreground_service_running_summary_one, titlesOfItems[0])
}
titlesOfItems.size == 2 -> {
getString(R.string.send_device_info_foreground_service_running_summary_two,
titlesOfItems[0], titlesOfItems[1])
getString(
R.string.send_device_info_foreground_service_running_summary_two,
titlesOfItems[0],
titlesOfItems[1]
)
}
else -> {
getString(R.string.send_device_info_foreground_service_running_summary_more,
titlesOfItems[0], titlesOfItems[1])
getString(
R.string.send_device_info_foreground_service_running_summary_more,
titlesOfItems[0],
titlesOfItems[1]
)
}
}
val title = getString(R.string.send_device_info_foreground_service_title)
Expand Down Expand Up @@ -105,6 +111,7 @@ class EventListenerService : Service() {

companion object {
private val TAG = EventListenerService::class.java.simpleName

@VisibleForTesting val KNOWN_EVENT_LISTENER_KEYS = listOf(
PrefKeys.SEND_BATTERY_LEVEL,
PrefKeys.SEND_CHARGING_STATE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,23 +292,23 @@ class ItemUpdateWorker(context: Context, params: WorkerParameters) : CoroutineWo
value: String,
mappedValue: String
): String = when (value) {
"ON" -> context.getString(R.string.item_update_success_message_on, label)
"OFF" -> context.getString(R.string.item_update_success_message_off, label)
"UP" -> context.getString(R.string.item_update_success_message_up, label)
"DOWN" -> context.getString(R.string.item_update_success_message_down, label)
"MOVE" -> context.getString(R.string.item_update_success_message_move, label)
"STOP" -> context.getString(R.string.item_update_success_message_stop, label)
"INCREASE" -> context.getString(R.string.item_update_success_message_increase, label)
"DECREASE" -> context.getString(R.string.item_update_success_message_decrease, label)
"UNDEF" -> context.getString(R.string.item_update_success_message_undefined, label)
"" -> context.getString(R.string.item_update_success_message_empty_string, label)
"PLAY" -> context.getString(R.string.item_update_success_message_play, label)
"PAUSE" -> context.getString(R.string.item_update_success_message_pause, label)
"NEXT" -> context.getString(R.string.item_update_success_message_next, label)
"PREVIOUS" -> context.getString(R.string.item_update_success_message_previous, label)
"REWIND" -> context.getString(R.string.item_update_success_message_rewind, label)
"FASTFORWARD" -> context.getString(R.string.item_update_success_message_fastforward, label)
else -> context.getString(R.string.item_update_success_message_generic, label, mappedValue)
"ON" -> context.getString(R.string.item_update_success_message_on, label)
"OFF" -> context.getString(R.string.item_update_success_message_off, label)
"UP" -> context.getString(R.string.item_update_success_message_up, label)
"DOWN" -> context.getString(R.string.item_update_success_message_down, label)
"MOVE" -> context.getString(R.string.item_update_success_message_move, label)
"STOP" -> context.getString(R.string.item_update_success_message_stop, label)
"INCREASE" -> context.getString(R.string.item_update_success_message_increase, label)
"DECREASE" -> context.getString(R.string.item_update_success_message_decrease, label)
"UNDEF" -> context.getString(R.string.item_update_success_message_undefined, label)
"" -> context.getString(R.string.item_update_success_message_empty_string, label)
"PLAY" -> context.getString(R.string.item_update_success_message_play, label)
"PAUSE" -> context.getString(R.string.item_update_success_message_pause, label)
"NEXT" -> context.getString(R.string.item_update_success_message_next, label)
"PREVIOUS" -> context.getString(R.string.item_update_success_message_previous, label)
"REWIND" -> context.getString(R.string.item_update_success_message_rewind, label)
"FASTFORWARD" -> context.getString(R.string.item_update_success_message_fastforward, label)
else -> context.getString(R.string.item_update_success_message_generic, label, mappedValue)
}

companion object {
Expand Down Expand Up @@ -359,10 +359,7 @@ class ItemUpdateWorker(context: Context, params: WorkerParameters) : CoroutineWo
.build()
}

fun getShortItemUpdateSuccessMessage(
context: Context,
value: String
): String = when (value) {
fun getShortItemUpdateSuccessMessage(context: Context, value: String): String = when (value) {
"ON" -> context.getString(R.string.item_update_short_success_message_on)
"OFF" -> context.getString(R.string.item_update_short_success_message_off)
"UP" -> context.getString(R.string.item_update_short_success_message_up)
Expand Down
Loading

0 comments on commit b959515

Please sign in to comment.