1 /* 2 * Copyright 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 package com.android.settingslib.media; 17 18 import static android.media.MediaRoute2Info.TYPE_BUILTIN_SPEAKER; 19 import static android.media.MediaRoute2Info.TYPE_DOCK; 20 import static android.media.MediaRoute2Info.TYPE_HDMI; 21 import static android.media.MediaRoute2Info.TYPE_USB_ACCESSORY; 22 import static android.media.MediaRoute2Info.TYPE_USB_DEVICE; 23 import static android.media.MediaRoute2Info.TYPE_USB_HEADSET; 24 import static android.media.MediaRoute2Info.TYPE_WIRED_HEADPHONES; 25 import static android.media.MediaRoute2Info.TYPE_WIRED_HEADSET; 26 27 import android.content.Context; 28 import android.graphics.drawable.Drawable; 29 import android.media.MediaRoute2Info; 30 import android.media.MediaRouter2Manager; 31 32 import androidx.annotation.VisibleForTesting; 33 34 import com.android.settingslib.R; 35 import com.android.settingslib.bluetooth.BluetoothUtils; 36 37 /** 38 * PhoneMediaDevice extends MediaDevice to represents Phone device. 39 */ 40 public class PhoneMediaDevice extends MediaDevice { 41 42 private static final String TAG = "PhoneMediaDevice"; 43 44 public static final String PHONE_ID = "phone_media_device_id"; 45 // For 3.5 mm wired headset 46 public static final String WIRED_HEADSET_ID = "wired_headset_media_device_id"; 47 public static final String USB_HEADSET_ID = "usb_headset_media_device_id"; 48 49 private String mSummary = ""; 50 PhoneMediaDevice(Context context, MediaRouter2Manager routerManager, MediaRoute2Info info, String packageName)51 PhoneMediaDevice(Context context, MediaRouter2Manager routerManager, MediaRoute2Info info, 52 String packageName) { 53 super(context, routerManager, info, packageName); 54 55 initDeviceRecord(); 56 } 57 58 @Override getName()59 public String getName() { 60 CharSequence name; 61 switch (mRouteInfo.getType()) { 62 case TYPE_WIRED_HEADSET: 63 case TYPE_WIRED_HEADPHONES: 64 case TYPE_USB_DEVICE: 65 case TYPE_USB_HEADSET: 66 case TYPE_USB_ACCESSORY: 67 name = mContext.getString(R.string.media_transfer_wired_usb_device_name); 68 break; 69 case TYPE_DOCK: 70 case TYPE_HDMI: 71 name = mRouteInfo.getName(); 72 break; 73 case TYPE_BUILTIN_SPEAKER: 74 default: 75 name = mContext.getString(R.string.media_transfer_this_device_name); 76 break; 77 } 78 return name.toString(); 79 } 80 81 @Override getSummary()82 public String getSummary() { 83 return mSummary; 84 } 85 86 @Override getIcon()87 public Drawable getIcon() { 88 final Drawable drawable = getIconWithoutBackground(); 89 setColorFilter(drawable); 90 return BluetoothUtils.buildAdvancedDrawable(mContext, drawable); 91 } 92 93 @Override getIconWithoutBackground()94 public Drawable getIconWithoutBackground() { 95 return mContext.getDrawable(getDrawableResId()); 96 } 97 98 @VisibleForTesting getDrawableResId()99 int getDrawableResId() { 100 int resId; 101 switch (mRouteInfo.getType()) { 102 case TYPE_USB_DEVICE: 103 case TYPE_USB_HEADSET: 104 case TYPE_USB_ACCESSORY: 105 case TYPE_DOCK: 106 case TYPE_HDMI: 107 case TYPE_WIRED_HEADSET: 108 case TYPE_WIRED_HEADPHONES: 109 resId = R.drawable.ic_headphone; 110 break; 111 case TYPE_BUILTIN_SPEAKER: 112 default: 113 resId = R.drawable.ic_smartphone; 114 break; 115 } 116 return resId; 117 } 118 119 @Override getId()120 public String getId() { 121 String id; 122 switch (mRouteInfo.getType()) { 123 case TYPE_WIRED_HEADSET: 124 case TYPE_WIRED_HEADPHONES: 125 id = WIRED_HEADSET_ID; 126 break; 127 case TYPE_USB_DEVICE: 128 case TYPE_USB_HEADSET: 129 case TYPE_USB_ACCESSORY: 130 case TYPE_DOCK: 131 case TYPE_HDMI: 132 id = USB_HEADSET_ID; 133 break; 134 case TYPE_BUILTIN_SPEAKER: 135 default: 136 id = PHONE_ID; 137 break; 138 } 139 return id; 140 } 141 142 @Override isConnected()143 public boolean isConnected() { 144 return true; 145 } 146 147 /** 148 * According current active device is {@link PhoneMediaDevice} or not to update summary. 149 */ updateSummary(boolean isActive)150 public void updateSummary(boolean isActive) { 151 mSummary = isActive 152 ? mContext.getString(R.string.bluetooth_active_no_battery_level) 153 : ""; 154 } 155 } 156