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.annotation.Nullable; 19 import android.content.Context; 20 import android.net.NetworkInfo; 21 import android.net.wifi.WifiManager; 22 import android.support.annotation.UiThread; 23 24 import com.android.settingslib.wifi.AccessPoint; 25 import com.android.settingslib.wifi.WifiTracker; 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 final Context mContext; 35 private Listener mListener; 36 private boolean mStarted; 37 38 private WifiTracker mWifiTracker; 39 private WifiManager mWifiManager; 40 public interface Listener { 41 /** 42 * Something about wifi setting changed. 43 */ onAccessPointsChanged()44 void onAccessPointsChanged(); 45 46 /** 47 * Called when the state of Wifi has changed, the state will be one of 48 * the following. 49 * 50 * <li>{@link WifiManager#WIFI_STATE_DISABLED}</li> 51 * <li>{@link WifiManager#WIFI_STATE_ENABLED}</li> 52 * <li>{@link WifiManager#WIFI_STATE_DISABLING}</li> 53 * <li>{@link WifiManager#WIFI_STATE_ENABLING}</li> 54 * <li>{@link WifiManager#WIFI_STATE_UNKNOWN}</li> 55 * <p> 56 * 57 * @param state The new state of wifi. 58 */ onWifiStateChanged(int state)59 void onWifiStateChanged(int state); 60 } 61 CarWifiManager(Context context, Listener listener)62 public CarWifiManager(Context context, Listener listener) { 63 mContext = context; 64 mListener = listener; 65 mWifiManager = (WifiManager) mContext.getSystemService(WifiManager.class); 66 mWifiTracker = new WifiTracker(context, this, true, true); 67 } 68 69 /** 70 * Starts {@link CarWifiManager}. 71 * This should be called only from main thread. 72 */ 73 @UiThread start()74 public void start() { 75 if (!mStarted) { 76 mStarted = true; 77 mWifiTracker.onStart(); 78 } 79 } 80 81 /** 82 * Stops {@link CarWifiManager}. 83 * This should be called only from main thread. 84 */ 85 @UiThread stop()86 public void stop() { 87 if (mStarted) { 88 mStarted = false; 89 mWifiTracker.onStop(); 90 } 91 } 92 93 /** 94 * Destroys {@link CarWifiManager} 95 * This should only be called from main thread. 96 */ 97 @UiThread destroy()98 public void destroy() { 99 mWifiTracker.onDestroy(); 100 } 101 102 /** 103 * Returns a list of all reachable access points. 104 */ getAllAccessPoints()105 public List<AccessPoint> getAllAccessPoints() { 106 return getAccessPoints(false); 107 } 108 109 /** 110 * Returns a list of saved access points. 111 */ getSavedAccessPoints()112 public List<AccessPoint> getSavedAccessPoints() { 113 return getAccessPoints(true); 114 } 115 getAccessPoints(boolean saved)116 private List<AccessPoint> getAccessPoints(boolean saved) { 117 List<AccessPoint> accessPoints = new ArrayList<AccessPoint>(); 118 if (mWifiManager.isWifiEnabled()) { 119 for (AccessPoint accessPoint : mWifiTracker.getAccessPoints()) { 120 // ignore out of reach access points. 121 if (shouldIncludeAp(accessPoint, saved)) { 122 accessPoints.add(accessPoint); 123 } 124 } 125 } 126 return accessPoints; 127 } 128 shouldIncludeAp(AccessPoint accessPoint, boolean saved)129 private boolean shouldIncludeAp(AccessPoint accessPoint, boolean saved) { 130 return saved ? accessPoint.isReachable() && accessPoint.isSaved() 131 : accessPoint.isReachable(); 132 } 133 134 @Nullable getConnectedAccessPoint()135 public AccessPoint getConnectedAccessPoint() { 136 for (AccessPoint accessPoint : getAllAccessPoints()) { 137 if (accessPoint.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) { 138 return accessPoint; 139 } 140 } 141 return null; 142 } 143 isWifiEnabled()144 public boolean isWifiEnabled() { 145 return mWifiManager.isWifiEnabled(); 146 } 147 getWifiState()148 public int getWifiState() { 149 return mWifiManager.getWifiState(); 150 } 151 setWifiEnabled(boolean enabled)152 public boolean setWifiEnabled(boolean enabled) { 153 return mWifiManager.setWifiEnabled(enabled); 154 } 155 connectToPublicWifi(AccessPoint accessPoint, WifiManager.ActionListener listener)156 public void connectToPublicWifi(AccessPoint accessPoint, WifiManager.ActionListener listener) { 157 accessPoint.generateOpenNetworkConfig(); 158 mWifiManager.connect(accessPoint.getConfig(), listener); 159 } 160 161 @Override onWifiStateChanged(int state)162 public void onWifiStateChanged(int state) { 163 mListener.onWifiStateChanged(state); 164 } 165 166 @Override onConnectedChanged()167 public void onConnectedChanged() { 168 } 169 170 @Override onAccessPointsChanged()171 public void onAccessPointsChanged() { 172 mListener.onAccessPointsChanged(); 173 } 174 } 175