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