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() { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 100 | if (DEBUG) Log.d(TAG, "scan!"); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 101 | mWifiTracker.forceScan(); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public int getIcon(AccessPoint ap) { |
| 106 | int level = ap.getLevel(); |
| 107 | return ICONS[level >= 0 ? level : 0]; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 110 | public boolean connect(AccessPoint ap) { |
| 111 | if (ap == null) return false; |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 112 | if (DEBUG) Log.d(TAG, "connect networkId=" + ap.getConfig().networkId); |
| 113 | if (ap.isSaved()) { |
| 114 | mWifiTracker.getManager().connect(ap.getConfig().networkId, mConnectListener); |
| 115 | } else { |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 116 | // Unknown network, need to add it. |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 117 | if (ap.getSecurity() != AccessPoint.SECURITY_NONE) { |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 118 | Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS); |
Jason Monk | cc4b31e | 2015-06-18 12:37:03 -0400 | [diff] [blame^] | 119 | intent.putExtra(EXTRA_START_CONNECT_SSID, ap.getSsidStr()); |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 120 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Jason Monk | 17f3c3f | 2015-01-14 10:13:22 -0500 | [diff] [blame] | 121 | fireSettingsIntentCallback(intent); |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 122 | return true; |
| 123 | } else { |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 124 | ap.generateOpenNetworkConfig(); |
| 125 | mWifiTracker.getManager().connect(ap.getConfig(), mConnectListener); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 126 | } |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 127 | } |
| 128 | return false; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 129 | } |
| 130 | |
Jason Monk | 17f3c3f | 2015-01-14 10:13:22 -0500 | [diff] [blame] | 131 | private void fireSettingsIntentCallback(Intent intent) { |
| 132 | for (AccessPointCallback callback : mCallbacks) { |
| 133 | callback.onSettingsActivityTriggered(intent); |
| 134 | } |
| 135 | } |
| 136 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 137 | private void fireAcccessPointsCallback(List<AccessPoint> aps) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 138 | for (AccessPointCallback callback : mCallbacks) { |
| 139 | callback.onAccessPointsChanged(aps); |
| 140 | } |
| 141 | } |
| 142 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 143 | public void dump(PrintWriter pw) { |
| 144 | mWifiTracker.dump(pw); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 147 | @Override |
| 148 | public void onWifiStateChanged(int state) { |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 149 | } |
| 150 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 151 | @Override |
| 152 | public void onConnectedChanged() { |
| 153 | fireAcccessPointsCallback(mWifiTracker.getAccessPoints()); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 156 | @Override |
| 157 | public void onAccessPointsChanged() { |
| 158 | fireAcccessPointsCallback(mWifiTracker.getAccessPoints()); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 161 | private final ActionListener mConnectListener = new ActionListener() { |
| 162 | @Override |
| 163 | public void onSuccess() { |
| 164 | if (DEBUG) Log.d(TAG, "connect success"); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void onFailure(int reason) { |
| 169 | if (DEBUG) Log.d(TAG, "connect failure reason=" + reason); |
| 170 | } |
| 171 | }; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 172 | } |