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 android.bluetooth.BluetoothClass;
19 import android.bluetooth.BluetoothDevice;
20 import android.content.Context;
21 import android.graphics.drawable.BitmapDrawable;
22 import android.graphics.drawable.Drawable;
23 import android.media.MediaRoute2Info;
24 import android.media.MediaRouter2Manager;
25 
26 import com.android.settingslib.R;
27 import com.android.settingslib.bluetooth.BluetoothUtils;
28 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
29 
30 /**
31  * BluetoothMediaDevice extends MediaDevice to represents Bluetooth device.
32  */
33 public class BluetoothMediaDevice extends MediaDevice {
34 
35     private static final String TAG = "BluetoothMediaDevice";
36 
37     private CachedBluetoothDevice mCachedDevice;
38 
BluetoothMediaDevice(Context context, CachedBluetoothDevice device, MediaRouter2Manager routerManager, MediaRoute2Info info, String packageName)39     BluetoothMediaDevice(Context context, CachedBluetoothDevice device,
40             MediaRouter2Manager routerManager, MediaRoute2Info info, String packageName) {
41         super(context, routerManager, info, packageName);
42         mCachedDevice = device;
43         initDeviceRecord();
44     }
45 
46     @Override
getName()47     public String getName() {
48         return mCachedDevice.getName();
49     }
50 
51     @Override
getSummary()52     public String getSummary() {
53         return isConnected() || mCachedDevice.isBusy()
54                 ? mCachedDevice.getConnectionSummary()
55                 : mContext.getString(R.string.bluetooth_disconnected);
56     }
57 
58     @Override
getIcon()59     public Drawable getIcon() {
60         final Drawable drawable =
61                 BluetoothUtils.getBtDrawableWithDescription(mContext, mCachedDevice).first;
62         if (!(drawable instanceof BitmapDrawable)) {
63             setColorFilter(drawable);
64         }
65         return BluetoothUtils.buildAdvancedDrawable(mContext, drawable);
66     }
67 
68     @Override
getIconWithoutBackground()69     public Drawable getIconWithoutBackground() {
70         return BluetoothUtils.getBtClassDrawableWithDescription(mContext, mCachedDevice).first;
71     }
72 
73     @Override
getId()74     public String getId() {
75         return MediaDeviceUtils.getId(mCachedDevice);
76     }
77 
78     /**
79      * Get current CachedBluetoothDevice
80      */
getCachedDevice()81     public CachedBluetoothDevice getCachedDevice() {
82         return mCachedDevice;
83     }
84 
85     @Override
isCarKitDevice()86     protected boolean isCarKitDevice() {
87         final BluetoothClass bluetoothClass = mCachedDevice.getDevice().getBluetoothClass();
88         if (bluetoothClass != null) {
89             switch (bluetoothClass.getDeviceClass()) {
90                 // Both are common CarKit class
91                 case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE:
92                 case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO:
93                     return true;
94             }
95         }
96         return false;
97     }
98 
99     @Override
isFastPairDevice()100     public boolean isFastPairDevice() {
101         return mCachedDevice != null
102                 && BluetoothUtils.getBooleanMetaData(
103                 mCachedDevice.getDevice(), BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET);
104     }
105 
106     @Override
isConnected()107     public boolean isConnected() {
108         return mCachedDevice.getBondState() == BluetoothDevice.BOND_BONDED
109                 && mCachedDevice.isConnected();
110     }
111 }
112