blob: fe415fd5c52d9f4b4d85d90890a866c3b2b5d40c (
plain)
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
|
/*
* Copyright © 2013 The Android Open Source Project
* Copyright © 2017-2023 WireGuard LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.wireguard.android.widget
import android.content.Context
import android.os.Parcelable
import android.util.AttributeSet
import android.widget.Switch
class ToggleSwitch @JvmOverloads constructor(context: Context?, attrs: AttributeSet? = null) : Switch(context, attrs) {
private var isRestoringState = false
private var listener: OnBeforeCheckedChangeListener? = null
override fun onRestoreInstanceState(state: Parcelable) {
isRestoringState = true
super.onRestoreInstanceState(state)
isRestoringState = false
}
override fun setChecked(checked: Boolean) {
if (checked == isChecked) return
if (isRestoringState || listener == null) {
super.setChecked(checked)
return
}
isEnabled = false
listener!!.onBeforeCheckedChanged(this, checked)
}
fun setCheckedInternal(checked: Boolean) {
super.setChecked(checked)
isEnabled = true
}
fun setOnBeforeCheckedChangeListener(listener: OnBeforeCheckedChangeListener?) {
this.listener = listener
}
interface OnBeforeCheckedChangeListener {
fun onBeforeCheckedChanged(toggleSwitch: ToggleSwitch?, checked: Boolean)
}
}
|