• 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 package com.android.car.settings.bluetooth;
17 
18 import android.bluetooth.BluetoothAdapter;
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.os.Bundle;
22 import android.util.Log;
23 import android.view.View;
24 import android.support.car.ui.PagedListView;
25 import android.support.v7.widget.RecyclerView;
26 import android.widget.ProgressBar;
27 import android.widget.Switch;
28 import android.widget.TextView;
29 import android.widget.ViewSwitcher;
30 
31 import com.android.car.settings.common.CarSettingActivity;
32 import com.android.car.settings.R;
33 
34 import com.android.settingslib.bluetooth.BluetoothCallback;
35 import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
36 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
37 import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
38 import com.android.settingslib.bluetooth.LocalBluetoothManager;
39 
40 /**
41  * Activity to host Bluetooth related preferences.
42  */
43 public class BluetoothSettingsActivity extends CarSettingActivity implements BluetoothCallback {
44     private static final String TAG = "BluetoothSettingsActivity";
45 
46     private Switch mBluetoothSwitch;
47     private ProgressBar mProgressBar;
48     private PagedListView mDeviceListView;
49     private ViewSwitcher mViewSwitcher;
50     private TextView mMessageView;
51     private BluetoothDeviceListAdapter mDeviceAdapter;
52     private LocalBluetoothAdapter mLocalAdapter;
53     private LocalBluetoothManager mLocalManager;
54 
55     @Override
onCreate(Bundle savedInstanceState)56     protected void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58         setContentView(R.layout.bluetooth_list);
59 
60         ((TextView) findViewById(R.id.title)).setText(R.string.bluetooth_settings);
61         mBluetoothSwitch = (Switch) findViewById(R.id.toggle_switch);
62         mBluetoothSwitch.setOnClickListener(v -> {
63                 if (mBluetoothSwitch.isChecked()) {
64                     // bt scan was turned on at state listener, when state is on.
65                     mLocalAdapter.setBluetoothEnabled(true);
66                 } else {
67                     mLocalAdapter.stopScanning();
68                     mLocalAdapter.setBluetoothEnabled(false);
69                 }
70             });
71 
72         mProgressBar = (ProgressBar) findViewById(R.id.bt_search_progress);
73         mDeviceListView = (PagedListView) findViewById(R.id.list);
74         mViewSwitcher = (ViewSwitcher) findViewById(R.id.view_switcher);
75         mMessageView = (TextView) findViewById(R.id.bt_message);
76 
77         mLocalManager = LocalBluetoothManager.getInstance(this /* context */ , null /* listener */);
78         if (mLocalManager == null) {
79             Log.e(TAG, "Bluetooth is not supported on this device");
80             return;
81         }
82         mLocalAdapter = mLocalManager.getBluetoothAdapter();
83 
84         mDeviceListView.setDefaultItemDecoration(new ItemDecoration(this));
85         // Set this to light mode, since the scroll bar buttons always appear
86         // on top of a dark scrim.
87         mDeviceListView.setDarkMode();
88         mDeviceAdapter = new BluetoothDeviceListAdapter(this /* context */ , mLocalManager);
89         mDeviceListView.setAdapter(mDeviceAdapter);
90     }
91 
92     @Override
setupActionBar()93     public void setupActionBar() {
94         super.setupActionBar();
95         getActionBar().setCustomView(R.layout.action_bar_with_toggle);
96         getActionBar().setDisplayShowCustomEnabled(true);
97     }
98 
99     @Override
onStart()100     public void onStart() {
101         super.onStart();
102         if (mLocalManager == null) {
103             return;
104         }
105         mLocalManager.setForegroundActivity(this);
106         mLocalManager.getEventManager().registerCallback(this);
107         mBluetoothSwitch.setChecked(mLocalAdapter.isEnabled());
108         if (mLocalAdapter.isEnabled()) {
109             setProgressBarVisible(true);
110             mLocalAdapter.startScanning(true);
111             if (mViewSwitcher.getCurrentView() != mDeviceListView) {
112                 mViewSwitcher.showPrevious();
113             }
114         } else {
115             setProgressBarVisible(false);
116             if (mViewSwitcher.getCurrentView() != mMessageView) {
117                 mViewSwitcher.showNext();
118             }
119         }
120         mDeviceAdapter.start();
121     }
122 
123     @Override
onStop()124     public void onStop() {
125         super.onStop();
126         if (mLocalManager == null) {
127             return;
128         }
129         mDeviceAdapter.stop();
130         mLocalManager.setForegroundActivity(null);
131         mLocalAdapter.stopScanning();
132         mLocalManager.getEventManager().unregisterCallback(this);
133     }
134 
135     @Override
onBluetoothStateChanged(int bluetoothState)136     public void onBluetoothStateChanged(int bluetoothState) {
137         switch (bluetoothState) {
138             case BluetoothAdapter.STATE_OFF:
139                 setProgressBarVisible(false);
140                 mBluetoothSwitch.setChecked(false);
141                 if (mViewSwitcher.getCurrentView() != mMessageView) {
142                     mViewSwitcher.showNext();
143                 }
144                 break;
145             case BluetoothAdapter.STATE_ON:
146             case BluetoothAdapter.STATE_TURNING_ON:
147                 setProgressBarVisible(true);
148                 mBluetoothSwitch.setChecked(true);
149                 if (mViewSwitcher.getCurrentView() != mDeviceListView) {
150                         mViewSwitcher.showPrevious();
151                 }
152                 break;
153             case BluetoothAdapter.STATE_TURNING_OFF:
154                 setProgressBarVisible(true);
155                 break;
156         }
157     }
158 
159     @Override
onScanningStateChanged(boolean started)160     public void onScanningStateChanged(boolean started) {
161         if (!started) {
162             setProgressBarVisible(false);
163         }
164     }
165 
166     @Override
onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState)167     public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
168         // no-op
169     }
170 
171     @Override
onDeviceAdded(CachedBluetoothDevice cachedDevice)172     public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
173         // no-op
174     }
175 
176     @Override
onDeviceDeleted(CachedBluetoothDevice cachedDevice)177     public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
178         // no-op
179     }
180 
181     @Override
onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state)182     public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
183         // no-op
184     }
185 
setProgressBarVisible(boolean visible)186     private  void setProgressBarVisible(boolean visible) {
187         if (mProgressBar != null) {
188             mProgressBar.setVisibility(visible ? View.VISIBLE : View.GONE);
189         }
190     }
191 
192     /**
193      * Default {@link android.support.car.ui.PagedListView.Decoration} for the {@link PagedListView}
194      * that removes the dividing lines between items.
195      */
196     private static class ItemDecoration extends PagedListView.Decoration {
ItemDecoration(Context context)197         public ItemDecoration(Context context) {
198             super(context);
199         }
200 
201         @Override
onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)202         public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {}
203     }
204 }
205