Skip to content

Commit

Permalink
Merge pull request #107 from NordicSemiconductor/feature/logger
Browse files Browse the repository at this point in the history
Add support for nRF Logger
  • Loading branch information
sylwester-zielinski authored Apr 1, 2022
2 parents 520d39b + 44436df commit 7c7befe
Show file tree
Hide file tree
Showing 38 changed files with 212 additions and 52 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ dependencies {
implementation project(":lib_theme")
implementation project(":lib_utils")
implementation project(":lib_service")
implementation project(":lib_log")

implementation libs.nordic.ble.common
implementation libs.nordic.ui.scanner
Expand Down
2 changes: 2 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
-keep class org.simpleframework.xml.core.** { *; }
-keep class org.simpleframework.xml.util.** { *; }

-keep class no.nordicsemi.android.log.** { *; }

-keepattributes ElementList, Root, InnerClasses, LineNumberTable

-keepclasseswithmembers class * {
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
</intent>
</queries>

<queries>
<package android:name="no.nordicsemi.android.log" />

<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
</queries>

<application
android:name=".NrfToolboxApplication"
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.OnLifecycleEvent
import no.nordicsemi.android.nrftoolbox.BuildConfig
import no.nordicsemi.android.nrftoolbox.ProfileDestination
import no.nordicsemi.android.nrftoolbox.R
Expand All @@ -24,6 +23,9 @@ import no.nordicsemi.android.theme.view.TitleAppBar
private const val DFU_PACKAGE_NAME = "no.nordicsemi.android.dfu"
private const val DFU_LINK = "https://play.google.com/store/apps/details?id=no.nordicsemi.android.dfu"

private const val LOGGER_PACKAGE_NAME = "no.nordicsemi.android.log"
private const val LOGGER_LINK = "https://play.google.com/store/apps/details?id=no.nordicsemi.android.log"

@Composable
fun HomeScreen() {
val viewModel: HomeViewModel = hiltViewModel()
Expand Down Expand Up @@ -141,6 +143,21 @@ fun HomeScreen() {

Spacer(modifier = Modifier.height(16.dp))

val loggerDescription = packageManger.getLaunchIntentForPackage(LOGGER_PACKAGE_NAME)?.let {
R.string.logger_module_info
} ?: R.string.dfu_module_install

FeatureButton(R.drawable.ic_logger, R.string.logger_module, R.string.logger_module_full, null, loggerDescription) {
val intent = packageManger.getLaunchIntentForPackage(LOGGER_PACKAGE_NAME)
if (intent != null) {
context.startActivity(intent)
} else {
uriHandler.openUri(LOGGER_LINK)
}
}

Spacer(modifier = Modifier.height(16.dp))

Text(
text = BuildConfig.VERSION_NAME,
style = MaterialTheme.typography.labelSmall,
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_logger.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="1024dp"
android:height="1024dp"
android:viewportWidth="1024"
android:viewportHeight="1024">
<path
android:pathData="M989.6,949.6L772.8,732.8c30.8,-33.4 55.3,-71.5 73,-113.4c20.6,-48.7 31,-100.4 31,-153.6s-10.4,-105 -31,-153.6c-19.9,-47 -48.3,-89.2 -84.6,-125.4c-36.2,-36.2 -78.4,-64.7 -125.4,-84.6c-48.7,-20.6 -100.4,-31 -153.6,-31s-105,10.4 -153.6,31c-47,19.9 -89.2,48.3 -125.4,84.6c-36.2,36.2 -64.7,78.4 -84.6,125.4c-20.6,48.7 -31,100.4 -31,153.6s10.4,105 31,153.6c19.9,47 48.3,89.2 84.6,125.4c36.2,36.2 78.4,64.7 125.4,84.6c48.7,20.6 100.4,31 153.6,31s105,-10.4 153.6,-31c34.6,-14.6 66.6,-33.9 95.5,-57.5l218,218c5.6,5.6 12.9,8.3 20.2,8.3s14.6,-2.8 20.2,-8.3C1000.8,978.8 1000.8,960.7 989.6,949.6zM482.2,803.4c-112.7,0 -212.6,-55.5 -274,-140.6h352.3c15.7,0 28.5,-12.8 28.5,-28.5s-12.8,-28.5 -28.5,-28.5H175c-15.8,-34.5 -25.9,-72.1 -29.2,-111.6h414.8c15.7,0 28.5,-12.8 28.5,-28.5s-12.8,-28.5 -28.5,-28.5H145.7c3.3,-39.5 13.5,-77.1 29.2,-111.6h385.5c15.7,0 28.5,-12.8 28.5,-28.5s-12.8,-28.5 -28.5,-28.5H208.2c61.4,-85.1 161.3,-140.6 274,-140.6c186.2,0 337.7,151.5 337.7,337.7C819.9,651.9 668.4,803.4 482.2,803.4z"
android:fillColor="#00B3DC"/>
</vector>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<string name="dfu_module_full">Device Firmware Update</string>
<string name="dfu_module_info">Open DFU application.</string>
<string name="dfu_module_install">Download from Google Play.</string>
<string name="logger_module">nRF Logger</string>
<string name="logger_module_full">nRF Logger</string>
<string name="logger_module_info">Open nRF Logger application.</string>

<string name="viewmodel_profiles">ViewModel profiles</string>
<string name="service_profiles">Service profiles</string>
Expand Down
5 changes: 5 additions & 0 deletions lib_log/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apply from: rootProject.file("library.gradle")

dependencies {
implementation libs.nordic.log
}
7 changes: 7 additions & 0 deletions lib_log/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="no.nordicsemi.android.log">

<uses-permission android:name="no.nordicsemi.android.LOG"/>

</manifest>
28 changes: 28 additions & 0 deletions lib_log/src/main/java/no/nordicsemi/android/log/ToolboxLogger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package no.nordicsemi.android.log

import android.content.Context
import android.util.Log
import no.nordicsemi.android.log.annotation.LogLevel

internal const val LOG_TAG = "nRF Toolbox"

class ToolboxLogger(
private val context: Context,
private val key: String,
) {

private var logSession: LogSession? = null

fun log(@LogLevel level: Int, message: String) {
val logSession = getLogger()
if (logSession != null) {
Logger.log(logSession, LogContract.Log.Level.fromPriority(level), message)
}
Log.println(level, LOG_TAG, message)
}

private fun getLogger(): LogSession? {
logSession = logSession ?: Logger.newSession(context, key, LOG_TAG)
return logSession
}
}
1 change: 0 additions & 1 deletion lib_service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies {

implementation libs.nordic.ble.common
implementation libs.nordic.ble.ktx
implementation libs.nordic.log
implementation libs.nordic.ui.scanner

implementation libs.lifecycle.service
Expand Down
2 changes: 1 addition & 1 deletion profile_bps/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ dependencies {
implementation project(":lib_service")
implementation project(":lib_theme")
implementation project(":lib_utils")
implementation project(":lib_log")

implementation libs.nordic.ble.common
implementation libs.nordic.ble.ktx
implementation libs.nordic.navigation
implementation libs.nordic.theme

implementation libs.nordic.log
implementation libs.nordic.ui.scanner

implementation libs.bundles.compose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ import no.nordicsemi.android.ble.common.callback.battery.BatteryLevelResponse
import no.nordicsemi.android.ble.common.callback.bps.BloodPressureMeasurementResponse
import no.nordicsemi.android.ble.common.callback.bps.IntermediateCuffPressureResponse
import no.nordicsemi.android.ble.ktx.asValidResponseFlow
import no.nordicsemi.android.bps.data.BPSData
import no.nordicsemi.android.bps.data.copyWithNewResponse
import no.nordicsemi.android.log.ToolboxLogger
import no.nordicsemi.android.service.ConnectionObserverAdapter
import java.util.*

Expand All @@ -50,7 +49,8 @@ private val BATTERY_LEVEL_CHARACTERISTIC_UUID = UUID.fromString("00002A19-0000-1

internal class BPSManager(
@ApplicationContext context: Context,
private val scope: CoroutineScope
private val scope: CoroutineScope,
private val logger: ToolboxLogger
) : BleManager(context) {

private var batteryLevelCharacteristic: BluetoothGattCharacteristic? = null
Expand All @@ -68,6 +68,10 @@ internal class BPSManager(
}.launchIn(scope)
}

override fun log(priority: Int, message: String) {
logger.log(priority, message)
}

override fun getMinLogPriority(): Int {
return Log.VERBOSE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import no.nordicsemi.android.bps.data.BPSData
import no.nordicsemi.android.bps.data.BPSManager
import no.nordicsemi.android.log.ToolboxLogger
import no.nordicsemi.android.service.BleManagerResult
import javax.inject.Inject

@ViewModelScoped
internal class BPSRepository @Inject constructor(
@ApplicationContext
private val context: Context,
private val context: Context
) {

fun downloadData(device: BluetoothDevice): Flow<BleManagerResult<BPSData>> = callbackFlow {
val scope = this
val manager = BPSManager(context, scope)
val manager = BPSManager(context, scope, ToolboxLogger(context, "BPS"))

manager.dataHolder.status.onEach {
trySend(it)
Expand Down
2 changes: 1 addition & 1 deletion profile_cgms/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ dependencies {
implementation project(":lib_service")
implementation project(":lib_theme")
implementation project(":lib_utils")
implementation project(":lib_log")

implementation libs.nordic.ble.common
implementation libs.nordic.ble.ktx

implementation libs.nordic.log
implementation libs.nordic.theme
implementation libs.nordic.ui.scanner
implementation libs.nordic.navigation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import no.nordicsemi.android.ble.ktx.asValidResponseFlow
import no.nordicsemi.android.ble.ktx.suspend
import no.nordicsemi.android.ble.ktx.suspendForValidResponse
import no.nordicsemi.android.cgms.repository.toList
import no.nordicsemi.android.log.ToolboxLogger
import no.nordicsemi.android.service.ConnectionObserverAdapter
import no.nordicsemi.android.utils.launchWithCatch
import java.util.*
Expand All @@ -62,7 +63,8 @@ private val BATTERY_LEVEL_CHARACTERISTIC_UUID = UUID.fromString("00002A19-0000-1

internal class CGMManager(
context: Context,
private val scope: CoroutineScope
private val scope: CoroutineScope,
private val logger: ToolboxLogger
) : BleManager(context) {

private var cgmStatusCharacteristic: BluetoothGattCharacteristic? = null
Expand Down Expand Up @@ -95,8 +97,11 @@ internal class CGMManager(
}

override fun log(priority: Int, message: String) {
super.log(priority, message)
Log.d("CGM-PROFILE", message)
logger.log(priority, message)
}

override fun getMinLogPriority(): Int {
return Log.VERBOSE
}

private inner class CGMManagerGattCallback : BleManagerGattCallback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import kotlinx.coroutines.launch
import no.nordicsemi.android.ble.ktx.suspend
import no.nordicsemi.android.cgms.data.CGMData
import no.nordicsemi.android.cgms.data.CGMManager
import no.nordicsemi.android.log.ToolboxLogger
import no.nordicsemi.android.service.BleManagerResult
import no.nordicsemi.android.service.ConnectingResult
import no.nordicsemi.android.service.ServiceManager
import java.lang.Exception
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class CGMRepository @Inject constructor(
@ApplicationContext
private val context: Context,
private val serviceManager: ServiceManager,
private val serviceManager: ServiceManager
) {
private var manager: CGMManager? = null

Expand All @@ -35,7 +35,7 @@ class CGMRepository @Inject constructor(
}

fun start(device: BluetoothDevice, scope: CoroutineScope) {
val manager = CGMManager(context, scope)
val manager = CGMManager(context, scope, ToolboxLogger(context, "CGMS"))
this.manager = manager

manager.dataHolder.status.onEach {
Expand Down
2 changes: 1 addition & 1 deletion profile_csc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ dependencies {
implementation project(":lib_service")
implementation project(":lib_theme")
implementation project(":lib_utils")
implementation project(":lib_log")

implementation libs.nordic.ble.common
implementation libs.nordic.ble.ktx

implementation libs.nordic.log
implementation libs.nordic.theme
implementation libs.nordic.navigation
implementation libs.nordic.ui.scanner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package no.nordicsemi.android.csc.data
import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothGattCharacteristic
import android.content.Context
import android.util.Log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.launchIn
Expand All @@ -32,8 +33,7 @@ import no.nordicsemi.android.ble.BleManager
import no.nordicsemi.android.ble.common.callback.battery.BatteryLevelResponse
import no.nordicsemi.android.ble.common.callback.csc.CyclingSpeedAndCadenceMeasurementResponse
import no.nordicsemi.android.ble.ktx.asValidResponseFlow
import no.nordicsemi.android.csc.data.CSCData
import no.nordicsemi.android.csc.data.WheelSize
import no.nordicsemi.android.log.ToolboxLogger
import no.nordicsemi.android.service.ConnectionObserverAdapter
import java.util.*

Expand All @@ -46,6 +46,7 @@ private val BATTERY_LEVEL_CHARACTERISTIC_UUID = UUID.fromString("00002A19-0000-1
internal class CSCManager(
context: Context,
private val scope: CoroutineScope,
private val logger: ToolboxLogger
) : BleManager(context) {

private var batteryLevelCharacteristic: BluetoothGattCharacteristic? = null
Expand All @@ -65,6 +66,14 @@ internal class CSCManager(
}.launchIn(scope)
}

override fun log(priority: Int, message: String) {
logger.log(priority, message)
}

override fun getMinLogPriority(): Int {
return Log.VERBOSE
}

override fun getGattCallback(): BleManagerGattCallback {
return CSCManagerGattCallback()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import no.nordicsemi.android.ble.ktx.suspend
import no.nordicsemi.android.csc.data.CSCData
import no.nordicsemi.android.csc.data.CSCManager
import no.nordicsemi.android.csc.data.WheelSize
import no.nordicsemi.android.log.ToolboxLogger
import no.nordicsemi.android.service.BleManagerResult
import no.nordicsemi.android.service.ConnectingResult
import no.nordicsemi.android.service.ServiceManager
Expand All @@ -20,7 +21,7 @@ import javax.inject.Singleton
class CSCRepository @Inject constructor(
@ApplicationContext
private val context: Context,
private val serviceManager: ServiceManager,
private val serviceManager: ServiceManager
) {
private var manager: CSCManager? = null

Expand All @@ -35,7 +36,7 @@ class CSCRepository @Inject constructor(
}

fun start(device: BluetoothDevice, scope: CoroutineScope) {
val manager = CSCManager(context, scope)
val manager = CSCManager(context, scope, ToolboxLogger(context, "CSC"))
this.manager = manager

manager.dataHolder.status.onEach {
Expand Down
3 changes: 1 addition & 2 deletions profile_gls/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dependencies {
implementation project(":lib_service")
implementation project(":lib_theme")
implementation project(":lib_utils")
implementation project(":lib_log")

implementation libs.chart

Expand All @@ -14,8 +15,6 @@ dependencies {
implementation libs.nordic.ui.scanner
implementation libs.nordic.navigation

implementation libs.nordic.log

implementation libs.bundles.compose
implementation libs.androidx.core
implementation libs.material
Expand Down
Loading

0 comments on commit 7c7befe

Please sign in to comment.