1 /* 2 * Copyright (C) 2020 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.wifi.details; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.content.Context; 21 import android.graphics.drawable.Drawable; 22 import android.graphics.drawable.StateListDrawable; 23 import android.net.Network; 24 import android.net.NetworkCapabilities; 25 import android.text.TextUtils; 26 27 import com.android.car.settings.R; 28 import com.android.car.settings.common.EntityHeaderPreference; 29 import com.android.car.settings.common.FragmentController; 30 import com.android.car.settings.wifi.WifiUtil; 31 32 /** 33 * Shows Wi-Fi network header with icon, SSID, and Wi-Fi entry summary. 34 */ 35 public class WifiDetailsHeaderPreferenceController extends 36 WifiDetailsBasePreferenceController<EntityHeaderPreference> { 37 38 private static final int[] STATE_SECURED = {com.android.settingslib.R.attr.state_encrypted}; 39 private static final int[] STATE_NONE = {}; 40 private static final int[] sWifiSignalAttributes = {com.android.settingslib.R.attr.wifi_signal}; 41 private final StateListDrawable mWifiSld; 42 private final String mSummaryPlaceholder; 43 WifiDetailsHeaderPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)44 public WifiDetailsHeaderPreferenceController(Context context, 45 String preferenceKey, FragmentController fragmentController, 46 CarUxRestrictions uxRestrictions) { 47 super(context, preferenceKey, fragmentController, uxRestrictions); 48 mWifiSld = (StateListDrawable) context.getTheme() 49 .obtainStyledAttributes(sWifiSignalAttributes).getDrawable(0); 50 mSummaryPlaceholder = context.getString(R.string.empty_placeholder); 51 } 52 53 @Override getPreferenceType()54 protected Class<EntityHeaderPreference> getPreferenceType() { 55 return EntityHeaderPreference.class; 56 } 57 58 @Override updateState(EntityHeaderPreference preference)59 protected void updateState(EntityHeaderPreference preference) { 60 preference.setTitle(getWifiEntry().getSsid()); 61 preference.setIcon(getWifiEntryIcon()); 62 String summary = getWifiEntry().getSummary(/* concise= */ false); 63 if (TextUtils.isEmpty(summary)) { 64 // If the summary is currently empty, use the placeholder string to prevent 65 // vertical shifting during the initial render. 66 summary = mSummaryPlaceholder; 67 } 68 preference.setSummary(summary); 69 } 70 71 @Override getDefaultAvailabilityStatus()72 protected int getDefaultAvailabilityStatus() { 73 if (!WifiUtil.isWifiAvailable(getContext())) { 74 return UNSUPPORTED_ON_DEVICE; 75 } 76 return AVAILABLE; 77 } 78 79 @Override onCapabilitiesChanged(Network network, NetworkCapabilities nc)80 public void onCapabilitiesChanged(Network network, NetworkCapabilities nc) { 81 refreshUi(); 82 } 83 getWifiEntryIcon()84 private Drawable getWifiEntryIcon() { 85 if (mWifiSld == null) { 86 return null; 87 } 88 mWifiSld.setState( 89 WifiUtil.isOpenNetwork(getWifiEntry().getSecurity()) 90 ? STATE_NONE 91 : STATE_SECURED); 92 Drawable drawable = mWifiSld.getCurrent(); 93 drawable.setLevel(getWifiEntry().getLevel()); 94 return drawable; 95 } 96 } 97