Android Question Obfuscation causes ActivityOnKey event to always return null

Charlie Burnham

Member
Licensed User
Longtime User
Hi, I'm using a BT media button device to provide remote control of my app by detecting media button (key) presses with ActivityOnKey event. This works well in regular release mode. However when compiled in obfuscated mode, the log output is "activity raiseEvent returned null" when any media button is pressed. This also occurs if I press volume up or down on the phone. Long-pressing the media play/pause button stops my activity and offers to turn on Google Assistant, which would be very undesirable on many levels.

I can't figure out why obfuscation would affect the key pressed processing. Perhaps it should be renamed Activity_OnKey, but I have no idea how to do this since it isn't my library.
 

Charlie Burnham

Member
Licensed User
Longtime User
It is actually reflection, which I do not fully understand. This is the skeletized code, which works well in non-obfuscated form, but not in obfuscated form. In non-obfuscated form, when you physically press the volume down button.
B4X:
Sub Globals
    Dim re As Reflector
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    re.Target = Activity
    re.SetOnKeyListener("ActivityOnKey")
    re.RunMethod2("setFocusable", "True",  "java.lang.boolean")
    re.RunMethod2("setFocusableInTouchMode", "True", "java.lang.boolean")
    Activity.RequestFocus   
End Sub

Private Sub ActivityOnKey(vTag As Object, keyCode As Int, KeyEvent As Object) As Boolean
    Log("Key detected")
    
    re.Target = KeyEvent
    Dim Action As Int = re.RunMethod("getAction")
    Log(Action)
    Select Action
        Case Activity.ACTION_UP
            
            Select keyCode               
                Case KeyCodes.KEYCODE_MEDIA_PREVIOUS                   
                        'click a button in the app
                    Return(True) 'to cancel the event
                        
                    'longer press
                Case KeyCodes.KEYCODE_MEDIA_PLAY_PAUSE
                    'click another button in the app                   
                    Return(True) 'to cancel the event           
            
                Case KeyCodes.KEYCODE_MEDIA_NEXT                                       
                    'click another button in the app               
                    Return(True) 'to cancel the event
            
                Case KeyCodes.KEYCODE_VOLUME_UP
                     'click some other button
                    Return True
                Case KeyCodes.KEYCODE_VOLUME_DOWN
                    Log("Volume Down Physical Button (Action = Up)")                   
                    'click some other button
                    Return(True) 'to cancel the event           
            End Select 'for case action up keycodes       
    End Select 'for all actions
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Private Sub ActivityOnKey(vTag As Object, keyCode As Int, KeyEvent As Object) As Boolean
add a _ to the subname to prevent renaming the sub when using obfuscation.
B4X:
Private Sub Activity_OnKey(vTag As Object, keyCode As Int, KeyEvent As Object) As Boolean
re.SetOnKeyListener("ActivityOnKey")
B4X:
re.SetOnKeyListener("Activity_OnKey")
 
Upvote 0

Charlie Burnham

Member
Licensed User
Longtime User
That did the trick. Thank you, Don, as always. Hazards of borrowing someone else's code without understanding it. I totally missed that I had control over the listener name. Best, Charlie
 
Upvote 0
Top