1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-29 15:51:17 +00:00

WifiSsidPreference: Catch java.lang.SecurityException

See changes in Android Q, https://developer.android.com/reference/android/net/wifi/WifiManager.html#getConfiguredNetworks()
This commit is contained in:
Catfriend1 2019-03-23 11:46:03 +01:00
parent 1ca258877c
commit 205576d9e0

View file

@ -12,6 +12,7 @@ import android.preference.MultiSelectListPreference;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
@ -40,6 +41,8 @@ import java.util.TreeSet;
*/ */
public class WifiSsidPreference extends MultiSelectListPreference { public class WifiSsidPreference extends MultiSelectListPreference {
private static final String TAG = "WifiSsidPreference";
public WifiSsidPreference(Context context, AttributeSet attrs) { public WifiSsidPreference(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
setDefaultValue(new TreeSet<String>()); setDefaultValue(new TreeSet<String>());
@ -132,22 +135,30 @@ public class WifiSsidPreference extends MultiSelectListPreference {
private WifiConfiguration[] loadConfiguredNetworksSorted() { private WifiConfiguration[] loadConfiguredNetworksSorted() {
WifiManager wifiManager = (WifiManager) WifiManager wifiManager = (WifiManager)
getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE); getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null) { if (wifiManager == null) {
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks(); // WiFi is turned off or device doesn't have WiFi.
// if WiFi is turned off, getConfiguredNetworks returns null on many devices return null;
if (configuredNetworks != null) {
WifiConfiguration[] result = configuredNetworks.toArray(new WifiConfiguration[configuredNetworks.size()]);
Arrays.sort(result, (lhs, rhs) -> {
// See #620: There may be null-SSIDs
String l = lhs.SSID != null ? lhs.SSID : "";
String r = rhs.SSID != null ? rhs.SSID : "";
return l.compareToIgnoreCase(r);
});
return result;
}
} }
// WiFi is turned off or device doesn't have WiFi
return null; List<WifiConfiguration> configuredNetworks = null;
try {
configuredNetworks = wifiManager.getConfiguredNetworks();
} catch (SecurityException e) {
// See changes in Android Q, https://developer.android.com/reference/android/net/wifi/WifiManager.html#getConfiguredNetworks()
Log.e(TAG, "loadConfiguredNetworksSorted:", e);
}
if (configuredNetworks == null) {
return null;
}
WifiConfiguration[] result = configuredNetworks.toArray(new WifiConfiguration[configuredNetworks.size()]);
Arrays.sort(result, (lhs, rhs) -> {
// See #620: There may be null-SSIDs
String l = lhs.SSID != null ? lhs.SSID : "";
String r = rhs.SSID != null ? rhs.SSID : "";
return l.compareToIgnoreCase(r);
});
return result;
} }
} }