Home | History | Annotate | Download | only in media
      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.Drawable;
     22 import android.util.Log;
     23 import android.util.Pair;
     24 
     25 import com.android.settingslib.R;
     26 import com.android.settingslib.bluetooth.BluetoothUtils;
     27 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
     28 
     29 /**
     30  * BluetoothMediaDevice extends MediaDevice to represents Bluetooth device.
     31  */
     32 public class BluetoothMediaDevice extends MediaDevice {
     33 
     34     private static final String TAG = "BluetoothMediaDevice";
     35 
     36     private CachedBluetoothDevice mCachedDevice;
     37 
     38     BluetoothMediaDevice(Context context, CachedBluetoothDevice device) {
     39         super(context, MediaDeviceType.TYPE_BLUETOOTH_DEVICE);
     40         mCachedDevice = device;
     41         initDeviceRecord();
     42     }
     43 
     44     @Override
     45     public String getName() {
     46         return mCachedDevice.getName();
     47     }
     48 
     49     @Override
     50     public String getSummary() {
     51         return isConnected() || mCachedDevice.isBusy()
     52                 ? mCachedDevice.getConnectionSummary()
     53                 : mContext.getString(R.string.bluetooth_disconnected);
     54     }
     55 
     56     @Override
     57     public Drawable getIcon() {
     58         final Pair<Drawable, String> pair = BluetoothUtils
     59                 .getBtRainbowDrawableWithDescription(mContext, mCachedDevice);
     60         return pair.first;
     61     }
     62 
     63     @Override
     64     public String getId() {
     65         return MediaDeviceUtils.getId(mCachedDevice);
     66     }
     67 
     68     @Override
     69     public boolean connect() {
     70         //TODO(b/117129183): add callback to notify LocalMediaManager connection state.
     71         final boolean isConnected = mCachedDevice.setActive();
     72         setConnectedRecord();
     73         Log.d(TAG, "connect() device : " + getName() + ", is selected : " + isConnected);
     74         return isConnected;
     75     }
     76 
     77     @Override
     78     public void disconnect() {
     79         //TODO(b/117129183): disconnected last select device
     80     }
     81 
     82     /**
     83      * Get current CachedBluetoothDevice
     84      */
     85     public CachedBluetoothDevice getCachedDevice() {
     86         return mCachedDevice;
     87     }
     88 
     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     }
    102 
    103     @Override
    104     public boolean isConnected() {
    105         return mCachedDevice.getBondState() == BluetoothDevice.BOND_BONDED
    106                 && mCachedDevice.isConnected();
    107     }
    108 }
    109