• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.WifiP2pManager;
24 import android.net.wifi.p2p.WifiP2pDevice;
25 import android.preference.Preference;
26 import android.text.TextUtils;
27 import android.view.View;
28 import android.widget.ImageView;
29 
30 import java.util.Comparator;
31 
32 public class WifiP2pPeer extends Preference {
33 
34     private static final int[] STATE_SECURED = {R.attr.state_encrypted};
35     public WifiP2pDevice device;
36 
37     private int mRssi;
38     private ImageView mSignal;
39 
40     private static final int SIGNAL_LEVELS = 4;
41 
WifiP2pPeer(Context context, WifiP2pDevice dev)42     public WifiP2pPeer(Context context, WifiP2pDevice dev) {
43         super(context);
44         device = dev;
45         setWidgetLayoutResource(R.layout.preference_widget_wifi_signal);
46         mRssi = 60; //TODO: fix
47     }
48 
49     @Override
onBindView(View view)50     protected void onBindView(View view) {
51         if (TextUtils.isEmpty(device.deviceName)) {
52             setTitle(device.deviceAddress);
53         } else {
54             setTitle(device.deviceName);
55         }
56         mSignal = (ImageView) view.findViewById(R.id.signal);
57         if (mRssi == Integer.MAX_VALUE) {
58             mSignal.setImageDrawable(null);
59         } else {
60             mSignal.setImageResource(R.drawable.wifi_signal);
61             mSignal.setImageState(STATE_SECURED,  true);
62         }
63         refresh();
64         super.onBindView(view);
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 
refresh()94     private void refresh() {
95         if (mSignal == null) {
96             return;
97         }
98         Context context = getContext();
99         mSignal.setImageLevel(getLevel());
100         String[] statusArray = context.getResources().getStringArray(R.array.wifi_p2p_status);
101         setSummary(statusArray[device.status]);
102     }
103 }
104