1 /* 2 * Copyright (C) 2017 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.tv.settings.connectivity.util; 18 19 import android.net.IpConfiguration; 20 import android.net.LinkAddress; 21 import android.net.NetworkUtils; 22 import android.net.ProxyInfo; 23 import android.net.StaticIpConfiguration; 24 import android.text.TextUtils; 25 26 import androidx.fragment.app.FragmentActivity; 27 import androidx.lifecycle.ViewModelProviders; 28 29 import com.android.tv.settings.R; 30 import com.android.tv.settings.connectivity.WifiConfigHelper; 31 import com.android.tv.settings.connectivity.setup.AdvancedOptionsFlowInfo; 32 33 import java.net.Inet4Address; 34 35 /** 36 * Class that helps process proxy and IP settings. 37 */ 38 public class AdvancedOptionsFlowUtil { 39 40 /** 41 * Process Proxy Settings. 42 * 43 * @param activity the activity that calls this method. 44 * @return 0 if successful, or other different values based on the result. 45 */ processProxySettings(FragmentActivity activity)46 public static int processProxySettings(FragmentActivity activity) { 47 AdvancedOptionsFlowInfo flowInfo = ViewModelProviders 48 .of(activity) 49 .get(AdvancedOptionsFlowInfo.class); 50 IpConfiguration mIpConfiguration = flowInfo.getIpConfiguration(); 51 boolean hasProxySettings = 52 (!flowInfo.containsPage(AdvancedOptionsFlowInfo.ADVANCED_OPTIONS) 53 || !flowInfo.choiceChosen( 54 activity.getString(R.string.wifi_action_advanced_no), 55 AdvancedOptionsFlowInfo.ADVANCED_OPTIONS)) 56 && !flowInfo.choiceChosen(activity.getString(R.string.wifi_action_proxy_none), 57 AdvancedOptionsFlowInfo.PROXY_SETTINGS); 58 mIpConfiguration.setProxySettings(hasProxySettings 59 ? IpConfiguration.ProxySettings.STATIC : IpConfiguration.ProxySettings.NONE); 60 if (hasProxySettings) { 61 String host = flowInfo.get(AdvancedOptionsFlowInfo.PROXY_HOSTNAME); 62 String portStr = flowInfo.get(AdvancedOptionsFlowInfo.PROXY_PORT); 63 String exclusionList = flowInfo.get(AdvancedOptionsFlowInfo.PROXY_BYPASS); 64 int port = 0; 65 int result; 66 try { 67 port = Integer.parseInt(portStr); 68 result = WifiConfigHelper.validate(host, portStr, exclusionList); 69 } catch (NumberFormatException e) { 70 result = R.string.proxy_error_invalid_port; 71 } 72 if (result == 0) { 73 mIpConfiguration.setHttpProxy(new ProxyInfo(host, port, exclusionList)); 74 } else { 75 return result; 76 } 77 } else { 78 mIpConfiguration.setHttpProxy(null); 79 } 80 return 0; 81 } 82 83 /** 84 * Process Ip Settings. 85 * 86 * @param activity the activity that calls this method. 87 * @return 0 if successful, or other different values based on the result. 88 */ processIpSettings(FragmentActivity activity)89 public static int processIpSettings(FragmentActivity activity) { 90 AdvancedOptionsFlowInfo flowInfo = ViewModelProviders 91 .of(activity) 92 .get(AdvancedOptionsFlowInfo.class); 93 IpConfiguration mIpConfiguration = flowInfo.getIpConfiguration(); 94 boolean hasIpSettings = 95 (!flowInfo.containsPage(AdvancedOptionsFlowInfo.ADVANCED_OPTIONS) 96 || !flowInfo.choiceChosen( 97 activity.getString(R.string.wifi_action_advanced_no), 98 AdvancedOptionsFlowInfo.ADVANCED_OPTIONS)) 99 && !flowInfo.choiceChosen(activity.getString(R.string.wifi_action_dhcp), 100 AdvancedOptionsFlowInfo.IP_SETTINGS); 101 mIpConfiguration.setIpAssignment(hasIpSettings 102 ? IpConfiguration.IpAssignment.STATIC 103 : IpConfiguration.IpAssignment.DHCP); 104 if (hasIpSettings) { 105 StaticIpConfiguration staticConfig = new StaticIpConfiguration(); 106 mIpConfiguration.setStaticIpConfiguration(staticConfig); 107 String ipAddr = flowInfo.get(AdvancedOptionsFlowInfo.IP_ADDRESS); 108 if (TextUtils.isEmpty(ipAddr)) { 109 return R.string.wifi_ip_settings_invalid_ip_address; 110 } 111 Inet4Address inetAddr; 112 try { 113 inetAddr = (Inet4Address) NetworkUtils.numericToInetAddress(ipAddr); 114 } catch (IllegalArgumentException | ClassCastException e) { 115 return R.string.wifi_ip_settings_invalid_ip_address; 116 } 117 118 try { 119 int networkPrefixLength = Integer.parseInt(flowInfo.get( 120 AdvancedOptionsFlowInfo.NETWORK_PREFIX_LENGTH)); 121 if (networkPrefixLength < 0 || networkPrefixLength > 32) { 122 return R.string.wifi_ip_settings_invalid_network_prefix_length; 123 } 124 staticConfig.ipAddress = new LinkAddress(inetAddr, networkPrefixLength); 125 } catch (NumberFormatException e) { 126 return R.string.wifi_ip_settings_invalid_ip_address; 127 } 128 129 String gateway = flowInfo.get(AdvancedOptionsFlowInfo.GATEWAY); 130 if (!TextUtils.isEmpty(gateway)) { 131 try { 132 staticConfig.gateway = 133 NetworkUtils.numericToInetAddress(gateway); 134 } catch (IllegalArgumentException | ClassCastException e) { 135 return R.string.wifi_ip_settings_invalid_gateway; 136 } 137 } 138 139 String dns1 = flowInfo.get(AdvancedOptionsFlowInfo.DNS1); 140 if (!TextUtils.isEmpty(dns1)) { 141 try { 142 staticConfig.dnsServers.add( 143 NetworkUtils.numericToInetAddress(dns1)); 144 } catch (IllegalArgumentException | ClassCastException e) { 145 return R.string.wifi_ip_settings_invalid_dns; 146 } 147 } 148 149 String dns2 = flowInfo.get(AdvancedOptionsFlowInfo.DNS2); 150 if (!TextUtils.isEmpty(dns2)) { 151 try { 152 staticConfig.dnsServers.add( 153 NetworkUtils.numericToInetAddress(dns2)); 154 } catch (IllegalArgumentException | ClassCastException e) { 155 return R.string.wifi_ip_settings_invalid_dns; 156 } 157 } 158 } else { 159 mIpConfiguration.setStaticIpConfiguration(null); 160 } 161 return 0; 162 } 163 } 164