2019-10-27 20:40:17 +01:00
|
|
|
package org.kde.kdeconnect.Helpers;
|
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
import android.Manifest;
|
2019-10-27 20:40:17 +01:00
|
|
|
import android.content.Context;
|
2019-10-27 23:37:40 +01:00
|
|
|
import android.content.pm.PackageManager;
|
2019-10-27 20:40:17 +01:00
|
|
|
import android.net.wifi.SupplicantState;
|
|
|
|
import android.net.wifi.WifiInfo;
|
|
|
|
import android.net.wifi.WifiManager;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
2020-07-07 16:45:02 +05:30
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2019-10-27 23:37:40 +01:00
|
|
|
|
2019-10-27 20:40:17 +01:00
|
|
|
public class TrustedNetworkHelper {
|
|
|
|
|
|
|
|
private static final String KEY_CUSTOM_TRUSTED_NETWORKS = "trusted_network_preference";
|
|
|
|
private static final String KEY_CUSTOM_TRUST_ALL_NETWORKS = "trust_all_network_preference";
|
|
|
|
private static final String NETWORK_SSID_DELIMITER = "#_#";
|
|
|
|
private static final String NOT_AVAILABLE_SSID_RESULT = "<unknown ssid>";
|
|
|
|
|
|
|
|
|
|
|
|
private final Context context;
|
|
|
|
|
|
|
|
public TrustedNetworkHelper(Context context) {
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> read() {
|
|
|
|
String serializeTrustedNetwork = PreferenceManager.getDefaultSharedPreferences(context).getString(
|
|
|
|
KEY_CUSTOM_TRUSTED_NETWORKS, "");
|
|
|
|
if (serializeTrustedNetwork.isEmpty())
|
|
|
|
return Collections.emptyList();
|
|
|
|
return Arrays.asList(serializeTrustedNetwork.split(NETWORK_SSID_DELIMITER));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void update(List<String> trustedNetworks) {
|
|
|
|
String serialized = TextUtils.join(NETWORK_SSID_DELIMITER, trustedNetworks);
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(
|
|
|
|
KEY_CUSTOM_TRUSTED_NETWORKS, serialized).apply();
|
|
|
|
}
|
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
public boolean allAllowed() {
|
|
|
|
if (!hasPermissions()) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-27 20:40:17 +01:00
|
|
|
return PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(context)
|
|
|
|
.getBoolean(KEY_CUSTOM_TRUST_ALL_NETWORKS, Boolean.TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void allAllowed(boolean isChecked) {
|
|
|
|
PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(context)
|
|
|
|
.edit()
|
|
|
|
.putBoolean(KEY_CUSTOM_TRUST_ALL_NETWORKS, isChecked)
|
|
|
|
.apply();
|
|
|
|
}
|
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
public boolean hasPermissions() {
|
2020-04-21 20:47:35 +02:00
|
|
|
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
|
2019-10-27 23:37:40 +01:00
|
|
|
return (result == PackageManager.PERMISSION_GRANTED);
|
|
|
|
}
|
|
|
|
|
2019-10-27 20:40:17 +01:00
|
|
|
public String currentSSID() {
|
2020-07-07 16:45:02 +05:30
|
|
|
WifiManager wifiManager = ContextCompat.getSystemService(context.getApplicationContext(),
|
|
|
|
WifiManager.class);
|
2019-10-27 20:40:17 +01:00
|
|
|
if (wifiManager == null) return "";
|
|
|
|
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
|
|
|
if (wifiInfo.getSupplicantState() != SupplicantState.COMPLETED) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
String ssid = wifiInfo.getSSID();
|
|
|
|
if (ssid.equalsIgnoreCase(NOT_AVAILABLE_SSID_RESULT)){
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return ssid;
|
|
|
|
}
|
|
|
|
|
2020-01-05 17:27:16 +01:00
|
|
|
public static boolean isTrustedNetwork(Context context) {
|
2019-10-27 20:40:17 +01:00
|
|
|
TrustedNetworkHelper trustedNetworkHelper = new TrustedNetworkHelper(context);
|
|
|
|
if (trustedNetworkHelper.allAllowed()){
|
2020-01-05 17:27:16 +01:00
|
|
|
return true;
|
2019-10-27 20:40:17 +01:00
|
|
|
}
|
2020-01-05 17:27:16 +01:00
|
|
|
return trustedNetworkHelper.read().contains(trustedNetworkHelper.currentSSID());
|
2019-10-27 20:40:17 +01:00
|
|
|
}
|
|
|
|
}
|