1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-26 06:11:19 +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.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Toast;
import com.nutomic.syncthingandroid.R;
@ -40,6 +41,8 @@ import java.util.TreeSet;
*/
public class WifiSsidPreference extends MultiSelectListPreference {
private static final String TAG = "WifiSsidPreference";
public WifiSsidPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDefaultValue(new TreeSet<String>());
@ -132,22 +135,30 @@ public class WifiSsidPreference extends MultiSelectListPreference {
private WifiConfiguration[] loadConfiguredNetworksSorted() {
WifiManager wifiManager = (WifiManager)
getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null) {
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
// if WiFi is turned off, getConfiguredNetworks returns null on many devices
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;
}
if (wifiManager == null) {
// WiFi is turned off or device doesn't have WiFi.
return null;
}
// 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;
}
}