Add Room to Android

This commit is contained in:
Florian Märkl 2019-10-02 21:31:43 +02:00
commit dae2a03589
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
7 changed files with 225 additions and 23 deletions

View file

@ -1,8 +1,7 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 29
@ -59,4 +58,13 @@ dependencies {
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0"
implementation "io.reactivex.rxjava2:rxjava:2.2.12"
implementation "io.reactivex.rxjava2:rxkotlin:2.4.0"
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
def room_version = "2.2.0-rc01"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
}

View file

@ -0,0 +1,54 @@
/*
* 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
import android.content.Context
import androidx.room.*
@Database(
version = 1,
entities = [RegisteredHost::class, ManualHost::class])
@TypeConverters(Converters::class)
abstract class AppDatabase: RoomDatabase()
{
abstract fun registeredHostDao(): RegisteredHostDao
abstract fun manualHostDao(): ManualHostDao
}
private var database: AppDatabase? = null
fun getDatabase(context: Context): AppDatabase
{
val currentDb = database
if(currentDb != null)
return currentDb
val db = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"chiaki").build()
database = db
return db
}
private class Converters
{
@TypeConverter
fun macFromValue(v: Long) = MacAddress(v)
@TypeConverter
fun macToValue(addr: MacAddress) = addr.value
}

View file

@ -17,31 +17,19 @@
package com.metallic.chiaki.common
import android.net.MacAddress
import com.metallic.chiaki.lib.DiscoveryHost
data class RegisteredHost(
val apSsid: String?,
val apBssid: String?,
val apKey: String?,
val apName: String?,
val ps4Mac: MacAddress,
val ps4Nickname: String?,
val rpRegistKey: ByteArray, // CHIAKI_SESSION_AUTH_SIZE
val rpKeyType: UInt,
val rpKey: ByteArray // 0x10
)
sealed class DisplayHost(
val registeredHost: RegisteredHost?
)
sealed class DisplayHost
{
abstract val registeredHost: RegisteredHost?
}
class DiscoveredDisplayServer(
registeredHost: RegisteredHost?,
override val registeredHost: RegisteredHost?,
val discoveredHost: DiscoveryHost
): DisplayHost(registeredHost)
): DisplayHost()
class ManualDisplayServer(
registeredHost: RegisteredHost?,
val manualHost: Int
): DisplayHost(registeredHost)
override val registeredHost: RegisteredHost?,
val manualHost: ManualHost
): DisplayHost()

View file

@ -0,0 +1,31 @@
/*
* 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
class MacAddress(v: Long)
{
val value: Long = v and 0xffffffffffff
override fun equals(other: Any?): Boolean =
if(other is MacAddress)
other.value == value
else
super.equals(other)
override fun hashCode() = value.hashCode()
}

View file

@ -0,0 +1,48 @@
/*
* 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
import androidx.room.*
import androidx.room.ForeignKey.SET_NULL
import io.reactivex.Completable
import io.reactivex.Flowable
@Entity(tableName = "manual_host",
foreignKeys = [
ForeignKey(
entity = RegisteredHost::class,
parentColumns = ["id"],
childColumns = ["registered_host"],
onDelete = SET_NULL
)
])
data class ManualHost(
@PrimaryKey(autoGenerate = true) val id: Int = 0,
val host: String,
@ColumnInfo(name = "registered_host") val registeredHost: Int?
)
@Dao
interface ManualHostDao
{
@Query("SELECT * FROM manual_host")
fun getAll(): Flowable<List<ManualHost>>
@Insert
fun insert(host: ManualHost): Completable
}

View file

@ -0,0 +1,44 @@
/*
* 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
import androidx.room.*
import androidx.room.ColumnInfo.BLOB
import io.reactivex.Flowable
@Suppress("ArrayInDataClass")
@Entity(tableName = "registered_host")
data class RegisteredHost(
@PrimaryKey(autoGenerate = true) val id: Int = 0,
@ColumnInfo(name = "ap_ssid") val apSsid: String?,
@ColumnInfo(name = "ap_bssid") val apBssid: String?,
@ColumnInfo(name = "ap_key") val apKey: String?,
@ColumnInfo(name = "ap_name") val apName: String?,
@ColumnInfo(name = "ps4_mac") val ps4Mac: MacAddress,
@ColumnInfo(name = "ps4_nickname") val ps4Nickname: String?,
@ColumnInfo(name = "rp_regist_key", typeAffinity = BLOB) val rpRegistKey: ByteArray, // CHIAKI_SESSION_AUTH_SIZE
@ColumnInfo(name = "rp_key_type") val rpKeyType: Int,
@ColumnInfo(name = "rp_key", typeAffinity = BLOB) val rpKey: ByteArray // 0x10
)
@Dao
interface RegisteredHostDao
{
@Query("SELECT * FROM registered_host")
fun getAll(): Flowable<List<RegisteredHost>>
}

View file

@ -21,12 +21,22 @@ import android.app.ActivityOptions
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.metallic.chiaki.R
import com.metallic.chiaki.TestStartActivity
import com.metallic.chiaki.common.ManualHost
import com.metallic.chiaki.common.getDatabase
import io.reactivex.Completable
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity()
{
private val disposable = CompositeDisposable()
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
@ -39,5 +49,24 @@ class MainActivity : AppCompatActivity()
startActivity(it, ActivityOptions.makeSceneTransitionAnimation(this).toBundle())
}
}
/*val db = getDatabase(this)
Completable.mergeArray(
db.manualHostDao().insert(ManualHost(host = "test", registeredHost = null)),
db.manualHostDao().insert(ManualHost(host = "adsgsdfgdsfg", registeredHost = null)),
db.manualHostDao().insert(ManualHost(host = "sdfgsdfg", registeredHost = null))
).andThen(db.manualHostDao().getAll())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
Log.i("MainActivity", "got $it")
}
.also { disposable.add(it) }*/
}
override fun onDestroy()
{
super.onDestroy()
disposable.dispose()
}
}