1 /* 2 * Copyright (C) 2018 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.settings.sound; 18 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import android.content.res.XmlResourceParser; 22 import android.media.AudioAttributes; 23 import android.util.AttributeSet; 24 import android.util.SparseArray; 25 import android.util.Xml; 26 27 import androidx.annotation.DrawableRes; 28 import androidx.annotation.StringRes; 29 import androidx.annotation.XmlRes; 30 31 import com.android.car.settings.R; 32 import com.android.car.settings.common.Logger; 33 34 import org.xmlpull.v1.XmlPullParserException; 35 36 import java.io.IOException; 37 38 /** 39 * Parses the xml file which specifies which Audio usages should be considered by sound settings. 40 */ 41 public class VolumeItemParser { 42 private static final Logger LOG = new Logger(VolumeItemParser.class); 43 44 private static final String XML_TAG_VOLUME_ITEMS = "carVolumeItems"; 45 private static final String XML_TAG_VOLUME_ITEM = "item"; 46 47 /** 48 * Parses the volume items listed in the xml resource provided. This is returned as a sparse 49 * array which is keyed by the rank (the order in which the volume item appears in the xml 50 * resrouce). 51 */ loadAudioUsageItems(Context context, @XmlRes int volumeItemsXml)52 public static SparseArray<VolumeItem> loadAudioUsageItems(Context context, 53 @XmlRes int volumeItemsXml) { 54 SparseArray<VolumeItem> volumeItems = new SparseArray<>(); 55 try (XmlResourceParser parser = context.getResources().getXml(volumeItemsXml)) { 56 AttributeSet attrs = Xml.asAttributeSet(parser); 57 int type; 58 // Traverse to the first start tag. 59 while ((type = parser.next()) != XmlResourceParser.END_DOCUMENT 60 && type != XmlResourceParser.START_TAG) { 61 continue; 62 } 63 64 if (!XML_TAG_VOLUME_ITEMS.equals(parser.getName())) { 65 throw new RuntimeException("Meta-data does not start with carVolumeItems tag"); 66 } 67 int outerDepth = parser.getDepth(); 68 int rank = 0; 69 while ((type = parser.next()) != XmlResourceParser.END_DOCUMENT 70 && (type != XmlResourceParser.END_TAG || parser.getDepth() > outerDepth)) { 71 if (type == XmlResourceParser.END_TAG) { 72 continue; 73 } 74 if (XML_TAG_VOLUME_ITEM.equals(parser.getName())) { 75 TypedArray item = context.getResources().obtainAttributes( 76 attrs, R.styleable.carVolumeItems_item); 77 int usage = item.getInt(R.styleable.carVolumeItems_item_usage, -1); 78 if (usage >= 0) { 79 volumeItems.put(usage, new VolumeItemParser.VolumeItem( 80 usage, rank, 81 item.getResourceId(R.styleable.carVolumeItems_item_titleText, 0), 82 item.getResourceId(R.styleable.carVolumeItems_item_icon, 0))); 83 rank++; 84 } 85 item.recycle(); 86 } 87 } 88 } catch (XmlPullParserException | IOException e) { 89 LOG.e("Error parsing volume groups configuration", e); 90 } 91 return volumeItems; 92 } 93 94 /** 95 * Wrapper class which contains information to render volume item on UI. 96 */ 97 public static class VolumeItem { 98 @AudioAttributes.AttributeUsage 99 private final int mUsage; 100 private final int mRank; 101 @StringRes 102 private final int mTitle; 103 @DrawableRes 104 private final int mIcon; 105 106 /** Constructs the VolumeItem container with the given values. */ VolumeItem(@udioAttributes.AttributeUsage int usage, int rank, @StringRes int title, @DrawableRes int icon)107 public VolumeItem(@AudioAttributes.AttributeUsage int usage, int rank, 108 @StringRes int title, @DrawableRes int icon) { 109 mUsage = usage; 110 mRank = rank; 111 mTitle = title; 112 mIcon = icon; 113 } 114 115 /** 116 * Usage is used to represent what purpose the sound is used for. The values should be 117 * defined within AudioAttributes.USAGE_*. 118 */ getUsage()119 public int getUsage() { 120 return mUsage; 121 } 122 123 /** 124 * Rank represents the order in which the usage appears in 125 * {@link R.xml#car_volume_items}. This order is used to determine which title and icon 126 * should be used for each audio group. The lowest rank has the highest precedence. 127 */ getRank()128 public int getRank() { 129 return mRank; 130 } 131 132 /** Title which should be used for the seek bar preference. */ getTitle()133 public int getTitle() { 134 return mTitle; 135 } 136 137 /** Icon which should be used for the seek bar preference. */ getIcon()138 public int getIcon() { 139 return mIcon; 140 } 141 } 142 } 143