blob: c039fcce5fa8c70256536e5a556d5912fcdd300b [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;
19import android.bluetooth.BluetoothProfile;
hughchenb89929a2018-11-12 15:44:39 +080020import android.content.Context;
21import android.util.Log;
22
23import com.android.settingslib.R;
24import com.android.settingslib.bluetooth.CachedBluetoothDevice;
25
26/**
27 * BluetoothMediaDevice extends MediaDevice to represents Bluetooth device.
28 */
29public class BluetoothMediaDevice extends MediaDevice {
30
31 private static final String TAG = "BluetoothMediaDevice";
32
33 private CachedBluetoothDevice mCachedDevice;
34
35 BluetoothMediaDevice(Context context, CachedBluetoothDevice device) {
36 super(context, MediaDeviceType.TYPE_BLUETOOTH_DEVICE);
37 mCachedDevice = device;
timhypenga8b826c2018-11-14 15:33:53 +080038 initDeviceRecord();
39 buildConnectedState(device);
40 }
41
42 private void buildConnectedState(CachedBluetoothDevice device) {
43 mIsConnected = device.isActiveDevice(BluetoothProfile.A2DP)
44 || device.isActiveDevice(BluetoothProfile.HEARING_AID);
hughchenb89929a2018-11-12 15:44:39 +080045 }
46
47 @Override
48 public String getName() {
49 return mCachedDevice.getName();
50 }
51
52 @Override
53 public int getIcon() {
54 //TODO(b/117129183): This is not final icon for bluetooth device, just for demo.
55 return R.drawable.ic_bt_headphones_a2dp;
56 }
57
58 @Override
59 public String getId() {
60 return MediaDeviceUtils.getId(mCachedDevice);
61 }
62
63 @Override
timhypenga8b826c2018-11-14 15:33:53 +080064 public void notifyConnectedChanged() {
65 buildConnectedState(mCachedDevice);
66 }
67
68 @Override
hughchenb89929a2018-11-12 15:44:39 +080069 public void connect() {
70 //TODO(b/117129183): add callback to notify LocalMediaManager connection state.
71 mIsConnected = mCachedDevice.setActive();
timhypenga8b826c2018-11-14 15:33:53 +080072 super.connect();
hughchenb89929a2018-11-12 15:44:39 +080073 Log.d(TAG, "connect() device : " + getName() + ", is selected : " + mIsConnected);
74 }
75
76 @Override
77 public void disconnect() {
78 //TODO(b/117129183): disconnected last select device
79 mIsConnected = false;
80 }
81
82 /**
83 * Get current CachedBluetoothDevice
84 */
85 public CachedBluetoothDevice getCachedDevice() {
86 return mCachedDevice;
87 }
timhypenga8b826c2018-11-14 15:33:53 +080088
89 @Override
90 protected boolean isCarKitDevice() {
91 final BluetoothClass bluetoothClass = mCachedDevice.getDevice().getBluetoothClass();
92 if (bluetoothClass != null) {
93 switch (bluetoothClass.getDeviceClass()) {
94 // Both are common CarKit class
95 case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE:
96 case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO:
97 return true;
98 }
99 }
100 return false;
101 }
hughchenb89929a2018-11-12 15:44:39 +0800102}