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