1b98463f8b0de186dc263217286449a76d6df91a8repo sync/* 2b98463f8b0de186dc263217286449a76d6df91a8repo sync * Copyright (C) 2011 The Android Open Source Project 3b98463f8b0de186dc263217286449a76d6df91a8repo sync * 4b98463f8b0de186dc263217286449a76d6df91a8repo sync * Licensed under the Apache License, Version 2.0 (the "License"); 5b98463f8b0de186dc263217286449a76d6df91a8repo sync * you may not use this file except in compliance with the License. 6b98463f8b0de186dc263217286449a76d6df91a8repo sync * You may obtain a copy of the License at 7b98463f8b0de186dc263217286449a76d6df91a8repo sync * 8b98463f8b0de186dc263217286449a76d6df91a8repo sync * http://www.apache.org/licenses/LICENSE-2.0 9b98463f8b0de186dc263217286449a76d6df91a8repo sync * 10b98463f8b0de186dc263217286449a76d6df91a8repo sync * Unless required by applicable law or agreed to in writing, software 11b98463f8b0de186dc263217286449a76d6df91a8repo sync * distributed under the License is distributed on an "AS IS" BASIS, 12b98463f8b0de186dc263217286449a76d6df91a8repo sync * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b98463f8b0de186dc263217286449a76d6df91a8repo sync * See the License for the specific language governing permissions and 14b98463f8b0de186dc263217286449a76d6df91a8repo sync * limitations under the License. 15b98463f8b0de186dc263217286449a76d6df91a8repo sync */ 16b98463f8b0de186dc263217286449a76d6df91a8repo sync 17b98463f8b0de186dc263217286449a76d6df91a8repo syncpackage com.android.settings.wifi.p2p; 18b98463f8b0de186dc263217286449a76d6df91a8repo sync 19b98463f8b0de186dc263217286449a76d6df91a8repo syncimport com.android.settings.R; 20b98463f8b0de186dc263217286449a76d6df91a8repo sync 21b98463f8b0de186dc263217286449a76d6df91a8repo syncimport android.content.Context; 22b98463f8b0de186dc263217286449a76d6df91a8repo syncimport android.net.wifi.WifiManager; 23b98463f8b0de186dc263217286449a76d6df91a8repo syncimport android.net.wifi.p2p.WifiP2pManager; 24b98463f8b0de186dc263217286449a76d6df91a8repo syncimport android.net.wifi.p2p.WifiP2pDevice; 25b98463f8b0de186dc263217286449a76d6df91a8repo syncimport android.preference.Preference; 26b98463f8b0de186dc263217286449a76d6df91a8repo syncimport android.text.TextUtils; 27b98463f8b0de186dc263217286449a76d6df91a8repo syncimport android.view.View; 28b98463f8b0de186dc263217286449a76d6df91a8repo syncimport android.widget.ImageView; 29b98463f8b0de186dc263217286449a76d6df91a8repo sync 30b98463f8b0de186dc263217286449a76d6df91a8repo syncimport java.util.Comparator; 31b98463f8b0de186dc263217286449a76d6df91a8repo sync 32b98463f8b0de186dc263217286449a76d6df91a8repo syncpublic class WifiP2pPeer extends Preference { 33b98463f8b0de186dc263217286449a76d6df91a8repo sync 34b98463f8b0de186dc263217286449a76d6df91a8repo sync private static final int[] STATE_SECURED = {R.attr.state_encrypted}; 35b98463f8b0de186dc263217286449a76d6df91a8repo sync public WifiP2pDevice device; 36b98463f8b0de186dc263217286449a76d6df91a8repo sync 37b98463f8b0de186dc263217286449a76d6df91a8repo sync private int mRssi; 38b98463f8b0de186dc263217286449a76d6df91a8repo sync private ImageView mSignal; 39b98463f8b0de186dc263217286449a76d6df91a8repo sync 40b98463f8b0de186dc263217286449a76d6df91a8repo sync private static final int SIGNAL_LEVELS = 4; 41b98463f8b0de186dc263217286449a76d6df91a8repo sync 42b98463f8b0de186dc263217286449a76d6df91a8repo sync public WifiP2pPeer(Context context, WifiP2pDevice dev) { 43b98463f8b0de186dc263217286449a76d6df91a8repo sync super(context); 44b98463f8b0de186dc263217286449a76d6df91a8repo sync device = dev; 45b98463f8b0de186dc263217286449a76d6df91a8repo sync setWidgetLayoutResource(R.layout.preference_widget_wifi_signal); 46b98463f8b0de186dc263217286449a76d6df91a8repo sync mRssi = 60; //TODO: fix 47b98463f8b0de186dc263217286449a76d6df91a8repo sync } 48b98463f8b0de186dc263217286449a76d6df91a8repo sync 49b98463f8b0de186dc263217286449a76d6df91a8repo sync @Override 50b98463f8b0de186dc263217286449a76d6df91a8repo sync protected void onBindView(View view) { 51b98463f8b0de186dc263217286449a76d6df91a8repo sync if (TextUtils.isEmpty(device.deviceName)) { 52b98463f8b0de186dc263217286449a76d6df91a8repo sync setTitle(device.deviceAddress); 53b98463f8b0de186dc263217286449a76d6df91a8repo sync } else { 54b98463f8b0de186dc263217286449a76d6df91a8repo sync setTitle(device.deviceName); 55b98463f8b0de186dc263217286449a76d6df91a8repo sync } 56b98463f8b0de186dc263217286449a76d6df91a8repo sync mSignal = (ImageView) view.findViewById(R.id.signal); 57b98463f8b0de186dc263217286449a76d6df91a8repo sync if (mRssi == Integer.MAX_VALUE) { 58b98463f8b0de186dc263217286449a76d6df91a8repo sync mSignal.setImageDrawable(null); 59b98463f8b0de186dc263217286449a76d6df91a8repo sync } else { 60b98463f8b0de186dc263217286449a76d6df91a8repo sync mSignal.setImageResource(R.drawable.wifi_signal); 61b98463f8b0de186dc263217286449a76d6df91a8repo sync mSignal.setImageState(STATE_SECURED, true); 62b98463f8b0de186dc263217286449a76d6df91a8repo sync } 63b98463f8b0de186dc263217286449a76d6df91a8repo sync refresh(); 64b98463f8b0de186dc263217286449a76d6df91a8repo sync super.onBindView(view); 65b98463f8b0de186dc263217286449a76d6df91a8repo sync } 66b98463f8b0de186dc263217286449a76d6df91a8repo sync 67b98463f8b0de186dc263217286449a76d6df91a8repo sync @Override 68b98463f8b0de186dc263217286449a76d6df91a8repo sync public int compareTo(Preference preference) { 69b98463f8b0de186dc263217286449a76d6df91a8repo sync if (!(preference instanceof WifiP2pPeer)) { 70b98463f8b0de186dc263217286449a76d6df91a8repo sync return 1; 71b98463f8b0de186dc263217286449a76d6df91a8repo sync } 72b98463f8b0de186dc263217286449a76d6df91a8repo sync WifiP2pPeer other = (WifiP2pPeer) preference; 73b98463f8b0de186dc263217286449a76d6df91a8repo sync 74b98463f8b0de186dc263217286449a76d6df91a8repo sync // devices go in the order of the status 75b98463f8b0de186dc263217286449a76d6df91a8repo sync if (device.status != other.device.status) { 76ec10578884e3c23b3585bccde76e9c12978dda05Irfan Sheriff return device.status < other.device.status ? -1 : 1; 77b98463f8b0de186dc263217286449a76d6df91a8repo sync } 78b98463f8b0de186dc263217286449a76d6df91a8repo sync 79b98463f8b0de186dc263217286449a76d6df91a8repo sync // Sort by name/address 80b98463f8b0de186dc263217286449a76d6df91a8repo sync if (device.deviceName != null) { 81b98463f8b0de186dc263217286449a76d6df91a8repo sync return device.deviceName.compareToIgnoreCase(other.device.deviceName); 82b98463f8b0de186dc263217286449a76d6df91a8repo sync } 83b98463f8b0de186dc263217286449a76d6df91a8repo sync 84b98463f8b0de186dc263217286449a76d6df91a8repo sync return device.deviceAddress.compareToIgnoreCase(other.device.deviceAddress); 85b98463f8b0de186dc263217286449a76d6df91a8repo sync } 86b98463f8b0de186dc263217286449a76d6df91a8repo sync 87b98463f8b0de186dc263217286449a76d6df91a8repo sync int getLevel() { 88b98463f8b0de186dc263217286449a76d6df91a8repo sync if (mRssi == Integer.MAX_VALUE) { 89b98463f8b0de186dc263217286449a76d6df91a8repo sync return -1; 90b98463f8b0de186dc263217286449a76d6df91a8repo sync } 91b98463f8b0de186dc263217286449a76d6df91a8repo sync return WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS); 92b98463f8b0de186dc263217286449a76d6df91a8repo sync } 93b98463f8b0de186dc263217286449a76d6df91a8repo sync 94b98463f8b0de186dc263217286449a76d6df91a8repo sync private void refresh() { 95b98463f8b0de186dc263217286449a76d6df91a8repo sync if (mSignal == null) { 96b98463f8b0de186dc263217286449a76d6df91a8repo sync return; 97b98463f8b0de186dc263217286449a76d6df91a8repo sync } 98b98463f8b0de186dc263217286449a76d6df91a8repo sync Context context = getContext(); 99b98463f8b0de186dc263217286449a76d6df91a8repo sync mSignal.setImageLevel(getLevel()); 100b98463f8b0de186dc263217286449a76d6df91a8repo sync String[] statusArray = context.getResources().getStringArray(R.array.wifi_p2p_status); 101ec10578884e3c23b3585bccde76e9c12978dda05Irfan Sheriff setSummary(statusArray[device.status]); 102b98463f8b0de186dc263217286449a76d6df91a8repo sync } 103b98463f8b0de186dc263217286449a76d6df91a8repo sync} 104