Skip to content

Commit

Permalink
Aperture: Dynamically fill the gestures settings
Browse files Browse the repository at this point in the history
Change-Id: I04f054543c13afef7dee0569f38be05d21b81810
  • Loading branch information
SebaUbuntu authored and luk1337 committed Aug 27, 2024
1 parent 1b1631b commit 1e1064c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 67 deletions.
55 changes: 55 additions & 0 deletions app/src/main/java/org/lineageos/aperture/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.appbar.MaterialToolbar
import org.lineageos.aperture.ext.gestureActionToString
import org.lineageos.aperture.ext.setOffset
import org.lineageos.aperture.models.HardwareKey
import org.lineageos.aperture.utils.CameraSoundsUtils
Expand Down Expand Up @@ -224,6 +225,60 @@ class SettingsActivity : AppCompatActivity(R.layout.activity_settings) {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
super.onCreatePreferences(savedInstanceState, rootKey)

val context = requireContext()

for (hardwareKey in HardwareKey.entries) {
val actionPreference = ListPreference(context, null).apply {
key = "${hardwareKey.sharedPreferencesKeyPrefix}_action"
setTitle(hardwareKey.actionPreferenceTitleStringResId)
setDialogTitle(hardwareKey.actionPreferenceTitleStringResId)
when {
hardwareKey.supportsDefault && hardwareKey.isTwoWayKey -> {
setEntries(R.array.gesture_actions_entries)
setEntryValues(R.array.gesture_actions_values)
}
hardwareKey.supportsDefault && !hardwareKey.isTwoWayKey -> {
setEntries(R.array.gesture_actions_no_two_way_entries)
setEntryValues(R.array.gesture_actions_no_two_way_values)
}
!hardwareKey.supportsDefault && hardwareKey.isTwoWayKey -> {
setEntries(R.array.gesture_actions_no_two_way_entries)
setEntryValues(R.array.gesture_actions_no_two_way_values)
}
else -> {
setEntries(R.array.gesture_actions_no_default_no_two_way_entries)
setEntryValues(R.array.gesture_actions_no_default_no_two_way_values)
}
}
setDefaultValue(gestureActionToString(hardwareKey.defaultAction))
isIconSpaceReserved = false
summaryProvider = ListPreference.SimpleSummaryProvider.getInstance()
}

if (!hardwareKey.isTwoWayKey) {
singleButtonsPreferenceCategory?.addPreference(actionPreference)
} else {
val invertPreference = SwitchPreference(context, null).apply {
key = "${hardwareKey.sharedPreferencesKeyPrefix}_invert"
setTitle(hardwareKey.invertPreferenceTitleStringResId!!)
setSummary(hardwareKey.invertPreferenceSummaryStringResId!!)
setDefaultValue(false)
isIconSpaceReserved = false
}

val keyCategory = PreferenceCategory(context, null).apply {
key = hardwareKey.sharedPreferencesKeyPrefix
setTitle(hardwareKey.preferenceCategoryTitleStringResId!!)
isIconSpaceReserved = false
}

preferenceScreen.addPreference(keyCategory)

keyCategory.addPreference(actionPreference)
keyCategory.addPreference(invertPreference)
}
}

recheckKeys()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,11 @@ internal fun SharedPreferences.getHardwareKeyInvert(
) = hardwareKey.isTwoWayKey && getBoolean(
"${hardwareKey.sharedPreferencesKeyPrefix}_invert", false
)

internal fun gestureActionToString(gestureAction: GestureAction) = when (gestureAction) {
GestureAction.SHUTTER -> "shutter"
GestureAction.FOCUS -> "focus"
GestureAction.ZOOM -> "zoom"
GestureAction.DEFAULT -> "default"
GestureAction.NOTHING -> "nothing"
}
24 changes: 24 additions & 0 deletions app/src/main/java/org/lineageos/aperture/models/HardwareKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package org.lineageos.aperture.models

import android.content.SharedPreferences
import android.view.KeyEvent
import androidx.annotation.StringRes
import org.lineageos.aperture.R

/**
* Collection of keys that can be used to do things.
Expand All @@ -24,38 +26,60 @@ enum class HardwareKey(
val sharedPreferencesKeyPrefix: String,
val supportsDefault: Boolean,
val defaultAction: GestureAction,
@StringRes val actionPreferenceTitleStringResId: Int,
@StringRes val preferenceCategoryTitleStringResId: Int? = null,
@StringRes val invertPreferenceTitleStringResId: Int? = null,
@StringRes val invertPreferenceSummaryStringResId: Int? = null,
) {
CAMERA(
KeyEvent.KEYCODE_CAMERA,
null,
"camera_button",
false,
GestureAction.SHUTTER,
R.string.camera_button_action_title,
),
FOCUS(
KeyEvent.KEYCODE_FOCUS,
null,
"focus_button",
false,
GestureAction.FOCUS,
R.string.focus_button_action_title,
),
VOLUME(
KeyEvent.KEYCODE_VOLUME_UP,
KeyEvent.KEYCODE_VOLUME_DOWN,
"volume_buttons",
true,
GestureAction.SHUTTER,
R.string.volume_buttons_action_title,
R.string.volume_buttons_title,
R.string.volume_buttons_invert_title,
R.string.volume_buttons_invert_summary,
),
ZOOM(
KeyEvent.KEYCODE_ZOOM_IN,
KeyEvent.KEYCODE_ZOOM_OUT,
"zoom_buttons",
false,
GestureAction.ZOOM,
R.string.zoom_buttons_action_title,
R.string.zoom_buttons_title,
R.string.zoom_buttons_invert_title,
R.string.zoom_buttons_invert_summary,
);

val isTwoWayKey = secondKeycode != null

init {
require(
!isTwoWayKey || (preferenceCategoryTitleStringResId != null
&& invertPreferenceTitleStringResId != null
&& invertPreferenceSummaryStringResId != null)
)
}

companion object {
/**
* keycode to ([HardwareKey], first or increase)
Expand Down
68 changes: 1 addition & 67 deletions app/src/main/res/xml/gestures_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,6 @@
<PreferenceCategory
app:iconSpaceReserved="false"
app:key="single_buttons"
app:title="@string/single_buttons_title">

<ListPreference
app:defaultValue="shutter"
app:entries="@array/gesture_actions_no_default_no_two_way_entries"
app:entryValues="@array/gesture_actions_no_default_no_two_way_values"
app:iconSpaceReserved="false"
app:key="camera_button_action"
app:title="@string/camera_button_action_title"
app:useSimpleSummaryProvider="true" />

<ListPreference
app:defaultValue="focus"
app:entries="@array/gesture_actions_no_default_no_two_way_entries"
app:entryValues="@array/gesture_actions_no_default_no_two_way_values"
app:iconSpaceReserved="false"
app:key="focus_button_action"
app:title="@string/focus_button_action_title"
app:useSimpleSummaryProvider="true" />

</PreferenceCategory>

<PreferenceCategory
app:iconSpaceReserved="false"
app:key="volume_buttons"
app:title="@string/volume_buttons_title">

<ListPreference
app:defaultValue="shutter"
app:entries="@array/gesture_actions_entries"
app:entryValues="@array/gesture_actions_values"
app:iconSpaceReserved="false"
app:key="volume_buttons_action"
app:title="@string/volume_buttons_action_title"
app:useSimpleSummaryProvider="true" />

<SwitchPreference
app:defaultValue="false"
app:iconSpaceReserved="false"
app:key="volume_buttons_invert"
app:summary="@string/volume_buttons_invert_summary"
app:title="@string/volume_buttons_invert_title" />

</PreferenceCategory>

<PreferenceCategory
app:iconSpaceReserved="false"
app:key="zoom_buttons"
app:title="@string/zoom_buttons_title">

<ListPreference
app:defaultValue="zoom"
app:entries="@array/gesture_actions_no_default_entries"
app:entryValues="@array/gesture_actions_no_default_values"
app:iconSpaceReserved="false"
app:key="zoom_buttons_action"
app:title="@string/zoom_buttons_action_title"
app:useSimpleSummaryProvider="true" />

<SwitchPreference
app:defaultValue="false"
app:iconSpaceReserved="false"
app:key="zoom_buttons_invert"
app:summary="@string/zoom_buttons_invert_summary"
app:title="@string/zoom_buttons_invert_title" />

</PreferenceCategory>
app:title="@string/single_buttons_title" />

</PreferenceScreen>

0 comments on commit 1e1064c

Please sign in to comment.