Jonathan Koo | 1689100 | 2019-02-13 14:01:10 -0800 | [diff] [blame] | 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.car.settings.system; |
| 18 | |
| 19 | import android.car.drivingstate.CarUxRestrictions; |
| 20 | import android.content.Context; |
| 21 | import android.content.pm.PackageManager; |
| 22 | import android.net.wifi.WifiInfo; |
| 23 | import android.net.wifi.WifiManager; |
| 24 | |
| 25 | import androidx.preference.Preference; |
| 26 | |
| 27 | import com.android.car.settings.R; |
| 28 | import com.android.car.settings.common.FragmentController; |
| 29 | import com.android.car.settings.common.PreferenceController; |
| 30 | |
| 31 | /** Updates the vehicle Wi-Fi mac address summary. */ |
| 32 | public class WifiMacAddressPreferenceController extends PreferenceController<Preference> { |
| 33 | |
| 34 | private WifiInfo mWifiInfo; |
| 35 | |
| 36 | public WifiMacAddressPreferenceController(Context context, String preferenceKey, |
| 37 | FragmentController fragmentController, CarUxRestrictions uxRestrictions) { |
| 38 | super(context, preferenceKey, fragmentController, uxRestrictions); |
| 39 | init(context); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | protected Class<Preference> getPreferenceType() { |
| 44 | return Preference.class; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | protected int getAvailabilityStatus() { |
| 49 | if (!getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI) |
| 50 | || !getContext().getResources().getBoolean(R.bool.config_show_wifi_mac_address)) { |
| 51 | return UNSUPPORTED_ON_DEVICE; |
| 52 | } |
| 53 | return super.getAvailabilityStatus(); |
| 54 | } |
| 55 | |
| 56 | protected void init(Context context) { |
| 57 | WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); |
| 58 | mWifiInfo = wifiManager.getConnectionInfo(); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | protected void updateState(Preference preference) { |
| 63 | String macAddress = mWifiInfo == null ? getContext().getString(R.string.status_unavailable) |
| 64 | : mWifiInfo.getMacAddress(); |
| 65 | preference.setSummary(macAddress); |
| 66 | } |
| 67 | } |