hughchen | b89929a | 2018-11-12 15:44:39 +0800 | [diff] [blame] | 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 | |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 18 | import android.bluetooth.BluetoothClass; |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 19 | import android.bluetooth.BluetoothDevice; |
hughchen | b89929a | 2018-11-12 15:44:39 +0800 | [diff] [blame] | 20 | import android.content.Context; |
hughchen | 7e853a4 | 2019-03-14 15:56:43 +0800 | [diff] [blame] | 21 | import android.graphics.drawable.Drawable; |
hughchen | b89929a | 2018-11-12 15:44:39 +0800 | [diff] [blame] | 22 | import android.util.Log; |
hughchen | 7e853a4 | 2019-03-14 15:56:43 +0800 | [diff] [blame] | 23 | import android.util.Pair; |
hughchen | b89929a | 2018-11-12 15:44:39 +0800 | [diff] [blame] | 24 | |
hughchen | 7e853a4 | 2019-03-14 15:56:43 +0800 | [diff] [blame] | 25 | import com.android.settingslib.bluetooth.BluetoothUtils; |
hughchen | b89929a | 2018-11-12 15:44:39 +0800 | [diff] [blame] | 26 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
| 27 | |
| 28 | /** |
| 29 | * BluetoothMediaDevice extends MediaDevice to represents Bluetooth device. |
| 30 | */ |
| 31 | public 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; |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 40 | initDeviceRecord(); |
hughchen | b89929a | 2018-11-12 15:44:39 +0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public String getName() { |
| 45 | return mCachedDevice.getName(); |
| 46 | } |
| 47 | |
| 48 | @Override |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 49 | public String getSummary() { |
| 50 | return mCachedDevice.getConnectionSummary(); |
| 51 | } |
| 52 | |
| 53 | @Override |
hughchen | 7e853a4 | 2019-03-14 15:56:43 +0800 | [diff] [blame] | 54 | public Drawable getIcon() { |
| 55 | final Pair<Drawable, String> pair = BluetoothUtils |
| 56 | .getBtRainbowDrawableWithDescription(mContext, mCachedDevice); |
| 57 | return pair.first; |
hughchen | b89929a | 2018-11-12 15:44:39 +0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public String getId() { |
| 62 | return MediaDeviceUtils.getId(mCachedDevice); |
| 63 | } |
| 64 | |
| 65 | @Override |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 66 | public boolean connect() { |
hughchen | b89929a | 2018-11-12 15:44:39 +0800 | [diff] [blame] | 67 | //TODO(b/117129183): add callback to notify LocalMediaManager connection state. |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 68 | final boolean isConnected = mCachedDevice.setActive(); |
| 69 | setConnectedRecord(); |
| 70 | Log.d(TAG, "connect() device : " + getName() + ", is selected : " + isConnected); |
| 71 | return isConnected; |
hughchen | b89929a | 2018-11-12 15:44:39 +0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void disconnect() { |
| 76 | //TODO(b/117129183): disconnected last select device |
hughchen | b89929a | 2018-11-12 15:44:39 +0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get current CachedBluetoothDevice |
| 81 | */ |
| 82 | public CachedBluetoothDevice getCachedDevice() { |
| 83 | return mCachedDevice; |
| 84 | } |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 85 | |
| 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 | } |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 99 | |
| 100 | @Override |
| 101 | public boolean isConnected() { |
| 102 | return mCachedDevice.getBondState() == BluetoothDevice.BOND_BONDED |
| 103 | && mCachedDevice.isConnected(); |
| 104 | } |
hughchen | b89929a | 2018-11-12 15:44:39 +0800 | [diff] [blame] | 105 | } |