From 26acea8efeed3e54fb92a16eb09850d7e7a17ab7 Mon Sep 17 00:00:00 2001 From: Mygod Date: Fri, 30 Mar 2018 15:40:17 -0700 Subject: [PATCH] Fix code style issues --- detekt.yml | 42 ++++++++++++------- .../java/com/github/shadowsocks/AppManager.kt | 30 +++++++------ .../com/github/shadowsocks/bg/BaseService.kt | 15 +++++-- .../shadowsocks/plugin/PluginManager.kt | 24 +++++------ .../shadowsocks/preference/DataStore.kt | 1 - .../com/github/shadowsocks/tasker/Settings.kt | 4 +- 6 files changed, 65 insertions(+), 51 deletions(-) diff --git a/detekt.yml b/detekt.yml index ef1033e1..59c4e133 100644 --- a/detekt.yml +++ b/detekt.yml @@ -1,4 +1,4 @@ -# https://github.com/arturbosch/detekt/blob/801994bd60cc759b181649356cea045b8125301b/detekt-cli/src/main/resources/default-detekt-config.yml +# https://github.com/arturbosch/detekt/blob/RC6-4/detekt-cli/src/main/resources/default-detekt-config.yml comments: active: false @@ -7,7 +7,7 @@ complexity: active: true ComplexCondition: active: true - threshold: 3 + threshold: 4 ComplexInterface: active: true threshold: 10 @@ -15,6 +15,7 @@ complexity: ComplexMethod: active: true threshold: 10 + ignoreSingleWhenExpression: false LabeledExpression: active: true LargeClass: @@ -25,31 +26,32 @@ complexity: threshold: 20 LongParameterList: active: true - threshold: 5 + threshold: 6 ignoreDefaultParameters: true MethodOverloading: active: false NestedBlockDepth: active: true - threshold: 3 + threshold: 4 StringLiteralDuplication: active: true - threshold: 2 + threshold: 3 ignoreAnnotation: true excludeStringsWithLessThan5Characters: true ignoreStringsRegex: '$^' TooManyFunctions: active: true - thresholdInFiles: 10 - thresholdInClasses: 10 - thresholdInInterfaces: 10 - thresholdInObjects: 10 - thresholdInEnums: 10 + thresholdInFiles: 11 + thresholdInClasses: 11 + thresholdInInterfaces: 11 + thresholdInObjects: 11 + thresholdInEnums: 11 empty-blocks: active: true EmptyCatchBlock: - active: false + active: true + allowedExceptionNameRegex: "^(_|ignore|expected).*" EmptyClassBlock: active: true EmptyDefaultConstructor: @@ -64,6 +66,7 @@ empty-blocks: active: true EmptyFunctionBlock: active: true + ignoreOverriddenFunctions: false EmptyIfBlock: active: true EmptyInitBlock: @@ -105,7 +108,7 @@ exceptions: active: true TooGenericExceptionCaught: active: true - exceptions: + exceptionNames: - ArrayIndexOutOfBoundsException - Error - Exception @@ -116,10 +119,9 @@ exceptions: - Throwable TooGenericExceptionThrown: active: true - exceptions: + exceptionNames: - Error - Exception - - NullPointerException - Throwable - RuntimeException @@ -143,6 +145,7 @@ naming: FunctionNaming: active: true functionPattern: '^([a-z$][a-zA-Z$0-9]*)|(`.*`)$' + excludeClassPattern: '$^' MatchingDeclarationName: active: true MemberNameEqualsClassName: @@ -167,6 +170,7 @@ naming: active: true variablePattern: '[a-z][A-Za-z0-9]*' privateVariablePattern: '(_)?[a-z][A-Za-z0-9]*' + excludeClassPattern: '$^' performance: active: true @@ -239,15 +243,17 @@ style: maxLineLength: 120 excludePackageStatements: false excludeImportStatements: false + MayBeConst: + active: true ModifierOrder: active: true NestedClassesVisibility: active: true NewLineAtEndOfFile: active: true - OptionalAbstractKeyword: + NoTabs: active: true - OptionalReturnKeyword: + OptionalAbstractKeyword: active: true OptionalUnit: active: true @@ -268,6 +274,8 @@ style: ThrowsCount: active: true max: 2 + TrailingWhitespace: + active: true UnnecessaryAbstractClass: active: true UnnecessaryInheritance: @@ -278,6 +286,8 @@ style: active: true UnusedImports: active: true + UnusedPrivateMember: + active: true UseDataClass: active: false UtilityClassWithPublicConstructor: diff --git a/mobile/src/main/java/com/github/shadowsocks/AppManager.kt b/mobile/src/main/java/com/github/shadowsocks/AppManager.kt index 3d8cca65..9af5609e 100644 --- a/mobile/src/main/java/com/github/shadowsocks/AppManager.kt +++ b/mobile/src/main/java/com/github/shadowsocks/AppManager.kt @@ -62,23 +62,21 @@ class AppManager : AppCompatActivity(), Toolbar.OnMenuItemClickListener { private var receiver: BroadcastReceiver? = null private var cachedApps: List? = null - private fun getApps(pm: PackageManager): List { - return synchronized(AppManager) { - if (receiver == null) receiver = app.listenForPackageChanges { - synchronized(AppManager) { - receiver = null - cachedApps = null - } - AppManager.instance?.reloadApps() + private fun getApps(pm: PackageManager) = synchronized(AppManager) { + if (receiver == null) receiver = app.listenForPackageChanges { + synchronized(AppManager) { + receiver = null + cachedApps = null } - // Labels and icons can change on configuration (locale, etc.) changes, therefore they are not cached. - val cachedApps = cachedApps ?: pm.getInstalledPackages(PackageManager.GET_PERMISSIONS) - .filter { it.packageName != app.packageName && - it.requestedPermissions?.contains(Manifest.permission.INTERNET) ?: false } - this.cachedApps = cachedApps - cachedApps - }.map { ProxiedApp(pm, it.applicationInfo, it.packageName) } - } + AppManager.instance?.reloadApps() + } + // Labels and icons can change on configuration (locale, etc.) changes, therefore they are not cached. + val cachedApps = cachedApps ?: pm.getInstalledPackages(PackageManager.GET_PERMISSIONS) + .filter { it.packageName != app.packageName && + it.requestedPermissions?.contains(Manifest.permission.INTERNET) ?: false } + this.cachedApps = cachedApps + cachedApps + }.map { ProxiedApp(pm, it.applicationInfo, it.packageName) } } private class ProxiedApp(private val pm: PackageManager, private val appInfo: ApplicationInfo, diff --git a/mobile/src/main/java/com/github/shadowsocks/bg/BaseService.kt b/mobile/src/main/java/com/github/shadowsocks/bg/BaseService.kt index c0889567..5422e87d 100644 --- a/mobile/src/main/java/com/github/shadowsocks/bg/BaseService.kt +++ b/mobile/src/main/java/com/github/shadowsocks/bg/BaseService.kt @@ -117,7 +117,10 @@ object BaseService { val item = callbacks.getBroadcastItem(i) if (bandwidthListeners.contains(item.asBinder())) item.trafficUpdated(profile!!.id, txRate, rxRate, txTotal, rxTotal) - } catch (_: Exception) { } // ignore + } catch (e: Exception) { + e.printStackTrace() + app.track(e) + } callbacks.finishBroadcast() } } @@ -159,7 +162,10 @@ object BaseService { try { val item = callbacks.getBroadcastItem(i) if (bandwidthListeners.contains(item.asBinder())) item.trafficPersisted(profile.id) - } catch (_: Exception) { } // ignore + } catch (e: Exception) { + e.printStackTrace() + app.track(e) + } } callbacks.finishBroadcast() } @@ -211,7 +217,10 @@ object BaseService { val n = callbacks.beginBroadcast() for (i in 0 until n) try { callbacks.getBroadcastItem(i).stateChanged(s, binder.profileName, msg) - } catch (_: Exception) { } // ignore + } catch (e: Exception) { + e.printStackTrace() + app.track(e) + } callbacks.finishBroadcast() } state = s diff --git a/mobile/src/main/java/com/github/shadowsocks/plugin/PluginManager.kt b/mobile/src/main/java/com/github/shadowsocks/plugin/PluginManager.kt index 615254d1..94023ba0 100644 --- a/mobile/src/main/java/com/github/shadowsocks/plugin/PluginManager.kt +++ b/mobile/src/main/java/com/github/shadowsocks/plugin/PluginManager.kt @@ -87,21 +87,19 @@ object PluginManager { private var receiver: BroadcastReceiver? = null private var cachedPlugins: Map? = null - fun fetchPlugins(): Map { - return synchronized(this) { - if (receiver == null) receiver = app.listenForPackageChanges { - synchronized(this) { - receiver = null - cachedPlugins = null - } + fun fetchPlugins(): Map = synchronized(this) { + if (receiver == null) receiver = app.listenForPackageChanges { + synchronized(this) { + receiver = null + cachedPlugins = null } - if (cachedPlugins == null) { - val pm = app.packageManager - cachedPlugins = (pm.queryIntentContentProviders(Intent(PluginContract.ACTION_NATIVE_PLUGIN), - PackageManager.GET_META_DATA).map { NativePlugin(it) } + NoPlugin).associate { it.id to it } - } - cachedPlugins!! } + if (cachedPlugins == null) { + val pm = app.packageManager + cachedPlugins = (pm.queryIntentContentProviders(Intent(PluginContract.ACTION_NATIVE_PLUGIN), + PackageManager.GET_META_DATA).map { NativePlugin(it) } + NoPlugin).associate { it.id to it } + } + cachedPlugins!! } private fun buildUri(id: String) = Uri.Builder() diff --git a/mobile/src/main/java/com/github/shadowsocks/preference/DataStore.kt b/mobile/src/main/java/com/github/shadowsocks/preference/DataStore.kt index 4573fc98..144f2eab 100644 --- a/mobile/src/main/java/com/github/shadowsocks/preference/DataStore.kt +++ b/mobile/src/main/java/com/github/shadowsocks/preference/DataStore.kt @@ -22,7 +22,6 @@ package com.github.shadowsocks.preference import android.os.Binder import com.github.shadowsocks.App.Companion.app -import com.github.shadowsocks.BootReceiver import com.github.shadowsocks.database.PrivateDatabase import com.github.shadowsocks.database.PublicDatabase import com.github.shadowsocks.utils.DirectBoot diff --git a/mobile/src/main/java/com/github/shadowsocks/tasker/Settings.kt b/mobile/src/main/java/com/github/shadowsocks/tasker/Settings.kt index ed4e6f9c..79af911d 100644 --- a/mobile/src/main/java/com/github/shadowsocks/tasker/Settings.kt +++ b/mobile/src/main/java/com/github/shadowsocks/tasker/Settings.kt @@ -29,8 +29,8 @@ import com.twofortyfouram.locale.api.Intent as ApiIntent class Settings(bundle: Bundle?) { companion object { - private val KEY_SWITCH_ON = "switch_on" - private val KEY_PROFILE_ID = "profile_id" + private const val KEY_SWITCH_ON = "switch_on" + private const val KEY_PROFILE_ID = "profile_id" fun fromIntent(intent: Intent) = Settings(intent.getBundleExtra(ApiIntent.EXTRA_BUNDLE)) }