blob: 2711e3175957766b2c6e242f8dd83c4fa9efed8f [file] [log] [blame]
hughchenb89929a2018-11-12 15:44:39 +08001/*
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 */
16package com.android.settingslib.media;
17
timhypenga8b826c2018-11-14 15:33:53 +080018import android.bluetooth.BluetoothClass;
hughchenc0c11632019-03-07 16:21:17 +080019import android.bluetooth.BluetoothDevice;
hughchenb89929a2018-11-12 15:44:39 +080020import android.content.Context;
hughchen7e853a42019-03-14 15:56:43 +080021import android.graphics.drawable.Drawable;
hughchenb89929a2018-11-12 15:44:39 +080022import android.util.Log;
hughchen7e853a42019-03-14 15:56:43 +080023import android.util.Pair;
hughchenb89929a2018-11-12 15:44:39 +080024
hughchen7e853a42019-03-14 15:56:43 +080025import com.android.settingslib.bluetooth.BluetoothUtils;
hughchenb89929a2018-11-12 15:44:39 +080026import com.android.settingslib.bluetooth.CachedBluetoothDevice;
27
28/**
29 * BluetoothMediaDevice extends MediaDevice to represents Bluetooth device.
30 */
31public class BluetoothMediaDevice extends MediaDevice {
32
33 private static final String TAG = "BluetoothMediaDevice";
34
35 private CachedBluetoothDevice mCachedDevice;
36
37 BluetoothMediaDevice(Context context, CachedBluetoothDevice device) {
38 super(context, MediaDeviceType.TYPE_BLUETOOTH_DEVICE);
39 mCachedDevice = device;
timhypenga8b826c2018-11-14 15:33:53 +080040 initDeviceRecord();
hughchenb89929a2018-11-12 15:44:39 +080041 }
42
43 @Override
44 public String getName() {
45 return mCachedDevice.getName();
46 }
47
48 @Override
hughchenc0c11632019-03-07 16:21:17 +080049 public String getSummary() {
50 return mCachedDevice.getConnectionSummary();
51 }
52
53 @Override
hughchen7e853a42019-03-14 15:56:43 +080054 public Drawable getIcon() {
55 final Pair<Drawable, String> pair = BluetoothUtils
56 .getBtRainbowDrawableWithDescription(mContext, mCachedDevice);
57 return pair.first;
hughchenb89929a2018-11-12 15:44:39 +080058 }
59
60 @Override
61 public String getId() {
62 return MediaDeviceUtils.getId(mCachedDevice);
63 }
64
65 @Override
hughchenbd0dd492019-01-08 14:34:10 +080066 public boolean connect() {
hughchenb89929a2018-11-12 15:44:39 +080067 //TODO(b/117129183): add callback to notify LocalMediaManager connection state.
hughchenbd0dd492019-01-08 14:34:10 +080068 final boolean isConnected = mCachedDevice.setActive();
69 setConnectedRecord();
70 Log.d(TAG, "connect() device : " + getName() + ", is selected : " + isConnected);
71 return isConnected;
hughchenb89929a2018-11-12 15:44:39 +080072 }
73
74 @Override
75 public void disconnect() {
76 //TODO(b/117129183): disconnected last select device
hughchenb89929a2018-11-12 15:44:39 +080077 }
78
79 /**
80 * Get current CachedBluetoothDevice
81 */
82 public CachedBluetoothDevice getCachedDevice() {
83 return mCachedDevice;
84 }
timhypenga8b826c2018-11-14 15:33:53 +080085
86 @Override
87 protected boolean isCarKitDevice() {
88 final BluetoothClass bluetoothClass = mCachedDevice.getDevice().getBluetoothClass();
89 if (bluetoothClass != null) {
90 switch (bluetoothClass.getDeviceClass()) {
91 // Both are common CarKit class
92 case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE:
93 case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO:
94 return true;
95 }
96 }
97 return false;
98 }
hughchenc0c11632019-03-07 16:21:17 +080099
100 @Override
101 public boolean isConnected() {
102 return mCachedDevice.getBondState() == BluetoothDevice.BOND_BONDED
103 && mCachedDevice.isConnected();
104 }
hughchenb89929a2018-11-12 15:44:39 +0800105}