1 /* 2 * Copyright (C) 2017 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 17 package com.android.car.settings.bluetooth; 18 19 import android.bluetooth.BluetoothDevice; 20 import android.bluetooth.BluetoothProfile; 21 import android.content.Context; 22 import android.view.View; 23 import android.widget.CheckBox; 24 25 import com.android.car.list.CheckBoxLineItem; 26 import com.android.car.settings.R; 27 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 28 import com.android.settingslib.bluetooth.LocalBluetoothProfile; 29 import com.android.settingslib.bluetooth.PanProfile; 30 31 /** 32 * Represents a line item for a Bluetooth mProfile. 33 */ 34 public class BluetoothProfileLineItem extends CheckBoxLineItem { 35 private final LocalBluetoothProfile mProfile; 36 private final CachedBluetoothDevice mCachedDevice; 37 private CheckboxLineItemViewHolder mViewHolder; 38 private DataChangedListener mDataChangedListener; 39 private final Context mContext; 40 41 public interface DataChangedListener { onDataChanged()42 void onDataChanged(); 43 } 44 BluetoothProfileLineItem(Context context, LocalBluetoothProfile profile, CachedBluetoothDevice cachedBluetoothDevice, DataChangedListener listener)45 public BluetoothProfileLineItem(Context context, LocalBluetoothProfile profile, 46 CachedBluetoothDevice cachedBluetoothDevice, DataChangedListener listener) { 47 super(context.getText(profile.getNameResource(cachedBluetoothDevice.getDevice()))); 48 mContext = context; 49 mCachedDevice = cachedBluetoothDevice; 50 mProfile = profile; 51 mDataChangedListener = listener; 52 } 53 54 @Override onClick(View view)55 public void onClick(View view) { 56 if (((CheckBox) view.findViewById(R.id.checkbox)).isChecked()) { 57 mCachedDevice.disconnect(mProfile); 58 mProfile.setPreferred(mCachedDevice.getDevice(), false); 59 } else if (mProfile.isPreferred(mCachedDevice.getDevice())) { 60 if (mProfile instanceof PanProfile) { 61 mCachedDevice.connectProfile(mProfile); 62 } else { 63 mProfile.setPreferred(mCachedDevice.getDevice(), false); 64 } 65 } else { 66 mProfile.setPreferred(mCachedDevice.getDevice(), true); 67 mCachedDevice.connectProfile(mProfile); 68 } 69 mDataChangedListener.onDataChanged(); 70 } 71 72 @Override isExpandable()73 public boolean isExpandable() { 74 return false; 75 } 76 77 @Override isEnabled()78 public boolean isEnabled() { 79 return true; 80 } 81 82 @Override bindViewHolder(CheckboxLineItemViewHolder holder)83 public void bindViewHolder(CheckboxLineItemViewHolder holder) { 84 super.bindViewHolder(holder); 85 mViewHolder = holder; 86 } 87 88 @Override isChecked()89 public boolean isChecked() { 90 BluetoothDevice device = mCachedDevice.getDevice(); 91 if (mProfile instanceof PanProfile) { 92 return mProfile.getConnectionStatus(device) == BluetoothProfile.STATE_CONNECTED; 93 } else { 94 return mProfile.isPreferred(device); 95 } 96 } 97 } 98