1 /*
2  * Copyright (C) 2019 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.network;
18 
19 import android.bluetooth.BluetoothAdapter;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.net.ConnectivityManager;
25 import android.text.TextUtils;
26 
27 import androidx.lifecycle.Lifecycle;
28 import androidx.lifecycle.OnLifecycleEvent;
29 
30 import com.google.common.annotations.VisibleForTesting;
31 
32 /**
33  * This controller helps to manage the switch state and visibility of bluetooth tether switch
34  * preference.
35  */
36 public final class BluetoothTetherPreferenceController extends TetherBasePreferenceController {
37     private int mBluetoothState;
38 
BluetoothTetherPreferenceController(Context context, String preferenceKey)39     public BluetoothTetherPreferenceController(Context context, String preferenceKey) {
40         super(context, preferenceKey);
41     }
42 
43     @OnLifecycleEvent(Lifecycle.Event.ON_START)
onStart()44     public void onStart() {
45         mBluetoothState = BluetoothAdapter.getDefaultAdapter().getState();
46         mContext.registerReceiver(mBluetoothChangeReceiver,
47                 new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
48     }
49 
50     @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
onStop()51     public void onStop() {
52         mContext.unregisterReceiver(mBluetoothChangeReceiver);
53     }
54 
55     @Override
shouldEnable()56     public boolean shouldEnable() {
57         switch (mBluetoothState) {
58             case BluetoothAdapter.STATE_ON:
59             case BluetoothAdapter.STATE_OFF:
60                 // fall through.
61             case BluetoothAdapter.ERROR:
62                 return true;
63             case BluetoothAdapter.STATE_TURNING_OFF:
64             case BluetoothAdapter.STATE_TURNING_ON:
65                 // fall through.
66             default:
67                 return false;
68         }
69     }
70 
71     @Override
shouldShow()72     public boolean shouldShow() {
73         final String[] bluetoothRegexs = mCm.getTetherableBluetoothRegexs();
74         return bluetoothRegexs != null && bluetoothRegexs.length != 0;
75     }
76 
77     @Override
getTetherType()78     public int getTetherType() {
79         return ConnectivityManager.TETHERING_BLUETOOTH;
80     }
81 
82     @VisibleForTesting
83     final BroadcastReceiver mBluetoothChangeReceiver = new BroadcastReceiver() {
84         @Override
85         public void onReceive(Context context, Intent intent) {
86             if (TextUtils.equals(BluetoothAdapter.ACTION_STATE_CHANGED, intent.getAction())) {
87                 mBluetoothState =
88                         intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
89                 updateState(mPreference);
90             }
91         }
92     };
93 }
94