Skip to content

Commit

Permalink
replace room to bboltdb
Browse files Browse the repository at this point in the history
  • Loading branch information
Asutorufa committed Sep 7, 2024
1 parent 8238a3b commit beddeac
Show file tree
Hide file tree
Showing 23 changed files with 271 additions and 887 deletions.
10 changes: 2 additions & 8 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import java.util.Date
plugins {
id("com.android.application")
kotlin("android")
id("com.google.devtools.ksp")
id("androidx.navigation.safeargs.kotlin")
kotlin("plugin.serialization") version "2.0.20"
}
Expand Down Expand Up @@ -174,13 +173,8 @@ dependencies {
implementation("androidx.browser:browser:1.8.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.2")

// room
val roomVersion = "2.6.1"
implementation("androidx.room:room-runtime:$roomVersion")
ksp("androidx.room:room-compiler:$roomVersion")

// nav
val navVersion = "2.7.7"
val navVersion = "2.8.0"
implementation("androidx.navigation:navigation-fragment-ktx:$navVersion")
implementation("androidx.navigation:navigation-ui-ktx:$navVersion")

Expand All @@ -203,7 +197,7 @@ dependencies {
*/

testImplementation("androidx.test:core:1.6.1")
testImplementation("androidx.test:runner:1.6.1")
testImplementation("androidx.test:runner:1.6.2")
testImplementation("junit:junit:4.13.2")
testImplementation("androidx.test.espresso:espresso-core:3.6.1")
testImplementation("androidx.test.ext:junit-ktx:1.2.1")
Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@

<receiver
android:name=".BootReceiver"
android:process=":bg"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.github.logviewer.ReadLogcat.Companion.ignore
import io.github.asutorufa.yuhaiin.databinding.ApplistDialogFragmentBinding
import kotlinx.coroutines.*
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

class AppListDialogFragment : DialogFragment() {

Expand All @@ -38,7 +40,15 @@ class AppListDialogFragment : DialogFragment() {
ignore {
val list = packages
requireActivity().runOnUiThread {
this@AppListDialogFragment.adapter.setAppList(list, MainApplication.profile.appList)
val applist = MainApplication.store.getString("app_list")
var checkedApps = HashSet<String>()
try {
checkedApps = Json.decodeFromString<HashSet<String>>(applist)
} catch (e: Exception) {
Log.d("app_list", e.toString())
}

this@AppListDialogFragment.adapter.setAppList(list, checkedApps)
appListBinding.appListProgressIndicator.visibility = View.GONE
}
}
Expand All @@ -57,8 +67,7 @@ class AppListDialogFragment : DialogFragment() {
override fun onPause() {
Log.d("appListFragment", "onPause: ${adapter.checkedApps}")
adapter.checkedApps?.let {
MainApplication.profile.appList = it
MainApplication.db.updateProfile(MainApplication.profile)
MainApplication.store.putString("app_list", Json.encodeToString(it))
}
super.onPause()
}
Expand Down Expand Up @@ -87,7 +96,13 @@ class AppListDialogFragment : DialogFragment() {
else
packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS)

val checkedApps = MainApplication.profile.appList
val applist = MainApplication.store.getString("app_list")
var checkedApps = HashSet<String>()
try {
checkedApps = Json.decodeFromString<HashSet<String>>(applist)
} catch (e: Exception) {
Log.d("app_list", e.toString())
}

val apps = mutableListOf<AppList>()

Expand Down
12 changes: 7 additions & 5 deletions app/src/main/kotlin/io/github/asutorufa/yuhaiin/BootReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ import android.net.VpnService
import android.util.Log
import androidx.core.content.ContextCompat
import io.github.asutorufa.yuhaiin.service.YuhaiinVpnService
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import yuhaiin.Yuhaiin

class BootReceiver : BroadcastReceiver() {
private val tag = this.javaClass.simpleName

override fun onReceive(context: Context, intent: Intent) {
val autoConnect = MainApplication.store.getBoolean("auto_connect")
val data = MainApplication.store.dump()
Yuhaiin.closeStore()
if (
Intent.ACTION_BOOT_COMPLETED == intent.action
&& MainApplication.profile.autoConnect
&& autoConnect
&& VpnService.prepare(context) == null
) {
Log.d(tag, "starting VPN service on boot")
ContextCompat.startForegroundService(
context,
Intent(context, YuhaiinVpnService::class.java).apply {
putExtra("profile", Json.encodeToString(MainApplication.profile))
putExtra("profile", data)
}
)
}
}
}
}
156 changes: 4 additions & 152 deletions app/src/main/kotlin/io/github/asutorufa/yuhaiin/DnsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ import androidx.preference.ListPreference
import androidx.preference.MultiSelectListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import com.google.android.material.transition.platform.MaterialSharedAxis
import io.github.asutorufa.yuhaiin.database.DNS
import io.github.asutorufa.yuhaiin.MainApplication.Companion.profile
import io.github.asutorufa.yuhaiin.MainApplication.Companion.setOnPreferenceChangeListener


class DnsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) =
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = (activity as MainActivity).dataStore
setPreferencesFromResource(R.xml.dns, rootKey)
}


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
view.transitionName = "transition_common"
Expand All @@ -36,135 +35,12 @@ class DnsFragment : PreferenceFragmentCompat() {
exitTransition = MaterialSharedAxis(MaterialSharedAxis.Y, true)
reenterTransition = MaterialSharedAxis(MaterialSharedAxis.Y, false)

preferenceManager.preferenceDataStore = (activity as MainActivity).dataStore

findPreference<EditTextPreference>(resources.getString(R.string.adv_dns_port_key))!!.apply {
text = profile.dnsPort.toString()
setOnPreferenceChangeListener(this) { _, newValue ->
profile.dnsPort = newValue.toString().toInt()
}
}

findPreference<Preference>(resources.getString(R.string.dns_hosts_key))!!.apply {
setOnPreferenceClickListener {
findNavController().navigate(DnsFragmentDirections.actionDnsFragmentToHostsListFragment())
true
}
}

findPreference<EditTextPreference>(resources.getString(R.string.adv_fake_dns_cidr_key))!!.apply {
text = profile.fakeDnsCidr
setOnPreferenceChangeListener(this) { _, newValue ->
profile.fakeDnsCidr = newValue as String
}
}
findPreference<EditTextPreference>(resources.getString(R.string.adv_fake_dnsv6_cidr_key))!!.apply {
text = profile.fakeDnsv6Cidr
setOnPreferenceChangeListener(this) { _, newValue ->
profile.fakeDnsv6Cidr = newValue as String
}
}

findPreference<SwitchPreferenceCompat>(resources.getString(R.string.dns_hijacking))!!.apply {
isChecked = profile.dnsHijacking
setOnPreferenceChangeListener(this) { _, newValue ->
profile.dnsHijacking = newValue as Boolean
}
}

// remote dns
findPreference<EditTextPreference>(resources.getString(R.string.remote_dns_host_key))!!.apply {
text = profile.remoteDns.host
setOnPreferenceChangeListener(this) { _, newValue ->
profile.remoteDns.host = newValue as String
}
}

findPreference<ListPreference>(resources.getString(R.string.remote_dns_type_key))!!.apply {
value = dnsTypeToStr(DNS.Type.fromInt(profile.remoteDns.type))
setOnPreferenceChangeListener(this) { _, newValue ->
profile.remoteDns.type = strToDNSType(newValue as String).value
}
}

findPreference<EditTextPreference>(resources.getString(R.string.remote_dns_subnet_key))!!.apply {
text = profile.remoteDns.subnet
setOnPreferenceChangeListener(this) { _, newValue ->
profile.remoteDns.subnet = newValue as String
}
}

findPreference<EditTextPreference>(resources.getString(R.string.remote_dns_tls_server_name_key))!!.apply {
text = profile.remoteDns.tlsServerName
setOnPreferenceChangeListener(this) { _, newValue ->
profile.remoteDns.tlsServerName = newValue as String
}
}

findPreference<SwitchPreferenceCompat>(resources.getString(R.string.remote_dns_resolve_domain_key))!!.apply {
isChecked = profile.resolveRemoteDomain
setOnPreferenceChangeListener(this) { _, newValue ->
profile.resolveRemoteDomain = newValue as Boolean
}
}

// localdns
findPreference<EditTextPreference>(resources.getString(R.string.local_dns_host_key))!!.apply {
text = profile.localDns.host
setOnPreferenceChangeListener(this) { _, newValue ->
profile.localDns.host = newValue as String
}
}
findPreference<ListPreference>(resources.getString(R.string.local_dns_type_key))!!.apply {
value = dnsTypeToStr(DNS.Type.fromInt(profile.localDns.type))
setOnPreferenceChangeListener(this) { _, newValue ->
profile.localDns.type = strToDNSType(newValue as String).value
}
}

findPreference<EditTextPreference>(resources.getString(R.string.local_dns_subnet_key))!!.apply {
text = profile.localDns.subnet
setOnPreferenceChangeListener(this) { _, newValue ->
profile.localDns.subnet = newValue as String
}
}

findPreference<EditTextPreference>(resources.getString(R.string.local_dns_tls_server_name_key))!!.apply {
text = profile.localDns.tlsServerName
setOnPreferenceChangeListener(this) { _, newValue ->
profile.localDns.tlsServerName = newValue as String
}
}


// bootstrap
findPreference<EditTextPreference>(resources.getString(R.string.bootstrap_dns_host_key))!!.apply {
text = profile.bootstrapDns.host
setOnPreferenceChangeListener(this) { _, newValue ->
profile.bootstrapDns.host = newValue as String
}
}
findPreference<ListPreference>(resources.getString(R.string.bootstrap_dns_type_key))!!.apply {
value = dnsTypeToStr(DNS.Type.fromInt(profile.bootstrapDns.type))
setOnPreferenceChangeListener(this) { _, newValue ->
profile.bootstrapDns.type = strToDNSType(newValue as String).value
}
}

findPreference<EditTextPreference>(resources.getString(R.string.bootstrap_dns_subnet_key))!!.apply {
text = profile.bootstrapDns.subnet
setOnPreferenceChangeListener(this) { _, newValue ->
profile.bootstrapDns.subnet = newValue as String
}
}

findPreference<EditTextPreference>(resources.getString(R.string.bootstrap_dns_tls_server_name_key))!!.apply {
text = profile.bootstrapDns.tlsServerName
setOnPreferenceChangeListener(this) { _, newValue ->
profile.bootstrapDns.tlsServerName = newValue as String
}
}

}

override fun onDisplayPreferenceDialog(preference: Preference) {
Expand All @@ -175,28 +51,4 @@ class DnsFragment : PreferenceFragmentCompat() {
else -> super.onDisplayPreferenceDialog(preference)
}
}

private fun strToDNSType(str: String): DNS.Type {
return when (str) {
resources.getString(R.string.dns_type_doudp) -> DNS.Type.UDP
resources.getString(R.string.dns_type_dotcp) -> DNS.Type.TCP
resources.getString(R.string.dns_type_doh) -> DNS.Type.HTTPS
resources.getString(R.string.dns_type_dot) -> DNS.Type.TLS
resources.getString(R.string.dns_type_doq) -> DNS.Type.QUIC
resources.getString(R.string.dns_type_doh3) -> DNS.Type.HTTPS3
else -> DNS.Type.Reserve
}
}

private fun dnsTypeToStr(type: DNS.Type): String {
return when (type) {
DNS.Type.UDP -> resources.getString(R.string.dns_type_doudp)
DNS.Type.TCP -> resources.getString(R.string.dns_type_dotcp)
DNS.Type.HTTPS -> resources.getString(R.string.dns_type_doh)
DNS.Type.TLS -> resources.getString(R.string.dns_type_dot)
DNS.Type.QUIC -> resources.getString(R.string.dns_type_doq)
DNS.Type.HTTPS3 -> resources.getString(R.string.dns_type_doh3)
else -> resources.getString(R.string.dns_type_doudp)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import io.github.asutorufa.yuhaiin.databinding.HostsDialogBinding
import io.github.asutorufa.yuhaiin.databinding.ItemRecyclerHostsBinding
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

class HostsDialogFragment : DialogFragment() {

Expand All @@ -36,8 +38,9 @@ class HostsDialogFragment : DialogFragment() {
override fun onPause() {
Log.d("appListFragment", "onPause: ${adapter.hostsMap}")
adapter.hostsMap.let {
MainApplication.profile.hosts = it
MainApplication.db.updateProfile(MainApplication.profile)
MainApplication.store.putString("hosts", Json.encodeToString(it))
// MainApplication.profile.hosts = it
// MainApplication.db.updateProfile(MainApplication.profile)
}
super.onPause()
}
Expand All @@ -53,7 +56,15 @@ class HostsDialogFragment : DialogFragment() {
layoutManager = LinearLayoutManager(hostsDialogFragmentBinding.root.context)
adapter = this@HostsDialogFragment.adapter

this@HostsDialogFragment.adapter.setHostsList(MainApplication.profile.hosts.toMutableMap())
val hosts = MainApplication.store.getString("hosts")
var hostsSet = HashMap<String,String>()
try {
hostsSet = Json.decodeFromString<HashMap<String,String>>(hosts)
}catch (e:Exception){
Log.d("hosts", e.toString())
}

this@HostsDialogFragment.adapter.setHostsList(hostsSet)
}


Expand Down
Loading

0 comments on commit beddeac

Please sign in to comment.