diff options
Diffstat (limited to 'app/src/fil/libre/repwifiapp/activities/Ipv4SettingsActivity.java')
-rw-r--r-- | app/src/fil/libre/repwifiapp/activities/Ipv4SettingsActivity.java | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/app/src/fil/libre/repwifiapp/activities/Ipv4SettingsActivity.java b/app/src/fil/libre/repwifiapp/activities/Ipv4SettingsActivity.java new file mode 100644 index 0000000..1f15252 --- /dev/null +++ b/app/src/fil/libre/repwifiapp/activities/Ipv4SettingsActivity.java @@ -0,0 +1,181 @@ +// +// Copyright 2017 Filippo "Fil" Bergamo <fil.bergamo@riseup.net> +// +// This file is part of RepWifiApp. +// +// RepWifiApp is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// RepWifiApp is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with RepWifiApp. If not, see <http://www.gnu.org/licenses/>. +// +// ******************************************************************** + +package fil.libre.repwifiapp.activities; + +import fil.libre.repwifiapp.ActivityLauncher; +import fil.libre.repwifiapp.Commons; +import fil.libre.repwifiapp.R; +import fil.libre.repwifiapp.helpers.AccessPointInfo; +import fil.libre.repwifiapp.helpers.DhcpSettings; +import android.nfc.FormatException; +import android.os.Bundle; +import android.app.Activity; +import android.content.Intent; +import android.view.Menu; +import android.view.View; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.EditText; +import android.widget.CompoundButton.OnCheckedChangeListener; + +public class Ipv4SettingsActivity extends Activity implements OnCheckedChangeListener { + + private AccessPointInfo currentNetwork; + private DhcpSettings currentSettings; + + private EditText txtIp; + private EditText txtGw; + private EditText txtMask; + private CheckBox chkDhcp; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_ipv4_settings); + + chkDhcp = (CheckBox) findViewById(R.id.chk_use_dhcp); + txtIp = (EditText) findViewById(R.id.txt_static_ip); + txtMask = (EditText) findViewById(R.id.txt_netmask); + txtGw = (EditText) findViewById(R.id.txt_gateway); + + chkDhcp.setOnCheckedChangeListener(this); + + Intent intent = getIntent(); + if (!intent.hasExtra(ActivityLauncher.EXTRA_APINFO)) { + this.setResult(RESULT_CANCELED); + this.finish(); + return; + } + + this.currentNetwork = (AccessPointInfo) intent.getExtras().getSerializable( + ActivityLauncher.EXTRA_APINFO); + if (this.currentNetwork == null) { + this.setResult(RESULT_CANCELED); + this.finish(); + return; + } + + this.currentNetwork = Commons.storage.getSavedNetwork(currentNetwork); + + loadNetwork(); + + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + // getMenuInflater().inflate(R.menu.activity_manage_networks, menu); + return true; + } + + private void loadNetwork() { + + setTitle(this.currentNetwork.getSsid()); + currentSettings = this.currentNetwork.getDhcpConfiguration(); + loadSettings(); + + } + + private void loadSettings() { + + if (currentSettings.useDhcp) { + + chkDhcp.setChecked(true); + + txtIp.setText(""); + txtIp.setEnabled(false); + + txtMask.setText(""); + txtMask.setEnabled(false); + + txtGw.setText(""); + txtGw.setEnabled(false); + + } else { + chkDhcp.setChecked(false); + + txtIp.setText(currentSettings.getStaticIP()); + txtIp.setEnabled(true); + + txtMask.setText(currentSettings.getSubnetMaskString()); + txtMask.setEnabled(true); + + txtGw.setText(currentSettings.getDefaultGateway()); + txtGw.setEnabled(true); + } + + } + + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + + if (buttonView == findViewById(R.id.chk_use_dhcp)) { + currentSettings.useDhcp = isChecked; + loadSettings(); + + } + + } + + public void btnSaveClick(View v) throws FormatException { + + if (chkDhcp.isChecked()) { + currentSettings.useDhcp = true; + + } else { + + String ip = txtIp.getText().toString(); + String mask = txtMask.getText().toString(); + String gw = txtGw.getText().toString(); + + if (!DhcpSettings.isValidAddress(ip)) { + Commons.showMessage(getString(R.string.msg_invalid_ip),this); + return; + } + + if (!DhcpSettings.isValidMaks(mask)) { + Commons.showMessage(getString(R.string.msg_invalid_netmask),this); + return; + } + + if (!DhcpSettings.isValidAddress(gw)) { + Commons.showMessage(getString(R.string.msg_invalid_gateway),this); + return; + } + + currentSettings = new DhcpSettings(chkDhcp.isChecked(), ip, mask, gw); + + } + + currentNetwork.setDhcpConfiguration(currentSettings); + if (Commons.storage.save(currentNetwork)) { + Commons.showMessage(getString(R.string.msg_network_saved),this); + } else { + Commons.showMessage(getString(R.string.msg_network_save_fail),this); + } + + } + + public void btnBackClick(View v) { + finish(); + } + +} |