Skip to content

Commit

Permalink
Load custom shutter buttons + only show notifications at end
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Oct 20, 2024
1 parent 487ab70 commit d68dd0d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ import com.kylecorry.luna.timer.CoroutineTimer
class IntervalometerService : AccessibilityService() {

private val prefs by lazy { SharedPreferences(this) }
private val knownShutterButtons = listOf(
private val knownShutterButtonIds = listOf(
"com.android.camera:id/shutter_button",
"com.android.camera2:id/shutter_button",
"com.google.android.GoogleCamera:id/shutter_button",
"com.riseupgames.proshot2:id/cameraButton",
"net.sourceforge.opencamera:id/take_photo",
"net.sourceforge.opencamera:id/take_photo"
)

private val shutterButtonIds: List<String>
get() = (prefs.getString("shutter_buttons") ?: "").split(",").map { it.trim() }
.filter { it.isNotBlank() } + knownShutterButtonIds

private var stopReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
timer.stop()
Expand All @@ -39,7 +43,11 @@ class IntervalometerService : AccessibilityService() {
private val timer = CoroutineTimer {
// Trigger a notification with the remaining time
secondsUntilNextPhoto--
Notify.cancel(this, 2)

// Only cancel and reshow when the time is less than 5 seconds
if (secondsUntilNextPhoto <= 5) {
Notify.cancel(this, 2)
}
Notify.send(
this,
2,
Expand All @@ -49,7 +57,13 @@ class IntervalometerService : AccessibilityService() {
secondsUntilNextPhoto.toString(),
null,
R.drawable.bubble,
group = "alerts"
group = "alerts",
intent = PendingIntent.getActivity(
this,
0,
Intent(this, MainActivity::class.java),
PendingIntent.FLAG_IMMUTABLE
)
)
)
if (secondsUntilNextPhoto <= 0) {
Expand All @@ -61,7 +75,7 @@ class IntervalometerService : AccessibilityService() {

override fun onAccessibilityEvent(event: AccessibilityEvent?) {
// Start the timer when a shutter button is clicked
if (!timer.isRunning() && knownShutterButtons.contains(
if (!timer.isRunning() && shutterButtonIds.contains(
event?.source?.viewIdResourceName ?: ""
)
) {
Expand Down Expand Up @@ -135,7 +149,7 @@ class IntervalometerService : AccessibilityService() {

private fun clickShutterButton() {
val nodeInfo = rootInActiveWindow
for (shutterButton in knownShutterButtons) {
for (shutterButton in shutterButtonIds) {
val nodes = nodeInfo?.findAccessibilityNodeInfosByViewId(shutterButton)
if (nodes != null && nodes.isNotEmpty()) {
nodes.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class MainFragment : BoundFragment<FragmentMainBinding>() {
}

binding.interval.setText(prefs.getLong("interval")?.toString())

binding.shutterButtonIds.doOnTextChanged { text, _, _, _ ->
prefs.putString("shutter_buttons", text.toString())
}

binding.shutterButtonIds.setText(prefs.getString("shutter_buttons"))
}

override fun generateBinding(
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,21 @@
android:id="@+id/grant_permission"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Start / Stop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/interval" />

<EditText
android:id="@+id/shutter_button_ids"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Shutter button IDs"
android:inputType="text|textNoSuggestions"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/grant_permission" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit d68dd0d

Please sign in to comment.