File indexing completed on 2025-02-02 04:47:49

0001 /*
0002  * SPDX-FileCopyrightText: 2019 Juan David Vega <jdvr.93@hotmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 package org.kde.kdeconnect.Helpers;
0008 
0009 import android.Manifest;
0010 import android.content.Context;
0011 import android.content.pm.PackageManager;
0012 import android.net.wifi.SupplicantState;
0013 import android.net.wifi.WifiInfo;
0014 import android.net.wifi.WifiManager;
0015 import android.preference.PreferenceManager;
0016 import android.text.TextUtils;
0017 import android.util.Log;
0018 
0019 import androidx.core.content.ContextCompat;
0020 
0021 import org.apache.commons.lang3.ArrayUtils;
0022 
0023 import java.util.List;
0024 
0025 public class TrustedNetworkHelper {
0026 
0027     private static final String KEY_CUSTOM_TRUSTED_NETWORKS = "trusted_network_preference";
0028     private static final String KEY_CUSTOM_TRUST_ALL_NETWORKS = "trust_all_network_preference";
0029     private static final String NETWORK_SSID_DELIMITER = "#_#";
0030     private static final String NOT_AVAILABLE_SSID_RESULT = "<unknown ssid>";
0031 
0032 
0033     private final Context context;
0034 
0035     public TrustedNetworkHelper(Context context) {
0036         this.context = context;
0037     }
0038 
0039     public String[] read() {
0040         String serializeTrustedNetwork = PreferenceManager.getDefaultSharedPreferences(context).getString(
0041                 KEY_CUSTOM_TRUSTED_NETWORKS, "");
0042         if (serializeTrustedNetwork.isEmpty())
0043             return ArrayUtils.EMPTY_STRING_ARRAY;
0044         return serializeTrustedNetwork.split(NETWORK_SSID_DELIMITER);
0045     }
0046 
0047     public void update(List<String> trustedNetworks) {
0048         String serialized = TextUtils.join(NETWORK_SSID_DELIMITER, trustedNetworks);
0049         PreferenceManager.getDefaultSharedPreferences(context).edit().putString(
0050                 KEY_CUSTOM_TRUSTED_NETWORKS, serialized).apply();
0051     }
0052 
0053     public boolean allAllowed() {
0054         if (!hasPermissions()) {
0055             return true;
0056         }
0057         return PreferenceManager
0058                 .getDefaultSharedPreferences(context)
0059                 .getBoolean(KEY_CUSTOM_TRUST_ALL_NETWORKS, Boolean.TRUE);
0060     }
0061 
0062     public void allAllowed(boolean isChecked) {
0063         PreferenceManager
0064                 .getDefaultSharedPreferences(context)
0065                 .edit()
0066                 .putBoolean(KEY_CUSTOM_TRUST_ALL_NETWORKS, isChecked)
0067                 .apply();
0068     }
0069 
0070     public boolean hasPermissions() {
0071         int result = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
0072         return (result == PackageManager.PERMISSION_GRANTED);
0073     }
0074 
0075     public String currentSSID() {
0076         WifiManager wifiManager = ContextCompat.getSystemService(context.getApplicationContext(),
0077                 WifiManager.class);
0078         if (wifiManager == null) return "";
0079         WifiInfo wifiInfo = wifiManager.getConnectionInfo();
0080         if (wifiInfo.getSupplicantState() != SupplicantState.COMPLETED) {
0081             return "";
0082         }
0083         String ssid = wifiInfo.getSSID();
0084         if (ssid.equalsIgnoreCase(NOT_AVAILABLE_SSID_RESULT)){
0085             Log.d("TrustedNetworkHelper", "Current SSID is unknown");
0086             return "";
0087         }
0088         return ssid;
0089     }
0090 
0091     public static boolean isTrustedNetwork(Context context) {
0092         TrustedNetworkHelper trustedNetworkHelper = new TrustedNetworkHelper(context);
0093         if (trustedNetworkHelper.allAllowed()){
0094             return true;
0095         }
0096         return ArrayUtils.contains(trustedNetworkHelper.read(), trustedNetworkHelper.currentSSID());
0097     }
0098 }