diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2020-02-23 14:54:48 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2020-02-23 15:07:39 +0530 |
commit | 33e69db436c01c252781a01d169857a69867c6c3 (patch) | |
tree | 5d67e0b50a6bc2c6917c739e9444b9c7dd9f3e78 /app/src/main/java/com/wireguard/android/fragment | |
parent | 13cbec28d65791d87e10e846580f3dcbe00a0965 (diff) |
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 <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java/com/wireguard/android/fragment')
-rw-r--r-- | app/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.java | 19 |
1 files changed, 10 insertions, 9 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 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<String, ApplicationData> appData = new ObservableKeyedArrayList<>(); - @Nullable private List<String> currentlyExcludedApps; + private List<String> currentlyExcludedApps = Collections.emptyList(); public static <T extends Fragment & AppExclusionListener> AppListDialogFragment newInstance(final ArrayList<String> excludedApps, final T target) { @@ -60,14 +60,14 @@ public class AppListDialogFragment extends DialogFragment { launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER); final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(launcherIntent, 0); - final List<ApplicationData> appData = new ArrayList<>(); + final List<ApplicationData> 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<String> 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<String> excludedApps = new ArrayList<>(); for (final ApplicationData data : appData) { if (data.isExcludedFromTunnel()) { |