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