1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
/*
* Copyright © 2017-2023 WireGuard LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.wireguard.android.activity
import android.os.Bundle
import androidx.databinding.CallbackRegistry
import androidx.databinding.CallbackRegistry.NotifierCallback
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.wireguard.android.Application
import com.wireguard.android.model.ObservableTunnel
import kotlinx.coroutines.launch
/**
* Base class for activities that need to remember the currently-selected tunnel.
*/
abstract class BaseActivity : ThemeChangeAwareActivity() {
private val selectionChangeRegistry = SelectionChangeRegistry()
var selectedTunnel: ObservableTunnel? = null
set(value) {
val oldTunnel = field
if (oldTunnel == value) return
field = value
onSelectedTunnelChanged(oldTunnel, value)
selectionChangeRegistry.notifyCallbacks(oldTunnel, 0, value)
}
fun addOnSelectedTunnelChangedListener(listener: OnSelectedTunnelChangedListener) {
selectionChangeRegistry.add(listener)
}
override fun onCreate(savedInstanceState: Bundle?) {
// Restore the saved tunnel if there is one; otherwise grab it from the arguments.
val savedTunnelName = when {
savedInstanceState != null -> savedInstanceState.getString(KEY_SELECTED_TUNNEL)
intent != null -> intent.getStringExtra(KEY_SELECTED_TUNNEL)
else -> null
}
if (savedTunnelName != null)
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
selectedTunnel = Application.getTunnelManager().getTunnels()[savedTunnelName]
}
}
// The selected tunnel must be set before the superclass method recreates fragments.
super.onCreate(savedInstanceState)
}
override fun onSaveInstanceState(outState: Bundle) {
if (selectedTunnel != null) outState.putString(KEY_SELECTED_TUNNEL, selectedTunnel!!.name)
super.onSaveInstanceState(outState)
}
protected abstract fun onSelectedTunnelChanged(oldTunnel: ObservableTunnel?, newTunnel: ObservableTunnel?)
fun removeOnSelectedTunnelChangedListener(
listener: OnSelectedTunnelChangedListener) {
selectionChangeRegistry.remove(listener)
}
interface OnSelectedTunnelChangedListener {
fun onSelectedTunnelChanged(oldTunnel: ObservableTunnel?, newTunnel: ObservableTunnel?)
}
private class SelectionChangeNotifier : NotifierCallback<OnSelectedTunnelChangedListener, ObservableTunnel, ObservableTunnel>() {
override fun onNotifyCallback(
listener: OnSelectedTunnelChangedListener,
oldTunnel: ObservableTunnel?,
ignored: Int,
newTunnel: ObservableTunnel?
) {
listener.onSelectedTunnelChanged(oldTunnel, newTunnel)
}
}
private class SelectionChangeRegistry :
CallbackRegistry<OnSelectedTunnelChangedListener, ObservableTunnel, ObservableTunnel>(SelectionChangeNotifier())
companion object {
private const val KEY_SELECTED_TUNNEL = "selected_tunnel"
}
}
|