summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android
diff options
context:
space:
mode:
authorEric Kuck <eric@bluelinelabs.com>2018-07-04 16:47:55 -0500
committerJason A. Donenfeld <Jason@zx2c4.com>2018-07-06 04:14:19 +0200
commit500a705531a3210aa98b17b77085cf1ea86c274d (patch)
tree8eee63dc202e5e1414354f5827873a1e9c642be9 /app/src/main/java/com/wireguard/android
parent5729947d6ca3aa99d3811ca0e6624ad0ce0f969d (diff)
AppListDialogFragment: add implementation for excluding applications
Signed-off-by: Eric Kuck <eric@bluelinelabs.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/android')
-rw-r--r--app/src/main/java/com/wireguard/android/backend/GoBackend.java4
-rw-r--r--app/src/main/java/com/wireguard/android/databinding/BindingAdapters.java34
-rw-r--r--app/src/main/java/com/wireguard/android/databinding/ObservableKeyedRecyclerViewAdapter.java135
-rw-r--r--app/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.java132
-rw-r--r--app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java28
-rw-r--r--app/src/main/java/com/wireguard/android/model/ApplicationData.java54
6 files changed, 385 insertions, 2 deletions
diff --git a/app/src/main/java/com/wireguard/android/backend/GoBackend.java b/app/src/main/java/com/wireguard/android/backend/GoBackend.java
index fbe9d8ea..466e580d 100644
--- a/app/src/main/java/com/wireguard/android/backend/GoBackend.java
+++ b/app/src/main/java/com/wireguard/android/backend/GoBackend.java
@@ -171,6 +171,9 @@ public final class GoBackend implements Backend {
configureIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
builder.setConfigureIntent(PendingIntent.getActivity(context, 0, configureIntent, 0));
+ for (final String excludedApplication : config.getInterface().getExcludedApplications())
+ builder.addDisallowedApplication(excludedApplication);
+
for (final InetNetwork addr : config.getInterface().getAddresses())
builder.addAddress(addr.getAddress(), addr.getMask());
@@ -250,5 +253,6 @@ public final class GoBackend implements Backend {
}
return super.onStartCommand(intent, flags, startId);
}
+
}
}
diff --git a/app/src/main/java/com/wireguard/android/databinding/BindingAdapters.java b/app/src/main/java/com/wireguard/android/databinding/BindingAdapters.java
index ba6845ba..9a4a9a27 100644
--- a/app/src/main/java/com/wireguard/android/databinding/BindingAdapters.java
+++ b/app/src/main/java/com/wireguard/android/databinding/BindingAdapters.java
@@ -9,16 +9,18 @@ package com.wireguard.android.databinding;
import android.databinding.BindingAdapter;
import android.databinding.ObservableList;
import android.databinding.adapters.ListenerUtil;
+import android.support.v7.widget.LinearLayoutManager;
+import android.support.v7.widget.RecyclerView;
import android.text.InputFilter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.wireguard.android.R;
-import com.wireguard.util.Keyed;
import com.wireguard.android.util.ObservableKeyedList;
import com.wireguard.android.widget.ToggleSwitch;
import com.wireguard.android.widget.ToggleSwitch.OnBeforeCheckedChangeListener;
+import com.wireguard.util.Keyed;
/**
* Static methods for use by generated code in the Android data binding library.
@@ -91,9 +93,39 @@ public final class BindingAdapters {
adapter.setList(newList);
}
+ @BindingAdapter({"items", "layout"})
+ public static <K, E extends Keyed<? extends K>>
+ void setItems(final RecyclerView view,
+ final ObservableKeyedList<K, E> oldList, final int oldLayoutId,
+ final ObservableKeyedList<K, E> newList, final int newLayoutId) {
+ if (view.getLayoutManager() == null)
+ view.setLayoutManager(new LinearLayoutManager(view.getContext(), RecyclerView.VERTICAL, false));
+
+ if (oldList == newList && oldLayoutId == newLayoutId)
+ return;
+ // The ListAdapter interface is not generic, so this cannot be checked.
+ @SuppressWarnings("unchecked") ObservableKeyedRecyclerViewAdapter<K, E> adapter =
+ (ObservableKeyedRecyclerViewAdapter<K, E>) view.getAdapter();
+ // If the layout changes, any existing adapter must be replaced.
+ if (adapter != null && oldList != null && oldLayoutId != newLayoutId) {
+ adapter.setList(null);
+ adapter = null;
+ }
+ // Avoid setting an adapter when there is no new list or layout.
+ if (newList == null || newLayoutId == 0)
+ return;
+ if (adapter == null) {
+ adapter = new ObservableKeyedRecyclerViewAdapter<>(view.getContext(), newLayoutId, newList);
+ view.setAdapter(adapter);
+ }
+ // Either the list changed, or this is an entirely new listener because the layout changed.
+ adapter.setList(newList);
+ }
+
@BindingAdapter("onBeforeCheckedChanged")
public static void setOnBeforeCheckedChanged(final ToggleSwitch view,
final OnBeforeCheckedChangeListener listener) {
view.setOnBeforeCheckedChangeListener(listener);
}
+
}
diff --git a/app/src/main/java/com/wireguard/android/databinding/ObservableKeyedRecyclerViewAdapter.java b/app/src/main/java/com/wireguard/android/databinding/ObservableKeyedRecyclerViewAdapter.java
new file mode 100644
index 00000000..8917441f
--- /dev/null
+++ b/app/src/main/java/com/wireguard/android/databinding/ObservableKeyedRecyclerViewAdapter.java
@@ -0,0 +1,135 @@
+package com.wireguard.android.databinding;
+
+import android.content.Context;
+import android.databinding.DataBindingUtil;
+import android.databinding.ObservableList;
+import android.databinding.ViewDataBinding;
+import android.support.annotation.NonNull;
+import android.support.v7.widget.RecyclerView;
+import android.support.v7.widget.RecyclerView.Adapter;
+import android.view.LayoutInflater;
+import android.view.ViewGroup;
+
+import com.wireguard.android.BR;
+import com.wireguard.android.util.ObservableKeyedList;
+import com.wireguard.util.Keyed;
+
+import java.lang.ref.WeakReference;
+
+/**
+ * A generic {@code RecyclerView.Adapter} backed by a {@code ObservableKeyedList}.
+ */
+
+class ObservableKeyedRecyclerViewAdapter<K, E extends Keyed<? extends K>> extends Adapter<ObservableKeyedRecyclerViewAdapter.ViewHolder> {
+
+ private final OnListChangedCallback<E> callback = new OnListChangedCallback<>(this);
+ private final int layoutId;
+ private final LayoutInflater layoutInflater;
+ private ObservableKeyedList<K, E> list;
+
+ ObservableKeyedRecyclerViewAdapter(final Context context, final int layoutId,
+ final ObservableKeyedList<K, E> list) {
+ this.layoutId = layoutId;
+ layoutInflater = LayoutInflater.from(context);
+ setList(list);
+ }
+
+ @Override
+ public int getItemCount() {
+ return list != null ? list.size() : 0;
+ }
+
+ private E getItem(final int position) {
+ if (list == null || position < 0 || position >= list.size())
+ return null;
+ return list.get(position);
+ }
+
+ @Override
+ public long getItemId(final int position) {
+ final K key = getKey(position);
+ return key != null ? key.hashCode() : -1;
+ }
+
+ private K getKey(final int position) {
+ final E item = getItem(position);
+ return item != null ? item.getKey() : null;
+ }
+
+ @NonNull @Override
+ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
+ return new ViewHolder(DataBindingUtil.inflate(layoutInflater, layoutId, parent, false));
+ }
+
+ @Override
+ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
+ holder.binding.setVariable(BR.collection, list);
+ holder.binding.setVariable(BR.key, getKey(position));
+ holder.binding.setVariable(BR.item, getItem(position));
+ holder.binding.executePendingBindings();
+ }
+
+ void setList(final ObservableKeyedList<K, E> newList) {
+ if (list != null)
+ list.removeOnListChangedCallback(callback);
+ list = newList;
+ if (list != null) {
+ list.addOnListChangedCallback(callback);
+ }
+ notifyDataSetChanged();
+ }
+
+ private static final class OnListChangedCallback<E extends Keyed<?>>
+ extends ObservableList.OnListChangedCallback<ObservableList<E>> {
+
+ private final WeakReference<ObservableKeyedRecyclerViewAdapter<?, E>> weakAdapter;
+
+ private OnListChangedCallback(final ObservableKeyedRecyclerViewAdapter<?, E> adapter) {
+ weakAdapter = new WeakReference<>(adapter);
+ }
+
+ @Override
+ public void onChanged(final ObservableList<E> sender) {
+ final ObservableKeyedRecyclerViewAdapter adapter = weakAdapter.get();
+ if (adapter != null)
+ adapter.notifyDataSetChanged();
+ else
+ sender.removeOnListChangedCallback(this);
+ }
+
+ @Override
+ public void onItemRangeChanged(final ObservableList<E> sender, final int positionStart,
+ final int itemCount) {
+ onChanged(sender);
+ }
+
+ @Override
+ public void onItemRangeInserted(final ObservableList<E> sender, final int positionStart,
+ final int itemCount) {
+ onChanged(sender);
+ }
+
+ @Override
+ public void onItemRangeMoved(final ObservableList<E> sender, final int fromPosition,
+ final int toPosition, final int itemCount) {
+ onChanged(sender);
+ }
+
+ @Override
+ public void onItemRangeRemoved(final ObservableList<E> sender, final int positionStart,
+ final int itemCount) {
+ onChanged(sender);
+ }
+ }
+
+ public static class ViewHolder extends RecyclerView.ViewHolder {
+ final ViewDataBinding binding;
+
+ public ViewHolder(ViewDataBinding binding) {
+ super(binding.getRoot());
+
+ this.binding = binding;
+ }
+ }
+
+}
diff --git a/app/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.java b/app/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.java
new file mode 100644
index 00000000..7b238dd5
--- /dev/null
+++ b/app/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.java
@@ -0,0 +1,132 @@
+package com.wireguard.android.fragment;
+
+import android.app.Activity;
+import android.app.Dialog;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.os.Bundle;
+import android.support.annotation.Nullable;
+import android.support.v4.app.DialogFragment;
+import android.support.v4.app.Fragment;
+import android.support.v7.app.AlertDialog;
+import android.widget.Toast;
+
+import com.wireguard.android.Application;
+import com.wireguard.android.R;
+import com.wireguard.android.activity.BaseActivity;
+import com.wireguard.android.databinding.AppListDialogFragmentBinding;
+import com.wireguard.android.model.ApplicationData;
+import com.wireguard.android.model.Tunnel;
+import com.wireguard.android.util.ExceptionLoggers;
+import com.wireguard.android.util.ObservableKeyedArrayList;
+import com.wireguard.android.util.ObservableKeyedList;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+public class AppListDialogFragment extends DialogFragment {
+
+ private static final String KEY_EXCLUDED_APPS = "excludedApps";
+
+ private List<String> currentlyExcludedApps;
+ private Tunnel tunnel;
+ private final ObservableKeyedList<String, ApplicationData> appData = new ObservableKeyedArrayList<>();
+
+ public static <T extends Fragment & AppExclusionListener> AppListDialogFragment newInstance(String[] excludedApps, T target) {
+ Bundle extras = new Bundle();
+ extras.putStringArray(KEY_EXCLUDED_APPS, excludedApps);
+ AppListDialogFragment fragment = new AppListDialogFragment();
+ fragment.setTargetFragment(target, 0);
+ fragment.setArguments(extras);
+ return fragment;
+ }
+
+ @Override
+ public void onCreate(@Nullable Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ currentlyExcludedApps = Arrays.asList(getArguments().getStringArray(KEY_EXCLUDED_APPS));
+ }
+
+ @Override
+ public void onAttach(final Context context) {
+ super.onAttach(context);
+ if (context instanceof BaseActivity) {
+ tunnel = ((BaseActivity) context).getSelectedTunnel();
+ }
+ }
+
+ @Override
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
+ AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
+ alertDialogBuilder.setTitle(R.string.excluded_applications);
+
+ AppListDialogFragmentBinding binding = AppListDialogFragmentBinding.inflate(getActivity().getLayoutInflater(), null, false);
+ binding.executePendingBindings();
+ alertDialogBuilder.setView(binding.getRoot());
+
+ alertDialogBuilder.setPositiveButton(R.string.set_exclusions, (dialog, which) -> setExclusionsAndDismiss());
+ alertDialogBuilder.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
+
+ binding.setFragment(this);
+ binding.setAppData(appData);
+
+ loadData();
+
+ return alertDialogBuilder.create();
+ }
+
+ private void loadData() {
+ final Activity activity = getActivity();
+ if (activity == null) {
+ return;
+ }
+
+ final PackageManager pm = activity.getPackageManager();
+ Application.getAsyncWorker().supplyAsync(() -> {
+ Intent launcherIntent = new Intent(Intent.ACTION_MAIN, null);
+ launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
+ List<ResolveInfo> resolveInfos = pm.queryIntentActivities(launcherIntent, 0);
+
+ List<ApplicationData> appData = new ArrayList<>();
+ for (ResolveInfo resolveInfo : resolveInfos) {
+ String packageName = resolveInfo.activityInfo.packageName;
+ appData.add(new ApplicationData(resolveInfo.loadIcon(pm), resolveInfo.loadLabel(pm).toString(), packageName, currentlyExcludedApps.contains(packageName)));
+ }
+
+ Collections.sort(appData, (lhs, rhs) -> lhs.getName().toLowerCase().compareTo(rhs.getName().toLowerCase()));
+ return appData;
+ }).whenComplete(((data, throwable) -> {
+ if (data != null) {
+ appData.clear();
+ appData.addAll(data);
+ } else {
+ final String error = throwable != null ? ExceptionLoggers.unwrapMessage(throwable) : "Unknown";
+ final String message = activity.getString(R.string.error_fetching_apps, error);
+ Toast.makeText(activity, message, Toast.LENGTH_LONG).show();
+ dismissAllowingStateLoss();
+ }
+ }));
+ }
+
+ void setExclusionsAndDismiss() {
+ final List<String> excludedApps = new ArrayList<>();
+ for (ApplicationData data : appData) {
+ if (data.isExcludedFromTunnel()) {
+ excludedApps.add(data.getPackageName());
+ }
+ }
+
+ ((AppExclusionListener) getTargetFragment()).onExcludedAppsSelected(excludedApps);
+ dismiss();
+ }
+
+ public interface AppExclusionListener {
+ void onExcludedAppsSelected(List<String> excludedApps);
+ }
+
+}
diff --git a/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java b/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java
index ea03077c..c2778c36 100644
--- a/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java
+++ b/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java
@@ -11,6 +11,7 @@ import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
+import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
@@ -24,16 +25,20 @@ import android.widget.Toast;
import com.wireguard.android.Application;
import com.wireguard.android.R;
import com.wireguard.android.databinding.TunnelEditorFragmentBinding;
+import com.wireguard.android.fragment.AppListDialogFragment.AppExclusionListener;
import com.wireguard.android.model.Tunnel;
import com.wireguard.android.model.TunnelManager;
import com.wireguard.android.util.ExceptionLoggers;
+import com.wireguard.config.Attribute;
import com.wireguard.config.Config;
+import java.util.List;
+
/**
* Fragment for editing a WireGuard configuration.
*/
-public class TunnelEditorFragment extends BaseFragment {
+public class TunnelEditorFragment extends BaseFragment implements AppExclusionListener {
private static final String KEY_LOCAL_CONFIG = "local_config";
private static final String KEY_ORIGINAL_NAME = "original_name";
private static final String TAG = "WireGuard/" + TunnelEditorFragment.class.getSimpleName();
@@ -202,6 +207,8 @@ public class TunnelEditorFragment extends BaseFragment {
@Override
public void onViewStateRestored(final Bundle savedInstanceState) {
+ binding.setFragment(this);
+
if (savedInstanceState == null) {
onSelectedTunnelChanged(null, getSelectedTunnel());
} else {
@@ -216,4 +223,23 @@ public class TunnelEditorFragment extends BaseFragment {
super.onViewStateRestored(savedInstanceState);
}
+
+ public void onRequestSetExcludedApplications(@SuppressWarnings("unused") final View view) {
+ FragmentManager fragmentManager = getFragmentManager();
+ if (fragmentManager != null) {
+ String[] excludedApps = excludedApplications();
+ AppListDialogFragment fragment = AppListDialogFragment.newInstance(excludedApps, this);
+ fragment.show(getFragmentManager(), null);
+ }
+ }
+
+ @Override
+ public void onExcludedAppsSelected(List<String> excludedApps) {
+ binding.getConfig().getInterfaceSection().setExcludedApplications(Attribute.iterableToString(excludedApps));
+ }
+
+ public String[] excludedApplications() {
+ return Attribute.stringToList(binding.getConfig().getInterfaceSection().getExcludedApplications());
+ }
+
}
diff --git a/app/src/main/java/com/wireguard/android/model/ApplicationData.java b/app/src/main/java/com/wireguard/android/model/ApplicationData.java
new file mode 100644
index 00000000..77e8da33
--- /dev/null
+++ b/app/src/main/java/com/wireguard/android/model/ApplicationData.java
@@ -0,0 +1,54 @@
+package com.wireguard.android.model;
+
+import android.databinding.BaseObservable;
+import android.databinding.Bindable;
+import android.graphics.drawable.Drawable;
+import android.support.annotation.NonNull;
+
+import com.wireguard.android.BR;
+import com.wireguard.util.Keyed;
+
+public class ApplicationData extends BaseObservable implements Keyed<String> {
+
+ @NonNull private final Drawable icon;
+ @NonNull private final String name;
+ @NonNull private final String packageName;
+ private boolean excludedFromTunnel;
+
+ public ApplicationData(@NonNull Drawable icon, @NonNull String name, @NonNull String packageName, boolean excludedFromTunnel) {
+ this.icon = icon;
+ this.name = name;
+ this.packageName = packageName;
+ this.excludedFromTunnel = excludedFromTunnel;
+ }
+
+ @NonNull
+ public Drawable getIcon() {
+ return icon;
+ }
+
+ @NonNull
+ public String getName() {
+ return name;
+ }
+
+ @NonNull
+ public String getPackageName() {
+ return packageName;
+ }
+
+ @Bindable
+ public boolean isExcludedFromTunnel() {
+ return excludedFromTunnel;
+ }
+
+ public void setExcludedFromTunnel(boolean excludedFromTunnel) {
+ this.excludedFromTunnel = excludedFromTunnel;
+ notifyPropertyChanged(BR.excludedFromTunnel);
+ }
+
+ @Override
+ public String getKey() {
+ return name;
+ }
+}