summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorParménides GV <parmegv@sdf.org>2014-12-09 11:20:31 +0100
committerParménides GV <parmegv@sdf.org>2014-12-09 11:20:31 +0100
commit84f71ca604abb2b4aeb410dcec2c3cc5dff9bc39 (patch)
tree53d1b8ada01b3de40de043a7781c5801da05bd5b
parent202036aa6befcb1c74a76803f22e020404d4ba34 (diff)
Switching provider doesn't refresh provider list.
It shows just one provider, and it's not the one that gets configured if selected.
-rw-r--r--app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java100
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/Dashboard.java32
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/Provider.java16
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java79
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/ProviderListFragment.java26
5 files changed, 107 insertions, 146 deletions
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<Provider> 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<Renderer<Provider>> prototypes = new ArrayList<Renderer<Provider>>();
prototypes.add(new ProviderRenderer(this));
ProviderRendererBuilder providerRendererBuilder = new ProviderRendererBuilder(prototypes);
- adapter = new RendererAdapter<Provider>(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<T> extends ArrayAdapter<T> {
+public class ProviderListAdapter extends RendererAdapter<Provider> {
private static boolean[] hidden = null;
public void hide(int position) {
@@ -23,10 +29,23 @@ public class ProviderListAdapter<T> extends ArrayAdapter<T> {
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<T> extends ArrayAdapter<T> {
return (hidden.length - getHiddenCount());
}
- public ProviderListAdapter(Context mContext, int layout, List<T> objects) {
- super(mContext, layout, objects);
+ public ProviderListAdapter(LayoutInflater layoutInflater, RendererBuilder rendererBuilder,
+ AdapteeCollection<Provider> 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<T> objects, boolean show_all_providers) {
- super(mContext, layout, objects);
+ public ProviderListAdapter(LayoutInflater layoutInflater, RendererBuilder rendererBuilder,
+ AdapteeCollection<Provider> 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<T> extends ArrayAdapter<T> {
}
@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<Provider> 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<ProviderItem> 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<ProviderListContent.ProviderItem>(
- getActivity(),
- R.layout.provider_list_item,
- ProviderListContent.ITEMS, getArguments().getBoolean(SHOW_ALL_PROVIDERS));
- else
- content_adapter = new ProviderListAdapter<ProviderListContent.ProviderItem>(
- 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.