diff options
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/BaseConfigurationWizard.java')
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/BaseConfigurationWizard.java | 213 |
1 files changed, 102 insertions, 111 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/BaseConfigurationWizard.java b/app/src/main/java/se/leap/bitmaskclient/BaseConfigurationWizard.java index 63453ac3..35cc23fd 100644 --- a/app/src/main/java/se/leap/bitmaskclient/BaseConfigurationWizard.java +++ b/app/src/main/java/se/leap/bitmaskclient/BaseConfigurationWizard.java @@ -17,9 +17,6 @@ package se.leap.bitmaskclient; -import android.app.Activity; -import android.app.DialogFragment; -import android.app.FragmentTransaction; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -27,6 +24,8 @@ import android.content.IntentFilter; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.FragmentTransaction; import android.view.Display; import android.view.Menu; import android.view.MenuItem; @@ -48,15 +47,20 @@ import java.util.List; import javax.inject.Inject; -import butterknife.ButterKnife; import butterknife.InjectView; import butterknife.OnItemClick; -import se.leap.bitmaskclient.userstatus.SessionDialog; +import se.leap.bitmaskclient.fragments.AboutFragment; import static android.view.View.GONE; import static android.view.View.INVISIBLE; import static android.view.View.VISIBLE; +import static se.leap.bitmaskclient.Constants.APP_ACTION_QUIT; +import static se.leap.bitmaskclient.Constants.PROVIDER_ALLOW_ANONYMOUS; +import static se.leap.bitmaskclient.Constants.PROVIDER_KEY; +import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; import static se.leap.bitmaskclient.ProviderAPI.ERRORS; +import static se.leap.bitmaskclient.ProviderAPI.SET_UP_PROVIDER; +import static se.leap.bitmaskclient.ProviderAPI.UPDATE_PROGRESSBAR; /** * abstract base Activity that builds and shows the list of known available providers. @@ -69,27 +73,28 @@ import static se.leap.bitmaskclient.ProviderAPI.ERRORS; * @author cyberta */ -public abstract class BaseConfigurationWizard extends Activity - implements NewProviderDialog.NewProviderDialogInterface, ProviderDetailFragment.ProviderDetailFragmentInterface, DownloadFailedDialog.DownloadFailedDialogInterface, ProviderAPIResultReceiver.Receiver { +public abstract class BaseConfigurationWizard extends ButterKnifeActivity + implements NewProviderDialog.NewProviderDialogInterface, DownloadFailedDialog.DownloadFailedDialogInterface, ProviderAPIResultReceiver.Receiver { @InjectView(R.id.progressbar_configuration_wizard) protected ProgressBar mProgressBar; @InjectView(R.id.progressbar_description) - protected TextView progressbar_description; + protected TextView progressbarDescription; @InjectView(R.id.provider_list) - protected ListView provider_list_view; + protected ListView providerListView; @Inject protected ProviderListAdapter adapter; - private ProviderManager provider_manager; + private ProviderManager providerManager; protected Intent mConfigState = new Intent(PROVIDER_NOT_SET); - protected Provider selected_provider; + protected Provider selectedProvider; final public static String TAG = ConfigurationWizard.class.getSimpleName(); final protected static String PROVIDER_NOT_SET = "PROVIDER NOT SET"; final protected static String SETTING_UP_PROVIDER = "PROVIDER GETS SET"; - final private static String PENDING_SHOW_PROVIDER_DETAILS = "PROVIDER DETAILS SHOWN"; + final private static String PENDING_SHOW_PROVIDER_DETAILS = "PENDING PROVIDER DETAILS"; + final private static String SHOWING_PROVIDER_DETAILS = "SHOWING PROVIDER DETAILS SHOWN"; final private static String PENDING_SHOW_FAILED_DIALOG = "SHOW FAILED DIALOG"; final private static String REASON_TO_FAIL = "REASON TO FAIL"; final protected static String PROVIDER_SET = "PROVIDER SET"; @@ -97,16 +102,17 @@ public abstract class BaseConfigurationWizard extends Activity final private static String PROGRESSBAR_TEXT = TAG + "PROGRESSBAR_TEXT"; final private static String PROGRESSBAR_NUMBER = TAG + "PROGRESSBAR_NUMBER"; + final private static String ACTIVITY_STATE = "ACTIVITY STATE"; - public ProviderAPIResultReceiver providerAPI_result_receiver; - private ProviderAPIBroadcastReceiver_Update providerAPI_broadcast_receiver_update; + public ProviderAPIResultReceiver providerAPIResultReceiver; + private ProviderAPIBroadcastReceiver providerAPIBroadcastReceiver; protected static SharedPreferences preferences; - FragmentManagerEnhanced fragment_manager; - //TODO: add some states (values for progressbar_text) about ongoing setup or remove that field - private String progressbar_text = ""; - + FragmentManagerEnhanced fragmentManager; + //TODO: add some states (values for progressbarText) about ongoing setup or remove that field + private String progressbarText = ""; + private boolean isActivityShowing; public abstract void retrySetUpProvider(); @@ -117,26 +123,27 @@ public abstract class BaseConfigurationWizard extends Activity List<Renderer<Provider>> prototypes = new ArrayList<>(); prototypes.add(new ProviderRenderer(this)); ProviderRendererBuilder providerRendererBuilder = new ProviderRendererBuilder(prototypes); - adapter = new ProviderListAdapter(getLayoutInflater(), providerRendererBuilder, provider_manager); - provider_list_view.setAdapter(adapter); + adapter = new ProviderListAdapter(getLayoutInflater(), providerRendererBuilder, providerManager); + providerListView.setAdapter(adapter); } @Override protected void onSaveInstanceState(@NotNull Bundle outState) { if (mProgressBar != null) outState.putInt(PROGRESSBAR_NUMBER, mProgressBar.getProgress()); - if (progressbar_description != null) - outState.putString(PROGRESSBAR_TEXT, progressbar_description.getText().toString()); - outState.putParcelable(Provider.KEY, selected_provider); + if (progressbarDescription != null) + outState.putString(PROGRESSBAR_TEXT, progressbarDescription.getText().toString()); + outState.putString(ACTIVITY_STATE, mConfigState.getAction()); + outState.putParcelable(Provider.KEY, selectedProvider); super.onSaveInstanceState(outState); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - preferences = getSharedPreferences(Constants.SHARED_PREFERENCES, MODE_PRIVATE); - fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); - provider_manager = ProviderManager.getInstance(getAssets(), getExternalFilesDir(null)); + preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); + fragmentManager = new FragmentManagerEnhanced(getSupportFragmentManager()); + providerManager = ProviderManager.getInstance(getAssets(), getExternalFilesDir(null)); setUpInitialUI(); @@ -148,81 +155,85 @@ public abstract class BaseConfigurationWizard extends Activity } private void restoreState(Bundle savedInstanceState) { - progressbar_text = savedInstanceState.getString(PROGRESSBAR_TEXT, ""); - selected_provider = savedInstanceState.getParcelable(Provider.KEY); - - if (fragment_manager.findFragmentByTag(ProviderDetailFragment.TAG) == null && - (SETTING_UP_PROVIDER.equals(mConfigState.getAction()) || - PENDING_SHOW_PROVIDER_DETAILS.equals(mConfigState.getAction()) || - PENDING_SHOW_FAILED_DIALOG.equals(mConfigState.getAction()) - )) { - onItemSelectedUi(); - } + progressbarText = savedInstanceState.getString(PROGRESSBAR_TEXT, ""); + selectedProvider = savedInstanceState.getParcelable(Provider.KEY); + mConfigState.setAction(savedInstanceState.getString(ACTIVITY_STATE, PROVIDER_NOT_SET)); } @Override protected void onResume() { super.onResume(); + hideProgressBar(); + isActivityShowing = true; if (SETTING_UP_PROVIDER.equals(mConfigState.getAction())) { showProgressBar(); - adapter.hideAllBut(adapter.indexOf(selected_provider)); + adapter.hideAllBut(adapter.indexOf(selectedProvider)); + // TODO find a better replacement! This can lead to loops + onItemSelectedLogic(); } else if (PENDING_SHOW_PROVIDER_DETAILS.equals(mConfigState.getAction())) { showProviderDetails(); } else if (PENDING_SHOW_FAILED_DIALOG.equals(mConfigState.getAction())) { showDownloadFailedDialog(mConfigState.getStringExtra(REASON_TO_FAIL)); + } else if (SHOWING_PROVIDER_DETAILS.equals(mConfigState.getAction())) { + cancelAndShowAllProviders(); } } private void setUpInitialUI() { setContentView(R.layout.configuration_wizard_activity); - ButterKnife.inject(this); - hideProgressBar(); } private void hideProgressBar() { - //needs to be "INVISIBLE" instead of GONE b/c the progressbar_description gets translated + //needs to be "INVISIBLE" instead of GONE b/c the progressbarDescription gets translated // by the height of mProgressbar (and the height of the first list item) mProgressBar.setVisibility(INVISIBLE); - progressbar_description.setVisibility(INVISIBLE); + progressbarDescription.setVisibility(INVISIBLE); mProgressBar.setProgress(0); } protected void showProgressBar() { mProgressBar.setVisibility(VISIBLE); - progressbar_description.setVisibility(VISIBLE); + progressbarDescription.setVisibility(VISIBLE); + } + + @Override + protected void onPause() { + super.onPause(); + isActivityShowing = false; } @Override protected void onDestroy() { super.onDestroy(); - if (providerAPI_broadcast_receiver_update != null) - unregisterReceiver(providerAPI_broadcast_receiver_update); + if (providerAPIBroadcastReceiver != null) + unregisterReceiver(providerAPIBroadcastReceiver); } private void setUpProviderAPIResultReceiver() { - providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler(), this); - providerAPI_broadcast_receiver_update = new ProviderAPIBroadcastReceiver_Update(); + providerAPIResultReceiver = new ProviderAPIResultReceiver(new Handler(), this); + providerAPIBroadcastReceiver = new ProviderAPIBroadcastReceiver(); - IntentFilter update_intent_filter = new IntentFilter(ProviderAPI.UPDATE_PROGRESSBAR); - update_intent_filter.addCategory(Intent.CATEGORY_DEFAULT); - registerReceiver(providerAPI_broadcast_receiver_update, update_intent_filter); + IntentFilter updateIntentFilter = new IntentFilter(UPDATE_PROGRESSBAR); + updateIntentFilter.addAction(SET_UP_PROVIDER); + updateIntentFilter.addCategory(Intent.CATEGORY_DEFAULT); + registerReceiver(providerAPIBroadcastReceiver, updateIntentFilter); } @Override public void onReceiveResult(int resultCode, Bundle resultData) { if (resultCode == ProviderAPI.PROVIDER_OK) { try { - String provider_json_string = preferences.getString(Provider.KEY, ""); - if (!provider_json_string.isEmpty()) - selected_provider.define(new JSONObject(provider_json_string)); + String providerJsonString = preferences.getString(Provider.KEY, ""); + if (!providerJsonString.isEmpty()) + selectedProvider.define(new JSONObject(providerJsonString)); String caCert = preferences.getString(Provider.CA_CERT, ""); - selected_provider.setCACert(caCert); + selectedProvider.setCACert(caCert); } catch (JSONException e) { e.printStackTrace(); } - if (preferences.getBoolean(Constants.PROVIDER_ALLOW_ANONYMOUS, false)) { + if (preferences.getBoolean(PROVIDER_ALLOW_ANONYMOUS, false)) { mConfigState.putExtra(SERVICES_RETRIEVED, true); downloadVpnCertificate(); @@ -230,7 +241,6 @@ public abstract class BaseConfigurationWizard extends Activity mProgressBar.incrementProgressBy(1); hideProgressBar(); - showProviderDetails(); } } else if (resultCode == ProviderAPI.PROVIDER_NOK) { mConfigState.setAction(PROVIDER_NOT_SET); @@ -248,7 +258,7 @@ public abstract class BaseConfigurationWizard extends Activity mConfigState.setAction(PROVIDER_NOT_SET); hideProgressBar(); setResult(RESULT_CANCELED, mConfigState); - } else if (resultCode == AboutActivity.VIEWED) { + } else if (resultCode == AboutFragment.VIEWED) { // Do nothing, right now // I need this for CW to wait for the About activity to end before going back to Dashboard. } @@ -264,13 +274,13 @@ public abstract class BaseConfigurationWizard extends Activity //TODO Code 2 pane view mConfigState.setAction(SETTING_UP_PROVIDER); - selected_provider = adapter.getItem(position); + selectedProvider = adapter.getItem(position); onItemSelectedUi(); onItemSelectedLogic(); } protected void onItemSelectedUi() { - adapter.hideAllBut(adapter.indexOf(selected_provider)); + adapter.hideAllBut(adapter.indexOf(selectedProvider)); startProgressBar(); } @@ -290,7 +300,7 @@ public abstract class BaseConfigurationWizard extends Activity ProviderAPI.stop(); mProgressBar.setVisibility(GONE); mProgressBar.setProgress(0); - progressbar_description.setVisibility(GONE); + progressbarDescription.setVisibility(GONE); cancelSettingUpProvider(); } @@ -300,27 +310,27 @@ public abstract class BaseConfigurationWizard extends Activity hideProgressBar(); mConfigState.setAction(PROVIDER_NOT_SET); adapter.showAllProviders(); - preferences.edit().remove(Provider.KEY).remove(Constants.PROVIDER_ALLOW_ANONYMOUS).remove(Constants.PROVIDER_KEY).apply(); + preferences.edit().remove(Provider.KEY).remove(PROVIDER_ALLOW_ANONYMOUS).remove(PROVIDER_KEY).apply(); } @Override public void updateProviderDetails() { mConfigState.setAction(SETTING_UP_PROVIDER); - Intent provider_API_command = new Intent(this, ProviderAPI.class); + Intent providerAPICommand = new Intent(this, ProviderAPI.class); - provider_API_command.setAction(ProviderAPI.UPDATE_PROVIDER_DETAILS); - provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); + providerAPICommand.setAction(ProviderAPI.UPDATE_PROVIDER_DETAILS); + providerAPICommand.putExtra(ProviderAPI.RECEIVER_KEY, providerAPIResultReceiver); Bundle parameters = new Bundle(); - parameters.putString(Provider.MAIN_URL, selected_provider.getMainUrl().toString()); - provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters); + parameters.putString(Provider.MAIN_URL, selectedProvider.getMainUrl().toString()); + providerAPICommand.putExtra(ProviderAPI.PARAMETERS, parameters); - startService(provider_API_command); + startService(providerAPICommand); } private void askDashboardToQuitApp() { - Intent ask_quit = new Intent(); - ask_quit.putExtra(Dashboard.ACTION_QUIT, Dashboard.ACTION_QUIT); - setResult(RESULT_CANCELED, ask_quit); + Intent askQuit = new Intent(); + askQuit.putExtra(APP_ACTION_QUIT, APP_ACTION_QUIT); + setResult(RESULT_CANCELED, askQuit); } private void startProgressBar() { @@ -330,11 +340,11 @@ public abstract class BaseConfigurationWizard extends Activity int measured_height = listItemHeight(); mProgressBar.setTranslationY(measured_height); - progressbar_description.setTranslationY(measured_height + mProgressBar.getHeight()); + progressbarDescription.setTranslationY(measured_height + mProgressBar.getHeight()); } private int listItemHeight() { - View listItem = adapter.getView(0, null, provider_list_view); + View listItem = adapter.getView(0, null, providerListView); listItem.setLayoutParams(new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); @@ -355,33 +365,33 @@ public abstract class BaseConfigurationWizard extends Activity * Asks ProviderApiService to download an anonymous (anon) VPN certificate. */ private void downloadVpnCertificate() { - Intent provider_API_command = new Intent(this, ProviderAPI.class); + Intent providerAPICommand = new Intent(this, ProviderAPI.class); - provider_API_command.setAction(ProviderAPI.DOWNLOAD_CERTIFICATE); - provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); + providerAPICommand.setAction(ProviderAPI.DOWNLOAD_CERTIFICATE); + providerAPICommand.putExtra(ProviderAPI.RECEIVER_KEY, providerAPIResultReceiver); - startService(provider_API_command); + startService(providerAPICommand); } /** * Open the new provider dialog */ public void addAndSelectNewProvider() { - FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(NewProviderDialog.TAG); - new NewProviderDialog().show(fragment_transaction, NewProviderDialog.TAG); + FragmentTransaction fragmentTransaction = fragmentManager.removePreviousFragment(NewProviderDialog.TAG); + new NewProviderDialog().show(fragmentTransaction, NewProviderDialog.TAG); } /** * Open the new provider dialog with data */ public void addAndSelectNewProvider(String main_url) { - FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(NewProviderDialog.TAG); + FragmentTransaction fragmentTransaction = fragmentManager.removePreviousFragment(NewProviderDialog.TAG); DialogFragment newFragment = new NewProviderDialog(); Bundle data = new Bundle(); data.putString(Provider.MAIN_URL, main_url); newFragment.setArguments(data); - newFragment.show(fragment_transaction, NewProviderDialog.TAG); + newFragment.show(fragmentTransaction, NewProviderDialog.TAG); } /** @@ -391,7 +401,7 @@ public abstract class BaseConfigurationWizard extends Activity */ public void showDownloadFailedDialog(String reasonToFail) { try { - FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(DownloadFailedDialog.TAG); + FragmentTransaction fragmentTransaction = fragmentManager.removePreviousFragment(DownloadFailedDialog.TAG); DialogFragment newFragment; try { JSONObject errorJson = new JSONObject(reasonToFail); @@ -400,7 +410,7 @@ public abstract class BaseConfigurationWizard extends Activity e.printStackTrace(); newFragment = DownloadFailedDialog.newInstance(reasonToFail); } - newFragment.show(fragment_transaction, DownloadFailedDialog.TAG); + newFragment.show(fragmentTransaction, DownloadFailedDialog.TAG); } catch (IllegalStateException e) { e.printStackTrace(); mConfigState.setAction(PENDING_SHOW_FAILED_DIALOG); @@ -417,13 +427,14 @@ public abstract class BaseConfigurationWizard extends Activity * */ public void showProviderDetails() { - try { - FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(ProviderDetailFragment.TAG); - - DialogFragment newFragment = ProviderDetailFragment.newInstance(); - newFragment.show(fragment_transaction, ProviderDetailFragment.TAG); - } catch (IllegalStateException e) { - e.printStackTrace(); + // show only if current activity is shown + if (isActivityShowing) { + mConfigState.setAction(SHOWING_PROVIDER_DETAILS); + Intent intent = new Intent(this, ProviderDetailActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); + startActivity(intent); + } else { + // may be called AFTER onSaveInstanceState (!) mConfigState.setAction(PENDING_SHOW_PROVIDER_DETAILS); } } @@ -438,7 +449,7 @@ public abstract class BaseConfigurationWizard extends Activity public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.about_leap: - startActivityForResult(new Intent(this, AboutActivity.class), 0); + startActivityForResult(new Intent(this, AboutFragment.class), 0); return true; case R.id.new_provider: addAndSelectNewProvider(); @@ -448,33 +459,13 @@ public abstract class BaseConfigurationWizard extends Activity } } - @Override public void cancelAndShowAllProviders() { mConfigState.setAction(PROVIDER_NOT_SET); - selected_provider = null; + selectedProvider = null; adapter.showAllProviders(); } - @Override - public void login() { - mConfigState.setAction(PROVIDER_SET); - Intent ask_login = new Intent(); - ask_login.putExtra(Provider.KEY, selected_provider); - ask_login.putExtra(SessionDialog.TAG, SessionDialog.TAG); - setResult(RESULT_OK, ask_login); - finish(); - } - - @Override - public void use_anonymously() { - mConfigState.setAction(PROVIDER_SET); - Intent pass_provider = new Intent(); - pass_provider.putExtra(Provider.KEY, selected_provider); - setResult(RESULT_OK, pass_provider); - finish(); - } - - public class ProviderAPIBroadcastReceiver_Update extends BroadcastReceiver { + public class ProviderAPIBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int update = intent.getIntExtra(ProviderAPI.CURRENT_PROGRESS, 0); |