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 package com.android.car.settings.bluetooth;
     17 
     18 import android.bluetooth.BluetoothDevice;
     19 import android.os.Bundle;
     20 import android.util.Log;
     21 import android.view.View;
     22 import android.widget.TextView;
     23 
     24 import com.android.car.settings.R;
     25 import com.android.car.settings.common.EditTextLineItem;
     26 import com.android.car.settings.common.ListSettingsFragment;
     27 import com.android.car.settings.common.SingleTextLineItem;
     28 import com.android.car.settings.common.TypedPagedListAdapter;
     29 import com.android.car.view.PagedListView;
     30 
     31 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
     32 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
     33 import com.android.settingslib.bluetooth.LocalBluetoothManager;
     34 import com.android.settingslib.bluetooth.LocalBluetoothProfile;
     35 import com.android.settingslib.bluetooth.MapClientProfile;
     36 import com.android.settingslib.bluetooth.PanProfile;
     37 import com.android.settingslib.bluetooth.PbapClientProfile;
     38 
     39 import java.util.ArrayList;
     40 
     41 /**
     42  * Shows details about a bluetooth device, including actions related to the device,
     43  * e.g. forget etc. The intent should include information about the device, use that to
     44  * render UI, e.g. show name etc.
     45  */
     46 public class BluetoothDetailFragment extends ListSettingsFragment implements
     47         BluetoothProfileLineItem.DataChangedListener {
     48     private static final String TAG = "BluetoothDetailFragment";
     49 
     50     public static final String EXTRA_BT_DEVICE = "extra_bt_device";
     51 
     52     private BluetoothDevice mDevice;
     53     private CachedBluetoothDevice mCachedDevice;
     54 
     55     private CachedBluetoothDeviceManager mDeviceManager;
     56     private LocalBluetoothManager mLocalManager;
     57     private EditTextLineItem mInputLineItem;
     58     private TextView mOkButton;
     59 
     60     public static BluetoothDetailFragment getInstance(BluetoothDevice btDevice) {
     61         BluetoothDetailFragment bluetoothDetailFragment = new BluetoothDetailFragment();
     62         Bundle bundle = ListSettingsFragment.getBundle();
     63         bundle.putParcelable(EXTRA_BT_DEVICE, btDevice);
     64         bundle.putInt(EXTRA_TITLE_ID, R.string.bluetooth_settings);
     65         bundle.putInt(EXTRA_ACTION_BAR_LAYOUT, R.layout.action_bar_with_button);
     66         bluetoothDetailFragment.setArguments(bundle);
     67         return bluetoothDetailFragment;
     68     }
     69 
     70     @Override
     71     public void onCreate(Bundle savedInstanceState) {
     72         super.onCreate(savedInstanceState);
     73         mDevice = getArguments().getParcelable(EXTRA_BT_DEVICE);
     74         mLocalManager = LocalBluetoothManager.getInstance(getContext(), null /* listener */);
     75         if (mLocalManager == null) {
     76             Log.e(TAG, "Bluetooth is not supported on this device");
     77             return;
     78         }
     79         mDeviceManager = mLocalManager.getCachedDeviceManager();
     80         mCachedDevice = mDeviceManager.findDevice(mDevice);
     81         if (mCachedDevice == null) {
     82             mCachedDevice = mDeviceManager.addDevice(
     83                     mLocalManager.getBluetoothAdapter(),
     84                     mLocalManager.getProfileManager(),
     85                     mDevice);
     86         }
     87     }
     88 
     89     @Override
     90     public void onActivityCreated(Bundle savedInstanceState) {
     91         if (mDevice == null) {
     92             Log.w(TAG, "No bluetooth device set.");
     93             return;
     94         }
     95         super.onActivityCreated(savedInstanceState);
     96 
     97         setupForgetButton();
     98         setupOkButton();
     99     }
    100 
    101     @Override
    102     public void onDataChanged() {
    103         mPagedListAdapter.notifyDataSetChanged();
    104     }
    105 
    106     @Override
    107     public ArrayList<TypedPagedListAdapter.LineItem> getLineItems() {
    108         ArrayList<TypedPagedListAdapter.LineItem> lineItems = new ArrayList<>();
    109         mInputLineItem = new EditTextLineItem(
    110                 getContext().getText(R.string.bluetooth_preference_paired_dialog_name_label),
    111                 mCachedDevice.getName());
    112         mInputLineItem.setTextType(EditTextLineItem.TextType.TEXT);
    113         lineItems.add(mInputLineItem);
    114         lineItems.add(new SingleTextLineItem(getContext().getText(
    115                 R.string.bluetooth_device_advanced_profile_header_title)));
    116         addProfileLineItems(lineItems);
    117         return lineItems;
    118     }
    119 
    120     private void addProfileLineItems(ArrayList<TypedPagedListAdapter.LineItem> lineItems) {
    121         for (LocalBluetoothProfile profile : mCachedDevice.getConnectableProfiles()) {
    122             lineItems.add(new BluetoothProfileLineItem(
    123                     getContext(), profile, mCachedDevice, this));
    124         }
    125     }
    126 
    127     private void setupForgetButton() {
    128         TextView fortgetButton = getActivity().findViewById(R.id.action_button2);
    129         fortgetButton.setVisibility(View.VISIBLE);
    130         fortgetButton.setText(R.string.forget);
    131         fortgetButton.setOnClickListener(v -> {
    132             mCachedDevice.unpair();
    133             mFragmentController.goBack();
    134         });
    135     }
    136 
    137     private void setupOkButton() {
    138         mOkButton = getActivity().findViewById(R.id.action_button1);
    139         mOkButton.setText(R.string.okay);
    140         mOkButton.setOnClickListener(v -> {
    141             if (!mInputLineItem.getInput().equals(mCachedDevice.getName())) {
    142                 mCachedDevice.setName(mInputLineItem.getInput());
    143             }
    144             mFragmentController.goBack();
    145         });
    146     }
    147 }
    148