summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/statusbar/policy/AccessPointControllerImpl.java
blob: ad4c2110d24ec78ef0a31298818c014572354d82 (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * 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.android.systemui.statusbar.policy;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.ActionListener;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;

import com.android.systemui.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


// TODO: Unify this logic with platform settings (see WifiSettings and AccessPoint). There is a
// fair amount of complexity here in statuses and logic beyond just connected/disconnected.
public class AccessPointControllerImpl implements NetworkController.AccessPointController {
    private static final String TAG = "AccessPointController";
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);

    // This string extra specifies a network to open the connect dialog on, so the user can enter
    // network credentials.  This is used by quick settings for secured networks.
    private static final String EXTRA_START_CONNECT_SSID = "wifi_start_connect_ssid";

    private static final int[] ICONS = {
        R.drawable.ic_qs_wifi_0,
        R.drawable.ic_qs_wifi_full_1,
        R.drawable.ic_qs_wifi_full_2,
        R.drawable.ic_qs_wifi_full_3,
        R.drawable.ic_qs_wifi_full_4,
    };

    private final Context mContext;
    private final ArrayList<AccessPointCallback> mCallbacks = new ArrayList<AccessPointCallback>();
    private final WifiManager mWifiManager;
    private final UserManager mUserManager;
    private final Receiver mReceiver = new Receiver();

    private NetworkControllerImpl mNetworkController;
    private boolean mScanning;
    private int mCurrentUser;

    public AccessPointControllerImpl(Context context) {
        mContext = context;
        mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
        mCurrentUser = ActivityManager.getCurrentUser();
    }

    void setNetworkController(NetworkControllerImpl networkController) {
        mNetworkController = networkController;
    }

    public boolean canConfigWifi() {
        return !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI,
                new UserHandle(mCurrentUser));
    }

    public void onUserSwitched(int newUserId) {
        mCurrentUser = newUserId;
    }

    @Override
    public void addAccessPointCallback(AccessPointCallback callback) {
        if (callback == null || mCallbacks.contains(callback)) return;
        if (DEBUG) Log.d(TAG, "addCallback " + callback);
        mCallbacks.add(callback);
        mReceiver.setListening(!mCallbacks.isEmpty());
    }

    @Override
    public void removeAccessPointCallback(AccessPointCallback callback) {
        if (callback == null) return;
        if (DEBUG) Log.d(TAG, "removeCallback " + callback);
        mCallbacks.remove(callback);
        mReceiver.setListening(!mCallbacks.isEmpty());
    }

    @Override
    public void scanForAccessPoints() {
        if (mScanning) return;
        if (DEBUG) Log.d(TAG, "scan!");
        mScanning = mWifiManager.startScan();
        // Grab current networks immediately while we wait for scan.
        updateAccessPoints();
    }

    public boolean connect(AccessPoint ap) {
        if (ap == null) return false;
        if (DEBUG) Log.d(TAG, "connect networkId=" + ap.networkId);
        if (ap.networkId < 0) {
            // Unknown network, need to add it.
            if (ap.hasSecurity) {
                Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
                intent.putExtra(EXTRA_START_CONNECT_SSID, ap.ssid);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                fireSettingsIntentCallback(intent);
                return true;
            } else {
                WifiConfiguration config = new WifiConfiguration();
                config.SSID = "\"" + ap.ssid + "\"";
                config.allowedKeyManagement.set(KeyMgmt.NONE);
                mWifiManager.connect(config, mConnectListener);
            }
        } else {
            mWifiManager.connect(ap.networkId, mConnectListener);
        }
        return false;
    }

    private void fireSettingsIntentCallback(Intent intent) {
        for (AccessPointCallback callback : mCallbacks) {
            callback.onSettingsActivityTriggered(intent);
        }
    }

    private void fireAcccessPointsCallback(AccessPoint[] aps) {
        for (AccessPointCallback callback : mCallbacks) {
            callback.onAccessPointsChanged(aps);
        }
    }

    private static String trimDoubleQuotes(String v) {
        return v != null && v.length() >= 2 && v.charAt(0) == '\"'
                && v.charAt(v.length() - 1) == '\"' ? v.substring(1, v.length() - 1) : v;
    }

    private int getConnectedNetworkId(WifiInfo wifiInfo) {
        return wifiInfo != null ? wifiInfo.getNetworkId() : AccessPoint.NO_NETWORK;
    }

    private ArrayMap<String, WifiConfiguration> getConfiguredNetworksBySsid() {
        final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
        if (configs == null || configs.size() == 0) return ArrayMap.EMPTY;
        final ArrayMap<String, WifiConfiguration> rt = new ArrayMap<String, WifiConfiguration>();
        for (WifiConfiguration config : configs) {
            rt.put(trimDoubleQuotes(config.SSID), config);
        }
        return rt;
    }

    private void updateAccessPoints() {
        final WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
        final int connectedNetworkId = getConnectedNetworkId(wifiInfo);
        if (DEBUG) Log.d(TAG, "connectedNetworkId: " + connectedNetworkId);
        final List<ScanResult> scanResults = mWifiManager.getScanResults();
        final ArrayMap<String, WifiConfiguration> configured = getConfiguredNetworksBySsid();
        if (DEBUG) Log.d(TAG, "scanResults: " + scanResults);
        final List<AccessPoint> aps = new ArrayList<AccessPoint>(scanResults.size());
        final ArraySet<String> ssids = new ArraySet<String>();
        for (ScanResult scanResult : scanResults) {
            if (scanResult == null) {
                continue;
            }
            final String ssid = scanResult.SSID;
            if (TextUtils.isEmpty(ssid) || ssids.contains(ssid)) continue;
            ssids.add(ssid);
            final WifiConfiguration config = configured.get(ssid);
            final int level = WifiManager.calculateSignalLevel(scanResult.level, ICONS.length);
            final AccessPoint ap = new AccessPoint();
            ap.isConfigured = config != null;
            ap.networkId = config != null ? config.networkId : AccessPoint.NO_NETWORK;
            ap.ssid = ssid;
            // Connected if either:
            // -The network ID in the active WifiInfo matches this network's ID.
            // -The network is ephemeral (no configuration) but the SSID matches.
            ap.isConnected = (ap.networkId != AccessPoint.NO_NETWORK
                    && ap.networkId == connectedNetworkId) ||
                    (ap.networkId == WifiConfiguration.INVALID_NETWORK_ID && wifiInfo != null &&
                    ap.ssid.equals(trimDoubleQuotes(wifiInfo.getSSID())));
            if (ap.isConnected && mNetworkController != null) {
                // Ensure we have the connected network's RSSI.
                ap.level = mNetworkController.getConnectedWifiLevel();
            } else {
                ap.level = level;
            }
            ap.iconId = ICONS[ap.level];
            // Based on Settings AccessPoint#getSecurity, keep up to date
            // with better methods of determining no security or not.
            ap.hasSecurity = scanResult.capabilities.contains("WEP")
                    || scanResult.capabilities.contains("PSK")
                    || scanResult.capabilities.contains("EAP");
            aps.add(ap);
        }
        Collections.sort(aps, mByStrength);
        fireAcccessPointsCallback(aps.toArray(new AccessPoint[aps.size()]));
    }

    private final ActionListener mConnectListener = new ActionListener() {
        @Override
        public void onSuccess() {
            if (DEBUG) Log.d(TAG, "connect success");
        }

        @Override
        public void onFailure(int reason) {
            if (DEBUG) Log.d(TAG, "connect failure reason=" + reason);
        }
    };

    private final Comparator<AccessPoint> mByStrength = new Comparator<AccessPoint> () {
        @Override
        public int compare(AccessPoint lhs, AccessPoint rhs) {
            return -Integer.compare(score(lhs), score(rhs));
        }

        private int score(AccessPoint ap) {
            return ap.level + (ap.isConnected ? 20 : 0) + (ap.isConfigured ? 10 : 0);
        }
    };

    private final class Receiver extends BroadcastReceiver {
        private boolean mRegistered;

        public void setListening(boolean listening) {
            if (listening && !mRegistered) {
                if (DEBUG) Log.d(TAG, "Registering receiver");
                final IntentFilter filter = new IntentFilter();
                filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
                filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
                filter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
                filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
                filter.addAction(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION);
                filter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);
                filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
                filter.addAction(WifiManager.RSSI_CHANGED_ACTION);
                mContext.registerReceiver(this, filter);
                mRegistered = true;
            } else if (!listening && mRegistered) {
                if (DEBUG) Log.d(TAG, "Unregistering receiver");
                mContext.unregisterReceiver(this);
                mRegistered = false;
            }
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (DEBUG) Log.d(TAG, "onReceive " + intent.getAction());
            if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())) {
                updateAccessPoints();
                mScanning = false;
            }
        }
    }
}