Merge pull request #3416 from orionlee/bugfix_autodownload_wifi_filter_on_android10_3387

Make Automatic Download WiFi filter UI work on Android 10+
This commit is contained in:
H. Lehmann 2019-09-29 11:16:10 +02:00 committed by GitHub
commit 373f31c53e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 2 deletions

View File

@ -1,29 +1,41 @@
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 static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private static final String PREF_KEY_LOCATION_PERMISSION_REQUEST_PROMPT = "prefAutoDownloadWifiFilterAndroid10PermissionPrompt";
private CheckBoxPreference[] selectedNetworks;
private Preference prefPermissionRequestPromptOnAndroid10 = null;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preferences_autodownload);
@ -175,10 +187,65 @@ public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat {
}
private void setSelectedNetworksEnabled(boolean b) {
if (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) {
if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
return;
}
if (permissions.length > 0 && permissions[0].equals(Manifest.permission.ACCESS_FINE_LOCATION) &&
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;
}
// Cases Android 10(Q) or later
if (prefPermissionRequestPromptOnAndroid10 != null) {
getPreferenceScreen().removePreference(prefPermissionRequestPromptOnAndroid10);
prefPermissionRequestPromptOnAndroid10 = null;
}
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
// 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_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 -> {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
return true;
});
pref.setPersistent(false);
getPreferenceScreen().addPreference(pref);
prefPermissionRequestPromptOnAndroid10 = pref;
return true;
}
}

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FF0000"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M1,21h22L12,2 1,21zM13,18h-2v-2h2v2zM13,14h-2v-4h2v4z"/>
</vector>

View File

@ -394,6 +394,8 @@
<string name="pref_automatic_download_sum">Configure the automatic download of episodes.</string>
<string name="pref_autodl_wifi_filter_title">Enable Wi-Fi filter</string>
<string name="pref_autodl_wifi_filter_sum">Allow automatic download only for selected Wi-Fi networks.</string>
<string name="autodl_wifi_filter_permission_title">Permission required</string>
<string name="autodl_wifi_filter_permission_message">Location permission is required for Wi-Fi filter. Tap to grant the permission.</string>
<string name="pref_automatic_download_on_battery_title">Download when not charging</string>
<string name="pref_automatic_download_on_battery_sum">Allow automatic download when the battery is not charging</string>
<string name="pref_parallel_downloads_title">Parallel Downloads</string>