keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | package com.android.car; |
| 17 | |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 18 | import android.car.media.CarAudioManager; |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.res.Resources; |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 21 | import android.media.AudioAttributes; |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 22 | import android.util.Log; |
| 23 | |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 24 | import java.io.PrintWriter; |
| 25 | import java.util.Arrays; |
| 26 | |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Holds audio routing policy from config.xml. R.array.audioRoutingPolicy can contain |
| 29 | * multiple policies and VEHICLE_PROPERTY_AUDIO_HW_VARIANT decide which one to use. |
| 30 | */ |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 31 | public class AudioRoutingPolicy { |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 32 | |
| 33 | private final int USAGE_TYPE_INVALID = -1; |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 34 | |
Keun-young Park | 32b6382 | 2016-08-02 11:22:29 -0700 | [diff] [blame] | 35 | private static final String ROUTING_POLICY_FOR_MOCKED_TEST = |
| 36 | "0:call,media,radio,unknown#1:nav_guidance,voice_command,alarm,notification,system,safety"; |
| 37 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 38 | /** Physical stream to logical streams mapping */ |
| 39 | private final int[][] mLogicalStreams; |
| 40 | /** Logical stream to physical stream mapping */ |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 41 | private final int[] mPhysicalStreamForLogicalStream; |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 42 | |
| 43 | public static AudioRoutingPolicy create(Context context, int policyNumber) { |
| 44 | final Resources res = context.getResources(); |
| 45 | String[] policies = res.getStringArray(R.array.audioRoutingPolicy); |
Keun-young Park | 32b6382 | 2016-08-02 11:22:29 -0700 | [diff] [blame] | 46 | String policy; |
Keun-young Park | 4c6834a | 2016-06-28 12:58:23 -0700 | [diff] [blame] | 47 | if (policyNumber > (policies.length - 1)) { |
| 48 | Log.e(CarLog.TAG_AUDIO, "AudioRoutingPolicy.create got wrong policy number:" + |
| 49 | policyNumber + ", num of avaiable policies:" + policies.length); |
Keun-young Park | 32b6382 | 2016-08-02 11:22:29 -0700 | [diff] [blame] | 50 | policy = policies[0]; |
| 51 | } else if (policyNumber < 0) { // this is special case for mocked testing. |
| 52 | policy = ROUTING_POLICY_FOR_MOCKED_TEST; |
| 53 | } else { |
| 54 | policy = policies[policyNumber]; |
Keun-young Park | 4c6834a | 2016-06-28 12:58:23 -0700 | [diff] [blame] | 55 | } |
Keun-young Park | 32b6382 | 2016-08-02 11:22:29 -0700 | [diff] [blame] | 56 | return new AudioRoutingPolicy(policy); |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | private static int getStreamType(String str) { |
| 60 | switch (str) { |
| 61 | case "call": |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 62 | return CarAudioManager.CAR_AUDIO_USAGE_VOICE_CALL; |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 63 | case "media": |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 64 | return CarAudioManager.CAR_AUDIO_USAGE_MUSIC; |
Keun-young Park | 3057ebd | 2016-03-28 18:12:09 -0700 | [diff] [blame] | 65 | case "radio": |
| 66 | return CarAudioManager.CAR_AUDIO_USAGE_RADIO; |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 67 | case "nav_guidance": |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 68 | return CarAudioManager.CAR_AUDIO_USAGE_NAVIGATION_GUIDANCE; |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 69 | case "voice_command": |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 70 | return CarAudioManager.CAR_AUDIO_USAGE_VOICE_COMMAND; |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 71 | case "alarm": |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 72 | return CarAudioManager.CAR_AUDIO_USAGE_ALARM; |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 73 | case "notification": |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 74 | return CarAudioManager.CAR_AUDIO_USAGE_NOTIFICATION; |
| 75 | case "system": |
| 76 | return CarAudioManager.CAR_AUDIO_USAGE_SYSTEM_SOUND; |
| 77 | case "safety": |
| 78 | return CarAudioManager.CAR_AUDIO_USAGE_SYSTEM_SAFETY_ALERT; |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 79 | case "unknown": |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 80 | return CarAudioManager.CAR_AUDIO_USAGE_DEFAULT; |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 81 | } |
| 82 | throw new IllegalArgumentException("Wrong audioRoutingPolicy config, unknown stream type:" + |
| 83 | str); |
| 84 | } |
| 85 | |
| 86 | private AudioRoutingPolicy(String policy) { |
| 87 | String[] streamPolicies = policy.split("#"); |
| 88 | final int nPhysicalStreams = streamPolicies.length; |
| 89 | mLogicalStreams = new int[nPhysicalStreams][]; |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 90 | mPhysicalStreamForLogicalStream = new int[CarAudioManager.CAR_AUDIO_USAGE_MAX + 1]; |
| 91 | for (int i = 0; i < mPhysicalStreamForLogicalStream.length; i++) { |
| 92 | mPhysicalStreamForLogicalStream[i] = USAGE_TYPE_INVALID; |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 93 | } |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 94 | int defaultStreamType = USAGE_TYPE_INVALID; |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 95 | for (String streamPolicy : streamPolicies) { |
| 96 | String[] numberVsStreams = streamPolicy.split(":"); |
| 97 | int physicalStream = Integer.parseInt(numberVsStreams[0]); |
| 98 | String[] logicalStreams = numberVsStreams[1].split(","); |
| 99 | int[] logicalStreamsInt = new int[logicalStreams.length]; |
| 100 | for (int i = 0; i < logicalStreams.length; i++) { |
| 101 | int logicalStreamNumber = getStreamType(logicalStreams[i]); |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 102 | if (logicalStreamNumber == CarAudioManager.CAR_AUDIO_USAGE_DEFAULT) { |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 103 | defaultStreamType = physicalStream; |
| 104 | } |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 105 | logicalStreamsInt[i] = logicalStreamNumber; |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 106 | mPhysicalStreamForLogicalStream[logicalStreamNumber] = physicalStream; |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 107 | } |
| 108 | Arrays.sort(logicalStreamsInt); |
| 109 | mLogicalStreams[physicalStream] = logicalStreamsInt; |
| 110 | } |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 111 | if (defaultStreamType == USAGE_TYPE_INVALID) { |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 112 | Log.e(CarLog.TAG_AUDIO, "Audio routing policy did not include unknown"); |
| 113 | defaultStreamType = 0; |
| 114 | } |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 115 | for (int i = 0; i < mPhysicalStreamForLogicalStream.length; i++) { |
| 116 | if (mPhysicalStreamForLogicalStream[i] == USAGE_TYPE_INVALID) { |
Keun-young Park | 3057ebd | 2016-03-28 18:12:09 -0700 | [diff] [blame] | 117 | Log.w(CarLog.TAG_AUDIO, "Audio routing policy did not cover logical stream " + i); |
| 118 | mPhysicalStreamForLogicalStream[i] = defaultStreamType; |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | public int getPhysicalStreamsCount() { |
| 124 | return mLogicalStreams.length; |
| 125 | } |
| 126 | |
| 127 | public int[] getLogicalStreamsForPhysicalStream(int physicalStreamNumber) { |
| 128 | return mLogicalStreams[physicalStreamNumber]; |
| 129 | } |
| 130 | |
| 131 | public int getPhysicalStreamForLogicalStream(int logicalStream) { |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 132 | return mPhysicalStreamForLogicalStream[logicalStream]; |
keunyoung | a74b9ca | 2015-10-21 13:33:58 -0700 | [diff] [blame] | 133 | } |
| 134 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 135 | public void dump(PrintWriter writer) { |
| 136 | writer.println("*AudioRoutingPolicy*"); |
| 137 | writer.println("**Logical Streams**"); |
| 138 | for (int i = 0; i < mLogicalStreams.length; i++) { |
| 139 | writer.print("physical stream " + i + ":"); |
| 140 | for (int logicalStream : mLogicalStreams[i]) { |
| 141 | writer.print(Integer.toString(logicalStream) + ","); |
| 142 | } |
| 143 | writer.println(""); |
| 144 | } |
| 145 | } |
| 146 | } |