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.home; 18 19 import android.annotation.DrawableRes; 20 import android.bluetooth.BluetoothAdapter; 21 import android.bluetooth.BluetoothManager; 22 import android.content.Context; 23 24 import com.android.car.settings.R; 25 import com.android.car.settings.bluetooth.BluetoothSettingsFragment; 26 import com.android.car.settings.common.BaseFragment; 27 import com.android.car.settings.common.IconToggleLineItem; 28 29 30 /** 31 * Represents the Bluetooth line item on settings home page. 32 */ 33 public class BluetoothLineItem extends IconToggleLineItem { 34 private BluetoothAdapter mBluetoothAdapter; 35 private BaseFragment.FragmentController mFragmentController; 36 BluetoothLineItem(Context context, BaseFragment.FragmentController fragmentController)37 public BluetoothLineItem(Context context, BaseFragment.FragmentController fragmentController) { 38 super(context.getText(R.string.bluetooth_settings), context); 39 mFragmentController = fragmentController; 40 mBluetoothAdapter = 41 ((BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE)) 42 .getAdapter(); 43 } 44 45 @Override onToggleClicked(boolean isChecked)46 public void onToggleClicked(boolean isChecked) { 47 if (isChecked) { 48 mBluetoothAdapter.enable(); 49 } else { 50 mBluetoothAdapter.disable(); 51 } 52 } 53 54 @Override isExpandable()55 public boolean isExpandable() { 56 return false; 57 } 58 59 @Override isClickable()60 public boolean isClickable() { 61 return true; 62 } 63 64 @Override onClicked()65 public void onClicked() { 66 mFragmentController.launchFragment(BluetoothSettingsFragment.getInstance()); 67 } 68 69 @Override getDesc()70 public CharSequence getDesc() { 71 return mContext.getText(R.string.bluetooth_settings_summary); 72 } 73 74 @Override isChecked()75 public boolean isChecked() { 76 return mBluetoothAdapter.isEnabled(); 77 } 78 79 @Override getIcon()80 public @DrawableRes int getIcon() { 81 return getIconRes(mBluetoothAdapter.isEnabled()); 82 } 83 onBluetoothStateChanged(boolean enabled)84 public void onBluetoothStateChanged(boolean enabled) { 85 if (mIconUpdateListener == null) { 86 return; 87 } 88 mIconUpdateListener.onUpdateIcon(getIconRes(enabled)); 89 } 90 getIconRes(boolean enabled)91 private @DrawableRes int getIconRes(boolean enabled) { 92 return enabled 93 ? R.drawable.ic_settings_bluetooth : R.drawable.ic_settings_bluetooth_disabled; 94 } 95 } 96