Skip to content

Commit

Permalink
Use more Kotlin-esque code where applicable
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
  • Loading branch information
msfjarvis committed Mar 19, 2020
1 parent fc0660c commit 0899b49
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ class AddTunnelsSheet : BottomSheetDialogFragment() {
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
val dialog = dialog as BottomSheetDialog? ?: return
behavior = dialog.behavior
behavior.state = BottomSheetBehavior.STATE_EXPANDED
behavior.peekHeight = 0
behavior.addBottomSheetCallback(bottomSheetCallback)
behavior.apply {
state = BottomSheetBehavior.STATE_EXPANDED
peekHeight = 0
addBottomSheetCallback(bottomSheetCallback)
}
dialog.findViewById<View>(R.id.create_empty)?.setOnClickListener {
dismiss()
onRequestCreateConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import com.wireguard.android.model.ApplicationData
import com.wireguard.android.util.ErrorMessages
import com.wireguard.android.util.ObservableKeyedArrayList
import com.wireguard.android.util.ObservableKeyedList
import java9.util.Comparators
import java9.util.function.Function
import java.util.Collections

class AppListDialogFragment : DialogFragment() {
private val appData: ObservableKeyedList<String, ApplicationData> = ObservableKeyedArrayList()
Expand All @@ -39,8 +36,7 @@ class AppListDialogFragment : DialogFragment() {
val packageName = it.activityInfo.packageName
applicationData.add(ApplicationData(it.loadIcon(pm), it.loadLabel(pm).toString(), packageName, currentlyExcludedApps.contains(packageName)))
}

Collections.sort(applicationData, Comparators.comparing(Function { obj: ApplicationData -> obj.name }, java.lang.String.CASE_INSENSITIVE_ORDER))
applicationData.sortWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.name })
applicationData
}.whenComplete { data, throwable ->
if (data != null) {
Expand Down Expand Up @@ -77,7 +73,7 @@ class AppListDialogFragment : DialogFragment() {
dialog.setOnShowListener {
dialog.getButton(DialogInterface.BUTTON_NEUTRAL).setOnClickListener {
val selectedItems = appData
.filter { obj: ApplicationData -> obj.isExcludedFromTunnel }
.filter { it.isExcludedFromTunnel }

val excludeAll = selectedItems.isEmpty()
appData.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ abstract class BaseFragment : Fragment(), OnSelectedTunnelChangedListener {
is TunnelDetailFragmentBinding -> binding.tunnel
is TunnelListItemBinding -> binding.item
else -> return
}
} ?: return
Application.getBackendAsync().thenAccept { backend: Backend? ->
if (backend is GoBackend) {
val intent = GoBackend.VpnService.prepare(view.context)
Expand All @@ -80,7 +80,7 @@ abstract class BaseFragment : Fragment(), OnSelectedTunnelChangedListener {
return@thenAccept
}
}
setTunnelStateWithPermissionsResult(tunnel!!, checked)
setTunnelStateWithPermissionsResult(tunnel, checked)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/
package com.wireguard.android.fragment

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
Expand All @@ -15,7 +14,6 @@ import androidx.fragment.app.DialogFragment
import com.wireguard.android.Application
import com.wireguard.android.R
import com.wireguard.android.databinding.ConfigNamingDialogFragmentBinding
import com.wireguard.android.model.ObservableTunnel
import com.wireguard.config.BadConfigException
import com.wireguard.config.Config
import java.io.ByteArrayInputStream
Expand All @@ -28,13 +26,13 @@ class ConfigNamingDialogFragment : DialogFragment() {
private var imm: InputMethodManager? = null

private fun createTunnelAndDismiss() {
if (binding != null) {
val name = binding!!.tunnelNameText.text.toString()
Application.getTunnelManager().create(name, config).whenComplete { tunnel: ObservableTunnel?, throwable: Throwable ->
binding?.let {
val name = it.tunnelNameText.text.toString()
Application.getTunnelManager().create(name, config).whenComplete { tunnel, throwable ->
if (tunnel != null) {
dismiss()
} else {
binding!!.tunnelNameTextLayout.error = throwable.message
it.tunnelNameTextLayout.error = throwable.message
}
}
}
Expand All @@ -51,15 +49,16 @@ class ConfigNamingDialogFragment : DialogFragment() {
val configBytes = configText!!.toByteArray(StandardCharsets.UTF_8)
config = try {
Config.parse(ByteArrayInputStream(configBytes))
} catch (e: BadConfigException) {
throw IllegalArgumentException("Invalid config passed to " + javaClass.simpleName, e)
} catch (e: IOException) {
throw IllegalArgumentException("Invalid config passed to " + javaClass.simpleName, e)
} catch(e: Exception) {
when(e) {
is BadConfigException, is IOException -> throw IllegalArgumentException("Invalid config passed to ${javaClass.simpleName}", e)
else -> throw e
}
}
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val activity: Activity = requireActivity()
val activity = requireActivity()
imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val alertDialogBuilder = AlertDialog.Builder(activity)
alertDialogBuilder.setTitle(R.string.import_from_qr_code)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import com.wireguard.android.databinding.TunnelDetailPeerBinding
import com.wireguard.android.model.ObservableTunnel
import com.wireguard.android.ui.EdgeToEdge.setUpRoot
import com.wireguard.android.ui.EdgeToEdge.setUpScrollingContent
import com.wireguard.config.Config
import java.util.Timer
import java.util.TimerTask

Expand All @@ -27,7 +26,7 @@ import java.util.TimerTask
*/
class TunnelDetailFragment : BaseFragment() {
private var binding: TunnelDetailFragmentBinding? = null
private var lastState: Tunnel.State? = Tunnel.State.TOGGLE
private var lastState = Tunnel.State.TOGGLE
private var timer: Timer? = null

private fun formatBytes(bytes: Long): String {
Expand Down Expand Up @@ -78,9 +77,9 @@ class TunnelDetailFragment : BaseFragment() {
}

override fun onSelectedTunnelChanged(oldTunnel: ObservableTunnel?, newTunnel: ObservableTunnel?) {
if (binding == null) return
binding ?: return
binding!!.tunnel = newTunnel
if (newTunnel == null) binding!!.config = null else newTunnel.configAsync.thenAccept { config: Config? -> binding!!.config = config }
if (newTunnel == null) binding!!.config = null else newTunnel.configAsync.thenAccept { config -> binding!!.config = config }
lastState = Tunnel.State.TOGGLE
updateStats()
}
Expand All @@ -94,9 +93,7 @@ class TunnelDetailFragment : BaseFragment() {
}

override fun onViewStateRestored(savedInstanceState: Bundle?) {
if (binding == null) {
return
}
binding ?: return
binding!!.fragment = this
onSelectedTunnelChanged(null, selectedTunnel)
super.onViewStateRestored(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ class TunnelEditorFragment : BaseFragment(), AppExclusionListener {
private var binding: TunnelEditorFragmentBinding? = null
private var tunnel: ObservableTunnel? = null
private fun onConfigLoaded(config: Config) {
if (binding != null) {
binding!!.config = ConfigProxy(config)
}
binding?.config = ConfigProxy(config)
}

private fun onConfigSaved(savedTunnel: Tunnel, throwable: Throwable?) {
Expand All @@ -51,8 +49,8 @@ class TunnelEditorFragment : BaseFragment(), AppExclusionListener {
val error = ErrorMessages.get(throwable)
message = getString(R.string.config_save_error, savedTunnel.name, error)
Log.e(TAG, message, throwable)
if (binding != null) {
Snackbar.make(binding!!.mainContainer, message, Snackbar.LENGTH_LONG).show()
binding?.let {
Snackbar.make(it.mainContainer, message, Snackbar.LENGTH_LONG).show()
}
}
}
Expand Down Expand Up @@ -112,9 +110,8 @@ class TunnelEditorFragment : BaseFragment(), AppExclusionListener {

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.menu_action_save) {
if (binding == null) return false
val newConfig: Config
newConfig = try {
binding ?: return false
val newConfig = try {
binding!!.config!!.resolve()
} catch (e: Exception) {
val error = ErrorMessages.get(e)
Expand All @@ -129,7 +126,7 @@ class TunnelEditorFragment : BaseFragment(), AppExclusionListener {
Log.d(TAG, "Attempting to create new tunnel " + binding!!.name)
val manager = Application.getTunnelManager()
manager.create(binding!!.name, newConfig)
.whenComplete { newTunnel, throwable -> onTunnelCreated(newTunnel, throwable) }
.whenComplete(this::onTunnelCreated)
}
tunnel!!.name != binding!!.name -> {
Log.d(TAG, "Attempting to rename tunnel to " + binding!!.name)
Expand Down Expand Up @@ -169,7 +166,7 @@ class TunnelEditorFragment : BaseFragment(), AppExclusionListener {
binding!!.config = ConfigProxy()
if (tunnel != null) {
binding!!.name = tunnel!!.name
tunnel!!.configAsync.thenAccept { config: Config -> onConfigLoaded(config) }
tunnel!!.configAsync.thenAccept(this::onConfigLoaded)
} else {
binding!!.name = ""
}
Expand All @@ -187,8 +184,8 @@ class TunnelEditorFragment : BaseFragment(), AppExclusionListener {
val error = ErrorMessages.get(throwable)
message = getString(R.string.tunnel_create_error, error)
Log.e(TAG, message, throwable)
if (binding != null) {
Snackbar.make(binding!!.mainContainer, message, Snackbar.LENGTH_LONG).show()
binding?.let {
Snackbar.make(it.mainContainer, message, Snackbar.LENGTH_LONG).show()
}
}
}
Expand All @@ -206,16 +203,14 @@ class TunnelEditorFragment : BaseFragment(), AppExclusionListener {
val error = ErrorMessages.get(throwable)
message = getString(R.string.tunnel_rename_error, error)
Log.e(TAG, message, throwable)
if (binding != null) {
Snackbar.make(binding!!.mainContainer, message, Snackbar.LENGTH_LONG).show()
binding?.let {
Snackbar.make(it.mainContainer, message, Snackbar.LENGTH_LONG).show()
}
}
}

override fun onViewStateRestored(savedInstanceState: Bundle?) {
if (binding == null) {
return
}
binding ?: return
binding!!.fragment = this
if (savedInstanceState == null) {
onSelectedTunnelChanged(null, selectedTunnel)
Expand Down
Loading

0 comments on commit 0899b49

Please sign in to comment.