Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.server.net; |
| 18 | |
Felipe Leme | 9982b30 | 2016-03-01 13:40:31 -0800 | [diff] [blame] | 19 | import static android.net.NetworkPolicyManager.POLICY_NONE; |
| 20 | import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND; |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 21 | import static android.net.wifi.WifiInfo.removeDoubleQuotes; |
| 22 | import static com.android.server.net.NetworkPolicyManagerService.newWifiPolicy; |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 23 | import static com.android.server.net.NetworkPolicyManagerService.TAG; |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 24 | |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 25 | import java.io.PrintWriter; |
| 26 | import java.util.ArrayList; |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 27 | import java.util.HashSet; |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 28 | import java.util.List; |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 29 | import java.util.Set; |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 30 | |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 31 | import android.content.Context; |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 32 | import android.net.INetworkPolicyManager; |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 33 | import android.net.NetworkPolicy; |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 34 | import android.net.NetworkTemplate; |
| 35 | import android.net.wifi.WifiConfiguration; |
| 36 | import android.net.wifi.WifiManager; |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 37 | import android.os.Binder; |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 38 | import android.os.RemoteException; |
| 39 | import android.os.ShellCommand; |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 40 | import android.util.Log; |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 41 | |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 42 | class NetworkPolicyManagerShellCommand extends ShellCommand { |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 43 | |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 44 | private final INetworkPolicyManager mInterface; |
| 45 | private final WifiManager mWifiManager; |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 46 | |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 47 | NetworkPolicyManagerShellCommand(Context context, INetworkPolicyManager service) { |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 48 | mInterface = service; |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 49 | mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public int onCommand(String cmd) { |
| 54 | if (cmd == null) { |
| 55 | return handleDefaultCommands(cmd); |
| 56 | } |
| 57 | final PrintWriter pw = getOutPrintWriter(); |
| 58 | try { |
| 59 | switch(cmd) { |
| 60 | case "get": |
| 61 | return runGet(); |
| 62 | case "set": |
| 63 | return runSet(); |
| 64 | case "list": |
| 65 | return runList(); |
| 66 | case "add": |
| 67 | return runAdd(); |
| 68 | case "remove": |
| 69 | return runRemove(); |
| 70 | default: |
| 71 | return handleDefaultCommands(cmd); |
| 72 | } |
| 73 | } catch (RemoteException e) { |
| 74 | pw.println("Remote exception: " + e); |
| 75 | } |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void onHelp() { |
| 81 | final PrintWriter pw = getOutPrintWriter(); |
| 82 | pw.println("Network policy manager (netpolicy) commands:"); |
| 83 | pw.println(" help"); |
| 84 | pw.println(" Print this help text."); |
| 85 | pw.println(""); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 86 | pw.println(" add restrict-background-whitelist UID"); |
| 87 | pw.println(" Adds a UID to the whitelist for restrict background usage."); |
Felipe Leme | 9982b30 | 2016-03-01 13:40:31 -0800 | [diff] [blame] | 88 | pw.println(" add restrict-background-blacklist UID"); |
| 89 | pw.println(" Adds a UID to the blacklist for restrict background usage."); |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 90 | pw.println(" get metered-network ID"); |
| 91 | pw.println(" Checks whether the given non-mobile network is metered or not."); |
| 92 | pw.println(" get restrict-background"); |
| 93 | pw.println(" Gets the global restrict background usage status."); |
| 94 | pw.println(" list metered-networks [BOOLEAN]"); |
| 95 | pw.println(" Lists all non-mobile networks and whether they are metered or not."); |
| 96 | pw.println(" If a boolean argument is passed, filters just the metered (or unmetered)"); |
| 97 | pw.println(" networks."); |
| 98 | pw.println(" list restrict-background-whitelist"); |
| 99 | pw.println(" Lists UIDs that are whitelisted for restrict background usage."); |
Felipe Leme | 9982b30 | 2016-03-01 13:40:31 -0800 | [diff] [blame] | 100 | pw.println(" list restrict-background-blacklist"); |
| 101 | pw.println(" Lists UIDs that are blacklisted for restrict background usage."); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 102 | pw.println(" remove restrict-background-whitelist UID"); |
| 103 | pw.println(" Removes a UID from the whitelist for restrict background usage."); |
Felipe Leme | 9982b30 | 2016-03-01 13:40:31 -0800 | [diff] [blame] | 104 | pw.println(" remove restrict-background-blacklist UID"); |
| 105 | pw.println(" Removes a UID from the blacklist for restrict background usage."); |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 106 | pw.println(" set metered-network ID BOOLEAN"); |
| 107 | pw.println(" Toggles whether the given non-mobile network is metered."); |
| 108 | pw.println(" set restrict-background BOOLEAN"); |
| 109 | pw.println(" Sets the global restrict background usage status."); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | private int runGet() throws RemoteException { |
| 113 | final PrintWriter pw = getOutPrintWriter(); |
| 114 | final String type = getNextArg(); |
| 115 | if (type == null) { |
| 116 | pw.println("Error: didn't specify type of data to get"); |
| 117 | return -1; |
| 118 | } |
| 119 | switch(type) { |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 120 | case "metered-network": |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 121 | return getMeteredWifiNetwork(); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 122 | case "restrict-background": |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 123 | return getRestrictBackground(); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 124 | } |
| 125 | pw.println("Error: unknown get type '" + type + "'"); |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | private int runSet() throws RemoteException { |
| 130 | final PrintWriter pw = getOutPrintWriter(); |
| 131 | final String type = getNextArg(); |
| 132 | if (type == null) { |
| 133 | pw.println("Error: didn't specify type of data to set"); |
| 134 | return -1; |
| 135 | } |
| 136 | switch(type) { |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 137 | case "metered-network": |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 138 | return setMeteredWifiNetwork(); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 139 | case "restrict-background": |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 140 | return setRestrictBackground(); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 141 | } |
| 142 | pw.println("Error: unknown set type '" + type + "'"); |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | private int runList() throws RemoteException { |
| 147 | final PrintWriter pw = getOutPrintWriter(); |
| 148 | final String type = getNextArg(); |
| 149 | if (type == null) { |
| 150 | pw.println("Error: didn't specify type of data to list"); |
| 151 | return -1; |
| 152 | } |
| 153 | switch(type) { |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 154 | case "metered-networks": |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 155 | return listMeteredWifiNetworks(); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 156 | case "restrict-background-whitelist": |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 157 | return listRestrictBackgroundWhitelist(); |
Felipe Leme | 9982b30 | 2016-03-01 13:40:31 -0800 | [diff] [blame] | 158 | case "restrict-background-blacklist": |
| 159 | return listRestrictBackgroundBlacklist(); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 160 | } |
| 161 | pw.println("Error: unknown list type '" + type + "'"); |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | private int runAdd() throws RemoteException { |
| 166 | final PrintWriter pw = getOutPrintWriter(); |
| 167 | final String type = getNextArg(); |
| 168 | if (type == null) { |
| 169 | pw.println("Error: didn't specify type of data to add"); |
| 170 | return -1; |
| 171 | } |
| 172 | switch(type) { |
| 173 | case "restrict-background-whitelist": |
| 174 | return addRestrictBackgroundWhitelist(); |
Felipe Leme | 9982b30 | 2016-03-01 13:40:31 -0800 | [diff] [blame] | 175 | case "restrict-background-blacklist": |
| 176 | return addRestrictBackgroundBlacklist(); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 177 | } |
| 178 | pw.println("Error: unknown add type '" + type + "'"); |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | private int runRemove() throws RemoteException { |
| 183 | final PrintWriter pw = getOutPrintWriter(); |
| 184 | final String type = getNextArg(); |
| 185 | if (type == null) { |
| 186 | pw.println("Error: didn't specify type of data to remove"); |
| 187 | return -1; |
| 188 | } |
| 189 | switch(type) { |
| 190 | case "restrict-background-whitelist": |
| 191 | return removeRestrictBackgroundWhitelist(); |
Felipe Leme | 9982b30 | 2016-03-01 13:40:31 -0800 | [diff] [blame] | 192 | case "restrict-background-blacklist": |
| 193 | return removeRestrictBackgroundBlacklist(); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 194 | } |
| 195 | pw.println("Error: unknown remove type '" + type + "'"); |
| 196 | return -1; |
| 197 | } |
| 198 | |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 199 | private int listRestrictBackgroundWhitelist() throws RemoteException { |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 200 | final PrintWriter pw = getOutPrintWriter(); |
| 201 | final int[] uids = mInterface.getRestrictBackgroundWhitelistedUids(); |
| 202 | pw.print("Restrict background whitelisted UIDs: "); |
| 203 | if (uids.length == 0) { |
| 204 | pw.println("none"); |
| 205 | } else { |
| 206 | for (int i = 0; i < uids.length; i++) { |
| 207 | int uid = uids[i]; |
| 208 | pw.print(uid); |
| 209 | pw.print(' '); |
| 210 | } |
| 211 | } |
| 212 | pw.println(); |
| 213 | return 0; |
| 214 | } |
| 215 | |
Felipe Leme | 9982b30 | 2016-03-01 13:40:31 -0800 | [diff] [blame] | 216 | private int listRestrictBackgroundBlacklist() throws RemoteException { |
| 217 | final PrintWriter pw = getOutPrintWriter(); |
| 218 | |
| 219 | final int[] uids = mInterface.getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND); |
| 220 | pw.print("Restrict background blacklisted UIDs: "); |
| 221 | if (uids.length == 0) { |
| 222 | pw.println("none"); |
| 223 | } else { |
| 224 | for (int i = 0; i < uids.length; i++) { |
| 225 | int uid = uids[i]; |
| 226 | pw.print(uid); |
| 227 | pw.print(' '); |
| 228 | } |
| 229 | } |
| 230 | pw.println(); |
| 231 | return 0; |
| 232 | } |
| 233 | |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 234 | private int getRestrictBackground() throws RemoteException { |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 235 | final PrintWriter pw = getOutPrintWriter(); |
| 236 | pw.print("Restrict background status: "); |
| 237 | pw.println(mInterface.getRestrictBackground() ? "enabled" : "disabled"); |
| 238 | return 0; |
| 239 | } |
| 240 | |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 241 | private int setRestrictBackground() throws RemoteException { |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 242 | final int enabled = getNextBooleanArg(); |
| 243 | if (enabled < 0) { |
| 244 | return enabled; |
| 245 | } |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 246 | mInterface.setRestrictBackground(enabled > 0); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | private int addRestrictBackgroundWhitelist() throws RemoteException { |
| 251 | final int uid = getUidFromNextArg(); |
| 252 | if (uid < 0) { |
| 253 | return uid; |
| 254 | } |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 255 | mInterface.addRestrictBackgroundWhitelistedUid(uid); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | private int removeRestrictBackgroundWhitelist() throws RemoteException { |
| 260 | final int uid = getUidFromNextArg(); |
| 261 | if (uid < 0) { |
| 262 | return uid; |
| 263 | } |
Felipe Leme | 6a05eee | 2016-02-19 14:43:51 -0800 | [diff] [blame] | 264 | mInterface.removeRestrictBackgroundWhitelistedUid(uid); |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
Felipe Leme | 9982b30 | 2016-03-01 13:40:31 -0800 | [diff] [blame] | 268 | private int addRestrictBackgroundBlacklist() throws RemoteException { |
| 269 | final int uid = getUidFromNextArg(); |
| 270 | if (uid < 0) { |
| 271 | return uid; |
| 272 | } |
| 273 | mInterface.setUidPolicy(uid, POLICY_REJECT_METERED_BACKGROUND); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | private int removeRestrictBackgroundBlacklist() throws RemoteException { |
| 278 | final int uid = getUidFromNextArg(); |
| 279 | if (uid < 0) { |
| 280 | return uid; |
| 281 | } |
| 282 | mInterface.setUidPolicy(uid, POLICY_NONE); |
| 283 | return 0; |
| 284 | } |
| 285 | |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 286 | private int listMeteredWifiNetworks() throws RemoteException { |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 287 | final PrintWriter pw = getOutPrintWriter(); |
| 288 | final String arg = getNextArg(); |
| 289 | final Boolean filter = arg == null ? null : Boolean.valueOf(arg); |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 290 | for (NetworkPolicy policy : getWifiPolicies()) { |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 291 | if (filter != null && filter.booleanValue() != policy.metered) { |
| 292 | continue; |
| 293 | } |
| 294 | pw.print(getNetworkId(policy)); |
| 295 | pw.print(';'); |
| 296 | pw.println(policy.metered); |
| 297 | } |
| 298 | return 0; |
| 299 | } |
| 300 | |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 301 | private int getMeteredWifiNetwork() throws RemoteException { |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 302 | final PrintWriter pw = getOutPrintWriter(); |
| 303 | final String id = getNextArg(); |
| 304 | if (id == null) { |
| 305 | pw.println("Error: didn't specify ID"); |
| 306 | return -1; |
| 307 | } |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 308 | final List<NetworkPolicy> policies = getWifiPolicies(); |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 309 | for (NetworkPolicy policy: policies) { |
| 310 | if (id.equals(getNetworkId(policy))) { |
| 311 | pw.println(policy.metered); |
| 312 | return 0; |
| 313 | } |
| 314 | } |
| 315 | return 0; |
| 316 | } |
| 317 | |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 318 | private int setMeteredWifiNetwork() throws RemoteException { |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 319 | final PrintWriter pw = getOutPrintWriter(); |
| 320 | final String id = getNextArg(); |
| 321 | if (id == null) { |
| 322 | pw.println("Error: didn't specify ID"); |
| 323 | return -1; |
| 324 | } |
| 325 | final String arg = getNextArg(); |
| 326 | if (arg == null) { |
| 327 | pw.println("Error: didn't specify BOOLEAN"); |
| 328 | return -1; |
| 329 | } |
| 330 | final boolean metered = Boolean.valueOf(arg); |
| 331 | final NetworkPolicy[] policies = mInterface.getNetworkPolicies(null); |
| 332 | boolean changed = false; |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 333 | // First try to find a policy with such id |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 334 | for (NetworkPolicy policy : policies) { |
| 335 | if (policy.template.isMatchRuleMobile() || policy.metered == metered) { |
| 336 | continue; |
| 337 | } |
| 338 | final String networkId = getNetworkId(policy); |
| 339 | if (id.equals(networkId)) { |
| 340 | Log.i(TAG, "Changing " + networkId + " metered status to " + metered); |
| 341 | policy.metered = metered; |
| 342 | changed = true; |
| 343 | } |
| 344 | } |
| 345 | if (changed) { |
| 346 | mInterface.setNetworkPolicies(policies); |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | // Policy not found: check if there is a saved wi-fi with such id. |
| 350 | for (WifiConfiguration config : mWifiManager.getConfiguredNetworks()) { |
| 351 | final String ssid = removeDoubleQuotes(config.SSID); |
| 352 | if (id.equals(ssid)) { |
| 353 | final NetworkPolicy policy = newPolicy(ssid); |
| 354 | Log.i(TAG, "Creating new policy for " + ssid + ": " + policy); |
| 355 | final NetworkPolicy[] newPolicies = new NetworkPolicy[policies.length + 1]; |
| 356 | System.arraycopy(policies, 0, newPolicies, 0, policies.length); |
| 357 | newPolicies[newPolicies.length - 1] = policy; |
| 358 | mInterface.setNetworkPolicies(newPolicies); |
| 359 | } |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 360 | } |
| 361 | return 0; |
| 362 | } |
| 363 | |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 364 | private List<NetworkPolicy> getWifiPolicies() throws RemoteException { |
| 365 | // First gets a list of saved wi-fi networks. |
| 366 | final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks(); |
| 367 | final Set<String> ssids = new HashSet<>(configs.size()); |
| 368 | for (WifiConfiguration config : configs) { |
| 369 | ssids.add(removeDoubleQuotes(config.SSID)); |
| 370 | } |
| 371 | |
| 372 | // Then gets the saved policies. |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 373 | final NetworkPolicy[] policies = mInterface.getNetworkPolicies(null); |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 374 | final List<NetworkPolicy> wifiPolicies = new ArrayList<NetworkPolicy>(policies.length); |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 375 | for (NetworkPolicy policy: policies) { |
| 376 | if (!policy.template.isMatchRuleMobile()) { |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 377 | wifiPolicies.add(policy); |
| 378 | final String netId = getNetworkId(policy); |
| 379 | ssids.remove(netId); |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 380 | } |
| 381 | } |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 382 | // Finally, creates new default policies for saved WI-FIs not policied yet. |
| 383 | for (String ssid : ssids) { |
| 384 | final NetworkPolicy policy = newPolicy(ssid); |
| 385 | wifiPolicies.add(policy); |
| 386 | } |
| 387 | return wifiPolicies; |
| 388 | } |
| 389 | |
| 390 | private NetworkPolicy newPolicy(String ssid) { |
| 391 | final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(ssid); |
| 392 | final NetworkPolicy policy = newWifiPolicy(template, false); |
| 393 | return policy; |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | private String getNetworkId(NetworkPolicy policy) { |
Felipe Leme | b1a65ee | 2016-02-08 10:12:01 -0800 | [diff] [blame] | 397 | return removeDoubleQuotes(policy.template.getNetworkId()); |
Felipe Leme | de4e8e3 | 2016-02-02 18:55:22 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Felipe Leme | 50a235e | 2016-01-15 18:37:06 -0800 | [diff] [blame] | 400 | private int getNextBooleanArg() { |
| 401 | final PrintWriter pw = getOutPrintWriter(); |
| 402 | final String arg = getNextArg(); |
| 403 | if (arg == null) { |
| 404 | pw.println("Error: didn't specify BOOLEAN"); |
| 405 | return -1; |
| 406 | } |
| 407 | return Boolean.valueOf(arg) ? 1 : 0; |
| 408 | } |
| 409 | |
| 410 | private int getUidFromNextArg() { |
| 411 | final PrintWriter pw = getOutPrintWriter(); |
| 412 | final String arg = getNextArg(); |
| 413 | if (arg == null) { |
| 414 | pw.println("Error: didn't specify UID"); |
| 415 | return -1; |
| 416 | } |
| 417 | try { |
| 418 | return Integer.parseInt(arg); |
| 419 | } catch (NumberFormatException e) { |
| 420 | pw.println("Error: UID (" + arg + ") should be a number"); |
| 421 | return -2; |
| 422 | } |
| 423 | } |
| 424 | } |