-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVolumeControlsPlugin.kt
More file actions
107 lines (92 loc) · 4.35 KB
/
VolumeControlsPlugin.kt
File metadata and controls
107 lines (92 loc) · 4.35 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package co.stonephone.stonecamera.plugins
import android.annotation.SuppressLint
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.KeyEvent
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import co.stonephone.stonecamera.MyApplication
import co.stonephone.stonecamera.StoneCameraViewModel
class VolumeControlsPlugin : IPlugin {
override val id: String = "volumeControlsPlugin"
override val name: String = "Volume Controls"
var viewModel: StoneCameraViewModel? = null
var isZoomMode = false
private val handler = Handler(Looper.getMainLooper())
private var clearValueRunnable: Runnable? = null
@SuppressLint("ClickableViewAccessibility")
override fun initialize(viewModel: StoneCameraViewModel) {
this.viewModel = viewModel
val zoomBasePlugin = viewModel.plugins.find { it.id == "zoomBasePlugin" } as ZoomBasePlugin
val activity = MyApplication.getAppActivity()
activity?.let {
it.window.callback = object : android.view.Window.Callback by it.window.callback {
override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
val controlModeGet = viewModel.getSetting<String>("volumeControlMode")
if (isZoomMode || controlModeGet == "Zoom") {
clearValueRunnable?.let { handler.removeCallbacks(it) }
clearValueRunnable = Runnable {
isZoomMode = false
clearValueRunnable = null
}
handler.postDelayed(clearValueRunnable!!, 5000)
if (event?.keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.action == KeyEvent.ACTION_DOWN) {
Log.d("CaptureUtilsPlugin", "Volume up button pressed")
zoomBasePlugin.setZoom(zoomBasePlugin.currentZoom + 0.1f)
return true
} else if (event?.keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && event.action == KeyEvent.ACTION_DOWN) {
Log.d("CaptureUtilsPlugin", "Volume down button pressed")
zoomBasePlugin.setZoom(zoomBasePlugin.currentZoom - 0.1f)
return true
} else {
return false
}
} else {
if (event?.keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.action == KeyEvent.ACTION_DOWN) {
isZoomMode = true
zoomBasePlugin.setZoom(zoomBasePlugin.currentZoom + 0.1f)
clearValueRunnable = Runnable {
isZoomMode = false
clearValueRunnable = null
}
handler.postDelayed(clearValueRunnable!!, 5000)
return true
} else if (event?.keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && event.action == KeyEvent.ACTION_DOWN) {
Log.d("CaptureUtilsPlugin", "Volume down button pressed")
viewModel.capturePhoto()
return true
} else {
return false
}
}
}
}
}
}
override val settings: List<PluginSetting> = listOf(
PluginSetting.EnumSetting(
key = "volumeControlMode",
options = listOf("Zoom", "Capture", "Smart"),
defaultValue = "Smart",
renderLocation = SettingLocation.TOP,
render = { value ->
Text(
value.uppercase(), color = Color.White,
style = MaterialTheme.typography.bodyLarge,
fontWeight = FontWeight.Bold,
modifier = Modifier
.padding(8.dp)
)
},
onChange = { viewModel, value ->
// NOOP
}
)
)
}