diff --git a/android/app/build.gradle b/android/app/build.gradle index dea9a93..da054bc 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -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" + } diff --git a/android/app/src/main/java/com/metallic/chiaki/common/AppDatabase.kt b/android/app/src/main/java/com/metallic/chiaki/common/AppDatabase.kt new file mode 100644 index 0000000..165574b --- /dev/null +++ b/android/app/src/main/java/com/metallic/chiaki/common/AppDatabase.kt @@ -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 . + */ + +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 +} \ No newline at end of file diff --git a/android/app/src/main/java/com/metallic/chiaki/common/Host.kt b/android/app/src/main/java/com/metallic/chiaki/common/DisplayHost.kt similarity index 61% rename from android/app/src/main/java/com/metallic/chiaki/common/Host.kt rename to android/app/src/main/java/com/metallic/chiaki/common/DisplayHost.kt index e12e653..00f4d21 100644 --- a/android/app/src/main/java/com/metallic/chiaki/common/Host.kt +++ b/android/app/src/main/java/com/metallic/chiaki/common/DisplayHost.kt @@ -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) \ No newline at end of file + override val registeredHost: RegisteredHost?, + val manualHost: ManualHost +): DisplayHost() \ No newline at end of file diff --git a/android/app/src/main/java/com/metallic/chiaki/common/MacAddress.kt b/android/app/src/main/java/com/metallic/chiaki/common/MacAddress.kt new file mode 100644 index 0000000..435a7b2 --- /dev/null +++ b/android/app/src/main/java/com/metallic/chiaki/common/MacAddress.kt @@ -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 . + */ + +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() +} \ No newline at end of file diff --git a/android/app/src/main/java/com/metallic/chiaki/common/ManualHost.kt b/android/app/src/main/java/com/metallic/chiaki/common/ManualHost.kt new file mode 100644 index 0000000..62f9874 --- /dev/null +++ b/android/app/src/main/java/com/metallic/chiaki/common/ManualHost.kt @@ -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 . + */ + +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> + + @Insert + fun insert(host: ManualHost): Completable +} \ No newline at end of file diff --git a/android/app/src/main/java/com/metallic/chiaki/common/RegisteredHost.kt b/android/app/src/main/java/com/metallic/chiaki/common/RegisteredHost.kt new file mode 100644 index 0000000..d985b33 --- /dev/null +++ b/android/app/src/main/java/com/metallic/chiaki/common/RegisteredHost.kt @@ -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 . + */ + +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> +} \ No newline at end of file diff --git a/android/app/src/main/java/com/metallic/chiaki/main/MainActivity.kt b/android/app/src/main/java/com/metallic/chiaki/main/MainActivity.kt index 4311b3c..69c743d 100644 --- a/android/app/src/main/java/com/metallic/chiaki/main/MainActivity.kt +++ b/android/app/src/main/java/com/metallic/chiaki/main/MainActivity.kt @@ -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() } }