roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 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 | |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 18 | import android.annotation.Nullable; |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 20 | import android.net.NetworkInfo; |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 21 | import android.net.wifi.WifiManager; |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 22 | import android.support.annotation.UiThread; |
| 23 | |
| 24 | import com.android.settingslib.wifi.AccessPoint; |
| 25 | import com.android.settingslib.wifi.WifiTracker; |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.List; |
| 29 | |
| 30 | /** |
| 31 | * Manages Wifi configuration: e.g. monitors wifi states, change wifi setting etc. |
| 32 | */ |
| 33 | public 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 xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 40 | private WifiManager mWifiManager; |
roger xue | b65a076 | 2017-03-24 13:20:14 -0700 | [diff] [blame] | 41 | public interface Listener { |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 42 | /** |
| 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 Mantler | 9fe1a51 | 2017-09-28 15:03:35 -0700 | [diff] [blame] | 67 | mWifiTracker = new WifiTracker(context, this, true, true); |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 68 | } |
| 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 Mantler | 9fe1a51 | 2017-09-28 15:03:35 -0700 | [diff] [blame] | 78 | mWifiTracker.onStart(); |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 79 | } |
| 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 Mantler | 9fe1a51 | 2017-09-28 15:03:35 -0700 | [diff] [blame] | 90 | mWifiTracker.onStop(); |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Tony Mantler | 9fe1a51 | 2017-09-28 15:03:35 -0700 | [diff] [blame] | 94 | /** |
| 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 Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 103 | /** |
| 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 xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 118 | List<AccessPoint> accessPoints = new ArrayList<AccessPoint>(); |
| 119 | if (mWifiManager.isWifiEnabled()) { |
| 120 | for (AccessPoint accessPoint : mWifiTracker.getAccessPoints()) { |
| 121 | // ignore out of reach access points. |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 122 | if (shouldIncludeAp(accessPoint, saved)) { |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 123 | accessPoints.add(accessPoint); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | return accessPoints; |
| 128 | } |
| 129 | |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 130 | private boolean shouldIncludeAp(AccessPoint accessPoint, boolean saved) { |
| 131 | return saved ? accessPoint.isReachable() && accessPoint.isSaved() |
| 132 | : accessPoint.isReachable(); |
| 133 | } |
| 134 | |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 135 | @Nullable |
| 136 | public AccessPoint getConnectedAccessPoint() { |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 137 | for (AccessPoint accessPoint : getAllAccessPoints()) { |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 138 | if (accessPoint.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) { |
| 139 | return accessPoint; |
| 140 | } |
| 141 | } |
| 142 | return null; |
| 143 | } |
| 144 | |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 145 | public boolean isWifiEnabled() { |
| 146 | return mWifiManager.isWifiEnabled(); |
| 147 | } |
| 148 | |
roger xue | b65a076 | 2017-03-24 13:20:14 -0700 | [diff] [blame] | 149 | public int getWifiState() { |
| 150 | return mWifiManager.getWifiState(); |
| 151 | } |
| 152 | |
roger xue | 843fbc4 | 2017-02-22 16:20:08 -0800 | [diff] [blame] | 153 | 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 | } |