From d80daee37e0e56573802a793b3dde6d1dc89fa0e Mon Sep 17 00:00:00 2001 From: orionlee Date: Thu, 12 Sep 2019 09:12:28 -0700 Subject: [PATCH 1/7] #3387 new permission for WiFi filter UI be compatible with Android 10+ --- core/src/main/AndroidManifest.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/AndroidManifest.xml b/core/src/main/AndroidManifest.xml index 7d84bdddb..c57696072 100644 --- a/core/src/main/AndroidManifest.xml +++ b/core/src/main/AndroidManifest.xml @@ -9,11 +9,11 @@ - - + Date: Fri, 13 Sep 2019 12:00:22 -0700 Subject: [PATCH 2/7] #3387 ask for location permission for Wi-Fi filter UI on Android 10+. --- .../AutoDownloadPreferencesFragment.java | 87 ++++++++++++++++++- core/src/main/res/values/strings.xml | 2 + 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java index a04615a00..49a58a621 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java @@ -1,25 +1,31 @@ package de.danoeh.antennapod.fragment.preferences; +import android.Manifest; import android.app.Activity; import android.content.Context; +import android.content.pm.PackageManager; import android.content.res.Resources; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; +import android.os.Build; import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.content.ContextCompat; import android.support.v7.preference.CheckBoxPreference; import android.support.v7.preference.ListPreference; import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceFragmentCompat; import android.support.v7.preference.PreferenceScreen; import android.util.Log; -import de.danoeh.antennapod.R; -import de.danoeh.antennapod.core.preferences.UserPreferences; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.core.preferences.UserPreferences; + public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat { private static final String TAG = "AutoDnldPrefFragment"; private CheckBoxPreference[] selectedNetworks; @@ -175,10 +181,87 @@ public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat { } private void setSelectedNetworksEnabled(boolean b) { + if (permissionHelper.showPermissionRequestPromptOnAndroid10IfNeeded(b)) { + return; + } + if (selectedNetworks != null) { for (Preference p : selectedNetworks) { p.setEnabled(b); } } } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + permissionHelper.doOnRequestPermissionsResult(requestCode, permissions, grantResults); + } + + private class PermissionHelper { + private static final String requestedPermission = Manifest.permission.ACCESS_COARSE_LOCATION; + private static final int permissionRequestCode = 1; + + private static final String PREF_KEY_PERMISSION_REQUEST_PROMPT = "prefAutoDownloadWifiFilterAndroid10PermissionPrompt"; + + private Preference prefPermissionRequestPromptOnAndroid10 = null; + + void doOnRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + if (requestCode != permissionRequestCode) { + return; + } + if (permissions.length > 0 && permissions[0].equals(requestedPermission) && + grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + buildAutodownloadSelectedNetworksPreference(); + } + } + + boolean showPermissionRequestPromptOnAndroid10IfNeeded(boolean wifiFilterEnabled) { + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) { + return false; + } + + // Cases Android 10(Q) or later + + final PreferenceScreen prefScreen = getPreferenceScreen(); + + if (prefPermissionRequestPromptOnAndroid10 != null) { + prefScreen.removePreference(prefPermissionRequestPromptOnAndroid10); + prefPermissionRequestPromptOnAndroid10 = null; + } + + + if (hasLocationPermission()) { + return false; + } + + // Case location permission not yet granted, permission-specific UI is needed + + if (!wifiFilterEnabled) { // don't show the UI when WiFi filter disabled + return true; + } + + Preference pref = new Preference(requireActivity()); + pref.setKey(PREF_KEY_PERMISSION_REQUEST_PROMPT); + pref.setTitle(R.string.autodl_wifi_filter_permission_title); + pref.setSummary(R.string.autodl_wifi_filter_permission_message); + pref.setOnPreferenceClickListener(preference -> { + requestLocationPermission(); + return true; + }); + pref.setPersistent(false); + getPreferenceScreen().addPreference(pref); + prefPermissionRequestPromptOnAndroid10 = pref; + + return true; + } + + private boolean hasLocationPermission() { + return ContextCompat.checkSelfPermission(requireContext(), requestedPermission) == PackageManager.PERMISSION_GRANTED; + } + + private void requestLocationPermission() { + requestPermissions(new String[]{requestedPermission}, permissionRequestCode); + } + } + private final PermissionHelper permissionHelper = new PermissionHelper(); } diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index c7bd2d2a2..adf938d75 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -394,6 +394,8 @@ Configure the automatic download of episodes. Enable Wi-Fi filter Allow automatic download only for selected Wi-Fi networks. + Permission required + Location permission is required for Wi-Fi filter. Tap to grant the permission. Download when not charging Allow automatic download when the battery is not charging Parallel Downloads From d559a8b9067d4efefd3ffcd45439909b1ac616ca Mon Sep 17 00:00:00 2001 From: orionlee Date: Fri, 13 Sep 2019 12:53:59 -0700 Subject: [PATCH 3/7] #3387 use ACCESS_FINE_LOCATION per Google feedback https://issuetracker.google.com/issues/140696830 --- .../fragment/preferences/AutoDownloadPreferencesFragment.java | 2 +- core/src/main/AndroidManifest.xml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java index 49a58a621..4994e6594 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java @@ -198,7 +198,7 @@ public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat { } private class PermissionHelper { - private static final String requestedPermission = Manifest.permission.ACCESS_COARSE_LOCATION; + private static final String requestedPermission = Manifest.permission.ACCESS_FINE_LOCATION; private static final int permissionRequestCode = 1; private static final String PREF_KEY_PERMISSION_REQUEST_PROMPT = "prefAutoDownloadWifiFilterAndroid10PermissionPrompt"; diff --git a/core/src/main/AndroidManifest.xml b/core/src/main/AndroidManifest.xml index c57696072..7d84bdddb 100644 --- a/core/src/main/AndroidManifest.xml +++ b/core/src/main/AndroidManifest.xml @@ -9,11 +9,11 @@ - - + Date: Sat, 14 Sep 2019 13:14:57 -0700 Subject: [PATCH 4/7] #3387 permission prompt - add warning icon to make it stand out. --- .../preferences/AutoDownloadPreferencesFragment.java | 1 + core/src/main/res/drawable/ic_warning_red.xml | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 core/src/main/res/drawable/ic_warning_red.xml diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java index 4994e6594..7d1d98cfd 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java @@ -244,6 +244,7 @@ public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat { pref.setKey(PREF_KEY_PERMISSION_REQUEST_PROMPT); pref.setTitle(R.string.autodl_wifi_filter_permission_title); pref.setSummary(R.string.autodl_wifi_filter_permission_message); + pref.setIcon(R.drawable.ic_warning_red); pref.setOnPreferenceClickListener(preference -> { requestLocationPermission(); return true; diff --git a/core/src/main/res/drawable/ic_warning_red.xml b/core/src/main/res/drawable/ic_warning_red.xml new file mode 100644 index 000000000..475a41bbb --- /dev/null +++ b/core/src/main/res/drawable/ic_warning_red.xml @@ -0,0 +1,5 @@ + + + From 292aaa610ec261d959340cedc3eea66cb37fedd4 Mon Sep 17 00:00:00 2001 From: orionlee Date: Sat, 14 Sep 2019 13:15:50 -0700 Subject: [PATCH 5/7] #3387 permission prompt - add comments for case Wi-Fi filter not enabled. --- .../preferences/AutoDownloadPreferencesFragment.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java index 7d1d98cfd..859016bbc 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java @@ -236,7 +236,10 @@ public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat { // Case location permission not yet granted, permission-specific UI is needed - if (!wifiFilterEnabled) { // don't show the UI when WiFi filter disabled + if (!wifiFilterEnabled) { + // Don't show the UI when WiFi filter disabled. + // it still return true, so that the caller knows + // it does not have required permission, and will not invoke codes that require so. return true; } From 342fe60279d1079e562ed48dd3f3ad1438b7f1bc Mon Sep 17 00:00:00 2001 From: orionlee Date: Fri, 27 Sep 2019 10:46:23 -0700 Subject: [PATCH 6/7] Auto Wifi filter permission UI - flattened the logic back to the fragment per review. --- .../AutoDownloadPreferencesFragment.java | 113 ++++++++---------- 1 file changed, 53 insertions(+), 60 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java index 859016bbc..f2ff71478 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java @@ -28,8 +28,15 @@ import de.danoeh.antennapod.core.preferences.UserPreferences; public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat { private static final String TAG = "AutoDnldPrefFragment"; + + private static final String requestedPermission = Manifest.permission.ACCESS_FINE_LOCATION; + private static final int permissionRequestCode = 1; + private static final String PREF_KEY_PERMISSION_REQUEST_PROMPT = "prefAutoDownloadWifiFilterAndroid10PermissionPrompt"; + private CheckBoxPreference[] selectedNetworks; + private Preference prefPermissionRequestPromptOnAndroid10 = null; + @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { addPreferencesFromResource(R.xml.preferences_autodownload); @@ -181,7 +188,7 @@ public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat { } private void setSelectedNetworksEnabled(boolean b) { - if (permissionHelper.showPermissionRequestPromptOnAndroid10IfNeeded(b)) { + if (showPermissionRequestPromptOnAndroid10IfNeeded(b)) { return; } @@ -194,78 +201,64 @@ public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - permissionHelper.doOnRequestPermissionsResult(requestCode, permissions, grantResults); + if (requestCode != permissionRequestCode) { + return; + } + if (permissions.length > 0 && permissions[0].equals(requestedPermission) && + grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + buildAutodownloadSelectedNetworksPreference(); + } } - private class PermissionHelper { - private static final String requestedPermission = Manifest.permission.ACCESS_FINE_LOCATION; - private static final int permissionRequestCode = 1; - - private static final String PREF_KEY_PERMISSION_REQUEST_PROMPT = "prefAutoDownloadWifiFilterAndroid10PermissionPrompt"; - - private Preference prefPermissionRequestPromptOnAndroid10 = null; - - void doOnRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - if (requestCode != permissionRequestCode) { - return; - } - if (permissions.length > 0 && permissions[0].equals(requestedPermission) && - grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - buildAutodownloadSelectedNetworksPreference(); - } + private boolean showPermissionRequestPromptOnAndroid10IfNeeded(boolean wifiFilterEnabled) { + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) { + return false; } - boolean showPermissionRequestPromptOnAndroid10IfNeeded(boolean wifiFilterEnabled) { - if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) { - return false; - } + // Cases Android 10(Q) or later - // Cases Android 10(Q) or later + final PreferenceScreen prefScreen = getPreferenceScreen(); - final PreferenceScreen prefScreen = getPreferenceScreen(); - - if (prefPermissionRequestPromptOnAndroid10 != null) { - prefScreen.removePreference(prefPermissionRequestPromptOnAndroid10); - prefPermissionRequestPromptOnAndroid10 = null; - } + if (prefPermissionRequestPromptOnAndroid10 != null) { + prefScreen.removePreference(prefPermissionRequestPromptOnAndroid10); + prefPermissionRequestPromptOnAndroid10 = null; + } - if (hasLocationPermission()) { - return false; - } + if (hasLocationPermission()) { + return false; + } - // Case location permission not yet granted, permission-specific UI is needed - - if (!wifiFilterEnabled) { - // Don't show the UI when WiFi filter disabled. - // it still return true, so that the caller knows - // it does not have required permission, and will not invoke codes that require so. - return true; - } - - Preference pref = new Preference(requireActivity()); - pref.setKey(PREF_KEY_PERMISSION_REQUEST_PROMPT); - pref.setTitle(R.string.autodl_wifi_filter_permission_title); - pref.setSummary(R.string.autodl_wifi_filter_permission_message); - pref.setIcon(R.drawable.ic_warning_red); - pref.setOnPreferenceClickListener(preference -> { - requestLocationPermission(); - return true; - }); - pref.setPersistent(false); - getPreferenceScreen().addPreference(pref); - prefPermissionRequestPromptOnAndroid10 = pref; + // Case location permission not yet granted, permission-specific UI is needed + if (!wifiFilterEnabled) { + // Don't show the UI when WiFi filter disabled. + // it still return true, so that the caller knows + // it does not have required permission, and will not invoke codes that require so. return true; } - private boolean hasLocationPermission() { - return ContextCompat.checkSelfPermission(requireContext(), requestedPermission) == PackageManager.PERMISSION_GRANTED; - } + Preference pref = new Preference(requireActivity()); + pref.setKey(PREF_KEY_PERMISSION_REQUEST_PROMPT); + pref.setTitle(R.string.autodl_wifi_filter_permission_title); + pref.setSummary(R.string.autodl_wifi_filter_permission_message); + pref.setIcon(R.drawable.ic_warning_red); + pref.setOnPreferenceClickListener(preference -> { + requestLocationPermission(); + return true; + }); + pref.setPersistent(false); + getPreferenceScreen().addPreference(pref); + prefPermissionRequestPromptOnAndroid10 = pref; - private void requestLocationPermission() { - requestPermissions(new String[]{requestedPermission}, permissionRequestCode); - } + return true; + } + + private boolean hasLocationPermission() { + return ContextCompat.checkSelfPermission(requireContext(), requestedPermission) == PackageManager.PERMISSION_GRANTED; + } + + private void requestLocationPermission() { + requestPermissions(new String[]{requestedPermission}, permissionRequestCode); } - private final PermissionHelper permissionHelper = new PermissionHelper(); } From d6e2803bebca2b42305039a0c265b7e10d870eda Mon Sep 17 00:00:00 2001 From: orionlee Date: Sat, 28 Sep 2019 13:04:39 -0700 Subject: [PATCH 7/7] Wifi Filter UI - code style tweaks, mainly inlining. --- .../AutoDownloadPreferencesFragment.java | 31 ++++++------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java index f2ff71478..d0c209326 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/AutoDownloadPreferencesFragment.java @@ -29,9 +29,8 @@ import de.danoeh.antennapod.core.preferences.UserPreferences; public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat { private static final String TAG = "AutoDnldPrefFragment"; - private static final String requestedPermission = Manifest.permission.ACCESS_FINE_LOCATION; - private static final int permissionRequestCode = 1; - private static final String PREF_KEY_PERMISSION_REQUEST_PROMPT = "prefAutoDownloadWifiFilterAndroid10PermissionPrompt"; + private static final int LOCATION_PERMISSION_REQUEST_CODE = 1; + private static final String PREF_KEY_LOCATION_PERMISSION_REQUEST_PROMPT = "prefAutoDownloadWifiFilterAndroid10PermissionPrompt"; private CheckBoxPreference[] selectedNetworks; @@ -201,10 +200,10 @@ public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - if (requestCode != permissionRequestCode) { + if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) { 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) { buildAutodownloadSelectedNetworksPreference(); } @@ -216,21 +215,17 @@ public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat { } // Cases Android 10(Q) or later - - final PreferenceScreen prefScreen = getPreferenceScreen(); - if (prefPermissionRequestPromptOnAndroid10 != null) { - prefScreen.removePreference(prefPermissionRequestPromptOnAndroid10); + getPreferenceScreen().removePreference(prefPermissionRequestPromptOnAndroid10); prefPermissionRequestPromptOnAndroid10 = null; } - - if (hasLocationPermission()) { + if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) + == PackageManager.PERMISSION_GRANTED) { return false; } // Case location permission not yet granted, permission-specific UI is needed - if (!wifiFilterEnabled) { // Don't show the UI when WiFi filter disabled. // it still return true, so that the caller knows @@ -239,26 +234,18 @@ public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat { } 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.setSummary(R.string.autodl_wifi_filter_permission_message); pref.setIcon(R.drawable.ic_warning_red); pref.setOnPreferenceClickListener(preference -> { - requestLocationPermission(); + requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE); return true; }); pref.setPersistent(false); getPreferenceScreen().addPreference(pref); prefPermissionRequestPromptOnAndroid10 = pref; - return true; } - private boolean hasLocationPermission() { - return ContextCompat.checkSelfPermission(requireContext(), requestedPermission) == PackageManager.PERMISSION_GRANTED; - } - - private void requestLocationPermission() { - requestPermissions(new String[]{requestedPermission}, permissionRequestCode); - } }