Skip to content

Commit

Permalink
remove profile change
Browse files Browse the repository at this point in the history
  • Loading branch information
Asutorufa committed May 4, 2024
1 parent d359a24 commit 7d6ee57
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import android.view.Window
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.logviewer.ReadLogcat.Companion.ignore
import io.github.asutorufa.yuhaiin.database.Manager
import io.github.asutorufa.yuhaiin.databinding.ApplistDialogFragmentBinding
import kotlinx.coroutines.*

Expand All @@ -39,7 +38,7 @@ class AppListDialogFragment : DialogFragment() {
ignore {
val list = packages
requireActivity().runOnUiThread {
this@AppListDialogFragment.adapter.setAppList(list, Manager.profile.appList)
this@AppListDialogFragment.adapter.setAppList(list, MainApplication.profile.appList)
appListBinding.appListProgressIndicator.visibility = View.GONE
}
}
Expand All @@ -58,8 +57,8 @@ class AppListDialogFragment : DialogFragment() {
override fun onPause() {
Log.d("appListFragment", "onPause: ${adapter.checkedApps}")
adapter.checkedApps?.let {
Manager.profile.appList = it
Manager.db.updateProfile(Manager.profile)
MainApplication.profile.appList = it
MainApplication.db.updateProfile(MainApplication.profile)
}
super.onPause()
}
Expand Down Expand Up @@ -88,7 +87,7 @@ class AppListDialogFragment : DialogFragment() {
else
packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS)

val checkedApps = Manager.profile.appList
val checkedApps = MainApplication.profile.appList

val apps = mutableListOf<AppList>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.content.Intent
import android.net.VpnService
import android.util.Log
import androidx.core.content.ContextCompat
import io.github.asutorufa.yuhaiin.database.Manager
import io.github.asutorufa.yuhaiin.service.YuhaiinVpnService

class BootReceiver : BroadcastReceiver() {
Expand All @@ -15,7 +14,7 @@ class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (
Intent.ACTION_BOOT_COMPLETED == intent.action
&& Manager.profile.autoConnect
&& MainApplication.profile.autoConnect
&& VpnService.prepare(context) == null
) {
Log.d(tag, "starting VPN service on boot")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ 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.database.Manager.profile
import io.github.asutorufa.yuhaiin.database.Manager.setOnPreferenceChangeListener
import io.github.asutorufa.yuhaiin.MainApplication.Companion.profile
import io.github.asutorufa.yuhaiin.MainApplication.Companion.setOnPreferenceChangeListener


class DnsFragment : PreferenceFragmentCompat() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import io.github.asutorufa.yuhaiin.database.Manager
import io.github.asutorufa.yuhaiin.databinding.HostsDialogBinding
import io.github.asutorufa.yuhaiin.databinding.ItemRecyclerHostsBinding

Expand All @@ -37,8 +36,8 @@ class HostsDialogFragment : DialogFragment() {
override fun onPause() {
Log.d("appListFragment", "onPause: ${adapter.hostsMap}")
adapter.hostsMap.let {
Manager.profile.hosts = it
Manager.db.updateProfile(Manager.profile)
MainApplication.profile.hosts = it
MainApplication.db.updateProfile(MainApplication.profile)
}
super.onPause()
}
Expand All @@ -54,7 +53,7 @@ class HostsDialogFragment : DialogFragment() {
layoutManager = LinearLayoutManager(hostsDialogFragmentBinding.root.context)
adapter = this@HostsDialogFragment.adapter

this@HostsDialogFragment.adapter.setHostsList(Manager.profile.hosts.toMutableMap())
this@HostsDialogFragment.adapter.setHostsList(MainApplication.profile.hosts.toMutableMap())
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
import androidx.preference.PreferenceDataStore
import com.google.android.material.snackbar.Snackbar
import io.github.asutorufa.yuhaiin.database.Manager
import io.github.asutorufa.yuhaiin.databinding.MainActivityBinding
import io.github.asutorufa.yuhaiin.service.YuhaiinVpnService
import io.github.asutorufa.yuhaiin.service.YuhaiinVpnService.Companion.State
Expand All @@ -52,7 +51,7 @@ class MainActivity : AppCompatActivity() {
.apply {
setColorScheme(CustomTabsIntent.COLOR_SCHEME_SYSTEM)
}.build().apply {
intent.data = Uri.parse("http://127.0.0.1:${Manager.profile.yuhaiinPort}")
intent.data = Uri.parse("http://127.0.0.1:${MainApplication.profile.yuhaiinPort}")
this@MainActivity.startActivity(intent)
}
}
Expand Down Expand Up @@ -271,7 +270,7 @@ class MainActivity : AppCompatActivity() {
this,
YuhaiinVpnService::class.java
).apply {
putExtra("profile", Json.encodeToString(Manager.profile))
putExtra("profile", Json.encodeToString(MainApplication.profile))
}
)
}
Expand All @@ -295,7 +294,7 @@ class MainActivity : AppCompatActivity() {
VpnService.prepare(this)?.apply {
vpnPermissionDialogLauncher.launch(this)
} ?: startService(Intent(this, YuhaiinVpnService::class.java).apply {
putExtra("profile", Json.encodeToString(Manager.profile))
putExtra("profile", Json.encodeToString(MainApplication.profile))
})

}
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/kotlin/io/github/asutorufa/yuhaiin/MainApplication.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
package io.github.asutorufa.yuhaiin

import android.app.Application
import androidx.preference.Preference
import com.google.android.material.color.DynamicColors
import go.Seq
import io.github.asutorufa.yuhaiin.database.Profile
import io.github.asutorufa.yuhaiin.database.ProfileDao
import io.github.asutorufa.yuhaiin.database.YuhaiinDatabase

open class MainApplication : Application() {

companion object {
lateinit var db: ProfileDao
lateinit var profile:Profile

fun setOnPreferenceChangeListener(
it: Preference,
run: (p: Preference, newValue: Any) -> Unit
) {
it.setOnPreferenceChangeListener { preference, newValue ->
try {
run(preference, newValue)
db.updateProfile(profile)
true
} catch (e: Exception) {
false
}
}
}
}

override fun onCreate() {
super.onCreate()

Seq.setContext(this)
db = YuhaiinDatabase.getInstance(applicationContext).ProfileDao()
profile = db.getProfileByName(db.getLastProfile() ?: "Default")
DynamicColors.applyToActivitiesIfAvailable(this)
}
}
104 changes: 3 additions & 101 deletions app/src/main/kotlin/io/github/asutorufa/yuhaiin/ProfileFragment.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package io.github.asutorufa.yuhaiin

import android.os.Bundle
import android.text.InputType
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.widget.EditText
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.view.MenuProvider
import androidx.lifecycle.Lifecycle
import androidx.navigation.fragment.findNavController
import androidx.preference.EditTextPreference
import androidx.preference.ListPreference
Expand All @@ -20,9 +12,8 @@ import androidx.preference.SwitchPreferenceCompat
import com.github.logviewer.LogcatActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.transition.platform.MaterialSharedAxis
import io.github.asutorufa.yuhaiin.database.Manager
import io.github.asutorufa.yuhaiin.database.Manager.profile
import io.github.asutorufa.yuhaiin.database.Manager.setOnPreferenceChangeListener
import io.github.asutorufa.yuhaiin.MainApplication.Companion.profile
import io.github.asutorufa.yuhaiin.MainApplication.Companion.setOnPreferenceChangeListener
import io.github.asutorufa.yuhaiin.databinding.PortsDialogBinding

class ProfileFragment : PreferenceFragmentCompat() {
Expand All @@ -45,7 +36,6 @@ class ProfileFragment : PreferenceFragmentCompat() {
super.onViewCreated(view, savedInstanceState)
view.transitionName = "transition_common"

mainActivity.addMenuProvider(menuProvider, viewLifecycleOwner, Lifecycle.State.RESUMED)
preferenceManager.preferenceDataStore = mainActivity.dataStore
reload()
}
Expand Down Expand Up @@ -74,101 +64,15 @@ class ProfileFragment : PreferenceFragmentCompat() {
show()
}

private val menuProvider = object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) =
menuInflater.inflate(R.menu.main, menu)

override fun onMenuItemSelected(menuItem: MenuItem): Boolean =
when (menuItem.itemId) {
R.id.prof_add -> {
val e = AppCompatEditText(requireContext()).apply { isSingleLine = true }
showAlertDialog(R.string.prof_add, e, null) {
val name = e.text.toString()
if (name.isEmpty()) return@showAlertDialog
try {
Manager.addProfile(name)
} catch (e: Exception) {
e.message?.let { mainActivity.showSnackBar(it) }
}
reload()
}
true
}

R.id.prof_del -> {
showAlertDialog(
R.string.prof_del,
null,
String.format(getString(R.string.prof_del_confirm), profile.name)
) {
try {
Manager.deleteProfile()
reload()
} catch (e: Exception) {
e.message?.let { mainActivity.showSnackBar(it) }
}
}
true
}

else -> false
}
}

private fun initPreferences() {
findPreference<ListPreference>(resources.getString(R.string.profile_key))!!.also {
refreshPreferences.add {
it.value = profile.name
val profiles = Manager.getProfileNames()
it.entries = profiles.toTypedArray()
it.entryValues = profiles.toTypedArray()
}

setOnPreferenceChangeListener(it) { _, newValue ->
Manager.switchProfile(newValue as String)
reload()
}
}

findPreference<SwitchPreferenceCompat>(resources.getString(R.string.append_http_proxy_to_vpn_key))!!.also {
setOnPreferenceChangeListener(it) { _, newValue ->
profile.appendHttpProxyToSystem = newValue as Boolean
}
refreshPreferences.add { it.isChecked = profile.appendHttpProxyToSystem }
}
//
// findPreference<SwitchPreferenceCompat>(resources.getString(R.string.auth_userpw_key))!!.also {
// setOnPreferenceChangeListener(it) { _, newValue ->
// profile.isUserPw = newValue as Boolean
// reload()
// }
// refreshPreferences.add { it.isChecked = profile.isUserPw }
// }
//
// findPreference<EditTextPreference>(resources.getString(R.string.auth_username_key))!!.also {
// setOnPreferenceChangeListener(it) { _, newValue ->
// profile.username = newValue.toString()
// }
// refreshPreferences.add {
// it.text = profile.username
// it.isVisible = profile.isUserPw
// }
// }
//
// findPreference<EditTextPreference>(resources.getString(R.string.auth_password_key))!!.also {
// it.setOnBindEditTextListener { editText: EditText ->
// editText.inputType =
// InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
// }
//
// setOnPreferenceChangeListener(it) { _, newValue ->
// profile.password = newValue.toString()
// }
// refreshPreferences.add {
// it.text = profile.password
// it.isVisible = profile.isUserPw
// }
// }

findPreference<ListPreference>(resources.getString(R.string.adv_tun_driver_key))!!.also {
setOnPreferenceChangeListener(it) { _, newValue ->
Expand Down Expand Up @@ -269,18 +173,16 @@ class ProfileFragment : PreferenceFragmentCompat() {

val bind = PortsDialogBinding.inflate(requireActivity().layoutInflater, null, false)

// bind.socks5.setText(profile.socks5ServerPort.toString())
bind.http.setText(profile.httpServerPort.toString())
bind.yuhaiin.setText(profile.yuhaiinPort.toString())
showAlertDialog(
R.string.ports_title,
bind.root,
null
) {
// profile.socks5ServerPort = bind.socks5.text.toString().toInt()
profile.httpServerPort = bind.http.text.toString().toInt()
profile.yuhaiinPort = bind.yuhaiin.text.toString().toInt()
Manager.db.updateProfile(profile)
MainApplication.db.updateProfile(profile)
}
true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import androidx.preference.*
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.transition.platform.MaterialSharedAxis
import io.github.asutorufa.yuhaiin.database.Bypass
import io.github.asutorufa.yuhaiin.database.Manager.profile
import io.github.asutorufa.yuhaiin.database.Manager.setOnPreferenceChangeListener
import io.github.asutorufa.yuhaiin.MainApplication.Companion.profile
import io.github.asutorufa.yuhaiin.MainApplication.Companion.setOnPreferenceChangeListener
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.room.TypeConverter
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json


object Converters {
@TypeConverter
fun fromString(value: String?): Set<String> {
Expand Down
Loading

0 comments on commit 7d6ee57

Please sign in to comment.