diff options
author | Samuel Holland <samuel@sholland.org> | 2018-09-05 18:33:42 -0500 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2018-11-11 21:50:23 -0600 |
commit | a264f7ab36bf1335999d53cb4a0d753c54b231d0 (patch) | |
tree | 8adb020e43111ff0420569c16c8c529f69b6a0e5 /app/src/main/java/com/wireguard/android/fragment | |
parent | 4e134772d7d2fea02722a87e413947965764fef8 (diff) |
Auto-format the source directories
Blame Jason for writing Java in vim.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/android/fragment')
5 files changed, 184 insertions, 186 deletions
diff --git a/app/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.java b/app/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.java index 8bc44ddf..8bf5a22d 100644 --- a/app/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.java @@ -34,9 +34,8 @@ import java.util.List; public class AppListDialogFragment extends DialogFragment { private static final String KEY_EXCLUDED_APPS = "excludedApps"; - - private List<String> currentlyExcludedApps; private final ObservableKeyedList<String, ApplicationData> appData = new ObservableKeyedArrayList<>(); + private List<String> currentlyExcludedApps; public static <T extends Fragment & AppExclusionListener> AppListDialogFragment newInstance(final String[] excludedApps, final T target) { final Bundle extras = new Bundle(); @@ -47,39 +46,6 @@ public class AppListDialogFragment extends DialogFragment { return fragment; } - @Override - public void onCreate(@Nullable final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - currentlyExcludedApps = Arrays.asList(getArguments().getStringArray(KEY_EXCLUDED_APPS)); - } - - @Override - public Dialog onCreateDialog(final Bundle savedInstanceState) { - final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity()); - alertDialogBuilder.setTitle(R.string.excluded_applications); - - final 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()); - alertDialogBuilder.setNeutralButton(R.string.deselect_all, (dialog, which) -> { }); - - binding.setFragment(this); - binding.setAppData(appData); - - loadData(); - - final AlertDialog dialog = alertDialogBuilder.create(); - dialog.setOnShowListener(d -> dialog.getButton(DialogInterface.BUTTON_NEUTRAL).setOnClickListener(view -> { - for (final ApplicationData app : appData) - app.setExcludedFromTunnel(false); - })); - return dialog; - } - private void loadData() { final Activity activity = getActivity(); if (activity == null) { @@ -113,6 +79,40 @@ public class AppListDialogFragment extends DialogFragment { })); } + @Override + public void onCreate(@Nullable final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + currentlyExcludedApps = Arrays.asList(getArguments().getStringArray(KEY_EXCLUDED_APPS)); + } + + @Override + public Dialog onCreateDialog(final Bundle savedInstanceState) { + final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity()); + alertDialogBuilder.setTitle(R.string.excluded_applications); + + final 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()); + alertDialogBuilder.setNeutralButton(R.string.deselect_all, (dialog, which) -> { + }); + + binding.setFragment(this); + binding.setAppData(appData); + + loadData(); + + final AlertDialog dialog = alertDialogBuilder.create(); + dialog.setOnShowListener(d -> dialog.getButton(DialogInterface.BUTTON_NEUTRAL).setOnClickListener(view -> { + for (final ApplicationData app : appData) + app.setExcludedFromTunnel(false); + })); + return dialog; + } + void setExclusionsAndDismiss() { final List<String> excludedApps = new ArrayList<>(); for (final ApplicationData data : appData) { diff --git a/app/src/main/java/com/wireguard/android/fragment/BaseFragment.java b/app/src/main/java/com/wireguard/android/fragment/BaseFragment.java index ca1b9922..e3003f2e 100644 --- a/app/src/main/java/com/wireguard/android/fragment/BaseFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/BaseFragment.java @@ -33,9 +33,8 @@ import com.wireguard.android.util.ExceptionLoggers; */ public abstract class BaseFragment extends Fragment implements OnSelectedTunnelChangedListener { - private static final String TAG = "WireGuard/" + BaseFragment.class.getSimpleName(); private static final int REQUEST_CODE_VPN_PERMISSION = 23491; - + private static final String TAG = "WireGuard/" + BaseFragment.class.getSimpleName(); @Nullable private BaseActivity activity; @Nullable private Tunnel pendingTunnel; @Nullable private Boolean pendingTunnelUp; @@ -46,6 +45,18 @@ public abstract class BaseFragment extends Fragment implements OnSelectedTunnelC } @Override + public void onActivityResult(final int requestCode, final int resultCode, @Nullable final Intent data) { + super.onActivityResult(requestCode, resultCode, data); + + if (requestCode == REQUEST_CODE_VPN_PERMISSION) { + if (pendingTunnel != null && pendingTunnelUp != null) + setTunnelStateWithPermissionsResult(pendingTunnel, pendingTunnelUp); + pendingTunnel = null; + pendingTunnelUp = null; + } + } + + @Override public void onAttach(final Context context) { super.onAttach(context); if (context instanceof BaseActivity) { @@ -64,18 +75,6 @@ public abstract class BaseFragment extends Fragment implements OnSelectedTunnelC super.onDetach(); } - @Override - public void onActivityResult(final int requestCode, final int resultCode, @Nullable final Intent data) { - super.onActivityResult(requestCode, resultCode, data); - - if (requestCode == REQUEST_CODE_VPN_PERMISSION) { - if (pendingTunnel != null && pendingTunnelUp != null) - setTunnelStateWithPermissionsResult(pendingTunnel, pendingTunnelUp); - pendingTunnel = null; - pendingTunnelUp = null; - } - } - protected void setSelectedTunnel(@Nullable final Tunnel tunnel) { if (activity != null) activity.setSelectedTunnel(tunnel); diff --git a/app/src/main/java/com/wireguard/android/fragment/ConfigNamingDialogFragment.java b/app/src/main/java/com/wireguard/android/fragment/ConfigNamingDialogFragment.java index 3d2c6294..83799818 100644 --- a/app/src/main/java/com/wireguard/android/fragment/ConfigNamingDialogFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/ConfigNamingDialogFragment.java @@ -13,7 +13,6 @@ import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; import android.support.v7.app.AlertDialog; -import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import com.wireguard.android.Application; @@ -27,9 +26,8 @@ import java.util.Objects; public class ConfigNamingDialogFragment extends DialogFragment { private static final String KEY_CONFIG_TEXT = "config_text"; - - @Nullable private Config config; @Nullable private ConfigNamingDialogFragmentBinding binding; + @Nullable private Config config; @Nullable private InputMethodManager imm; public static ConfigNamingDialogFragment newInstance(final String configText) { @@ -40,6 +38,26 @@ public class ConfigNamingDialogFragment extends DialogFragment { return fragment; } + private void createTunnelAndDismiss() { + if (binding != null) { + final String name = binding.tunnelNameText.getText().toString(); + + Application.getTunnelManager().create(name, config).whenComplete((tunnel, throwable) -> { + if (tunnel != null) { + dismiss(); + } else { + binding.tunnelNameTextLayout.setError(throwable.getMessage()); + } + }); + } + } + + @Override + public void dismiss() { + setKeyboardVisible(false); + super.dismiss(); + } + @Override public void onCreate(@Nullable final Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -51,17 +69,6 @@ public class ConfigNamingDialogFragment extends DialogFragment { } } - @Override public void onResume() { - super.onResume(); - - final AlertDialog dialog = (AlertDialog) getDialog(); - if (dialog != null) { - dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(v -> createTunnelAndDismiss()); - - setKeyboardVisible(true); - } - } - @Override public Dialog onCreateDialog(final Bundle savedInstanceState) { final Activity activity = getActivity(); @@ -81,23 +88,14 @@ public class ConfigNamingDialogFragment extends DialogFragment { return alertDialogBuilder.create(); } - @Override - public void dismiss() { - setKeyboardVisible(false); - super.dismiss(); - } + @Override public void onResume() { + super.onResume(); - private void createTunnelAndDismiss() { - if (binding != null) { - final String name = binding.tunnelNameText.getText().toString(); + final AlertDialog dialog = (AlertDialog) getDialog(); + if (dialog != null) { + dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(v -> createTunnelAndDismiss()); - Application.getTunnelManager().create(name, config).whenComplete((tunnel, throwable) -> { - if (tunnel != null) { - dismiss(); - } else { - binding.tunnelNameTextLayout.setError(throwable.getMessage()); - } - }); + setKeyboardVisible(true); } } 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 b51feb37..8f319e1e 100644 --- a/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java @@ -48,8 +48,59 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi 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(); - + private final Collection<Object> breakObjectOrientedLayeringHandlerReceivers = new ArrayList<>(); @Nullable private TunnelEditorFragmentBinding binding; + private final Observable.OnPropertyChangedCallback breakObjectOrientedLayeringHandler = new Observable.OnPropertyChangedCallback() { + @Override + public void onPropertyChanged(final Observable sender, final int propertyId) { + if (binding == null) + return; + final Config.Observable config = binding.getConfig(); + if (config == null) + return; + if (propertyId == BR.config) { + config.addOnPropertyChangedCallback(breakObjectOrientedLayeringHandler); + breakObjectOrientedLayeringHandlerReceivers.add(config); + config.getInterfaceSection().addOnPropertyChangedCallback(breakObjectOrientedLayeringHandler); + breakObjectOrientedLayeringHandlerReceivers.add(config.getInterfaceSection()); + config.getPeers().addOnListChangedCallback(breakObjectListOrientedLayeringHandler); + breakObjectOrientedLayeringHandlerReceivers.add(config.getPeers()); + } else if (propertyId == BR.dnses || propertyId == BR.peers) + ; + else + return; + final int numSiblings = config.getPeers().size() - 1; + for (final Peer.Observable peer : config.getPeers()) { + peer.setInterfaceDNSRoutes(config.getInterfaceSection().getDnses()); + peer.setNumSiblings(numSiblings); + } + } + }; + private final ObservableList.OnListChangedCallback<? extends ObservableList<Peer.Observable>> breakObjectListOrientedLayeringHandler = new ObservableList.OnListChangedCallback<ObservableList<Peer.Observable>>() { + @Override + public void onChanged(final ObservableList<Peer.Observable> sender) { + } + + @Override + public void onItemRangeChanged(final ObservableList<Peer.Observable> sender, final int positionStart, final int itemCount) { + } + + @Override + public void onItemRangeInserted(final ObservableList<Peer.Observable> sender, final int positionStart, final int itemCount) { + if (binding != null) + breakObjectOrientedLayeringHandler.onPropertyChanged(binding.getConfig(), BR.peers); + } + + @Override + public void onItemRangeMoved(final ObservableList<Peer.Observable> sender, final int fromPosition, final int toPosition, final int itemCount) { + } + + @Override + public void onItemRangeRemoved(final ObservableList<Peer.Observable> sender, final int positionStart, final int itemCount) { + if (binding != null) + breakObjectOrientedLayeringHandler.onPropertyChanged(binding.getConfig(), BR.peers); + } + }; @Nullable private Tunnel tunnel; private void onConfigLoaded(final String name, final Config config) { @@ -87,54 +138,6 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi inflater.inflate(R.menu.config_editor, menu); } - private final ObservableList.OnListChangedCallback<? extends ObservableList<Peer.Observable>> breakObjectListOrientedLayeringHandler = new ObservableList.OnListChangedCallback<ObservableList<Peer.Observable>>() { - @Override - public void onChanged(final ObservableList<Peer.Observable> sender) { } - @Override - public void onItemRangeChanged(final ObservableList<Peer.Observable> sender, final int positionStart, final int itemCount) { } - @Override - public void onItemRangeMoved(final ObservableList<Peer.Observable> sender, final int fromPosition, final int toPosition, final int itemCount) { } - - @Override - public void onItemRangeInserted(final ObservableList<Peer.Observable> sender, final int positionStart, final int itemCount) { - if (binding != null) - breakObjectOrientedLayeringHandler.onPropertyChanged(binding.getConfig(), BR.peers); - } - @Override - public void onItemRangeRemoved(final ObservableList<Peer.Observable> sender, final int positionStart, final int itemCount) { - if (binding != null) - breakObjectOrientedLayeringHandler.onPropertyChanged(binding.getConfig(), BR.peers); - } - }; - - private final Collection<Object> breakObjectOrientedLayeringHandlerReceivers = new ArrayList<>(); - private final Observable.OnPropertyChangedCallback breakObjectOrientedLayeringHandler = new Observable.OnPropertyChangedCallback() { - @Override - public void onPropertyChanged(final Observable sender, final int propertyId) { - if (binding == null) - return; - final Config.Observable config = binding.getConfig(); - if (config == null) - return; - if (propertyId == BR.config) { - config.addOnPropertyChangedCallback(breakObjectOrientedLayeringHandler); - breakObjectOrientedLayeringHandlerReceivers.add(config); - config.getInterfaceSection().addOnPropertyChangedCallback(breakObjectOrientedLayeringHandler); - breakObjectOrientedLayeringHandlerReceivers.add(config.getInterfaceSection()); - config.getPeers().addOnListChangedCallback(breakObjectListOrientedLayeringHandler); - breakObjectOrientedLayeringHandlerReceivers.add(config.getPeers()); - } else if (propertyId == BR.dnses || propertyId == BR.peers) - ; - else - return; - final int numSiblings = config.getPeers().size() - 1; - for (final Peer.Observable peer : config.getPeers()) { - peer.setInterfaceDNSRoutes(config.getInterfaceSection().getDnses()); - peer.setNumSiblings(numSiblings); - } - } - }; - @Override public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) { @@ -152,13 +155,19 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi binding = null; for (final Object o : breakObjectOrientedLayeringHandlerReceivers) { if (o instanceof Observable) - ((Observable)o).removeOnPropertyChangedCallback(breakObjectOrientedLayeringHandler); + ((Observable) o).removeOnPropertyChangedCallback(breakObjectOrientedLayeringHandler); else if (o instanceof ObservableList) - ((ObservableList)o).removeOnListChangedCallback(breakObjectListOrientedLayeringHandler); + ((ObservableList) o).removeOnListChangedCallback(breakObjectListOrientedLayeringHandler); } super.onDestroyView(); } + @Override + public void onExcludedAppsSelected(final List<String> excludedApps) { + Objects.requireNonNull(binding, "Tried to set excluded apps while no view was loaded"); + binding.getConfig().getInterfaceSection().setExcludedApplications(Attribute.iterableToString(excludedApps)); + } + private void onFinished() { // Hide the keyboard; it rarely goes away on its own. final Activity activity = getActivity(); @@ -217,6 +226,15 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi } } + public void onRequestSetExcludedApplications(@SuppressWarnings("unused") final View view) { + final FragmentManager fragmentManager = getFragmentManager(); + if (fragmentManager != null && binding != null) { + final String[] excludedApps = Attribute.stringToList(binding.getConfig().getInterfaceSection().getExcludedApplications()); + final AppListDialogFragment fragment = AppListDialogFragment.newInstance(excludedApps, this); + fragment.show(fragmentManager, null); + } + } + @Override public void onSaveInstanceState(final Bundle outState) { outState.putParcelable(KEY_LOCAL_CONFIG, binding.getConfig()); @@ -294,19 +312,4 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi super.onViewStateRestored(savedInstanceState); } - public void onRequestSetExcludedApplications(@SuppressWarnings("unused") final View view) { - final FragmentManager fragmentManager = getFragmentManager(); - if (fragmentManager != null && binding != null) { - final String[] excludedApps = Attribute.stringToList(binding.getConfig().getInterfaceSection().getExcludedApplications()); - final AppListDialogFragment fragment = AppListDialogFragment.newInstance(excludedApps, this); - fragment.show(fragmentManager, null); - } - } - - @Override - public void onExcludedAppsSelected(final List<String> excludedApps) { - Objects.requireNonNull(binding, "Tried to set excluded apps while no view was loaded"); - binding.getConfig().getInterfaceSection().setExcludedApplications(Attribute.iterableToString(excludedApps)); - } - } diff --git a/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java b/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java index 80895041..7509e40c 100644 --- a/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java @@ -38,13 +38,11 @@ import com.wireguard.android.databinding.TunnelListFragmentBinding; import com.wireguard.android.databinding.TunnelListItemBinding; import com.wireguard.android.model.Tunnel; import com.wireguard.android.util.ExceptionLoggers; -import com.wireguard.android.util.ObservableSortedKeyedList; import com.wireguard.android.widget.MultiselectableRelativeLayout; import com.wireguard.android.widget.fab.FloatingActionsMenuRecyclerViewScrollListener; import com.wireguard.config.Config; import java.io.BufferedReader; -import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -186,6 +184,19 @@ public class TunnelListFragment extends BaseFragment { } @Override + public void onActivityCreated(@Nullable final Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + if (savedInstanceState != null) { + final Collection<Integer> checkedItems = savedInstanceState.getIntegerArrayList("CHECKED_ITEMS"); + if (checkedItems != null) { + for (final Integer i : checkedItems) + actionModeListener.setItemChecked(i, true); + } + } + } + + @Override public void onActivityResult(final int requestCode, final int resultCode, @Nullable final Intent data) { switch (requestCode) { case REQUEST_IMPORT: @@ -228,6 +239,14 @@ public class TunnelListFragment extends BaseFragment { super.onDestroyView(); } + @Override + public void onPause() { + if (binding != null) { + binding.createMenu.collapse(); + } + super.onPause(); + } + public void onRequestCreateConfig(@SuppressWarnings("unused") final View view) { startActivity(new Intent(getActivity(), TunnelCreatorActivity.class)); if (binding != null) @@ -255,15 +274,10 @@ public class TunnelListFragment extends BaseFragment { } @Override - public void onPause() { - if (binding != null) { - binding.createMenu.collapse(); - } - super.onPause(); - } + public void onSaveInstanceState(final Bundle outState) { + super.onSaveInstanceState(outState); - private MultiselectableRelativeLayout viewForTunnel(final Tunnel tunnel, final List tunnels) { - return (MultiselectableRelativeLayout)binding.tunnelList.findViewHolderForAdapterPosition(tunnels.indexOf(tunnel)).itemView; + outState.putIntegerArrayList("CHECKED_ITEMS", actionModeListener.getCheckedItems()); } @Override @@ -318,26 +332,6 @@ public class TunnelListFragment extends BaseFragment { } @Override - public void onSaveInstanceState(final Bundle outState) { - super.onSaveInstanceState(outState); - - outState.putIntegerArrayList("CHECKED_ITEMS", actionModeListener.getCheckedItems()); - } - - @Override - public void onActivityCreated(@Nullable final Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - - if (savedInstanceState != null) { - final Collection<Integer> checkedItems = savedInstanceState.getIntegerArrayList("CHECKED_ITEMS"); - if (checkedItems != null) { - for (final Integer i : checkedItems) - actionModeListener.setItemChecked(i, true); - } - } - } - - @Override public void onViewStateRestored(@Nullable final Bundle savedInstanceState) { super.onViewStateRestored(savedInstanceState); @@ -362,17 +356,25 @@ public class TunnelListFragment extends BaseFragment { }); if (actionMode != null) - ((MultiselectableRelativeLayout)binding.getRoot()).setMultiSelected(actionModeListener.checkedItems.contains(position)); + ((MultiselectableRelativeLayout) binding.getRoot()).setMultiSelected(actionModeListener.checkedItems.contains(position)); else - ((MultiselectableRelativeLayout)binding.getRoot()).setSingleSelected(getSelectedTunnel() == tunnel); + ((MultiselectableRelativeLayout) binding.getRoot()).setSingleSelected(getSelectedTunnel() == tunnel); }); } + private MultiselectableRelativeLayout viewForTunnel(final Tunnel tunnel, final List tunnels) { + return (MultiselectableRelativeLayout) binding.tunnelList.findViewHolderForAdapterPosition(tunnels.indexOf(tunnel)).itemView; + } + private final class ActionModeListener implements ActionMode.Callback { private final Collection<Integer> checkedItems = new HashSet<>(); @Nullable private Resources resources; + public ArrayList<Integer> getCheckedItems() { + return new ArrayList<>(checkedItems); + } + @Override public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) { switch (item.getItemId()) { @@ -425,12 +427,10 @@ public class TunnelListFragment extends BaseFragment { binding.tunnelList.getAdapter().notifyDataSetChanged(); } - void toggleItemChecked(final int position) { - setItemChecked(position, !checkedItems.contains(position)); - } - - public ArrayList<Integer> getCheckedItems() { - return new ArrayList<>(checkedItems); + @Override + public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) { + updateTitle(mode); + return false; } void setItemChecked(final int position, final boolean checked) { @@ -454,10 +454,8 @@ public class TunnelListFragment extends BaseFragment { updateTitle(actionMode); } - @Override - public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) { - updateTitle(mode); - return false; + void toggleItemChecked(final int position) { + setItemChecked(position, !checkedItems.contains(position)); } private void updateTitle(@Nullable final ActionMode mode) { |