From 7e59bdf8afe4a432ed21a92f3e5ee8aec8a44a38 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 23 Feb 2020 14:54:48 +0530 Subject: AppListDialogFragment: Code cleanup - Using the require_() methods provides helpful error messages when things are null compared to the get_() methods which throw NPEs. - Ensure currentlyExcludedApps is empty but never null - Rename inner variable to silence name shadowing lint - Make setExclusionsAndDismiss private Signed-off-by: Harsh Shandilya --- .../android/fragment/AppListDialogFragment.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'app/src/main/java') 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 c513d47f..fc01e86f 100644 --- a/app/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.java @@ -36,7 +36,7 @@ public class AppListDialogFragment extends DialogFragment { private static final String KEY_EXCLUDED_APPS = "excludedApps"; private final ObservableKeyedList appData = new ObservableKeyedArrayList<>(); - @Nullable private List currentlyExcludedApps; + private List currentlyExcludedApps = Collections.emptyList(); public static AppListDialogFragment newInstance(final ArrayList excludedApps, final T target) { @@ -60,14 +60,14 @@ public class AppListDialogFragment extends DialogFragment { launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER); final List resolveInfos = pm.queryIntentActivities(launcherIntent, 0); - final List appData = new ArrayList<>(); + final List applicationData = 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))); + applicationData.add(new ApplicationData(resolveInfo.loadIcon(pm), resolveInfo.loadLabel(pm).toString(), packageName, currentlyExcludedApps.contains(packageName))); } - Collections.sort(appData, Comparators.comparing(ApplicationData::getName, String.CASE_INSENSITIVE_ORDER)); - return appData; + Collections.sort(applicationData, Comparators.comparing(ApplicationData::getName, String.CASE_INSENSITIVE_ORDER)); + return applicationData; }).whenComplete(((data, throwable) -> { if (data != null) { appData.clear(); @@ -84,15 +84,16 @@ public class AppListDialogFragment extends DialogFragment { @Override public void onCreate(@Nullable final Bundle savedInstanceState) { super.onCreate(savedInstanceState); - currentlyExcludedApps = getArguments().getStringArrayList(KEY_EXCLUDED_APPS); + final List excludedApps = requireArguments().getStringArrayList(KEY_EXCLUDED_APPS); + currentlyExcludedApps = (excludedApps != null) ? excludedApps : Collections.emptyList(); } @Override public Dialog onCreateDialog(@Nullable final Bundle savedInstanceState) { - final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity()); + final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(requireActivity()); alertDialogBuilder.setTitle(R.string.excluded_applications); - final AppListDialogFragmentBinding binding = AppListDialogFragmentBinding.inflate(getActivity().getLayoutInflater(), null, false); + final AppListDialogFragmentBinding binding = AppListDialogFragmentBinding.inflate(requireActivity().getLayoutInflater(), null, false); binding.executePendingBindings(); alertDialogBuilder.setView(binding.getRoot()); @@ -114,7 +115,7 @@ public class AppListDialogFragment extends DialogFragment { return dialog; } - void setExclusionsAndDismiss() { + private void setExclusionsAndDismiss() { final List excludedApps = new ArrayList<>(); for (final ApplicationData data : appData) { if (data.isExcludedFromTunnel()) { -- cgit v1.2.3