1 /* 2 * Copyright (C) 2022 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.settingslib.media; 18 19 import android.annotation.DrawableRes; 20 import android.content.Context; 21 import android.graphics.drawable.Drawable; 22 import android.media.AudioDeviceInfo; 23 import android.media.MediaRoute2Info; 24 25 import com.android.settingslib.R; 26 27 import java.util.Arrays; 28 import java.util.HashMap; 29 import java.util.List; 30 import java.util.Map; 31 32 /** A util class to get the appropriate icon for different device types. */ 33 public class DeviceIconUtil { 34 // A map from a @AudioDeviceInfo.AudioDeviceType to full device information. 35 private final Map<Integer, Device> mAudioDeviceTypeToIconMap = new HashMap<>(); 36 // A map from a @MediaRoute2Info.Type to full device information. 37 private final Map<Integer, Device> mMediaRouteTypeToIconMap = new HashMap<>(); 38 // A default icon to use if the type is not present in the map. 39 @DrawableRes private static final int DEFAULT_ICON = R.drawable.ic_smartphone; 40 DeviceIconUtil()41 public DeviceIconUtil() { 42 List<Device> deviceList = Arrays.asList( 43 new Device( 44 AudioDeviceInfo.TYPE_USB_DEVICE, 45 MediaRoute2Info.TYPE_USB_DEVICE, 46 R.drawable.ic_headphone), 47 new Device( 48 AudioDeviceInfo.TYPE_USB_HEADSET, 49 MediaRoute2Info.TYPE_USB_HEADSET, 50 R.drawable.ic_headphone), 51 new Device( 52 AudioDeviceInfo.TYPE_USB_ACCESSORY, 53 MediaRoute2Info.TYPE_USB_ACCESSORY, 54 R.drawable.ic_headphone), 55 new Device( 56 AudioDeviceInfo.TYPE_DOCK, 57 MediaRoute2Info.TYPE_DOCK, 58 R.drawable.ic_dock_device), 59 new Device( 60 AudioDeviceInfo.TYPE_HDMI, 61 MediaRoute2Info.TYPE_HDMI, 62 R.drawable.ic_headphone), 63 new Device( 64 AudioDeviceInfo.TYPE_WIRED_HEADSET, 65 MediaRoute2Info.TYPE_WIRED_HEADSET, 66 R.drawable.ic_headphone), 67 new Device( 68 AudioDeviceInfo.TYPE_WIRED_HEADPHONES, 69 MediaRoute2Info.TYPE_WIRED_HEADPHONES, 70 R.drawable.ic_headphone), 71 new Device( 72 AudioDeviceInfo.TYPE_BUILTIN_SPEAKER, 73 MediaRoute2Info.TYPE_BUILTIN_SPEAKER, 74 R.drawable.ic_smartphone)); 75 for (int i = 0; i < deviceList.size(); i++) { 76 Device device = deviceList.get(i); 77 mAudioDeviceTypeToIconMap.put(device.mAudioDeviceType, device); 78 mMediaRouteTypeToIconMap.put(device.mMediaRouteType, device); 79 } 80 } 81 82 /** Returns a drawable for an icon representing the given audioDeviceType. */ getIconFromAudioDeviceType( @udioDeviceInfo.AudioDeviceType int audioDeviceType, Context context)83 public Drawable getIconFromAudioDeviceType( 84 @AudioDeviceInfo.AudioDeviceType int audioDeviceType, Context context) { 85 return context.getDrawable(getIconResIdFromAudioDeviceType(audioDeviceType)); 86 } 87 88 /** Returns a drawable res ID for an icon representing the given audioDeviceType. */ 89 @DrawableRes getIconResIdFromAudioDeviceType( @udioDeviceInfo.AudioDeviceType int audioDeviceType)90 public int getIconResIdFromAudioDeviceType( 91 @AudioDeviceInfo.AudioDeviceType int audioDeviceType) { 92 if (mAudioDeviceTypeToIconMap.containsKey(audioDeviceType)) { 93 return mAudioDeviceTypeToIconMap.get(audioDeviceType).mIconDrawableRes; 94 } 95 return DEFAULT_ICON; 96 } 97 98 /** Returns a drawable res ID for an icon representing the given mediaRouteType. */ 99 @DrawableRes getIconResIdFromMediaRouteType( @ediaRoute2Info.Type int mediaRouteType)100 public int getIconResIdFromMediaRouteType( 101 @MediaRoute2Info.Type int mediaRouteType) { 102 if (mMediaRouteTypeToIconMap.containsKey(mediaRouteType)) { 103 return mMediaRouteTypeToIconMap.get(mediaRouteType).mIconDrawableRes; 104 } 105 return DEFAULT_ICON; 106 } 107 108 private static class Device { 109 @AudioDeviceInfo.AudioDeviceType 110 private final int mAudioDeviceType; 111 112 @MediaRoute2Info.Type 113 private final int mMediaRouteType; 114 115 @DrawableRes 116 private final int mIconDrawableRes; 117 Device(@udioDeviceInfo.AudioDeviceType int audioDeviceType, @MediaRoute2Info.Type int mediaRouteType, @DrawableRes int iconDrawableRes)118 Device(@AudioDeviceInfo.AudioDeviceType int audioDeviceType, 119 @MediaRoute2Info.Type int mediaRouteType, 120 @DrawableRes int iconDrawableRes) { 121 mAudioDeviceType = audioDeviceType; 122 mMediaRouteType = mediaRouteType; 123 mIconDrawableRes = iconDrawableRes; 124 } 125 } 126 } 127