AudioDeviceCallback fails when sharedUserId="android.uid.system"

Viewed 680

I am developing an app for an Android custom build.

This app needs to subscribe to AudioDeviceCallback in Android's AudioManager.

I'm using:

mAudioManager.registerAudioDeviceCallback(new MyDeviceCallback(), null);

where:

private class MyDeviceCallback extends AudioDeviceCallback {

    @Override
    public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
        Log.d(LOG_TAG, "onAudioDevicesAdded(): New devices detected");
        updateAuxStatus();
    }

    @Override
    public void onAudioDevicesRemoved(AudioDeviceInfo[] devices) {
        Log.d(LOG_TAG, "onAudioDevicesAdded(): devices removed");
        updateAuxStatus();
    }

    private void updateAuxStatus() {
        AudioDeviceInfo[] devices = mAudioManager.getDevices(AudioManager.GET_DEVICES_INPUTS);
        boolean isPluggedIn = false;
        for (AudioDeviceInfo device : devices) {
            if (device.getType() == AudioDeviceInfo.TYPE_LINE_ANALOG) {
                isPluggedIn = true;
            }
        }
        onAuxPluggedInChanged(isPluggedIn);
    }
}

This works fine when I run the app with normal user (u0_aXX) but when I run the app as system sharedUser (adding to the Manifest):

android:sharedUserId="android.uid.system"

This callback is being called only after subscribing, but never again. Even when other apps without system user are getting the call normally.

I've traced the call to the AudioManager code and found that postEventFromNative in the AudioPortEventHandler is never being called for my app when it is running as system sharedUser. Since that is a jni call I stopped because I don't fully understand how it works.

What really troubles me is that when running without system sharedUser the same code is working as intended. Is there some restriction to system sharedUser that might be causing this problem?

1 Answers

In the AudioPolicy, registerClient use uid as the key. When the phone booted, system_server or telecom already register notification, if your app run as system user, it will never working.

mNotificationClients.add(uid, notificationClient);

Related