Using AI to improve function documentation

This commit is contained in:
2dust
2025-03-19 12:15:43 +08:00
parent 1a7ab97a3a
commit d0e8937f03
3 changed files with 99 additions and 20 deletions
@@ -10,10 +10,13 @@ import com.v2ray.ang.handler.SettingsManager
class AngApplication : MultiDexApplication() {
companion object {
//const val PREF_LAST_VERSION = "pref_last_version"
lateinit var application: AngApplication
}
/**
* Attaches the base context to the application.
* @param base The base context.
*/
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
application = this
@@ -23,16 +26,12 @@ class AngApplication : MultiDexApplication() {
.setDefaultProcessName("${ANG_PACKAGE}:bg")
.build()
/**
* Initializes the application.
*/
override fun onCreate() {
super.onCreate()
// LeakCanary.install(this)
// val defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
// firstRun = defaultSharedPreferences.getInt(PREF_LAST_VERSION, 0) != BuildConfig.VERSION_CODE
// if (firstRun)
// defaultSharedPreferences.edit().putInt(PREF_LAST_VERSION, BuildConfig.VERSION_CODE).apply()
MMKV.initialize(this)
SettingsManager.setNightMode()
@@ -41,5 +40,4 @@ class AngApplication : MultiDexApplication() {
SettingsManager.initRoutingRulesets(this)
}
}
@@ -49,7 +49,6 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
* Refer to the official documentation for [registerReceiver](https://developer.android.com/reference/androidx/core/content/ContextCompat#registerReceiver(android.content.Context,android.content.BroadcastReceiver,android.content.IntentFilter,int):
* `registerReceiver(Context, BroadcastReceiver, IntentFilter, int)`.
*/
fun startListenBroadcast() {
isRunning.value = false
val mFilter = IntentFilter(AppConfig.BROADCAST_ACTION_ACTIVITY)
@@ -57,6 +56,9 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
MessageUtil.sendMsg2Service(getApplication(), AppConfig.MSG_REGISTER_CLIENT, "")
}
/**
* Called when the ViewModel is cleared.
*/
override fun onCleared() {
getApplication<AngApplication>().unregisterReceiver(mMsgReceiver)
tcpingTestScope.coroutineContext[Job]?.cancelChildren()
@@ -65,12 +67,19 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
super.onCleared()
}
/**
* Reloads the server list.
*/
fun reloadServerList() {
serverList = MmkvManager.decodeServerList()
updateCache()
updateListAction.value = -1
}
/**
* Removes a server by its GUID.
* @param guid The GUID of the server to remove.
*/
fun removeServer(guid: String) {
serverList.remove(guid)
MmkvManager.removeServer(guid)
@@ -80,6 +89,11 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
}
}
/**
* Appends a custom configuration server.
* @param server The server configuration to append.
* @return True if the server was successfully appended, false otherwise.
*/
fun appendCustomConfigServer(server: String): Boolean {
if (server.contains("inbounds")
&& server.contains("outbounds")
@@ -107,6 +121,11 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
return false
}
/**
* Swaps the positions of two servers.
* @param fromPosition The initial position of the server.
* @param toPosition The target position of the server.
*/
fun swapServer(fromPosition: Int, toPosition: Int) {
if (subscriptionId.isEmpty()) {
Collections.swap(serverList, fromPosition, toPosition)
@@ -119,6 +138,9 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
MmkvManager.encodeServerList(serverList)
}
/**
* Updates the cache of servers.
*/
@Synchronized
fun updateCache() {
serversCache.clear()
@@ -147,6 +169,10 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
}
}
/**
* Updates the configuration via subscription for all servers.
* @return The number of updated configurations.
*/
fun updateConfigViaSubAll(): Int {
if (subscriptionId.isEmpty()) {
return AngConfigManager.updateConfigViaSubAll()
@@ -156,6 +182,10 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
}
}
/**
* Exports all servers.
* @return The number of exported servers.
*/
fun exportAllServer(): Int {
val serverListCopy =
if (subscriptionId.isEmpty() && keywordFilter.isEmpty()) {
@@ -171,14 +201,15 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
return ret
}
/**
* Tests the TCP ping for all servers.
*/
fun testAllTcping() {
tcpingTestScope.coroutineContext[Job]?.cancelChildren()
SpeedtestManager.closeAllTcpSockets()
MmkvManager.clearAllTestDelayResults(serversCache.map { it.guid }.toList())
//updateListAction.value = -1 // update all
val serversCopy = serversCache.toList() // Create a copy of the list
val serversCopy = serversCache.toList()
for (item in serversCopy) {
item.profile.let { outbound ->
val serverAddress = outbound.server
@@ -196,23 +227,33 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
}
}
/**
* Tests the real ping for all servers.
*/
fun testAllRealPing() {
MessageUtil.sendMsg2TestService(getApplication(), AppConfig.MSG_MEASURE_CONFIG_CANCEL, "")
MmkvManager.clearAllTestDelayResults(serversCache.map { it.guid }.toList())
updateListAction.value = -1 // update all
updateListAction.value = -1
val serversCopy = serversCache.toList() // Create a copy of the list
viewModelScope.launch(Dispatchers.Default) { // without Dispatchers.Default viewModelScope will launch in main thread
val serversCopy = serversCache.toList()
viewModelScope.launch(Dispatchers.Default) {
for (item in serversCopy) {
MessageUtil.sendMsg2TestService(getApplication(), AppConfig.MSG_MEASURE_CONFIG, item.guid)
}
}
}
/**
* Tests the real ping for the current server.
*/
fun testCurrentServerRealPing() {
MessageUtil.sendMsg2Service(getApplication(), AppConfig.MSG_MEASURE_DELAY, "")
}
/**
* Changes the subscription ID.
* @param id The new subscription ID.
*/
fun subscriptionIdChanged(id: String) {
if (subscriptionId != id) {
subscriptionId = id
@@ -221,6 +262,11 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
}
}
/**
* Gets the subscriptions.
* @param context The context.
* @return A pair of lists containing the subscription IDs and remarks.
*/
fun getSubscriptions(context: Context): Pair<MutableList<String>?, MutableList<String>?> {
val subscriptions = MmkvManager.decodeSubscriptions()
if (subscriptionId.isNotEmpty()
@@ -239,6 +285,11 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
return listId to listRemarks
}
/**
* Gets the position of a server by its GUID.
* @param guid The GUID of the server.
* @return The position of the server.
*/
fun getPosition(guid: String): Int {
serversCache.forEachIndexed { index, it ->
if (it.guid == guid)
@@ -247,6 +298,10 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
return -1
}
/**
* Removes duplicate servers.
* @return The number of removed servers.
*/
fun removeDuplicateServer(): Int {
val serversCacheCopy = mutableListOf<Pair<String, ProfileItem>>()
for (it in serversCache) {
@@ -273,6 +328,10 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
return deleteServer.count()
}
/**
* Removes all servers.
* @return The number of removed servers.
*/
fun removeAllServer(): Int {
val count =
if (subscriptionId.isEmpty() && keywordFilter.isEmpty()) {
@@ -287,6 +346,10 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
return count
}
/**
* Removes invalid servers.
* @return The number of removed servers.
*/
fun removeInvalidServer(): Int {
var count = 0
if (subscriptionId.isEmpty() && keywordFilter.isEmpty()) {
@@ -300,6 +363,9 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
return count
}
/**
* Sorts servers by their test results.
*/
fun sortByTestResults() {
data class ServerDelay(var guid: String, var testDelayMillis: Long)
@@ -319,12 +385,20 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
MmkvManager.encodeServerList(serverList)
}
/**
* Initializes assets.
* @param assets The asset manager.
*/
fun initAssets(assets: AssetManager) {
viewModelScope.launch(Dispatchers.Default) {
SettingsManager.initAssets(getApplication<AngApplication>(), assets)
}
}
/**
* Filters the configuration by a keyword.
* @param keyword The keyword to filter by.
*/
fun filterConfig(keyword: String) {
if (keyword == keywordFilter) {
return
@@ -12,11 +12,17 @@ import com.v2ray.ang.handler.SettingsManager
class SettingsViewModel(application: Application) : AndroidViewModel(application),
SharedPreferences.OnSharedPreferenceChangeListener {
/**
* Starts listening for preference changes.
*/
fun startListenPreferenceChange() {
PreferenceManager.getDefaultSharedPreferences(getApplication())
.registerOnSharedPreferenceChangeListener(this)
}
/**
* Called when the ViewModel is cleared.
*/
override fun onCleared() {
PreferenceManager.getDefaultSharedPreferences(getApplication())
.unregisterOnSharedPreferenceChangeListener(this)
@@ -24,6 +30,11 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
super.onCleared()
}
/**
* Called when a shared preference is changed.
* @param sharedPreferences The shared preferences.
* @param key The key of the changed preference.
*/
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String?) {
Log.d(AppConfig.ANG_PACKAGE, "Observe settings changed: $key")
when (key) {
@@ -77,10 +88,6 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
AppConfig.PREF_MUX_XUDP_CONCURRENCY -> {
MmkvManager.encodeSettings(key, sharedPreferences.getString(key, "8"))
}
// AppConfig.PREF_PER_APP_PROXY_SET -> {
// MmkvManager.encodeSettings(key, sharedPreferences.getStringSet(key, setOf()))
// }
}
if (key == AppConfig.PREF_UI_MODE_NIGHT) {
SettingsManager.setNightMode()