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