TunnelEditor: fix nits

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-07-05 21:34:50 +02:00
parent 500a705531
commit 124f186983
7 changed files with 49 additions and 29 deletions

View File

@ -1,3 +1,9 @@
/*
* Copyright © 2018 Eric Kuck <eric@bluelinelabs.com>.
* Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
* SPDX-License-Identifier:
*/
package com.wireguard.android.databinding; package com.wireguard.android.databinding;
import android.content.Context; import android.content.Context;
@ -57,12 +63,12 @@ class ObservableKeyedRecyclerViewAdapter<K, E extends Keyed<? extends K>> extend
} }
@NonNull @Override @NonNull @Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) {
return new ViewHolder(DataBindingUtil.inflate(layoutInflater, layoutId, parent, false)); return new ViewHolder(DataBindingUtil.inflate(layoutInflater, layoutId, parent, false));
} }
@Override @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
holder.binding.setVariable(BR.collection, list); holder.binding.setVariable(BR.collection, list);
holder.binding.setVariable(BR.key, getKey(position)); holder.binding.setVariable(BR.key, getKey(position));
holder.binding.setVariable(BR.item, getItem(position)); holder.binding.setVariable(BR.item, getItem(position));
@ -125,7 +131,7 @@ class ObservableKeyedRecyclerViewAdapter<K, E extends Keyed<? extends K>> extend
public static class ViewHolder extends RecyclerView.ViewHolder { public static class ViewHolder extends RecyclerView.ViewHolder {
final ViewDataBinding binding; final ViewDataBinding binding;
public ViewHolder(ViewDataBinding binding) { public ViewHolder(final ViewDataBinding binding) {
super(binding.getRoot()); super(binding.getRoot());
this.binding = binding; this.binding = binding;

View File

@ -1,3 +1,9 @@
/*
* Copyright © 2018 Eric Kuck <eric@bluelinelabs.com>.
* Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
* SPDX-License-Identifier:
*/
package com.wireguard.android.fragment; package com.wireguard.android.fragment;
import android.app.Activity; import android.app.Activity;
@ -33,20 +39,19 @@ public class AppListDialogFragment extends DialogFragment {
private static final String KEY_EXCLUDED_APPS = "excludedApps"; private static final String KEY_EXCLUDED_APPS = "excludedApps";
private List<String> currentlyExcludedApps; private List<String> currentlyExcludedApps;
private Tunnel tunnel;
private final ObservableKeyedList<String, ApplicationData> appData = new ObservableKeyedArrayList<>(); private final ObservableKeyedList<String, ApplicationData> appData = new ObservableKeyedArrayList<>();
public static <T extends Fragment & AppExclusionListener> AppListDialogFragment newInstance(String[] excludedApps, T target) { public static <T extends Fragment & AppExclusionListener> AppListDialogFragment newInstance(final String[] excludedApps, final T target) {
Bundle extras = new Bundle(); final Bundle extras = new Bundle();
extras.putStringArray(KEY_EXCLUDED_APPS, excludedApps); extras.putStringArray(KEY_EXCLUDED_APPS, excludedApps);
AppListDialogFragment fragment = new AppListDialogFragment(); final AppListDialogFragment fragment = new AppListDialogFragment();
fragment.setTargetFragment(target, 0); fragment.setTargetFragment(target, 0);
fragment.setArguments(extras); fragment.setArguments(extras);
return fragment; return fragment;
} }
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
currentlyExcludedApps = Arrays.asList(getArguments().getStringArray(KEY_EXCLUDED_APPS)); currentlyExcludedApps = Arrays.asList(getArguments().getStringArray(KEY_EXCLUDED_APPS));
@ -55,14 +60,11 @@ public class AppListDialogFragment extends DialogFragment {
@Override @Override
public void onAttach(final Context context) { public void onAttach(final Context context) {
super.onAttach(context); super.onAttach(context);
if (context instanceof BaseActivity) {
tunnel = ((BaseActivity) context).getSelectedTunnel();
}
} }
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(final Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity()); final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle(R.string.excluded_applications); alertDialogBuilder.setTitle(R.string.excluded_applications);
AppListDialogFragmentBinding binding = AppListDialogFragmentBinding.inflate(getActivity().getLayoutInflater(), null, false); AppListDialogFragmentBinding binding = AppListDialogFragmentBinding.inflate(getActivity().getLayoutInflater(), null, false);
@ -88,11 +90,11 @@ public class AppListDialogFragment extends DialogFragment {
final PackageManager pm = activity.getPackageManager(); final PackageManager pm = activity.getPackageManager();
Application.getAsyncWorker().supplyAsync(() -> { Application.getAsyncWorker().supplyAsync(() -> {
Intent launcherIntent = new Intent(Intent.ACTION_MAIN, null); final Intent launcherIntent = new Intent(Intent.ACTION_MAIN, null);
launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER); launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(launcherIntent, 0); final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(launcherIntent, 0);
List<ApplicationData> appData = new ArrayList<>(); final List<ApplicationData> appData = new ArrayList<>();
for (ResolveInfo resolveInfo : resolveInfos) { for (ResolveInfo resolveInfo : resolveInfos) {
String packageName = resolveInfo.activityInfo.packageName; String packageName = resolveInfo.activityInfo.packageName;
appData.add(new ApplicationData(resolveInfo.loadIcon(pm), resolveInfo.loadLabel(pm).toString(), packageName, currentlyExcludedApps.contains(packageName))); appData.add(new ApplicationData(resolveInfo.loadIcon(pm), resolveInfo.loadLabel(pm).toString(), packageName, currentlyExcludedApps.contains(packageName)));
@ -115,7 +117,7 @@ public class AppListDialogFragment extends DialogFragment {
void setExclusionsAndDismiss() { void setExclusionsAndDismiss() {
final List<String> excludedApps = new ArrayList<>(); final List<String> excludedApps = new ArrayList<>();
for (ApplicationData data : appData) { for (final ApplicationData data : appData) {
if (data.isExcludedFromTunnel()) { if (data.isExcludedFromTunnel()) {
excludedApps.add(data.getPackageName()); excludedApps.add(data.getPackageName());
} }

View File

@ -225,21 +225,17 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi
} }
public void onRequestSetExcludedApplications(@SuppressWarnings("unused") final View view) { public void onRequestSetExcludedApplications(@SuppressWarnings("unused") final View view) {
FragmentManager fragmentManager = getFragmentManager(); final FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) { if (fragmentManager != null) {
String[] excludedApps = excludedApplications(); final String[] excludedApps = Attribute.stringToList(binding.getConfig().getInterfaceSection().getExcludedApplications());
AppListDialogFragment fragment = AppListDialogFragment.newInstance(excludedApps, this); final AppListDialogFragment fragment = AppListDialogFragment.newInstance(excludedApps, this);
fragment.show(getFragmentManager(), null); fragment.show(getFragmentManager(), null);
} }
} }
@Override @Override
public void onExcludedAppsSelected(List<String> excludedApps) { public void onExcludedAppsSelected(final List<String> excludedApps) {
binding.getConfig().getInterfaceSection().setExcludedApplications(Attribute.iterableToString(excludedApps)); binding.getConfig().getInterfaceSection().setExcludedApplications(Attribute.iterableToString(excludedApps));
} }
public String[] excludedApplications() {
return Attribute.stringToList(binding.getConfig().getInterfaceSection().getExcludedApplications());
}
} }

View File

@ -1,3 +1,9 @@
/*
* Copyright © 2018 Eric Kuck <eric@bluelinelabs.com>.
* Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
* SPDX-License-Identifier:
*/
package com.wireguard.android.model; package com.wireguard.android.model;
import android.databinding.BaseObservable; import android.databinding.BaseObservable;
@ -15,7 +21,7 @@ public class ApplicationData extends BaseObservable implements Keyed<String> {
@NonNull private final String packageName; @NonNull private final String packageName;
private boolean excludedFromTunnel; private boolean excludedFromTunnel;
public ApplicationData(@NonNull Drawable icon, @NonNull String name, @NonNull String packageName, boolean excludedFromTunnel) { public ApplicationData(@NonNull final Drawable icon, @NonNull final String name, @NonNull final String packageName, final boolean excludedFromTunnel) {
this.icon = icon; this.icon = icon;
this.name = name; this.name = name;
this.packageName = packageName; this.packageName = packageName;
@ -42,7 +48,7 @@ public class ApplicationData extends BaseObservable implements Keyed<String> {
return excludedFromTunnel; return excludedFromTunnel;
} }
public void setExcludedFromTunnel(boolean excludedFromTunnel) { public void setExcludedFromTunnel(final boolean excludedFromTunnel) {
this.excludedFromTunnel = excludedFromTunnel; this.excludedFromTunnel = excludedFromTunnel;
notifyPropertyChanged(BR.excludedFromTunnel); notifyPropertyChanged(BR.excludedFromTunnel);
} }

View File

@ -291,6 +291,11 @@ public class Interface {
return excludedApplications; return excludedApplications;
} }
@Bindable
public int getExcludedApplicationsCount() {
return Attribute.stringToList(excludedApplications).length;
}
@Bindable @Bindable
public String getListenPort() { public String getListenPort() {
return listenPort; return listenPort;
@ -334,6 +339,7 @@ public class Interface {
public void setExcludedApplications(final String excludedApplications) { public void setExcludedApplications(final String excludedApplications) {
this.excludedApplications = excludedApplications; this.excludedApplications = excludedApplications;
notifyPropertyChanged(BR.excludedApplications); notifyPropertyChanged(BR.excludedApplications);
notifyPropertyChanged(BR.excludedApplicationsCount);
} }
public void setListenPort(final String listenPort) { public void setListenPort(final String listenPort) {

View File

@ -218,12 +218,13 @@
<Button <Button
style="@style/Widget.AppCompat.Button.Borderless.Colored" style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:id="@+id/set_excluded_applications"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="-8dp" android:layout_marginLeft="-8dp"
android:layout_below="@+id/dns_servers_text" android:layout_below="@+id/dns_servers_text"
android:onClick="@{fragment::onRequestSetExcludedApplications}" android:onClick="@{fragment::onRequestSetExcludedApplications}"
android:text="@{fragment.excludedApplications().length == 0 ? @string/set_excluded_applications : String.format(@string/x_excluded_applications, fragment.excludedApplications().length)}" /> android:text="@{@plurals/set_excluded_applications(config.interfaceSection.excludedApplicationsCount, config.interfaceSection.excludedApplicationsCount)}" />
</RelativeLayout> </RelativeLayout>
</android.support.v7.widget.CardView> </android.support.v7.widget.CardView>

View File

@ -69,7 +69,10 @@
<string name="restore_on_boot_summary">Bring up previously-enabled tunnels on boot</string> <string name="restore_on_boot_summary">Bring up previously-enabled tunnels on boot</string>
<string name="restore_on_boot_title">Restore on boot</string> <string name="restore_on_boot_title">Restore on boot</string>
<string name="save">Save</string> <string name="save">Save</string>
<string name="set_excluded_applications">Set Excluded Applications</string> <plurals name="set_excluded_applications">
<item quantity="one">%d Excluded Application</item>
<item quantity="other">%d Excluded Applications</item>
</plurals>
<string name="set_exclusions">Set Exclusions</string> <string name="set_exclusions">Set Exclusions</string>
<string name="settings">Settings</string> <string name="settings">Settings</string>
<string name="toggle_error">Error toggling WireGuard tunnel: %s</string> <string name="toggle_error">Error toggling WireGuard tunnel: %s</string>