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