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:
parent
1ca258877c
commit
205576d9e0
1 changed files with 26 additions and 15 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue