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

Commit 2a6c3dc7 authored by Amy's avatar Amy Committed by Amy Zhang
Browse files

GiveAudioStatusHandler and test.

Test: atest com.android.server.hdmi
Change-Id: I402d3b783783bf9d79f3b7de55fb7a26f6c10be2
(cherry picked from commit 403a500216047e04fa8aebe8b24d4e3d89d28898)
parent cceb290e
Loading
Loading
Loading
Loading
+22 −0
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@
package com.android.server.hdmi;
package com.android.server.hdmi;


import android.hardware.hdmi.HdmiDeviceInfo;
import android.hardware.hdmi.HdmiDeviceInfo;
import android.media.AudioManager;
import android.os.SystemProperties;
import android.os.SystemProperties;
import com.android.server.hdmi.HdmiAnnotations.ServiceThreadOnly;
import com.android.server.hdmi.HdmiAnnotations.ServiceThreadOnly;


@@ -93,4 +94,25 @@ public class HdmiCecLocalDeviceAudioSystem extends HdmiCecLocalDevice {
        HdmiLogger.debug(TAG + "Stub handleReportArcTermination");
        HdmiLogger.debug(TAG + "Stub handleReportArcTermination");
        return true;
        return true;
    }
    }

    @Override
    @ServiceThreadOnly
    protected boolean handleGiveAudioStatus(HdmiCecMessage message) {
        assertRunOnServiceThread();

        reportAudioStatus(message);
        return true;
    }

    private void reportAudioStatus(HdmiCecMessage message) {
        assertRunOnServiceThread();

        int volume = mService.getAudioManager().getStreamVolume(AudioManager.STREAM_MUSIC);
        boolean mute = mService.getAudioManager().isStreamMute(AudioManager.STREAM_MUSIC);
        int maxVolume = mService.getAudioManager().getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int scaledVolume = VolumeControlAction.scaleToCecVolume(volume, maxVolume);

        mService.sendCecCommand(HdmiCecMessageBuilder
            .buildReportAudioStatus(mAddress, message.getSource(), scaledVolume, mute));
    }
}
}
+23 −0
Original line number Original line Diff line number Diff line
@@ -16,9 +16,11 @@


package com.android.server.hdmi;
package com.android.server.hdmi;


import android.annotation.Nullable;
import libcore.util.EmptyArray;
import libcore.util.EmptyArray;


import java.util.Arrays;
import java.util.Arrays;
import java.util.Objects;


/**
/**
 * A class to encapsulate HDMI-CEC message used for the devices connected via
 * A class to encapsulate HDMI-CEC message used for the devices connected via
@@ -44,6 +46,27 @@ public final class HdmiCecMessage {
        mParams = Arrays.copyOf(params, params.length);
        mParams = Arrays.copyOf(params, params.length);
    }
    }


    @Override
    public boolean equals(@Nullable Object message) {
        if (message instanceof HdmiCecMessage) {
            HdmiCecMessage that = (HdmiCecMessage) message;
            return this.mSource == that.getSource() &&
                this.mDestination == that.getDestination() &&
                this.mOpcode == that.getOpcode() &&
                Arrays.equals(this.mParams, that.getParams());
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(
            mSource,
            mDestination,
            mOpcode,
            Arrays.hashCode(mParams));
    }

    /**
    /**
     * Return the source address field of the message. It is the logical address
     * Return the source address field of the message. It is the logical address
     * of the device which generated the message.
     * of the device which generated the message.
+109 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.server.hdmi;

import static com.android.server.hdmi.Constants.ADDR_AUDIO_SYSTEM;
import static com.android.server.hdmi.Constants.ADDR_TV;
import static com.android.server.hdmi.Constants.ADDR_UNREGISTERED;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.assertEquals;

import android.media.AudioManager;
import android.support.test.filters.SmallTest;
import junit.framework.Assert;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@SmallTest
@RunWith(JUnit4.class)
/**
 * Tests for {@link HdmiCecLocalDeviceAudioSystem} class.
 */
public class HdmiCecLocalDeviceAudioSystemTest {


    private HdmiControlService mHdmiControlService;
    private HdmiCecLocalDeviceAudioSystem mHdmiCecLocalDeviceAudioSystem;
    private HdmiCecMessage mResultMessage;
    private int mMusicVolume;
    private int mMusicMaxVolume;
    private boolean mMusicMute;

    @Before
    public void SetUp() {
        mHdmiControlService = new HdmiControlService(null) {
            @Override
            AudioManager getAudioManager() {
                return new AudioManager() {
                    @Override
                    public int getStreamVolume(int streamType) {
                        switch (streamType) {
                            case STREAM_MUSIC:
                                return mMusicVolume;
                            default:
                                return 0;
                        }
                    }

                    @Override
                    public boolean isStreamMute(int streamType) {
                        switch (streamType) {
                            case STREAM_MUSIC:
                                return mMusicMute;
                            default:
                                return false;
                        }
                    }

                    @Override
                    public int getStreamMaxVolume(int streamType) {
                        switch (streamType) {
                            case STREAM_MUSIC:
                                return mMusicMaxVolume;
                            default:
                                return 100;
                        }
                    }
                };
            }

            @Override
            void sendCecCommand(HdmiCecMessage command) {
                mResultMessage = command;
            }
        };
        mHdmiCecLocalDeviceAudioSystem = new HdmiCecLocalDeviceAudioSystem(mHdmiControlService);
    }

    @Test
    public void handleGiveAudioStatus_volume_10_mute_true() {
        mMusicVolume = 10;
        mMusicMute = true;
        mMusicMaxVolume = 20;
        int scaledVolume = VolumeControlAction.scaleToCecVolume(10, mMusicMaxVolume);
        HdmiCecMessage expectMessage = HdmiCecMessageBuilder.buildReportAudioStatus(
            ADDR_UNREGISTERED, ADDR_TV, scaledVolume, true);

        HdmiCecMessage message = HdmiCecMessageBuilder.buildGiveAudioStatus(
            ADDR_TV, ADDR_AUDIO_SYSTEM);
        assertTrue(mHdmiCecLocalDeviceAudioSystem.handleGiveAudioStatus(message));

        assertTrue(mResultMessage.equals(expectMessage));
    }
}