Home | History | Annotate | Download | only in bluetooth
      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.support.v7.widget.RecyclerView;
     23 
     24 import com.android.car.settings.common.ToggleLineItem;
     25 
     26 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
     27 import com.android.settingslib.bluetooth.LocalBluetoothProfile;
     28 import com.android.settingslib.bluetooth.MapProfile;
     29 import com.android.settingslib.bluetooth.PanProfile;
     30 import com.android.settingslib.bluetooth.PbapServerProfile;
     31 
     32 /**
     33  * Represents a line item for a Bluetooth mProfile.
     34  */
     35 public class BluetoothProfileLineItem extends ToggleLineItem {
     36     private final LocalBluetoothProfile mProfile;
     37     private final CachedBluetoothDevice mCachedDevice;
     38     private ToggleLineItemViewHolder 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 void bindViewHolder(ToggleLineItemViewHolder holder) {
     79         super.bindViewHolder(holder);
     80         mViewHolder = holder;
     81     }
     82 
     83     @Override
     84     public CharSequence getDesc() {
     85         return null;
     86     }
     87 
     88     @Override
     89     public boolean isChecked() {
     90         BluetoothDevice device = mCachedDevice.getDevice();
     91 
     92         if (mProfile instanceof MapProfile) {
     93             return mCachedDevice.getMessagePermissionChoice()
     94                     == CachedBluetoothDevice.ACCESS_ALLOWED;
     95 
     96         } else if (mProfile instanceof PbapServerProfile) {
     97             return mCachedDevice.getPhonebookPermissionChoice()
     98                     == CachedBluetoothDevice.ACCESS_ALLOWED;
     99 
    100         } else if (mProfile instanceof PanProfile) {
    101             return mProfile.getConnectionStatus(device) == BluetoothProfile.STATE_CONNECTED;
    102 
    103         } else {
    104             return mProfile.isPreferred(device);
    105         }
    106     }
    107 }
    108