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 android.content.Context;
20b98463f8b0de186dc263217286449a76d6df91a8repo syncimport android.net.wifi.WifiManager;
21b98463f8b0de186dc263217286449a76d6df91a8repo syncimport android.net.wifi.p2p.WifiP2pDevice;
2239b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport android.support.v7.preference.Preference;
2339b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport android.support.v7.preference.PreferenceViewHolder;
24b98463f8b0de186dc263217286449a76d6df91a8repo syncimport android.text.TextUtils;
25b98463f8b0de186dc263217286449a76d6df91a8repo syncimport android.widget.ImageView;
2639b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport com.android.settings.R;
2739b467482d1bf256a111c757e9b7621c6f523271Jason Monk
28b98463f8b0de186dc263217286449a76d6df91a8repo syncpublic class WifiP2pPeer extends Preference {
29b98463f8b0de186dc263217286449a76d6df91a8repo sync
30b98463f8b0de186dc263217286449a76d6df91a8repo sync    private static final int[] STATE_SECURED = {R.attr.state_encrypted};
31b98463f8b0de186dc263217286449a76d6df91a8repo sync    public WifiP2pDevice device;
32b98463f8b0de186dc263217286449a76d6df91a8repo sync
33ac57f3e1eb56998550534c1b32d99cb7b5cc2b7bRussell Brenner    private final int mRssi;
34b98463f8b0de186dc263217286449a76d6df91a8repo sync    private ImageView mSignal;
35b98463f8b0de186dc263217286449a76d6df91a8repo sync
36b98463f8b0de186dc263217286449a76d6df91a8repo sync    private static final int SIGNAL_LEVELS = 4;
37b98463f8b0de186dc263217286449a76d6df91a8repo sync
38b98463f8b0de186dc263217286449a76d6df91a8repo sync    public WifiP2pPeer(Context context, WifiP2pDevice dev) {
39b98463f8b0de186dc263217286449a76d6df91a8repo sync        super(context);
40b98463f8b0de186dc263217286449a76d6df91a8repo sync        device = dev;
41b98463f8b0de186dc263217286449a76d6df91a8repo sync        setWidgetLayoutResource(R.layout.preference_widget_wifi_signal);
42b98463f8b0de186dc263217286449a76d6df91a8repo sync        mRssi = 60; //TODO: fix
43b98463f8b0de186dc263217286449a76d6df91a8repo sync        if (TextUtils.isEmpty(device.deviceName)) {
44b98463f8b0de186dc263217286449a76d6df91a8repo sync            setTitle(device.deviceAddress);
45b98463f8b0de186dc263217286449a76d6df91a8repo sync        } else {
46b98463f8b0de186dc263217286449a76d6df91a8repo sync            setTitle(device.deviceName);
47b98463f8b0de186dc263217286449a76d6df91a8repo sync        }
480b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        String[] statusArray = context.getResources().getStringArray(R.array.wifi_p2p_status);
490b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        setSummary(statusArray[device.status]);
500b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    }
510b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk
520b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    @Override
530b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk    public void onBindViewHolder(PreferenceViewHolder view) {
540b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        super.onBindViewHolder(view);
55b98463f8b0de186dc263217286449a76d6df91a8repo sync        mSignal = (ImageView) view.findViewById(R.id.signal);
56b98463f8b0de186dc263217286449a76d6df91a8repo sync        if (mRssi == Integer.MAX_VALUE) {
57b98463f8b0de186dc263217286449a76d6df91a8repo sync            mSignal.setImageDrawable(null);
58b98463f8b0de186dc263217286449a76d6df91a8repo sync        } else {
59ac57f3e1eb56998550534c1b32d99cb7b5cc2b7bRussell Brenner            mSignal.setImageResource(R.drawable.wifi_signal_dark);
60b98463f8b0de186dc263217286449a76d6df91a8repo sync            mSignal.setImageState(STATE_SECURED,  true);
61b98463f8b0de186dc263217286449a76d6df91a8repo sync        }
620b4fdc49fba83ad2a950681ef014b6927e438007Jason Monk        mSignal.setImageLevel(getLevel());
63b98463f8b0de186dc263217286449a76d6df91a8repo sync    }
64b98463f8b0de186dc263217286449a76d6df91a8repo sync
65b98463f8b0de186dc263217286449a76d6df91a8repo sync    @Override
66b98463f8b0de186dc263217286449a76d6df91a8repo sync    public int compareTo(Preference preference) {
67b98463f8b0de186dc263217286449a76d6df91a8repo sync        if (!(preference instanceof WifiP2pPeer)) {
68b98463f8b0de186dc263217286449a76d6df91a8repo sync            return 1;
69b98463f8b0de186dc263217286449a76d6df91a8repo sync        }
70b98463f8b0de186dc263217286449a76d6df91a8repo sync        WifiP2pPeer other = (WifiP2pPeer) preference;
71b98463f8b0de186dc263217286449a76d6df91a8repo sync
72b98463f8b0de186dc263217286449a76d6df91a8repo sync        // devices go in the order of the status
73b98463f8b0de186dc263217286449a76d6df91a8repo sync        if (device.status != other.device.status) {
74ec10578884e3c23b3585bccde76e9c12978dda05Irfan Sheriff            return device.status < other.device.status ? -1 : 1;
75b98463f8b0de186dc263217286449a76d6df91a8repo sync        }
76b98463f8b0de186dc263217286449a76d6df91a8repo sync
77b98463f8b0de186dc263217286449a76d6df91a8repo sync        // Sort by name/address
78b98463f8b0de186dc263217286449a76d6df91a8repo sync        if (device.deviceName != null) {
79b98463f8b0de186dc263217286449a76d6df91a8repo sync            return device.deviceName.compareToIgnoreCase(other.device.deviceName);
80b98463f8b0de186dc263217286449a76d6df91a8repo sync        }
81b98463f8b0de186dc263217286449a76d6df91a8repo sync
82b98463f8b0de186dc263217286449a76d6df91a8repo sync        return device.deviceAddress.compareToIgnoreCase(other.device.deviceAddress);
83b98463f8b0de186dc263217286449a76d6df91a8repo sync    }
84b98463f8b0de186dc263217286449a76d6df91a8repo sync
85b98463f8b0de186dc263217286449a76d6df91a8repo sync    int getLevel() {
86b98463f8b0de186dc263217286449a76d6df91a8repo sync        if (mRssi == Integer.MAX_VALUE) {
87b98463f8b0de186dc263217286449a76d6df91a8repo sync            return -1;
88b98463f8b0de186dc263217286449a76d6df91a8repo sync        }
89b98463f8b0de186dc263217286449a76d6df91a8repo sync        return WifiManager.calculateSignalLevel(mRssi, SIGNAL_LEVELS);
90b98463f8b0de186dc263217286449a76d6df91a8repo sync    }
91b98463f8b0de186dc263217286449a76d6df91a8repo sync}
92