hc
2023-08-30 862c27fc9920c83318c784bfdadf43a65df1ec8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/******************************************************************************
 *
 *  Copyright (C) 2013-2014 Cypress Semiconductor
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/
package com.rockchip.smart.rockhome;
 
import android.app.Activity;
import android.app.ListFragment;
import android.content.Context;
import android.graphics.Color;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * UI component to allow users to scan for Bluetooth LE devices and pick a
 * selected device *
 */
public class WifiListFragment extends ListFragment {
    private static final String TAG = "WifiListFragment";
 
    /**
     * Interface to listen for results
     */
    public static interface WifiCallback {
        public void onWifiPicked(WifiInfo info);
 
        public void onWifiPickCancelled();
    }
 
    /**
     * Helper class used for displaying LE devices in a pick list
     */
    public static class WifiAdapter extends BaseAdapter {
 
        private final Context mContext;
        private List<WifiInfo> mWifis;
        private final LayoutInflater mInflater;
 
        public WifiAdapter(Context context) {
            mContext = context;
 
            mInflater = LayoutInflater.from(context);
            mWifis = new ArrayList<WifiInfo>();
        }
 
        public void update(List<WifiInfo> wifis) {
            mWifis = wifis;
            notifyDataSetChanged();
        }
 
        @Override
        public int getCount() {
            return mWifis.size();
        }
 
        @Override
        public Object getItem(int position) {
            return mWifis.get(position);
        }
 
        @Override
        public long getItemId(int position) {
            return position;
        }
 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
 
            if (convertView == null || convertView.findViewById(R.id.ssid) == null) {
                convertView = mInflater.inflate(R.layout.dialog_wifilist_item, null);
                holder = new ViewHolder();
                holder.ssid = (TextView) convertView.findViewById(R.id.ssid);
                holder.level = (ImageView) convertView.findViewById(R.id.wifi_level);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
 
            WifiInfo rec = mWifis.get(position);
            holder.ssid.setText(rec.getSsid());
            if (rec.isConnecting()) {
                holder.ssid.setTextColor(Color.BLUE);
            }
            int level= WifiManager.calculateSignalLevel(Integer.parseInt(rec.getSignalLevel()), 5);
            if(rec.getFlags() == null || rec.getFlags().contains("WEP") || rec.getFlags().contains("PSK") || rec.getFlags().contains("EAP")){
                holder.level.setImageResource(R.drawable.wifi_signal_lock);
            }else{
                holder.level.setImageResource(R.drawable.wifi_signal_open);
            }
            holder.level.setImageLevel(level);
 
            return convertView;
        }
 
        static class ViewHolder {
            TextView ssid;
            ImageView level;
        }
    }
 
    private WifiAdapter mWifiAdapter;
    private WifiCallback mCallback;
    private boolean mDevicePicked;
 
    /**
     * Set the callback object to invoke when a device is picked OR if the
     * device picker is cancelled
     *
     * @param cb
     */
    public void setCallback(WifiCallback cb) {
        mCallback = cb;
    }
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Activity activity = getActivity();
 
        // Otherwise populate the list device
        mWifiAdapter = new WifiAdapter(activity);
        setListAdapter(mWifiAdapter);
    }
 
    @Override
    public void onListItemClick(ListView list, View view, int position, long id) {
        WifiInfo wifi = (WifiInfo) mWifiAdapter.getItem(position);
        if (wifi != null && mCallback != null) {
            try {
                mDevicePicked = true;
                mCallback.onWifiPicked(wifi);
            } catch (Throwable t) {
                Log.w(TAG, "onListItemClick: error calling callback", t);
            }
        }
    }
 
    @Override
    public void onResume() {
        super.onResume();
        Log.d("BlueFragment", "WifiListFragment onResume");
    }
 
    @Override
    public void onPause() {
        super.onPause();
        Log.d("BlueFragment", "WifiListFragment onPause");
        if (!mDevicePicked && mCallback != null)
            mCallback.onWifiPickCancelled();
    }
 
    public void scan(boolean enable) {
        scan(enable, null);
    }
 
    /**
     * Start or stop scanning for LE devices
     *
     * @param enable
     */
    public void scan(boolean enable, List<WifiInfo> wifiList) {
        if (enable && wifiList != null) {
            mWifiAdapter.update(wifiList);
        }
 
        getActivity().invalidateOptionsMenu();
    }
}