Hayden Gomes | 38e1813 | 2020-04-09 11:29:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.car.audio.hal; |
| 18 | |
| 19 | import android.hardware.automotive.audiocontrol.V2_0.IAudioControl; |
| 20 | import android.hardware.automotive.audiocontrol.V2_0.ICloseHandle; |
| 21 | import android.hardware.automotive.audiocontrol.V2_0.IFocusListener; |
| 22 | import android.media.AudioAttributes; |
| 23 | import android.media.AudioAttributes.AttributeUsage; |
| 24 | import android.os.RemoteException; |
| 25 | import android.util.Log; |
| 26 | |
| 27 | import androidx.annotation.Nullable; |
| 28 | |
| 29 | import java.io.PrintWriter; |
| 30 | import java.util.NoSuchElementException; |
| 31 | import java.util.Objects; |
| 32 | |
| 33 | final class AudioControlWrapperV2 implements AudioControlWrapper { |
| 34 | private static final String TAG = AudioControlWrapperV2.class.getSimpleName(); |
| 35 | |
| 36 | private IAudioControl mAudioControlV2; |
| 37 | |
Hayden Gomes | 890ad02 | 2020-04-09 15:53:58 -0700 | [diff] [blame] | 38 | private AudioControlDeathRecipient mDeathRecipient; |
Hayden Gomes | 38e1813 | 2020-04-09 11:29:25 -0700 | [diff] [blame] | 39 | private ICloseHandle mCloseHandle; |
| 40 | |
| 41 | public static @Nullable IAudioControl getService() { |
| 42 | try { |
| 43 | return IAudioControl.getService(true); |
| 44 | } catch (RemoteException e) { |
| 45 | throw new IllegalStateException("Failed to get IAudioControl@2.0 service", e); |
| 46 | } catch (NoSuchElementException e) { |
| 47 | return null; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | AudioControlWrapperV2(IAudioControl audioControlV2) { |
| 52 | mAudioControlV2 = Objects.requireNonNull(audioControlV2); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void unregisterFocusListener() { |
| 57 | if (mCloseHandle != null) { |
| 58 | try { |
| 59 | mCloseHandle.close(); |
| 60 | } catch (RemoteException e) { |
| 61 | Log.e(TAG, "Failed to close focus listener", e); |
| 62 | } finally { |
| 63 | mCloseHandle = null; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean supportsHalAudioFocus() { |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void registerFocusListener(IFocusListener focusListener) { |
| 75 | Log.d(TAG, "Registering focus listener on AudioControl HAL"); |
| 76 | try { |
| 77 | mCloseHandle = mAudioControlV2.registerFocusListener(focusListener); |
| 78 | } catch (RemoteException e) { |
| 79 | Log.e(TAG, "Failed to register focus listener"); |
| 80 | throw new IllegalStateException("IAudioControl#registerFocusListener failed", e); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void onAudioFocusChange(@AttributeUsage int usage, int zoneId, int focusChange) { |
| 86 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 87 | Log.d(TAG, "onAudioFocusChange: usage " + AudioAttributes.usageToString(usage) |
| 88 | + ", zoneId " + zoneId + ", focusChange " + focusChange); |
| 89 | } |
| 90 | try { |
| 91 | mAudioControlV2.onAudioFocusChange(usage, zoneId, focusChange); |
| 92 | } catch (RemoteException e) { |
| 93 | throw new IllegalStateException("Failed to query IAudioControl#onAudioFocusChange", e); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Dumps the current state of the {@code AudioControlWrapperV2}. |
| 99 | * |
| 100 | * @param indent indent to append to each new line. |
| 101 | * @param writer stream to write current state. |
| 102 | */ |
| 103 | @Override |
| 104 | public void dump(String indent, PrintWriter writer) { |
| 105 | writer.printf("%s*AudioControlWrapperV2*\n", indent); |
| 106 | writer.printf("%s\tFocus listener registered on HAL? %b", indent, (mCloseHandle != null)); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void setFadeTowardFront(float value) { |
| 111 | try { |
| 112 | mAudioControlV2.setFadeTowardFront(value); |
| 113 | } catch (RemoteException e) { |
| 114 | Log.e(TAG, "setFadeTowardFront failed", e); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void setBalanceTowardRight(float value) { |
| 120 | try { |
| 121 | mAudioControlV2.setBalanceTowardRight(value); |
| 122 | } catch (RemoteException e) { |
| 123 | Log.e(TAG, "setBalanceTowardRight failed", e); |
| 124 | } |
| 125 | } |
Hayden Gomes | 890ad02 | 2020-04-09 15:53:58 -0700 | [diff] [blame] | 126 | |
| 127 | @Override |
| 128 | public void linkToDeath(@Nullable AudioControlDeathRecipient deathRecipient) { |
| 129 | try { |
| 130 | mAudioControlV2.linkToDeath(this::serviceDied, 0); |
| 131 | mDeathRecipient = deathRecipient; |
| 132 | } catch (RemoteException e) { |
| 133 | throw new IllegalStateException("Call to IAudioControl@2.0#linkToDeath failed", e); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void unlinkToDeath() { |
| 139 | try { |
| 140 | mAudioControlV2.unlinkToDeath(this::serviceDied); |
| 141 | mDeathRecipient = null; |
| 142 | } catch (RemoteException e) { |
| 143 | throw new IllegalStateException("Call to IAudioControl@2.0#unlinkToDeath failed", e); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | private void serviceDied(long cookie) { |
| 148 | Log.w(TAG, "IAudioControl@2.0 died. Fetching new handle"); |
| 149 | mAudioControlV2 = AudioControlWrapperV2.getService(); |
| 150 | linkToDeath(mDeathRecipient); |
| 151 | if (mDeathRecipient != null) { |
| 152 | mDeathRecipient.serviceDied(); |
| 153 | } |
| 154 | } |
Hayden Gomes | 38e1813 | 2020-04-09 11:29:25 -0700 | [diff] [blame] | 155 | } |