Home | History | Annotate | Download | only in deviceinfo
      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.settingslib.deviceinfo;
     18 
     19 import android.annotation.SuppressLint;
     20 import android.bluetooth.BluetoothAdapter;
     21 import android.content.Context;
     22 import android.text.TextUtils;
     23 
     24 import androidx.annotation.VisibleForTesting;
     25 import androidx.preference.Preference;
     26 import androidx.preference.PreferenceScreen;
     27 
     28 import com.android.settingslib.R;
     29 import com.android.settingslib.core.lifecycle.Lifecycle;
     30 
     31 /**
     32  * Preference controller for bluetooth address
     33  */
     34 public abstract class AbstractBluetoothAddressPreferenceController
     35         extends AbstractConnectivityPreferenceController {
     36 
     37     @VisibleForTesting
     38     static final String KEY_BT_ADDRESS = "bt_address";
     39 
     40     private static final String[] CONNECTIVITY_INTENTS = {
     41             BluetoothAdapter.ACTION_STATE_CHANGED
     42     };
     43 
     44     private Preference mBtAddress;
     45 
     46     public AbstractBluetoothAddressPreferenceController(Context context, Lifecycle lifecycle) {
     47         super(context, lifecycle);
     48     }
     49 
     50     @Override
     51     public boolean isAvailable() {
     52         return BluetoothAdapter.getDefaultAdapter() != null;
     53     }
     54 
     55     @Override
     56     public String getPreferenceKey() {
     57         return KEY_BT_ADDRESS;
     58     }
     59 
     60     @Override
     61     public void displayPreference(PreferenceScreen screen) {
     62         super.displayPreference(screen);
     63         mBtAddress = screen.findPreference(KEY_BT_ADDRESS);
     64         updateConnectivity();
     65     }
     66 
     67     @Override
     68     protected String[] getConnectivityIntents() {
     69         return CONNECTIVITY_INTENTS;
     70     }
     71 
     72     @SuppressLint("HardwareIds")
     73     @Override
     74     protected void updateConnectivity() {
     75         BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
     76         if (bluetooth != null && mBtAddress != null) {
     77             String address = bluetooth.isEnabled() ? bluetooth.getAddress() : null;
     78             if (!TextUtils.isEmpty(address)) {
     79                 // Convert the address to lowercase for consistency with the wifi MAC address.
     80                 mBtAddress.setSummary(address.toLowerCase());
     81             } else {
     82                 mBtAddress.setSummary(R.string.status_unavailable);
     83             }
     84         }
     85     }
     86 }
     87