Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Improvements MmkvManager
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust authored and AnGgIt86 committed Sep 23, 2024
1 parent 51b4b46 commit 5acae4b
Show file tree
Hide file tree
Showing 25 changed files with 101 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import com.tencent.mmkv.MMKV
import com.neko.v2ray.AppConfig
import com.neko.v2ray.service.V2RayServiceManager
import com.neko.v2ray.util.MmkvManager

import com.neko.v2ray.util.Utils

class TaskerReceiver : BroadcastReceiver() {
private val mainStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_MAIN, MMKV.MULTI_PROCESS_MODE) }

override fun onReceive(context: Context, intent: Intent?) {

Expand All @@ -27,7 +24,7 @@ class TaskerReceiver : BroadcastReceiver() {
if (guid == AppConfig.TASKER_DEFAULT_GUID) {
Utils.startVServiceFromToggle(context)
} else {
mainStorage?.encode(MmkvManager.KEY_SELECTED_SERVER, guid)
MmkvManager.setSelectServer(guid)
V2RayServiceManager.startV2Ray(context)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import com.tencent.mmkv.MMKV
import com.neko.v2ray.AppConfig
import com.neko.v2ray.AppConfig.ANG_PACKAGE
import com.neko.v2ray.AppConfig.TAG_DIRECT
Expand All @@ -24,6 +23,7 @@ import com.neko.v2ray.extension.toast
import com.neko.v2ray.ui.MainActivity
import com.neko.v2ray.util.MessageUtil
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.MmkvManager.settingsStorage
import com.neko.v2ray.util.Utils
import com.neko.v2ray.util.V2rayConfigUtil
import go.Seq
Expand All @@ -46,8 +46,6 @@ object V2RayServiceManager {

val v2rayPoint: V2RayPoint = Libv2ray.newV2RayPoint(V2RayCallback(), Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1)
private val mMsgReceive = ReceiveMessageHandler()
private val mainStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_MAIN, MMKV.MULTI_PROCESS_MODE) }
private val settingsStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_SETTING, MMKV.MULTI_PROCESS_MODE) }

var serviceControl: SoftReference<ServiceControl>? = null
set(value) {
Expand All @@ -64,7 +62,7 @@ object V2RayServiceManager {

fun startV2Ray(context: Context) {
if (v2rayPoint.isRunning) return
val guid = mainStorage?.decodeString(MmkvManager.KEY_SELECTED_SERVER) ?: return
val guid = MmkvManager.getSelectServer() ?: return
val result = V2rayConfigUtil.getV2rayConfig(context, guid)
if (!result.status) return

Expand Down Expand Up @@ -129,7 +127,7 @@ object V2RayServiceManager {

fun startV2rayPoint() {
val service = serviceControl?.get()?.getService() ?: return
val guid = mainStorage?.decodeString(MmkvManager.KEY_SELECTED_SERVER) ?: return
val guid = MmkvManager.getSelectServer() ?: return
val config = MmkvManager.decodeServerConfig(guid) ?: return
if (v2rayPoint.isRunning) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import android.os.ParcelFileDescriptor
import android.os.StrictMode
import android.util.Log
import androidx.annotation.RequiresApi
import com.tencent.mmkv.MMKV
import com.neko.v2ray.AppConfig
import com.neko.v2ray.R
import com.neko.v2ray.dto.ERoutingMode
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.MmkvManager.settingsStorage
import com.neko.v2ray.util.MyContextWrapper
import com.neko.v2ray.util.Utils
import kotlinx.coroutines.CoroutineScope
Expand All @@ -39,7 +38,6 @@ class V2RayVpnService : VpnService(), ServiceControl {
private const val TUN2SOCKS = "libtun2socks.so"
}

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

private lateinit var mInterface: ParcelFileDescriptor
private var isRunning = false
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/kotlin/com/neko/v2ray/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.neko.v2ray.helper.SimpleItemTouchHelperCallback
import com.neko.v2ray.service.V2RayServiceManager
import com.neko.v2ray.util.AngConfigManager
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.MmkvManager.settingsStorage
import com.neko.v2ray.util.Utils
import com.neko.v2ray.viewmodel.MainViewModel
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
Expand Down Expand Up @@ -139,7 +140,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
binding.fab.setOnClickListener {
if (mainViewModel.isRunning.value == true) {
Utils.stopVService(this)
} else if ((MmkvManager.settingsStorage?.decodeString(AppConfig.PREF_MODE) ?: "VPN") == "VPN") {
} else if ((settingsStorage?.decodeString(AppConfig.PREF_MODE) ?: "VPN") == "VPN") {
val intent = VpnService.prepare(this)
if (intent == null) {
startV2Ray()
Expand Down Expand Up @@ -310,7 +311,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
}

fun startV2Ray() {
if (MmkvManager.mainStorage?.decodeString(MmkvManager.KEY_SELECTED_SERVER).isNullOrEmpty()) {
if (MmkvManager.getSelectServer().isNullOrEmpty()) {
return
}
V2RayServiceManager.startV2Ray(this)
Expand Down
13 changes: 7 additions & 6 deletions app/src/main/kotlin/com/neko/v2ray/ui/MainRecyclerAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.neko.v2ray.helper.ItemTouchHelperViewHolder
import com.neko.v2ray.service.V2RayServiceManager
import com.neko.v2ray.util.AngConfigManager
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.MmkvManager.settingsStorage
import com.neko.v2ray.util.Utils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
Expand Down Expand Up @@ -64,7 +65,7 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
} else {
holder.itemMainBinding.tvTestResult.setTextColor(ContextCompat.getColor(mActivity, R.color.colorPing))
}
if (guid == MmkvManager.mainStorage?.decodeString(MmkvManager.KEY_SELECTED_SERVER)) {
if (guid == MmkvManager.getSelectServer()) {
holder.itemMainBinding.layoutIndicator.setBackgroundResource(R.drawable.uwu_selected_indicator)
} else {
holder.itemMainBinding.layoutIndicator.setBackgroundResource(0)
Expand Down Expand Up @@ -132,8 +133,8 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
}
}
holder.itemMainBinding.layoutRemove.setOnClickListener {
if (guid != MmkvManager.mainStorage?.decodeString(MmkvManager.KEY_SELECTED_SERVER)) {
if (MmkvManager.settingsStorage?.decodeBool(AppConfig.PREF_CONFIRM_REMOVE) == true) {
if (guid != MmkvManager.getSelectServer()) {
if (settingsStorage?.decodeBool(AppConfig.PREF_CONFIRM_REMOVE) == true) {
MaterialAlertDialogBuilder(mActivity).setMessage(R.string.del_config_comfirm)
.setPositiveButton(android.R.string.ok) { _, _ ->
removeServer(guid, position)
Expand All @@ -151,9 +152,9 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
}

holder.itemMainBinding.infoContainer.setOnClickListener {
val selected = MmkvManager.mainStorage?.decodeString(MmkvManager.KEY_SELECTED_SERVER)
val selected = MmkvManager.getSelectServer()
if (guid != selected) {
MmkvManager.mainStorage?.encode(MmkvManager.KEY_SELECTED_SERVER, guid)
MmkvManager.setSelectServer(guid)
if (!TextUtils.isEmpty(selected)) {
notifyItemChanged(mActivity.mainViewModel.getPosition(selected.orEmpty()))
}
Expand Down Expand Up @@ -231,7 +232,7 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai

override fun onItemDismiss(position: Int) {
val guid = mActivity.mainViewModel.serversCache.getOrNull(position)?.guid ?: return
if (guid != MmkvManager.mainStorage?.decodeString(MmkvManager.KEY_SELECTED_SERVER)) {
if (guid != MmkvManager.getSelectServer()) {
// mActivity.alert(R.string.del_config_comfirm) {
// positiveButton(android.R.string.ok) {
mActivity.mainViewModel.removeServer(guid)
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/kotlin/com/neko/v2ray/ui/PerAppProxyActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import com.google.android.material.appbar.MaterialToolbar
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.appbar.CollapsingToolbarLayout
import com.tencent.mmkv.MMKV
import com.neko.v2ray.AppConfig
import com.neko.v2ray.AppConfig.ANG_PACKAGE
import com.neko.v2ray.R
Expand All @@ -22,7 +20,7 @@ import com.neko.v2ray.dto.AppInfo
import com.neko.v2ray.extension.toast
import com.neko.v2ray.extension.v2RayApplication
import com.neko.v2ray.util.AppManagerUtil
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.MmkvManager.settingsStorage
import com.neko.v2ray.util.Utils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
Expand All @@ -37,7 +35,6 @@ class PerAppProxyActivity : BaseActivity() {

private var adapter: PerAppProxyAdapter? = null
private var appsAll: List<AppInfo>? = null
private val settingsStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_SETTING, MMKV.MULTI_PROCESS_MODE) }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import androidx.appcompat.app.AppCompatActivity.RESULT_OK
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.tbruyelle.rxpermissions3.RxPermissions
import com.tencent.mmkv.MMKV
import com.neko.v2ray.AppConfig
import com.neko.v2ray.R
import com.neko.v2ray.databinding.FragmentRoutingSettingsBinding
import com.neko.v2ray.extension.toast
import com.neko.v2ray.extension.v2RayApplication
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.MmkvManager.settingsStorage
import com.neko.v2ray.util.Utils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand All @@ -33,8 +33,6 @@ class RoutingSettingsFragment : Fragment() {
private const val routing_arg = "routing_arg"
}

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

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/kotlin/com/neko/v2ray/ui/ScannerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import androidx.activity.result.contract.ActivityResultContracts
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.appbar.CollapsingToolbarLayout
import com.tbruyelle.rxpermissions3.RxPermissions
import com.tencent.mmkv.MMKV
import com.neko.v2ray.AppConfig
import com.neko.v2ray.R
import com.neko.v2ray.extension.toast
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.MmkvManager.settingsStorage
import com.neko.v2ray.util.QRCodeDecoder
import io.github.g00fy2.quickie.QRResult
import io.github.g00fy2.quickie.ScanCustomCode
Expand All @@ -24,7 +23,6 @@ import io.github.g00fy2.quickie.config.ScannerConfig
class ScannerActivity : BaseActivity() {

private val scanQrCode = registerForActivityResult(ScanCustomCode(), ::handleResult)
private val settingsStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_SETTING, MMKV.MULTI_PROCESS_MODE) }

public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
15 changes: 3 additions & 12 deletions app/src/main/kotlin/com/neko/v2ray/ui/ServerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import android.widget.Spinner
import android.widget.TextView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.appbar.MaterialToolbar
import com.tencent.mmkv.MMKV
import com.google.android.material.appbar.CollapsingToolbarLayout
import com.google.android.material.textfield.TextInputLayout
import com.neko.v2ray.AppConfig
Expand All @@ -31,8 +30,7 @@ import com.neko.v2ray.dto.V2rayConfig.Companion.TLS
import com.neko.v2ray.extension.removeWhiteSpace
import com.neko.v2ray.extension.toast
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.MmkvManager.ID_MAIN
import com.neko.v2ray.util.MmkvManager.KEY_SELECTED_SERVER
import com.neko.v2ray.util.MmkvManager.settingsStorage
import com.neko.v2ray.util.SoftInputAssist
import com.neko.v2ray.util.Utils
import com.neko.v2ray.util.Utils.getIpv6Address
Expand All @@ -49,18 +47,11 @@ import com.neko.imageslider.models.SlideModel
class ServerActivity : BaseActivity() {

private lateinit var softInputAssist: SoftInputAssist
private val mainStorage by lazy { MMKV.mmkvWithID(ID_MAIN, MMKV.MULTI_PROCESS_MODE) }
private val settingsStorage by lazy {
MMKV.mmkvWithID(
MmkvManager.ID_SETTING,
MMKV.MULTI_PROCESS_MODE
)
}
private val editGuid by lazy { intent.getStringExtra("guid").orEmpty() }
private val isRunning by lazy {
intent.getBooleanExtra("isRunning", false)
&& editGuid.isNotEmpty()
&& editGuid == mainStorage?.decodeString(KEY_SELECTED_SERVER)
&& editGuid == MmkvManager.getSelectServer()
}
private val createConfigType by lazy {
EConfigType.fromInt(intent.getIntExtra("createConfigType", EConfigType.VMESS.value))
Expand Down Expand Up @@ -646,7 +637,7 @@ class ServerActivity : BaseActivity() {
*/
private fun deleteServer(): Boolean {
if (editGuid.isNotEmpty()) {
if (editGuid != mainStorage?.decodeString(KEY_SELECTED_SERVER)) {
if (editGuid != MmkvManager.getSelectServer()) {
if (settingsStorage?.decodeBool(AppConfig.PREF_CONFIRM_REMOVE) == true) {
MaterialAlertDialogBuilder(this).setMessage(R.string.del_config_comfirm)
.setPositiveButton(android.R.string.ok) { _, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import com.blacksquircle.ui.editorkit.utils.EditorTheme
import com.blacksquircle.ui.language.json.JsonLanguage
import com.google.android.material.appbar.CollapsingToolbarLayout
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import com.neko.v2ray.R
import com.neko.v2ray.databinding.ActivityServerCustomConfigBinding
import com.neko.v2ray.dto.EConfigType
import com.neko.v2ray.dto.ServerConfig
import com.neko.v2ray.dto.V2rayConfig
import com.neko.v2ray.extension.toast
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.SoftInputAssist
import com.neko.v2ray.util.MmkvManager.serverRawStorage
import com.neko.v2ray.util.Utils
import me.drakeet.support.toast.ToastCompat

Expand All @@ -38,13 +37,11 @@ class ServerCustomConfigActivity : BaseActivity() {
private val binding by lazy { ActivityServerCustomConfigBinding.inflate(layoutInflater) }
private lateinit var softInputAssist: SoftInputAssist

private val mainStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_MAIN, MMKV.MULTI_PROCESS_MODE) }
private val serverRawStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_SERVER_RAW, MMKV.MULTI_PROCESS_MODE) }
private val editGuid by lazy { intent.getStringExtra("guid").orEmpty() }
private val isRunning by lazy {
intent.getBooleanExtra("isRunning", false)
&& editGuid.isNotEmpty()
&& editGuid == mainStorage?.decodeString(MmkvManager.KEY_SELECTED_SERVER)
&& editGuid == MmkvManager.getSelectServer()
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/kotlin/com/neko/v2ray/ui/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequest
import androidx.work.multiprocess.RemoteWorkManager
import com.google.android.material.appbar.CollapsingToolbarLayout
import com.tencent.mmkv.MMKV
import com.neko.v2ray.AngApplication
import com.neko.v2ray.AppConfig
import com.neko.v2ray.R
import com.neko.v2ray.extension.toLongEx
import com.neko.v2ray.service.SubscriptionUpdater
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.MmkvManager.settingsStorage
import com.neko.v2ray.util.Utils
import com.neko.v2ray.viewmodel.SettingsViewModel
import java.util.concurrent.TimeUnit
Expand All @@ -48,7 +47,6 @@ class SettingsActivity : BaseActivity() {
}

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

private val perAppProxy by lazy { findPreference<SwitchPreference>(AppConfig.PREF_PER_APP_PROXY) }
private val localDns by lazy { findPreference<SwitchPreference>(AppConfig.PREF_LOCAL_DNS_ENABLED) }
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/kotlin/com/neko/v2ray/ui/SubEditActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import com.google.android.material.appbar.MaterialToolbar
import androidx.lifecycle.lifecycleScope
import com.google.android.material.appbar.CollapsingToolbarLayout
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import com.neko.v2ray.R
import com.neko.v2ray.databinding.ActivitySubEditBinding
import com.neko.v2ray.dto.SubscriptionItem
import com.neko.v2ray.extension.toast
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.MmkvManager.subStorage
import com.neko.v2ray.util.SoftInputAssist
import com.neko.v2ray.util.Utils
import kotlinx.coroutines.Dispatchers
Expand All @@ -37,7 +37,6 @@ class SubEditActivity : BaseActivity() {
var del_config: MenuItem? = null
var save_config: MenuItem? = null

private val subStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_SUB, MMKV.MULTI_PROCESS_MODE) }
private val editSubId by lazy { intent.getStringExtra("subId").orEmpty() }

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@ import android.view.ViewGroup
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import com.neko.v2ray.R
import com.neko.v2ray.databinding.ItemQrcodeBinding
import com.neko.v2ray.databinding.ItemRecyclerSubSettingBinding
import com.neko.v2ray.dto.EConfigType
import com.neko.v2ray.extension.toast
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.MmkvManager.subStorage
import com.neko.v2ray.util.QRCodeDecoder
import com.neko.v2ray.util.Utils

class SubSettingRecyclerAdapter(val activity: SubSettingActivity) :
RecyclerView.Adapter<SubSettingRecyclerAdapter.MainViewHolder>() {

private var mActivity: SubSettingActivity = activity
private val subStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_SUB, MMKV.MULTI_PROCESS_MODE) }

private val share_method: Array<out String> by lazy {
mActivity.resources.getStringArray(R.array.share_sub_method)
Expand Down
Loading

0 comments on commit 5acae4b

Please sign in to comment.