Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3d233931 authored by Dmitry Shmidt's avatar Dmitry Shmidt
Browse files

AudioService: Add reporting of Mic Mute on start



Bug: 141192769
Test: Manual

Change-Id: I728e5bf9594622b7b80b640abd0f4938972d77ef
Signed-off-by: default avatarDmitry Shmidt <dimitrysh@google.com>
parent d6cf4d98
Loading
Loading
Loading
Loading
+26 −1
Original line number Diff line number Diff line
@@ -1856,13 +1856,38 @@ public class AudioManager {
        }
    }

    /**
     * @hide
     * Sets the microphone from switch mute on or off.
     * <p>
     * This method should only be used by InputManager to notify
     * Audio Subsystem about Microphone Mute switch state.
     *
     * @param on set <var>true</var> to mute the microphone;
     *           <var>false</var> to turn mute off
     */
    @UnsupportedAppUsage
    public void setMicrophoneMuteFromSwitch(boolean on) {
        final IAudioService service = getService();
        try {
            service.setMicrophoneMuteFromSwitch(on);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Checks whether the microphone mute is on or off.
     *
     * @return true if microphone is muted, false if it's not
     */
    public boolean isMicrophoneMute() {
        return AudioSystem.isMicrophoneMuted();
        final IAudioService service = getService();
        try {
            return service.isMicrophoneMuted();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
+4 −0
Original line number Diff line number Diff line
@@ -106,8 +106,12 @@ interface IAudioService {

    List<AudioProductStrategy> getAudioProductStrategies();

    boolean isMicrophoneMuted();

    void setMicrophoneMute(boolean on, String callingPackage, int userId);

    oneway void setMicrophoneMuteFromSwitch(boolean on);

    void setRingerModeExternal(int ringerMode, String caller);

    void setRingerModeInternal(int ringerMode, String caller);
+42 −7
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ import android.hardware.hdmi.HdmiAudioSystemClient;
import android.hardware.hdmi.HdmiControlManager;
import android.hardware.hdmi.HdmiPlaybackClient;
import android.hardware.hdmi.HdmiTvClient;
import android.hardware.input.InputManager;
import android.hardware.usb.UsbManager;
import android.media.AudioAttributes;
import android.media.AudioFocusInfo;
@@ -545,6 +546,10 @@ public class AudioService extends IAudioService.Stub
    private String mEnabledSurroundFormats;
    private boolean mSurroundModeChanged;

    private boolean mMicMuteFromSwitch;
    private boolean mMicMuteFromApi;
    private boolean mMicMuteFromRestrictions;

    @GuardedBy("mSettingsLock")
    private int mAssistantUid;

@@ -882,6 +887,8 @@ public class AudioService extends IAudioService.Stub
        mRoleObserver.register();

        onIndicateSystemReady();

        setMicMuteFromSwitchInput();
    }

    RoleObserver mRoleObserver;
@@ -1021,6 +1028,8 @@ public class AudioService extends IAudioService.Stub

        sendMsg(mAudioHandler, MSG_DISPATCH_AUDIO_SERVER_STATE,
                SENDMSG_QUEUE, 1, 0, null, 0);

        setMicMuteFromSwitchInput();
    }

    private void onDispatchAudioServerStateChange(boolean state) {
@@ -2837,20 +2846,45 @@ public class AudioService extends IAudioService.Stub
                != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        setMicrophoneMuteNoCallerCheck(on, userId);
        mMicMuteFromApi = on;
        setMicrophoneMuteNoCallerCheck(userId);
    }

    /** @see AudioManager#setMicrophoneMuteFromSwitch(boolean) */
    public void setMicrophoneMuteFromSwitch(boolean on) {
        int userId = Binder.getCallingUid();
        if (userId != android.os.Process.SYSTEM_UID) {
            Log.e(TAG, "setMicrophoneMuteFromSwitch() called from non system user!");
            return;
        }
        mMicMuteFromSwitch = on;
        setMicrophoneMuteNoCallerCheck(userId);
    }

    private void setMicMuteFromSwitchInput() {
        InputManager im = mContext.getSystemService(InputManager.class);
        final int isMicMuted = im.isMicMuted();
        if (isMicMuted != InputManager.SWITCH_STATE_UNKNOWN) {
            setMicrophoneMuteFromSwitch(im.isMicMuted() != InputManager.SWITCH_STATE_OFF);
        }
    }

    public boolean isMicrophoneMuted() {
        return mMicMuteFromSwitch || mMicMuteFromRestrictions || mMicMuteFromApi;
    }

    private void setMicrophoneMuteNoCallerCheck(boolean on, int userId) {
    private void setMicrophoneMuteNoCallerCheck(int userId) {
        final boolean muted = isMicrophoneMuted();
        if (DEBUG_VOL) {
            Log.d(TAG, String.format("Mic mute %s, user=%d", on, userId));
            Log.d(TAG, String.format("Mic mute %d, user=%d", muted, userId));
        }
        // only mute for the current user
        if (getCurrentUserId() == userId) {
        if (getCurrentUserId() == userId || userId == android.os.Process.SYSTEM_UID) {
            final boolean currentMute = AudioSystem.isMicrophoneMuted();
            final long identity = Binder.clearCallingIdentity();
            AudioSystem.muteMicrophone(on);
            AudioSystem.muteMicrophone(muted);
            Binder.restoreCallingIdentity(identity);
            if (on != currentMute) {
            if (muted != currentMute) {
                mContext.sendBroadcastAsUser(
                        new Intent(AudioManager.ACTION_MICROPHONE_MUTE_CHANGED)
                                .setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), UserHandle.ALL);
@@ -5378,7 +5412,8 @@ public class AudioService extends IAudioService.Stub
                final boolean isRestricted =
                        newRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE);
                if (wasRestricted != isRestricted) {
                    setMicrophoneMuteNoCallerCheck(isRestricted, userId);
                    mMicMuteFromRestrictions = isRestricted;
                    setMicrophoneMuteNoCallerCheck(userId);
                }
            }