diff options
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/Dashboard.java')
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/Dashboard.java | 177 |
1 files changed, 95 insertions, 82 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index 755aaf33..901dee89 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -17,9 +17,7 @@ package se.leap.bitmaskclient; import android.annotation.SuppressLint; -import android.app.Activity; import android.app.AlertDialog; -import android.app.FragmentTransaction; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -27,6 +25,7 @@ import android.content.SharedPreferences; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.os.Handler; +import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.view.Menu; import android.view.MenuItem; @@ -42,15 +41,25 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import butterknife.ButterKnife; import butterknife.InjectView; +import se.leap.bitmaskclient.fragments.AboutFragment; import de.blinkt.openvpn.core.VpnStatus; import se.leap.bitmaskclient.userstatus.SessionDialog; import se.leap.bitmaskclient.userstatus.User; import se.leap.bitmaskclient.userstatus.UserStatusFragment; +import static se.leap.bitmaskclient.Constants.APP_ACTION_CONFIGURE_ALWAYS_ON_PROFILE; +import static se.leap.bitmaskclient.Constants.APP_ACTION_QUIT; import static se.leap.bitmaskclient.Constants.EIP_IS_ALWAYS_ON; import static se.leap.bitmaskclient.Constants.EIP_RESTART_ON_BOOT; +import static se.leap.bitmaskclient.Constants.PREFERENCES_APP_VERSION; +import static se.leap.bitmaskclient.Constants.PROVIDER_ALLOW_ANONYMOUS; +import static se.leap.bitmaskclient.Constants.PROVIDER_CONFIGURED; +import static se.leap.bitmaskclient.Constants.PROVIDER_KEY; +import static se.leap.bitmaskclient.Constants.REQUEST_CODE_CONFIGURE_LEAP; +import static se.leap.bitmaskclient.Constants.REQUEST_CODE_KEY; +import static se.leap.bitmaskclient.Constants.REQUEST_CODE_SWITCH_PROVIDER; +import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; /** * The main user facing Activity of Bitmask Android, consisting of status, controls, @@ -59,13 +68,9 @@ import static se.leap.bitmaskclient.Constants.EIP_RESTART_ON_BOOT; * @author Sean Leonard <meanderingcode@aetherislands.net> * @author parmegv */ -public class Dashboard extends Activity implements ProviderAPIResultReceiver.Receiver { - - protected static final int CONFIGURE_LEAP = 0; - protected static final int SWITCH_PROVIDER = 1; +public class Dashboard extends ButterKnifeActivity { public static final String TAG = Dashboard.class.getSimpleName(); - public static final String ACTION_QUIT = "quit"; /** * When "Disconnect" is clicked from the notification this extra gets added to the calling intent. @@ -73,39 +78,38 @@ public class Dashboard extends Activity implements ProviderAPIResultReceiver.Rec public static final String ACTION_ASK_TO_CANCEL_VPN = "ask to cancel vpn"; /** * if always-on feature is enabled, but there's no provider configured the EIP Service - * adds this intent extra. ACTION_CONFIGURE_ALWAYS_ON_PROFILE + * adds this intent extra. Constants.APP_ACTION_CONFIGURE_ALWAYS_ON_PROFILE * serves to start the Configuration Wizard on top of the Dashboard Activity. */ - public static final String ACTION_CONFIGURE_ALWAYS_ON_PROFILE = "configure always-on profile"; - public static final String REQUEST_CODE = "request_code"; - public static final String PARAMETERS = "dashboard parameters"; - public static final String APP_VERSION = "bitmask version"; - //FIXME: context classes in static fields lead to memory leaks! - private static Context dashboardContext; protected static SharedPreferences preferences; - private FragmentManagerEnhanced fragment_manager; + private static FragmentManagerEnhanced fragment_manager; @InjectView(R.id.providerName) TextView provider_name; - VpnFragment eip_fragment; - UserStatusFragment user_status_fragment; + private VpnFragment eip_fragment; + private UserStatusFragment user_status_fragment; + private static Provider provider = new Provider(); - public ProviderAPIResultReceiver providerAPI_result_receiver; - private boolean switching_provider; + public static ProviderAPIResultReceiver providerAPI_result_receiver; + private static boolean switching_provider; + private boolean handledVersion; + + public static DashboardReceiver dashboardReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - preferences = getSharedPreferences(Constants.SHARED_PREFERENCES, MODE_PRIVATE); - fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); + dashboardReceiver = new DashboardReceiver(this); + preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); + fragment_manager = new FragmentManagerEnhanced(getSupportFragmentManager()); - providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler(), this); + providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler(), dashboardReceiver); - if (dashboardContext == null) { - dashboardContext = this; + if (!handledVersion) { handleVersion(); + handledVersion = true; } // initialize app necessities @@ -141,7 +145,7 @@ public class Dashboard extends Activity implements ProviderAPIResultReceiver.Rec private boolean providerInSharedPreferences() { return preferences != null && - preferences.getBoolean(Constants.PROVIDER_CONFIGURED, false); + preferences.getBoolean(PROVIDER_CONFIGURED, false); } @@ -171,7 +175,7 @@ public class Dashboard extends Activity implements ProviderAPIResultReceiver.Rec switch (versionCode) { case 91: // 0.6.0 without Bug #5999 case 101: // 0.8.0 - if (!preferences.getString(Constants.PROVIDER_KEY, "").isEmpty()) + if (!preferences.getString(PROVIDER_KEY, "").isEmpty()) eip_fragment.updateEipService(); break; } @@ -193,15 +197,15 @@ public class Dashboard extends Activity implements ProviderAPIResultReceiver.Rec } else if (intent.hasExtra(EIP_RESTART_ON_BOOT)) { Log.d(TAG, "Dashboard: EIP_RESTART_ON_BOOT"); prepareEIP(null); - } else if (intent.hasExtra(ACTION_CONFIGURE_ALWAYS_ON_PROFILE)) { - Log.d(TAG, "Dashboard: ACTION_CONFIGURE_ALWAYS_ON_PROFILE"); + } else if (intent.hasExtra(APP_ACTION_CONFIGURE_ALWAYS_ON_PROFILE)) { + Log.d(TAG, "Dashboard: Constants.APP_ACTION_CONFIGURE_ALWAYS_ON_PROFILE"); handleConfigureAlwaysOn(getIntent()); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == CONFIGURE_LEAP || requestCode == SWITCH_PROVIDER) { + if (requestCode == REQUEST_CODE_CONFIGURE_LEAP || requestCode == REQUEST_CODE_SWITCH_PROVIDER) { if (resultCode == RESULT_OK && data.hasExtra(Provider.KEY)) { provider = data.getParcelableExtra(Provider.KEY); providerToPreferences(provider); @@ -212,7 +216,7 @@ public class Dashboard extends Activity implements ProviderAPIResultReceiver.Rec sessionDialog(Bundle.EMPTY); } - } else if (resultCode == RESULT_CANCELED && data != null && data.hasExtra(ACTION_QUIT)) { + } else if (resultCode == RESULT_CANCELED && data != null && data.hasExtra(APP_ACTION_QUIT)) { finish(); } else configErrorDialog(); @@ -227,9 +231,9 @@ public class Dashboard extends Activity implements ProviderAPIResultReceiver.Rec } private void handleConfigureAlwaysOn(Intent intent) { - intent.removeExtra(ACTION_CONFIGURE_ALWAYS_ON_PROFILE); + intent.removeExtra(APP_ACTION_CONFIGURE_ALWAYS_ON_PROFILE); Log.d(TAG, "start Configuration wizard!"); - startActivityForResult(new Intent(this, ConfigurationWizard.class), CONFIGURE_LEAP); + startActivityForResult(new Intent(this, ConfigurationWizard.class), REQUEST_CODE_CONFIGURE_LEAP); } private void prepareEIP(Bundle savedInstanceState) { @@ -249,20 +253,20 @@ public class Dashboard extends Activity implements ProviderAPIResultReceiver.Rec } private void configureLeapProvider() { - if (getIntent().hasExtra(ACTION_CONFIGURE_ALWAYS_ON_PROFILE)) { - getIntent().removeExtra(ACTION_CONFIGURE_ALWAYS_ON_PROFILE); + if (getIntent().hasExtra(APP_ACTION_CONFIGURE_ALWAYS_ON_PROFILE)) { + getIntent().removeExtra(APP_ACTION_CONFIGURE_ALWAYS_ON_PROFILE); } - startActivityForResult(new Intent(this, ConfigurationWizard.class), CONFIGURE_LEAP); + startActivityForResult(new Intent(this, ConfigurationWizard.class), REQUEST_CODE_CONFIGURE_LEAP); } @SuppressLint("CommitPrefEdits") private void providerToPreferences(Provider provider) { - preferences.edit().putBoolean(Constants.PROVIDER_CONFIGURED, true). + preferences.edit().putBoolean(PROVIDER_CONFIGURED, true). putString(Provider.MAIN_URL, provider.getMainUrl().toString()). putString(Provider.KEY, provider.getDefinition().toString()).apply(); } private void configErrorDialog() { - AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getContext()); + AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this); alertBuilder.setTitle(getResources().getString(R.string.setup_error_title)); alertBuilder .setMessage(getResources().getString(R.string.setup_error_text)) @@ -270,13 +274,13 @@ public class Dashboard extends Activity implements ProviderAPIResultReceiver.Rec .setPositiveButton(getResources().getString(R.string.setup_error_configure_button), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - startActivityForResult(new Intent(getContext(), ConfigurationWizard.class), CONFIGURE_LEAP); + startActivityForResult(new Intent(getApplicationContext(), ConfigurationWizard.class), REQUEST_CODE_CONFIGURE_LEAP); } }) .setNegativeButton(getResources().getString(R.string.setup_error_close_button), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - preferences.edit().remove(Provider.KEY).remove(Constants.PROVIDER_CONFIGURED).apply(); + preferences.edit().remove(Provider.KEY).remove(PROVIDER_CONFIGURED).apply(); finish(); } }) @@ -288,11 +292,10 @@ public class Dashboard extends Activity implements ProviderAPIResultReceiver.Rec * service dependent UI elements to include. */ //TODO: REFACTOR ME! Consider implementing a manager that handles most of VpnFragment's logic about handling EIP commands. - //This way, we could avoid to create UI elements (like fragment_manager.replace(R.id.servicesCollection, eip_fragment, VpnFragment.TAG); ) + //This way, we could avoid to create UI elements (like fragmentManager.replace(R.id.servicesCollection, eip_fragment, VpnFragment.TAG); ) // just to start services and destroy them afterwards private void buildDashboard(boolean hideAndTurnOnEipOnBoot) { setContentView(R.layout.dashboard); - ButterKnife.inject(this); provider_name.setText(provider.getDomain()); @@ -316,9 +319,9 @@ public class Dashboard extends Activity implements ProviderAPIResultReceiver.Rec * * @param hideAndTurnOnEipOnBoot Flag that indicates if system intent android.intent.action.BOOT_COMPLETED * has caused to start Dashboard - * @return + * @return the created VPNFragment */ - private VpnFragment prepareEipFragment(boolean hideAndTurnOnEipOnBoot) { + public VpnFragment prepareEipFragment(boolean hideAndTurnOnEipOnBoot) { VpnFragment eip_fragment = new VpnFragment(); if (hideAndTurnOnEipOnBoot && !isAlwaysOn()) { @@ -334,7 +337,7 @@ public class Dashboard extends Activity implements ProviderAPIResultReceiver.Rec /** * checks if Android's VPN feature 'always-on' is enabled for Bitmask - * @return + * @return true if 'always-on' is enabled false if not */ private boolean isAlwaysOn() { return preferences.getBoolean(EIP_IS_ALWAYS_ON, false); @@ -378,25 +381,28 @@ public class Dashboard extends Activity implements ProviderAPIResultReceiver.Rec } public void showAbout() { - Intent intent = new Intent(this, AboutActivity.class); + Intent intent = new Intent(this, AboutFragment.class); startActivity(intent); } public void showLog() { - LogWindowWrapper log_window_wrapper = LogWindowWrapper.getInstance(getContext()); + LogWindowWrapper log_window_wrapper = LogWindowWrapper.getInstance(this); log_window_wrapper.showLog(); } - public void downloadVpnCertificate() { + + // TODO MOVE TO VPNManager(?) + public static void downloadVpnCertificate() { boolean is_authenticated = User.loggedIn(); - boolean allowed_anon = preferences.getBoolean(Constants.PROVIDER_ALLOW_ANONYMOUS, false); + boolean allowed_anon = preferences.getBoolean(PROVIDER_ALLOW_ANONYMOUS, false); if (allowed_anon || is_authenticated) ProviderAPICommand.execute(Bundle.EMPTY, ProviderAPI.DOWNLOAD_CERTIFICATE, providerAPI_result_receiver); else sessionDialog(Bundle.EMPTY); } - public void sessionDialog(Bundle resultData) { + // TODO how can we replace this + public static void sessionDialog(Bundle resultData) { try { FragmentTransaction transaction = fragment_manager.removePreviousFragment(SessionDialog.TAG); SessionDialog.getInstance(provider, resultData).show(transaction, SessionDialog.TAG); @@ -411,7 +417,7 @@ public class Dashboard extends Activity implements ProviderAPIResultReceiver.Rec clearDataOfLastProvider(); switching_provider = false; - startActivityForResult(new Intent(this, ConfigurationWizard.class), SWITCH_PROVIDER); + startActivityForResult(new Intent(this, ConfigurationWizard.class), REQUEST_CODE_SWITCH_PROVIDER); } private void clearDataOfLastProvider() { @@ -422,7 +428,7 @@ public class Dashboard extends Activity implements ProviderAPIResultReceiver.Rec if (entry.getKey().startsWith(Provider.KEY + ".") || entry.getKey().startsWith(Provider.CA_CERT + ".") || entry.getKey().startsWith(Provider.CA_CERT_FINGERPRINT + "." )|| - entry.getKey().equals(Constants.PREFERENCES_APP_VERSION) + entry.getKey().equals(PREFERENCES_APP_VERSION) ) { continue; } @@ -435,46 +441,53 @@ public class Dashboard extends Activity implements ProviderAPIResultReceiver.Rec } preferenceEditor.apply(); + switching_provider = false; + startActivityForResult(new Intent(this, ConfigurationWizard.class), REQUEST_CODE_SWITCH_PROVIDER); } - @Override - public void onReceiveResult(int resultCode, Bundle resultData) { - if (resultCode == ProviderAPI.SUCCESSFUL_SIGNUP) { - String username = resultData.getString(SessionDialog.USERNAME); - String password = resultData.getString(SessionDialog.PASSWORD); - user_status_fragment.logIn(username, password); - } else if (resultCode == ProviderAPI.FAILED_SIGNUP) { - sessionDialog(resultData); - } else if (resultCode == ProviderAPI.SUCCESSFUL_LOGIN) { - downloadVpnCertificate(); - } else if (resultCode == ProviderAPI.FAILED_LOGIN) { - sessionDialog(resultData); - } else if (resultCode == ProviderAPI.SUCCESSFUL_LOGOUT) { - if (switching_provider) switchProvider(); - } else if (resultCode == ProviderAPI.LOGOUT_FAILED) { - setResult(RESULT_CANCELED); - } else if (resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE) { - eip_fragment.updateEipService(); - setResult(RESULT_OK); - } else if (resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE) { - setResult(RESULT_CANCELED); - } else if (resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_EIP_SERVICE) { - eip_fragment.updateEipService(); - setResult(RESULT_OK); - } else if (resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_EIP_SERVICE) { - setResult(RESULT_CANCELED); + private static class DashboardReceiver implements ProviderAPIResultReceiver.Receiver{ + + private Dashboard dashboard; + + DashboardReceiver(Dashboard dashboard) { + this.dashboard = dashboard; } - } - public static Context getContext() { - return dashboardContext; + @Override + public void onReceiveResult(int resultCode, Bundle resultData) { + if (resultCode == ProviderAPI.SUCCESSFUL_SIGNUP) { + String username = resultData.getString(SessionDialog.USERNAME); + String password = resultData.getString(SessionDialog.PASSWORD); + dashboard.user_status_fragment.logIn(username, password); + } else if (resultCode == ProviderAPI.FAILED_SIGNUP) { + MainActivity.sessionDialog(resultData); + } else if (resultCode == ProviderAPI.SUCCESSFUL_LOGIN) { + Dashboard.downloadVpnCertificate(); + } else if (resultCode == ProviderAPI.FAILED_LOGIN) { + MainActivity.sessionDialog(resultData); + } else if (resultCode == ProviderAPI.SUCCESSFUL_LOGOUT) { + if (switching_provider) dashboard.switchProvider(); + } else if (resultCode == ProviderAPI.LOGOUT_FAILED) { + dashboard.setResult(RESULT_CANCELED); + } else if (resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE) { + dashboard.eip_fragment.updateEipService(); + dashboard.setResult(RESULT_OK); + } else if (resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE) { + dashboard.setResult(RESULT_CANCELED); + } else if (resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_EIP_SERVICE) { + dashboard.eip_fragment.updateEipService(); + dashboard.setResult(RESULT_OK); + } else if (resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_EIP_SERVICE) { + dashboard.setResult(RESULT_CANCELED); + } + } } public static Provider getProvider() { return provider; } @Override public void startActivityForResult(Intent intent, int requestCode) { - intent.putExtra(Dashboard.REQUEST_CODE, requestCode); + intent.putExtra(REQUEST_CODE_KEY, requestCode); super.startActivityForResult(intent, requestCode); } |