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