1 /* 2 * Copyright (C) 2011 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.settings.wifi.p2p; 18 19 import android.content.Context; 20 import android.net.wifi.WifiManager; 21 import android.net.wifi.p2p.WifiP2pDevice; 22 import android.text.TextUtils; 23 import android.widget.ImageView; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceViewHolder; 28 29 import com.android.settings.R; 30 31 public class WifiP2pPeer extends Preference { 32 33 private static final int FIXED_RSSI = 60; 34 private static final int[] STATE_SECURED = {com.android.settingslib.R.attr.state_encrypted}; 35 public WifiP2pDevice device; 36 37 @VisibleForTesting final int mRssi; 38 private ImageView mSignal; 39 40 @VisibleForTesting 41 static final int SIGNAL_LEVELS = 4; 42 WifiP2pPeer(Context context, WifiP2pDevice dev)43 public WifiP2pPeer(Context context, WifiP2pDevice dev) { 44 super(context); 45 device = dev; 46 setWidgetLayoutResource(R.layout.preference_widget_wifi_signal); 47 mRssi = FIXED_RSSI; //TODO: fix 48 if (TextUtils.isEmpty(device.deviceName)) { 49 setTitle(device.deviceAddress); 50 } else { 51 setTitle(device.deviceName); 52 } 53 String[] statusArray = context.getResources().getStringArray(R.array.wifi_p2p_status); 54 setSummary(statusArray[device.status]); 55 } 56 57 @Override onBindViewHolder(PreferenceViewHolder view)58 public void onBindViewHolder(PreferenceViewHolder view) { 59 super.onBindViewHolder(view); 60 mSignal = (ImageView) view.findViewById(R.id.signal); 61 if (mRssi == Integer.MAX_VALUE) { 62 mSignal.setImageDrawable(null); 63 } else { 64 mSignal.setImageResource(R.drawable.wifi_signal); 65 mSignal.setImageState(STATE_SECURED, true); 66 } 67 mSignal.setImageLevel(getLevel()); 68 } 69 70 @Override compareTo(Preference preference)71 public int compareTo(Preference preference) { 72 if (!(preference instanceof WifiP2pPeer)) { 73 return 1; 74 } 75 WifiP2pPeer other = (WifiP2pPeer) preference; 76 77 // devices go in the order of the status 78 if (device.status != other.device.status) { 79 return device.status < other.device.status ? -1 : 1; 80 } 81 82 // Sort by name/address 83 if (device.deviceName != null) { 84 return device.deviceName.compareToIgnoreCase(other.device.deviceName); 85 } 86 87 return device.deviceAddress.compareToIgnoreCase(other.device.deviceAddress); 88 } 89 getLevel()90 int getLevel() { 91 if (mRssi == Integer.MAX_VALUE) { 92 return -1; 93 } 94 return WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS); 95 } 96 } 97