2 * Copyright 2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Cell <https://www.stoutner.com/privacy-cell>.
6 * Privacy Cell is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Cell is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Cell. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacycell.helpers
22 import android.content.Context
23 import android.telephony.TelephonyDisplayInfo
24 import android.telephony.TelephonyManager
26 import com.stoutner.privacycell.R
28 class ProtocolHelper {
30 // Define the public static companion object constants.
31 const val UNPOPULATED = -1
33 const val INSECURE = 1
34 const val ANTIQUATED = 2
37 fun checkNetwork(networkType: Int, consider3gAntiquated: Boolean): Int {
38 if ((networkType == TelephonyManager.NETWORK_TYPE_NR) ||
39 (networkType == TelephonyManager.NETWORK_TYPE_IWLAN) ||
40 (networkType == TelephonyManager.NETWORK_TYPE_UNKNOWN)) {
41 // The network is secure.
43 } else if (networkType == TelephonyManager.NETWORK_TYPE_LTE) {
44 // The network is insecure.
46 } else if (!consider3gAntiquated &&
47 ((networkType == TelephonyManager.NETWORK_TYPE_1xRTT) ||
48 (networkType == TelephonyManager.NETWORK_TYPE_EVDO_0) ||
49 (networkType == TelephonyManager.NETWORK_TYPE_EVDO_A) ||
50 (networkType == TelephonyManager.NETWORK_TYPE_EVDO_B) ||
51 (networkType == TelephonyManager.NETWORK_TYPE_EHRPD) ||
52 (networkType == TelephonyManager.NETWORK_TYPE_UMTS) ||
53 (networkType == TelephonyManager.NETWORK_TYPE_TD_SCDMA) ||
54 (networkType == TelephonyManager.NETWORK_TYPE_HSDPA) ||
55 (networkType == TelephonyManager.NETWORK_TYPE_HSUPA) ||
56 (networkType == TelephonyManager.NETWORK_TYPE_HSPA) ||
57 (networkType == TelephonyManager.NETWORK_TYPE_HSPAP))) {
58 // The network is insecure.
61 // Either 3G networks are considered antiquated, or this is a 2G network.
62 // TelephonyManager.NETWORK_TYPE_GPRS
63 // TelephonyManager.NETWORK_TYPE_EDGE
64 // TelephonyManager.NETWORK_TYPE_CDMA
65 // TelephonyManager.NETWORK_TYPE_IDEN
66 // TelephonyManager.NETWORK_TYPE_GSM
71 fun checkAdditionalNetworkInfo(additionalNetworkInfoType: Int): Int {
72 return if ((additionalNetworkInfoType == TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE) ||
73 (additionalNetworkInfoType == TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED)) {
74 // The additional network info is secure.
77 // The additional network info is insecure.
78 // TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA
79 // TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO
80 // TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA
81 // TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE -- Can be removed once the minimum API >= 31.
86 fun getNetworkTypeStringArray(networkType: Int, context: Context) : Array<String> {
87 // Return the string array that corresponds to the network type.
88 return when(networkType) {
89 TelephonyManager.NETWORK_TYPE_UNKNOWN -> arrayOf(context.getString(R.string.unknown), "")
90 TelephonyManager.NETWORK_TYPE_GPRS -> arrayOf(context.getString(R.string.gprs), context.getString(R.string.gprs_detail))
91 TelephonyManager.NETWORK_TYPE_EDGE -> arrayOf(context.getString(R.string.edge), context.getString(R.string.edge_detail))
92 TelephonyManager.NETWORK_TYPE_UMTS -> arrayOf(context.getString(R.string.umts), context.getString(R.string.umts_detail))
93 TelephonyManager.NETWORK_TYPE_CDMA -> arrayOf(context.getString(R.string.cdma), context.getString(R.string.cdma_detail))
94 TelephonyManager.NETWORK_TYPE_EVDO_0 -> arrayOf(context.getString(R.string.evdo_0), context.getString(R.string.evdo_0_detail))
95 TelephonyManager.NETWORK_TYPE_EVDO_A -> arrayOf(context.getString(R.string.evdo_a), context.getString(R.string.evdo_a_detail))
96 TelephonyManager.NETWORK_TYPE_1xRTT -> arrayOf(context.getString(R.string.rtt), context.getString(R.string.rtt_detail))
97 TelephonyManager.NETWORK_TYPE_HSDPA -> arrayOf(context.getString(R.string.hsdpa), context.getString(R.string.hsdpa_detail))
98 TelephonyManager.NETWORK_TYPE_HSUPA -> arrayOf(context.getString(R.string.hsupa), context.getString(R.string.hsupa_detail))
99 TelephonyManager.NETWORK_TYPE_HSPA -> arrayOf(context.getString(R.string.hspa), context.getString(R.string.hspa_detail))
100 TelephonyManager.NETWORK_TYPE_IDEN -> arrayOf(context.getString(R.string.iden), context.getString(R.string.iden_detail))
101 TelephonyManager.NETWORK_TYPE_EVDO_B -> arrayOf(context.getString(R.string.evdo_b), context.getString(R.string.evdo_b_detail))
102 TelephonyManager.NETWORK_TYPE_LTE -> arrayOf(context.getString(R.string.lte), context.getString(R.string.lte_detail))
103 TelephonyManager.NETWORK_TYPE_EHRPD -> arrayOf(context.getString(R.string.ehrpd), context.getString(R.string.ehrpd_detail))
104 TelephonyManager.NETWORK_TYPE_HSPAP -> arrayOf(context.getString(R.string.hspap), context.getString(R.string.hspap_detail))
105 TelephonyManager.NETWORK_TYPE_GSM -> arrayOf(context.getString(R.string.gsm), context.getString(R.string.gsm_detail))
106 TelephonyManager.NETWORK_TYPE_TD_SCDMA -> arrayOf(context.getString(R.string.td_scdma), context.getString(R.string.td_scdma_detail))
107 TelephonyManager.NETWORK_TYPE_IWLAN -> arrayOf(context.getString(R.string.iwlan), context.getString(R.string.iwlan_detail))
108 TelephonyManager.NETWORK_TYPE_NR -> arrayOf(context.getString(R.string.nr), context.getString(R.string.nr_detail))
109 else -> arrayOf(context.getString(R.string.error), "")
113 fun getAdditionalNetworkInfoStringArray(overrideNetworkType: Int, context: Context) : Array<String> {
114 // Return the string array that corresponds to the override network type.
115 @Suppress("DEPRECATION") // OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE can be removed once the minimum API >= 31.
116 return when(overrideNetworkType) {
117 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE -> arrayOf(context.getString(R.string.none), "")
118 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA -> arrayOf(context.getString(R.string.lte_ca), context.getString(R.string.lte_ca_detail))
119 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO -> arrayOf(context.getString(R.string.lte_advanced_pro), context.getString(R.string.lte_advanced_pro_detail))
120 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA -> arrayOf(context.getString(R.string.nr_nsa), context.getString(R.string.nr_nsa_detail))
121 // OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE can be removed once the minimum API >= 31.
122 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE -> arrayOf(context.getString(R.string.nr_nsa_mmwave), context.getString(R.string.nr_nsa_mmwave_detail))
123 TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_ADVANCED -> arrayOf(context.getString(R.string.nr_advanced), context.getString(R.string.nr_advanced_detail))
124 else -> arrayOf(context.getString(R.string.error), "")