Skip to content
This repository was archived by the owner on Aug 15, 2021. It is now read-only.

Commit ba554d8

Browse files
committed
2020-02-17 Version 1.2.0: Refactored BtMonitor realizations
1 parent df6544d commit ba554d8

File tree

22 files changed

+278
-152
lines changed

22 files changed

+278
-152
lines changed

app/build.gradle

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,30 @@ android {
1212
minSdkVersion 21
1313
targetSdkVersion 29
1414
versionCode 1
15-
versionName "1.1.0"
15+
versionName "1.2.0"
16+
17+
buildConfigField "String", "API_IMPL", "\"\""
18+
}
19+
productFlavors {
20+
devicebt {
21+
flavorDimensions "build"
22+
23+
buildConfigField "String", "API_IMPL", "\"DEVICE_BT\""
24+
}
25+
debugbt {
26+
flavorDimensions "build"
27+
28+
buildConfigField "String", "API_IMPL", "\"DEBUG_BT\""
29+
}
1630
}
1731
}
1832

1933
dependencies {
2034
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
2135

2236
implementation 'androidx.appcompat:appcompat:1.1.0'
23-
implementation 'com.google.android.material:material:1.0.0'
24-
implementation 'androidx.core:core-ktx:1.1.0'
37+
implementation 'com.google.android.material:material:1.1.0'
38+
implementation 'androidx.core:core-ktx:1.2.0'
2539
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
2640

2741
// Unit tests

app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class MainActivity : AppCompatActivity() {
2828
private fun handleOnPause() {
2929
val currentFragment = supportFragmentManager.findFragmentByTag(CURRENT_FRAGMENT_TAG)
3030
if (currentFragment != null && currentFragment is BtPauseTarget) {
31-
currentFragment.handleBtInOnPause()
31+
currentFragment.btOnPause()
3232
}
3333
}
3434

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.smlnskgmail.jaman.remotetemperaturecontrol.components.dialogs
1+
package com.smlnskgmail.jaman.remotetemperaturecontrol.components
22

33
import android.app.AlertDialog
44
import android.content.Context
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.smlnskgmail.jaman.remotetemperaturecontrol.components.bottomsheets
1+
package com.smlnskgmail.jaman.remotetemperaturecontrol.components
22

33
import android.os.Bundle
44
import android.view.LayoutInflater
@@ -8,7 +8,10 @@ import com.google.android.material.bottomsheet.BottomSheetDialogFragment
88

99
abstract class BaseBottomSheet : BottomSheetDialogFragment() {
1010

11-
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
11+
override fun onViewCreated(
12+
view: View,
13+
savedInstanceState: Bundle?
14+
) {
1215
initialize()
1316
}
1417

@@ -18,7 +21,11 @@ abstract class BaseBottomSheet : BottomSheetDialogFragment() {
1821
inflater: LayoutInflater,
1922
container: ViewGroup?,
2023
savedInstanceState: Bundle?
21-
) : View? = inflater.inflate(getLayoutResId(), container, false)
24+
) : View? = inflater.inflate(
25+
getLayoutResId(),
26+
container,
27+
false
28+
)
2229

2330
abstract fun getLayoutResId(): Int
2431

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
package com.smlnskgmail.jaman.remotetemperaturecontrol.components.fragments
1+
package com.smlnskgmail.jaman.remotetemperaturecontrol.components
22

33
import android.os.Bundle
44
import android.view.LayoutInflater
55
import android.view.View
66
import android.view.ViewGroup
77
import androidx.fragment.app.Fragment
8-
import com.smlnskgmail.jaman.remotetemperaturecontrol.components.bottomsheets.BaseBottomSheet
98
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.targets.BtPauseTarget
109

1110
abstract class BaseFragment : Fragment(), BtPauseTarget {
@@ -14,7 +13,11 @@ abstract class BaseFragment : Fragment(), BtPauseTarget {
1413
inflater: LayoutInflater,
1514
container: ViewGroup?,
1615
savedInstanceState: Bundle?
17-
) : View = inflater.inflate(getLayoutResId(), container, false)
16+
) : View = inflater.inflate(
17+
getLayoutResId(),
18+
container,
19+
false
20+
)
1821

1922
abstract fun getLayoutResId(): Int
2023

@@ -25,6 +28,6 @@ abstract class BaseFragment : Fragment(), BtPauseTarget {
2528
)
2629
}
2730

28-
override fun handleBtInOnPause() {}
31+
override fun btOnPause() {}
2932

3033
}

app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/deviceselector/BtDevicesBottomSheet.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.deviceselector
22

33
import com.smlnskgmail.jaman.remotetemperaturecontrol.R
4-
import com.smlnskgmail.jaman.remotetemperaturecontrol.components.bottomsheets.BaseBottomSheet
4+
import com.smlnskgmail.jaman.remotetemperaturecontrol.components.BaseBottomSheet
55
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.deviceselector.recycler.BtDevicesAdapter
66
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.BtDevice
77
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.targets.BtConnectTarget

app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/MonitorFragment.kt

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import android.content.DialogInterface
66
import android.os.Bundle
77
import android.view.View
88
import android.widget.TextView
9+
import com.smlnskgmail.jaman.remotetemperaturecontrol.BuildConfig
910
import com.smlnskgmail.jaman.remotetemperaturecontrol.R
10-
import com.smlnskgmail.jaman.remotetemperaturecontrol.components.dialogs.AppDialog
11-
import com.smlnskgmail.jaman.remotetemperaturecontrol.components.fragments.BaseFragment
11+
import com.smlnskgmail.jaman.remotetemperaturecontrol.components.AppDialog
12+
import com.smlnskgmail.jaman.remotetemperaturecontrol.components.BaseFragment
1213
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.deviceselector.BtDevicesBottomSheet
1314
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.BtConnection
1415
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.BtMonitor
@@ -32,31 +33,45 @@ class MonitorFragment : BaseFragment(), BtMonitorTarget {
3233

3334
private var btMonitor: BtMonitor? = null
3435

35-
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
36-
if (btIsEnabled()) {
37-
btAdapter = BluetoothAdapter.getDefaultAdapter()
38-
val btDevices = getBtDevices()
39-
if (btDevices.isNotEmpty()) {
40-
btMonitor = DeviceBtMonitor(this)
41-
showDevicesList(btDevices)
36+
override fun onViewCreated(
37+
view: View,
38+
savedInstanceState: Bundle?
39+
) {
40+
if (BuildConfig.API_IMPL == "DEVICE_BT") {
41+
if (btIsEnabled()) {
42+
btAdapter = BluetoothAdapter.getDefaultAdapter()
43+
val btDevices = getBtDevices()
44+
if (btDevices.isNotEmpty()) {
45+
btMonitor = DeviceBtMonitor(this)
46+
showDevicesList(btDevices)
47+
} else {
48+
showBtDevicesNotFoundWarning()
49+
}
4250
} else {
43-
showBtDevicesNotFoundWarning()
51+
showBtDisabledWarning()
4452
}
4553
} else {
46-
showBtDisabledWarning()
54+
startInDebugMode()
55+
initializeButtons()
4756
}
4857
}
4958

5059
@SuppressLint("SetTextI18n")
51-
@Suppress("unused")
5260
private fun startInDebugMode() {
5361
btMonitor = DebugBtMonitor(this)
5462
monitorBtConnection = DebugBtConnection(btMonitor!!)
55-
monitorBtConnection!!.start()
63+
startMonitorThread()
5664

5765
tv_connected_device_info.text = "DEBUG"
58-
btn_main_options.isEnabled = false
59-
btn_reset_monitor.isEnabled = false
66+
}
67+
68+
private fun startMonitorThread() {
69+
object : Thread() {
70+
override fun run() {
71+
super.run()
72+
monitorBtConnection!!.connect()
73+
}
74+
}.start()
6075
}
6176

6277
private fun btIsEnabled() = true
@@ -65,14 +80,16 @@ class MonitorFragment : BaseFragment(), BtMonitorTarget {
6580
val devicesBottomSheet = BtDevicesBottomSheet()
6681
devicesBottomSheet.setBtDevices(btDevices)
6782
devicesBottomSheet.setBtDeviceSelectCallback(object : BtConnectTarget {
68-
override fun onBtDeviceSelected(name: String, address: String) {
83+
override fun onBtDeviceSelected(
84+
name: String,
85+
address: String
86+
) {
6987
monitorBtConnection = DeviceBtConnection(
7088
btAdapter!!,
7189
address,
7290
btMonitor!!
7391
)
74-
monitorBtConnection!!.connect()
75-
monitorBtConnection!!.start()
92+
startMonitorThread()
7693
setDeviceName(name)
7794
initializeButtons()
7895
}
@@ -132,7 +149,10 @@ class MonitorFragment : BaseFragment(), BtMonitorTarget {
132149
}
133150
}
134151

135-
private fun setTextOnUIThread(textView: TextView, text: String) {
152+
private fun setTextOnUIThread(
153+
textView: TextView,
154+
text: String
155+
) {
136156
activity!!.runOnUiThread {
137157
textView.text = text
138158
}
@@ -186,7 +206,7 @@ class MonitorFragment : BaseFragment(), BtMonitorTarget {
186206
showBottomSheet(settingsBottomSheet)
187207
}
188208

189-
override fun handleBtInOnPause() {
209+
override fun btOnPause() {
190210
monitorBtConnection!!.handleOnResume()
191211
}
192212

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api
22

3-
abstract class BtConnection : Thread() {
3+
interface BtConnection {
44

5-
open fun connect() {
5+
fun connect()
6+
fun disconnect()
7+
fun handleOnResume()
68

7-
}
8-
9-
open fun disconnect() {
10-
11-
}
12-
13-
open fun handleOnResume() {
14-
15-
}
16-
17-
open fun send(btMonitorSignalType: BtMonitorSignalType) {
18-
19-
}
9+
fun send(btMonitorSignalType: BtMonitorSignalType)
2010

2111
}

app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/BtMonitor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api
33
interface BtMonitor {
44

55
fun onNewDataAvailable(
6-
btMonitorSignalType: BtMonitorSignalType,
6+
signalType: BtMonitorSignalType,
77
rawData: String
88
)
99

app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/BtMonitorSignalType.kt

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,6 @@ enum class BtMonitorSignalType {
99
HumidityMinimum,
1010
HumidityMaximum,
1111
Reset,
12-
Other;
13-
14-
companion object {
15-
16-
fun fromRawData(rawData: String): BtMonitorSignalType {
17-
return when (rawData[0].toString()) {
18-
"t" -> Temperature
19-
"i" -> TemperatureMinimum
20-
"m" -> TemperatureMaximum
21-
"h" -> Humidity
22-
"q" -> HumidityMinimum
23-
"w" -> HumidityMaximum
24-
"r" -> Reset
25-
else -> Other
26-
}
27-
}
28-
29-
fun signalOf(btMonitorSignalType: BtMonitorSignalType): String {
30-
return when (btMonitorSignalType) {
31-
Temperature -> "t"
32-
TemperatureMinimum -> "i"
33-
TemperatureMaximum -> "m"
34-
Humidity -> "h"
35-
HumidityMinimum -> "q"
36-
HumidityMaximum -> "w"
37-
Reset -> "r"
38-
else -> ""
39-
}
40-
}
41-
42-
}
12+
Other
4313

4414
}

0 commit comments

Comments
 (0)