From 786a41b609437fcf6a6b599a1b8515e64dbb13c2 Mon Sep 17 00:00:00 2001 From: bannedbook Date: Mon, 29 Jun 2020 20:03:36 +0800 Subject: [PATCH] update --- build.gradle | 4 +- .../main/java/com/github/shadowsocks/Core.kt | 78 +++++---- gitupdate.bat | 2 +- mobile/src/main/AndroidManifest.xml | 1 - .../github/shadowsocks/ProfilesFragment.kt | 148 ++++++++++-------- .../subscription/SubscriptionFragment.kt | 13 +- 6 files changed, 127 insertions(+), 119 deletions(-) diff --git a/build.gradle b/build.gradle index 62ef2aef8c..b50d069b85 100644 --- a/build.gradle +++ b/build.gradle @@ -10,8 +10,8 @@ buildscript { compileSdkVersion = 29 buildToolsVersion = '29.0.3' desugarLibsVersion = '1.0.5' - versionCode = 441 - versionName = '5.0.6.9R' + versionCode = 442 + versionName = '5.0.6.10R' resConfigs = ['ar', 'es', 'fa', 'fr', 'ja', 'ko', 'ru', 'tr', 'zh-rCN', 'zh-rTW'] } diff --git a/core/src/main/java/com/github/shadowsocks/Core.kt b/core/src/main/java/com/github/shadowsocks/Core.kt index 585c2999de..73da8c567a 100644 --- a/core/src/main/java/com/github/shadowsocks/Core.kt +++ b/core/src/main/java/com/github/shadowsocks/Core.kt @@ -19,7 +19,7 @@ *******************************************************************************/ package com.github.shadowsocks -import SpeedUpVPN.VpnEncrypt + import android.app.* import android.app.admin.DevicePolicyManager import android.content.Context @@ -82,50 +82,46 @@ object Core { DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE_PER_USER } - val activeProfileIds get() = ProfileManager.getProfile(DataStore.profileId).let { - if (it == null) emptyList() else listOfNotNull(it.id, it.udpFallback) - } - val currentProfile: Pair? get() { - if (DataStore.directBootAware) DirectBoot.getDeviceProfile()?.apply { return this } - var theOne=ProfileManager.getProfile(DataStore.profileId) - if (theOne==null){ - theOne=ProfileManager.getRandomVPNServer() - if (theOne!=null)DataStore.profileId=theOne.id + val activeProfileIds + get() = ProfileManager.getProfile(DataStore.profileId).let { + if (it == null) emptyList() else listOfNotNull(it.id, it.udpFallback) + } + val currentProfile: Pair? + get() { + if (DataStore.directBootAware) DirectBoot.getDeviceProfile()?.apply { return this } + var theOne = ProfileManager.getProfile(DataStore.profileId) + if (theOne == null) { + theOne = ProfileManager.getRandomVPNServer() + if (theOne != null) DataStore.profileId = theOne.id + } + return ProfileManager.expand(theOne ?: return null) } - return ProfileManager.expand(theOne ?: return null) - } fun switchProfile(id: Long): Profile { val result = ProfileManager.getProfile(id) ?: ProfileManager.createProfile() DataStore.profileId = result.id return result } - fun updateBuiltinServers(activity:Activity? = null){ - Log.e("updateBuiltinServers ","...") + + fun updateBuiltinServers(activity: Activity? = null) { + Log.e("updateBuiltinServers ", "...") GlobalScope.launch { - val userCountry= Locale.getDefault().country - Log.e("userCountry",userCountry) - if ("CN" != userCountry){ - var builtinSubUrls = app.resources.getStringArray(R.array.builtinSubUrls) - var builtinSub:SSRSub?=null + val userCountry = Locale.getDefault().country + Log.e("userCountry", userCountry) + if ("CN" != userCountry) { + var builtinSubUrls = app.resources.getStringArray(R.array.builtinSubUrls) + var builtinSub: SSRSub? = null for (i in builtinSubUrls.indices) { - builtinSub= SSRSubManager.createBuiltinSub(builtinSubUrls[i],"aes") + builtinSub = SSRSubManager.createBuiltinSub(builtinSubUrls[i], "aes") if (builtinSub != null) { //val randomOne=ProfileManager.getRandomVPNServer() //if (randomOne!=null)DataStore.profileId=randomOne.id break } } - if (builtinSub == null) { - activity?.runOnUiThread(){alertMessage(app.getString(R.string.status_network_error),activity)} - }else {//如果不是APP启动时更新,则停止服务,提醒重新连接 - //stopService() - //activity?.runOnUiThread(){alertMessage(app.getString(R.string.update_servers_ok),activity)} - } } - - if(DataStore.is_get_free_servers)importFreeSubs() //update free_servers - app.startService(Intent(app, SubscriptionService::class.java)) //update Subscription + if (DataStore.is_get_free_servers) importFreeSubs() //update free_servers + try {app.startService(Intent(app, SubscriptionService::class.java))}catch (e:Exception){} } } @@ -133,17 +129,16 @@ object Core { * import free sub */ fun importFreeSubs(): Boolean { - try { - GlobalScope.launch { - var freesuburl = app.resources.getStringArray(R.array.freesuburl) + GlobalScope.launch { + try { + val freesuburl = app.resources.getStringArray(R.array.freesuburl) for (i in freesuburl.indices) { - var freeSub=SSRSubManager.createBuiltinSub(freesuburl[i]) + val freeSub = SSRSubManager.createBuiltinSub(freesuburl[i]) if (freeSub != null) break } + } catch (e: Exception) { + e.printStackTrace() } - } catch (e: Exception) { - e.printStackTrace() - return false } return true } @@ -216,26 +211,25 @@ object Core { fun startService() = ContextCompat.startForegroundService(app, Intent(app, ShadowsocksConnection.serviceClass)) fun reloadService() = app.sendBroadcast(Intent(Action.RELOAD).setPackage(app.packageName)) fun stopService() = app.sendBroadcast(Intent(Action.CLOSE).setPackage(app.packageName)) - fun startServiceForTest() = app.startService(Intent(app, ProxyService::class.java).putExtra("test","go")) + fun startServiceForTest() = app.startService(Intent(app, ProxyService::class.java).putExtra("test", "go")) fun showMessage(msg: String) { var toast = Toast.makeText(app, msg, Toast.LENGTH_LONG) toast.setGravity(Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL, 0, 150) toast.show() } - fun alertMessage(msg: String,activity:Context,title:String="SpeedUp VPN") { + fun alertMessage(msg: String, activity: Context, title: String = "SpeedUp VPN") { try { - if(activity==null || (activity as Activity).isFinishing)return + if (activity == null || (activity as Activity).isFinishing) return val builder: AlertDialog.Builder? = activity.let { AlertDialog.Builder(activity) } - builder?.setMessage(msg)?.setTitle(title)?.setPositiveButton("ok", DialogInterface.OnClickListener { - _, _ -> + builder?.setMessage(msg)?.setTitle(title)?.setPositiveButton("ok", DialogInterface.OnClickListener { _, _ -> }) val dialog: AlertDialog? = builder?.create() dialog?.show() + } catch (t: Throwable) { } - catch (t:Throwable){} } } diff --git a/gitupdate.bat b/gitupdate.bat index d42a9bb69b..ba3a950414 100644 --- a/gitupdate.bat +++ b/gitupdate.bat @@ -3,6 +3,6 @@ git pull origin master git add -A git commit -m "update" git push origin master -git tag -a v5.0.6.9 -m "release v5.0.6.9" +git tag -a v5.0.6.10 -m "release v5.0.6.10" git push origin --tags pause \ No newline at end of file diff --git a/mobile/src/main/AndroidManifest.xml b/mobile/src/main/AndroidManifest.xml index 9c15413ee1..44eb1a2e8c 100644 --- a/mobile/src/main/AndroidManifest.xml +++ b/mobile/src/main/AndroidManifest.xml @@ -8,7 +8,6 @@ ().apply { - if (item.elapsed > 0L ) this += String.format("%dms", item.elapsed) - if (item.elapsed == -1L ) this += "failed" - if (item.elapsed == -2L ) this += VpnEncrypt.testing - if (tx > 0 || rx > 0) this += getString(R.string.traffic, + traffic.text = ArrayList().apply { + if (item.elapsed > 0L) this += String.format("%dms", item.elapsed) + if (item.elapsed == -1L) this += "failed" + if (item.elapsed == -2L) this += VpnEncrypt.testing + if (tx > 0 || rx > 0) this += getString(R.string.traffic, Formatter.formatFileSize(context, tx), Formatter.formatFileSize(context, rx)) }.joinToString(" \t") @@ -370,7 +372,8 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { } inner class ProfilesAdapter : RecyclerView.Adapter(), ProfileManager.Listener { - internal val profiles = ProfileManager.getProfilesOrderlySpeed()?.toMutableList() ?: mutableListOf() + internal val profiles = ProfileManager.getProfilesOrderlySpeed()?.toMutableList() + ?: mutableListOf() private val updated = HashSet() init { @@ -382,12 +385,12 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { override fun onBindViewHolder(holder: ProfileViewHolder, position: Int) { try { holder.bind(profiles[position]) - } - catch (e:Exception){ - Log.e("speedup.vpn","",e) + } catch (e: Exception) { + Log.e("speedup.vpn", "", e) } } + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProfileViewHolder = ProfileViewHolder( LayoutInflater.from(parent.context).inflate(R.layout.layout_profile, parent, false)) @@ -395,9 +398,8 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { override fun getItemId(position: Int): Long { try { return profiles[position].id - } - catch (e:Exception){ - Log.e("speedup.vpn","",e) + } catch (e: Exception) { + Log.e("speedup.vpn", "", e) return 0 } } @@ -407,7 +409,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { val pos = itemCount profiles += profile (activity as MainActivity).runOnUiThread({ - notifyItemInserted(pos) + notifyItemInserted(pos) }) } @@ -468,7 +470,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { if (index < 0) return profiles.removeAt(index) (activity as MainActivity).runOnUiThread({ - notifyItemRemoved(index) + notifyItemRemoved(index) }) if (profileId == DataStore.profileId) DataStore.profileId = 0 // switch to null profile } @@ -517,12 +519,12 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { recommendedNewsView.setBackgroundColor(Color.BLACK); recommendedNewsView.webViewClient = object : WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean { - if(url.isNullOrEmpty() || url.isBlank()) return false + if (url.isNullOrEmpty() || url.isBlank()) return false (activity as MainActivity).launchUrl(url) return true } } - recommendedNewsView.loadDataWithBaseURL(null,recnews,"text/html; charset=utf-8", "UTF-8",null) + recommendedNewsView.loadDataWithBaseURL(null, recnews, "text/html; charset=utf-8", "UTF-8", null) profilesList = view.findViewById(R.id.list) profilesList.setOnApplyWindowInsetsListener(MainListListener) @@ -635,9 +637,9 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { profilesAdapter.notifyDataSetChanged() for (k in 0 until profilesAdapter.profiles.size) { - try { - Log.e("tcping", "$k") - GlobalScope.launch { + GlobalScope.launch { + try { + Log.e("tcping", "$k") profilesAdapter.profiles[k].elapsed = tcping(profilesAdapter.profiles[k].host, profilesAdapter.profiles[k].remotePort) ProfileManager.updateProfile(profilesAdapter.profiles[k]) Log.e("tcping", "$k - " + profilesAdapter.profiles[k].elapsed) @@ -645,9 +647,9 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { Log.e("tcping", "$k - update") profilesAdapter.refreshId(profilesAdapter.profiles[k].id) } + } catch (e: IndexOutOfBoundsException) { } } - catch(e:IndexOutOfBoundsException){} } true } @@ -664,18 +666,17 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { R.id.remove_invalid_servers -> { try { - for (k in profilesAdapter.profiles.size-1 downTo 0 ) { - if (profilesAdapter.profiles[k].elapsed == -1L){ + for (k in profilesAdapter.profiles.size - 1 downTo 0) { + if (profilesAdapter.profiles[k].elapsed == -1L) { ProfileManager.delProfile(profilesAdapter.profiles[k].id) //profilesAdapter.remove(k) } } - val list=profilesAdapter.profiles.sortedWith(compareBy({ it.url_group }, { it.elapsed })) + val list = profilesAdapter.profiles.sortedWith(compareBy({ it.url_group }, { it.elapsed })) profilesAdapter.profiles.clear() profilesAdapter.profiles.addAll(list) profilesAdapter.notifyDataSetChanged() - } - catch (e:Exception){ + } catch (e: Exception) { e.printStackTrace() } true @@ -683,11 +684,10 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { R.id.sort_servers_by_speed -> { try { - profilesAdapter.profiles.sortBy {it.elapsed } + profilesAdapter.profiles.sortBy { it.elapsed } //configs.vmess.reverse() profilesAdapter.notifyDataSetChanged() - } - catch (e:Exception){ + } catch (e: Exception) { e.printStackTrace() } true @@ -696,38 +696,45 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { } } - private fun realTestProfiles(testInvalidOnly:Boolean) { + private fun realTestProfiles(testInvalidOnly: Boolean) { GlobalScope.launch { val activity = activity as MainActivity + activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LOCKED Core.stopService() - var isProxyStarted=false + var isProxyStarted = false for (k in 0 until profilesAdapter.profiles.size) { try { - if(testInvalidOnly && profilesAdapter.profiles[k].elapsed>0)continue - Log.e("real_ping_all",k.toString()) - profilesAdapter.profiles[k].elapsed=-2 + if (testInvalidOnly && profilesAdapter.profiles[k].elapsed > 0) continue + Log.e("real_ping_all", k.toString()) + profilesAdapter.profiles[k].elapsed = -2 val old = DataStore.profileId Core.switchProfile(profilesAdapter.profiles[k].id) activity?.runOnUiThread() { - try {activity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)}catch (e:Exception){} + try { + activity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + } catch (e: Exception) { + } layoutManager.scrollToPositionWithOffset(k, 0) //layoutManager.stackFromEnd = true profilesAdapter.refreshId(old) profilesAdapter.refreshId(profilesAdapter.profiles[k].id) - try {activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)}catch (e:Exception){} + try { + activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + } catch (e: Exception) { + } } var result = tcping(profilesAdapter.profiles[k].host, profilesAdapter.profiles[k].remotePort) - if( result > 0) { - if(!isProxyStarted)Core.startServiceForTest() + if (result > 0) { + if (!isProxyStarted) Core.startServiceForTest() else Core.reloadService() var ttt = 0 while (tcping("127.0.0.1", DataStore.portProxy) < 0 || tcping("127.0.0.1", VpnEncrypt.HTTP_PROXY_PORT) < 0) { Log.e("starting", "$k try $ttt ...") if (ttt == 5) { - activity?.runOnUiThread() {Core.alertMessage(activity.getString(R.string.toast_test_interrupted,profilesAdapter.profiles[k].name),activity)} - Log.e("realTestProfiles","Server: "+profilesAdapter.profiles[k].name+" or the one before it caused the test to be interrupted.") + activity?.runOnUiThread() { Core.alertMessage(activity.getString(R.string.toast_test_interrupted, profilesAdapter.profiles[k].name), activity) } + Log.e("realTestProfiles", "Server: " + profilesAdapter.profiles[k].name + " or the one before it caused the test to be interrupted.") Core.stopService() return@launch } @@ -735,11 +742,10 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { ttt++ } Thread.sleep(3_000) //必须等几秒,否则有问题... - if(!isProxyStarted)isProxyStarted=true //第一次启动成功 + if (!isProxyStarted) isProxyStarted = true //第一次启动成功 result = testConnection2() profilesAdapter.profiles[k].elapsed = result - } - else{ + } else { profilesAdapter.profiles[k].elapsed = -1 } @@ -748,25 +754,31 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { activity?.runOnUiThread() { profilesAdapter.refreshId(profilesAdapter.profiles[k].id) } - } - catch (e:Exception){ - Log.e("real_ping_all",e.toString()) + } catch (e: Exception) { + Log.e("real_ping_all", e.toString()) } } Core.stopService() activity?.runOnUiThread() { //profilesAdapter.profiles.sortBy {it.elapsed ; it.url_group} // 低版本工作,但模拟器安卓10不工作 - val list=profilesAdapter.profiles.sortedWith(compareBy({ it.url_group }, { it.elapsed })) + val list = profilesAdapter.profiles.sortedWith(compareBy({ it.url_group }, { it.elapsed })) profilesAdapter.profiles.clear() profilesAdapter.profiles.addAll(list) profilesAdapter.notifyDataSetChanged() - try{Core.alertMessage(activity.getString(R.string.toast_test_ended),activity)}catch (t:Throwable){} + try { + Core.alertMessage(activity.getString(R.string.toast_test_ended), activity) + } catch (t: Throwable) { + } + } + try { + activity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER; + } catch (e: Exception) { } - try {activity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)}catch (e:Exception){} } } - private fun testConnection2(timeout:Int = 10_000): Long { + private fun testConnection2(timeout: Int = 10_000): Long { var result: Long var conn: HttpURLConnection? = null @@ -795,11 +807,11 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { } } catch (e: IOException) { // network exception - Log.d("testConnection2","IOException: "+Log.getStackTraceString(e)) + Log.d("testConnection2", "IOException: " + Log.getStackTraceString(e)) result = -1 } catch (e: Exception) { // library exception, eg sumsung - Log.d("testConnection2","Exception: "+Log.getStackTraceString(e)) + Log.d("testConnection2", "Exception: " + Log.getStackTraceString(e)) result = -1 } finally { conn?.disconnect() @@ -817,31 +829,33 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { var time = -1L for (k in 0 until 2) { val one = socketConnectTime(url, port) - if (one != -1L ) - if(time == -1L || one < time) { + if (one != -1L) + if (time == -1L || one < time) { time = one } } return time } + private fun socketConnectTime(url: String, port: Int): Long { try { val start = System.currentTimeMillis() val socket = Socket() var socketAddress = InetSocketAddress(url, port) - socket.connect(socketAddress,5000) + socket.connect(socketAddress, 5000) val time = System.currentTimeMillis() - start socket.close() return time } catch (e: UnknownHostException) { - Log.e("testConnection2",e.toString()) + Log.e("testConnection2", e.toString()) } catch (e: IOException) { - Log.e("testConnection2",e.toString()) + Log.e("testConnection2", e.toString()) } catch (e: Exception) { - Log.e("testConnection2",e.toString()) + Log.e("testConnection2", e.toString()) } return -1 } + private fun startFilesForResult(intent: Intent, requestCode: Int) { try { startActivityForResult(intent.addCategory(Intent.CATEGORY_OPENABLE), requestCode) diff --git a/mobile/src/main/java/com/github/shadowsocks/subscription/SubscriptionFragment.kt b/mobile/src/main/java/com/github/shadowsocks/subscription/SubscriptionFragment.kt index 11633c295f..1ac3c228dc 100644 --- a/mobile/src/main/java/com/github/shadowsocks/subscription/SubscriptionFragment.kt +++ b/mobile/src/main/java/com/github/shadowsocks/subscription/SubscriptionFragment.kt @@ -137,7 +137,7 @@ class SubscriptionFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener private inner class SubViewHolder(view: View) : RecyclerView.ViewHolder(view), View.OnClickListener { - lateinit var item: SSRSub + var item: SSRSub? = null private val text = view.findViewById(android.R.id.text1) init { @@ -154,9 +154,10 @@ class SubscriptionFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener } override fun onClick(v: View?) { - if (item.id == 0L) return - if(item.isBuiltin()||item.isBuiltin2()) return - SubDialogFragment().withArg(SubItem(adapterPosition, item.url, item.url_group)) + if (item==null) return + if (item?.id == 0L) return + if(item!!.isBuiltin()||item!!.isBuiltin2()) return + SubDialogFragment().withArg(SubItem(adapterPosition, item!!.url, item!!.url_group)) .show(this@SubscriptionFragment, REQUEST_CODE_EDIT) } } @@ -225,7 +226,7 @@ class SubscriptionFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.START) { override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { val index = viewHolder.adapterPosition - if ((viewHolder as SubViewHolder).item.id == 0L) adapter.notifyItemChanged(index) + if ((viewHolder as SubViewHolder).item?.id == 0L) adapter.notifyItemChanged(index) else adapter.del(index) } @@ -269,7 +270,7 @@ class SubscriptionFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener } catch (e: Exception) { printLog(e) GlobalScope.launch(Dispatchers.Main) { - (activity as MainActivity)?.snackbar()?.setText(e.readableMessage)?.show() + if(activity!=null)(activity as MainActivity)?.snackbar()?.setText(e.readableMessage)?.show() } } finally { withContext(Dispatchers.Main) {