blob: 872c222bc3d6959e2aca7391824dd61337993958 [file] [log] [blame]
roger xue843fbc42017-02-22 16:20:08 -08001/*
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 */
16package com.android.car.settings.wifi;
17
Lujiang Xue0daa9192017-12-06 11:48:39 -080018import android.annotation.Nullable;
roger xue843fbc42017-02-22 16:20:08 -080019import android.content.Context;
Lujiang Xue0daa9192017-12-06 11:48:39 -080020import android.net.NetworkInfo;
roger xue843fbc42017-02-22 16:20:08 -080021import android.net.wifi.WifiManager;
roger xue843fbc42017-02-22 16:20:08 -080022import android.support.annotation.UiThread;
23
24import com.android.settingslib.wifi.AccessPoint;
25import com.android.settingslib.wifi.WifiTracker;
roger xue843fbc42017-02-22 16:20:08 -080026
27import java.util.ArrayList;
28import java.util.List;
29
30/**
31 * Manages Wifi configuration: e.g. monitors wifi states, change wifi setting etc.
32 */
33public class CarWifiManager implements WifiTracker.WifiListener {
34 private static final String TAG = "CarWifiManager";
35 private final Context mContext;
36 private Listener mListener;
37 private boolean mStarted;
38
39 private WifiTracker mWifiTracker;
roger xue843fbc42017-02-22 16:20:08 -080040 private WifiManager mWifiManager;
roger xueb65a0762017-03-24 13:20:14 -070041 public interface Listener {
roger xue843fbc42017-02-22 16:20:08 -080042 /**
43 * Something about wifi setting changed.
44 */
45 void onAccessPointsChanged();
46
47 /**
48 * Called when the state of Wifi has changed, the state will be one of
49 * the following.
50 *
51 * <li>{@link WifiManager#WIFI_STATE_DISABLED}</li>
52 * <li>{@link WifiManager#WIFI_STATE_ENABLED}</li>
53 * <li>{@link WifiManager#WIFI_STATE_DISABLING}</li>
54 * <li>{@link WifiManager#WIFI_STATE_ENABLING}</li>
55 * <li>{@link WifiManager#WIFI_STATE_UNKNOWN}</li>
56 * <p>
57 *
58 * @param state The new state of wifi.
59 */
60 void onWifiStateChanged(int state);
61 }
62
63 public CarWifiManager(Context context, Listener listener) {
64 mContext = context;
65 mListener = listener;
66 mWifiManager = (WifiManager) mContext.getSystemService(WifiManager.class);
Tony Mantler9fe1a512017-09-28 15:03:35 -070067 mWifiTracker = new WifiTracker(context, this, true, true);
roger xue843fbc42017-02-22 16:20:08 -080068 }
69
70 /**
71 * Starts {@link CarWifiManager}.
72 * This should be called only from main thread.
73 */
74 @UiThread
75 public void start() {
76 if (!mStarted) {
77 mStarted = true;
Tony Mantler9fe1a512017-09-28 15:03:35 -070078 mWifiTracker.onStart();
roger xue843fbc42017-02-22 16:20:08 -080079 }
80 }
81
82 /**
83 * Stops {@link CarWifiManager}.
84 * This should be called only from main thread.
85 */
86 @UiThread
87 public void stop() {
88 if (mStarted) {
89 mStarted = false;
Tony Mantler9fe1a512017-09-28 15:03:35 -070090 mWifiTracker.onStop();
roger xue843fbc42017-02-22 16:20:08 -080091 }
92 }
93
Tony Mantler9fe1a512017-09-28 15:03:35 -070094 /**
95 * Destroys {@link CarWifiManager}
96 * This should only be called from main thread.
97 */
98 @UiThread
99 public void destroy() {
100 mWifiTracker.onDestroy();
101 }
102
Lujiang Xueeaff6c32018-04-10 08:20:29 -0700103 /**
104 * Returns a list of all reachable access points.
105 */
106 public List<AccessPoint> getAllAccessPoints() {
107 return getAccessPoints(false);
108 }
109
110 /**
111 * Returns a list of saved access points.
112 */
113 public List<AccessPoint> getSavedAccessPoints() {
114 return getAccessPoints(true);
115 }
116
117 private List<AccessPoint> getAccessPoints(boolean saved) {
roger xue843fbc42017-02-22 16:20:08 -0800118 List<AccessPoint> accessPoints = new ArrayList<AccessPoint>();
119 if (mWifiManager.isWifiEnabled()) {
120 for (AccessPoint accessPoint : mWifiTracker.getAccessPoints()) {
121 // ignore out of reach access points.
Lujiang Xueeaff6c32018-04-10 08:20:29 -0700122 if (shouldIncludeAp(accessPoint, saved)) {
roger xue843fbc42017-02-22 16:20:08 -0800123 accessPoints.add(accessPoint);
124 }
125 }
126 }
127 return accessPoints;
128 }
129
Lujiang Xueeaff6c32018-04-10 08:20:29 -0700130 private boolean shouldIncludeAp(AccessPoint accessPoint, boolean saved) {
131 return saved ? accessPoint.isReachable() && accessPoint.isSaved()
132 : accessPoint.isReachable();
133 }
134
Lujiang Xue0daa9192017-12-06 11:48:39 -0800135 @Nullable
136 public AccessPoint getConnectedAccessPoint() {
Lujiang Xueeaff6c32018-04-10 08:20:29 -0700137 for (AccessPoint accessPoint : getAllAccessPoints()) {
Lujiang Xue0daa9192017-12-06 11:48:39 -0800138 if (accessPoint.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) {
139 return accessPoint;
140 }
141 }
142 return null;
143 }
144
roger xue843fbc42017-02-22 16:20:08 -0800145 public boolean isWifiEnabled() {
146 return mWifiManager.isWifiEnabled();
147 }
148
roger xueb65a0762017-03-24 13:20:14 -0700149 public int getWifiState() {
150 return mWifiManager.getWifiState();
151 }
152
roger xue843fbc42017-02-22 16:20:08 -0800153 public boolean setWifiEnabled(boolean enabled) {
154 return mWifiManager.setWifiEnabled(enabled);
155 }
156
157 public void connectToPublicWifi(AccessPoint accessPoint, WifiManager.ActionListener listener) {
158 accessPoint.generateOpenNetworkConfig();
159 mWifiManager.connect(accessPoint.getConfig(), listener);
160 }
161
162 @Override
163 public void onWifiStateChanged(int state) {
164 mListener.onWifiStateChanged(state);
165 }
166
167 @Override
168 public void onConnectedChanged() {
169 }
170
171 @Override
172 public void onAccessPointsChanged() {
173 mListener.onAccessPointsChanged();
174 }
175}