How can I intercept "touch screen event" within my Android App from an External (car) Display?

176 Views Asked by At

Here's the part of code that deal with MediaSession of my actual MediaPlayerService class on Android (API Level 30):

@Override
    public void onCreate() {
        super.onCreate();

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioAttributes(new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build());

        mediaSession = new MediaSessionCompat(this, TAG);
        mediaSession.setCallback(new MediaSessionCallback());
        mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
                MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

        playbackStateBuilder = new PlaybackStateCompat.Builder()
                .setActions(PlaybackStateCompat.ACTION_PLAY |
                        PlaybackStateCompat.ACTION_PAUSE |
                        PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
                        PlaybackStateCompat.ACTION_SKIP_TO_NEXT);

        mediaSession.setPlaybackState(playbackStateBuilder.build());
        mediaSession.setActive(true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (mediaPlayer != null) {
            mediaPlayer.release();
        }
        if (mediaSession != null) {
            mediaSession.release();
        }
    }

    private class MediaSessionCallback extends MediaSessionCompat.Callback {
        @Override
        public void onPlay() {
            super.onPlay();
            play();
        }

        @Override
        public void onPause() {
            super.onPause();
            pause();
        }

        @Override
        public void onSkipToNext() {
            super.onSkipToNext();
            playSong(1);
        }

        @Override
        public void onSkipToPrevious() {
            super.onSkipToPrevious();
            playSong(-1);
        }
    }

Basically, it's a Music Player which received commands from my Car once connected via Bluetooth (play next, previous, and so on).

What I'd like to do is to intercept the touch of my Car Display (where the song/artist information are shown, so to speak).

Something like "touch ⇒ trigger a method on my App".

How can I achieve this? Is it possible? Thanks

1

There are 1 best solutions below

0
VonC On

Given the current Android API and standard practices with car infotainment systems, intercepting touch events directly from a car display to control an Android app does not seem feasible.

Android's input event system is designed to handle touch, key, and other input events from devices that are directly connected or integral to the device (e.g., the device's touchscreen, physical buttons, sensors).
External displays, especially those not explicitly designed to work with Android's input system, do not directly send touch events to connected Android devices in a manner that Android apps can intercept.

Most car displays operate independently of connected devices. When you connect your Android device to a car's infotainment system (via Bluetooth, USB, or other), the connection is usually limited to specific protocols (e.g., for audio streaming, call handling, or displaying metadata). These protocols do not include a standard for forwarding touch events from the car's display to a connected Android device.

Plus, allowing external devices to directly inject input events into an Android device would raise significant security and privacy concerns. That would make it easier for malicious devices to mimic user input, potentially leading to unauthorized actions.