diff options
Diffstat (limited to 'app/src/fil/libre/repwifiapp/activities/ShowStatusActivity.java')
-rw-r--r-- | app/src/fil/libre/repwifiapp/activities/ShowStatusActivity.java | 253 |
1 files changed, 23 insertions, 230 deletions
diff --git a/app/src/fil/libre/repwifiapp/activities/ShowStatusActivity.java b/app/src/fil/libre/repwifiapp/activities/ShowStatusActivity.java index 21bfc4c..7076556 100644 --- a/app/src/fil/libre/repwifiapp/activities/ShowStatusActivity.java +++ b/app/src/fil/libre/repwifiapp/activities/ShowStatusActivity.java @@ -1,5 +1,5 @@ // -// Copyright 2017 Filippo "Fil" Bergamo <fil.bergamo@riseup.net> +// Copyright 2017, 2018 Filippo "Fil" Bergamo <fil.bergamo@riseup.net> // // This file is part of RepWifiApp. // @@ -20,274 +20,67 @@ package fil.libre.repwifiapp.activities; -import fil.libre.repwifiapp.ActivityLauncher; -import fil.libre.repwifiapp.Commons; -import fil.libre.repwifiapp.R; -import fil.libre.repwifiapp.ActivityLauncher.RequestCode; -import fil.libre.repwifiapp.helpers.AccessPointInfo; -import fil.libre.repwifiapp.helpers.ConnectionStatus; -import fil.libre.repwifiapp.helpers.OpenVpnManager; -import fil.libre.repwifiapp.helpers.Utils; -import android.content.Intent; import android.os.Bundle; -import android.os.RemoteException; import android.view.View; -import android.widget.Button; import android.widget.TextView; -import android.widget.Toast; +import fil.libre.repwifiapp.ActivityLauncher; +import fil.libre.repwifiapp.R; +import fil.libre.repwifiapp.helpers.Logger; +import fil.libre.repwifiapp.network.ConnectionStatus; public class ShowStatusActivity extends MenuEnabledActivity { private ConnectionStatus status; - // private AccessPointInfo info; - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show_status); if (getIntent().hasExtra(ActivityLauncher.EXTRA_CONSTATUS)) { - this.status = (ConnectionStatus) getIntent().getSerializableExtra( + this.status = (ConnectionStatus) getIntent().getParcelableExtra( ActivityLauncher.EXTRA_CONSTATUS); } try { - showStatus(false); + showStatus(); } catch (Exception e) { - Utils.logError("Exception on showStatus", e); + Logger.logError("Exception on showStatus", e); } } - @Override - public void onRestart() { - super.onRestart(); - try { - showStatus(true); - } catch (Exception e) { - Utils.logError("Exception on showStatus", e); - } - } - - @Override - public void onActivityResult(int requestCode, int resultCode, Intent intent) { - - switch (requestCode) { + private void showStatus() { - case RequestCode.VPN_PERMISSION: - - if (resultCode == RESULT_OK) { - endLaunchVpn(true); - } else { - endLaunchVpn(false); - } + if (this.status != null) { - break; + String txt = getPrintableConnectionStatus(status); - default: - - break; + TextView t = (TextView) findViewById(R.id.txt_status); + t.setText(txt); } } - private void setMessage(String msg) { - TextView view = (TextView) findViewById(R.id.txt_status); - view.setText(msg); + public void onBtnMainClick(View v) { + finish(); } - private void showStatus(boolean refresh) throws Exception { - - if (refresh || status == null) { - this.status = Commons.connectionEngine.getConnectionStatus(); - } + public String getPrintableConnectionStatus(ConnectionStatus status) { if (status == null) { - this.finish(); - - } else if (this.status.isConnected()) { - Utils.logDebug("StatusActivity isConnected,showing buttons"); - setMessage(getString(R.string.msg_connected_to) + " " + status.SSID + "\n\n" - + getString(R.string.text_ip_address) + ": " + status.IP + "\n"); - toggleBtnDisconnect(true); - beginLauncVpn(); - - } else { - Utils.logDebug("StatusActivity status Else"); - setMessage(getString(R.string.text_status) + ":\n" + status.status); - toggleBtnDisconnect(false); + return "[NULL]"; } - Commons.updateNotification(this); + String fmat = "%s: %s\n%s: %s\n%s: %s\n%s: %s\n%s: %s\n"; - } - - private void toggleBtnDisconnect(boolean enable) { - - Button b = (Button) findViewById(R.id.btn_disconnect); - Button bk = (Button) findViewById(R.id.btn_back); - b.setEnabled(enable); - bk.setEnabled(!enable); - - if (enable) { - b.setVisibility(View.VISIBLE); - bk.setVisibility(View.INVISIBLE); - } else { - b.setVisibility(View.INVISIBLE); - bk.setVisibility(View.VISIBLE); - } + return String.format(fmat, getString(R.string.text_ip_address), status.IP, + getString(R.string.text_subnet_mask), status.subnetMask, + getString(R.string.text_gateway), status.gateway, + getString(R.string.text_broadcast_address), status.broadcastAddress, + getString(R.string.text_hardware_address), status.hwAddress); } - public void onBtnDisconnectClick(View v) { - - disconnectVpn(); - - boolean res = Commons.connectionEngine.disconnect(); - String msg = ""; - if (res) { - msg = getString(R.string.msg_disconnected); - } else { - msg = getString(R.string.msg_disconnect_fail); - } - - Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT); - toast.show(); - - try { - showStatus(true); - } catch (Exception e) { - Utils.logError("Exception on showStatus", e); - } - - } - - private void disconnectVpn() { - - if (! OpenVpnManager.isVpnConnected()){ - return; - } - - if (OpenVpnManager.disconnect()) { - Toast t = Toast.makeText(this, getString(R.string.msg_vpn_disconnect), - Toast.LENGTH_LONG); - t.show(); - - } else { - Commons.showMessage(getString(R.string.msg_vpn_disconnect_error), this); - } - - } - - private void beginLauncVpn() throws Exception { - - if (OpenVpnManager.isVpnConnected()){ - // already connected; - return; - } - - if (!OpenVpnManager.isExternalAppInstalled(this)) { - Utils.logDebug("External VPN app not installed."); - return; - } - - if (getVpnNameIfAny() == null){ - Utils.logDebug("No vpn profile set. Exiting beginLaunchVpn()"); - return; - } - - // first, we make sure we have permission to use the vpn service. - Intent pi; - - try { - pi = OpenVpnManager.askApiPermissionsGetIntent(); - } catch (RemoteException e) { - Utils.logError("Exception while asking for VPN permission", e); - Toast t = Toast.makeText(getApplicationContext(), getString(R.string.msg_vpn_connect_error), Toast.LENGTH_LONG); - t.show(); - return; - - } - - if (pi == null){ - // no need to ask for permission - Utils.logDebug("No need for vpn permission: going to endLaunchVpn."); - endLaunchVpn(true); - - } else{ - // launch the intent to ask permission - Utils.logDebug("Need to ask for vpn permission. Starting intent.."); - startActivityForResult(pi, ActivityLauncher.RequestCode.VPN_PERMISSION); - } - - } - - private void endLaunchVpn(boolean permissionGranted) { - - try { - - if (!permissionGranted) { - // warn user that permission must be granted - Utils.logDebug("User rejected vpn permission."); - String msg = getString(R.string.msg_vpn_no_permission).replace( - OpenVpnManager.PLACEHOLDER_APPNAME, OpenVpnManager.APP_COMMON_NAME); - Commons.showMessage(msg, this); - return; - } - - String profname = getVpnNameIfAny(); - - // check if profile exists - String profUuid = OpenVpnManager.getUuidFromName(profname); - if (profUuid == null){ - // warn user that selected profile doesn't exist - Commons.showMessage(getString(R.string.msg_vpn_wrong_profile), this); - return; - } - - if (OpenVpnManager.startVpn(profUuid)){ - Toast t = Toast.makeText(this, getString(R.string.msg_vpn_launched), Toast.LENGTH_LONG); - t.show(); - } else { - Commons.showMessage(getString(R.string.msg_vpn_connect_error),this); - } - - } catch (Exception e) { - Utils.logError("Exception while endLaunchVpn", e); - - } - - } - - private String getVpnNameIfAny(){ - - if (status == null) { - return null; - } - - AccessPointInfo i = status.getNetworkDetails(); - if (i == null) { - return null; - } - - String profname = i.getVpnProfileName(); - if (profname == null || profname.isEmpty()) { - return null; - } else { - return profname; - } - - } - - public void onBtnMainClick(View v) { - finish(); - } - - @Override - public void onBackPressed() { - moveTaskToBack(true); - } - } |