Skip to content

Commit

Permalink
Add switching theme dark or light
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Apr 19, 2024
1 parent 70e9320 commit b2fc6dc
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 61 deletions.
3 changes: 3 additions & 0 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/AngApplication.kt
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import androidx.multidex.MultiDexApplication
import androidx.work.Configuration
import com.tencent.mmkv.MMKV
import com.v2ray.ang.util.Utils

class AngApplication : MultiDexApplication(), Configuration.Provider {
companion object {
Expand Down Expand Up @@ -31,6 +32,8 @@ class AngApplication : MultiDexApplication(), Configuration.Provider {

//Logger.init().logLevel(if (BuildConfig.DEBUG) LogLevel.FULL else LogLevel.NONE)
MMKV.initialize(this)

Utils.setNightMode(application)
}

override fun getWorkManagerConfiguration(): Configuration {
Expand Down
1 change: 1 addition & 0 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/AppConfig.kt
Expand Up @@ -49,6 +49,7 @@ object AppConfig {
const val PREF_CONFIRM_REMOVE = "pref_confirm_remove"
const val PREF_START_SCAN_IMMEDIATE = "pref_start_scan_immediate"
const val PREF_LANGUAGE = "pref_language"
const val PREF_UI_MODE_NIGHT = "pref_ui_mode_night"

const val PREF_PREFER_IPV6 = "pref_prefer_ipv6"
const val PREF_PROXY_SHARING = "pref_proxy_sharing_enabled"
Expand Down
102 changes: 51 additions & 51 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SettingsActivity.kt
Expand Up @@ -68,11 +68,61 @@ class SettingsActivity : BaseActivity() {
override fun onCreatePreferences(bundle: Bundle?, s: String?) {
addPreferencesFromResource(R.xml.pref_settings)

perAppProxy?.setOnPreferenceClickListener {
startActivity(Intent(activity, PerAppProxyActivity::class.java))
perAppProxy?.isChecked = true
false
}
localDns?.setOnPreferenceChangeListener { _, any ->
updateLocalDns(any as Boolean)
true
}
localDnsPort?.setOnPreferenceChangeListener { _, any ->
val nval = any as String
localDnsPort?.summary =
if (TextUtils.isEmpty(nval)) AppConfig.PORT_LOCAL_DNS else nval
true
}
vpnDns?.setOnPreferenceChangeListener { _, any ->
vpnDns?.summary = any as String
true
}

routingCustom?.setOnPreferenceClickListener {
startActivity(Intent(activity, RoutingSettingsActivity::class.java))
false
}

mux?.setOnPreferenceChangeListener { _, newValue ->
updateMux(newValue as Boolean)
true
}
muxConcurrency?.setOnPreferenceChangeListener { _, newValue ->
updateMuxConcurrency(newValue as String)
true
}
muxXudpConcurrency?.setOnPreferenceChangeListener { _, newValue ->
updateMuxXudpConcurrency(newValue as String)
true
}

fragment?.setOnPreferenceChangeListener { _, newValue ->
updateFragment(newValue as Boolean)
true
}
fragmentPackets?.setOnPreferenceChangeListener { _, newValue ->
updateFragmentPackets(newValue as String)
true
}
fragmentLength?.setOnPreferenceChangeListener { _, newValue ->
updateFragmentLength(newValue as String)
true
}
fragmentInterval?.setOnPreferenceChangeListener { _, newValue ->
updateFragmentInterval(newValue as String)
true
}

autoUpdateCheck?.setOnPreferenceChangeListener { _, newValue ->
val value = newValue as Boolean
autoUpdateCheck?.isChecked = value
Expand All @@ -82,7 +132,6 @@ class SettingsActivity : BaseActivity() {
}
true
}

autoUpdateInterval?.setOnPreferenceChangeListener { _, any ->
var nval = any as String

Expand All @@ -94,13 +143,6 @@ class SettingsActivity : BaseActivity() {
true
}


perAppProxy?.setOnPreferenceClickListener {
startActivity(Intent(activity, PerAppProxyActivity::class.java))
perAppProxy?.isChecked = true
false
}

remoteDns?.setOnPreferenceChangeListener { _, any ->
// remoteDns.summary = any as String
val nval = any as String
Expand All @@ -113,21 +155,6 @@ class SettingsActivity : BaseActivity() {
domesticDns?.summary = if (nval == "") AppConfig.DNS_DIRECT else nval
true
}

localDns?.setOnPreferenceChangeListener { _, any ->
updateLocalDns(any as Boolean)
true
}
localDnsPort?.setOnPreferenceChangeListener { _, any ->
val nval = any as String
localDnsPort?.summary =
if (TextUtils.isEmpty(nval)) AppConfig.PORT_LOCAL_DNS else nval
true
}
vpnDns?.setOnPreferenceChangeListener { _, any ->
vpnDns?.summary = any as String
true
}
socksPort?.setOnPreferenceChangeListener { _, any ->
val nval = any as String
socksPort?.summary = if (TextUtils.isEmpty(nval)) AppConfig.PORT_SOCKS else nval
Expand All @@ -144,35 +171,7 @@ class SettingsActivity : BaseActivity() {
}
mode?.dialogLayoutResource = R.layout.preference_with_help_link
//loglevel.summary = "LogLevel"
mux?.setOnPreferenceChangeListener { _, newValue ->
updateMux(newValue as Boolean)
true
}
muxConcurrency?.setOnPreferenceChangeListener { _, newValue ->
updateMuxConcurrency(newValue as String)
true
}
muxXudpConcurrency?.setOnPreferenceChangeListener { _, newValue ->
updateMuxXudpConcurrency(newValue as String)
true
}

fragment?.setOnPreferenceChangeListener { _, newValue ->
updateFragment(newValue as Boolean)
true
}
fragmentPackets?.setOnPreferenceChangeListener { _, newValue ->
updateFragmentPackets(newValue as String)
true
}
fragmentLength?.setOnPreferenceChangeListener { _, newValue ->
updateFragmentLength(newValue as String)
true
}
fragmentInterval?.setOnPreferenceChangeListener { _, newValue ->
updateFragmentInterval(newValue as String)
true
}
}

override fun onStart() {
Expand Down Expand Up @@ -249,6 +248,7 @@ class SettingsActivity : BaseActivity() {
AppConfig.PREF_MUX_XUDP_QUIC,
AppConfig.PREF_FRAGMENT_PACKETS,
AppConfig.PREF_LANGUAGE,
AppConfig.PREF_UI_MODE_NIGHT,
AppConfig.PREF_LOGLEVEL,
AppConfig.PREF_MODE
).forEach { key ->
Expand Down
9 changes: 9 additions & 0 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt
Expand Up @@ -16,6 +16,7 @@ import android.util.Base64
import android.util.Log
import android.util.Patterns
import android.webkit.URLUtil
import androidx.appcompat.app.AppCompatDelegate
import com.tencent.mmkv.MMKV
import com.v2ray.ang.AppConfig
import com.v2ray.ang.AppConfig.ANG_PACKAGE
Expand Down Expand Up @@ -371,6 +372,14 @@ object Utils {
return mode != UI_MODE_NIGHT_NO
}

fun setNightMode(context: Context) {
when (settingsStorage?.decodeString(AppConfig.PREF_UI_MODE_NIGHT, "0")) {
"0" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
"1" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
"2" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
}

fun getIpv6Address(address: String): String {
return if (isIpv6Address(address) && !address.contains('[') && !address.contains(']')) {
String.format("[%s]", address)
Expand Down
Expand Up @@ -8,24 +8,33 @@ import androidx.preference.PreferenceManager
import com.tencent.mmkv.MMKV
import com.v2ray.ang.AppConfig
import com.v2ray.ang.util.MmkvManager
import com.v2ray.ang.util.Utils

class SettingsViewModel(application: Application) : AndroidViewModel(application), SharedPreferences.OnSharedPreferenceChangeListener {
class SettingsViewModel(application: Application) : AndroidViewModel(application),
SharedPreferences.OnSharedPreferenceChangeListener {

private val settingsStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_SETTING, MMKV.MULTI_PROCESS_MODE) }
private val settingsStorage by lazy {
MMKV.mmkvWithID(
MmkvManager.ID_SETTING,
MMKV.MULTI_PROCESS_MODE
)
}

fun startListenPreferenceChange() {
PreferenceManager.getDefaultSharedPreferences(getApplication()).registerOnSharedPreferenceChangeListener(this)
PreferenceManager.getDefaultSharedPreferences(getApplication())
.registerOnSharedPreferenceChangeListener(this)
}

override fun onCleared() {
PreferenceManager.getDefaultSharedPreferences(getApplication()).unregisterOnSharedPreferenceChangeListener(this)
PreferenceManager.getDefaultSharedPreferences(getApplication())
.unregisterOnSharedPreferenceChangeListener(this)
Log.i(AppConfig.ANG_PACKAGE, "Settings ViewModel is cleared")
super.onCleared()
}

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String?) {
Log.d(AppConfig.ANG_PACKAGE, "Observe settings changed: $key")
when(key) {
when (key) {
AppConfig.PREF_MODE,
AppConfig.PREF_VPN_DNS,
AppConfig.PREF_REMOTE_DNS,
Expand All @@ -35,6 +44,7 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
AppConfig.PREF_HTTP_PORT,
AppConfig.PREF_LOGLEVEL,
AppConfig.PREF_LANGUAGE,
AppConfig.PREF_UI_MODE_NIGHT,
AppConfig.PREF_ROUTING_DOMAIN_STRATEGY,
AppConfig.PREF_ROUTING_MODE,
AppConfig.PREF_V2RAY_ROUTING_AGENT,
Expand All @@ -44,9 +54,11 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
AppConfig.PREF_FRAGMENT_PACKETS,
AppConfig.PREF_FRAGMENT_LENGTH,
AppConfig.PREF_FRAGMENT_INTERVAL,
AppConfig.PREF_MUX_XUDP_QUIC, -> {
AppConfig.PREF_MUX_XUDP_QUIC,
-> {
settingsStorage?.encode(key, sharedPreferences.getString(key, ""))
}

AppConfig.PREF_SPEED_ENABLED,
AppConfig.PREF_PROXY_SHARING,
AppConfig.PREF_LOCAL_DNS_ENABLED,
Expand All @@ -59,19 +71,29 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
AppConfig.PREF_START_SCAN_IMMEDIATE,
AppConfig.SUBSCRIPTION_AUTO_UPDATE,
AppConfig.PREF_FRAGMENT_ENABLED,
AppConfig.PREF_MUX_ENABLED, -> {
AppConfig.PREF_MUX_ENABLED,
-> {
settingsStorage?.encode(key, sharedPreferences.getBoolean(key, false))
}

AppConfig.PREF_SNIFFING_ENABLED -> {
settingsStorage?.encode(key, sharedPreferences.getBoolean(key, true))
}

AppConfig.PREF_MUX_CONCURRENCY,
AppConfig.PREF_MUX_XUDP_CONCURRENCY -> {
settingsStorage?.encode(key, sharedPreferences.getString(key, "8")?.toIntOrNull() ?: 8)
settingsStorage?.encode(
key,
sharedPreferences.getString(key, "8")?.toIntOrNull() ?: 8
)
}

AppConfig.PREF_PER_APP_PROXY_SET -> {
settingsStorage?.encode(key, sharedPreferences.getStringSet(key, setOf()))
}
}
if (key == AppConfig.PREF_UI_MODE_NIGHT) {
Utils.setNightMode(getApplication())
}
}
}
7 changes: 7 additions & 0 deletions V2rayNG/app/src/main/res/values-ar/strings.xml
Expand Up @@ -163,6 +163,7 @@
<string name="title_mode_help">انقر علي للمزيد من المساعدة</string>
<string name="title_language">اللغة</string>
<string name="title_ui_settings">إعدادات واجهة المستخدم</string>
<string name="title_pref_ui_mode_night">UI mode settings</string>
<string name="title_logcat">Logcat</string>
<string name="logcat_copy">نسخ</string>
<string name="logcat_clear">مسح</string>
Expand Down Expand Up @@ -230,4 +231,10 @@
</string-array>
<string name="menu_item_add_asset">يضيف</string>
<string name="menu_item_add_url">إضافة رابط</string>

<string-array name="ui_mode_night">
<item>Follow system</item>
<item>Light</item>
<item>Dark</item>
</string-array>
</resources>
7 changes: 7 additions & 0 deletions V2rayNG/app/src/main/res/values-fa/strings.xml
Expand Up @@ -188,6 +188,7 @@
<string name="title_mode_help">برای راهنمایی بیشتر روی این متن، کلیک کنید</string>
<string name="title_language">زبان</string>
<string name="title_ui_settings">تنظیمات رابط کاربری</string>
<string name="title_pref_ui_mode_night">UI mode settings</string>

<string name="title_logcat">گزارشات</string>
<string name="logcat_copy">کپی</string>
Expand Down Expand Up @@ -270,4 +271,10 @@
<string name="menu_item_add_asset">افزودن</string>
<string name="menu_item_add_url">افزودن لینک</string>

<string-array name="ui_mode_night">
<item>Follow system</item>
<item>Light</item>
<item>Dark</item>
</string-array>

</resources>
7 changes: 7 additions & 0 deletions V2rayNG/app/src/main/res/values-ru/strings.xml
Expand Up @@ -192,6 +192,7 @@
<string name="title_mode_help">Нажмите для получения дополнительной информации</string>
<string name="title_language">Язык</string>
<string name="title_ui_settings">Настройки интерфейса</string>
<string name="title_pref_ui_mode_night">UI mode settings</string>

<string name="title_logcat">Журнал</string>
<string name="logcat_copy">Копировать</string>
Expand Down Expand Up @@ -273,4 +274,10 @@
<item>Только прокси</item>
</string-array>

<string-array name="ui_mode_night">
<item>Follow system</item>
<item>Light</item>
<item>Dark</item>
</string-array>

</resources>
7 changes: 7 additions & 0 deletions V2rayNG/app/src/main/res/values-vi/strings.xml
Expand Up @@ -191,6 +191,7 @@
<string name="title_mode_help">Nhấn vào đây nếu bạn cần trợ giúp!</string>
<string name="title_language">Ngôn ngữ</string>
<string name="title_ui_settings">Cài đặt giao diện</string>
<string name="title_pref_ui_mode_night">UI mode settings</string>

<string name="title_logcat">Logcat</string>
<string name="logcat_copy">Sao chép</string>
Expand Down Expand Up @@ -269,4 +270,10 @@
<string name="menu_item_add_asset">Thêm vào</string>
<string name="menu_item_add_url">Thêm liên kết</string>

<string-array name="ui_mode_night">
<item>Follow system</item>
<item>Light</item>
<item>Dark</item>
</string-array>

</resources>
7 changes: 7 additions & 0 deletions V2rayNG/app/src/main/res/values-zh-rCN/strings.xml
Expand Up @@ -190,6 +190,7 @@
<string name="title_mode_help">点此查看更多帮助</string>
<string name="title_language">语言</string>
<string name="title_ui_settings">用户界面设置</string>
<string name="title_pref_ui_mode_night">界面颜色设置</string>

<string name="title_logcat">Logcat</string>
<string name="logcat_copy">复制</string>
Expand Down Expand Up @@ -274,4 +275,10 @@
<item>仅代理</item>
</string-array>

<string-array name="ui_mode_night">
<item>跟随系统</item>
<item>浅色</item>
<item>深色</item>
</string-array>

</resources>

0 comments on commit b2fc6dc

Please sign in to comment.