Skip to content

Commit ade656f

Browse files
authored
Merge pull request #122 from wiiznokes/settings-tiles2
refractor some stuff
2 parents 7450c9b + ce98c12 commit ade656f

File tree

7 files changed

+111
-80
lines changed

7 files changed

+111
-80
lines changed

Android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
4545
android:resource="@xml/accessory_filter" />
4646
</activity>
47+
4748
<service
4849
android:name="io.github.teamclouday.AndroidMic.domain.service.ForegroundService"
4950
android:foregroundServiceType="microphone" />

Android/app/src/main/java/io/github/teamclouday/AndroidMic/AndroidMicApp.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class AndroidMicApp : Application() {
2020

2121
companion object {
2222
lateinit var appModule: AppModule
23-
var mService: Messenger? = null
24-
var mBound = false
23+
var service: Messenger? = null
24+
var isBound = false
2525
}
2626

2727
private val scope = MainScope()
@@ -38,8 +38,8 @@ class AndroidMicApp : Application() {
3838
private val mConnection = object : ServiceConnection {
3939
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
4040
Log.d(TAG, "onServiceConnected")
41-
mService = Messenger(service)
42-
mBound = true
41+
Companion.service = Messenger(service)
42+
isBound = true
4343
// notify current running activity that service is connected
4444
val notifyIntent = Intent(applicationContext, MainActivity::class.java).apply {
4545
action = Intent.ACTION_VIEW
@@ -51,8 +51,8 @@ class AndroidMicApp : Application() {
5151

5252
override fun onServiceDisconnected(name: ComponentName?) {
5353
Log.d(TAG, "onServiceDisconnected")
54-
mService = null
55-
mBound = false
54+
service = null
55+
isBound = false
5656
}
5757
}
5858

@@ -67,7 +67,7 @@ class AndroidMicApp : Application() {
6767

6868
fun unBindService() {
6969
unbindService(mConnection)
70-
mService = null
71-
mBound = false
70+
service = null
71+
isBound = false
7272
}
7373
}

Android/app/src/main/java/io/github/teamclouday/AndroidMic/domain/service/Command.kt

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ package io.github.teamclouday.AndroidMic.domain.service
22

33
import android.os.Bundle
44
import android.os.Message
5+
import io.github.teamclouday.AndroidMic.AppPreferences
56
import io.github.teamclouday.AndroidMic.AudioFormat
67
import io.github.teamclouday.AndroidMic.ChannelCount
8+
import io.github.teamclouday.AndroidMic.Dialogs
79
import io.github.teamclouday.AndroidMic.Mode
810
import io.github.teamclouday.AndroidMic.SampleRates
11+
import io.github.teamclouday.AndroidMic.utils.Either
12+
import io.github.teamclouday.AndroidMic.utils.checkIp
13+
import io.github.teamclouday.AndroidMic.utils.checkPort
914

1015

1116
private const val ID_MSG: String = "ID_MSG"
@@ -66,26 +71,62 @@ data class CommandData(
6671
audioFormat = msg.data.getOrdinal(ID_AUDIO_FORMAT)?.let { AudioFormat.entries[it] },
6772
)
6873
}
74+
75+
suspend fun fromPref(
76+
prefs: AppPreferences,
77+
command: Command
78+
): Either<CommandData, Dialogs> {
79+
val ip = prefs.ip.get()
80+
val port = prefs.port.get()
81+
val mode = prefs.mode.get()
82+
83+
val data = CommandData(
84+
command = command,
85+
sampleRate = prefs.sampleRate.get(),
86+
channelCount = prefs.channelCount.get(),
87+
audioFormat = prefs.audioFormat.get(),
88+
mode = mode
89+
)
90+
91+
when (mode) {
92+
Mode.WIFI, Mode.UDP -> {
93+
if (!checkIp(ip) || !checkPort(port, true)) {
94+
95+
return Either.Right(Dialogs.IpPort)
96+
}
97+
data.ip = ip
98+
data.port = try {
99+
port.toInt()
100+
} catch (_: Exception) {
101+
null
102+
}
103+
}
104+
105+
else -> {}
106+
}
107+
108+
return Either.Left(data)
109+
}
69110
}
70111

71112
fun toCommandMsg(): Message {
72113

73-
val r = Bundle()
114+
val data = Bundle()
74115

75-
this.mode?.let { r.putInt(ID_MODE, it.ordinal) }
116+
this.mode?.let { data.putInt(ID_MODE, it.ordinal) }
76117

77-
this.ip?.let { r.putString(ID_IP, it) }
78-
r.putInt(ID_PORT, this.port ?: -1)
118+
this.ip?.let { data.putString(ID_IP, it) }
119+
data.putInt(ID_PORT, this.port ?: -1)
79120

80-
this.sampleRate?.let { r.putInt(ID_SAMPLE_RATE, it.ordinal) }
81-
this.channelCount?.let { r.putInt(ID_CHANNEL_COUNT, it.ordinal) }
82-
this.audioFormat?.let { r.putInt(ID_AUDIO_FORMAT, it.ordinal) }
121+
this.sampleRate?.let { data.putInt(ID_SAMPLE_RATE, it.ordinal) }
122+
this.channelCount?.let { data.putInt(ID_CHANNEL_COUNT, it.ordinal) }
123+
this.audioFormat?.let { data.putInt(ID_AUDIO_FORMAT, it.ordinal) }
83124

84-
val reply = Message.obtain()
85-
reply.data = r
86-
reply.what = this.command.ordinal
125+
val message = Message.obtain()
126+
message.data = data
127+
message.what = this.command.ordinal
87128

88-
return reply
129+
return message
89130
}
90131

91132

Android/app/src/main/java/io/github/teamclouday/AndroidMic/domain/service/ForegroundService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ data class ServiceStates(
3131
)
3232

3333
private const val TAG = "ForegroundService"
34-
private const val WAIT_PERIOD = 500L
34+
const val WAIT_PERIOD = 500L
3535

3636

3737
const val BIND_SERVICE_ACTION = "BIND_SERVICE_ACTION"

Android/app/src/main/java/io/github/teamclouday/AndroidMic/ui/MainActivity.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.activity.ComponentActivity
77
import androidx.activity.compose.setContent
88
import androidx.activity.viewModels
99
import io.github.teamclouday.AndroidMic.AndroidMicApp
10+
import io.github.teamclouday.AndroidMic.domain.service.WAIT_PERIOD
1011
import io.github.teamclouday.AndroidMic.ui.home.HomeScreen
1112
import io.github.teamclouday.AndroidMic.ui.home.openAppSettings
1213
import io.github.teamclouday.AndroidMic.ui.theme.AndroidMicTheme
@@ -17,8 +18,6 @@ import io.github.teamclouday.AndroidMic.utils.ignore
1718
class MainActivity : ComponentActivity() {
1819
private val TAG = "MainActivity"
1920

20-
private val WAIT_PERIOD = 500L
21-
2221
val vm: MainViewModel by viewModels()
2322

2423
override fun onCreate(savedInstanceState: Bundle?) {
@@ -66,7 +65,7 @@ class MainActivity : ComponentActivity() {
6665
override fun onStop() {
6766
super.onStop()
6867
Log.d(TAG, "onStop")
69-
vm.mMessengerLooper.quitSafely()
68+
vm.messengerLooper.quitSafely()
7069
ignore { vm.handlerThread.join(WAIT_PERIOD) }
7170

7271
(application as AndroidMicApp).unBindService()

Android/app/src/main/java/io/github/teamclouday/AndroidMic/ui/MainViewModel.kt

Lines changed: 42 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import io.github.teamclouday.AndroidMic.domain.service.Response
2525
import io.github.teamclouday.AndroidMic.domain.service.ResponseData
2626
import io.github.teamclouday.AndroidMic.domain.service.ServiceState
2727
import io.github.teamclouday.AndroidMic.ui.utils.UiHelper
28-
import io.github.teamclouday.AndroidMic.utils.checkIp
29-
import io.github.teamclouday.AndroidMic.utils.checkPort
28+
import io.github.teamclouday.AndroidMic.utils.Either
3029
import kotlinx.coroutines.launch
30+
import kotlinx.coroutines.runBlocking
3131

3232

3333
class MainViewModel : ViewModel() {
@@ -38,13 +38,12 @@ class MainViewModel : ViewModel() {
3838
val prefs: AppPreferences = AndroidMicApp.appModule.appPreferences
3939
val uiHelper: UiHelper = AndroidMicApp.appModule.uiHelper
4040

41-
private var mService: Messenger? = null
42-
private var mBound = false
41+
private var service: Messenger? = null
42+
private var isBound = false
4343

4444
lateinit var handlerThread: HandlerThread
45-
private lateinit var mMessenger: Messenger
46-
lateinit var mMessengerLooper: Looper
47-
private lateinit var mMessengerHandler: ReplyHandler
45+
private lateinit var messenger: Messenger
46+
lateinit var messengerLooper: Looper
4847

4948

5049
val textLog = mutableStateOf("")
@@ -61,7 +60,7 @@ class MainViewModel : ViewModel() {
6160

6261
val data = ResponseData.fromMessage(msg);
6362

64-
when (Response.entries[msg.what]) {
63+
when (data.kind) {
6564
Response.Standard -> {
6665
data.state?.let {
6766
isButtonConnectClickable.value = true
@@ -77,71 +76,57 @@ class MainViewModel : ViewModel() {
7776
}
7877

7978
fun handlerServiceResponse() {
80-
handlerThread = HandlerThread("activity", Process.THREAD_PRIORITY_BACKGROUND)
79+
handlerThread =
80+
HandlerThread("MainViewModelResponseHandler", Process.THREAD_PRIORITY_BACKGROUND)
8181
handlerThread.start()
82-
mMessengerLooper = handlerThread.looper
83-
mMessengerHandler = ReplyHandler(mMessengerLooper)
84-
mMessenger = Messenger(mMessengerHandler)
82+
messengerLooper = handlerThread.looper
83+
messenger = Messenger(ReplyHandler(messengerLooper))
8584
}
8685

8786
fun refreshAppVariables() {
88-
mBound = AndroidMicApp.mBound
89-
mService = AndroidMicApp.mService
87+
isBound = AndroidMicApp.isBound
88+
service = AndroidMicApp.service
9089

9190
isStreamStarted.value = false
9291
isButtonConnectClickable.value = false
9392

9493
val msg = CommandData(Command.BindCheck).toCommandMsg()
95-
msg.replyTo = mMessenger
96-
mService?.send(msg)
94+
msg.replyTo = messenger
95+
service?.send(msg)
9796
}
9897

9998
fun onConnectButton(): Dialogs? {
100-
if (!mBound) return null
101-
val reply = if (isStreamStarted.value) {
99+
if (!isBound) return null
100+
val message = if (isStreamStarted.value) {
102101
Log.d(TAG, "onConnectButton: stop stream")
103-
CommandData(Command.StopStream).toCommandMsg()
102+
CommandData(Command.StopStream)
104103
} else {
105-
val ip = prefs.ip.getBlocking()
106-
val port = prefs.port.getBlocking()
107-
val mode = prefs.mode.getBlocking()
108-
109-
val data = CommandData(
110-
command = Command.StartStream,
111-
sampleRate = prefs.sampleRate.getBlocking(),
112-
channelCount = prefs.channelCount.getBlocking(),
113-
audioFormat = prefs.audioFormat.getBlocking(),
114-
mode = mode
115-
)
116-
117-
when (mode) {
118-
Mode.WIFI, Mode.UDP -> {
119-
if (!checkIp(ip) || !checkPort(port, true)) {
120-
uiHelper.makeToast(
121-
uiHelper.getString(R.string.invalid_ip_port)
122-
)
123-
return Dialogs.IpPort
124-
}
125-
data.ip = ip
126-
data.port = try {
127-
port.toInt()
128-
} catch (_: Exception) {
129-
null
130-
}
104+
val res = runBlocking {
105+
CommandData.fromPref(prefs, Command.StartStream)
106+
}
107+
val data = when (res) {
108+
is Either.Left<CommandData> -> {
109+
res.value
131110
}
132111

133-
else -> {}
112+
is Either.Right<Dialogs> -> {
113+
uiHelper.makeToast(
114+
uiHelper.getString(R.string.invalid_ip_port)
115+
)
116+
return res.value
117+
}
134118
}
135119

136120
Log.d(TAG, "onConnectButton: start stream")
137-
// lock button to avoid duplicate events
138-
isButtonConnectClickable.value = false
139121

140-
data.toCommandMsg()
141-
}
122+
data
123+
}.toCommandMsg()
124+
125+
// lock button to avoid duplicate events
126+
isButtonConnectClickable.value = false
142127

143-
reply.replyTo = mMessenger
144-
mService?.send(reply)
128+
message.replyTo = messenger
129+
service?.send(message)
145130

146131
return null
147132
}
@@ -197,11 +182,11 @@ class MainViewModel : ViewModel() {
197182

198183
// ask foreground service for current status
199184
fun askForStatus() {
200-
if (!mBound) return
201-
val reply = Message.obtain()
202-
reply.what = Command.GetStatus.ordinal
203-
reply.replyTo = mMessenger
204-
mService?.send(reply)
185+
if (!isBound) return
186+
val message = Message.obtain()
187+
message.what = Command.GetStatus.ordinal
188+
message.replyTo = messenger
189+
service?.send(message)
205190
}
206191

207192

Android/app/src/main/java/io/github/teamclouday/AndroidMic/utils/Utils.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,9 @@ fun ByteArray.chunked(size: Int): List<ByteArray> {
6060
)
6161
)
6262
}
63+
}
64+
65+
sealed class Either<out A, out B> {
66+
class Left<A>(val value: A) : Either<A, Nothing>()
67+
class Right<B>(val value: B) : Either<Nothing, B>()
6368
}

0 commit comments

Comments
 (0)