• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.bluetooth;
18 
19 import android.content.Context;
20 import android.graphics.drawable.Drawable;
21 import android.support.v14.preference.PreferenceFragment;
22 import android.support.v7.preference.PreferenceScreen;
23 import android.util.Pair;
24 
25 import com.android.internal.annotations.VisibleForTesting;
26 import com.android.settings.R;
27 import com.android.settings.applications.LayoutPreference;
28 import com.android.settings.widget.EntityHeaderController;
29 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
31 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
32 import com.android.settingslib.bluetooth.LocalBluetoothManager;
33 
34 /**
35  * This class adds a header with device name and status (connected/disconnected, etc.).
36  */
37 public class BluetoothDetailsHeaderController extends BluetoothDetailsController {
38     private static final String KEY_DEVICE_HEADER = "bluetooth_device_header";
39 
40     private EntityHeaderController mHeaderController;
41     private LocalBluetoothManager mLocalManager;
42     private CachedBluetoothDeviceManager mDeviceManager;
43 
BluetoothDetailsHeaderController(Context context, PreferenceFragment fragment, CachedBluetoothDevice device, Lifecycle lifecycle, LocalBluetoothManager bluetoothManager)44     public BluetoothDetailsHeaderController(Context context, PreferenceFragment fragment,
45             CachedBluetoothDevice device, Lifecycle lifecycle,
46             LocalBluetoothManager bluetoothManager) {
47         super(context, fragment, device, lifecycle);
48         mLocalManager = bluetoothManager;
49         mDeviceManager = mLocalManager.getCachedDeviceManager();
50     }
51 
52     @Override
init(PreferenceScreen screen)53     protected void init(PreferenceScreen screen) {
54         final LayoutPreference headerPreference =
55                 (LayoutPreference) screen.findPreference(KEY_DEVICE_HEADER);
56         mHeaderController = EntityHeaderController.newInstance(mFragment.getActivity(), mFragment,
57                 headerPreference.findViewById(R.id.entity_header));
58         screen.addPreference(headerPreference);
59     }
60 
setHeaderProperties()61     protected void setHeaderProperties() {
62         final Pair<Drawable, String> pair = com.android.settingslib.bluetooth.Utils
63                 .getBtClassDrawableWithDescription(mContext, mCachedDevice,
64                 mContext.getResources().getFraction(R.fraction.bt_battery_scale_fraction, 1, 1));
65         String summaryText = mCachedDevice.getConnectionSummary();
66         // If both the hearing aids are connected, two battery status should be shown.
67         final String pairDeviceSummary = mDeviceManager
68             .getHearingAidPairDeviceSummary(mCachedDevice);
69         if (pairDeviceSummary != null) {
70             mHeaderController.setSecondSummary(pairDeviceSummary);
71         }
72         mHeaderController.setLabel(mCachedDevice.getName());
73         mHeaderController.setIcon(pair.first);
74         mHeaderController.setIconContentDescription(pair.second);
75         mHeaderController.setSummary(summaryText);
76     }
77 
78     @Override
refresh()79     protected void refresh() {
80         setHeaderProperties();
81         mHeaderController.done(mFragment.getActivity(), true /* rebindActions */);
82     }
83 
84     @Override
getPreferenceKey()85     public String getPreferenceKey() {
86         return KEY_DEVICE_HEADER;
87     }
88 }
89