1 /* 2 * Copyright (C) 2021 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; 18 19 import static android.net.NetworkCapabilitiesProto.NET_CAPABILITY_NOT_RESTRICTED; 20 21 import android.content.Context; 22 import android.graphics.drawable.Drawable; 23 import android.graphics.drawable.StateListDrawable; 24 25 import androidx.annotation.Nullable; 26 27 import com.android.car.settings.R; 28 import com.android.car.settings.common.Logger; 29 import com.android.car.settings.wifi.details.WifiInfoProvider; 30 import com.android.car.ui.preference.CarUiTwoActionIconPreference; 31 import com.android.wifitrackerlib.WifiEntry; 32 33 /** Renders a {@link WifiEntry} as a preference. */ 34 public class WifiEntryPreference extends CarUiTwoActionIconPreference 35 implements WifiEntry.WifiEntryCallback{ 36 private static final Logger LOG = new Logger(WifiEntryPreference.class); 37 private static final int[] STATE_SECURED = { 38 com.android.settingslib.R.attr.state_encrypted 39 }; 40 private static final int[] STATE_NONE = {}; 41 private static final int[] sWifiSignalAttributes = {com.android.settingslib.R.attr.wifi_signal}; 42 private final Drawable mRestrictedSignalDrawable; 43 44 private final WifiEntry mWifiEntry; 45 @Nullable 46 private final StateListDrawable mWifiSld; 47 private final WifiInfoProvider mWifiInfoProvider; 48 WifiEntryPreference(Context context, WifiEntry wifiEntry)49 public WifiEntryPreference(Context context, WifiEntry wifiEntry) { 50 super(context); 51 LOG.d("creating preference for: " + wifiEntry); 52 mWifiSld = (StateListDrawable) context.getTheme() 53 .obtainStyledAttributes(sWifiSignalAttributes).getDrawable(0); 54 if (mWifiSld != null) { 55 mWifiSld.mutate(); 56 } 57 mWifiEntry = wifiEntry; 58 mWifiEntry.setListener(this); 59 mRestrictedSignalDrawable = context.getDrawable(R.drawable.restricted_wifi_signal); 60 mWifiInfoProvider = new WifiInfoProvider(context, wifiEntry); 61 setKey(wifiEntry.getKey()); 62 setSecondaryActionVisible(false); 63 setShowChevron(false); 64 refresh(); 65 } 66 67 /** 68 * Returns the {@link WifiEntry} that is represented by this preference. 69 */ getWifiEntry()70 public WifiEntry getWifiEntry() { 71 return mWifiEntry; 72 } 73 74 @Override onUpdated()75 public void onUpdated() { 76 refresh(); 77 } 78 refresh()79 private void refresh() { 80 setTitle(mWifiEntry.getTitle()); 81 setSummary(mWifiEntry.getSummary(/* concise= */ false)); 82 setIcon(getWifiEntryIcon()); 83 } 84 getWifiEntryIcon()85 private Drawable getWifiEntryIcon() { 86 if (mWifiEntry.getConnectedState() == WifiEntry.CONNECTED_STATE_CONNECTED 87 && mWifiInfoProvider.getNetworkCapabilities() != null 88 && !mWifiInfoProvider.getNetworkCapabilities() 89 .hasCapability(NET_CAPABILITY_NOT_RESTRICTED)) { 90 mRestrictedSignalDrawable.setLevel(mWifiEntry.getLevel()); 91 return mRestrictedSignalDrawable; 92 } 93 94 if (mWifiSld == null) { 95 LOG.w("wifiSld is null."); 96 return null; 97 } 98 mWifiSld.setState( 99 WifiUtil.isOpenNetwork(mWifiEntry.getSecurity()) 100 ? STATE_NONE 101 : STATE_SECURED); 102 Drawable drawable = mWifiSld.getCurrent(); 103 drawable.setLevel(mWifiEntry.getLevel()); 104 return drawable; 105 } 106 } 107