Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Helpers/TrustedNetworkHelper.java
- This file was added.
1 | package org.kde.kdeconnect.Helpers; | ||||
---|---|---|---|---|---|
2 | | ||||
3 | import java.util.Arrays; | ||||
4 | import java.util.Collections; | ||||
5 | import java.util.List; | ||||
6 | | ||||
7 | import android.content.Context; | ||||
8 | import android.net.wifi.SupplicantState; | ||||
9 | import android.net.wifi.WifiInfo; | ||||
10 | import android.net.wifi.WifiManager; | ||||
11 | import android.preference.PreferenceManager; | ||||
12 | import android.text.TextUtils; | ||||
13 | import android.util.Log; | ||||
14 | | ||||
15 | public class TrustedNetworkHelper { | ||||
16 | | ||||
17 | private static final String KEY_CUSTOM_TRUSTED_NETWORKS = "trusted_network_preference"; | ||||
18 | private static final String KEY_CUSTOM_TRUST_ALL_NETWORKS = "trust_all_network_preference"; | ||||
19 | private static final String NETWORK_SSID_DELIMITER = "#_#"; | ||||
20 | private static final String NOT_AVAILABLE_SSID_RESULT = "<unknown ssid>"; | ||||
21 | | ||||
22 | | ||||
23 | private final Context context; | ||||
24 | | ||||
25 | public TrustedNetworkHelper(Context context) { | ||||
26 | this.context = context; | ||||
27 | } | ||||
28 | | ||||
29 | public List<String> read() { | ||||
30 | String serializeTrustedNetwork = PreferenceManager.getDefaultSharedPreferences(context).getString( | ||||
31 | KEY_CUSTOM_TRUSTED_NETWORKS, ""); | ||||
32 | if (serializeTrustedNetwork.isEmpty()) | ||||
33 | return Collections.emptyList(); | ||||
34 | return Arrays.asList(serializeTrustedNetwork.split(NETWORK_SSID_DELIMITER)); | ||||
35 | } | ||||
36 | | ||||
37 | public void update(List<String> trustedNetworks) { | ||||
38 | String serialized = TextUtils.join(NETWORK_SSID_DELIMITER, trustedNetworks); | ||||
39 | PreferenceManager.getDefaultSharedPreferences(context).edit().putString( | ||||
40 | KEY_CUSTOM_TRUSTED_NETWORKS, serialized).apply(); | ||||
41 | } | ||||
42 | | ||||
43 | public Boolean allAllowed() { | ||||
44 | return PreferenceManager | ||||
45 | .getDefaultSharedPreferences(context) | ||||
46 | .getBoolean(KEY_CUSTOM_TRUST_ALL_NETWORKS, Boolean.TRUE); | ||||
47 | } | ||||
48 | | ||||
49 | public void allAllowed(boolean isChecked) { | ||||
50 | PreferenceManager | ||||
51 | .getDefaultSharedPreferences(context) | ||||
52 | .edit() | ||||
53 | .putBoolean(KEY_CUSTOM_TRUST_ALL_NETWORKS, isChecked) | ||||
54 | .apply(); | ||||
55 | } | ||||
56 | | ||||
57 | public String currentSSID() { | ||||
58 | WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); | ||||
59 | Log.d("Fou", "get"); | ||||
60 | if (wifiManager == null) return ""; | ||||
61 | WifiInfo wifiInfo = wifiManager.getConnectionInfo(); | ||||
62 | if (wifiInfo.getSupplicantState() != SupplicantState.COMPLETED) { | ||||
63 | Log.d("Fou", "fooo"); | ||||
64 | return ""; | ||||
65 | } | ||||
66 | String ssid = wifiInfo.getSSID(); | ||||
67 | if (ssid.equalsIgnoreCase(NOT_AVAILABLE_SSID_RESULT)){ | ||||
68 | Log.d("Fou", "navail"); | ||||
69 | return ""; | ||||
70 | } | ||||
71 | Log.d("Fou", "retn"); | ||||
72 | return ssid; | ||||
73 | } | ||||
74 | | ||||
75 | public static boolean isNotTrustedNetwork(Context context) { | ||||
76 | TrustedNetworkHelper trustedNetworkHelper = new TrustedNetworkHelper(context); | ||||
77 | if (trustedNetworkHelper.allAllowed()){ | ||||
78 | return false; | ||||
79 | } | ||||
80 | return trustedNetworkHelper.read().indexOf(trustedNetworkHelper.currentSSID()) == -1; | ||||
81 | } | ||||
82 | } |