Refine code style

This commit is contained in:
Mygod
2019-03-20 12:21:22 +08:00
parent ef32866c66
commit 673ff9800a
3 changed files with 32 additions and 21 deletions

View File

@@ -363,7 +363,7 @@ style:
OptionalUnit: OptionalUnit:
active: true active: true
OptionalWhenBraces: OptionalWhenBraces:
active: true active: false
PreferToOverPairSyntax: PreferToOverPairSyntax:
active: false active: false
ProtectedMemberInFinalClass: ProtectedMemberInFinalClass:

View File

@@ -22,10 +22,7 @@ import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.netId
import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.requestPersistentGroupInfo import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.requestPersistentGroupInfo
import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.setWifiP2pChannels import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.setWifiP2pChannels
import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.startWps import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.startWps
import be.mygod.vpnhotspot.util.StickyEvent0 import be.mygod.vpnhotspot.util.*
import be.mygod.vpnhotspot.util.StickyEvent1
import be.mygod.vpnhotspot.util.broadcastReceiver
import be.mygod.vpnhotspot.util.intentFilter
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import timber.log.Timber import timber.log.Timber
import java.lang.reflect.InvocationTargetException import java.lang.reflect.InvocationTargetException
@@ -233,29 +230,41 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, SharedPrefere
registerReceiver(receiver, intentFilter(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION, registerReceiver(receiver, intentFilter(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION,
WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)) WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION))
receiverRegistered = true receiverRegistered = true
p2pManager.requestGroupInfo(channel) { try {
when { p2pManager.requestGroupInfo(channel) {
it == null -> doStart() when {
it.isGroupOwner -> if (routingManager == null) doStart(it) it == null -> doStart()
else -> { it.isGroupOwner -> if (routingManager == null) doStart(it)
Timber.i("Removing old group ($it)") else -> {
p2pManager.removeGroup(channel, object : WifiP2pManager.ActionListener { Timber.i("Removing old group ($it)")
override fun onSuccess() = doStart() p2pManager.removeGroup(channel, object : WifiP2pManager.ActionListener {
override fun onFailure(reason: Int) = override fun onSuccess() = doStart()
startFailure(formatReason(R.string.repeater_remove_old_group_failure, reason)) override fun onFailure(reason: Int) =
}) startFailure(formatReason(R.string.repeater_remove_old_group_failure, reason))
})
}
} }
} }
} catch (e: SecurityException) {
Timber.w(e)
startFailure(e.readableMessage)
} }
return START_NOT_STICKY return START_NOT_STICKY
} }
/** /**
* startService Step 2 (if a group isn't already available) * startService Step 2 (if a group isn't already available)
*/ */
private fun doStart() = p2pManager.createGroup(channel, object : WifiP2pManager.ActionListener { private fun doStart() = try {
override fun onFailure(reason: Int) = startFailure(formatReason(R.string.repeater_create_group_failure, reason)) p2pManager.createGroup(channel, object : WifiP2pManager.ActionListener {
override fun onSuccess() { } // wait for WIFI_P2P_CONNECTION_CHANGED_ACTION to fire to go to step 3 override fun onFailure(reason: Int) {
}) startFailure(formatReason(R.string.repeater_create_group_failure, reason))
}
override fun onSuccess() { } // wait for WIFI_P2P_CONNECTION_CHANGED_ACTION to fire to go to step 3
})
} catch (e: SecurityException) {
Timber.w(e)
startFailure(e.readableMessage)
}
/** /**
* Used during step 2, also called when connection changed * Used during step 2, also called when connection changed
*/ */

View File

@@ -1,5 +1,6 @@
package be.mygod.vpnhotspot.manage package be.mygod.vpnhotspot.manage
import android.annotation.SuppressLint
import android.annotation.TargetApi import android.annotation.TargetApi
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
@@ -21,7 +22,8 @@ abstract class Manager {
const val VIEW_TYPE_REPEATER = 7 const val VIEW_TYPE_REPEATER = 7
override fun areItemsTheSame(oldItem: Manager, newItem: Manager) = oldItem.isSameItemAs(newItem) override fun areItemsTheSame(oldItem: Manager, newItem: Manager) = oldItem.isSameItemAs(newItem)
override fun areContentsTheSame(oldItem: Manager, newItem: Manager) = oldItem == newItem @SuppressLint("DiffUtilEquals")
override fun areContentsTheSame(oldItem: Manager, newItem: Manager) = oldItem === newItem
fun createViewHolder(inflater: LayoutInflater, parent: ViewGroup, type: Int) = when (type) { fun createViewHolder(inflater: LayoutInflater, parent: ViewGroup, type: Int) = when (type) {
VIEW_TYPE_INTERFACE -> VIEW_TYPE_INTERFACE ->