• 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.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 import android.view.MotionEvent;
24 import android.view.View;
25 import android.widget.Switch;
26 
27 import com.android.car.list.IconToggleLineItem;
28 import com.android.car.settings.R;
29 import com.android.car.settings.bluetooth.BluetoothSettingsFragment;
30 import com.android.car.settings.common.BaseFragment;
31 
32 
33 /**
34  * Represents the Bluetooth line item on settings home page.
35  */
36 public class BluetoothLineItem extends IconToggleLineItem {
37     private BluetoothAdapter mBluetoothAdapter;
38     private BaseFragment.FragmentController mFragmentController;
39 
BluetoothLineItem(Context context, BaseFragment.FragmentController fragmentController)40     public BluetoothLineItem(Context context, BaseFragment.FragmentController fragmentController) {
41         super(context.getText(R.string.bluetooth_settings), context);
42         mFragmentController = fragmentController;
43         mBluetoothAdapter =
44                 ((BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE))
45                         .getAdapter();
46     }
47 
48     @Override
onToggleTouched(Switch toggleSwitch, MotionEvent event)49     public boolean onToggleTouched(Switch toggleSwitch, MotionEvent event) {
50         if (!isBluetoothAvailable()) {
51             return true;
52         }
53         // intercept touch event, so we can process the request and update the switch
54         // state accordingly
55         if (event.getAction() == MotionEvent.ACTION_DOWN) {
56             if (isChecked()) {
57                 mBluetoothAdapter.disable();
58             } else {
59                 mBluetoothAdapter.enable();
60             }
61         }
62         return true;
63     }
64 
65     @Override
isExpandable()66     public boolean isExpandable() {
67         return false;
68     }
69 
70     @Override
isClickable()71     public boolean isClickable() {
72         return isBluetoothAvailable();
73     }
74 
75     @Override
onClick(View view)76     public void onClick(View view) {
77         if (isBluetoothAvailable()) {
78             mFragmentController.launchFragment(BluetoothSettingsFragment.getInstance());
79         }
80     }
81 
82     @Override
getDesc()83     public CharSequence getDesc() {
84         return mContext.getText(R.string.bluetooth_settings_summary);
85     }
86 
87     @Override
isChecked()88     public boolean isChecked() {
89         return isBluetoothAvailable() && mBluetoothAdapter.isEnabled();
90     }
91 
92     @Override
getIcon()93     public @DrawableRes int getIcon() {
94         return getIconRes(isBluetoothAvailable() && mBluetoothAdapter.isEnabled());
95     }
96 
onBluetoothStateChanged(boolean enabled)97     public void onBluetoothStateChanged(boolean enabled) {
98         if (mIconUpdateListener != null) {
99             mIconUpdateListener.onUpdateIcon(getIconRes(enabled));
100         }
101         if (mSwitchStateUpdateListener != null) {
102             mSwitchStateUpdateListener.onToggleChanged(enabled);
103         }
104     }
105 
isBluetoothAvailable()106     private boolean isBluetoothAvailable() {
107         return mBluetoothAdapter != null;
108     }
109 
getIconRes(boolean enabled)110     private @DrawableRes int getIconRes(boolean enabled) {
111         return enabled
112                 ? R.drawable.ic_settings_bluetooth : R.drawable.ic_settings_bluetooth_disabled;
113     }
114 }
115