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