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