BluetoothProfileLineItem.java revision 664ed2dc2488c50194a80004af759a765f0ce18f
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 17package com.android.car.settings.bluetooth; 18 19import android.bluetooth.BluetoothDevice; 20import android.bluetooth.BluetoothProfile; 21import android.content.Context; 22import android.support.v7.widget.RecyclerView; 23 24import com.android.car.settings.common.CheckBoxLineItem; 25 26import com.android.settingslib.bluetooth.CachedBluetoothDevice; 27import com.android.settingslib.bluetooth.LocalBluetoothProfile; 28import com.android.settingslib.bluetooth.MapProfile; 29import com.android.settingslib.bluetooth.PanProfile; 30import com.android.settingslib.bluetooth.PbapServerProfile; 31 32/** 33 * Represents a line item for a Bluetooth mProfile. 34 */ 35public class BluetoothProfileLineItem extends CheckBoxLineItem { 36 private final LocalBluetoothProfile mProfile; 37 private final CachedBluetoothDevice mCachedDevice; 38 private CheckboxLineItemViewHolder mViewHolder; 39 private DataChangedListener mDataChangedListener; 40 41 public interface DataChangedListener { 42 void onDataChanged(); 43 } 44 45 public BluetoothProfileLineItem(Context context, LocalBluetoothProfile profile, 46 CachedBluetoothDevice cachedBluetoothDevice, DataChangedListener listener) { 47 super(context.getText(profile.getNameResource(cachedBluetoothDevice.getDevice()))); 48 mCachedDevice = cachedBluetoothDevice; 49 mProfile = profile; 50 mDataChangedListener = listener; 51 } 52 53 @Override 54 public void onClick(boolean isChecked) { 55 if (mProfile instanceof PbapServerProfile) { 56 mCachedDevice.setPhonebookPermissionChoice(isChecked 57 ? CachedBluetoothDevice.ACCESS_REJECTED : CachedBluetoothDevice.ACCESS_ALLOWED); 58 } else if (mProfile instanceof MapProfile) { 59 mCachedDevice.setMessagePermissionChoice(isChecked 60 ? CachedBluetoothDevice.ACCESS_REJECTED : CachedBluetoothDevice.ACCESS_ALLOWED); 61 } else if (isChecked) { 62 mCachedDevice.disconnect(mProfile); 63 mProfile.setPreferred(mCachedDevice.getDevice(), false); 64 } else if (mProfile.isPreferred(mCachedDevice.getDevice())) { 65 if (mProfile instanceof PanProfile) { 66 mCachedDevice.connectProfile(mProfile); 67 } else { 68 mProfile.setPreferred(mCachedDevice.getDevice(), false); 69 } 70 } else { 71 mProfile.setPreferred(mCachedDevice.getDevice(), true); 72 mCachedDevice.connectProfile(mProfile); 73 } 74 mDataChangedListener.onDataChanged(); 75 } 76 77 @Override 78 public boolean isExpandable() { 79 return false; 80 } 81 82 @Override 83 public boolean isEnabled() { 84 return true; 85 } 86 87 @Override 88 public void bindViewHolder(CheckboxLineItemViewHolder holder) { 89 super.bindViewHolder(holder); 90 mViewHolder = holder; 91 } 92 93 @Override 94 public boolean isChecked() { 95 BluetoothDevice device = mCachedDevice.getDevice(); 96 97 if (mProfile instanceof MapProfile) { 98 return mCachedDevice.getMessagePermissionChoice() 99 == CachedBluetoothDevice.ACCESS_ALLOWED; 100 } else if (mProfile instanceof PbapServerProfile) { 101 return mCachedDevice.getPhonebookPermissionChoice() 102 == CachedBluetoothDevice.ACCESS_ALLOWED; 103 } else if (mProfile instanceof PanProfile) { 104 return mProfile.getConnectionStatus(device) == BluetoothProfile.STATE_CONNECTED; 105 } else { 106 return mProfile.isPreferred(device); 107 } 108 } 109} 110