Finish Display Host on Android

This commit is contained in:
Florian Märkl 2019-10-20 19:28:27 +02:00
commit 44795194ab
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
9 changed files with 94 additions and 14 deletions

View file

@ -25,6 +25,8 @@ sealed class DisplayHost
abstract val host: String
abstract val name: String?
abstract val id: String?
val isRegistered get() = registeredHost != null
}
class DiscoveredDisplayHost(

View file

@ -22,13 +22,18 @@ import java.nio.ByteOrder
class MacAddress(v: Long)
{
companion object
{
val LENGTH = 6
}
constructor(data: ByteArray) : this(
if(data.size != 6)
if(data.size != LENGTH)
throw IllegalArgumentException("Data has invalid length for MAC")
else
data.let {
val buf = ByteBuffer.allocate(8)
buf.put(it, 0, 6)
buf.put(it, 0, LENGTH)
buf.order(ByteOrder.LITTLE_ENDIAN)
buf.getLong(0)
})

View file

@ -34,7 +34,7 @@ import io.reactivex.Flowable
data class ManualHost(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
val host: String,
@ColumnInfo(name = "registered_host") val registeredHost: Int?
@ColumnInfo(name = "registered_host") val registeredHost: Long?
)
@Dao

View file

@ -0,0 +1,22 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
package com.metallic.chiaki.common.ext
fun String.hexToByteArray(): ByteArray? = ByteArray(this.length / 2) {
this.substring(it * 2, it * 2 + 2).toIntOrNull(16)?.toByte() ?: return null
}

View file

@ -18,6 +18,8 @@
package com.metallic.chiaki.discovery
import android.util.Log
import com.metallic.chiaki.common.MacAddress
import com.metallic.chiaki.common.ext.hexToByteArray
import com.metallic.chiaki.lib.CreateError
import com.metallic.chiaki.lib.DiscoveryHost
import com.metallic.chiaki.lib.DiscoveryService
@ -27,6 +29,13 @@ import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import java.net.InetSocketAddress
val DiscoveryHost.ps4Mac get() = this.hostId?.hexToByteArray()?.let {
if(it.size == MacAddress.LENGTH)
MacAddress(it)
else
null
}
class DiscoveryManager
{
companion object

View file

@ -51,7 +51,22 @@ class DisplayHostRecyclerViewAdapter: RecyclerView.Adapter<DisplayHostRecyclerVi
it.nameTextView.text = host.name
it.hostTextView.text = context.getString(R.string.display_host_host, host.host)
val id = host.id
it.idTextView.text = if(id != null) context.getString(R.string.display_host_id, id) else ""
it.idTextView.text =
if(id != null)
context.getString(
if(host.isRegistered)
R.string.display_host_id_registered
else
R.string.display_host_id_unregistered,
id)
else
""
it.bottomInfoTextView.text = (host as? DiscoveredDisplayHost)?.discoveredHost?.let { discoveredHost ->
if(discoveredHost.runningAppName != null || discoveredHost.runningAppTitleid != null)
context.getString(R.string.display_host_app_title_id, discoveredHost.runningAppName ?: "", discoveredHost.runningAppTitleid ?: "")
else
""
} ?: ""
it.discoveredIndicatorLayout.visibility = if(host is DiscoveredDisplayHost) View.VISIBLE else View.GONE
it.stateIndicatorImageView.setImageResource(
if(host is DiscoveredDisplayHost)

View file

@ -23,6 +23,7 @@ import com.metallic.chiaki.common.DiscoveredDisplayHost
import com.metallic.chiaki.common.ManualDisplayHost
import com.metallic.chiaki.common.ext.toLiveData
import com.metallic.chiaki.discovery.DiscoveryManager
import com.metallic.chiaki.discovery.ps4Mac
import io.reactivex.rxkotlin.Observables
class MainViewModel(val database: AppDatabase): ViewModel()
@ -30,13 +31,18 @@ class MainViewModel(val database: AppDatabase): ViewModel()
val discoveryManager = DiscoveryManager().also { it.active = true /* TODO: from shared preferences */ }
val displayHosts by lazy {
Observables.combineLatest(database.manualHostDao().getAll().toObservable(), discoveryManager.discoveredHosts)
{ manualHosts, discoveredHosts ->
Observables.combineLatest(
database.manualHostDao().getAll().toObservable(),
database.registeredHostDao().getAll().toObservable(),
discoveryManager.discoveredHosts)
{ manualHosts, registeredHosts, discoveredHosts ->
val macRegisteredHosts = registeredHosts.associateBy { it.ps4Mac }
val idRegisteredHosts = registeredHosts.associateBy { it.id }
discoveredHosts.map {
DiscoveredDisplayHost(null /* TODO */, it)
DiscoveredDisplayHost(it.ps4Mac?.let { mac -> macRegisteredHosts[mac] }, it)
} +
manualHosts.map {
ManualDisplayHost(null /* TODO */, it)
ManualDisplayHost(it.registeredHost?.let { id -> idRegisteredHosts[id] }, it)
}
}
.toLiveData()

View file

@ -60,7 +60,9 @@
tools:text="Address: Host"
android:textSize="16sp"
android:gravity="center"
android:textColor="?attr/colorOnSurface"/>
android:textColor="?attr/colorOnSurface"
android:maxLines="1"
android:ellipsize="end"/>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/idTextView"
@ -71,17 +73,34 @@
tools:text="ID: C0FFEE1337"
android:textSize="16sp"
android:gravity="center"
android:textColor="?attr/colorOnSurface"/>
android:textColor="?attr/colorOnSurface"
android:maxLines="1"
android:ellipsize="end"/>
<ImageView
android:id="@+id/stateIndicatorImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="0dp"
android:src="@drawable/ic_console"
app:layout_constraintTop_toBottomOf="@id/idTextView"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp" />
android:layout_marginRight="32dp"
android:layout_marginTop="8dp"/>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/bottomInfoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="?attr/colorOnSurface"
android:textSize="16sp"
android:fontFamily="sans-serif-condensed-light"
android:gravity="center"
tools:text="App: Persona 5\nTitle ID: 1337"
app:layout_constraintTop_toBottomOf="@id/stateIndicatorImageView"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:maxLines="2"
android:ellipsize="end"/>
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -12,7 +12,9 @@
<string name="action_settings">Settings</string>
<string name="action_discover">Discover Consoles Automatically</string>
<string name="display_host_host">Address: %s</string>
<string name="display_host_id">ID: %s</string>
<string name="display_host_id_unregistered">ID: %s</string>
<string name="display_host_id_registered">ID: %s (registered)</string>
<string name="display_host_app_title_id">App: %s\nTitle ID: %s</string>
<string name="action_register">Register Console</string>
<string name="action_add_manual">Add Console Manually</string>
<string name="regist_pin_instructions_before">On the PS4, navigate to</string>