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