From 6c79290b1783a303fad5ea8be3c3583cc79dad84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 4 Dec 2014 00:46:25 +0100 Subject: Learning to use Butterknife, refactoring small things. --- app/build.gradle | 1 + .../se/leap/bitmaskclient/ConfigurationWizard.java | 140 +++++++++------------ .../main/java/se/leap/bitmaskclient/Dashboard.java | 5 +- .../main/java/se/leap/bitmaskclient/Provider.java | 66 +++------- .../se/leap/bitmaskclient/ConfigurationWizard.java | 90 ++++++------- 5 files changed, 122 insertions(+), 180 deletions(-) (limited to 'app') diff --git a/app/build.gradle b/app/build.gradle index 9c1d5b4e..6cf06281 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -47,6 +47,7 @@ android { dependencies { androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.2.1' + compile 'com.jakewharton:butterknife:6.0.0+' compile 'com.intellij:annotations:12.0' } diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java index 7c79c7c7..65cab91e 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2013 LEAP Encryption Access Project and contributers + * Copyright (c) 2013 LEAP Encryption Access Project and contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,9 +23,13 @@ import android.view.*; import android.widget.*; import java.io.*; import java.net.*; -import java.util.*; + +import butterknife.ButterKnife; +import butterknife.InjectView; +import org.jetbrains.annotations.NotNull; import org.json.*; +import butterknife.OnItemSelected; import se.leap.bitmaskclient.DownloadFailedDialog.DownloadFailedDialogInterface; import se.leap.bitmaskclient.NewProviderDialog.NewProviderDialogInterface; import se.leap.bitmaskclient.ProviderAPIResultReceiver.Receiver; @@ -41,11 +45,14 @@ import se.leap.bitmaskclient.eip.Constants; * @author parmegv * */ -public class ConfigurationWizard extends Activity +public class ConfigurationWizard extends ListActivity implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderDetailFragmentInterface, DownloadFailedDialogInterface, Receiver { - private ProgressBar mProgressBar; - private TextView progressbar_description; + @InjectView(R.id.progressbar_configuration_wizard) + ProgressBar mProgressBar; + @InjectView(R.id.progressbar_description) + TextView progressbar_description; + private ProviderListFragment provider_list_fragment; private Intent mConfigState = new Intent(); private ProviderItem selected_provider; @@ -73,7 +80,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD private int progress = -1; @Override - protected void onSaveInstanceState(Bundle outState) { + protected void onSaveInstanceState(@NotNull Bundle outState) { if(mProgressBar != null) outState.putInt(PROGRESSBAR_NUMBER, mProgressBar.getProgress()); if(progressbar_description != null) @@ -89,6 +96,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD super.onCreate(savedInstanceState); preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); + ButterKnife.inject(this); setUpInitialUI(); @@ -99,15 +107,19 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD setUpProviderList(); if(savedInstanceState != null) { - progressbar_text = savedInstanceState.getString(PROGRESSBAR_TEXT, ""); - provider_name = savedInstanceState.getString(Provider.NAME, ""); - selected_provider = getProvider(provider_name); - progress = savedInstanceState.getInt(PROGRESSBAR_NUMBER, -1); - providerAPI_result_receiver = savedInstanceState.getParcelable(ProviderAPI.RECEIVER_KEY); - providerAPI_result_receiver.setReceiver(this); + restoreState(savedInstanceState); } } + private void restoreState(Bundle savedInstanceState) { + progressbar_text = savedInstanceState.getString(PROGRESSBAR_TEXT, ""); + provider_name = savedInstanceState.getString(Provider.NAME, ""); + selected_provider = getProvider(provider_name); + progress = savedInstanceState.getInt(PROGRESSBAR_NUMBER, -1); + providerAPI_result_receiver = savedInstanceState.getParcelable(ProviderAPI.RECEIVER_KEY); + providerAPI_result_receiver.setReceiver(this); + } + @Override protected void onPostResume() { super.onPostResume(); @@ -128,10 +140,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD } private void hideProgressBar() { - mProgressBar = (ProgressBar) findViewById(R.id.progressbar_configuration_wizard); mProgressBar.setVisibility(ProgressBar.INVISIBLE); - - progressbar_description = (TextView) findViewById(R.id.progressbar_description); progressbar_description.setVisibility(TextView.INVISIBLE); } @@ -145,11 +154,6 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD provider_list_fragment.setArguments(arguments); - putProviderListFragment(provider_list_fragment); - } - - private void putProviderListFragment(ProviderListFragment fragment) { - fragment_manager.replace(R.id.configuration_wizard_layout, provider_list_fragment, ProviderListFragment.TAG); } @Override @@ -194,7 +198,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD setResult(RESULT_CANCELED, mConfigState); String reason_to_fail = resultData.getString(ProviderAPI.ERRORS); - showDownloadFailedDialog(getCurrentFocus(), reason_to_fail); + showDownloadFailedDialog(reason_to_fail); } else if(resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE) { mProgressBar.incrementProgressBy(1); @@ -256,21 +260,14 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD } private void usualBackButton() { - try { - boolean is_provider_set_up = new JSONObject(preferences.getString(Provider.KEY, "no provider")) != null ? true : false; - boolean is_provider_set_up_truly = new JSONObject(preferences.getString(Provider.KEY, "no provider")).length() != 0 ? true : false; - if(!is_provider_set_up || !is_provider_set_up_truly) { - askDashboardToQuitApp(); - } else { - setResult(RESULT_OK); - } - } catch (JSONException e) { - askDashboardToQuitApp(); - super.onBackPressed(); - e.printStackTrace(); - } - super.onBackPressed(); + if(preferences.getString(Provider.KEY, "").isEmpty()) { + askDashboardToQuitApp(); + } else { + setResult(RESULT_OK); + } + super.onBackPressed(); } + private void askDashboardToQuitApp() { Intent ask_quit = new Intent(); ask_quit.putExtra(Dashboard.ACTION_QUIT, Dashboard.ACTION_QUIT); @@ -278,13 +275,11 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD } private ProviderItem getProvider(String name) { - Iterator providers_iterator = ProviderListContent.ITEMS.iterator(); - while(providers_iterator.hasNext()) { - ProviderItem provider = providers_iterator.next(); - if(provider.name().equalsIgnoreCase(name)) { - return provider; - } - } + for (ProviderItem provider : ProviderListContent.ITEMS) { + if (provider.name().equalsIgnoreCase(name)) { + return provider; + } + } return null; } @@ -301,13 +296,11 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD private int getProviderIndex(String id) { int index = 0; - Iterator providers_iterator = ProviderListContent.ITEMS.iterator(); - while(providers_iterator.hasNext()) { - ProviderItem provider = providers_iterator.next(); - if(provider.name().equalsIgnoreCase(id)) { - break; - } else index++; - } + for (ProviderItem provider : ProviderListContent.ITEMS) { + if (provider.name().equalsIgnoreCase(id)) { + break; + } else index++; + } return index; } @@ -358,15 +351,16 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD } } - private String extractProviderMainUrlFromAssetsFile(String filepath) { + private String extractProviderMainUrlFromAssetsFile(String file_path) { String provider_main_url = ""; try { - InputStream input_stream_file_contents = getAssets().open(filepath); + InputStream input_stream_file_contents = getAssets().open(file_path); byte[] urls_file_bytes = new byte[input_stream_file_contents.available()]; - input_stream_file_contents.read(urls_file_bytes); - String urls_file_content = new String(urls_file_bytes); - JSONObject file_contents = new JSONObject(urls_file_content); - provider_main_url = file_contents.getString(Provider.MAIN_URL); + if(input_stream_file_contents.read(urls_file_bytes) > 0) { + String urls_file_content = new String(urls_file_bytes); + JSONObject file_contents = new JSONObject(urls_file_content); + provider_main_url = file_contents.getString(Provider.MAIN_URL); + } } catch (JSONException e) { } catch (IOException e) { } @@ -376,14 +370,12 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD private String getId(String provider_main_url) { try { URL provider_url = new URL(provider_main_url); - Iterator providers_iterator = ProviderListContent.ITEMS.iterator(); - while(providers_iterator.hasNext()) { - ProviderItem provider = providers_iterator.next(); - URL aux_provider_url = new URL(provider.providerMainUrl()); - if(isSameURL(provider_url, aux_provider_url)) { - return provider.name(); - } - } + for (ProviderItem provider : ProviderListContent.ITEMS) { + URL aux_provider_url = new URL(provider.providerMainUrl()); + if (isSameURL(provider_url, aux_provider_url)) { + return provider.name(); + } + } } catch (MalformedURLException e) { e.printStackTrace(); } @@ -399,16 +391,9 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD * same protocol, false otherwise. */ private boolean isSameURL(final URL url, final URL baseUrl) { - if (!url.getProtocol().equals(baseUrl.getProtocol())) { - return false; - } - if (!url.getHost().equals(baseUrl.getHost())) { - return false; - } - if (url.getPort() != baseUrl.getPort()) { - return false; - } - return true; + return url.getProtocol().equals(baseUrl.getProtocol()) && + url.getHost().equals(baseUrl.getHost()) && + url.getPort() == baseUrl.getPort(); } /** @@ -455,10 +440,9 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD * Once selected a provider, this fragment offers the user to log in, * use it anonymously (if possible) * or cancel his/her election pressing the back button. - * @param view - * @param reason_to_fail + * @param reason_to_fail */ - public void showDownloadFailedDialog(View view, String reason_to_fail) { + public void showDownloadFailedDialog(String reason_to_fail) { FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(DownloadFailedDialog.TAG); DialogFragment newFragment = DownloadFailedDialog.newInstance(reason_to_fail); @@ -492,7 +476,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD } private void autoSelectProvider(String provider_main_url, boolean danger_on) { - preferences.edit().putBoolean(ProviderItem.DANGER_ON, danger_on).commit(); + preferences.edit().putBoolean(ProviderItem.DANGER_ON, danger_on).apply(); onItemSelected(getId(provider_main_url)); } @@ -559,7 +543,7 @@ n * @param provider_main_url if(provider_list_fragment != null && preferences.contains(ProviderItem.DANGER_ON)) { provider_list_fragment.removeLastItem(); } - preferences.edit().remove(Provider.KEY).remove(ProviderItem.DANGER_ON).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).commit(); + preferences.edit().remove(Provider.KEY).remove(ProviderItem.DANGER_ON).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).apply(); } @Override diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index 94de2fe8..c4f845b8 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -86,8 +86,8 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); handleVersion(); - boolean provider_configured = preferences.getString(Constants.KEY, "").isEmpty(); - if (provider_configured) + boolean no_provider_configured = preferences.getString(Constants.KEY, "").isEmpty(); + if (no_provider_configured) startActivityForResult(new Intent(this,ConfigurationWizard.class),CONFIGURE_LEAP); else buildDashboard(getIntent().getBooleanExtra(ON_BOOT, false)); @@ -135,6 +135,7 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf preferences.edit().putInt(Constants.PARSED_SERIAL, 0).apply(); preferences.edit().putBoolean(Constants.AUTHED_EIP, authed_eip).apply(); updateEipService(); + buildDashboard(false); invalidateOptionsMenu(); if(data != null && data.hasExtra(LogInDialog.TAG)) { logInDialog(Bundle.EMPTY); diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index fa1a4fb5..bb30905c 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -110,11 +110,10 @@ public final class Provider implements Serializable { } protected String getDomain(){ - String domain = "Null"; + String domain = ""; try { domain = definition.getString(API_TERM_DOMAIN); } catch (JSONException e) { - domain = "Null"; e.printStackTrace(); } return domain; @@ -157,58 +156,25 @@ public final class Provider implements Serializable { } protected boolean hasEIP() { - JSONArray services = null; try { - services = definition.getJSONArray(API_TERM_SERVICES); // returns ["openvpn"] + JSONArray services = definition.getJSONArray(API_TERM_SERVICES); // returns ["openvpn"] + for (int i=0;i providers_iterator = ProviderListContent.ITEMS.iterator(); - while(providers_iterator.hasNext()) { - ProviderItem provider = providers_iterator.next(); - if(provider.name().equalsIgnoreCase(name)) { - return provider; - } + for (ProviderItem provider : ProviderListContent.ITEMS) { + if(provider.name().equalsIgnoreCase(name)) { + return provider; } - return null; + } + return null; } private void startProgressBar() { @@ -299,14 +300,12 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD private int getProviderIndex(String id) { int index = 0; - Iterator providers_iterator = ProviderListContent.ITEMS.iterator(); - while(providers_iterator.hasNext()) { - ProviderItem provider = providers_iterator.next(); - if(provider.name().equalsIgnoreCase(id)) { - break; - } else index++; - } - return index; + for (ProviderItem provider : ProviderListContent.ITEMS) { + if(provider.name().equalsIgnoreCase(id)) { + break; + } else index++; + } + return index; } private int listItemHeight() { @@ -314,8 +313,8 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD ListAdapter provider_list_adapter = provider_list_view.getAdapter(); View listItem = provider_list_adapter.getView(0, null, provider_list_view); listItem.setLayoutParams(new RelativeLayout.LayoutParams( - RelativeLayout.LayoutParams.WRAP_CONTENT, - RelativeLayout.LayoutParams.WRAP_CONTENT)); + RelativeLayout.LayoutParams.WRAP_CONTENT, + RelativeLayout.LayoutParams.WRAP_CONTENT)); WindowManager wm = (WindowManager) getApplicationContext() .getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); @@ -361,10 +360,11 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD try { InputStream input_stream_file_contents = getAssets().open(filepath); byte[] urls_file_bytes = new byte[input_stream_file_contents.available()]; - input_stream_file_contents.read(urls_file_bytes); - String urls_file_content = new String(urls_file_bytes); - JSONObject file_contents = new JSONObject(urls_file_content); - provider_main_url = file_contents.getString(Provider.MAIN_URL); + if(input_stream_file_contents.read(urls_file_bytes) > 0) { + String urls_file_content = new String(urls_file_bytes); + JSONObject file_contents = new JSONObject(urls_file_content); + provider_main_url = file_contents.getString(Provider.MAIN_URL); + } } catch (JSONException e) { } catch (IOException e) { } @@ -373,15 +373,13 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD private String getId(String provider_main_url) { try { - URL provider_url = new URL(provider_main_url); - Iterator providers_iterator = ProviderListContent.ITEMS.iterator(); - while(providers_iterator.hasNext()) { - ProviderItem provider = providers_iterator.next(); - URL aux_provider_url = new URL(provider.providerMainUrl()); - if(isSameURL(provider_url, aux_provider_url)) { - return provider.name(); + URL provider_url = new URL(provider_main_url); + for (ProviderItem provider : ProviderListContent.ITEMS) { + URL aux_provider_url = new URL(provider.providerMainUrl()); + if(isSameURL(provider_url, aux_provider_url)) { + return provider.name(); + } } - } } catch (MalformedURLException e) { e.printStackTrace(); } @@ -397,16 +395,9 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD * same protocol, false otherwise. */ private boolean isSameURL(final URL url, final URL baseUrl) { - if (!url.getProtocol().equals(baseUrl.getProtocol())) { - return false; - } - if (!url.getHost().equals(baseUrl.getHost())) { - return false; - } - if (url.getPort() != baseUrl.getPort()) { - return false; - } - return true; + return url.getProtocol().equals(baseUrl.getProtocol()) && + url.getHost().equals(baseUrl.getHost()) && + url.getPort() == baseUrl.getPort(); } /** @@ -452,10 +443,9 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD * Once selected a provider, this fragment offers the user to log in, * use it anonymously (if possible) * or cancel his/her election pressing the back button. - * @param view * @param reason_to_fail */ - public void showDownloadFailedDialog(View view, String reason_to_fail) { + public void showDownloadFailedDialog(String reason_to_fail) { FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(DownloadFailedDialog.TAG); DialogFragment newFragment = DownloadFailedDialog.newInstance(reason_to_fail); @@ -555,7 +545,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD if(provider_list_fragment != null) { provider_list_fragment.removeLastItem(); } - preferences.edit().remove(Provider.KEY).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).commit(); + preferences.edit().remove(Provider.KEY).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).apply(); } @Override -- cgit v1.2.3 From 568e5b77e7e34e10e5585614f5587df29f45cfd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 4 Dec 2014 01:14:03 +0100 Subject: CW loads, but no provider list appears. --- app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java | 7 ++++--- app/src/main/java/se/leap/bitmaskclient/ProviderListFragment.java | 5 ++++- app/src/main/res/layout/configuration_wizard_activity.xml | 1 - 3 files changed, 8 insertions(+), 5 deletions(-) (limited to 'app') diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java index 65cab91e..55bb43a2 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -45,7 +45,7 @@ import se.leap.bitmaskclient.eip.Constants; * @author parmegv * */ -public class ConfigurationWizard extends ListActivity +public class ConfigurationWizard extends Activity implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderDetailFragmentInterface, DownloadFailedDialogInterface, Receiver { @InjectView(R.id.progressbar_configuration_wizard) @@ -96,7 +96,6 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD super.onCreate(savedInstanceState); preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); - ButterKnife.inject(this); setUpInitialUI(); @@ -136,7 +135,9 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD private void setUpInitialUI() { setContentView(R.layout.configuration_wizard_activity); - hideProgressBar(); + ButterKnife.inject(this); + + hideProgressBar(); } private void hideProgressBar() { diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderListFragment.java b/app/src/main/java/se/leap/bitmaskclient/ProviderListFragment.java index e5baebc0..6d40bf9f 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderListFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderListFragment.java @@ -24,6 +24,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ListView; +import butterknife.ButterKnife; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; /** @@ -107,7 +108,9 @@ public class ProviderListFragment extends ListFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) { - return inflater.inflate(R.layout.provider_list_fragment, container, false); + View view = inflater.inflate(R.layout.provider_list_fragment, container, false); + ButterKnife.inject(this, view); + return view; } @Override diff --git a/app/src/main/res/layout/configuration_wizard_activity.xml b/app/src/main/res/layout/configuration_wizard_activity.xml index f3d0e48b..49c4b2b9 100644 --- a/app/src/main/res/layout/configuration_wizard_activity.xml +++ b/app/src/main/res/layout/configuration_wizard_activity.xml @@ -22,5 +22,4 @@ android:textAppearance="?android:attr/textAppearanceMedium" android:layout_centerHorizontal="true" android:textColor="@android:color/holo_blue_bright" /> - \ No newline at end of file -- cgit v1.2.3 From f9a200a7004b74cb2d64e37129e6234427c84c7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 4 Dec 2014 16:38:21 +0100 Subject: Renderers and dagger, refactoring CW. --- app/build.gradle | 2 + .../se/leap/bitmaskclient/ConfigurationWizard.java | 103 +++++++----------- .../main/java/se/leap/bitmaskclient/Dashboard.java | 15 +-- .../main/java/se/leap/bitmaskclient/Provider.java | 116 ++++++++++---------- .../se/leap/bitmaskclient/ProviderManager.java | 120 +++++++++++++++++++++ .../se/leap/bitmaskclient/ProviderRenderer.java | 55 ++++++++++ .../bitmaskclient/ProviderRendererBuilder.java | 25 +++++ .../res/layout/configuration_wizard_activity.xml | 6 ++ app/src/main/res/layout/provider_list_item.xml | 8 +- .../se/leap/bitmaskclient/ConfigurationWizard.java | 100 +++++++---------- 10 files changed, 352 insertions(+), 198 deletions(-) create mode 100644 app/src/main/java/se/leap/bitmaskclient/ProviderManager.java create mode 100644 app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java create mode 100644 app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java (limited to 'app') diff --git a/app/build.gradle b/app/build.gradle index 6cf06281..4211e379 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -48,6 +48,8 @@ android { dependencies { androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.2.1' compile 'com.jakewharton:butterknife:6.0.0+' + provided 'com.squareup.dagger:dagger-compiler:1.2.2+' + compile 'com.github.pedrovgs:renderers:1.3+' compile 'com.intellij:annotations:12.0' } diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java index 55bb43a2..f178b754 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -21,15 +21,21 @@ import android.content.*; import android.os.*; import android.view.*; import android.widget.*; + +import com.pedrogomez.renderers.Renderer; +import com.pedrogomez.renderers.RendererAdapter; + import java.io.*; import java.net.*; +import java.util.*; import butterknife.ButterKnife; import butterknife.InjectView; import org.jetbrains.annotations.NotNull; import org.json.*; -import butterknife.OnItemSelected; +import javax.inject.Inject; + import se.leap.bitmaskclient.DownloadFailedDialog.DownloadFailedDialogInterface; import se.leap.bitmaskclient.NewProviderDialog.NewProviderDialogInterface; import se.leap.bitmaskclient.ProviderAPIResultReceiver.Receiver; @@ -48,11 +54,13 @@ import se.leap.bitmaskclient.eip.Constants; public class ConfigurationWizard extends Activity implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderDetailFragmentInterface, DownloadFailedDialogInterface, Receiver { - @InjectView(R.id.progressbar_configuration_wizard) - ProgressBar mProgressBar; - @InjectView(R.id.progressbar_description) - TextView progressbar_description; + @InjectView(R.id.progressbar_configuration_wizard) ProgressBar mProgressBar; + @InjectView(R.id.progressbar_description) TextView progressbar_description; + + @InjectView(R.id.provider_list) ListView provider_list_view; + @Inject RendererAdapter adapter; + private ProviderManager provider_manager; private ProviderListFragment provider_list_fragment; private Intent mConfigState = new Intent(); private ProviderItem selected_provider; @@ -64,7 +72,6 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD final protected static String PROVIDER_SET = "PROVIDER SET"; final protected static String SERVICES_RETRIEVED = "SERVICES RETRIEVED"; - final protected static String ASSETS_URL_FOLDER = "urls"; final private static String PROGRESSBAR_TEXT = TAG + "PROGRESSBAR_TEXT"; final private static String PROGRESSBAR_NUMBER = TAG + "PROGRESSBAR_NUMBER"; @@ -79,6 +86,14 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD private String provider_name = ""; private int progress = -1; + private void initProviderList() { + List> prototypes = new ArrayList>(); + prototypes.add(new ProviderRenderer(this)); + ProviderRendererBuilder providerRendererBuilder = new ProviderRendererBuilder(prototypes); + adapter = new RendererAdapter(getLayoutInflater(), providerRendererBuilder, provider_manager); + provider_list_view.setAdapter(adapter); + } + @Override protected void onSaveInstanceState(@NotNull Bundle outState) { if(mProgressBar != null) @@ -96,11 +111,10 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD super.onCreate(savedInstanceState); preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); + provider_manager = new ProviderManager(getAssets()); setUpInitialUI(); - loadPreseededProviders(); - setUpProviderAPIResultReceiver(); setUpProviderList(); @@ -146,14 +160,15 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD } private void setUpProviderList() { - provider_list_fragment = ProviderListFragment.newInstance(); - - Bundle arguments = new Bundle(); - int configuration_wizard_request_code = getIntent().getIntExtra(Dashboard.REQUEST_CODE, -1); - if(configuration_wizard_request_code == Dashboard.SWITCH_PROVIDER) - arguments.putBoolean(ProviderListFragment.SHOW_ALL_PROVIDERS, true); - - provider_list_fragment.setArguments(arguments); + initProviderList(); + // provider_list_fragment = ProviderListFragment.newInstance(); + + // Bundle arguments = new Bundle(); + // int configuration_wizard_request_code = getIntent().getIntExtra(Dashboard.REQUEST_CODE, -1); + // if(configuration_wizard_request_code == Dashboard.SWITCH_PROVIDER) + // arguments.putBoolean(ProviderListFragment.SHOW_ALL_PROVIDERS, true); + + // provider_list_fragment.setArguments(arguments); } @@ -325,49 +340,6 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD return listItem.getMeasuredHeight(); } - /** - * Loads providers data from url files contained in the assets folder - * @return true if the files were correctly read - */ - private boolean loadPreseededProviders() { - boolean loaded_preseeded_providers = true; - try { - //TODO Put that folder in a better place (also inside the "for") - String[] urls_filepaths = getAssets().list(ASSETS_URL_FOLDER); - for(String url_filepath : urls_filepaths) { - addNewProviderToList(url_filepath); - } - } catch (IOException e) { - loaded_preseeded_providers = false; - } - - return loaded_preseeded_providers; - } - - private void addNewProviderToList(String url_filepath) { - String provider_main_url = extractProviderMainUrlFromAssetsFile(ASSETS_URL_FOLDER + "/" + url_filepath); - if(getId(provider_main_url).isEmpty()) { - String provider_name = url_filepath.subSequence(0, url_filepath.lastIndexOf(".")).toString(); - ProviderListContent.addItem(new ProviderItem(provider_name, provider_main_url)); - } - } - - private String extractProviderMainUrlFromAssetsFile(String file_path) { - String provider_main_url = ""; - try { - InputStream input_stream_file_contents = getAssets().open(file_path); - byte[] urls_file_bytes = new byte[input_stream_file_contents.available()]; - if(input_stream_file_contents.read(urls_file_bytes) > 0) { - String urls_file_content = new String(urls_file_bytes); - JSONObject file_contents = new JSONObject(urls_file_content); - provider_main_url = file_contents.getString(Provider.MAIN_URL); - } - } catch (JSONException e) { - } catch (IOException e) { - } - return provider_main_url; - } - private String getId(String provider_main_url) { try { URL provider_url = new URL(provider_main_url); @@ -465,17 +437,14 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD } public void showAndSelectProvider(String provider_main_url, boolean danger_on) { - if(getId(provider_main_url).isEmpty()) - showProvider(provider_main_url); + try { + provider_manager.add(new Provider(new URL((provider_main_url)))); + } catch (MalformedURLException e) { + e.printStackTrace(); + } autoSelectProvider(provider_main_url, danger_on); } - private void showProvider(final String provider_main_url) { - String provider_name = provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("\\/", "_"); - ProviderItem added_provider = new ProviderItem(provider_name, provider_main_url); - provider_list_fragment.addItem(added_provider); - } - private void autoSelectProvider(String provider_main_url, boolean danger_on) { preferences.edit().putBoolean(ProviderItem.DANGER_ON, danger_on).apply(); onItemSelected(getId(provider_main_url)); diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index c4f845b8..e1d08d08 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -71,9 +71,10 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf private TextView status_message; public ProviderAPIResultReceiver providerAPI_result_receiver; + private Provider provider; private static boolean authed_eip; - - @Override + + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -137,9 +138,12 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf updateEipService(); buildDashboard(false); invalidateOptionsMenu(); - if(data != null && data.hasExtra(LogInDialog.TAG)) { + if(data != null) + if(data.hasExtra(LogInDialog.TAG)) { logInDialog(Bundle.EMPTY); } + if(data.hasExtra(Provider.KEY)) + provider = data.getParcelableExtra(Provider.KEY); } else if(resultCode == RESULT_CANCELED && (data == null || data.hasExtra(ACTION_QUIT))) { finish(); } else @@ -180,9 +184,6 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf * service dependent UI elements to include. */ private void buildDashboard(boolean hide_and_turn_on_eip) { - Provider provider = Provider.getInstance(); - provider.init( this ); - setContentView(R.layout.client_dashboard); TextView providerNameTV = (TextView) findViewById(R.id.providerName); @@ -262,7 +263,7 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf startActivity(startLW); return true; case R.id.switch_provider: - if (Provider.getInstance().hasEIP()){ + if (provider.hasEIP()){ if (preferences.getBoolean(Constants.AUTHED_EIP, false)){ logOut(); } diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index bb30905c..5dd54175 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -19,12 +19,16 @@ package se.leap.bitmaskclient; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; +import android.os.Parcel; +import android.os.Parcelable; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.Serializable; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Arrays; import java.util.Locale; @@ -32,16 +36,11 @@ import java.util.Locale; * @author Sean Leonard * */ -public final class Provider implements Serializable { +public final class Provider implements Parcelable { - private static final long serialVersionUID = 6003835972151761353L; - - private static Provider instance = null; - - // We'll access our preferences here - private static SharedPreferences preferences = null; - // Represents our Provider's provider.json - private static JSONObject definition = null; + private JSONObject definition; // Represents our Provider's provider.json + private URL main_url; + private boolean is_custom = false; final public static String API_URL = "api_uri", @@ -69,54 +68,39 @@ public final class Provider implements Serializable { private static final String API_TERM_DEFAULT_LANGUAGE = "default_language"; protected static final String[] API_EIP_TYPES = {"openvpn"}; - private static final String PREFS_EIP_NAME = null; + public Provider(URL main_url) { + this.main_url = main_url; + } + public static final Parcelable.Creator CREATOR + = new Parcelable.Creator() { + public Provider createFromParcel(Parcel in) { + return new Provider(in); + } - - // What, no individual fields?! We're going to gamble on org.json.JSONObject and JSONArray - // Supporting multiple API versions will probably break this paradigm, - // Forcing me to write a real constructor and rewrite getters/setters - // Also will refactor if i'm instantiating the same local variables all the time - - /** - * - */ - private Provider() {} - - protected static Provider getInstance(){ - if(instance==null){ - instance = new Provider(); - } - return instance; - } + public Provider[] newArray(int size) { + return new Provider[size]; + } + }; - protected void init(Activity activity) { - - // Load our preferences from SharedPreferences - // If there's nothing there, we will end up returning a rather empty object - // to whoever called getInstance() and they can run the First Run Wizard - //preferences = context.getgetPreferences(0); // 0 == MODE_PRIVATE, but we don't extend Android's classes... - - // Load SharedPreferences - preferences = activity.getSharedPreferences(Dashboard.SHARED_PREFERENCES,Context.MODE_PRIVATE); - // Inflate our provider.json data - try { - definition = new JSONObject( preferences.getString(Provider.KEY, "") ); - } catch (JSONException e) { - // TODO: handle exception - - // FIXME!! We want "real" data!! - } - } + private Provider(Parcel in) { + try { + main_url = new URL(in.readString()); + definition = new JSONObject((in.readString())); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + } + + + protected void define(JSONObject provider_json) { + definition = provider_json; + } protected String getDomain(){ - String domain = ""; - try { - domain = definition.getString(API_TERM_DOMAIN); - } catch (JSONException e) { - e.printStackTrace(); - } - return domain; + return main_url.getHost(); } protected String getName(){ @@ -124,15 +108,12 @@ public final class Provider implements Serializable { String lang = Locale.getDefault().getLanguage(); String name = "Null"; // Should it actually /be/ null, for error conditions? try { - name = definition.getJSONObject(API_TERM_NAME).getString(lang); + if(definition != null) + name = definition.getJSONObject(API_TERM_NAME).getString(lang); + else throw new JSONException("Provider not defined"); } catch (JSONException e) { - // TODO: Nesting try/catch blocks? Crazy - // Maybe you should actually handle exception? - try { - name = definition.getJSONObject(API_TERM_NAME).getString( definition.getString(API_TERM_DEFAULT_LANGUAGE) ); - } catch (JSONException e2) { - // TODO: Will you handle the exception already? - } + String host = main_url.getHost(); + name = host.substring(0, host.indexOf(".")); } return name; @@ -177,4 +158,19 @@ public final class Provider implements Serializable { } return false; } + + protected boolean isCustom() { + return is_custom; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeString(main_url.toString()); + parcel.writeString(definition.toString()); + } } diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java new file mode 100644 index 00000000..59507d9c --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java @@ -0,0 +1,120 @@ +package se.leap.bitmaskclient; + +import android.content.res.AssetManager; + +import com.pedrogomez.renderers.AdapteeCollection; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +/** + * Created by parmegv on 4/12/14. + */ +public class ProviderManager implements AdapteeCollection { + + private AssetManager assets_manager; + private Set default_providers; + private Set custom_providers; + + final protected static String URLS = "urls"; + + public ProviderManager(AssetManager assets_manager) { + this.assets_manager = assets_manager; + default_providers = default_providers(); + custom_providers = new HashSet(); + } + + /** + * Loads providers data from url files contained in the assets folder + * @return true if the files were correctly read + */ + private Set default_providers() { + Set providers = new HashSet(); + try { + for(String file : assets_manager.list(URLS)) { + String main_url = extractProviderMainUrlFromAssetsFile(URLS + "/" + file); + providers.add(new Provider(new URL(main_url))); + } + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return providers; + } + + private String extractProviderMainUrlFromAssetsFile(String file_path) { + String provider_main_url = ""; + try { + InputStream input_stream_file_contents = assets_manager.open(file_path); + byte[] urls_file_bytes = new byte[input_stream_file_contents.available()]; + if(input_stream_file_contents.read(urls_file_bytes) > 0) { + String urls_file_content = new String(urls_file_bytes); + JSONObject file_contents = new JSONObject(urls_file_content); + provider_main_url = file_contents.getString(Provider.MAIN_URL); + } + } catch (JSONException e) { + } catch (IOException e) { + } + return provider_main_url; + } + + public Set providers() { + Set all_providers = new HashSet(); + all_providers.addAll(default_providers); + all_providers.addAll(custom_providers); + return all_providers; + } + + @Override + public int size() { + return providers().size(); + } + + @Override + public Provider get(int index) { + Iterator iterator = providers().iterator(); + while(iterator.hasNext() && index > 0) { + iterator.next(); + index--; + } + return iterator.next(); + } + + @Override + public void add(Provider element) { + custom_providers.add(element); + } + + @Override + public void remove(Provider element) { + custom_providers.remove(element); + } + + @Override + public void addAll(Collection elements) { + custom_providers.addAll(elements); + } + + @Override + public void removeAll(Collection elements) { + custom_providers.removeAll(elements); + default_providers.removeAll(elements); + } + + @Override + public void clear() { + default_providers.clear(); + custom_providers.clear(); + } +} diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java b/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java new file mode 100644 index 00000000..2e376dee --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java @@ -0,0 +1,55 @@ +package se.leap.bitmaskclient; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import com.pedrogomez.renderers.Renderer; + +import butterknife.ButterKnife; +import butterknife.InjectView; + +/** + * Created by parmegv on 4/12/14. + */ +public class ProviderRenderer extends Renderer { + private final Context context; + + @InjectView(R.id.provider_name) + TextView name; + @InjectView(R.id.provider_domain) + TextView domain; + + public ProviderRenderer(Context context) { + this.context = context; + } + + @Override + protected View inflate(LayoutInflater inflater, ViewGroup parent) { + View view = inflater.inflate(R.layout.provider_list_item, parent, false); + ButterKnife.inject(this, view); + return view; + } + + @Override + protected void setUpView(View rootView) { + /* + * Empty implementation substituted with the usage of ButterKnife library by Jake Wharton. + */ + } + + @Override + protected void hookListeners(View rootView) { + //Empty + } + + @Override + public void render() { + Provider provider = getContent(); + name.setText(provider.getName()); + domain.setText(provider.getDomain()); + } +} diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java b/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java new file mode 100644 index 00000000..7366e68e --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java @@ -0,0 +1,25 @@ +package se.leap.bitmaskclient; + +import android.content.Context; + +import com.pedrogomez.renderers.Renderer; +import com.pedrogomez.renderers.RendererBuilder; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +import javax.inject.Inject; + +/** + * Created by parmegv on 4/12/14. + */ + public class ProviderRendererBuilder extends RendererBuilder { + public ProviderRendererBuilder(Collection> prototypes) { + super(prototypes); + } + @Override + protected Class getPrototypeClass(Provider content) { + return ProviderRenderer.class; + } +} diff --git a/app/src/main/res/layout/configuration_wizard_activity.xml b/app/src/main/res/layout/configuration_wizard_activity.xml index 49c4b2b9..a5bca1e9 100644 --- a/app/src/main/res/layout/configuration_wizard_activity.xml +++ b/app/src/main/res/layout/configuration_wizard_activity.xml @@ -5,6 +5,12 @@ android:layout_height="match_parent" tools:context=".ConfigurationWizard" > + + - - diff --git a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java index cb9f42fb..c47f68b9 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -21,10 +21,21 @@ import android.content.*; import android.os.*; import android.view.*; import android.widget.*; + +import com.pedrogomez.renderers.Renderer; +import com.pedrogomez.renderers.RendererAdapter; + import java.io.*; import java.net.*; +import java.util.*; + +import butterknife.ButterKnife; +import butterknife.InjectView; +import org.jetbrains.annotations.NotNull; import org.json.*; +import javax.inject.Inject; + import se.leap.bitmaskclient.DownloadFailedDialog.DownloadFailedDialogInterface; import se.leap.bitmaskclient.NewProviderDialog.NewProviderDialogInterface; import se.leap.bitmaskclient.ProviderAPIResultReceiver.Receiver; @@ -32,7 +43,6 @@ import se.leap.bitmaskclient.ProviderDetailFragment.ProviderDetailFragmentInterf import se.leap.bitmaskclient.ProviderListContent.ProviderItem; import se.leap.bitmaskclient.FragmentManagerEnhanced; import se.leap.bitmaskclient.eip.Constants; -import se.leap.bitmaskclient.R; /** * Activity that builds and shows the list of known available providers. @@ -46,11 +56,13 @@ public class ConfigurationWizard extends Activity implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderDetailFragmentInterface, DownloadFailedDialogInterface, Receiver { - @InjectView(R.id.progressbar_configuration_wizard) - private ProgressBar mProgressBar; - @InjectView(R.id.progressbar_description) - private TextView progressbar_description; + @InjectView(R.id.progressbar_configuration_wizard) ProgressBar mProgressBar; + @InjectView(R.id.progressbar_description) TextView progressbar_description; + @InjectView(R.id.provider_list) ListView provider_list_view; + @Inject RendererAdapter adapter; + + private ProviderManager provider_manager; private ProviderListFragment provider_list_fragment; private Intent mConfigState = new Intent(); private ProviderItem selected_provider; @@ -77,6 +89,14 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD private String provider_name = ""; private int progress = -1; + private void initProviderList() { + List> prototypes = new ArrayList>(); + prototypes.add(new ProviderRenderer(this)); + ProviderRendererBuilder providerRendererBuilder = new ProviderRendererBuilder(prototypes); + adapter = new RendererAdapter(getLayoutInflater(), providerRendererBuilder, provider_manager); + provider_list_view.setAdapter(adapter); + } + @Override protected void onSaveInstanceState(Bundle outState) { if(mProgressBar != null) @@ -94,25 +114,28 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD super.onCreate(savedInstanceState); preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); - + provider_manager = new ProviderManager(getAssets()); + setUpInitialUI(); - loadPreseededProviders(); - setUpProviderAPIResultReceiver(); setUpProviderList(); if ( savedInstanceState != null ) { - progressbar_text = savedInstanceState.getString(PROGRESSBAR_TEXT, ""); - provider_name = savedInstanceState.getString(Provider.NAME, ""); - selected_provider = getProvider(provider_name); - progress = savedInstanceState.getInt(PROGRESSBAR_NUMBER, -1); - providerAPI_result_receiver = savedInstanceState.getParcelable(ProviderAPI.RECEIVER_KEY); - providerAPI_result_receiver.setReceiver(this); + restoreState(savedInstanceState); } } + private void restoreState(Bundle savedInstanceState) { + progressbar_text = savedInstanceState.getString(PROGRESSBAR_TEXT, ""); + provider_name = savedInstanceState.getString(Provider.NAME, ""); + selected_provider = getProvider(provider_name); + progress = savedInstanceState.getInt(PROGRESSBAR_NUMBER, -1); + providerAPI_result_receiver = savedInstanceState.getParcelable(ProviderAPI.RECEIVER_KEY); + providerAPI_result_receiver.setReceiver(this); + } + @Override protected void onPostResume() { super.onPostResume(); @@ -129,19 +152,19 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD private void setUpInitialUI() { setContentView(R.layout.configuration_wizard_activity); - + ButterKnife.inject(this); + hideProgressBar(); } private void hideProgressBar() { - mProgressBar = (ProgressBar) findViewById(R.id.progressbar_configuration_wizard); mProgressBar.setVisibility(ProgressBar.INVISIBLE); - progressbar_description = (TextView) findViewById(R.id.progressbar_description); progressbar_description.setVisibility(TextView.INVISIBLE); } private void setUpProviderList() { + initProviderList(); provider_list_fragment = ProviderListFragment.newInstance(); Bundle arguments = new Bundle(); @@ -326,49 +349,6 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD listItem.measure(widthSpec, 0); return listItem.getMeasuredHeight(); -} - - /** - * Loads providers data from url files contained in the assets folder - * @return true if the files were correctly read - */ - private boolean loadPreseededProviders() { - boolean loaded_preseeded_providers = true; - try { - //TODO Put that folder in a better place (also inside the "for") - String[] urls_filepaths = getAssets().list(ASSETS_URL_FOLDER); - for(String url_filepath : urls_filepaths) { - addNewProviderToList(url_filepath); - } - } catch (IOException e) { - loaded_preseeded_providers = false; - } - - return loaded_preseeded_providers; - } - - private void addNewProviderToList(String url_filepath) { - String provider_main_url = extractProviderMainUrlFromAssetsFile(ASSETS_URL_FOLDER + "/" + url_filepath); - if(getId(provider_main_url).isEmpty()) { - String provider_name = url_filepath.subSequence(0, url_filepath.lastIndexOf(".")).toString(); - ProviderListContent.addItem(new ProviderItem(provider_name, provider_main_url)); - } - } - - private String extractProviderMainUrlFromAssetsFile(String filepath) { - String provider_main_url = ""; - try { - InputStream input_stream_file_contents = getAssets().open(filepath); - byte[] urls_file_bytes = new byte[input_stream_file_contents.available()]; - if(input_stream_file_contents.read(urls_file_bytes) > 0) { - String urls_file_content = new String(urls_file_bytes); - JSONObject file_contents = new JSONObject(urls_file_content); - provider_main_url = file_contents.getString(Provider.MAIN_URL); - } - } catch (JSONException e) { - } catch (IOException e) { - } - return provider_main_url; } private String getId(String provider_main_url) { -- cgit v1.2.3 From 202036aa6befcb1c74a76803f22e020404d4ba34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 4 Dec 2014 18:42:06 +0100 Subject: Click on an item, no UI but functional. --- .../se/leap/bitmaskclient/ConfigurationWizard.java | 52 +++++++++---------- .../main/java/se/leap/bitmaskclient/Provider.java | 4 ++ .../se/leap/bitmaskclient/ProviderRenderer.java | 2 + .../se/leap/bitmaskclient/ConfigurationWizard.java | 60 +++++++++++----------- 4 files changed, 63 insertions(+), 55 deletions(-) (limited to 'app') diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java index f178b754..53a7eb46 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -19,6 +19,7 @@ package se.leap.bitmaskclient; import android.app.*; import android.content.*; import android.os.*; +import android.util.Log; import android.view.*; import android.widget.*; @@ -31,6 +32,7 @@ import java.util.*; import butterknife.ButterKnife; import butterknife.InjectView; +import butterknife.OnItemClick; import org.jetbrains.annotations.NotNull; import org.json.*; @@ -52,7 +54,7 @@ import se.leap.bitmaskclient.eip.Constants; * */ public class ConfigurationWizard extends Activity -implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderDetailFragmentInterface, DownloadFailedDialogInterface, Receiver { +implements NewProviderDialogInterface, ProviderDetailFragmentInterface, DownloadFailedDialogInterface, Receiver { @InjectView(R.id.progressbar_configuration_wizard) ProgressBar mProgressBar; @InjectView(R.id.progressbar_description) TextView progressbar_description; @@ -63,7 +65,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD private ProviderManager provider_manager; private ProviderListFragment provider_list_fragment; private Intent mConfigState = new Intent(); - private ProviderItem selected_provider; + private Provider selected_provider; final public static String TAG = ConfigurationWizard.class.getSimpleName(); final public static String TYPE_OF_CERTIFICATE = "type_of_certificate"; @@ -101,7 +103,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD if(progressbar_description != null) outState.putString(PROGRESSBAR_TEXT, progressbar_description.getText().toString()); if(selected_provider != null) - outState.putString(Provider.NAME, selected_provider.name()); + outState.putParcelable(Provider.KEY, selected_provider); outState.putParcelable(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); super.onSaveInstanceState(outState); } @@ -127,7 +129,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD private void restoreState(Bundle savedInstanceState) { progressbar_text = savedInstanceState.getString(PROGRESSBAR_TEXT, ""); provider_name = savedInstanceState.getString(Provider.NAME, ""); - selected_provider = getProvider(provider_name); + selected_provider = savedInstanceState.getParcelable(Provider.KEY); progress = savedInstanceState.getInt(PROGRESSBAR_NUMBER, -1); providerAPI_result_receiver = savedInstanceState.getParcelable(ProviderAPI.RECEIVER_KEY); providerAPI_result_receiver.setReceiver(this); @@ -232,21 +234,17 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD // I need this for CW to wait for the About activity to end before going back to Dashboard. } } - - /** - * Callback method from {@link ProviderListFragment.Callbacks} - * indicating that the item with the given ID was selected. - */ - @Override - public void onItemSelected(String id) { - //TODO Code 2 pane view - selected_provider = getProvider(id); - onItemSelectedUi(selected_provider); + + @OnItemClick(R.id.provider_list) + void onItemSelected(int position) { + //TODO Code 2 pane view + selected_provider = adapter.getItem(position); + //onItemSelectedUi(selected_provider); boolean danger_on = true; if(preferences.contains(ProviderItem.DANGER_ON)) danger_on = preferences.getBoolean(ProviderItem.DANGER_ON, false); - setUpProvider(selected_provider.providerMainUrl(), danger_on); + setUpProvider(selected_provider.mainUrl(), danger_on); } private void onItemSelectedUi(ProviderItem provider) { @@ -436,29 +434,31 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD } } - public void showAndSelectProvider(String provider_main_url, boolean danger_on) { + public void showAndSelectProvider(String provider_main_url, boolean danger_on) { try { - provider_manager.add(new Provider(new URL((provider_main_url)))); + selected_provider = new Provider(new URL((provider_main_url))); + provider_manager.add(selected_provider); + autoSelectProvider(selected_provider, danger_on); } catch (MalformedURLException e) { e.printStackTrace(); } - autoSelectProvider(provider_main_url, danger_on); - } - - private void autoSelectProvider(String provider_main_url, boolean danger_on) { - preferences.edit().putBoolean(ProviderItem.DANGER_ON, danger_on).apply(); - onItemSelected(getId(provider_main_url)); - } + } + + private void autoSelectProvider(Provider provider, boolean danger_on) { + preferences.edit().putBoolean(ProviderItem.DANGER_ON, danger_on).apply(); + selected_provider = provider; + //onItemSelectedUi(selected_provider); + } /** * Asks ProviderAPI to download a new provider.json file n * @param provider_main_url * @param danger_on tells if HTTPS client should bypass certificate errors */ - public void setUpProvider(String provider_main_url, boolean danger_on) { + public void setUpProvider(URL provider_main_url, boolean danger_on) { Intent provider_API_command = new Intent(this, ProviderAPI.class); Bundle parameters = new Bundle(); - parameters.putString(Provider.MAIN_URL, provider_main_url); + parameters.putString(Provider.MAIN_URL, provider_main_url.toString()); parameters.putBoolean(ProviderItem.DANGER_ON, danger_on); provider_API_command.setAction(ProviderAPI.SET_UP_PROVIDER); diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index 5dd54175..0d584310 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -102,6 +102,10 @@ public final class Provider implements Parcelable { protected String getDomain(){ return main_url.getHost(); } + + protected URL mainUrl() { + return main_url; + } protected String getName(){ // Should we pass the locale in, or query the system here? diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java b/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java index 2e376dee..6e194e84 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java @@ -11,6 +11,8 @@ import com.pedrogomez.renderers.Renderer; import butterknife.ButterKnife; import butterknife.InjectView; +import butterknife.OnItemClick; +import butterknife.OnItemSelected; /** * Created by parmegv on 4/12/14. diff --git a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java index c47f68b9..beab12cc 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -31,6 +31,7 @@ import java.util.*; import butterknife.ButterKnife; import butterknife.InjectView; +import butterknife.OnItemClick; import org.jetbrains.annotations.NotNull; import org.json.*; @@ -53,7 +54,7 @@ import se.leap.bitmaskclient.eip.Constants; * */ public class ConfigurationWizard extends Activity -implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderDetailFragmentInterface, DownloadFailedDialogInterface, Receiver { +implements NewProviderDialogInterface, ProviderDetailFragmentInterface, DownloadFailedDialogInterface, Receiver { @InjectView(R.id.progressbar_configuration_wizard) ProgressBar mProgressBar; @@ -65,7 +66,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD private ProviderManager provider_manager; private ProviderListFragment provider_list_fragment; private Intent mConfigState = new Intent(); - private ProviderItem selected_provider; + private Provider selected_provider; final public static String TAG = ConfigurationWizard.class.getSimpleName(); final public static String TYPE_OF_CERTIFICATE = "type_of_certificate"; @@ -104,7 +105,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD if(progressbar_description != null) outState.putString(PROGRESSBAR_TEXT, progressbar_description.getText().toString()); if(selected_provider != null) - outState.putString(Provider.NAME, selected_provider.name()); + outState.putParcelable(Provider.KEY, selected_provider); outState.putParcelable(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); super.onSaveInstanceState(outState); } @@ -130,7 +131,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD private void restoreState(Bundle savedInstanceState) { progressbar_text = savedInstanceState.getString(PROGRESSBAR_TEXT, ""); provider_name = savedInstanceState.getString(Provider.NAME, ""); - selected_provider = getProvider(provider_name); + selected_provider = savedInstanceState.getParcelable(Provider.KEY); progress = savedInstanceState.getInt(PROGRESSBAR_NUMBER, -1); providerAPI_result_receiver = savedInstanceState.getParcelable(ProviderAPI.RECEIVER_KEY); providerAPI_result_receiver.setReceiver(this); @@ -165,16 +166,16 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD private void setUpProviderList() { initProviderList(); - provider_list_fragment = ProviderListFragment.newInstance(); + // provider_list_fragment = ProviderListFragment.newInstance(); - Bundle arguments = new Bundle(); - int configuration_wizard_request_code = getIntent().getIntExtra(Dashboard.REQUEST_CODE, -1); - if(configuration_wizard_request_code == Dashboard.SWITCH_PROVIDER) - arguments.putBoolean(ProviderListFragment.SHOW_ALL_PROVIDERS, true); + // Bundle arguments = new Bundle(); + // int configuration_wizard_request_code = getIntent().getIntExtra(Dashboard.REQUEST_CODE, -1); + // if(configuration_wizard_request_code == Dashboard.SWITCH_PROVIDER) + // arguments.putBoolean(ProviderListFragment.SHOW_ALL_PROVIDERS, true); - provider_list_fragment.setArguments(arguments); + // provider_list_fragment.setArguments(arguments); - putProviderListFragment(); + // putProviderListFragment(); } private void putProviderListFragment() { @@ -241,16 +242,12 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD } } - /** - * Callback method from {@link ProviderListFragment.Callbacks} - * indicating that the item with the given ID was selected. - */ - @Override - public void onItemSelected(String id) { + @OnItemClick(R.id.provider_list) + void onItemSelected(int position) { //TODO Code 2 pane view - selected_provider = getProvider(id); - onItemSelectedUi(selected_provider); - setUpProvider(selected_provider.providerMainUrl()); + selected_provider = adapter.getItem(position); + //onItemSelectedUi(selected_provider); + setUpProvider(selected_provider.mainUrl()); } private void onItemSelectedUi(ProviderItem provider) { @@ -447,11 +444,15 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD } } - public void showAndSelectProvider(String provider_main_url) { - if(getId(provider_main_url).isEmpty()) - showProvider(provider_main_url); - autoSelectProvider(provider_main_url); + public void showAndSelectProvider(String provider_main_url) { + try { + selected_provider = new Provider(new URL((provider_main_url))); + provider_manager.add(selected_provider); + autoSelectProvider(selected_provider); + } catch (MalformedURLException e) { + e.printStackTrace(); } + } private void showProvider(final String provider_main_url) { String provider_name = provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("\\/", "_"); @@ -459,19 +460,20 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD provider_list_fragment.addItem(added_provider); } - private void autoSelectProvider(String provider_main_url) { - onItemSelected(getId(provider_main_url)); - } + private void autoSelectProvider(Provider provider) { + selected_provider = provider; + //onItemSelected(provider); + } /** * Asks ProviderAPI to download a new provider.json file * @param provider_name * @param provider_main_url */ - public void setUpProvider(String provider_main_url) { + public void setUpProvider(URL provider_main_url) { Intent provider_API_command = new Intent(this, ProviderAPI.class); Bundle parameters = new Bundle(); - parameters.putString(Provider.MAIN_URL, provider_main_url); + parameters.putString(Provider.MAIN_URL, provider_main_url.toString()); provider_API_command.setAction(ProviderAPI.SET_UP_PROVIDER); provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters); -- cgit v1.2.3 From 84f71ca604abb2b4aeb410dcec2c3cc5dff9bc39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 9 Dec 2014 11:20:31 +0100 Subject: Switching provider doesn't refresh provider list. It shows just one provider, and it's not the one that gets configured if selected. --- .../se/leap/bitmaskclient/ConfigurationWizard.java | 100 ++++++--------------- .../main/java/se/leap/bitmaskclient/Dashboard.java | 32 +++---- .../main/java/se/leap/bitmaskclient/Provider.java | 16 +++- .../se/leap/bitmaskclient/ProviderListAdapter.java | 79 ++++++++++------ .../leap/bitmaskclient/ProviderListFragment.java | 26 +----- 5 files changed, 107 insertions(+), 146 deletions(-) (limited to 'app') diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java index 53a7eb46..dfca91ba 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -19,22 +19,18 @@ package se.leap.bitmaskclient; import android.app.*; import android.content.*; import android.os.*; -import android.util.Log; import android.view.*; import android.widget.*; -import com.pedrogomez.renderers.Renderer; -import com.pedrogomez.renderers.RendererAdapter; +import com.pedrogomez.renderers.*; -import java.io.*; import java.net.*; import java.util.*; -import butterknife.ButterKnife; -import butterknife.InjectView; -import butterknife.OnItemClick; +import butterknife.*; import org.jetbrains.annotations.NotNull; -import org.json.*; +import org.json.JSONException; +import org.json.JSONObject; import javax.inject.Inject; @@ -60,10 +56,9 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download @InjectView(R.id.progressbar_description) TextView progressbar_description; @InjectView(R.id.provider_list) ListView provider_list_view; - @Inject RendererAdapter adapter; + @Inject ProviderListAdapter adapter; private ProviderManager provider_manager; - private ProviderListFragment provider_list_fragment; private Intent mConfigState = new Intent(); private Provider selected_provider; @@ -92,7 +87,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download List> prototypes = new ArrayList>(); prototypes.add(new ProviderRenderer(this)); ProviderRendererBuilder providerRendererBuilder = new ProviderRendererBuilder(prototypes); - adapter = new RendererAdapter(getLayoutInflater(), providerRendererBuilder, provider_manager); + adapter = new ProviderListAdapter(getLayoutInflater(), providerRendererBuilder, provider_manager); provider_list_view.setAdapter(adapter); } @@ -140,7 +135,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download super.onPostResume(); if(!progressbar_text.isEmpty() && !provider_name.isEmpty() && progress != -1) { progressbar_description.setText(progressbar_text); - onItemSelectedUi(getProvider(provider_name)); + //onItemSelectedUi(getProvider(provider_name)); mProgressBar.setProgress(progress); progressbar_text = ""; @@ -196,6 +191,14 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download if(resultCode == ProviderAPI.PROVIDER_OK) { mConfigState.setAction(PROVIDER_SET); + try { + String provider_json_string = preferences.getString(Provider.KEY, ""); + if(!provider_json_string.isEmpty()) + selected_provider.define(new JSONObject(provider_json_string)); + } catch (JSONException e) { + e.printStackTrace(); + } + if (preferences.getBoolean(Constants.ALLOWED_ANON, false)){ mConfigState.putExtra(SERVICES_RETRIEVED, true); @@ -239,7 +242,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download void onItemSelected(int position) { //TODO Code 2 pane view selected_provider = adapter.getItem(position); - //onItemSelectedUi(selected_provider); + onItemSelectedUi(selected_provider); boolean danger_on = true; if(preferences.contains(ProviderItem.DANGER_ON)) @@ -247,10 +250,9 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download setUpProvider(selected_provider.mainUrl(), danger_on); } - private void onItemSelectedUi(ProviderItem provider) { + private void onItemSelectedUi(Provider provider) { startProgressBar(); - int provider_index = getProviderIndex(provider.name()); - provider_list_fragment.hideAllBut(provider_index); + adapter.hideAllBut(adapter.indexOf(provider)); } @Override @@ -288,15 +290,6 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download setResult(RESULT_CANCELED, ask_quit); } - private ProviderItem getProvider(String name) { - for (ProviderItem provider : ProviderListContent.ITEMS) { - if (provider.name().equalsIgnoreCase(name)) { - return provider; - } - } - return null; - } - private void startProgressBar() { mProgressBar.setVisibility(ProgressBar.VISIBLE); progressbar_description.setVisibility(TextView.VISIBLE); @@ -307,21 +300,9 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download mProgressBar.setTranslationY(measured_height); progressbar_description.setTranslationY(measured_height + mProgressBar.getHeight()); } - - private int getProviderIndex(String id) { - int index = 0; - for (ProviderItem provider : ProviderListContent.ITEMS) { - if (provider.name().equalsIgnoreCase(id)) { - break; - } else index++; - } - return index; - } private int listItemHeight() { - ListView provider_list_view = (ListView)findViewById(android.R.id.list); - ListAdapter provider_list_adapter = provider_list_view.getAdapter(); - View listItem = provider_list_adapter.getView(0, null, provider_list_view); + View listItem = adapter.getView(0, null, provider_list_view); listItem.setLayoutParams(new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); @@ -338,35 +319,6 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download return listItem.getMeasuredHeight(); } - private String getId(String provider_main_url) { - try { - URL provider_url = new URL(provider_main_url); - for (ProviderItem provider : ProviderListContent.ITEMS) { - URL aux_provider_url = new URL(provider.providerMainUrl()); - if (isSameURL(provider_url, aux_provider_url)) { - return provider.name(); - } - } - } catch (MalformedURLException e) { - e.printStackTrace(); - } - return ""; - } - - /** - * Checks, whether 2 urls are pointing to the same location. - * - * @param url a url - * @param baseUrl an other url, that should be compared. - * @return true, if the urls point to the same host and port and use the - * same protocol, false otherwise. - */ - private boolean isSameURL(final URL url, final URL baseUrl) { - return url.getProtocol().equals(baseUrl.getProtocol()) && - url.getHost().equals(baseUrl.getHost()) && - url.getPort() == baseUrl.getPort(); - } - /** * Asks ProviderAPI to download an anonymous (anon) VPN certificate. */ @@ -503,16 +455,11 @@ n * @param provider_main_url } public void showAllProviders() { - provider_list_fragment = (ProviderListFragment) fragment_manager.findFragmentByTag(ProviderListFragment.TAG); - if(provider_list_fragment != null) - provider_list_fragment.unhideAll(); + adapter.showAllProviders(); } public void cancelSettingUpProvider() { - provider_list_fragment = (ProviderListFragment) fragment_manager.findFragmentByTag(ProviderListFragment.TAG); - if(provider_list_fragment != null && preferences.contains(ProviderItem.DANGER_ON)) { - provider_list_fragment.removeLastItem(); - } + showAllProviders(); preferences.edit().remove(Provider.KEY).remove(ProviderItem.DANGER_ON).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).apply(); } @@ -520,6 +467,7 @@ n * @param provider_main_url public void login() { Intent ask_login = new Intent(); ask_login.putExtra(LogInDialog.TAG, LogInDialog.TAG); + ask_login.putExtra(Provider.KEY, selected_provider); setResult(RESULT_OK, ask_login); setting_up_provider = false; finish(); @@ -527,7 +475,9 @@ n * @param provider_main_url @Override public void use_anonymously() { - setResult(RESULT_OK); + Intent pass_provider = new Intent(); + pass_provider.putExtra(Provider.KEY, selected_provider); + setResult(RESULT_OK, pass_provider); setting_up_provider = false; finish(); } diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index e1d08d08..e277221b 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -87,8 +87,8 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); handleVersion(); - boolean no_provider_configured = preferences.getString(Constants.KEY, "").isEmpty(); - if (no_provider_configured) + + if (provider == null) startActivityForResult(new Intent(this,ConfigurationWizard.class),CONFIGURE_LEAP); else buildDashboard(getIntent().getBooleanExtra(ON_BOOT, false)); @@ -132,21 +132,21 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf Log.d(TAG, "onActivityResult: requestCode = " + requestCode); if ( requestCode == CONFIGURE_LEAP || requestCode == SWITCH_PROVIDER) { // It should be equivalent: if ( (requestCode == CONFIGURE_LEAP) || (data!= null && data.hasExtra(STOP_FIRST))) { - if ( resultCode == RESULT_OK ){ - preferences.edit().putInt(Constants.PARSED_SERIAL, 0).apply(); - preferences.edit().putBoolean(Constants.AUTHED_EIP, authed_eip).apply(); - updateEipService(); - buildDashboard(false); - invalidateOptionsMenu(); - if(data != null) - if(data.hasExtra(LogInDialog.TAG)) { - logInDialog(Bundle.EMPTY); - } - if(data.hasExtra(Provider.KEY)) + if ( resultCode == RESULT_OK ) { + preferences.edit().putInt(Constants.PARSED_SERIAL, 0).apply(); + preferences.edit().putBoolean(Constants.AUTHED_EIP, authed_eip).apply(); + updateEipService(); + + if (data.hasExtra(Provider.KEY)) provider = data.getParcelableExtra(Provider.KEY); - } else if(resultCode == RESULT_CANCELED && (data == null || data.hasExtra(ACTION_QUIT))) { - finish(); - } else + buildDashboard(false); + invalidateOptionsMenu(); + if (data.hasExtra(LogInDialog.TAG)) { + logInDialog(Bundle.EMPTY); + } else if (resultCode == RESULT_CANCELED && data.hasExtra(ACTION_QUIT)) { + finish(); + } + } else configErrorDialog(); } else if(requestCode == EIP.DISCONNECT) { EipStatus.getInstance().setConnectedOrDisconnected(); diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index 0d584310..dd679bb2 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -86,7 +86,9 @@ public final class Provider implements Parcelable { private Provider(Parcel in) { try { main_url = new URL(in.readString()); - definition = new JSONObject((in.readString())); + String definition_string = in.readString(); + if(definition_string != null) + definition = new JSONObject((definition_string)); } catch (MalformedURLException e) { e.printStackTrace(); } catch (JSONException e) { @@ -94,7 +96,6 @@ public final class Provider implements Parcelable { } } - protected void define(JSONObject provider_json) { definition = provider_json; } @@ -175,6 +176,15 @@ public final class Provider implements Parcelable { @Override public void writeToParcel(Parcel parcel, int i) { parcel.writeString(main_url.toString()); - parcel.writeString(definition.toString()); + if(definition != null) + parcel.writeString(definition.toString()); + } + + @Override + public boolean equals(Object o) { + if(o instanceof Provider) { + Provider p = (Provider) o; + return p.mainUrl().equals(mainUrl()); + } else return false; } } diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java index 1148e65e..92b2ae2e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java @@ -7,9 +7,15 @@ import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TwoLineListItem; +import com.pedrogomez.renderers.AdapteeCollection; +import com.pedrogomez.renderers.RendererAdapter; +import com.pedrogomez.renderers.RendererBuilder; + +import java.util.Collection; import java.util.List; +import java.util.Set; -public class ProviderListAdapter extends ArrayAdapter { +public class ProviderListAdapter extends RendererAdapter { private static boolean[] hidden = null; public void hide(int position) { @@ -23,10 +29,23 @@ public class ProviderListAdapter extends ArrayAdapter { notifyDataSetChanged(); notifyDataSetInvalidated(); } + + public void showAllProviders() { + for(int i = 0; i < hidden.length; i++) + hidden[i] = false; + notifyDataSetChanged(); + notifyDataSetInvalidated(); + } - public void unHideAll() { - for (int provider_index = 0; provider_index < hidden.length; provider_index++) - hidden[provider_index] = false; + public void hideAllBut(int position) { + for (int i = 0; i < hidden.length; i++) { + if (i != position) + hidden[i] = true; + else + hidden[i] = false; + } + notifyDataSetChanged(); + notifyDataSetInvalidated(); } private int getRealPosition(int position) { @@ -60,26 +79,28 @@ public class ProviderListAdapter extends ArrayAdapter { return (hidden.length - getHiddenCount()); } - public ProviderListAdapter(Context mContext, int layout, List objects) { - super(mContext, layout, objects); + public ProviderListAdapter(LayoutInflater layoutInflater, RendererBuilder rendererBuilder, + AdapteeCollection collection) { + super(layoutInflater, rendererBuilder, collection); if(hidden == null) { - hidden = new boolean[objects.size()]; - for (int i = 0; i < objects.size(); i++) + hidden = new boolean[collection.size()]; + for (int i = 0; i < collection.size(); i++) hidden[i] = false; } } - public ProviderListAdapter(Context mContext, int layout, List objects, boolean show_all_providers) { - super(mContext, layout, objects); + public ProviderListAdapter(LayoutInflater layoutInflater, RendererBuilder rendererBuilder, + AdapteeCollection collection, boolean show_all_providers) { + super(layoutInflater, rendererBuilder, collection); if(show_all_providers) { - hidden = new boolean[objects.size()]; - for (int i = 0; i < objects.size(); i++) + hidden = new boolean[collection.size()]; + for (int i = 0; i < collection.size(); i++) hidden[i] = false; } } @Override - public void add(T item) { + public void add(Provider item) { super.add(item); boolean[] new_hidden = new boolean[hidden.length+1]; System.arraycopy(hidden, 0, new_hidden, 0, hidden.length); @@ -88,27 +109,27 @@ public class ProviderListAdapter extends ArrayAdapter { } @Override - public void remove(T item) { + public void remove(Provider item) { super.remove(item); boolean[] new_hidden = new boolean[hidden.length-1]; System.arraycopy(hidden, 0, new_hidden, 0, hidden.length-1); hidden = new_hidden; } - @Override - public View getView(int index, View convertView, ViewGroup parent) { - TwoLineListItem row; - int position = getRealPosition(index); - if (convertView == null) { - LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); - row = (TwoLineListItem)inflater.inflate(R.layout.provider_list_item, null); - } else { - row = (TwoLineListItem)convertView; - } - ProviderListContent.ProviderItem data = ProviderListContent.ITEMS.get(position); - row.getText1().setText(data.domain()); - row.getText2().setText(data.name()); + protected int indexOf(Provider item) { + int index = 0; + ProviderManager provider_manager = (ProviderManager) getCollection(); + Set providers = provider_manager.providers(); + for (Provider provider : providers) { + if (provider.equals(item)) { + break; + } else index++; + } + return index; + } - return row; - } + @Override + public View getView(int position, View convertView, ViewGroup parent) { + return super.getView(getRealPosition(position), convertView, parent); + } } diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderListFragment.java b/app/src/main/java/se/leap/bitmaskclient/ProviderListFragment.java index 6d40bf9f..875b76d7 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderListFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderListFragment.java @@ -40,7 +40,7 @@ public class ProviderListFragment extends ListFragment { public static String TAG = "provider_list_fragment"; public static String SHOW_ALL_PROVIDERS = "show_all_providers"; public static String TOP_PADDING = "top padding from providerlistfragment"; - private ProviderListAdapter content_adapter; + private ProviderListAdapter content_adapter; /** * The serialization (saved instance state) Bundle key representing the @@ -91,19 +91,7 @@ public class ProviderListFragment extends ListFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if(getArguments().containsKey(SHOW_ALL_PROVIDERS)) - content_adapter = new ProviderListAdapter( - getActivity(), - R.layout.provider_list_item, - ProviderListContent.ITEMS, getArguments().getBoolean(SHOW_ALL_PROVIDERS)); - else - content_adapter = new ProviderListAdapter( - getActivity(), - R.layout.provider_list_item, - ProviderListContent.ITEMS); - - - setListAdapter(content_adapter); + } @Override @@ -198,12 +186,11 @@ public class ProviderListFragment extends ListFragment { } public void removeLastItem() { - unhideAll(); content_adapter.remove(content_adapter.getItem(content_adapter.getCount()-1)); content_adapter.notifyDataSetChanged(); } - public void addItem(ProviderItem provider) { + public void addItem(Provider provider) { content_adapter.add(provider); content_adapter.notifyDataSetChanged(); } @@ -219,13 +206,6 @@ public class ProviderListFragment extends ListFragment { i++; } } - - public void unhideAll() { - if(content_adapter != null) { - content_adapter.unHideAll(); - content_adapter.notifyDataSetChanged(); - } - } /** * @return a new instance of this ListFragment. -- cgit v1.2.3 From 7da04dc635505c0bcfdcc2c86d186a7a6f0535f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 9 Dec 2014 12:08:40 +0100 Subject: Release flavor updated, back button works. --- .../se/leap/bitmaskclient/ConfigurationWizard.java | 27 ++-- .../main/java/se/leap/bitmaskclient/Dashboard.java | 4 +- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 18 +-- .../se/leap/bitmaskclient/ConfigurationWizard.java | 148 ++++++--------------- 4 files changed, 63 insertions(+), 134 deletions(-) (limited to 'app') diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java index dfca91ba..20545473 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -258,9 +258,10 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download @Override public void onBackPressed() { if(setting_up_provider) { - stopSettingUpProvider(); + stopSettingUpProvider(); } else { - usualBackButton(); + askDashboardToQuitApp(); + super.onBackPressed(); } } @@ -270,18 +271,13 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download mProgressBar.setProgress(0); progressbar_description.setVisibility(TextView.GONE); - preferences.edit().remove(Provider.KEY).apply(); - setting_up_provider = false; - showAllProviders(); + cancelSettingUpProvider(); } - - private void usualBackButton() { - if(preferences.getString(Provider.KEY, "").isEmpty()) { - askDashboardToQuitApp(); - } else { - setResult(RESULT_OK); - } - super.onBackPressed(); + + public void cancelSettingUpProvider() { + showAllProviders(); + setting_up_provider = false; + preferences.edit().remove(Provider.KEY).remove(ProviderItem.DANGER_ON).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).commit(); } private void askDashboardToQuitApp() { @@ -457,11 +453,6 @@ n * @param provider_main_url public void showAllProviders() { adapter.showAllProviders(); } - - public void cancelSettingUpProvider() { - showAllProviders(); - preferences.edit().remove(Provider.KEY).remove(ProviderItem.DANGER_ON).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).apply(); - } @Override public void login() { diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index e277221b..e3bd9b7d 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -143,9 +143,9 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf invalidateOptionsMenu(); if (data.hasExtra(LogInDialog.TAG)) { logInDialog(Bundle.EMPTY); - } else if (resultCode == RESULT_CANCELED && data.hasExtra(ACTION_QUIT)) { - finish(); } + } else if (resultCode == RESULT_CANCELED && data.hasExtra(ACTION_QUIT)) { + finish(); } else configErrorDialog(); } else if(requestCode == EIP.DISCONNECT) { diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java index b4208556..4363dd13 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -47,7 +47,6 @@ import static se.leap.bitmaskclient.eip.Constants.ACTION_STOP_EIP; import static se.leap.bitmaskclient.eip.Constants.ACTION_UPDATE_EIP_SERVICE; import static se.leap.bitmaskclient.eip.Constants.CERTIFICATE; import static se.leap.bitmaskclient.eip.Constants.KEY; -import static se.leap.bitmaskclient.eip.Constants.PARSED_SERIAL; import static se.leap.bitmaskclient.eip.Constants.RECEIVER_TAG; import static se.leap.bitmaskclient.eip.Constants.REQUEST_TAG; @@ -209,18 +208,19 @@ public final class EIP extends IntentService { */ private void updateGateways(){ try { - JSONArray gatewaysDefined = eip_definition.getJSONArray("gateways"); - for ( int i=0 ; i < gatewaysDefined.length(); i++ ){ - JSONObject gw = gatewaysDefined.getJSONObject(i); - if(isOpenVpnGateway(gw)) { - addGateway(new Gateway(eip_definition, context, gw)); - } - } + if(eip_definition != null) { + JSONArray gatewaysDefined = eip_definition.getJSONArray("gateways"); + for (int i = 0; i < gatewaysDefined.length(); i++) { + JSONObject gw = gatewaysDefined.getJSONObject(i); + if (isOpenVpnGateway(gw)) { + addGateway(new Gateway(eip_definition, context, gw)); + } + } + } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } - preferences.edit().putInt(PARSED_SERIAL, eip_definition.optInt(Provider.API_RETURN_SERIAL)).apply(); } private boolean isOpenVpnGateway(JSONObject gateway) { diff --git a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java index beab12cc..d54b9a30 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -22,16 +22,13 @@ import android.os.*; import android.view.*; import android.widget.*; -import com.pedrogomez.renderers.Renderer; -import com.pedrogomez.renderers.RendererAdapter; +import com.pedrogomez.renderers.*; import java.io.*; import java.net.*; import java.util.*; -import butterknife.ButterKnife; -import butterknife.InjectView; -import butterknife.OnItemClick; +import butterknife.*; import org.jetbrains.annotations.NotNull; import org.json.*; @@ -61,7 +58,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download @InjectView(R.id.progressbar_description) TextView progressbar_description; @InjectView(R.id.provider_list) ListView provider_list_view; - @Inject RendererAdapter adapter; + @Inject ProviderListAdapter adapter; private ProviderManager provider_manager; private ProviderListFragment provider_list_fragment; @@ -94,7 +91,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download List> prototypes = new ArrayList>(); prototypes.add(new ProviderRenderer(this)); ProviderRendererBuilder providerRendererBuilder = new ProviderRendererBuilder(prototypes); - adapter = new RendererAdapter(getLayoutInflater(), providerRendererBuilder, provider_manager); + adapter = new ProviderListAdapter(getLayoutInflater(), providerRendererBuilder, provider_manager); provider_list_view.setAdapter(adapter); } @@ -142,7 +139,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download super.onPostResume(); if(!progressbar_text.isEmpty() && !provider_name.isEmpty() && progress != -1) { progressbar_description.setText(progressbar_text); - onItemSelectedUi(getProvider(provider_name)); + //onItemSelectedUi(getProvider(provider_name)); mProgressBar.setProgress(progress); progressbar_text = ""; @@ -202,6 +199,13 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download public void onReceiveResult(int resultCode, Bundle resultData) { if(resultCode == ProviderAPI.PROVIDER_OK) { mConfigState.setAction(PROVIDER_SET); + try { + String provider_json_string = preferences.getString(Provider.KEY, ""); + if(!provider_json_string.isEmpty()) + selected_provider.define(new JSONObject(provider_json_string)); + } catch (JSONException e) { + e.printStackTrace(); + } if (preferences.getBoolean(Constants.ALLOWED_ANON, false)){ mConfigState.putExtra(SERVICES_RETRIEVED, true); @@ -246,22 +250,22 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download void onItemSelected(int position) { //TODO Code 2 pane view selected_provider = adapter.getItem(position); - //onItemSelectedUi(selected_provider); + onItemSelectedUi(selected_provider); setUpProvider(selected_provider.mainUrl()); } - private void onItemSelectedUi(ProviderItem provider) { + private void onItemSelectedUi(Provider provider) { startProgressBar(); - int provider_index = getProviderIndex(provider.name()); - provider_list_fragment.hideAllBut(provider_index); + adapter.hideAllBut(adapter.indexOf(provider)); } @Override public void onBackPressed() { - if(setting_up_provider) { - stopSettingUpProvider(); + if(setting_up_provider) { + stopSettingUpProvider(); } else { - usualBackButton(); + askDashboardToQuitApp(); + super.onBackPressed(); } } @@ -277,35 +281,18 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download } private void usualBackButton() { - try { - boolean is_provider_set_up = new JSONObject(preferences.getString(Provider.KEY, "no provider")) != null ? true : false; - boolean is_provider_set_up_truly = new JSONObject(preferences.getString(Provider.KEY, "no provider")).length() != 0 ? true : false; - if(!is_provider_set_up || !is_provider_set_up_truly) { - askDashboardToQuitApp(); - } else { - setResult(RESULT_OK); - } - } catch (JSONException e) { - askDashboardToQuitApp(); - super.onBackPressed(); - e.printStackTrace(); - } - super.onBackPressed(); + if(preferences.getString(Provider.KEY, "").isEmpty()) { + askDashboardToQuitApp(); + } else { + setResult(RESULT_OK); + } + super.onBackPressed(); } private void askDashboardToQuitApp() { Intent ask_quit = new Intent(); ask_quit.putExtra(Dashboard.ACTION_QUIT, Dashboard.ACTION_QUIT); setResult(RESULT_CANCELED, ask_quit); } - - private ProviderItem getProvider(String name) { - for (ProviderItem provider : ProviderListContent.ITEMS) { - if(provider.name().equalsIgnoreCase(name)) { - return provider; - } - } - return null; - } private void startProgressBar() { mProgressBar.setVisibility(ProgressBar.VISIBLE); @@ -317,21 +304,9 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download mProgressBar.setTranslationY(measured_height); progressbar_description.setTranslationY(measured_height + mProgressBar.getHeight()); } - - private int getProviderIndex(String id) { - int index = 0; - for (ProviderItem provider : ProviderListContent.ITEMS) { - if(provider.name().equalsIgnoreCase(id)) { - break; - } else index++; - } - return index; - } private int listItemHeight() { - ListView provider_list_view = (ListView)findViewById(android.R.id.list); - ListAdapter provider_list_adapter = provider_list_view.getAdapter(); - View listItem = provider_list_adapter.getView(0, null, provider_list_view); + View listItem = adapter.getView(0, null, provider_list_view); listItem.setLayoutParams(new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); @@ -348,35 +323,6 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download return listItem.getMeasuredHeight(); } - private String getId(String provider_main_url) { - try { - URL provider_url = new URL(provider_main_url); - for (ProviderItem provider : ProviderListContent.ITEMS) { - URL aux_provider_url = new URL(provider.providerMainUrl()); - if(isSameURL(provider_url, aux_provider_url)) { - return provider.name(); - } - } - } catch (MalformedURLException e) { - e.printStackTrace(); - } - return ""; - } - - /** - * Checks, whether 2 urls are pointing to the same location. - * - * @param url a url - * @param baseUrl an other url, that should be compared. - * @return true, if the urls point to the same host and port and use the - * same protocol, false otherwise. - */ - private boolean isSameURL(final URL url, final URL baseUrl) { - return url.getProtocol().equals(baseUrl.getProtocol()) && - url.getHost().equals(baseUrl.getHost()) && - url.getPort() == baseUrl.getPort(); - } - /** * Asks ProviderAPI to download an anonymous (anon) VPN certificate. */ @@ -453,13 +399,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download e.printStackTrace(); } } - - private void showProvider(final String provider_main_url) { - String provider_name = provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("\\/", "_"); - ProviderItem added_provider = new ProviderItem(provider_name, provider_main_url); - provider_list_fragment.addItem(added_provider); - } - + private void autoSelectProvider(Provider provider) { selected_provider = provider; //onItemSelected(provider); @@ -516,19 +456,15 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download } } - public void showAllProviders() { - provider_list_fragment = (ProviderListFragment) fragment_manager.findFragmentByTag(ProviderListFragment.TAG); - if(provider_list_fragment != null) - provider_list_fragment.unhideAll(); - } + public void showAllProviders() { + adapter.showAllProviders(); + } - public void cancelSettingUpProvider() { - provider_list_fragment = (ProviderListFragment) fragment_manager.findFragmentByTag(ProviderListFragment.TAG); - if(provider_list_fragment != null) { - provider_list_fragment.removeLastItem(); - } - preferences.edit().remove(Provider.KEY).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).apply(); - } + public void cancelSettingUpProvider() { + adapter.showAllProviders(); + setting_up_provider = false; + preferences.edit().remove(Provider.KEY).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).apply(); + } @Override public void login() { @@ -539,12 +475,14 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download finish(); } - @Override - public void use_anonymously() { - setResult(RESULT_OK); - setting_up_provider = false; - finish(); - } + @Override + public void use_anonymously() { + Intent pass_provider = new Intent(); + pass_provider.putExtra(Provider.KEY, selected_provider); + setResult(RESULT_OK, pass_provider); + setting_up_provider = false; + finish(); + } public class ProviderAPIBroadcastReceiver_Update extends BroadcastReceiver { -- cgit v1.2.3 From afc9998ebefb19fcb2f9e3c46c6430fd69262e47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 9 Dec 2014 12:14:58 +0100 Subject: Show all providers onCreate CW. --- .../se/leap/bitmaskclient/ProviderListAdapter.java | 24 ++++++---------------- 1 file changed, 6 insertions(+), 18 deletions(-) (limited to 'app') diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java index 92b2ae2e..8360954b 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java @@ -79,25 +79,13 @@ public class ProviderListAdapter extends RendererAdapter { return (hidden.length - getHiddenCount()); } - public ProviderListAdapter(LayoutInflater layoutInflater, RendererBuilder rendererBuilder, + public ProviderListAdapter(LayoutInflater layoutInflater, RendererBuilder rendererBuilder, AdapteeCollection collection) { - super(layoutInflater, rendererBuilder, collection); - if(hidden == null) { - hidden = new boolean[collection.size()]; - for (int i = 0; i < collection.size(); i++) - hidden[i] = false; - } - } - - public ProviderListAdapter(LayoutInflater layoutInflater, RendererBuilder rendererBuilder, - AdapteeCollection collection, boolean show_all_providers) { - super(layoutInflater, rendererBuilder, collection); - if(show_all_providers) { - hidden = new boolean[collection.size()]; - for (int i = 0; i < collection.size(); i++) - hidden[i] = false; - } - } + super(layoutInflater, rendererBuilder, collection); + hidden = new boolean[collection.size()]; + for (int i = 0; i < collection.size(); i++) + hidden[i] = false; + } @Override public void add(Provider item) { -- cgit v1.2.3 From 5978db9bf758b7b229616d0575e327c58ad8cf94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 9 Dec 2014 14:08:17 +0100 Subject: EIP fragment shown correctly. --- .../se/leap/bitmaskclient/ConfigurationWizard.java | 16 +++++--- .../java/se/leap/bitmaskclient/ProviderAPI.java | 18 ++++----- .../main/java/se/leap/bitmaskclient/Dashboard.java | 47 ++++++++++++++-------- .../main/java/se/leap/bitmaskclient/Provider.java | 10 +++-- .../java/se/leap/bitmaskclient/eip/Constants.java | 1 + .../se/leap/bitmaskclient/ConfigurationWizard.java | 10 ++++- .../java/se/leap/bitmaskclient/ProviderAPI.java | 29 +++++++------ 7 files changed, 81 insertions(+), 50 deletions(-) (limited to 'app') diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java index 20545473..9af31fd8 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -242,12 +242,15 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download void onItemSelected(int position) { //TODO Code 2 pane view selected_provider = adapter.getItem(position); + onItemSelectedLogic(selected_provider); onItemSelectedUi(selected_provider); + } - boolean danger_on = true; - if(preferences.contains(ProviderItem.DANGER_ON)) - danger_on = preferences.getBoolean(ProviderItem.DANGER_ON, false); - setUpProvider(selected_provider.mainUrl(), danger_on); + private void onItemSelectedLogic(Provider selected_provider) { + boolean danger_on = true; + if(preferences.contains(ProviderItem.DANGER_ON)) + danger_on = preferences.getBoolean(ProviderItem.DANGER_ON, false); + setUpProvider(selected_provider.mainUrl(), danger_on); } private void onItemSelectedUi(Provider provider) { @@ -255,6 +258,8 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download adapter.hideAllBut(adapter.indexOf(provider)); } + + @Override public void onBackPressed() { if(setting_up_provider) { @@ -395,7 +400,8 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download private void autoSelectProvider(Provider provider, boolean danger_on) { preferences.edit().putBoolean(ProviderItem.DANGER_ON, danger_on).apply(); selected_provider = provider; - //onItemSelectedUi(selected_provider); + onItemSelectedLogic(selected_provider); + onItemSelectedUi(selected_provider); } /** diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java index a89ef357..52a424f4 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java @@ -32,7 +32,6 @@ import org.apache.http.client.ClientProtocolException; import org.json.*; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; -import se.leap.bitmaskclient.R; import se.leap.bitmaskclient.eip.*; /** @@ -59,11 +58,10 @@ public class ProviderAPI extends IntentService { ERRORS = "errors", UPDATE_PROGRESSBAR = "update_progressbar", CURRENT_PROGRESS = "current_progress", - TAG = ProviderAPI.class.getSimpleName(); + TAG = ProviderAPI.class.getSimpleName() ; final public static int - CUSTOM_PROVIDER_ADDED = 0, SRP_AUTHENTICATION_SUCCESSFUL = 3, SRP_AUTHENTICATION_FAILED = 4, SRP_REGISTRATION_SUCCESSFUL = 5, @@ -73,9 +71,7 @@ public class ProviderAPI extends IntentService { CORRECTLY_DOWNLOADED_CERTIFICATE = 9, INCORRECTLY_DOWNLOADED_CERTIFICATE = 10, PROVIDER_OK = 11, - PROVIDER_NOK = 12, - CORRECTLY_DOWNLOADED_ANON_CERTIFICATE = 13, - INCORRECTLY_DOWNLOADED_ANON_CERTIFICATE = 14 + PROVIDER_NOK = 12 ; private static boolean @@ -128,11 +124,11 @@ public class ProviderAPI extends IntentService { try { JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "no provider")); provider_api_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION); + setting_up_provider = true; } catch (JSONException e) { + setting_up_provider = false; } } - - setting_up_provider = true; if(action.equalsIgnoreCase(SET_UP_PROVIDER)) { Bundle result = setUpProvider(parameters); @@ -370,7 +366,7 @@ public class ProviderAPI extends IntentService { * Sends an HTTP POST request to the api server to register a new user. * @param server_url * @param username - * @param salted_password + * @param salt * @param password_verifier * @return response from authentication server */ @@ -491,6 +487,7 @@ public class ProviderAPI extends IntentService { last_danger_on = task.getBoolean(ProviderItem.DANGER_ON); last_provider_main_url = task.getString(Provider.MAIN_URL); CA_CERT_DOWNLOADED = PROVIDER_JSON_DOWNLOADED = EIP_SERVICE_JSON_DOWNLOADED = false; + setting_up_provider = true; } if(!PROVIDER_JSON_DOWNLOADED) @@ -694,7 +691,7 @@ public class ProviderAPI extends IntentService { /** * Tries to download the contents of the provided url using not commercially validated CA certificate from chosen provider. - * @param url as a string + * @param url_string as a string * @param danger_on true to download CA certificate in case it has not been downloaded. * @return an empty string if it fails, the url content if not. */ @@ -818,7 +815,6 @@ public class ProviderAPI extends IntentService { /** * Logs out from the api url retrieved from the task. - * @param task containing api url from which the user will log out * @return true if there were no exceptions */ private boolean logOut() { diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index e3bd9b7d..b84b834c 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -37,6 +37,9 @@ import android.widget.TextView; import org.json.JSONException; import org.json.JSONObject; +import java.net.MalformedURLException; +import java.net.URL; + import de.blinkt.openvpn.activities.LogWindow; import se.leap.bitmaskclient.eip.Constants; import se.leap.bitmaskclient.eip.EIP; @@ -74,6 +77,13 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf private Provider provider; private static boolean authed_eip; + @Override + protected void onSaveInstanceState(Bundle outState) { + if(provider != null) + outState.putParcelable(Provider.KEY, provider); + super.onSaveInstanceState(outState); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -88,7 +98,19 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); handleVersion(); - if (provider == null) + if(savedInstanceState != null) + provider = savedInstanceState.getParcelable(Provider.KEY); + if(provider == null && preferences.getBoolean(Constants.PROVIDER_CONFIGURED, false)) + try { + provider = new Provider(new URL(preferences.getString(Provider.MAIN_URL, ""))); + provider.define(new JSONObject(preferences.getString(Provider.KEY, ""))); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + + if (provider == null || provider.getName().isEmpty()) startActivityForResult(new Intent(this,ConfigurationWizard.class),CONFIGURE_LEAP); else buildDashboard(getIntent().getBooleanExtra(ON_BOOT, false)); @@ -116,16 +138,6 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf Log.d(TAG, "Handle version didn't find any " + getPackageName() + " package"); } } - - @Override - protected void onDestroy() { - - super.onDestroy(); - } - - protected void onPause() { - super.onPause(); - } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ @@ -137,8 +149,12 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf preferences.edit().putBoolean(Constants.AUTHED_EIP, authed_eip).apply(); updateEipService(); - if (data.hasExtra(Provider.KEY)) + if (data.hasExtra(Provider.KEY)) { provider = data.getParcelableExtra(Provider.KEY); + preferences.edit().putBoolean(Constants.PROVIDER_CONFIGURED, true).commit(); + preferences.edit().putString(Provider.MAIN_URL, provider.mainUrl().toString()).apply(); + preferences.edit().putString(Provider.KEY, provider.definition().toString()).apply(); + } buildDashboard(false); invalidateOptionsMenu(); if (data.hasExtra(LogInDialog.TAG)) { @@ -172,7 +188,7 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf .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).apply(); + preferences.edit().remove(Provider.KEY).remove(Constants.PROVIDER_CONFIGURED).apply(); finish(); } }) @@ -194,9 +210,8 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf if ( provider.hasEIP()){ - EipServiceFragment eip_fragment = (EipServiceFragment) fragment_manager.findFragmentByTag(EipServiceFragment.TAG); - if(eip_fragment == null) - eip_fragment = new EipServiceFragment(); + fragment_manager.removePreviousFragment(EipServiceFragment.TAG); + EipServiceFragment eip_fragment = new EipServiceFragment(); if (hide_and_turn_on_eip) { preferences.edit().remove(Dashboard.START_ON_BOOT).apply(); diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index dd679bb2..0b021b84 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -100,6 +100,8 @@ public final class Provider implements Parcelable { definition = provider_json; } + protected JSONObject definition() { return definition; } + protected String getDomain(){ return main_url.getHost(); } @@ -111,14 +113,16 @@ public final class Provider implements Parcelable { protected String getName(){ // Should we pass the locale in, or query the system here? String lang = Locale.getDefault().getLanguage(); - String name = "Null"; // Should it actually /be/ null, for error conditions? + String name = ""; try { if(definition != null) name = definition.getJSONObject(API_TERM_NAME).getString(lang); else throw new JSONException("Provider not defined"); } catch (JSONException e) { - String host = main_url.getHost(); - name = host.substring(0, host.indexOf(".")); + if(main_url != null) { + String host = main_url.getHost(); + name = host.substring(0, host.indexOf(".")); + } } return name; diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java b/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java index e1a7e616..01a83d5f 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java @@ -43,5 +43,6 @@ public interface Constants { public final static String RECEIVER_TAG = TAG + ".RECEIVER_TAG"; public final static String REQUEST_TAG = TAG + ".REQUEST_TAG"; public final static String START_BLOCKING_VPN_PROFILE = TAG + ".START_BLOCKING_VPN_PROFILE"; + public final static String PROVIDER_CONFIGURED = TAG + ".PROVIDER_CONFIGURED"; } diff --git a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java index d54b9a30..ef7986f1 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -251,7 +251,11 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download //TODO Code 2 pane view selected_provider = adapter.getItem(position); onItemSelectedUi(selected_provider); - setUpProvider(selected_provider.mainUrl()); + onItemSelectedLogic(selected_provider); + } + + private void onItemSelectedLogic(Provider selected_provider) { + setUpProvider(selected_provider.mainUrl()); } private void onItemSelectedUi(Provider provider) { @@ -402,7 +406,8 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download private void autoSelectProvider(Provider provider) { selected_provider = provider; - //onItemSelected(provider); + onItemSelectedUi(selected_provider); + onItemSelectedLogic(selected_provider); } /** @@ -469,6 +474,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download @Override public void login() { Intent ask_login = new Intent(); + ask_login.putExtra(Provider.KEY, selected_provider); ask_login.putExtra(LogInDialog.TAG, LogInDialog.TAG); setResult(RESULT_OK, ask_login); setting_up_provider = false; diff --git a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java index 0fa60550..fc74b702 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java @@ -58,7 +58,7 @@ public class ProviderAPI extends IntentService { ERRORS = "errors", UPDATE_PROGRESSBAR = "update_progressbar", CURRENT_PROGRESS = "current_progress", - TAG = ProviderAPI.class.getSimpleName(); + TAG = ProviderAPI.class.getSimpleName() ; final public static int @@ -72,9 +72,7 @@ public class ProviderAPI extends IntentService { CORRECTLY_DOWNLOADED_CERTIFICATE = 9, INCORRECTLY_DOWNLOADED_CERTIFICATE = 10, PROVIDER_OK = 11, - PROVIDER_NOK = 12, - CORRECTLY_DOWNLOADED_ANON_CERTIFICATE = 13, - INCORRECTLY_DOWNLOADED_ANON_CERTIFICATE = 14 + PROVIDER_NOK = 12 ; private static boolean @@ -100,14 +98,9 @@ public class ProviderAPI extends IntentService { @Override public void onCreate() { super.onCreate(); + preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER)); - if(provider_api_url == null && preferences.contains(Provider.KEY)) { - try { - JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); - provider_api_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION); - } catch (JSONException e) {} - } } public static String lastProviderMainUrl() { @@ -123,7 +116,16 @@ public class ProviderAPI extends IntentService { final ResultReceiver receiver = command.getParcelableExtra(RECEIVER_KEY); String action = command.getAction(); Bundle parameters = command.getBundleExtra(PARAMETERS); - setting_up_provider = true; + + if(provider_api_url == null && preferences.contains(Provider.KEY)) { + try { + JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); + provider_api_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION); + setting_up_provider = true; + } catch (JSONException e) { + setting_up_provider = false; + } + } if(action.equalsIgnoreCase(SET_UP_PROVIDER)) { Bundle result = setUpProvider(parameters); @@ -360,7 +362,7 @@ public class ProviderAPI extends IntentService { * Sends an HTTP POST request to the api server to register a new user. * @param server_url * @param username - * @param salted_password + * @param salt * @param password_verifier * @return response from authentication server */ @@ -477,6 +479,7 @@ public class ProviderAPI extends IntentService { if(task != null && task.containsKey(Provider.MAIN_URL)) { last_provider_main_url = task.getString(Provider.MAIN_URL); CA_CERT_DOWNLOADED = PROVIDER_JSON_DOWNLOADED = EIP_SERVICE_JSON_DOWNLOADED = false; + setting_up_provider = true; } if(!PROVIDER_JSON_DOWNLOADED) @@ -684,7 +687,7 @@ public class ProviderAPI extends IntentService { /** * Tries to download the contents of the provided url using not commercially validated CA certificate from chosen provider. - * @param url as a string + * @param url_string as a string * @return an empty string if it fails, the url content if not. */ private String downloadWithProviderCA(String url_string) { -- cgit v1.2.3 From 7bbb5b69717393d87869ae6f16d6aff0578a81bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 9 Dec 2014 15:58:09 +0100 Subject: Save custom providers to sdcard. --- .../se/leap/bitmaskclient/ConfigurationWizard.java | 14 +--- .../main/java/se/leap/bitmaskclient/Provider.java | 19 +++-- .../se/leap/bitmaskclient/ProviderListAdapter.java | 5 ++ .../se/leap/bitmaskclient/ProviderManager.java | 98 +++++++++++++++++----- .../se/leap/bitmaskclient/ConfigurationWizard.java | 20 +---- 5 files changed, 104 insertions(+), 52 deletions(-) (limited to 'app') diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java index 9af31fd8..2e3fb8f1 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -108,7 +108,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download super.onCreate(savedInstanceState); preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); - provider_manager = new ProviderManager(getAssets()); + provider_manager = ProviderManager.getInstance(getAssets(), getExternalFilesDir(null)); setUpInitialUI(); @@ -158,15 +158,6 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download private void setUpProviderList() { initProviderList(); - // provider_list_fragment = ProviderListFragment.newInstance(); - - // Bundle arguments = new Bundle(); - // int configuration_wizard_request_code = getIntent().getIntExtra(Dashboard.REQUEST_CODE, -1); - // if(configuration_wizard_request_code == Dashboard.SWITCH_PROVIDER) - // arguments.putBoolean(ProviderListFragment.SHOW_ALL_PROVIDERS, true); - - // provider_list_fragment.setArguments(arguments); - } @Override @@ -390,7 +381,8 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download public void showAndSelectProvider(String provider_main_url, boolean danger_on) { try { selected_provider = new Provider(new URL((provider_main_url))); - provider_manager.add(selected_provider); + adapter.add(selected_provider); + adapter.saveProviders(); autoSelectProvider(selected_provider, danger_on); } catch (MalformedURLException e) { e.printStackTrace(); diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index 0b021b84..a7ede2d1 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -26,6 +26,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import java.io.File; import java.io.Serializable; import java.net.MalformedURLException; import java.net.URL; @@ -40,7 +41,6 @@ public final class Provider implements Parcelable { private JSONObject definition; // Represents our Provider's provider.json private URL main_url; - private boolean is_custom = false; final public static String API_URL = "api_uri", @@ -72,6 +72,9 @@ public final class Provider implements Parcelable { this.main_url = main_url; } + public Provider(File provider_file) { + + } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { public Provider createFromParcel(Parcel in) { @@ -168,10 +171,6 @@ public final class Provider implements Parcelable { return false; } - protected boolean isCustom() { - return is_custom; - } - @Override public int describeContents() { return 0; @@ -191,4 +190,14 @@ public final class Provider implements Parcelable { return p.mainUrl().equals(mainUrl()); } else return false; } + + public JSONObject toJson() { + JSONObject json = new JSONObject(); + try { + json.put(Provider.MAIN_URL, main_url); + } catch (JSONException e) { + e.printStackTrace(); + } + return json; + } } diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java index 8360954b..72ca15c7 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java @@ -120,4 +120,9 @@ public class ProviderListAdapter extends RendererAdapter { public View getView(int position, View convertView, ViewGroup parent) { return super.getView(getRealPosition(position), convertView, parent); } + + public void saveProviders() { + ProviderManager provider_manager = (ProviderManager) getCollection(); + provider_manager.saveCustomProvidersToFile(); + } } diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java index 59507d9c..911144f7 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java @@ -7,6 +7,11 @@ import com.pedrogomez.renderers.AdapteeCollection; import org.json.JSONException; import org.json.JSONObject; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; @@ -22,51 +27,89 @@ import java.util.Set; public class ProviderManager implements AdapteeCollection { private AssetManager assets_manager; + private File external_files_dir; private Set default_providers; private Set custom_providers; + private static ProviderManager instance; + final protected static String URLS = "urls"; + public static ProviderManager getInstance(AssetManager assets_manager, File external_files_dir) { + if(instance == null) + instance = new ProviderManager(assets_manager); + + instance.addCustomProviders(external_files_dir); + return instance; + } + public ProviderManager(AssetManager assets_manager) { this.assets_manager = assets_manager; - default_providers = default_providers(); - custom_providers = new HashSet(); + addDefaultProviders(assets_manager); } - /** - * Loads providers data from url files contained in the assets folder - * @return true if the files were correctly read - */ - private Set default_providers() { + private void addDefaultProviders(AssetManager assets_manager) { + try { + default_providers = providersFromAssets(URLS, assets_manager.list(URLS)); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private Set providersFromAssets(String directory, String[] relative_file_paths) { Set providers = new HashSet(); try { - for(String file : assets_manager.list(URLS)) { - String main_url = extractProviderMainUrlFromAssetsFile(URLS + "/" + file); + for(String file : relative_file_paths) { + String main_url = extractMainUrlFromInputStream(assets_manager.open(directory + "/" + file)); providers.add(new Provider(new URL(main_url))); - } + } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } + return providers; + } + + + private void addCustomProviders(File external_files_dir) { + this.external_files_dir = external_files_dir; + custom_providers = external_files_dir.isDirectory() ? + providersFromFiles(external_files_dir.list()) : + new HashSet(); + } + + private Set providersFromFiles(String[] files) { + Set providers = new HashSet(); + try { + for(String file : files) { + String main_url = extractMainUrlFromInputStream(new FileInputStream(external_files_dir.getAbsolutePath() + "/" + file)); + providers.add(new Provider(new URL(main_url))); + } + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } return providers; } - private String extractProviderMainUrlFromAssetsFile(String file_path) { - String provider_main_url = ""; + private String extractMainUrlFromInputStream(InputStream input_stream_file_contents) { + String main_url = ""; + byte[] bytes = new byte[0]; try { - InputStream input_stream_file_contents = assets_manager.open(file_path); - byte[] urls_file_bytes = new byte[input_stream_file_contents.available()]; - if(input_stream_file_contents.read(urls_file_bytes) > 0) { - String urls_file_content = new String(urls_file_bytes); - JSONObject file_contents = new JSONObject(urls_file_content); - provider_main_url = file_contents.getString(Provider.MAIN_URL); + bytes = new byte[input_stream_file_contents.available()]; + if(input_stream_file_contents.read(bytes) > 0) { + JSONObject file_contents = new JSONObject(new String(bytes)); + main_url = file_contents.getString(Provider.MAIN_URL); } - } catch (JSONException e) { } catch (IOException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); } - return provider_main_url; + return main_url; } public Set providers() { @@ -117,4 +160,19 @@ public class ProviderManager implements AdapteeCollection { default_providers.clear(); custom_providers.clear(); } + + protected void saveCustomProvidersToFile() { + try { + for (Provider provider : custom_providers) { + File provider_file = new File(external_files_dir, provider.getName() + ".json"); + if(!provider_file.exists()) { + FileWriter writer = new FileWriter(provider_file); + writer.write(provider.toJson().toString()); + writer.close(); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java index ef7986f1..42736444 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -24,12 +24,11 @@ import android.widget.*; import com.pedrogomez.renderers.*; -import java.io.*; import java.net.*; import java.util.*; import butterknife.*; -import org.jetbrains.annotations.NotNull; + import org.json.*; import javax.inject.Inject; @@ -38,8 +37,6 @@ import se.leap.bitmaskclient.DownloadFailedDialog.DownloadFailedDialogInterface; import se.leap.bitmaskclient.NewProviderDialog.NewProviderDialogInterface; import se.leap.bitmaskclient.ProviderAPIResultReceiver.Receiver; import se.leap.bitmaskclient.ProviderDetailFragment.ProviderDetailFragmentInterface; -import se.leap.bitmaskclient.ProviderListContent.ProviderItem; -import se.leap.bitmaskclient.FragmentManagerEnhanced; import se.leap.bitmaskclient.eip.Constants; /** @@ -112,7 +109,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download super.onCreate(savedInstanceState); preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); - provider_manager = new ProviderManager(getAssets()); + provider_manager = ProviderManager.getInstance(getAssets(), getExternalFilesDir(null)); setUpInitialUI(); @@ -163,16 +160,6 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download private void setUpProviderList() { initProviderList(); - // provider_list_fragment = ProviderListFragment.newInstance(); - - // Bundle arguments = new Bundle(); - // int configuration_wizard_request_code = getIntent().getIntExtra(Dashboard.REQUEST_CODE, -1); - // if(configuration_wizard_request_code == Dashboard.SWITCH_PROVIDER) - // arguments.putBoolean(ProviderListFragment.SHOW_ALL_PROVIDERS, true); - - // provider_list_fragment.setArguments(arguments); - - // putProviderListFragment(); } private void putProviderListFragment() { @@ -397,7 +384,8 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download public void showAndSelectProvider(String provider_main_url) { try { selected_provider = new Provider(new URL((provider_main_url))); - provider_manager.add(selected_provider); + adapter.add(selected_provider); + adapter.saveProviders(); autoSelectProvider(selected_provider); } catch (MalformedURLException e) { e.printStackTrace(); -- cgit v1.2.3 From e980e1f888f29806ab0933495fdb0dd383c944e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 9 Dec 2014 16:22:28 +0100 Subject: No duplicated providers. --- app/src/main/java/se/leap/bitmaskclient/Provider.java | 5 +++++ .../main/java/se/leap/bitmaskclient/ProviderListAdapter.java | 10 ++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) (limited to 'app') diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index a7ede2d1..f22a4bfb 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -200,4 +200,9 @@ public final class Provider implements Parcelable { } return json; } + + @Override + public int hashCode() { + return main_url.hashCode(); + } } diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java index 72ca15c7..19a9a089 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java @@ -90,10 +90,12 @@ public class ProviderListAdapter extends RendererAdapter { @Override public void add(Provider item) { super.add(item); - boolean[] new_hidden = new boolean[hidden.length+1]; - System.arraycopy(hidden, 0, new_hidden, 0, hidden.length); - new_hidden[hidden.length] = false; - hidden = new_hidden; + if(getCollection().size() < hidden.length) { + boolean[] new_hidden = new boolean[hidden.length + 1]; + System.arraycopy(hidden, 0, new_hidden, 0, hidden.length); + new_hidden[hidden.length] = false; + hidden = new_hidden; + } } @Override -- cgit v1.2.3 From 062e4c4597f947816282b1c9c5e63c034549f057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 9 Dec 2014 18:57:35 +0100 Subject: Final refactoring for this bug. Removed the sign up dialog, log in one is enough with the third button I added some time ago to sign up. Removed classes we don't need due to the libraries I have recently introduced. --- .../se/leap/bitmaskclient/ConfigurationWizard.java | 2 +- .../java/se/leap/bitmaskclient/ProviderAPI.java | 28 +-- .../main/java/se/leap/bitmaskclient/Dashboard.java | 148 ++++++-------- .../se/leap/bitmaskclient/EipServiceFragment.java | 107 +++++----- .../java/se/leap/bitmaskclient/LogInDialog.java | 144 -------------- .../leap/bitmaskclient/ProviderListFragment.java | 216 --------------------- .../java/se/leap/bitmaskclient/SessionDialog.java | 159 +++++++++++++++ .../leap/bitmaskclient/SessionDialogInterface.java | 37 ---- .../java/se/leap/bitmaskclient/SignUpDialog.java | 132 ------------- .../main/res/layout-xlarge/client_dashboard.xml | 69 ------- app/src/main/res/layout-xlarge/dashboard.xml | 69 +++++++ .../res/layout-xlarge/eip_service_fragment.xml | 35 +--- app/src/main/res/layout-xlarge/log_in_dialog.xml | 39 ---- .../res/layout-xlarge/provider_list_fragment.xml | 16 -- app/src/main/res/layout-xlarge/session_dialog.xml | 39 ++++ app/src/main/res/layout/client_dashboard.xml | 51 ----- app/src/main/res/layout/dashboard.xml | 52 +++++ app/src/main/res/layout/eip_service_fragment.xml | 40 +--- app/src/main/res/layout/log_in_dialog.xml | 41 ---- app/src/main/res/layout/provider_list_fragment.xml | 15 -- app/src/main/res/layout/session_dialog.xml | 42 ++++ .../se/leap/bitmaskclient/ConfigurationWizard.java | 7 +- .../java/se/leap/bitmaskclient/ProviderAPI.java | 29 +-- 23 files changed, 521 insertions(+), 996 deletions(-) delete mode 100644 app/src/main/java/se/leap/bitmaskclient/LogInDialog.java delete mode 100644 app/src/main/java/se/leap/bitmaskclient/ProviderListFragment.java create mode 100644 app/src/main/java/se/leap/bitmaskclient/SessionDialog.java delete mode 100644 app/src/main/java/se/leap/bitmaskclient/SessionDialogInterface.java delete mode 100644 app/src/main/java/se/leap/bitmaskclient/SignUpDialog.java delete mode 100644 app/src/main/res/layout-xlarge/client_dashboard.xml create mode 100644 app/src/main/res/layout-xlarge/dashboard.xml delete mode 100644 app/src/main/res/layout-xlarge/log_in_dialog.xml delete mode 100644 app/src/main/res/layout-xlarge/provider_list_fragment.xml create mode 100644 app/src/main/res/layout-xlarge/session_dialog.xml delete mode 100644 app/src/main/res/layout/client_dashboard.xml create mode 100644 app/src/main/res/layout/dashboard.xml delete mode 100644 app/src/main/res/layout/log_in_dialog.xml delete mode 100644 app/src/main/res/layout/provider_list_fragment.xml create mode 100644 app/src/main/res/layout/session_dialog.xml (limited to 'app') diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java index 2e3fb8f1..63185d12 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -455,7 +455,7 @@ n * @param provider_main_url @Override public void login() { Intent ask_login = new Intent(); - ask_login.putExtra(LogInDialog.TAG, LogInDialog.TAG); + ask_login.putExtra(SessionDialog.TAG, SessionDialog.TAG); ask_login.putExtra(Provider.KEY, selected_provider); setResult(RESULT_OK, ask_login); setting_up_provider = false; diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java index 52a424f4..9ac5ff66 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java @@ -172,8 +172,8 @@ public class ProviderAPI extends IntentService { Bundle session_id_bundle = new Bundle(); int progress = 0; - String username = (String) task.get(SessionDialogInterface.USERNAME); - String password = (String) task.get(SessionDialogInterface.PASSWORD); + String username = (String) task.get(SessionDialog.USERNAME); + String password = (String) task.get(SessionDialog.PASSWORD); if(validUserLoginData(username, password)) { session_id_bundle = register(username, password); @@ -181,12 +181,12 @@ public class ProviderAPI extends IntentService { } else { if(!wellFormedPassword(password)) { session_id_bundle.putBoolean(RESULT_KEY, false); - session_id_bundle.putString(SessionDialogInterface.USERNAME, username); - session_id_bundle.putBoolean(SessionDialogInterface.PASSWORD_INVALID_LENGTH, true); + session_id_bundle.putString(SessionDialog.USERNAME, username); + session_id_bundle.putBoolean(SessionDialog.PASSWORD_INVALID_LENGTH, true); } if(username.isEmpty()) { session_id_bundle.putBoolean(RESULT_KEY, false); - session_id_bundle.putBoolean(SessionDialogInterface.USERNAME_MISSING, true); + session_id_bundle.putBoolean(SessionDialog.USERNAME_MISSING, true); } } @@ -205,8 +205,8 @@ public class ProviderAPI extends IntentService { if(api_result.has(ERRORS)) result = authFailedNotification(api_result, username); else { - result.putString(SessionDialogInterface.USERNAME, username); - result.putString(SessionDialogInterface.PASSWORD, password); + result.putString(SessionDialog.USERNAME, username); + result.putString(SessionDialog.PASSWORD, password); result.putBoolean(RESULT_KEY, true); } @@ -222,20 +222,20 @@ public class ProviderAPI extends IntentService { Bundle result = new Bundle(); int progress = 0; - String username = (String) task.get(SessionDialogInterface.USERNAME); - String password = (String) task.get(SessionDialogInterface.PASSWORD); + String username = (String) task.get(SessionDialog.USERNAME); + String password = (String) task.get(SessionDialog.PASSWORD); if(validUserLoginData(username, password)) { result = authenticate(username, password); broadcast_progress(progress++); } else { if(!wellFormedPassword(password)) { result.putBoolean(RESULT_KEY, false); - result.putString(SessionDialogInterface.USERNAME, username); - result.putBoolean(SessionDialogInterface.PASSWORD_INVALID_LENGTH, true); + result.putString(SessionDialog.USERNAME, username); + result.putBoolean(SessionDialog.PASSWORD_INVALID_LENGTH, true); } if(username.isEmpty()) { result.putBoolean(RESULT_KEY, false); - result.putBoolean(SessionDialogInterface.USERNAME_MISSING, true); + result.putBoolean(SessionDialog.USERNAME_MISSING, true); } } @@ -265,7 +265,7 @@ public class ProviderAPI extends IntentService { } } else { result.putBoolean(RESULT_KEY, false); - result.putString(SessionDialogInterface.USERNAME, username); + result.putString(SessionDialog.USERNAME, username); result.putString(getResources().getString(R.string.user_message), getResources().getString(R.string.error_srp_math_error_user_message)); } } catch (JSONException e) { @@ -296,7 +296,7 @@ public class ProviderAPI extends IntentService { } catch(JSONException e) {} if(!username.isEmpty()) - user_notification_bundle.putString(SessionDialogInterface.USERNAME, username); + user_notification_bundle.putString(SessionDialog.USERNAME, username); user_notification_bundle.putBoolean(RESULT_KEY, false); return user_notification_bundle; diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index b84b834c..c7691ed4 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -40,6 +40,8 @@ import org.json.JSONObject; import java.net.MalformedURLException; import java.net.URL; +import butterknife.ButterKnife; +import butterknife.InjectView; import de.blinkt.openvpn.activities.LogWindow; import se.leap.bitmaskclient.eip.Constants; import se.leap.bitmaskclient.eip.EIP; @@ -52,7 +54,7 @@ import se.leap.bitmaskclient.eip.EipStatus; * @author Sean Leonard * @author parmegv */ -public class Dashboard extends Activity implements LogInDialog.LogInDialogInterface, SignUpDialog.SignUpDialogInterface, ProviderAPIResultReceiver.Receiver { +public class Dashboard extends Activity implements SessionDialog.LogInDialogInterface, ProviderAPIResultReceiver.Receiver { protected static final int CONFIGURE_LEAP = 0; protected static final int SWITCH_PROVIDER = 1; @@ -70,12 +72,13 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf protected static SharedPreferences preferences; private FragmentManagerEnhanced fragment_manager; - private ProgressBar mProgressBar; - private TextView status_message; - public ProviderAPIResultReceiver providerAPI_result_receiver; + @InjectView(R.id.providerName) + TextView provider_name; + EipServiceFragment eip_fragment; private Provider provider; private static boolean authed_eip; + public ProviderAPIResultReceiver providerAPI_result_receiver; @Override protected void onSaveInstanceState(Bundle outState) { @@ -91,8 +94,6 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf app = this; PRNGFixes.apply(); - - mProgressBar = (ProgressBar) findViewById(R.id.eipProgress); preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); @@ -157,7 +158,7 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf } buildDashboard(false); invalidateOptionsMenu(); - if (data.hasExtra(LogInDialog.TAG)) { + if (data.hasExtra(SessionDialog.TAG)) { logInDialog(Bundle.EMPTY); } } else if (resultCode == RESULT_CANCELED && data.hasExtra(ACTION_QUIT)) { @@ -200,24 +201,21 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf * service dependent UI elements to include. */ private void buildDashboard(boolean hide_and_turn_on_eip) { - setContentView(R.layout.client_dashboard); - - TextView providerNameTV = (TextView) findViewById(R.id.providerName); - providerNameTV.setText(provider.getDomain()); - providerNameTV.setTextSize(28); - - mProgressBar = (ProgressBar) findViewById(R.id.eipProgress); + setContentView(R.layout.dashboard); + ButterKnife.inject(this); + + provider_name.setText(provider.getDomain()); if ( provider.hasEIP()){ fragment_manager.removePreviousFragment(EipServiceFragment.TAG); - EipServiceFragment eip_fragment = new EipServiceFragment(); + eip_fragment = new EipServiceFragment(); if (hide_and_turn_on_eip) { preferences.edit().remove(Dashboard.START_ON_BOOT).apply(); Bundle arguments = new Bundle(); arguments.putBoolean(EipServiceFragment.START_ON_BOOT, true); - eip_fragment.setArguments(arguments); + if(eip_fragment != null) eip_fragment.setArguments(arguments); } fragment_manager.replace(R.id.servicesCollection, eip_fragment, EipServiceFragment.TAG); @@ -279,10 +277,10 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf return true; case R.id.switch_provider: if (provider.hasEIP()){ - if (preferences.getBoolean(Constants.AUTHED_EIP, false)){ - logOut(); - } - eipStop(); + if (preferences.getBoolean(Constants.AUTHED_EIP, false)) { + logOut(); + } + eip_fragment.stopEIP(); } preferences.edit().clear().apply(); startActivityForResult(new Intent(this,ConfigurationWizard.class), SWITCH_PROVIDER); @@ -302,10 +300,7 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf } - private Intent prepareProviderAPICommand() { - mProgressBar = (ProgressBar) findViewById(R.id.eipProgress); - status_message = (TextView) findViewById(R.id.status_message); - + private Intent prepareProviderAPICommand() { providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler()); providerAPI_result_receiver.setReceiver(this); @@ -319,12 +314,12 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf * Shows the log in dialog. */ public void logInDialog(Bundle resultData) { - FragmentTransaction transaction = fragment_manager.removePreviousFragment(LogInDialog.TAG); + FragmentTransaction transaction = fragment_manager.removePreviousFragment(SessionDialog.TAG); - DialogFragment newFragment = LogInDialog.newInstance(); + DialogFragment newFragment = SessionDialog.newInstance(); if(resultData != null && !resultData.isEmpty()) newFragment.setArguments(resultData); - newFragment.show(transaction, LogInDialog.TAG); + newFragment.show(transaction, SessionDialog.TAG); } @Override @@ -334,12 +329,13 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf if(parameters == null) parameters = new Bundle(); - parameters.putString(SessionDialogInterface.USERNAME, username); - parameters.putString(SessionDialogInterface.PASSWORD, password); - - mProgressBar.setVisibility(ProgressBar.VISIBLE); - status_message.setText(R.string.authenticating_message); + parameters.putString(SessionDialog.USERNAME, username); + parameters.putString(SessionDialog.PASSWORD, password); + if(eip_fragment != null) { + eip_fragment.progress_bar.setVisibility(ProgressBar.VISIBLE); + eip_fragment.status_message.setText(R.string.authenticating_message); + } provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters); provider_API_command.setAction(ProviderAPI.SRP_AUTH); startService(provider_API_command); @@ -354,12 +350,11 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf */ public void logOut() { Intent provider_API_command = prepareProviderAPICommand(); - - if(mProgressBar == null) mProgressBar = (ProgressBar) findViewById(R.id.eipProgress); - mProgressBar.setVisibility(ProgressBar.VISIBLE); - if(status_message == null) status_message = (TextView) findViewById(R.id.status_message); - status_message.setText(R.string.logout_message); - + if(eip_fragment != null) { + + eip_fragment.progress_bar.setVisibility(ProgressBar.VISIBLE); + eip_fragment.status_message.setText(R.string.logout_message); + } provider_API_command.setAction(ProviderAPI.LOG_OUT); startService(provider_API_command); } @@ -368,13 +363,13 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf * Shows the sign up dialog. */ public void signUpDialog(Bundle resultData) { - FragmentTransaction transaction = fragment_manager.removePreviousFragment(SignUpDialog.TAG); + FragmentTransaction transaction = fragment_manager.removePreviousFragment(SessionDialog.TAG); - DialogFragment newFragment = SignUpDialog.newInstance(); + DialogFragment newFragment = SessionDialog.newInstance(); if(resultData != null && !resultData.isEmpty()) { newFragment.setArguments(resultData); } - newFragment.show(transaction, SignUpDialog.TAG); + newFragment.show(transaction, SessionDialog.TAG); } @Override @@ -384,12 +379,12 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf if(parameters == null) parameters = new Bundle(); - parameters.putString(SessionDialogInterface.USERNAME, username); - parameters.putString(SessionDialogInterface.PASSWORD, password); - - mProgressBar.setVisibility(ProgressBar.VISIBLE); - status_message.setText(R.string.signingup_message); - + parameters.putString(SessionDialog.USERNAME, username); + parameters.putString(SessionDialog.PASSWORD, password); + if(eip_fragment != null) { + eip_fragment.progress_bar.setVisibility(ProgressBar.VISIBLE); + eip_fragment.status_message.setText(R.string.signingup_message); + } provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters); provider_API_command.setAction(ProviderAPI.SRP_REGISTER); startService(provider_API_command); @@ -418,8 +413,8 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf public void onReceiveResult(int resultCode, Bundle resultData) { Log.d(TAG, "onReceiveResult"); if(resultCode == ProviderAPI.SRP_REGISTRATION_SUCCESSFUL) { - String username = resultData.getString(SessionDialogInterface.USERNAME); - String password = resultData.getString(SessionDialogInterface.PASSWORD); + String username = resultData.getString(SessionDialog.USERNAME); + String password = resultData.getString(SessionDialog.PASSWORD); logIn(username, password); } else if(resultCode == ProviderAPI.SRP_REGISTRATION_FAILED) { changeStatusMessage(resultCode); @@ -477,8 +472,7 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf String request = resultData.getString(Constants.REQUEST_TAG); if(request.equalsIgnoreCase(Constants.ACTION_UPDATE_EIP_SERVICE)) { if(resultCode == Activity.RESULT_OK) { - if(authed_eip) - eipStart(); + if(authed_eip && eip_fragment != null) eip_fragment.startEipFromScratch(); } } } @@ -493,17 +487,16 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf protected void onReceiveResult(int resultCode, Bundle resultData){ super.onReceiveResult(resultCode, resultData); String request = resultData.getString(Constants.REQUEST_TAG); - if(status_message == null) status_message = (TextView) findViewById(R.id.status_message); - if (request.equalsIgnoreCase(Constants.ACTION_IS_EIP_RUNNING)){ + if (request.equalsIgnoreCase(Constants.ACTION_IS_EIP_RUNNING)){ if (resultCode == Activity.RESULT_OK){ switch(previous_result_code){ - case ProviderAPI.SRP_AUTHENTICATION_SUCCESSFUL: status_message.setText(R.string.succesful_authentication_message); break; - case ProviderAPI.SRP_AUTHENTICATION_FAILED: status_message.setText(R.string.authentication_failed_message); break; - case ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE: status_message.setText(R.string.authed_secured_status); break; - case ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE: status_message.setText(R.string.incorrectly_downloaded_certificate_message); break; - case ProviderAPI.LOGOUT_SUCCESSFUL: status_message.setText(R.string.logged_out_message); break; - case ProviderAPI.LOGOUT_FAILED: status_message.setText(R.string.log_out_failed_message); break; + case ProviderAPI.SRP_AUTHENTICATION_SUCCESSFUL: eip_fragment.status_message.setText(R.string.succesful_authentication_message); break; + case ProviderAPI.SRP_AUTHENTICATION_FAILED: eip_fragment.status_message.setText(R.string.authentication_failed_message); break; + case ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE: eip_fragment.status_message.setText(R.string.authed_secured_status); break; + case ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE: eip_fragment.status_message.setText(R.string.incorrectly_downloaded_certificate_message); break; + case ProviderAPI.LOGOUT_SUCCESSFUL: eip_fragment.status_message.setText(R.string.logged_out_message); break; + case ProviderAPI.LOGOUT_FAILED: eip_fragment.status_message.setText(R.string.log_out_failed_message); break; } } @@ -511,13 +504,13 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf switch(previous_result_code){ - case ProviderAPI.SRP_AUTHENTICATION_SUCCESSFUL: status_message.setText(R.string.succesful_authentication_message); break; - case ProviderAPI.SRP_AUTHENTICATION_FAILED: status_message.setText(R.string.authentication_failed_message); break; - case ProviderAPI.SRP_REGISTRATION_FAILED: status_message.setText(R.string.registration_failed_message); break; + case ProviderAPI.SRP_AUTHENTICATION_SUCCESSFUL: eip_fragment.status_message.setText(R.string.succesful_authentication_message); break; + case ProviderAPI.SRP_AUTHENTICATION_FAILED: eip_fragment.status_message.setText(R.string.authentication_failed_message); break; + case ProviderAPI.SRP_REGISTRATION_FAILED: eip_fragment.status_message.setText(R.string.registration_failed_message); break; case ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE: break; - case ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE: status_message.setText(R.string.incorrectly_downloaded_certificate_message); break; - case ProviderAPI.LOGOUT_SUCCESSFUL: status_message.setText(R.string.logged_out_message); break; - case ProviderAPI.LOGOUT_FAILED: status_message.setText(R.string.log_out_failed_message); break; + case ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE: eip_fragment.status_message.setText(R.string.incorrectly_downloaded_certificate_message); break; + case ProviderAPI.LOGOUT_SUCCESSFUL: eip_fragment.status_message.setText(R.string.logged_out_message); break; + case ProviderAPI.LOGOUT_FAILED: eip_fragment.status_message.setText(R.string.log_out_failed_message); break; } } } @@ -528,11 +521,10 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf } private void hideProgressBar() { - if(mProgressBar == null) - mProgressBar = (ProgressBar) findViewById(R.id.eipProgress); - - mProgressBar.setProgress(0); - mProgressBar.setVisibility(ProgressBar.GONE); + if(eip_fragment != null) { + eip_fragment.progress_bar.setProgress(0); + eip_fragment.progress_bar.setVisibility(ProgressBar.GONE); + } } /** @@ -559,20 +551,4 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf eip_intent.putExtra(Constants.RECEIVER_TAG, eip_receiver); startService(eip_intent); } - - private void eipStop(){ - EipServiceFragment eipFragment = (EipServiceFragment) getFragmentManager().findFragmentByTag(EipServiceFragment.TAG); - eipFragment.stopEIP(); - } - - private void eipStart() { - EipServiceFragment eipFragment = (EipServiceFragment) getFragmentManager().findFragmentByTag(EipServiceFragment.TAG); - eipFragment.startEipFromScratch(); - } - - protected void showProgressBar() { - if(mProgressBar == null) - mProgressBar = (ProgressBar) findViewById(R.id.eipProgress); - mProgressBar.setVisibility(ProgressBar.VISIBLE); - } } diff --git a/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java index 592a9552..a8ffda72 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java @@ -12,19 +12,22 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.CompoundButton; +import android.widget.ProgressBar; import android.widget.Switch; import android.widget.TextView; import java.util.Observable; import java.util.Observer; +import butterknife.ButterKnife; +import butterknife.InjectView; +import butterknife.OnCheckedChanged; import de.blinkt.openvpn.activities.DisconnectVPN; import se.leap.bitmaskclient.eip.Constants; import se.leap.bitmaskclient.eip.EIP; import se.leap.bitmaskclient.eip.EipStatus; -public class EipServiceFragment extends Fragment implements Observer, CompoundButton.OnCheckedChangeListener { +public class EipServiceFragment extends Fragment implements Observer { public static String TAG = "se.leap.bitmask.EipServiceFragment"; @@ -33,9 +36,13 @@ public class EipServiceFragment extends Fragment implements Observer, CompoundBu protected static final String STATUS_MESSAGE = TAG + ".status_message"; public static final String START_ON_BOOT = "start on boot"; - private View eipFragment; - private static Switch eipSwitch; - private TextView status_message; + private View view; + @InjectView(R.id.eipSwitch) + Switch eip_switch; + @InjectView(R.id.status_message) + TextView status_message; + @InjectView(R.id.eipProgress) + ProgressBar progress_bar; private static Activity parent_activity; private static EIPReceiver mEIPReceiver; @@ -57,35 +64,27 @@ public class EipServiceFragment extends Fragment implements Observer, CompoundBu @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - - eipFragment = inflater.inflate(R.layout.eip_service_fragment, container, false); - View eipDetail = eipFragment.findViewById(R.id.eipDetail); - eipDetail.setVisibility(View.VISIBLE); - - View eipSettings = eipFragment.findViewById(R.id.eipSettings); - eipSettings.setVisibility(View.GONE); // FIXME too! + view = inflater.inflate(R.layout.eip_service_fragment, container, false); + ButterKnife.inject(this, view); if (eip_status.isConnecting()) - eipFragment.findViewById(R.id.eipProgress).setVisibility(View.VISIBLE); - - status_message = (TextView) eipFragment.findViewById(R.id.status_message); + eip_switch.setVisibility(View.VISIBLE); - eipSwitch = (Switch) eipFragment.findViewById(R.id.eipSwitch); - Log.d(TAG, "onCreateView, eipSwitch is checked? " + eipSwitch.isChecked()); - eipSwitch.setOnCheckedChangeListener(this); - - if(getArguments() != null && getArguments().containsKey(START_ON_BOOT) && getArguments().getBoolean(START_ON_BOOT)) + Log.d(TAG, "onCreateView, eip_switch is checked? " + eip_switch.isChecked()); + + Bundle arguments = getArguments(); + if(arguments != null && arguments.containsKey(START_ON_BOOT) && arguments.getBoolean(START_ON_BOOT)) startEipFromScratch(); if (savedInstanceState != null) { - setStatusMessage(savedInstanceState.getString(STATUS_MESSAGE)); + status_message.setText(savedInstanceState.getString(STATUS_MESSAGE)); if(savedInstanceState.getBoolean(IS_PENDING)) eip_status.setConnecting(); else if(savedInstanceState.getBoolean(IS_CONNECTED)) { eip_status.setConnectedOrDisconnected(); } } - return eipFragment; + return view; } @Override @@ -106,22 +105,16 @@ public class EipServiceFragment extends Fragment implements Observer, CompoundBu protected void saveEipStatus() { boolean eip_is_on = false; Log.d(TAG, "saveEipStatus"); - if(eipSwitch.isChecked()) { + if(eip_switch.isChecked()) { eip_is_on = true; } if(parent_activity != null) Dashboard.preferences.edit().putBoolean(Dashboard.START_ON_BOOT, eip_is_on).commit(); } - - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - if (buttonView.equals(eipSwitch)){ - handleSwitch(isChecked); - } - } - - private void handleSwitch(boolean isChecked) { + + @OnCheckedChanged(R.id.eipSwitch) + void handleSwitch(boolean isChecked) { if(isChecked) handleSwitchOn(); else @@ -175,31 +168,30 @@ public class EipServiceFragment extends Fragment implements Observer, CompoundBu .setNegativeButton(parent_activity.getString(R.string.no), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - eipSwitch.setChecked(true); + eip_switch.setChecked(true); } }) .show(); } public void startEipFromScratch() { - eipFragment.findViewById(R.id.eipProgress).setVisibility(View.VISIBLE); + progress_bar.setVisibility(View.VISIBLE); + eip_switch.setVisibility(View.VISIBLE); String status = parent_activity.getString(R.string.eip_status_start_pending); - setStatusMessage(status); + status_message.setText(status); - if(!eipSwitch.isChecked()) { - eipSwitch.setChecked(true); + if(!eip_switch.isChecked()) { + eip_switch.setChecked(true); saveEipStatus(); } eipCommand(Constants.ACTION_START_EIP); } protected void stopEIP() { - View eipProgressBar = parent_activity.findViewById(R.id.eipProgress); - if(eipProgressBar != null) - eipProgressBar.setVisibility(View.GONE); - + hideProgressBar(); + String status = parent_activity.getString(R.string.eip_state_not_connected); - setStatusMessage(status); + status_message.setText(status); eipCommand(Constants.ACTION_STOP_EIP); } @@ -247,26 +239,26 @@ public class EipServiceFragment extends Fragment implements Observer, CompoundBu hideProgressBar(); Log.d(TAG, "setConnectedUi? " + eip_status.isConnected()); adjustSwitch(); - setStatusMessage(parent_activity.getString(R.string.eip_state_connected)); + status_message.setText(parent_activity.getString(R.string.eip_state_connected)); } private void setDisconnectedUI(){ hideProgressBar(); adjustSwitch(); - setStatusMessage(parent_activity.getString(R.string.eip_state_not_connected)); + status_message.setText(parent_activity.getString(R.string.eip_state_not_connected)); } private void adjustSwitch() { if(eip_status.isConnected() || eip_status.isConnecting()) { - Log.d(TAG, "adjustSwitch, isConnected || isConnecting, is checked? " + eipSwitch.isChecked()); - if(!eipSwitch.isChecked()) { - eipSwitch.setChecked(true); + Log.d(TAG, "adjustSwitch, isConnected || isConnecting, is checked? " + eip_switch.isChecked()); + if(!eip_switch.isChecked()) { + eip_switch.setChecked(true); } } else { Log.d(TAG, "adjustSwitch, !isConnected && !isConnecting? " + eip_status.toString()); - if(eipSwitch.isChecked()) { - eipSwitch.setChecked(false); + if(eip_switch.isChecked()) { + eip_switch.setChecked(false); } } } @@ -275,8 +267,8 @@ public class EipServiceFragment extends Fragment implements Observer, CompoundBu int localizedResId = eip_status.getLocalizedResId(); String logmessage = eip_status.getLogMessage(); String prefix = parent_activity.getString(localizedResId); - - setStatusMessage(prefix + " " + logmessage); + + status_message.setText(prefix + " " + logmessage); adjustSwitch(); } @@ -287,8 +279,8 @@ public class EipServiceFragment extends Fragment implements Observer, CompoundBu } private void hideProgressBar() { - if(parent_activity != null && parent_activity.findViewById(R.id.eipProgress) != null) - parent_activity.findViewById(R.id.eipProgress).setVisibility(View.GONE); + if(progress_bar != null) + progress_bar.setVisibility(View.GONE); } protected class EIPReceiver extends ResultReceiver { @@ -307,10 +299,10 @@ public class EipServiceFragment extends Fragment implements Observer, CompoundBu switch (resultCode){ case Activity.RESULT_OK: Log.d(TAG, "Action start eip = Result OK"); - eipFragment.findViewById(R.id.eipProgress).setVisibility(View.VISIBLE); + progress_bar.setVisibility(View.VISIBLE); break; case Activity.RESULT_CANCELED: - eipFragment.findViewById(R.id.eipProgress).setVisibility(View.GONE); + progress_bar.setVisibility(View.GONE); break; } } else if (request.equals(Constants.ACTION_STOP_EIP)) { @@ -337,9 +329,8 @@ public class EipServiceFragment extends Fragment implements Observer, CompoundBu case Activity.RESULT_CANCELED: Dashboard dashboard = (Dashboard) parent_activity; - dashboard.showProgressBar(); - String status = parent_activity.getString(R.string.updating_certificate_message); - setStatusMessage(status); + progress_bar.setVisibility(View.VISIBLE); + status_message.setText(getString(R.string.updating_certificate_message)); if(LeapSRPSession.getToken().isEmpty() && !Dashboard.preferences.getBoolean(Constants.ALLOWED_ANON, false)) { dashboard.logInDialog(Bundle.EMPTY); } else { diff --git a/app/src/main/java/se/leap/bitmaskclient/LogInDialog.java b/app/src/main/java/se/leap/bitmaskclient/LogInDialog.java deleted file mode 100644 index 5263392e..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/LogInDialog.java +++ /dev/null @@ -1,144 +0,0 @@ -/** - * Copyright (c) 2013 LEAP Encryption Access Project and contributers - * - * This program 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. - * - * This program 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 this program. If not, see . - */ - package se.leap.bitmaskclient; - -import android.app.Activity; -import android.app.AlertDialog; -import android.app.DialogFragment; -import android.content.DialogInterface; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.EditText; -import android.widget.TextView; - -/** - * Implements the log in dialog, currently without progress dialog. - * - * It returns to the previous fragment when finished, and sends username and password to the authenticate method. - * - * It also notifies the user if the password is not valid. - * - * @author parmegv - * - */ -public class LogInDialog extends SessionDialogInterface { - - - final public static String TAG = LogInDialog.class.getSimpleName(); - - private static LogInDialog dialog; - - private static boolean is_eip_pending = false; - - public AlertDialog onCreateDialog(Bundle savedInstanceState) { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - LayoutInflater inflater = getActivity().getLayoutInflater(); - View log_in_dialog_view = inflater.inflate(R.layout.log_in_dialog, null); - - final TextView user_message = (TextView)log_in_dialog_view.findViewById(R.id.user_message); - final EditText username_field = (EditText)log_in_dialog_view.findViewById(R.id.username_entered); - final EditText password_field = (EditText)log_in_dialog_view.findViewById(R.id.password_entered); - - if(!username_field.getText().toString().isEmpty() && password_field.isFocusable()) { - password_field.requestFocus(); - } - if (getArguments() != null) { - is_eip_pending = getArguments().getBoolean(EipServiceFragment.IS_PENDING, false); - if (getArguments().containsKey(PASSWORD_INVALID_LENGTH)) - password_field.setError(getResources().getString(R.string.error_not_valid_password_user_message)); - if (getArguments().containsKey(USERNAME)) { - String username = getArguments().getString(USERNAME); - username_field.setText(username); - } - if (getArguments().containsKey(USERNAME_MISSING)) { - username_field.setError(getResources().getString(R.string.username_ask)); - } - if(getArguments().containsKey(getResources().getString(R.string.user_message))) - user_message.setText(getArguments().getString(getResources().getString(R.string.user_message))); - else - user_message.setVisibility(View.GONE); - } - - builder.setView(log_in_dialog_view) - .setPositiveButton(R.string.login_button, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - String username = username_field.getText().toString(); - String password = password_field.getText().toString(); - dialog.dismiss(); - interface_with_Dashboard.logIn(username, password); - } - }) - .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - interface_with_Dashboard.cancelLoginOrSignup(); - } - }) - .setNeutralButton(R.string.signup_button, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - String username = username_field.getText().toString(); - String password = password_field.getText().toString(); - interface_with_Dashboard.signUp(username, password); - } - }); - - return builder.create(); - } - - /** - * Interface used to communicate LogInDialog with Dashboard. - * - * @author parmegv - * - */ - public interface LogInDialogInterface { - public void logIn(String username, String password); - public void signUp(String username, String password); - public void cancelLoginOrSignup(); - } - - LogInDialogInterface interface_with_Dashboard; - - /** - * @return a new instance of this DialogFragment. - */ - public static DialogFragment newInstance() { - if(dialog == null) - dialog = new LogInDialog(); - - return dialog; - } - - @Override - public void onAttach(Activity activity) { - super.onAttach(activity); - try { - interface_with_Dashboard = (LogInDialogInterface) activity; - } catch (ClassCastException e) { - throw new ClassCastException(activity.toString() - + " must implement LogInDialogListener"); - } - } - - @Override - public void onCancel(DialogInterface dialog) { - super.onCancel(dialog); - if(is_eip_pending) - interface_with_Dashboard.cancelLoginOrSignup(); - } -} diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderListFragment.java b/app/src/main/java/se/leap/bitmaskclient/ProviderListFragment.java deleted file mode 100644 index 875b76d7..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderListFragment.java +++ /dev/null @@ -1,216 +0,0 @@ -/** - * Copyright (c) 2013 LEAP Encryption Access Project and contributers - * - * This program 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. - * - * This program 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 this program. If not, see . - */ - package se.leap.bitmaskclient; - -import android.app.Activity; -import android.app.ListFragment; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ListView; - -import butterknife.ButterKnife; -import se.leap.bitmaskclient.ProviderListContent.ProviderItem; - -/** - * A list fragment representing a list of Providers. This fragment - * also supports tablet devices by allowing list items to be given an - * 'activated' state upon selection. - *

- * Activities containing this fragment MUST implement the {@link Callbacks} - * interface. - */ -public class ProviderListFragment extends ListFragment { - - public static String TAG = "provider_list_fragment"; - public static String SHOW_ALL_PROVIDERS = "show_all_providers"; - public static String TOP_PADDING = "top padding from providerlistfragment"; - private ProviderListAdapter content_adapter; - - /** - * The serialization (saved instance state) Bundle key representing the - * activated item position. Only used on tablets. - */ - private static final String STATE_ACTIVATED_POSITION = "activated_position"; - - /** - * The fragment's current callback object, which is notified of list item - * clicks. - */ - private Callbacks mCallbacks = sDummyCallbacks; - - /** - * The current activated item position. Only used on tablets. - */ - private int mActivatedPosition = ListView.INVALID_POSITION; - - /** - * A callback interface that all activities containing this fragment must - * implement. This mechanism allows activities to be notified of item - * selections. - */ - public interface Callbacks { - /** - * Callback for when an item has been selected. - */ - public void onItemSelected(String id); - } - - /** - * A dummy implementation of the {@link Callbacks} interface that does - * nothing. Used only when this fragment is not attached to an activity. - */ - private static Callbacks sDummyCallbacks = new Callbacks() { - @Override - public void onItemSelected(String id) { - } - }; - - /** - * Mandatory empty constructor for the fragment manager to instantiate the - * fragment (e.g. upon screen orientation changes). - */ - public ProviderListFragment() { - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) { - View view = inflater.inflate(R.layout.provider_list_fragment, container, false); - ButterKnife.inject(this, view); - return view; - } - - @Override - public void onViewCreated(View view, Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - - // Restore the previously serialized activated item position. - if (savedInstanceState != null - && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) { - setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION)); - } - if(getArguments() != null && getArguments().containsKey(TOP_PADDING)) { - int topPadding = getArguments().getInt(TOP_PADDING); - View current_view = getView(); - current_view.setPadding(current_view.getPaddingLeft(), topPadding, current_view.getPaddingRight(), current_view.getPaddingBottom()); - } - } - - @Override - public void onAttach(Activity activity) { - super.onAttach(activity); - - // Activities containing this fragment must implement its callbacks. - if (!(activity instanceof Callbacks)) { - throw new IllegalStateException("Activity must implement fragment's callbacks."); - } - - mCallbacks = (Callbacks) activity; - } - - @Override - public void onDetach() { - super.onDetach(); - - // Reset the active callbacks interface to the dummy implementation. - mCallbacks = sDummyCallbacks; - } - - @Override - public void onListItemClick(ListView listView, View view, int position, long id) { - super.onListItemClick(listView, view, position, id); - - // Notify the active callbacks interface (the activity, if the - // fragment is attached to one) that an item has been selected. - mCallbacks.onItemSelected(ProviderListContent.ITEMS.get(position).name()); - - for(int item_position = 0; item_position < listView.getCount(); item_position++) { - if(item_position != position) - content_adapter.hide(item_position); - } - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - if (mActivatedPosition != ListView.INVALID_POSITION) { - // Serialize and persist the activated item position. - outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition); - } - } - - public void notifyAdapter() { - content_adapter.notifyDataSetChanged(); - } - /** - * Turns on activate-on-click mode. When this mode is on, list items will be - * given the 'activated' state when touched. - */ - public void setActivateOnItemClick(boolean activateOnItemClick) { - // When setting CHOICE_MODE_SINGLE, ListView will automatically - // give items the 'activated' state when touched. - getListView().setChoiceMode(activateOnItemClick - ? ListView.CHOICE_MODE_SINGLE - : ListView.CHOICE_MODE_NONE); - } - - private void setActivatedPosition(int position) { - if (position == ListView.INVALID_POSITION) { - getListView().setItemChecked(mActivatedPosition, false); - } else { - getListView().setItemChecked(position, true); - } - - mActivatedPosition = position; - } - - public void removeLastItem() { - content_adapter.remove(content_adapter.getItem(content_adapter.getCount()-1)); - content_adapter.notifyDataSetChanged(); - } - - public void addItem(Provider provider) { - content_adapter.add(provider); - content_adapter.notifyDataSetChanged(); - } - - public void hideAllBut(int position) { - int real_count = content_adapter.getCount(); - for(int i = 0; i < real_count;) - if(i != position) { - content_adapter.hide(i); - position--; - real_count--; - } else { - i++; - } - } - - /** - * @return a new instance of this ListFragment. - */ - public static ProviderListFragment newInstance() { - return new ProviderListFragment(); - } -} diff --git a/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java b/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java new file mode 100644 index 00000000..9676faa8 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java @@ -0,0 +1,159 @@ +/** + * Copyright (c) 2013 LEAP Encryption Access Project and contributers + * + * This program 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. + * + * This program 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 this program. If not, see . + */ + package se.leap.bitmaskclient; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.DialogFragment; +import android.content.DialogInterface; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.EditText; +import android.widget.TextView; + +import butterknife.ButterKnife; +import butterknife.InjectView; + +/** + * Implements the log in dialog, currently without progress dialog. + * + * It returns to the previous fragment when finished, and sends username and password to the authenticate method. + * + * It also notifies the user if the password is not valid. + * + * @author parmegv + * + */ +public class SessionDialog extends DialogFragment{ + + + final public static String TAG = SessionDialog.class.getSimpleName(); + + final public static String USERNAME = "username"; + final public static String PASSWORD = "password"; + final public static String USERNAME_MISSING = "username missing"; + final public static String PASSWORD_INVALID_LENGTH = "password_invalid_length"; + + @InjectView(R.id.user_message) + TextView user_message; + @InjectView(R.id.username_entered) + EditText username_field; + @InjectView(R.id.password_entered) + EditText password_field; + + private static SessionDialog dialog; + + private static boolean is_eip_pending = false; + + public AlertDialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + LayoutInflater inflater = getActivity().getLayoutInflater(); + View view = inflater.inflate(R.layout.session_dialog, null); + ButterKnife.inject(this, view); + + if(!username_field.getText().toString().isEmpty() && password_field.isFocusable()) { + password_field.requestFocus(); + } + + Bundle arguments = getArguments(); + if (arguments != null) { + is_eip_pending = arguments.getBoolean(EipServiceFragment.IS_PENDING, false); + if (arguments.containsKey(PASSWORD_INVALID_LENGTH)) + password_field.setError(getString(R.string.error_not_valid_password_user_message)); + if (arguments.containsKey(USERNAME)) { + String username = arguments.getString(USERNAME); + username_field.setText(username); + } + if (arguments.containsKey(USERNAME_MISSING)) { + username_field.setError(getString(R.string.username_ask)); + } + if(arguments.containsKey(getString(R.string.user_message))) + user_message.setText(arguments.getString(getString(R.string.user_message))); + else + user_message.setVisibility(View.GONE); + } + + builder.setView(view) + .setPositiveButton(R.string.login_button, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + String username = username_field.getText().toString(); + String password = password_field.getText().toString(); + dialog.dismiss(); + interface_with_Dashboard.logIn(username, password); + } + }) + .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + interface_with_Dashboard.cancelLoginOrSignup(); + } + }) + .setNeutralButton(R.string.signup_button, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + String username = username_field.getText().toString(); + String password = password_field.getText().toString(); + interface_with_Dashboard.signUp(username, password); + } + }); + + return builder.create(); + } + + + /** + * Interface used to communicate SessionDialog with Dashboard. + * + * @author parmegv + * + */ + public interface LogInDialogInterface { + public void logIn(String username, String password); + public void signUp(String username, String password); + public void cancelLoginOrSignup(); + } + + LogInDialogInterface interface_with_Dashboard; + + /** + * @return a new instance of this DialogFragment. + */ + public static DialogFragment newInstance() { + if(dialog == null) + dialog = new SessionDialog(); + + return dialog; + } + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + try { + interface_with_Dashboard = (LogInDialogInterface) activity; + } catch (ClassCastException e) { + throw new ClassCastException(activity.toString() + + " must implement LogInDialogListener"); + } + } + + @Override + public void onCancel(DialogInterface dialog) { + super.onCancel(dialog); + if(is_eip_pending) + interface_with_Dashboard.cancelLoginOrSignup(); + } +} diff --git a/app/src/main/java/se/leap/bitmaskclient/SessionDialogInterface.java b/app/src/main/java/se/leap/bitmaskclient/SessionDialogInterface.java deleted file mode 100644 index 66b86ccd..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/SessionDialogInterface.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright (c) 2013 LEAP Encryption Access Project and contributers - * - * This program 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. - * - * This program 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 this program. If not, see . - */ -package se.leap.bitmaskclient; - -import android.app.Activity; -import android.app.DialogFragment; -import android.content.DialogInterface; - -/** - * @author parmegv - */ -public abstract class SessionDialogInterface extends DialogFragment { - final public static String USERNAME = "username"; - final public static String PASSWORD = "password"; - final public static String USERNAME_MISSING = "username missing"; - final public static String PASSWORD_INVALID_LENGTH = "password_invalid_length"; - - @Override - public void onAttach(Activity activity) { super.onAttach(activity); } - - @Override - public void onCancel(DialogInterface dialog) { super.onCancel(dialog); } -} diff --git a/app/src/main/java/se/leap/bitmaskclient/SignUpDialog.java b/app/src/main/java/se/leap/bitmaskclient/SignUpDialog.java deleted file mode 100644 index f6d6cc3f..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/SignUpDialog.java +++ /dev/null @@ -1,132 +0,0 @@ -/** - * Copyright (c) 2013 LEAP Encryption Access Project and contributers - * - * This program 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. - * - * This program 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 this program. If not, see . - */ - package se.leap.bitmaskclient; - -import android.app.Activity; -import android.app.AlertDialog; -import android.app.DialogFragment; -import android.content.DialogInterface; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.EditText; -import android.widget.TextView; - -/** - * Implements the sign up dialog, currently without progress dialog. - * - * It returns to the previous fragment when finished, and sends username and password to the registration method. - * - * It also notifies the user if the password is not valid. - * - * @author parmegv - * - */ -public class SignUpDialog extends SessionDialogInterface { - - final public static String TAG = SignUpDialog.class.getSimpleName(); - - private static SignUpDialog dialog; - private static boolean is_eip_pending = false; - - public AlertDialog onCreateDialog(Bundle savedInstanceState) { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - LayoutInflater inflater = getActivity().getLayoutInflater(); - View log_in_dialog_view = inflater.inflate(R.layout.log_in_dialog, null); - - final TextView user_message = (TextView)log_in_dialog_view.findViewById(R.id.user_message); - final EditText username_field = (EditText)log_in_dialog_view.findViewById(R.id.username_entered); - final EditText password_field = (EditText)log_in_dialog_view.findViewById(R.id.password_entered); - - if(!username_field.getText().toString().isEmpty() && password_field.isFocusable()) { - password_field.requestFocus(); - } - if (getArguments() != null) { - is_eip_pending = getArguments().getBoolean(EipServiceFragment.IS_PENDING, false); - if (getArguments().containsKey(PASSWORD_INVALID_LENGTH)) - password_field.setError(getResources().getString(R.string.error_not_valid_password_user_message)); - if(getArguments().containsKey(USERNAME_MISSING)) - username_field.setError(getResources().getString(R.string.username_ask)); - if(getArguments().containsKey(USERNAME)) { - String username = getArguments().getString(USERNAME); - username_field.setText(username); - } - if(getArguments().containsKey(getResources().getString(R.string.user_message))) - user_message.setText(getArguments().getString(getResources().getString(R.string.user_message))); - else - user_message.setVisibility(View.GONE); - } - - builder.setView(log_in_dialog_view) - .setPositiveButton(R.string.signup_button, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - String username = username_field.getText().toString(); - String password = password_field.getText().toString(); - dialog.dismiss(); - interface_with_Dashboard.signUp(username, password); - } - }) - .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - interface_with_Dashboard.cancelLoginOrSignup(); - } - }); - - return builder.create(); - } - - /** - * Interface used to communicate SignUpDialog with Dashboard. - * - * @author parmegv - * - */ - public interface SignUpDialogInterface { - public void signUp(String username, String password); - public void cancelLoginOrSignup(); - } - - SignUpDialogInterface interface_with_Dashboard; - - /** - * @return a new instance of this DialogFragment. - */ - public static DialogFragment newInstance() { - if(dialog == null) - dialog = new SignUpDialog(); - return dialog; - } - - @Override - public void onAttach(Activity activity) { - super.onAttach(activity); - try { - interface_with_Dashboard = (SignUpDialogInterface) activity; - } catch (ClassCastException e) { - throw new ClassCastException(activity.toString() - + " must implement SignUpDialogListener"); - } - } - - @Override - public void onCancel(DialogInterface dialog) { - if(is_eip_pending) - interface_with_Dashboard.cancelLoginOrSignup(); - super.onCancel(dialog); - } -} diff --git a/app/src/main/res/layout-xlarge/client_dashboard.xml b/app/src/main/res/layout-xlarge/client_dashboard.xml deleted file mode 100644 index bd644e1e..00000000 --- a/app/src/main/res/layout-xlarge/client_dashboard.xml +++ /dev/null @@ -1,69 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout-xlarge/dashboard.xml b/app/src/main/res/layout-xlarge/dashboard.xml new file mode 100644 index 00000000..bd644e1e --- /dev/null +++ b/app/src/main/res/layout-xlarge/dashboard.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout-xlarge/eip_service_fragment.xml b/app/src/main/res/layout-xlarge/eip_service_fragment.xml index d217e1a1..38b6aca3 100644 --- a/app/src/main/res/layout-xlarge/eip_service_fragment.xml +++ b/app/src/main/res/layout-xlarge/eip_service_fragment.xml @@ -37,39 +37,20 @@ android:layout_marginLeft="15dp" android:layout_marginRight="15dp" /> - - - - - + android:layout_alignParentLeft="true" + android:layout_centerVertical="true" + android:clickable="true" + android:text="@string/eip_state_not_connected" + android:textSize="16sp" /> - diff --git a/app/src/main/res/layout-xlarge/log_in_dialog.xml b/app/src/main/res/layout-xlarge/log_in_dialog.xml deleted file mode 100644 index 3a9eebb8..00000000 --- a/app/src/main/res/layout-xlarge/log_in_dialog.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout-xlarge/provider_list_fragment.xml b/app/src/main/res/layout-xlarge/provider_list_fragment.xml deleted file mode 100644 index 59dd37d1..00000000 --- a/app/src/main/res/layout-xlarge/provider_list_fragment.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - diff --git a/app/src/main/res/layout-xlarge/session_dialog.xml b/app/src/main/res/layout-xlarge/session_dialog.xml new file mode 100644 index 00000000..3a9eebb8 --- /dev/null +++ b/app/src/main/res/layout-xlarge/session_dialog.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/client_dashboard.xml b/app/src/main/res/layout/client_dashboard.xml deleted file mode 100644 index f33ac285..00000000 --- a/app/src/main/res/layout/client_dashboard.xml +++ /dev/null @@ -1,51 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout/dashboard.xml b/app/src/main/res/layout/dashboard.xml new file mode 100644 index 00000000..67a1122f --- /dev/null +++ b/app/src/main/res/layout/dashboard.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/eip_service_fragment.xml b/app/src/main/res/layout/eip_service_fragment.xml index be2aa791..64d22147 100644 --- a/app/src/main/res/layout/eip_service_fragment.xml +++ b/app/src/main/res/layout/eip_service_fragment.xml @@ -22,8 +22,8 @@ android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="10dp" /> - - - - - - - - - + android:clickable="true" + android:text="@string/eip_state_not_connected" /> diff --git a/app/src/main/res/layout/log_in_dialog.xml b/app/src/main/res/layout/log_in_dialog.xml deleted file mode 100644 index c8a2f0a8..00000000 --- a/app/src/main/res/layout/log_in_dialog.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/provider_list_fragment.xml b/app/src/main/res/layout/provider_list_fragment.xml deleted file mode 100644 index 70dbae0d..00000000 --- a/app/src/main/res/layout/provider_list_fragment.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - diff --git a/app/src/main/res/layout/session_dialog.xml b/app/src/main/res/layout/session_dialog.xml new file mode 100644 index 00000000..62215ae8 --- /dev/null +++ b/app/src/main/res/layout/session_dialog.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java index 42736444..c90ce481 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -58,7 +58,6 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download @Inject ProviderListAdapter adapter; private ProviderManager provider_manager; - private ProviderListFragment provider_list_fragment; private Intent mConfigState = new Intent(); private Provider selected_provider; @@ -162,10 +161,6 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download initProviderList(); } - private void putProviderListFragment() { - fragment_manager.replace(R.id.configuration_wizard_layout, provider_list_fragment, ProviderListFragment.TAG); - } - @Override protected void onDestroy() { super.onDestroy(); @@ -463,7 +458,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download public void login() { Intent ask_login = new Intent(); ask_login.putExtra(Provider.KEY, selected_provider); - ask_login.putExtra(LogInDialog.TAG, LogInDialog.TAG); + ask_login.putExtra(SessionDialog.TAG, SessionDialog.TAG); setResult(RESULT_OK, ask_login); setting_up_provider = false; finish(); diff --git a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java index fc74b702..f5f27247 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java @@ -32,6 +32,7 @@ import org.apache.http.client.ClientProtocolException; import org.json.*; import se.leap.bitmaskclient.R; +import se.leap.bitmaskclient.SessionDialog; import se.leap.bitmaskclient.eip.*; /** @@ -169,8 +170,8 @@ public class ProviderAPI extends IntentService { Bundle session_id_bundle = new Bundle(); int progress = 0; - String username = (String) task.get(SessionDialogInterface.USERNAME); - String password = (String) task.get(SessionDialogInterface.PASSWORD); + String username = (String) task.get(SessionDialog.USERNAME); + String password = (String) task.get(SessionDialog.PASSWORD); if(validUserLoginData(username, password)) { session_id_bundle = register(username, password); @@ -178,12 +179,12 @@ public class ProviderAPI extends IntentService { } else { if(!wellFormedPassword(password)) { session_id_bundle.putBoolean(RESULT_KEY, false); - session_id_bundle.putString(SessionDialogInterface.USERNAME, username); - session_id_bundle.putBoolean(SessionDialogInterface.PASSWORD_INVALID_LENGTH, true); + session_id_bundle.putString(SessionDialog.USERNAME, username); + session_id_bundle.putBoolean(SessionDialog.PASSWORD_INVALID_LENGTH, true); } if(username.isEmpty()) { session_id_bundle.putBoolean(RESULT_KEY, false); - session_id_bundle.putBoolean(SessionDialogInterface.USERNAME_MISSING, true); + session_id_bundle.putBoolean(SessionDialog.USERNAME_MISSING, true); } } @@ -202,8 +203,8 @@ public class ProviderAPI extends IntentService { if(api_result.has(ERRORS)) result = authFailedNotification(api_result, username); else { - result.putString(SessionDialogInterface.USERNAME, username); - result.putString(SessionDialogInterface.PASSWORD, password); + result.putString(SessionDialog.USERNAME, username); + result.putString(SessionDialog.PASSWORD, password); result.putBoolean(RESULT_KEY, true); } @@ -220,20 +221,20 @@ public class ProviderAPI extends IntentService { Bundle result = new Bundle(); int progress = 0; - String username = (String) task.get(SessionDialogInterface.USERNAME); - String password = (String) task.get(SessionDialogInterface.PASSWORD); + String username = (String) task.get(SessionDialog.USERNAME); + String password = (String) task.get(SessionDialog.PASSWORD); if(validUserLoginData(username, password)) { result = authenticate(username, password); broadcast_progress(progress++); } else { if(!wellFormedPassword(password)) { result.putBoolean(RESULT_KEY, false); - result.putString(SessionDialogInterface.USERNAME, username); - result.putBoolean(SessionDialogInterface.PASSWORD_INVALID_LENGTH, true); + result.putString(SessionDialog.USERNAME, username); + result.putBoolean(SessionDialog.PASSWORD_INVALID_LENGTH, true); } if(username.isEmpty()) { result.putBoolean(RESULT_KEY, false); - result.putBoolean(SessionDialogInterface.USERNAME_MISSING, true); + result.putBoolean(SessionDialog.USERNAME_MISSING, true); } } @@ -262,7 +263,7 @@ public class ProviderAPI extends IntentService { } } else { result.putBoolean(RESULT_KEY, false); - result.putString(SessionDialogInterface.USERNAME, username); + result.putString(SessionDialog.USERNAME, username); result.putString(getResources().getString(R.string.user_message), getResources().getString(R.string.error_srp_math_error_user_message)); } } catch (JSONException e) { @@ -293,7 +294,7 @@ public class ProviderAPI extends IntentService { } catch(JSONException e) {} if(!username.isEmpty()) - user_notification_bundle.putString(SessionDialogInterface.USERNAME, username); + user_notification_bundle.putString(SessionDialog.USERNAME, username); user_notification_bundle.putBoolean(RESULT_KEY, false); return user_notification_bundle; -- cgit v1.2.3 From 2d764ff0261e902e3b41f7f5e1d79df52e51a1ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 9 Dec 2014 21:01:50 +0100 Subject: Bug in the add provider to the adapter fixed. Refactored the new provider dialog too. --- .../se/leap/bitmaskclient/ConfigurationWizard.java | 5 +- .../se/leap/bitmaskclient/NewProviderDialog.java | 73 +++++++------ .../se/leap/bitmaskclient/ProviderListAdapter.java | 2 +- .../se/leap/bitmaskclient/ConfigurationWizard.java | 5 +- .../se/leap/bitmaskclient/NewProviderDialog.java | 117 +++++++++++---------- 5 files changed, 100 insertions(+), 102 deletions(-) (limited to 'app') diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java index 63185d12..ac2e00b8 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -333,8 +333,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download */ public void addAndSelectNewProvider() { FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(NewProviderDialog.TAG); - DialogFragment newFragment = NewProviderDialog.newInstance(); - newFragment.show(fragment_transaction, NewProviderDialog.TAG); + new NewProviderDialog().show(fragment_transaction, NewProviderDialog.TAG); } /** @@ -343,7 +342,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download public void addAndSelectNewProvider(String main_url, boolean danger_on) { FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(NewProviderDialog.TAG); - DialogFragment newFragment = NewProviderDialog.newInstance(); + DialogFragment newFragment = new NewProviderDialog(); Bundle data = new Bundle(); data.putString(Provider.MAIN_URL, main_url); data.putBoolean(ProviderItem.DANGER_ON, danger_on); diff --git a/app/src/debug/java/se/leap/bitmaskclient/NewProviderDialog.java b/app/src/debug/java/se/leap/bitmaskclient/NewProviderDialog.java index cf09c64b..8fe1c3eb 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/NewProviderDialog.java +++ b/app/src/debug/java/se/leap/bitmaskclient/NewProviderDialog.java @@ -16,14 +16,14 @@ */ package se.leap.bitmaskclient; +import butterknife.ButterKnife; +import butterknife.InjectView; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; -import se.leap.bitmaskclient.R; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.content.DialogInterface; -import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -40,20 +40,17 @@ import android.widget.Toast; public class NewProviderDialog extends DialogFragment { final public static String TAG = "newProviderDialog"; - + + @InjectView(R.id.new_provider_url) + EditText url_input_field; + @InjectView(R.id.danger_checkbox) + CheckBox danger_checkbox; + public interface NewProviderDialogInterface { public void showAndSelectProvider(String url_provider, boolean danger_on); } NewProviderDialogInterface interface_with_ConfigurationWizard; - - /** - * @return a new instance of this DialogFragment. - */ - public static DialogFragment newInstance() { - NewProviderDialog dialog_fragment = new NewProviderDialog(); - return dialog_fragment; - } @Override public void onAttach(Activity activity) { @@ -70,36 +67,19 @@ public class NewProviderDialog extends DialogFragment { public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); - View new_provider_dialog_view = inflater.inflate(R.layout.new_provider_dialog, null); - final EditText url_input_field = (EditText)new_provider_dialog_view.findViewById(R.id.new_provider_url); - if(getArguments() != null && getArguments().containsKey(Provider.MAIN_URL)) { - url_input_field.setText(getArguments().getString(Provider.MAIN_URL)); - } - final CheckBox danger_checkbox = (CheckBox)new_provider_dialog_view.findViewById(R.id.danger_checkbox); - if(getArguments() != null && getArguments().containsKey(ProviderItem.DANGER_ON)) { - danger_checkbox.setActivated(getArguments().getBoolean(ProviderItem.DANGER_ON)); - } + View view = inflater.inflate(R.layout.new_provider_dialog, null); + ButterKnife.inject(this, view); + Bundle arguments = getArguments(); + if(arguments != null) { + url_input_field.setText(arguments.getString(Provider.MAIN_URL, "")); + danger_checkbox.setActivated(arguments.getBoolean(ProviderItem.DANGER_ON, false)); + } - builder.setView(new_provider_dialog_view) + builder.setView(view) .setMessage(R.string.introduce_new_provider) .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { - String entered_url = url_input_field.getText().toString().trim(); - if(!entered_url.startsWith("https://")) { - if (entered_url.startsWith("http://")){ - entered_url = entered_url.substring("http://".length()); - } - entered_url = "https://".concat(entered_url); - } - boolean danger_on = danger_checkbox.isChecked(); - if(validURL(entered_url)) { - interface_with_ConfigurationWizard.showAndSelectProvider(entered_url, danger_on); - Toast.makeText(getActivity().getApplicationContext(), R.string.valid_url_entered, Toast.LENGTH_LONG).show(); - } else { - url_input_field.setText(""); - danger_checkbox.setChecked(false); - Toast.makeText(getActivity().getApplicationContext(), R.string.not_valid_url_entered, Toast.LENGTH_LONG).show();; - } + saveProvider(); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @@ -111,6 +91,25 @@ public class NewProviderDialog extends DialogFragment { return builder.create(); } + private void saveProvider() { + String entered_url = url_input_field.getText().toString().trim(); + if(!entered_url.startsWith("https://")) { + if (entered_url.startsWith("http://")){ + entered_url = entered_url.substring("http://".length()); + } + entered_url = "https://".concat(entered_url); + } + boolean danger_on = danger_checkbox.isChecked(); + if(validURL(entered_url)) { + interface_with_ConfigurationWizard.showAndSelectProvider(entered_url, danger_on); + Toast.makeText(getActivity().getApplicationContext(), R.string.valid_url_entered, Toast.LENGTH_LONG).show(); + } else { + url_input_field.setText(""); + danger_checkbox.setChecked(false); + Toast.makeText(getActivity().getApplicationContext(), R.string.not_valid_url_entered, Toast.LENGTH_LONG).show();; + } + } + /** * Checks if the entered url is valid or not. * @param entered_url diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java index 19a9a089..c63e2edb 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java @@ -90,7 +90,7 @@ public class ProviderListAdapter extends RendererAdapter { @Override public void add(Provider item) { super.add(item); - if(getCollection().size() < hidden.length) { + if(getCollection().size() > hidden.length) { boolean[] new_hidden = new boolean[hidden.length + 1]; System.arraycopy(hidden, 0, new_hidden, 0, hidden.length); new_hidden[hidden.length] = false; diff --git a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java index c90ce481..73de29bc 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -331,8 +331,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download */ public void addAndSelectNewProvider() { FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(NewProviderDialog.TAG); - DialogFragment newFragment = NewProviderDialog.newInstance(); - newFragment.show(fragment_transaction, NewProviderDialog.TAG); + new NewProviderDialog().show(fragment_transaction, NewProviderDialog.TAG); } /** @@ -341,7 +340,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download public void addAndSelectNewProvider(String main_url) { FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(NewProviderDialog.TAG); - DialogFragment newFragment = NewProviderDialog.newInstance(); + DialogFragment newFragment = new NewProviderDialog(); Bundle data = new Bundle(); data.putString(Provider.MAIN_URL, main_url); newFragment.setArguments(data); diff --git a/app/src/release/java/se/leap/bitmaskclient/NewProviderDialog.java b/app/src/release/java/se/leap/bitmaskclient/NewProviderDialog.java index 7ed1940e..f6709c22 100644 --- a/app/src/release/java/se/leap/bitmaskclient/NewProviderDialog.java +++ b/app/src/release/java/se/leap/bitmaskclient/NewProviderDialog.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 LEAP Encryption Access Project and contributers - * + * * This program 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 @@ -14,16 +14,16 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package se.leap.bitmaskclient; +package se.leap.bitmaskclient; +import butterknife.ButterKnife; +import butterknife.InjectView; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; -import se.leap.bitmaskclient.R; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.content.DialogInterface; -import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -33,33 +33,28 @@ import android.widget.Toast; /** * Implements the new custom provider dialog. - * + * * @author parmegv * */ public class NewProviderDialog extends DialogFragment { final public static String TAG = "newProviderDialog"; - - public interface NewProviderDialogInterface { + + @InjectView(R.id.new_provider_url) + EditText url_input_field; + + public interface NewProviderDialogInterface { public void showAndSelectProvider(String url_provider); } - NewProviderDialogInterface interface_with_ConfigurationWizard; + NewProviderDialogInterface interface_with_ConfigurationWizard; - /** - * @return a new instance of this DialogFragment. - */ - public static DialogFragment newInstance() { - NewProviderDialog dialog_fragment = new NewProviderDialog(); - return dialog_fragment; - } - @Override public void onAttach(Activity activity) { super.onAttach(activity); try { - interface_with_ConfigurationWizard = (NewProviderDialogInterface) activity; + interface_with_ConfigurationWizard = (NewProviderDialogInterface) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); @@ -67,51 +62,57 @@ public class NewProviderDialog extends DialogFragment { } @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - LayoutInflater inflater = getActivity().getLayoutInflater(); - View new_provider_dialog_view = inflater.inflate(R.layout.new_provider_dialog, null); - final EditText url_input_field = (EditText)new_provider_dialog_view.findViewById(R.id.new_provider_url); - if(getArguments() != null && getArguments().containsKey(Provider.MAIN_URL)) { - url_input_field.setText(getArguments().getString(Provider.MAIN_URL)); - } - - builder.setView(new_provider_dialog_view) - .setMessage(R.string.introduce_new_provider) - .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - String entered_url = url_input_field.getText().toString().trim(); - if(!entered_url.startsWith("https://")) { - if (entered_url.startsWith("http://")){ - entered_url = entered_url.substring("http://".length()); - } - entered_url = "https://".concat(entered_url); - } - if(validURL(entered_url)) { - interface_with_ConfigurationWizard.showAndSelectProvider(entered_url); - Toast.makeText(getActivity().getApplicationContext(), R.string.valid_url_entered, Toast.LENGTH_LONG).show(); - } else { - url_input_field.setText(""); - Toast.makeText(getActivity().getApplicationContext(), R.string.not_valid_url_entered, Toast.LENGTH_LONG).show();; - } - } - }) - .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - } - }); - // Create the AlertDialog object and return it - return builder.create(); - } + public Dialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + LayoutInflater inflater = getActivity().getLayoutInflater(); + View view = inflater.inflate(R.layout.new_provider_dialog, null); + ButterKnife.inject(this, view); + Bundle arguments = getArguments(); + if(arguments != null) { + url_input_field.setText(arguments.getString(Provider.MAIN_URL, "")); + } + + builder.setView(view) + .setMessage(R.string.introduce_new_provider) + .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + saveProvider(); + } + }) + .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + } + }); + // Create the AlertDialog object and return it + return builder.create(); + } + + private void saveProvider() { + String entered_url = url_input_field.getText().toString().trim(); + if(!entered_url.startsWith("https://")) { + if (entered_url.startsWith("http://")){ + entered_url = entered_url.substring("http://".length()); + } + entered_url = "https://".concat(entered_url); + } + + if(validURL(entered_url)) { + interface_with_ConfigurationWizard.showAndSelectProvider(entered_url); + Toast.makeText(getActivity().getApplicationContext(), R.string.valid_url_entered, Toast.LENGTH_LONG).show(); + } else { + url_input_field.setText(""); + Toast.makeText(getActivity().getApplicationContext(), R.string.not_valid_url_entered, Toast.LENGTH_LONG).show();; + } + } /** * Checks if the entered url is valid or not. * @param entered_url * @return true if it's not empty nor contains only the protocol. */ - boolean validURL(String entered_url) { - //return !entered_url.isEmpty() && entered_url.matches("http[s]?://.+") && !entered_url.replaceFirst("http[s]?://", "").isEmpty(); - return android.util.Patterns.WEB_URL.matcher(entered_url).matches(); - } + boolean validURL(String entered_url) { + //return !entered_url.isEmpty() && entered_url.matches("http[s]?://.+") && !entered_url.replaceFirst("http[s]?://", "").isEmpty(); + return android.util.Patterns.WEB_URL.matcher(entered_url).matches(); + } } -- cgit v1.2.3 From 56a998deef38a635e2a2e61dd1f516b78d24c9c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 9 Dec 2014 21:30:25 +0100 Subject: SessionDialog restores eip switch if necessary. --- app/src/main/java/se/leap/bitmaskclient/Dashboard.java | 2 +- app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java | 4 +++- app/src/main/java/se/leap/bitmaskclient/SessionDialog.java | 6 +++--- 3 files changed, 7 insertions(+), 5 deletions(-) (limited to 'app') diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index c7691ed4..3ecf5e52 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -54,7 +54,7 @@ import se.leap.bitmaskclient.eip.EipStatus; * @author Sean Leonard * @author parmegv */ -public class Dashboard extends Activity implements SessionDialog.LogInDialogInterface, ProviderAPIResultReceiver.Receiver { +public class Dashboard extends Activity implements SessionDialog.SessionDialogInterface, ProviderAPIResultReceiver.Receiver { protected static final int CONFIGURE_LEAP = 0; protected static final int SWITCH_PROVIDER = 1; diff --git a/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java index a8ffda72..507cfe74 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java @@ -129,7 +129,9 @@ public class EipServiceFragment extends Fragment implements Observer { else if(canLogInToStartEIP()) { Log.d(TAG, "Can Log In to start EIP"); Dashboard dashboard = (Dashboard) parent_activity; - dashboard.logInDialog(Bundle.EMPTY); + Bundle bundle = new Bundle(); + bundle.putBoolean(IS_PENDING, true); + dashboard.logInDialog(bundle); } } diff --git a/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java b/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java index 9676faa8..60382cf0 100644 --- a/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java +++ b/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java @@ -121,13 +121,13 @@ public class SessionDialog extends DialogFragment{ * @author parmegv * */ - public interface LogInDialogInterface { + public interface SessionDialogInterface { public void logIn(String username, String password); public void signUp(String username, String password); public void cancelLoginOrSignup(); } - LogInDialogInterface interface_with_Dashboard; + SessionDialogInterface interface_with_Dashboard; /** * @return a new instance of this DialogFragment. @@ -143,7 +143,7 @@ public class SessionDialog extends DialogFragment{ public void onAttach(Activity activity) { super.onAttach(activity); try { - interface_with_Dashboard = (LogInDialogInterface) activity; + interface_with_Dashboard = (SessionDialogInterface) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement LogInDialogListener"); -- cgit v1.2.3 From 60adae615458f42fe7a36fe9c4faf90e3e463f95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 9 Dec 2014 21:46:26 +0100 Subject: Restore eip service UI onResume. --- app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java | 1 + 1 file changed, 1 insertion(+) (limited to 'app') diff --git a/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java index 507cfe74..904aa31d 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java @@ -91,6 +91,7 @@ public class EipServiceFragment extends Fragment implements Observer { public void onResume() { super.onResume(); eipCommand(Constants.ACTION_CHECK_CERT_VALIDITY); + handleNewState(eip_status); } @Override -- cgit v1.2.3