diff options
Diffstat (limited to 'app/src/main/java')
8 files changed, 512 insertions, 238 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/AbstractProviderDetailActivity.java b/app/src/main/java/se/leap/bitmaskclient/AbstractProviderDetailActivity.java index d521a3a9..a596cc7f 100644 --- a/app/src/main/java/se/leap/bitmaskclient/AbstractProviderDetailActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/AbstractProviderDetailActivity.java @@ -4,6 +4,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.annotation.Nullable; +import android.support.v7.widget.AppCompatTextView; import android.util.Log; import android.view.View; import android.widget.AdapterView; @@ -23,19 +24,12 @@ import static se.leap.bitmaskclient.Constants.PROVIDER_KEY; import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; import static se.leap.bitmaskclient.MainActivity.ACTION_SHOW_VPN_FRAGMENT; -public abstract class AbstractProviderDetailActivity extends ButterKnifeActivity { +public abstract class AbstractProviderDetailActivity extends ConfigWizardBaseActivity { final public static String TAG = "providerDetailActivity"; - protected SharedPreferences preferences; - - @InjectView(R.id.provider_detail_domain) - TextView domain; - - @InjectView(R.id.provider_detail_name) - TextView name; @InjectView(R.id.provider_detail_description) - TextView description; + AppCompatTextView description; @InjectView(R.id.provider_detail_options) ListView options; @@ -43,22 +37,18 @@ public abstract class AbstractProviderDetailActivity extends ButterKnifeActivity @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.provider_detail_fragment); + setContentView(R.layout.a_provider_detail); - preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); try { JSONObject providerJson = new JSONObject(preferences.getString(Provider.KEY, "")); - domain.setText(providerJson.getString(Provider.DOMAIN)); - name.setText(providerJson.getJSONObject(Provider.NAME).getString("en")); + setProviderHeaderText(getProviderName()); description.setText(providerJson.getJSONObject(Provider.DESCRIPTION).getString("en")); - setTitle(R.string.provider_details_title); - // Show only the options allowed by the provider ArrayList<String> optionsList = new ArrayList<>(); if (registrationAllowed(providerJson)) { - optionsList.add(getString(R.string.login_button)); - optionsList.add(getString(R.string.signup_button)); + optionsList.add(getString(R.string.login_to_profile)); + optionsList.add(getString(R.string.create_profile)); } if (anonAllowed(providerJson)) { optionsList.add(getString(R.string.use_anonymously_button)); @@ -75,10 +65,10 @@ public abstract class AbstractProviderDetailActivity extends ButterKnifeActivity public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String text = ((TextView) view).getText().toString(); Intent intent; - if (text.equals(getString(R.string.login_button))) { + if (text.equals(getString(R.string.login_to_profile))) { Log.d(TAG, "login selected"); intent = new Intent(getApplicationContext(), LoginActivity.class); - } else if (text.equals(getString(R.string.signup_button))) { + } else if (text.equals(getString(R.string.create_profile))) { Log.d(TAG, "signup selected"); intent = new Intent(getApplicationContext(), SignupActivity.class); } else { @@ -86,6 +76,7 @@ public abstract class AbstractProviderDetailActivity extends ButterKnifeActivity intent = new Intent(getApplicationContext(), MainActivity.class); intent.setAction(ACTION_SHOW_VPN_FRAGMENT); } + intent.putExtra(PROVIDER_KEY, provider); intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(intent); } @@ -95,6 +86,12 @@ public abstract class AbstractProviderDetailActivity extends ButterKnifeActivity } } + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + provider = intent.getParcelableExtra(PROVIDER_KEY); + } + private boolean anonAllowed(JSONObject providerJson) { try { JSONObject serviceDescription = providerJson.getJSONObject(Provider.SERVICE); diff --git a/app/src/main/java/se/leap/bitmaskclient/BaseConfigurationWizard.java b/app/src/main/java/se/leap/bitmaskclient/BaseConfigurationWizard.java index d0868437..1025a64e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/BaseConfigurationWizard.java +++ b/app/src/main/java/se/leap/bitmaskclient/BaseConfigurationWizard.java @@ -21,21 +21,14 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; 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.util.Log; -import android.view.Display; import android.view.Menu; import android.view.MenuItem; -import android.view.View; -import android.view.WindowManager; import android.widget.ListView; -import android.widget.ProgressBar; -import android.widget.RelativeLayout; -import android.widget.TextView; import com.pedrogomez.renderers.Renderer; @@ -53,12 +46,9 @@ import butterknife.OnItemClick; 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.CORRECTLY_DOWNLOADED_CERTIFICATE; import static se.leap.bitmaskclient.ProviderAPI.ERRORS; import static se.leap.bitmaskclient.ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE; @@ -68,7 +58,6 @@ import static se.leap.bitmaskclient.ProviderAPI.PROVIDER_OK; import static se.leap.bitmaskclient.ProviderAPI.PROVIDER_SET_UP; import static se.leap.bitmaskclient.ProviderAPI.RESULT_CODE; import static se.leap.bitmaskclient.ProviderAPI.RESULT_KEY; -import static se.leap.bitmaskclient.ProviderAPI.UPDATE_PROGRESSBAR; /** * abstract base Activity that builds and shows the list of known available providers. @@ -81,12 +70,8 @@ import static se.leap.bitmaskclient.ProviderAPI.UPDATE_PROGRESSBAR; * @author cyberta */ -public abstract class BaseConfigurationWizard extends ButterKnifeActivity +public abstract class BaseConfigurationWizard extends ConfigWizardBaseActivity implements NewProviderDialog.NewProviderDialogInterface, DownloadFailedDialog.DownloadFailedDialogInterface, ProviderAPIResultReceiver.Receiver { - @InjectView(R.id.progressbar_configuration_wizard) - protected ProgressBar mProgressBar; - @InjectView(R.id.progressbar_description) - protected TextView progressbarDescription; @InjectView(R.id.provider_list) protected ListView providerListView; @@ -95,10 +80,11 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity private ProviderManager providerManager; protected Intent mConfigState = new Intent(PROVIDER_NOT_SET); - protected Provider selectedProvider; final public static String TAG = ConfigurationWizard.class.getSimpleName(); + final private static String ACTIVITY_STATE = "ACTIVITY STATE"; + final protected static String PROVIDER_NOT_SET = "PROVIDER NOT SET"; final protected static String SETTING_UP_PROVIDER = "PROVIDER GETS SET"; final private static String SHOWING_PROVIDER_DETAILS = "SHOWING PROVIDER DETAILS"; @@ -106,16 +92,10 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity final private static String REASON_TO_FAIL = "REASON TO FAIL"; final protected static String SERVICES_RETRIEVED = "SERVICES RETRIEVED"; - 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 providerAPIResultReceiver; private ProviderAPIBroadcastReceiver providerAPIBroadcastReceiver; - protected static SharedPreferences preferences; FragmentManagerEnhanced fragmentManager; - //TODO: add some states (values for progressbarText) about ongoing setup or remove that field private boolean isActivityShowing; private String reasonToFail; @@ -135,12 +115,10 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity @Override protected void onSaveInstanceState(@NotNull Bundle outState) { - if (mProgressBar != null) - outState.putInt(PROGRESSBAR_NUMBER, mProgressBar.getProgress()); - if (progressbarDescription != null) - outState.putString(PROGRESSBAR_TEXT, progressbarDescription.getText().toString()); + //if (progressbarDescription != null) + // outState.putString(PROGRESSBAR_TEXT, progressbarDescription.getText().toString()); outState.putString(ACTIVITY_STATE, mConfigState.getAction()); - outState.putParcelable(Provider.KEY, selectedProvider); + outState.putParcelable(Provider.KEY, provider); DialogFragment dialogFragment = (DialogFragment) fragmentManager.findFragmentByTag(DownloadFailedDialog.TAG); if (dialogFragment != null) { @@ -154,7 +132,6 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); fragmentManager = new FragmentManagerEnhanced(getSupportFragmentManager()); providerManager = ProviderManager.getInstance(getAssets(), getExternalFilesDir(null)); @@ -169,7 +146,7 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity private void restoreState(Bundle savedInstanceState) { - selectedProvider = savedInstanceState.getParcelable(Provider.KEY); + provider = savedInstanceState.getParcelable(Provider.KEY); mConfigState.setAction(savedInstanceState.getString(ACTIVITY_STATE, PROVIDER_NOT_SET)); reasonToFail = savedInstanceState.getString(REASON_TO_FAIL); @@ -180,7 +157,7 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity if (SETTING_UP_PROVIDER.equals(mConfigState.getAction()) || PENDING_SHOW_FAILED_DIALOG.equals(mConfigState.getAction()) ) { - onItemSelectedUi(); + showProgressBar(); } } @@ -192,7 +169,7 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity isActivityShowing = true; if (SETTING_UP_PROVIDER.equals(mConfigState.getAction())) { showProgressBar(); - adapter.hideAllBut(adapter.indexOf(selectedProvider)); + adapter.hideAllBut(adapter.indexOf(provider)); checkProviderSetUp(); } else if (PENDING_SHOW_FAILED_DIALOG.equals(mConfigState.getAction())) { showDownloadFailedDialog(); @@ -203,22 +180,10 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity private void setUpInitialUI() { setContentView(R.layout.configuration_wizard_activity); + setProviderHeaderText(R.string.setup_provider); hideProgressBar(); } - private void hideProgressBar() { - //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); - progressbarDescription.setVisibility(INVISIBLE); - mProgressBar.setProgress(0); - } - - protected void showProgressBar() { - mProgressBar.setVisibility(VISIBLE); - progressbarDescription.setVisibility(VISIBLE); - } - @Override protected void onPause() { super.onPause(); @@ -237,8 +202,7 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity providerAPIResultReceiver = new ProviderAPIResultReceiver(new Handler(), this); providerAPIBroadcastReceiver = new ProviderAPIBroadcastReceiver(); - IntentFilter updateIntentFilter = new IntentFilter(UPDATE_PROGRESSBAR); - updateIntentFilter.addAction(PROVIDER_API_EVENT); + IntentFilter updateIntentFilter = new IntentFilter(PROVIDER_API_EVENT); updateIntentFilter.addCategory(Intent.CATEGORY_DEFAULT); registerReceiver(providerAPIBroadcastReceiver, updateIntentFilter); } @@ -247,9 +211,9 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity try { String providerJsonString = preferences.getString(Provider.KEY, ""); if (!providerJsonString.isEmpty()) - selectedProvider.define(new JSONObject(providerJsonString)); + provider.define(new JSONObject(providerJsonString)); String caCert = preferences.getString(Provider.CA_CERT, ""); - selectedProvider.setCACert(caCert); + provider.setCACert(caCert); } catch (JSONException e) { e.printStackTrace(); } @@ -259,9 +223,6 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity downloadVpnCertificate(); } else { - mProgressBar.incrementProgressBy(1); - hideProgressBar(); - showProviderDetails(); } } @@ -277,8 +238,6 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity } void handleCorrectlyDownloadedCertificate() { - mProgressBar.incrementProgressBy(1); - hideProgressBar(); showProviderDetails(); } @@ -307,16 +266,11 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity //TODO Code 2 pane view mConfigState.setAction(SETTING_UP_PROVIDER); - selectedProvider = adapter.getItem(position); - onItemSelectedUi(); + provider = adapter.getItem(position); + showProgressBar(); onItemSelectedLogic(); } - protected void onItemSelectedUi() { - adapter.hideAllBut(adapter.indexOf(selectedProvider)); - startProgressBar(); - } - @Override public void onBackPressed() { if (SETTING_UP_PROVIDER.equals(mConfigState.getAction()) || @@ -330,9 +284,7 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity private void stopSettingUpProvider() { ProviderAPI.stop(); - mProgressBar.setVisibility(GONE); - mProgressBar.setProgress(0); - progressbarDescription.setVisibility(GONE); + loadingScreen.setVisibility(GONE); cancelSettingUpProvider(); } @@ -352,7 +304,7 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity providerAPICommand.setAction(ProviderAPI.UPDATE_PROVIDER_DETAILS); Bundle parameters = new Bundle(); - parameters.putString(Provider.MAIN_URL, selectedProvider.getMainUrl().toString()); + parameters.putString(Provider.MAIN_URL, provider.getMainUrl().toString()); providerAPICommand.putExtra(ProviderAPI.PARAMETERS, parameters); startService(providerAPICommand); @@ -371,34 +323,6 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity setResult(RESULT_CANCELED, askQuit); } - private void startProgressBar() { - showProgressBar(); - mProgressBar.setProgress(0); - mProgressBar.setMax(3); - - int measured_height = listItemHeight(); - mProgressBar.setTranslationY(measured_height); - progressbarDescription.setTranslationY(measured_height + mProgressBar.getHeight()); - } - - private int listItemHeight() { - View listItem = adapter.getView(0, null, providerListView); - listItem.setLayoutParams(new RelativeLayout.LayoutParams( - RelativeLayout.LayoutParams.WRAP_CONTENT, - RelativeLayout.LayoutParams.WRAP_CONTENT)); - WindowManager wm = (WindowManager) getApplicationContext() - .getSystemService(Context.WINDOW_SERVICE); - Display display = wm.getDefaultDisplay(); - int screenWidth = display.getWidth(); // deprecated - - int listViewWidth = screenWidth - 10 - 10; - int widthSpec = View.MeasureSpec.makeMeasureSpec(listViewWidth, - View.MeasureSpec.AT_MOST); - listItem.measure(widthSpec, 0); - - return listItem.getMeasuredHeight(); - } - /** * Asks ProviderApiService to download an anonymous (anon) VPN certificate. */ @@ -461,10 +385,12 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity */ public void showProviderDetails() { // show only if current activity is shown - if (isActivityShowing && !mConfigState.getAction().equalsIgnoreCase(SHOWING_PROVIDER_DETAILS)) { + if (isActivityShowing && mConfigState.getAction() != null && + !mConfigState.getAction().equalsIgnoreCase(SHOWING_PROVIDER_DETAILS)) { mConfigState.setAction(SHOWING_PROVIDER_DETAILS); Intent intent = new Intent(this, ProviderDetailActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); + intent.putExtra(PROVIDER_KEY, provider); startActivity(intent); } } @@ -491,39 +417,42 @@ public abstract class BaseConfigurationWizard extends ButterKnifeActivity public void cancelAndShowAllProviders() { mConfigState.setAction(PROVIDER_NOT_SET); - selectedProvider = null; + provider = null; adapter.showAllProviders(); } public class ProviderAPIBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); + Log.d(TAG, "received Broadcast"); - if (action == null) { + String action = intent.getAction(); + if (action == null || !action.equalsIgnoreCase(PROVIDER_API_EVENT)) { return; } - if (action.equalsIgnoreCase(UPDATE_PROGRESSBAR)) { - int update = intent.getIntExtra(ProviderAPI.CURRENT_PROGRESS, 0); - mProgressBar.setProgress(update); - } else if (action.equalsIgnoreCase(PROVIDER_API_EVENT)) { + if (mConfigState.getAction() != null && + mConfigState.getAction().equalsIgnoreCase(SETTING_UP_PROVIDER)) { int resultCode = intent.getIntExtra(RESULT_CODE, -1); - - switch (resultCode) { - case PROVIDER_OK: - handleProviderSetUp(); - break; - case PROVIDER_NOK: - handleProviderSetupFailed((Bundle) intent.getParcelableExtra(RESULT_KEY)); - break; - case CORRECTLY_DOWNLOADED_CERTIFICATE: - handleCorrectlyDownloadedCertificate(); - break; - case INCORRECTLY_DOWNLOADED_CERTIFICATE: - handleIncorrectlyDownloadedCertificate(); - break; - + Log.d(TAG, "Broadcast resultCode: " + Integer.toString(resultCode)); + + + if (getProviderName().equalsIgnoreCase(provider.getName()) && + getProviderDomain().equalsIgnoreCase(provider.getDomain())) { + switch (resultCode) { + case PROVIDER_OK: + handleProviderSetUp(); + break; + case PROVIDER_NOK: + handleProviderSetupFailed((Bundle) intent.getParcelableExtra(RESULT_KEY)); + break; + case CORRECTLY_DOWNLOADED_CERTIFICATE: + handleCorrectlyDownloadedCertificate(); + break; + case INCORRECTLY_DOWNLOADED_CERTIFICATE: + handleIncorrectlyDownloadedCertificate(); + break; + } } } } diff --git a/app/src/main/java/se/leap/bitmaskclient/ConfigWizardBaseActivity.java b/app/src/main/java/se/leap/bitmaskclient/ConfigWizardBaseActivity.java new file mode 100644 index 00000000..98715c1a --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/ConfigWizardBaseActivity.java @@ -0,0 +1,134 @@ +package se.leap.bitmaskclient; + +import android.content.SharedPreferences; +import android.os.Bundle; +import android.support.annotation.DrawableRes; +import android.support.annotation.Nullable; +import android.support.annotation.StringRes; +import android.support.v4.app.FragmentTransaction; +import android.support.v7.widget.AppCompatImageView; +import android.support.v7.widget.AppCompatTextView; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.Locale; + +import butterknife.InjectView; +import se.leap.bitmaskclient.userstatus.SessionDialog; + +import static android.view.View.GONE; +import static android.view.View.VISIBLE; +import static se.leap.bitmaskclient.Constants.PROVIDER_KEY; +import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; + +/** + * Base Activity for configuration wizard activities + * + * Created by fupduck on 09.01.18. + */ + +public abstract class ConfigWizardBaseActivity extends ButterKnifeActivity { + + protected SharedPreferences preferences; + + @InjectView(R.id.provider_header_logo) + AppCompatImageView providerHeaderLogo; + + @InjectView(R.id.provider_header_text) + AppCompatTextView providerHeaderText; + + @InjectView(R.id.loading_screen) + protected LinearLayout loadingScreen; + + @InjectView(R.id.progressbar_description) + protected AppCompatTextView progressbarText; + + @InjectView(R.id.content) + protected LinearLayout content; + + protected Provider provider; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); + + provider = getIntent().getParcelableExtra(PROVIDER_KEY); + } + + @Override + public void setContentView(View view) { + super.setContentView(view); + setProviderHeaderText(getProviderName()); + } + + @Override + public void setContentView(int layoutResID) { + super.setContentView(layoutResID); + setProviderHeaderText(getProviderName()); + } + + @Override + public void setContentView(View view, ViewGroup.LayoutParams params) { + super.setContentView(view, params); + setProviderHeaderText(getProviderName()); + } + + protected void setProviderHeaderLogo(@DrawableRes int providerHeaderLogo) { + this.providerHeaderLogo.setImageResource(providerHeaderLogo); + } + + protected void setProviderHeaderText(String providerHeaderText) { + this.providerHeaderText.setText(providerHeaderText); + } + + protected void setProviderHeaderText(@StringRes int providerHeaderText) { + this.providerHeaderText.setText(providerHeaderText); + } + + protected String getProviderName() { + try { + JSONObject providerJson = new JSONObject(preferences.getString(Provider.KEY, "")); + String lang = Locale.getDefault().getLanguage(); + return providerJson.getJSONObject(Provider.NAME).getString(lang); + } catch (JSONException e) { + try { + JSONObject providerJson = new JSONObject(preferences.getString(Provider.KEY, "")); + return providerJson.getJSONObject(Provider.NAME).getString("en"); + } catch (JSONException e2) { + return null; + } + } + } + + protected String getProviderDomain() { + try { + JSONObject providerJson = new JSONObject(preferences.getString(Provider.KEY, "")); + return providerJson.getString(Provider.DOMAIN); + } catch (JSONException e) { + return null; + } + } + protected void hideProgressBar() { + loadingScreen.setVisibility(GONE); + content.setVisibility(VISIBLE); + } + + protected void showProgressBar() { + content.setVisibility(GONE); + loadingScreen.setVisibility(VISIBLE); + } + + protected void setProgressbarText(String progressbarText) { + this.progressbarText.setText(progressbarText); + } + + protected void setProgressbarText(@StringRes int progressbarText) { + this.progressbarText.setText(progressbarText); + } + +} diff --git a/app/src/main/java/se/leap/bitmaskclient/LoginActivity.java b/app/src/main/java/se/leap/bitmaskclient/LoginActivity.java index a5cbf5f5..6ae288c2 100644 --- a/app/src/main/java/se/leap/bitmaskclient/LoginActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/LoginActivity.java @@ -2,11 +2,12 @@ package se.leap.bitmaskclient; import android.os.Bundle; import android.support.annotation.Nullable; -import android.util.Log; import butterknife.OnClick; /** + * Activity to login to chosen Provider + * * Created by fupduck on 09.01.18. */ @@ -15,10 +16,9 @@ public class LoginActivity extends ProviderCredentialsBaseActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.a_login); - - setProviderHeaderText("providerNAME"); + setProgressbarText(R.string.logging_in); setProviderHeaderLogo(R.drawable.mask); + setProviderHeaderText(R.string.login_to_profile); } @Override @@ -29,6 +29,7 @@ public class LoginActivity extends ProviderCredentialsBaseActivity { @Override @OnClick(R.id.button) void handleButton() { + super.handleButton(); login(getUsername(), getPassword()); } diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index 60b1b93c..95daccc6 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -158,9 +158,13 @@ public final class Provider implements Parcelable { name = definition.getJSONObject(API_TERM_NAME).getString(lang); else throw new JSONException("Provider not defined"); } catch (JSONException e) { - if (mainUrl != null) { - String host = mainUrl.getDomain(); - name = host.substring(0, host.indexOf(".")); + try { + name = definition.getJSONObject(API_TERM_NAME).getString("en"); + } catch (JSONException e2) { + if (mainUrl != null) { + String host = mainUrl.getDomain(); + name = host.substring(0, host.indexOf(".")); + } } } diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderApiManagerBase.java b/app/src/main/java/se/leap/bitmaskclient/ProviderApiManagerBase.java index da2e4c8b..cfc6e49d 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderApiManagerBase.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderApiManagerBase.java @@ -96,7 +96,6 @@ import static se.leap.bitmaskclient.R.string.error_io_exception_user_message; import static se.leap.bitmaskclient.R.string.error_json_exception_user_message; import static se.leap.bitmaskclient.R.string.error_no_such_algorithm_exception_user_message; import static se.leap.bitmaskclient.R.string.malformed_url; -import static se.leap.bitmaskclient.R.string.routes_info_excl; import static se.leap.bitmaskclient.R.string.server_unreachable_message; import static se.leap.bitmaskclient.R.string.service_is_down_error; import static se.leap.bitmaskclient.R.string.warning_corrupted_provider_cert; diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderCredentialsBaseActivity.java b/app/src/main/java/se/leap/bitmaskclient/ProviderCredentialsBaseActivity.java index 1ed64559..67f4e787 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderCredentialsBaseActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderCredentialsBaseActivity.java @@ -1,91 +1,210 @@ package se.leap.bitmaskclient; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.os.Bundle; -import android.os.Handler; -import android.support.annotation.DrawableRes; import android.support.annotation.Nullable; import android.support.annotation.StringRes; import android.support.design.widget.TextInputEditText; -import android.widget.Button; -import android.widget.ImageView; +import android.support.design.widget.TextInputLayout; +import android.support.v7.widget.AppCompatButton; +import android.support.v7.widget.AppCompatTextView; +import android.text.Editable; +import android.text.TextWatcher; +import android.util.Log; +import android.view.KeyEvent; import android.widget.TextView; import butterknife.InjectView; import butterknife.OnClick; import se.leap.bitmaskclient.userstatus.SessionDialog; +import se.leap.bitmaskclient.userstatus.SessionDialog.ERRORS; import se.leap.bitmaskclient.userstatus.User; import static se.leap.bitmaskclient.MainActivity.ACTION_SHOW_VPN_FRAGMENT; +import static android.view.View.GONE; +import static android.view.View.VISIBLE; +import static android.view.inputmethod.EditorInfo.IME_ACTION_DONE; +import static se.leap.bitmaskclient.ProviderAPI.PROVIDER_API_EVENT; +import static se.leap.bitmaskclient.ProviderAPI.RESULT_CODE; +import static se.leap.bitmaskclient.ProviderAPI.RESULT_KEY; +import static se.leap.bitmaskclient.userstatus.SessionDialog.USERNAME; + /** + * Base Activity for activities concerning a provider interaction + * * Created by fupduck on 09.01.18. */ -public abstract class ProviderCredentialsBaseActivity extends ButterKnifeActivity { +public abstract class ProviderCredentialsBaseActivity extends ConfigWizardBaseActivity { + + final protected static String TAG = ProviderCredentialsBaseActivity.class.getName(); + + final private static String ACTIVITY_STATE = "ACTIVITY STATE"; - protected ProviderAPIResultReceiver providerAPIResultReceiver; + final private static String SHOWING_FORM = "SHOWING_FORM"; + final private static String PERFORMING_ACTION = "PERFORMING_ACTION"; + final private static String USER_MESSAGE = "USER_MESSAGE"; + final private static String USERNAME_ERROR = "USERNAME_ERROR"; + final private static String PASSWORD_ERROR = "PASSWORD_ERROR"; + final private static String PASSWORD_VERIFICATION_ERROR = "PASSWORD_VERIFICATION_ERROR"; - @InjectView(R.id.provider_header_logo) - ImageView providerHeaderLogo; + protected Intent mConfigState = new Intent(SHOWING_FORM); + protected ProviderAPIBroadcastReceiver providerAPIBroadcastReceiver; - @InjectView(R.id.provider_header_text) - TextView providerHeaderText; + @InjectView(R.id.provider_credentials_user_message) + AppCompatTextView userMessage; @InjectView(R.id.provider_credentials_username) - TextInputEditText providerCredentialsUsername; + TextInputEditText usernameField; @InjectView(R.id.provider_credentials_password) - TextInputEditText providerCredentialsPassword; + TextInputEditText passwordField; + + @InjectView(R.id.provider_credentials_password_verification) + TextInputEditText passwordVerificationField; + + @InjectView(R.id.provider_credentials_username_error) + TextInputLayout usernameError; + + @InjectView(R.id.provider_credentials_password_error) + TextInputLayout passwordError; + + @InjectView(R.id.provider_credentials_password_verification_error) + TextInputLayout passwordVerificationError; @InjectView(R.id.button) - Button providerCredentialsButton; + AppCompatButton button; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - providerAPIResultReceiver = new ProviderAPIResultReceiver(new Handler(), new ProviderCredentialsReceiver(this)); + setContentView(R.layout.a_provider_credentials); + providerAPIBroadcastReceiver = new ProviderAPIBroadcastReceiver(); + IntentFilter updateIntentFilter = new IntentFilter(PROVIDER_API_EVENT); + updateIntentFilter.addCategory(Intent.CATEGORY_DEFAULT); + registerReceiver(providerAPIBroadcastReceiver, updateIntentFilter); + + setUpListeners(); + if(savedInstanceState != null) { + restoreState(savedInstanceState); + } } - @OnClick(R.id.button) - abstract void handleButton(); + @Override + protected void onResume() { + super.onResume(); - protected void setProviderHeaderLogo(@DrawableRes int providerHeaderLogo) { - this.providerHeaderLogo.setImageResource(providerHeaderLogo); + String action = mConfigState.getAction(); + if (action == null) { + return; + } + + if(action.equalsIgnoreCase(PERFORMING_ACTION)) { + showProgressBar(); + } } - protected void setProviderHeaderText(String providerHeaderText) { - this.providerHeaderText.setText(providerHeaderText); + private void restoreState(Bundle savedInstance) { + if (savedInstance.getString(USER_MESSAGE) != null) { + userMessage.setText(savedInstance.getString(USER_MESSAGE)); + userMessage.setVisibility(VISIBLE); + } + usernameError.setError(savedInstance.getString(USERNAME_ERROR)); + passwordError.setError(savedInstance.getString(PASSWORD_ERROR)); + passwordVerificationError.setError(savedInstance.getString(PASSWORD_VERIFICATION_ERROR)); + if (savedInstance.getString(ACTIVITY_STATE) != null) { + mConfigState.setAction(savedInstance.getString(ACTIVITY_STATE)); + } } - protected void setProviderHeaderText(@StringRes int providerHeaderText) { - this.providerHeaderText.setText(providerHeaderText); + @Override + public void onSaveInstanceState(Bundle outState) { + outState.putString(ACTIVITY_STATE, mConfigState.getAction()); + if (userMessage.getText() != null && userMessage.getVisibility() == VISIBLE) { + outState.putString(USER_MESSAGE, userMessage.getText().toString()); + } + if (usernameError.getError() != null) { + outState.putString(USERNAME_ERROR, usernameError.getError().toString()); + } + if (passwordError.getError() != null) { + outState.putString(PASSWORD_ERROR, passwordError.getError().toString()); + } + if (passwordVerificationError.getError() != null) { + outState.putString(PASSWORD_VERIFICATION_ERROR, passwordVerificationError.getError().toString()); + } + + super.onSaveInstanceState(outState); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + if (providerAPIBroadcastReceiver != null) + unregisterReceiver(providerAPIBroadcastReceiver); + } + + @OnClick(R.id.button) + void handleButton() { + mConfigState.setAction(PERFORMING_ACTION); } protected void setButtonText(@StringRes int buttonText) { - providerCredentialsButton.setText(buttonText); + button.setText(buttonText); } String getUsername() { - return providerCredentialsUsername.getText().toString(); + String username = usernameField.getText().toString(); + String providerDomain = provider.getDomain(); + if (username.endsWith(providerDomain)) { + return username.split("@" + providerDomain)[0]; + } + return username; } String getPassword() { - return providerCredentialsPassword.getText().toString(); + return passwordField.getText().toString(); + } + + String getPasswordVerification() { + return passwordVerificationField.getText().toString(); } void login(String username, String password) { + showProgressBar(); + User.setUserName(username); + + Intent providerAPICommand = new Intent(this, ProviderAPI.class); Bundle parameters = bundlePassword(password); - ProviderAPICommand.execute(parameters, ProviderAPI.LOG_IN, providerAPIResultReceiver); + providerAPICommand.setAction(ProviderAPI.LOG_IN); + providerAPICommand.putExtra(ProviderAPI.PARAMETERS, parameters); + startService(providerAPICommand); } public void signUp(String username, String password) { + showProgressBar(); + User.setUserName(username); + + Intent providerAPICommand = new Intent(this, ProviderAPI.class); Bundle parameters = bundlePassword(password); - ProviderAPICommand.execute(parameters, ProviderAPI.SIGN_UP, providerAPIResultReceiver); + providerAPICommand.setAction(ProviderAPI.SIGN_UP); + providerAPICommand.putExtra(ProviderAPI.PARAMETERS, parameters); + startService(providerAPICommand); } + + void downloadVpnCertificate() { + Intent providerAPICommand = new Intent(this, ProviderAPI.class); + providerAPICommand.setAction(ProviderAPI.DOWNLOAD_CERTIFICATE); + providerAPICommand.putExtra(ProviderAPI.PARAMETERS, Bundle.EMPTY); + startService(providerAPICommand); + } + protected Bundle bundlePassword(String password) { Bundle parameters = new Bundle(); if (!password.isEmpty()) @@ -93,45 +212,163 @@ public abstract class ProviderCredentialsBaseActivity extends ButterKnifeActivit return parameters; } - public static class ProviderCredentialsReceiver implements ProviderAPIResultReceiver.Receiver{ + private void setUpListeners() { + usernameField.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + + @Override + public void afterTextChanged(Editable s) { + if (getUsername().equalsIgnoreCase("")) { + usernameError.setError(getString(R.string.username_ask)); + } else { + usernameError.setError(null); + } + } + }); + usernameField.setOnEditorActionListener(new TextView.OnEditorActionListener() { + @Override + public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { + if (actionId == IME_ACTION_DONE + || event != null && event.getAction() == KeyEvent.ACTION_DOWN + && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { + passwordField.requestFocus(); + return true; + } + return false; + } + }); + + passwordField.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + + @Override + public void afterTextChanged(Editable s) { + if(getPassword().length() < 8) { + passwordError.setError(getString(R.string.error_not_valid_password_user_message)); + } else { + passwordError.setError(null); + } + } + }); + passwordField.setOnEditorActionListener(new TextView.OnEditorActionListener() { + @Override + public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { + if (actionId == IME_ACTION_DONE + || event != null && event.getAction() == KeyEvent.ACTION_DOWN + && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { + if (passwordVerificationField.getVisibility() == VISIBLE) { + passwordVerificationField.requestFocus(); + } else { + button.performClick(); + } + return true; + } + return false; + } + }); + + passwordVerificationField.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } - private ProviderCredentialsBaseActivity activity; + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } - ProviderCredentialsReceiver(ProviderCredentialsBaseActivity activity) { - this.activity = activity; + @Override + public void afterTextChanged(Editable s) { + if(getPassword().equals(getPasswordVerification())) { + passwordVerificationError.setError(null); + } else { + passwordVerificationError.setError(getString(R.string.password_mismatch)); + } + } + }); + passwordVerificationField.setOnEditorActionListener(new TextView.OnEditorActionListener() { + @Override + public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { + if (actionId == IME_ACTION_DONE + || event != null && event.getAction() == KeyEvent.ACTION_DOWN + && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { + button.performClick(); + return true; + } + return false; + } + }); + } + + private void handleReceivedErrors(Bundle arguments) { + if (arguments.containsKey(ERRORS.PASSWORD_INVALID_LENGTH.toString())) + passwordError.setError(getString(R.string.error_not_valid_password_user_message)); + else if (arguments.containsKey(ERRORS.RISEUP_WARNING.toString())) { + userMessage.setVisibility(VISIBLE); + userMessage.setText(R.string.login_riseup_warning); + } + if (arguments.containsKey(USERNAME)) { + String username = arguments.getString(USERNAME); + usernameField.setText(username); + } + if (arguments.containsKey(ERRORS.USERNAME_MISSING.toString())) { + usernameError.setError(getString(R.string.username_ask)); + } + if (arguments.containsKey(getString(R.string.user_message))) { + userMessage.setText(arguments.getString(getString(R.string.user_message))); + userMessage.setVisibility(VISIBLE); + } else if (userMessage.getVisibility() != VISIBLE) { + userMessage.setVisibility(GONE); } + if (!usernameField.getText().toString().isEmpty() && passwordField.isFocusable()) + passwordField.requestFocus(); + + hideProgressBar(); + } + + public class ProviderAPIBroadcastReceiver extends BroadcastReceiver { @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); - activity.login(username, password); - } else if (resultCode == ProviderAPI.FAILED_SIGNUP) { - //MainActivity.sessionDialog(resultData); - } else if (resultCode == ProviderAPI.SUCCESSFUL_LOGIN) { - Intent intent = new Intent(activity, MainActivity.class); - intent.setAction(ACTION_SHOW_VPN_FRAGMENT); - activity.startActivity(intent); - } else if (resultCode == ProviderAPI.FAILED_LOGIN) { - //MainActivity.sessionDialog(resultData); -// TODO MOVE -// } else if (resultCode == ProviderAPI.SUCCESSFUL_LOGOUT) { -// if (switching_provider) activity.switchProvider(); -// } else if (resultCode == ProviderAPI.LOGOUT_FAILED) { -// activity.setResult(RESULT_CANCELED); -// } else if (resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE) { -// activity.eip_fragment.updateEipService(); -// activity.setResult(RESULT_OK); -// } else if (resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE) { -// activity.setResult(RESULT_CANCELED); -// } else if (resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_EIP_SERVICE) { -// activity.eip_fragment.updateEipService(); -// activity.setResult(RESULT_OK); -// } else if (resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_EIP_SERVICE) { -// activity.setResult(RESULT_CANCELED); + public void onReceive(Context context, Intent intent) { + Log.d(TAG, "received Broadcast"); + + String action = intent.getAction(); + if (action == null || !action.equalsIgnoreCase(PROVIDER_API_EVENT)) { + return; + } + + int resultCode = intent.getIntExtra(RESULT_CODE, -1); + switch (resultCode) { + case ProviderAPI.SUCCESSFUL_SIGNUP: + case ProviderAPI.SUCCESSFUL_LOGIN: + downloadVpnCertificate(); + break; + case ProviderAPI.FAILED_LOGIN: + case ProviderAPI.FAILED_SIGNUP: + handleReceivedErrors((Bundle) intent.getParcelableExtra(RESULT_KEY)); + break; + + case ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE: + intent = new Intent(ProviderCredentialsBaseActivity.this, MainActivity.class); + intent.setAction(ACTION_SHOW_VPN_FRAGMENT); + startActivity(intent); + //activity.eip_fragment.updateEipService(); + break; + case ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE: + // TODO activity.setResult(RESULT_CANCELED); + break; } } } - } diff --git a/app/src/main/java/se/leap/bitmaskclient/SignupActivity.java b/app/src/main/java/se/leap/bitmaskclient/SignupActivity.java index f6344065..0ff792bb 100644 --- a/app/src/main/java/se/leap/bitmaskclient/SignupActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/SignupActivity.java @@ -2,65 +2,38 @@ package se.leap.bitmaskclient; import android.os.Bundle; import android.support.annotation.Nullable; -import android.support.design.widget.TextInputEditText; -import android.support.design.widget.TextInputLayout; -import android.text.Editable; -import android.text.TextWatcher; +import android.view.View; -import butterknife.InjectView; import butterknife.OnClick; +import static android.view.View.VISIBLE; + /** * Create an account with a provider */ public class SignupActivity extends ProviderCredentialsBaseActivity { - @InjectView(R.id.provider_credentials_password_verification) - TextInputEditText providerCredentialsPasswordVerification; - - @InjectView(R.id.provider_credentials_password_verification_layout) - TextInputLayout providerCredentialsPasswordVerificationLayout; - @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.a_signup); - setProviderHeaderText("providerNAME"); setProviderHeaderLogo(R.drawable.mask); + setProviderHeaderText(R.string.create_profile); + setProgressbarText(R.string.signing_up); setButtonText(R.string.signup_button); - providerCredentialsPasswordVerification.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - - @Override - public void afterTextChanged(Editable s) { - if(getPassword().equals(getPasswordVerification())) { - providerCredentialsPasswordVerificationLayout.setError(null); - } else { - providerCredentialsPasswordVerificationLayout.setError(getString(R.string.password_mismatch)); - } - } - }); + passwordVerificationField.setVisibility(VISIBLE); + passwordVerificationError.setVisibility(VISIBLE); } @Override @OnClick(R.id.button) void handleButton() { + super.handleButton(); if (getPassword().equals(getPasswordVerification())) { signUp(getUsername(), getPassword()); } } - - private String getPasswordVerification() { - return providerCredentialsPasswordVerification.getText().toString(); - } } |