Skip to content

Commit

Permalink
chore: add detekt formatting rule set
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir committed Oct 14, 2024
1 parent 056d4a5 commit fe56d25
Show file tree
Hide file tree
Showing 58 changed files with 725 additions and 432 deletions.
16 changes: 9 additions & 7 deletions app/src/main/java/com/geeksville/mesh/DataPacket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ data class DataPacket(
* If this is a text message, return the string, otherwise null
*/
val text: String?
get() = if (dataType == Portnums.PortNum.TEXT_MESSAGE_APP_VALUE)
get() = if (dataType == Portnums.PortNum.TEXT_MESSAGE_APP_VALUE) {
bytes?.decodeToString()
else
} else {
null
}

constructor(to: String?, channel: Int, waypoint: MeshProtos.Waypoint) : this(
to = to,
Expand All @@ -75,10 +76,11 @@ data class DataPacket(
)

val waypoint: MeshProtos.Waypoint?
get() = if (dataType == Portnums.PortNum.WAYPOINT_APP_VALUE)
get() = if (dataType == Portnums.PortNum.WAYPOINT_APP_VALUE) {
MeshProtos.Waypoint.parseFrom(bytes)
else
} else {
null
}

// Autogenerated comparision, because we have a byte array

Expand Down Expand Up @@ -142,7 +144,7 @@ data class DataPacket(
return 0
}

/// Update our object from our parcel (used for inout parameters
// Update our object from our parcel (used for inout parameters
fun readFromParcel(parcel: Parcel) {
to = parcel.readString()
parcel.createByteArray()
Expand All @@ -164,7 +166,7 @@ data class DataPacket(
/** The Node ID for the local node - used for from when sender doesn't know our local node ID */
const val ID_LOCAL = "^local"

/// special broadcast address
// special broadcast address
const val NODENUM_BROADCAST = (0xffffffff).toInt()

// Public-key cryptography (PKC) channel index
Expand All @@ -181,4 +183,4 @@ data class DataPacket(
return arrayOfNulls(size)
}
}
}
}
36 changes: 19 additions & 17 deletions app/src/main/java/com/geeksville/mesh/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class MainActivity : AppCompatActivity(), Logging {
tab.icon = ContextCompat.getDrawable(this, tabInfos[position].icon)
}.attach()

binding.tabLayout.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener {
binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
val mainTab = tab?.position ?: 0
model.setCurrentTab(mainTab)
Expand Down Expand Up @@ -254,7 +254,7 @@ class MainActivity : AppCompatActivity(), Logging {

private var requestedChannelUrl: Uri? = null

/// Handle any itents that were passed into us
// Handle any intents that were passed into us
private fun handleIntent(intent: Intent) {
val appLinkAction = intent.action
val appLinkData: Uri? = intent.data
Expand Down Expand Up @@ -327,7 +327,7 @@ class MainActivity : AppCompatActivity(), Logging {
showSettingsPage() // Default to the settings page in this case
}

/// Called when we gain/lose a connection to our mesh radio
// Called when we gain/lose a connection to our mesh radio
private fun onMeshConnectionChanged(newConnection: MeshService.ConnectionState) {
if (newConnection == MeshService.ConnectionState.CONNECTED) {
serviceRepository.meshService?.let { service ->
Expand All @@ -336,17 +336,17 @@ class MainActivity : AppCompatActivity(), Logging {

if (info != null) {
val isOld = info.minAppVersion > BuildConfig.VERSION_CODE
if (isOld)
if (isOld) {
showAlert(R.string.app_too_old, R.string.must_update)
else {
} else {
// If we are already doing an update don't put up a dialog or try to get device info
val isUpdating = service.updateStatus >= 0
if (!isUpdating) {
val curVer = DeviceVersion(info.firmwareVersion ?: "0.0.0")

if (curVer < MeshService.minDeviceVersion)
if (curVer < MeshService.minDeviceVersion) {
showAlert(R.string.firmware_too_old, R.string.firmware_old)
else {
} else {
// If our app is too old/new, we probably don't understand the new DeviceConfig messages, so we don't read them until here

// we have a connection to our device now, do the channel change
Expand All @@ -359,8 +359,9 @@ class MainActivity : AppCompatActivity(), Logging {
warn("Abandoning connect $ex, because we probably just lost device connection")
}
// if provideLocation enabled: Start providing location (from phone GPS) to mesh
if (model.provideLocation.value == true)
if (model.provideLocation.value == true) {
service.startProvideLocation()
}
}

if (!hasNotificationPermission()) {
Expand Down Expand Up @@ -406,9 +407,9 @@ class MainActivity : AppCompatActivity(), Logging {
val channels = url.toChannelSet()
val shouldAdd = url.shouldAddChannels()
val primary = channels.primaryChannel
if (primary == null)
if (primary == null) {
showSnackbar(R.string.channel_invalid)
else {
} else {
val dialogMessage = if (!shouldAdd) {
getString(R.string.do_you_want_switch).format(primary.name)
} else {
Expand Down Expand Up @@ -651,10 +652,11 @@ class MainActivity : AppCompatActivity(), Logging {
handler.postDelayed({ postPing() }, 30000)
}
item.isChecked = !item.isChecked // toggle ping test
if (item.isChecked)
if (item.isChecked) {
postPing()
else
} else {
handler.removeCallbacksAndMessages(null)
}
return true
}
R.id.radio_config -> {
Expand Down Expand Up @@ -705,11 +707,11 @@ class MainActivity : AppCompatActivity(), Logging {
}
}

/// Theme functions
// Theme functions

private fun chooseThemeDialog() {

/// Prepare dialog and its items
// Prepare dialog and its items
val builder = MaterialAlertDialogBuilder(this)
builder.setTitle(getString(R.string.choose_theme))

Expand All @@ -719,7 +721,7 @@ class MainActivity : AppCompatActivity(), Logging {
getString(R.string.theme_system) to AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
)

/// Load preferences and its value
// Load preferences and its value
val prefs = UIViewModel.getPreferences(this)
val theme = prefs.getInt("theme", AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
debug("Theme from prefs: $theme")
Expand All @@ -740,13 +742,13 @@ class MainActivity : AppCompatActivity(), Logging {
}

private fun chooseLangDialog() {
/// Prepare dialog and its items
// Prepare dialog and its items
val builder = MaterialAlertDialogBuilder(this)
builder.setTitle(getString(R.string.preferences_language))

val languageTags = LanguageUtils.getLanguageTags(this)

/// Load preferences and its value
// Load preferences and its value
val lang = LanguageUtils.getLocale()
debug("Lang from prefs: $lang")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ fun Context.hasLocationPermission() = getLocationPermissions().isEmpty()
*/
fun Context.getNotificationPermissions(): Array<String> {
val perms = mutableListOf<String>()
if (android.os.Build.VERSION.SDK_INT >= 33)
if (android.os.Build.VERSION.SDK_INT >= 33) {
perms.add(Manifest.permission.POST_NOTIFICATIONS)
}

return getMissingPermissions(perms)
}
Expand Down
17 changes: 9 additions & 8 deletions app/src/main/java/com/geeksville/mesh/android/ServiceClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ open class ServiceClient<T : IInterface>(private val stubFactory: (IBinder) -> T

var serviceP: T? = null

/// A getter that returns the bound service or throws if not bound
// A getter that returns the bound service or throws if not bound
val service: T
get() {
waitConnect() // Wait for at least the initial connection to happen
Expand All @@ -40,11 +40,13 @@ open class ServiceClient<T : IInterface>(private val stubFactory: (IBinder) -> T
fun waitConnect() {
// Wait until this service is connected
lock.withLock {
if (context == null)
if (context == null) {
throw Exception("Haven't called connect")
}

if (serviceP == null)
if (serviceP == null) {
condition.await()
}
}
}

Expand All @@ -71,20 +73,19 @@ open class ServiceClient<T : IInterface>(private val stubFactory: (IBinder) -> T
isClosed = true
try {
context?.unbindService(connection)
}
catch(ex: IllegalArgumentException) {
} catch (ex: IllegalArgumentException) {
// Autobugs show this can generate an illegal arg exception for "service not registered" during reinstall?
warn("Ignoring error in ServiceClient.close, probably harmless")
}
serviceP = null
context = null
}

/// Called when we become connected
// Called when we become connected
open fun onConnected(service: T) {
}

/// called on loss of connection
// called on loss of connection
open fun onDisconnected() {
}

Expand All @@ -111,4 +112,4 @@ open class ServiceClient<T : IInterface>(private val stubFactory: (IBinder) -> T
onDisconnected()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.geeksville.mesh.concurrent

import com.geeksville.mesh.android.Logging


/**
* A deferred execution object (with various possible implementations)
*/
Expand Down Expand Up @@ -52,15 +51,17 @@ class SyncContinuation<T> : Continuation<T> {
while (result == null) {
mbox.wait(timeoutMsecs)

if (timeoutMsecs > 0 && ((System.currentTimeMillis() - startT) >= timeoutMsecs))
if (timeoutMsecs > 0 && ((System.currentTimeMillis() - startT) >= timeoutMsecs)) {
throw Exception("SyncContinuation timeout")
}
}

val r = result
if (r != null)
if (r != null) {
return r.getOrThrow()
else
} else {
throw Exception("This shouldn't happen")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ import com.geeksville.mesh.database.entity.QuickChatAction
QuickChatAction::class
],
autoMigrations = [
AutoMigration (from = 3, to = 4),
AutoMigration (from = 4, to = 5),
AutoMigration (from = 5, to = 6),
AutoMigration (from = 6, to = 7),
AutoMigration (from = 7, to = 8),
AutoMigration (from = 8, to = 9),
AutoMigration (from = 9, to = 10),
AutoMigration (from = 10, to = 11),
AutoMigration (from = 11, to = 12),
AutoMigration(from = 3, to = 4),
AutoMigration(from = 4, to = 5),
AutoMigration(from = 5, to = 6),
AutoMigration(from = 6, to = 7),
AutoMigration(from = 7, to = 8),
AutoMigration(from = 8, to = 9),
AutoMigration(from = 9, to = 10),
AutoMigration(from = 10, to = 11),
AutoMigration(from = 11, to = 12),
AutoMigration(from = 12, to = 13, spec = AutoMigration12to13::class),
],
version = 13,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ interface MeshLogDao {

@Query("DELETE FROM log")
fun deleteAll()

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ interface PacketDao {
fun getContactSettings(): Flow<Map<@MapColumn(columnName = "contact_key") String, ContactSettings>>

@Query("SELECT * FROM contact_settings WHERE contact_key = :contact")
suspend fun getContactSettings(contact:String): ContactSettings?
suspend fun getContactSettings(contact: String): ContactSettings?

@Upsert
fun upsertContactSettings(contacts: List<ContactSettings>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ data class NodeEntity(
val validPosition: MeshProtos.Position? get() = position.takeIf { hasValidPosition() }

// @return distance in meters to some other node (or null if unknown)
fun distance(o: NodeEntity): Int? {
return if (validPosition == null || o.validPosition == null) null
else latLongToMeter(latitude, longitude, o.latitude, o.longitude).toInt()
fun distance(o: NodeEntity): Int? = when {
validPosition == null || o.validPosition == null -> null
else -> latLongToMeter(latitude, longitude, o.latitude, o.longitude).toInt()
}

// @return a nice human readable string for the distance, or null for unknown
Expand All @@ -119,9 +119,9 @@ data class NodeEntity(
}

// @return bearing to the other position in degrees
fun bearing(o: NodeEntity?): Int? {
return if (validPosition == null || o?.validPosition == null) null
else bearing(latitude, longitude, o.latitude, o.longitude).toInt()
fun bearing(o: NodeEntity?): Int? = when {
validPosition == null || o?.validPosition == null -> null
else -> bearing(latitude, longitude, o.latitude, o.longitude).toInt()
}

fun gpsString(gpsFormat: Int): String = when (gpsFormat) {
Expand All @@ -140,7 +140,9 @@ data class NodeEntity(
} else {
"%.1f°C".format(temperature)
}
} else null
} else {
null
}
val humidity = if (relativeHumidity != 0f) "%.0f%%".format(relativeHumidity) else null
val pressure = if (barometricPressure != 0f) "%.1fhPa".format(barometricPressure) else null
val gas = if (gasResistance != 0f) "%.0fMΩ".format(gasResistance) else null
Expand Down Expand Up @@ -188,7 +190,7 @@ data class NodeEntity(
}

companion object {
/// Convert to a double representation of degrees
// Convert to a double representation of degrees
fun degD(i: Int) = i * 1e-7
fun degI(d: Double) = (d * 1e7).toInt()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import androidx.room.PrimaryKey
@Entity(tableName = "quick_chat")
data class QuickChatAction(
@PrimaryKey(autoGenerate = true) val uuid: Long,
@ColumnInfo(name="name") val name: String,
@ColumnInfo(name="message") val message: String,
@ColumnInfo(name="mode") val mode: Mode,
@ColumnInfo(name="position") val position: Int) {
@ColumnInfo(name = "name") val name: String,
@ColumnInfo(name = "message") val message: String,
@ColumnInfo(name = "mode") val mode: Mode,
@ColumnInfo(name = "position") val position: Int
) {
enum class Mode {
Append,
Instant,
}
}
}
Loading

0 comments on commit fe56d25

Please sign in to comment.