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.settings.R; 26 import com.android.settings.applications.LayoutPreference; 27 import com.android.settings.widget.EntityHeaderController; 28 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 29 import com.android.settingslib.core.lifecycle.Lifecycle; 30 31 /** 32 * This class adds a header with device name and status (connected/disconnected, etc.). 33 */ 34 public class BluetoothDetailsHeaderController extends BluetoothDetailsController { 35 private static final String KEY_DEVICE_HEADER = "bluetooth_device_header"; 36 37 private EntityHeaderController mHeaderController; 38 39 public BluetoothDetailsHeaderController(Context context, PreferenceFragment fragment, 40 CachedBluetoothDevice device, Lifecycle lifecycle) { 41 super(context, fragment, device, lifecycle); 42 } 43 44 @Override 45 protected void init(PreferenceScreen screen) { 46 final LayoutPreference headerPreference = 47 (LayoutPreference) screen.findPreference(KEY_DEVICE_HEADER); 48 mHeaderController = EntityHeaderController.newInstance(mFragment.getActivity(), mFragment, 49 headerPreference.findViewById(R.id.entity_header)); 50 screen.addPreference(headerPreference); 51 } 52 53 protected void setHeaderProperties() { 54 final Pair<Drawable, String> pair = Utils.getBtClassDrawableWithDescription( 55 mContext, mCachedDevice, 56 mContext.getResources().getFraction(R.fraction.bt_battery_scale_fraction, 1, 1)); 57 String summaryText = mCachedDevice.getConnectionSummary(); 58 mHeaderController.setLabel(mCachedDevice.getName()); 59 mHeaderController.setIcon(pair.first); 60 mHeaderController.setIconContentDescription(pair.second); 61 mHeaderController.setSummary(summaryText); 62 } 63 64 @Override 65 protected void refresh() { 66 setHeaderProperties(); 67 mHeaderController.done(mFragment.getActivity(), true /* rebindActions */); 68 } 69 70 @Override 71 public String getPreferenceKey() { 72 return KEY_DEVICE_HEADER; 73 } 74 } 75