John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
| 17 | package com.android.systemui.statusbar.policy; |
| 18 | |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 19 | import android.app.ActivityManager; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 22 | import android.net.wifi.WifiManager.ActionListener; |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 23 | import android.os.UserHandle; |
| 24 | import android.os.UserManager; |
| 25 | import android.provider.Settings; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 26 | import android.util.Log; |
| 27 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 28 | import com.android.settingslib.wifi.AccessPoint; |
| 29 | import com.android.settingslib.wifi.WifiTracker; |
| 30 | import com.android.settingslib.wifi.WifiTracker.WifiListener; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 31 | import com.android.systemui.R; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 32 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 33 | import java.io.PrintWriter; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 34 | import java.util.ArrayList; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 35 | import java.util.List; |
| 36 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 37 | public class AccessPointControllerImpl |
| 38 | implements NetworkController.AccessPointController, WifiListener { |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 39 | private static final String TAG = "AccessPointController"; |
| 40 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 41 | |
| 42 | // This string extra specifies a network to open the connect dialog on, so the user can enter |
| 43 | // network credentials. This is used by quick settings for secured networks. |
| 44 | private static final String EXTRA_START_CONNECT_SSID = "wifi_start_connect_ssid"; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 45 | |
| 46 | private static final int[] ICONS = { |
Jason Monk | 3d804a4 | 2015-05-19 13:46:00 -0400 | [diff] [blame] | 47 | R.drawable.ic_qs_wifi_full_0, |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 48 | R.drawable.ic_qs_wifi_full_1, |
| 49 | R.drawable.ic_qs_wifi_full_2, |
| 50 | R.drawable.ic_qs_wifi_full_3, |
| 51 | R.drawable.ic_qs_wifi_full_4, |
| 52 | }; |
| 53 | |
| 54 | private final Context mContext; |
| 55 | private final ArrayList<AccessPointCallback> mCallbacks = new ArrayList<AccessPointCallback>(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 56 | private final WifiTracker mWifiTracker; |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 57 | private final UserManager mUserManager; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 58 | |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 59 | private int mCurrentUser; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 60 | |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 61 | public AccessPointControllerImpl(Context context) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 62 | mContext = context; |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 63 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 64 | mWifiTracker = new WifiTracker(context, this, false, true); |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 65 | mCurrentUser = ActivityManager.getCurrentUser(); |
| 66 | } |
| 67 | |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 68 | @Override |
| 69 | protected void finalize() throws Throwable { |
| 70 | super.finalize(); |
| 71 | mWifiTracker.onDestroy(); |
| 72 | } |
| 73 | |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 74 | public boolean canConfigWifi() { |
| 75 | return !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI, |
| 76 | new UserHandle(mCurrentUser)); |
| 77 | } |
| 78 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 79 | public void onUserSwitched(int newUserId) { |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 80 | mCurrentUser = newUserId; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 83 | @Override |
| 84 | public void addAccessPointCallback(AccessPointCallback callback) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 85 | if (callback == null || mCallbacks.contains(callback)) return; |
| 86 | if (DEBUG) Log.d(TAG, "addCallback " + callback); |
| 87 | mCallbacks.add(callback); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 88 | if (mCallbacks.size() == 1) { |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 89 | mWifiTracker.onStart(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 90 | } |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 93 | @Override |
| 94 | public void removeAccessPointCallback(AccessPointCallback callback) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 95 | if (callback == null) return; |
| 96 | if (DEBUG) Log.d(TAG, "removeCallback " + callback); |
| 97 | mCallbacks.remove(callback); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 98 | if (mCallbacks.isEmpty()) { |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 99 | mWifiTracker.onStop(); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 100 | } |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 101 | } |
| 102 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 103 | @Override |
| 104 | public void scanForAccessPoints() { |
Sundeep Ghuman | c09b7f7 | 2017-07-26 20:23:32 -0700 | [diff] [blame] | 105 | if (DEBUG) Log.d(TAG, "force update APs!"); |
| 106 | mWifiTracker.forceUpdate(); |
| 107 | fireAcccessPointsCallback(mWifiTracker.getAccessPoints()); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public int getIcon(AccessPoint ap) { |
| 112 | int level = ap.getLevel(); |
| 113 | return ICONS[level >= 0 ? level : 0]; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 114 | } |
| 115 | |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 116 | public boolean connect(AccessPoint ap) { |
| 117 | if (ap == null) return false; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 118 | if (DEBUG) Log.d(TAG, "connect networkId=" + ap.getConfig().networkId); |
| 119 | if (ap.isSaved()) { |
| 120 | mWifiTracker.getManager().connect(ap.getConfig().networkId, mConnectListener); |
| 121 | } else { |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 122 | // Unknown network, need to add it. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 123 | if (ap.getSecurity() != AccessPoint.SECURITY_NONE) { |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 124 | Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS); |
Jason Monk | cc4b31e | 2015-06-18 12:37:03 -0400 | [diff] [blame] | 125 | intent.putExtra(EXTRA_START_CONNECT_SSID, ap.getSsidStr()); |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 126 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Jason Monk | 17f3c3f | 2015-01-14 10:13:22 -0500 | [diff] [blame] | 127 | fireSettingsIntentCallback(intent); |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 128 | return true; |
| 129 | } else { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 130 | ap.generateOpenNetworkConfig(); |
| 131 | mWifiTracker.getManager().connect(ap.getConfig(), mConnectListener); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 132 | } |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 133 | } |
| 134 | return false; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 135 | } |
| 136 | |
Jason Monk | 17f3c3f | 2015-01-14 10:13:22 -0500 | [diff] [blame] | 137 | private void fireSettingsIntentCallback(Intent intent) { |
| 138 | for (AccessPointCallback callback : mCallbacks) { |
| 139 | callback.onSettingsActivityTriggered(intent); |
| 140 | } |
| 141 | } |
| 142 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 143 | private void fireAcccessPointsCallback(List<AccessPoint> aps) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 144 | for (AccessPointCallback callback : mCallbacks) { |
| 145 | callback.onAccessPointsChanged(aps); |
| 146 | } |
| 147 | } |
| 148 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 149 | public void dump(PrintWriter pw) { |
| 150 | mWifiTracker.dump(pw); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 151 | } |
| 152 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 153 | @Override |
| 154 | public void onWifiStateChanged(int state) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 157 | @Override |
| 158 | public void onConnectedChanged() { |
| 159 | fireAcccessPointsCallback(mWifiTracker.getAccessPoints()); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 162 | @Override |
| 163 | public void onAccessPointsChanged() { |
| 164 | fireAcccessPointsCallback(mWifiTracker.getAccessPoints()); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 167 | private final ActionListener mConnectListener = new ActionListener() { |
| 168 | @Override |
| 169 | public void onSuccess() { |
| 170 | if (DEBUG) Log.d(TAG, "connect success"); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void onFailure(int reason) { |
| 175 | if (DEBUG) Log.d(TAG, "connect failure reason=" + reason); |
| 176 | } |
| 177 | }; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 178 | } |