@@ -189,6 +189,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
189189
190190 var leftLongPressAction : StemAction = StemAction .defaultActions[StemPressType .LONG_PRESS ]!!,
191191 var rightLongPressAction : StemAction = StemAction .defaultActions[StemPressType .LONG_PRESS ]!!,
192+
193+ var cameraAction : AACPManager .Companion .StemPressType ? = null ,
192194 )
193195
194196 private lateinit var config: ServiceConfig
@@ -469,6 +471,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
469471 " right_long_press_action" ,
470472 StemAction .defaultActions[StemPressType .LONG_PRESS ]!! .name
471473 )
474+ if (! contains(" camera_action" )) putString(" camera_action" , " SINGLE_PRESS" )
472475
473476 }
474477 }
@@ -735,22 +738,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
735738 @Suppress(" unused" )
736739 fun cameraOpened () {
737740 Log .d(TAG , " Camera opened, gonna handle stem presses and take action if enabled" )
738- val isCameraShutterUsed = listOf (
739- config.leftSinglePressAction,
740- config.rightSinglePressAction,
741- config.leftDoublePressAction,
742- config.rightDoublePressAction,
743- config.leftTriplePressAction,
744- config.rightTriplePressAction,
745- config.leftLongPressAction,
746- config.rightLongPressAction
747- ).any { it == StemAction .CAMERA_SHUTTER }
748-
749- if (isCameraShutterUsed) {
750- Log .d(TAG , " Camera opened, setting up stem actions" )
751- cameraActive = true
752- setupStemActions(isCameraActive = true )
753- }
741+ cameraActive = true
742+ setupStemActions()
754743 }
755744
756745 @Suppress(" unused" )
@@ -761,27 +750,27 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
761750
762751 fun isCustomAction (
763752 action : StemAction ? ,
764- default : StemAction ? ,
765- isCameraActive : Boolean = false
753+ default : StemAction ?
766754 ): Boolean {
767- Log .d(TAG , " Checking if action $action is custom against default $default , camera active: $isCameraActive " )
768- return action != default && (action != StemAction .CAMERA_SHUTTER || isCameraActive)
755+ return action != default
769756 }
770757
771- fun setupStemActions (isCameraActive : Boolean = false ) {
758+ fun setupStemActions () {
772759 val singlePressDefault = StemAction .defaultActions[StemPressType .SINGLE_PRESS ]
773760 val doublePressDefault = StemAction .defaultActions[StemPressType .DOUBLE_PRESS ]
774761 val triplePressDefault = StemAction .defaultActions[StemPressType .TRIPLE_PRESS ]
775762 val longPressDefault = StemAction .defaultActions[StemPressType .LONG_PRESS ]
776763
777- val singlePressCustomized = isCustomAction(config.leftSinglePressAction, singlePressDefault, isCameraActive) ||
778- isCustomAction(config.rightSinglePressAction, singlePressDefault, isCameraActive)
779- val doublePressCustomized = isCustomAction(config.leftDoublePressAction, doublePressDefault, isCameraActive) ||
780- isCustomAction(config.rightDoublePressAction, doublePressDefault, isCameraActive)
781- val triplePressCustomized = isCustomAction(config.leftTriplePressAction, triplePressDefault, isCameraActive) ||
782- isCustomAction(config.rightTriplePressAction, triplePressDefault, isCameraActive)
783- val longPressCustomized = isCustomAction(config.leftLongPressAction, longPressDefault, isCameraActive) ||
784- isCustomAction(config.rightLongPressAction, longPressDefault, isCameraActive)
764+ val singlePressCustomized = isCustomAction(config.leftSinglePressAction, singlePressDefault) ||
765+ isCustomAction(config.rightSinglePressAction, singlePressDefault) ||
766+ (cameraActive && config.cameraAction == StemPressType .SINGLE_PRESS )
767+ val doublePressCustomized = isCustomAction(config.leftDoublePressAction, doublePressDefault) ||
768+ isCustomAction(config.rightDoublePressAction, doublePressDefault)
769+ val triplePressCustomized = isCustomAction(config.leftTriplePressAction, triplePressDefault) ||
770+ isCustomAction(config.rightTriplePressAction, triplePressDefault)
771+ val longPressCustomized = isCustomAction(config.leftLongPressAction, longPressDefault) ||
772+ isCustomAction(config.rightLongPressAction, longPressDefault) ||
773+ (cameraActive && config.cameraAction == StemPressType .LONG_PRESS )
785774 Log .d(TAG , " Setting up stem actions: " +
786775 " Single Press Customized: $singlePressCustomized , " +
787776 " Double Press Customized: $doublePressCustomized , " +
@@ -963,12 +952,14 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
963952 override fun onStemPressReceived (stemPress : ByteArray ) {
964953 val (stemPressType, bud) = aacpManager.parseStemPressResponse(stemPress)
965954
966- Log .d(" AirPodsParser" , " Stem press received: $stemPressType on $bud " )
967-
968- val action = getActionFor(bud, stemPressType)
969- Log .d(" AirPodsParser" , " $bud $stemPressType action: $action " )
970-
971- action?.let { executeStemAction(it) }
955+ Log .d(" AirPodsParser" , " Stem press received: $stemPressType on $bud , cameraActive: $cameraActive , cameraAction: ${config.cameraAction} " )
956+ if (cameraActive && config.cameraAction != null && stemPressType == config.cameraAction) {
957+ Runtime .getRuntime().exec(arrayOf(" su" , " -c" , " input keyevent 27" ))
958+ } else {
959+ val action = getActionFor(bud, stemPressType)
960+ Log .d(" AirPodsParser" , " $bud $stemPressType action: $action " )
961+ action?.let { executeStemAction(it) }
962+ }
972963 }
973964 override fun onAudioSourceReceived (audioSource : ByteArray ) {
974965 Log .d(" AirPodsParser" , " Audio source changed mac: ${aacpManager.audioSource?.mac} , type: ${aacpManager.audioSource?.type?.name} " )
@@ -1024,7 +1015,6 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
10241015 StemAction .PLAY_PAUSE -> MediaController .sendPlayPause()
10251016 StemAction .PREVIOUS_TRACK -> MediaController .sendPreviousTrack()
10261017 StemAction .NEXT_TRACK -> MediaController .sendNextTrack()
1027- StemAction .CAMERA_SHUTTER -> Runtime .getRuntime().exec(arrayOf(" su" , " -c" , " input keyevent 27" ))
10281018 StemAction .DIGITAL_ASSISTANT -> {
10291019 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
10301020 val intent = Intent (Intent .ACTION_VOICE_COMMAND ).apply {
@@ -1171,7 +1161,9 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
11711161 rightTriplePressAction = StemAction .fromString(sharedPreferences.getString(" right_triple_press_action" , " PREVIOUS_TRACK" ) ? : " PREVIOUS_TRACK" )!! ,
11721162
11731163 leftLongPressAction = StemAction .fromString(sharedPreferences.getString(" left_long_press_action" , " CYCLE_NOISE_CONTROL_MODES" ) ? : " CYCLE_NOISE_CONTROL_MODES" )!! ,
1174- rightLongPressAction = StemAction .fromString(sharedPreferences.getString(" right_long_press_action" , " DIGITAL_ASSISTANT" ) ? : " DIGITAL_ASSISTANT" )!!
1164+ rightLongPressAction = StemAction .fromString(sharedPreferences.getString(" right_long_press_action" , " DIGITAL_ASSISTANT" ) ? : " DIGITAL_ASSISTANT" )!! ,
1165+
1166+ cameraAction = sharedPreferences.getString(" camera_action" , null )?.let { AACPManager .Companion .StemPressType .valueOf(it) },
11751167 )
11761168 }
11771169
@@ -1252,6 +1244,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
12521244 )!!
12531245 setupStemActions()
12541246 }
1247+ " camera_action" -> config.cameraAction = preferences.getString(key, null )?.let { AACPManager .Companion .StemPressType .valueOf(it) }
12551248 }
12561249
12571250 if (key == " mac_address" ) {
@@ -1780,6 +1773,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
17801773 handleIncomingCallOnceConnected = false
17811774 }
17821775 }
1776+
17831777 }
17841778 }
17851779
0 commit comments