Wifi Filter UI - code style tweaks, mainly inlining.

This commit is contained in:
orionlee 2019-09-28 13:04:39 -07:00
parent 342fe60279
commit d6e2803beb
1 changed files with 9 additions and 22 deletions

View File

@ -29,9 +29,8 @@ import de.danoeh.antennapod.core.preferences.UserPreferences;
public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat { public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat {
private static final String TAG = "AutoDnldPrefFragment"; private static final String TAG = "AutoDnldPrefFragment";
private static final String requestedPermission = Manifest.permission.ACCESS_FINE_LOCATION; private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private static final int permissionRequestCode = 1; private static final String PREF_KEY_LOCATION_PERMISSION_REQUEST_PROMPT = "prefAutoDownloadWifiFilterAndroid10PermissionPrompt";
private static final String PREF_KEY_PERMISSION_REQUEST_PROMPT = "prefAutoDownloadWifiFilterAndroid10PermissionPrompt";
private CheckBoxPreference[] selectedNetworks; private CheckBoxPreference[] selectedNetworks;
@ -201,10 +200,10 @@ public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat {
@Override @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode != permissionRequestCode) { if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
return; return;
} }
if (permissions.length > 0 && permissions[0].equals(requestedPermission) && if (permissions.length > 0 && permissions[0].equals(Manifest.permission.ACCESS_FINE_LOCATION) &&
grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
buildAutodownloadSelectedNetworksPreference(); buildAutodownloadSelectedNetworksPreference();
} }
@ -216,21 +215,17 @@ public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat {
} }
// Cases Android 10(Q) or later // Cases Android 10(Q) or later
final PreferenceScreen prefScreen = getPreferenceScreen();
if (prefPermissionRequestPromptOnAndroid10 != null) { if (prefPermissionRequestPromptOnAndroid10 != null) {
prefScreen.removePreference(prefPermissionRequestPromptOnAndroid10); getPreferenceScreen().removePreference(prefPermissionRequestPromptOnAndroid10);
prefPermissionRequestPromptOnAndroid10 = null; prefPermissionRequestPromptOnAndroid10 = null;
} }
if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION)
if (hasLocationPermission()) { == PackageManager.PERMISSION_GRANTED) {
return false; return false;
} }
// Case location permission not yet granted, permission-specific UI is needed // Case location permission not yet granted, permission-specific UI is needed
if (!wifiFilterEnabled) { if (!wifiFilterEnabled) {
// Don't show the UI when WiFi filter disabled. // Don't show the UI when WiFi filter disabled.
// it still return true, so that the caller knows // it still return true, so that the caller knows
@ -239,26 +234,18 @@ public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat {
} }
Preference pref = new Preference(requireActivity()); Preference pref = new Preference(requireActivity());
pref.setKey(PREF_KEY_PERMISSION_REQUEST_PROMPT); pref.setKey(PREF_KEY_LOCATION_PERMISSION_REQUEST_PROMPT);
pref.setTitle(R.string.autodl_wifi_filter_permission_title); pref.setTitle(R.string.autodl_wifi_filter_permission_title);
pref.setSummary(R.string.autodl_wifi_filter_permission_message); pref.setSummary(R.string.autodl_wifi_filter_permission_message);
pref.setIcon(R.drawable.ic_warning_red); pref.setIcon(R.drawable.ic_warning_red);
pref.setOnPreferenceClickListener(preference -> { pref.setOnPreferenceClickListener(preference -> {
requestLocationPermission(); requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
return true; return true;
}); });
pref.setPersistent(false); pref.setPersistent(false);
getPreferenceScreen().addPreference(pref); getPreferenceScreen().addPreference(pref);
prefPermissionRequestPromptOnAndroid10 = pref; prefPermissionRequestPromptOnAndroid10 = pref;
return true; return true;
} }
private boolean hasLocationPermission() {
return ContextCompat.checkSelfPermission(requireContext(), requestedPermission) == PackageManager.PERMISSION_GRANTED;
}
private void requestLocationPermission() {
requestPermissions(new String[]{requestedPermission}, permissionRequestCode);
}
} }