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