1 /*
2  * Copyright (C) 2017 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 package com.android.car.settings.wifi;
17 
18 import android.content.Context;
19 import android.net.wifi.WifiManager;
20 import android.os.HandlerThread;
21 import android.os.Process;
22 import android.support.annotation.UiThread;
23 
24 import com.android.settingslib.wifi.AccessPoint;
25 import com.android.settingslib.wifi.WifiTracker;
26 import com.android.settingslib.wifi.WifiTracker.WifiListener;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 /**
32  * Manages Wifi configuration: e.g. monitors wifi states, change wifi setting etc.
33  */
34 public class CarWifiManager implements WifiTracker.WifiListener {
35     private static final String TAG = "CarWifiManager";
36     private final Context mContext;
37     private Listener mListener;
38     private boolean mStarted;
39 
40     private WifiTracker mWifiTracker;
41     private final HandlerThread mBgThread;
42     private WifiManager mWifiManager;
43     public interface Listener {
44         /**
45          * Something about wifi setting changed.
46          */
onAccessPointsChanged()47         void onAccessPointsChanged();
48 
49         /**
50          * Called when the state of Wifi has changed, the state will be one of
51          * the following.
52          *
53          * <li>{@link WifiManager#WIFI_STATE_DISABLED}</li>
54          * <li>{@link WifiManager#WIFI_STATE_ENABLED}</li>
55          * <li>{@link WifiManager#WIFI_STATE_DISABLING}</li>
56          * <li>{@link WifiManager#WIFI_STATE_ENABLING}</li>
57          * <li>{@link WifiManager#WIFI_STATE_UNKNOWN}</li>
58          * <p>
59          *
60          * @param state The new state of wifi.
61          */
onWifiStateChanged(int state)62         void onWifiStateChanged(int state);
63     }
64 
CarWifiManager(Context context, Listener listener)65     public CarWifiManager(Context context, Listener listener) {
66         mContext = context;
67         mListener = listener;
68         mWifiManager = (WifiManager) mContext.getSystemService(WifiManager.class);
69         mBgThread = new HandlerThread(TAG, Process.THREAD_PRIORITY_BACKGROUND);
70         mBgThread.start();
71         mWifiTracker = new WifiTracker(context, this, mBgThread.getLooper(), true, true);
72     }
73 
74     /**
75      * Starts {@link CarWifiManager}.
76      * This should be called only from main thread.
77      */
78     @UiThread
start()79     public void start() {
80         if (!mStarted) {
81             mStarted = true;
82             mWifiTracker.startTracking();
83         }
84     }
85 
86     /**
87      * Stops {@link CarWifiManager}.
88      * This should be called only from main thread.
89      */
90     @UiThread
stop()91     public void stop() {
92         if (mStarted) {
93             mStarted = false;
94             mWifiTracker.stopTracking();
95         }
96     }
97 
getAccessPoints()98     public List<AccessPoint> getAccessPoints() {
99         List<AccessPoint> accessPoints = new ArrayList<AccessPoint>();
100         if (mWifiManager.isWifiEnabled()) {
101             for (AccessPoint accessPoint : mWifiTracker.getAccessPoints()) {
102                 // ignore out of reach access points.
103                 if (accessPoint.getLevel() != -1) {
104                     accessPoints.add(accessPoint);
105                 }
106             }
107         }
108         return accessPoints;
109     }
110 
isWifiEnabled()111     public boolean isWifiEnabled() {
112         return mWifiManager.isWifiEnabled();
113     }
114 
getWifiState()115     public int getWifiState() {
116         return mWifiManager.getWifiState();
117     }
118 
setWifiEnabled(boolean enabled)119     public boolean setWifiEnabled(boolean enabled) {
120         return mWifiManager.setWifiEnabled(enabled);
121     }
122 
connectToPublicWifi(AccessPoint accessPoint, WifiManager.ActionListener listener)123     public void connectToPublicWifi(AccessPoint accessPoint, WifiManager.ActionListener listener) {
124         accessPoint.generateOpenNetworkConfig();
125         mWifiManager.connect(accessPoint.getConfig(), listener);
126     }
127 
128     @Override
onWifiStateChanged(int state)129     public void onWifiStateChanged(int state) {
130         mListener.onWifiStateChanged(state);
131     }
132 
133     @Override
onConnectedChanged()134     public void onConnectedChanged() {
135     }
136 
137     @Override
onAccessPointsChanged()138     public void onAccessPointsChanged() {
139         mListener.onAccessPointsChanged();
140     }
141 }
142