b45701832350f774b513dca3664f393164567380
[PrivacyCell.git] / app / src / main / java / com / stoutner / privacycell / activities / PrivacyCell.kt
1 /*
2  * Copyright © 2021 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Cell <https://www.stoutner.com/privacy-cell>.
5  *
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.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacycell.activities
21
22 import android.Manifest
23 import android.content.Context
24 import android.content.pm.PackageManager
25 import android.os.Bundle
26 import android.telephony.PhoneStateListener
27 import android.telephony.TelephonyDisplayInfo
28 import android.telephony.TelephonyManager
29 import android.widget.ImageView
30 import android.widget.TextView
31
32 import androidx.appcompat.app.AppCompatActivity
33 import androidx.appcompat.content.res.AppCompatResources
34 import androidx.core.app.ActivityCompat
35
36 import com.stoutner.privacycell.R
37
38 class PrivacyCell : AppCompatActivity() {
39     override fun onCreate(savedInstanceState: Bundle?) {
40         // Run the default commands.
41         super.onCreate(savedInstanceState)
42
43         // Set the content view.
44         setContentView(R.layout.privacy_cell_linearlayout)
45
46         // Get handles for the views.
47         val secureFromStingrayImageView = findViewById<ImageView>(R.id.secure_from_stingray_imageview)
48         val secureFromStingrayTextView = findViewById<TextView>(R.id.secure_from_stingray_textview)
49         val voiceNetworkTextView = findViewById<TextView>(R.id.voice_network)
50         val voiceNetworkDetailsTextView = findViewById<TextView>(R.id.voice_network_details)
51         val dataNetworkTextView = findViewById<TextView>(R.id.data_network)
52         val dataNetworkDetailsTextView = findViewById<TextView>(R.id.data_network_details)
53         val additionalNetworkInfoTextView = findViewById<TextView>(R.id.additional_network_info)
54         val additionalNetworkInfoDetailsTextView = findViewById<TextView>(R.id.additional_network_info_details)
55
56         // TODO.
57         if (ActivityCompat.checkSelfPermission(
58                 this,
59                 Manifest.permission.READ_PHONE_STATE
60             ) != PackageManager.PERMISSION_GRANTED
61         ) {
62             // TODO: Consider calling
63             //    ActivityCompat#requestPermissions
64             // here to request the missing permissions, and then overriding
65             //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
66             //                                          int[] grantResults)
67             // to handle the case where the user grants the permission. See the documentation
68             // for ActivityCompat#requestPermissions for more details.
69             return
70         }
71
72         // Get a handle for the the telephone Manager and the context.
73         val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
74         val context: Context = this
75
76         // Get the voice network type.
77         val voiceNetworkType = getNetworkType(telephonyManager.voiceNetworkType)
78
79         // Populate the voice network text views.
80         voiceNetworkTextView.text = getString(R.string.voice_network, voiceNetworkType[0])
81         voiceNetworkDetailsTextView.text = voiceNetworkType[1]
82
83         // Listen to changes in the cell network state.
84         telephonyManager.listen(object : PhoneStateListener() {
85             override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {// Populate the secure from stingray text view.  <https://source.android.com/devices/tech/connect/acts-5g-testing>
86                 // Populate the stingray security information.
87                 if (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_NR) {  // This is a secure 5G NR SA network.
88                     // Populate the image view.
89                     secureFromStingrayImageView.setImageDrawable(AppCompatResources.getDrawable(context, R.drawable.secure_5g_nr_sa))
90
91                     // Set the text.
92                     secureFromStingrayTextView.text = getString(R.string.secure_from_stingray)
93
94                     // Set the text color.
95                     secureFromStingrayTextView.setTextColor(getColor(R.color.blue_700))
96                 } else {  // This is not a secure 5G NR SA network.
97                     // Populate the image view.
98                     secureFromStingrayImageView.setImageDrawable(AppCompatResources.getDrawable(context, R.drawable.not_secure))
99
100                     // Set the text.
101                     secureFromStingrayTextView.text = getString(R.string.not_secure_from_stingray)
102
103                     // Set the text color.
104                     secureFromStingrayTextView.setTextColor(getColor(R.color.red_700))
105
106                 // Get the strings that correspond to the network information.
107                 val dataNetworkType = getNetworkType(telephonyDisplayInfo.networkType)
108                 val additionalNetworkInfo = getAdditionalNetworkInfo(telephonyDisplayInfo.overrideNetworkType)
109
110                 // Populate the data network text views.
111                 dataNetworkTextView.text = getString(R.string.data_network, dataNetworkType[0])
112                 dataNetworkDetailsTextView.text = dataNetworkType[1]
113                 additionalNetworkInfoTextView.text = getString(R.string.additional_network_info, additionalNetworkInfo[0])
114                 additionalNetworkInfoDetailsTextView.text = additionalNetworkInfo[1]
115                 }
116             }
117         }, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED)
118     }
119
120     private fun getNetworkType(networkType: Int) : Array<String> {
121         // Return the string that corresponds to the network type.
122         return when(networkType) {
123             TelephonyManager.NETWORK_TYPE_UNKNOWN -> arrayOf(getString(R.string.unknown), getString(R.string.unknown))
124             TelephonyManager.NETWORK_TYPE_GPRS -> arrayOf(getString(R.string.gprs), getString(R.string.gprs_detal))
125             TelephonyManager.NETWORK_TYPE_EDGE -> arrayOf(getString(R.string.edge), getString(R.string.edge_detail))
126             TelephonyManager.NETWORK_TYPE_UMTS -> arrayOf(getString(R.string.umts), getString(R.string.umts_detail))
127             TelephonyManager.NETWORK_TYPE_CDMA -> arrayOf(getString(R.string.cdma), getString(R.string.cdma_detail))
128             TelephonyManager.NETWORK_TYPE_EVDO_0 -> arrayOf(getString(R.string.evdo_0), getString(R.string.evdo_0_detail))
129             TelephonyManager.NETWORK_TYPE_EVDO_A -> arrayOf(getString(R.string.evdo_a), getString(R.string.evdo_a_detail))
130             TelephonyManager.NETWORK_TYPE_1xRTT -> arrayOf(getString(R.string.rtt), getString(R.string.rtt_detail))
131             TelephonyManager.NETWORK_TYPE_HSDPA -> arrayOf(getString(R.string.hsdpa), getString(R.string.hsdpa_detail))
132             TelephonyManager.NETWORK_TYPE_HSUPA -> arrayOf(getString(R.string.hsupa), getString(R.string.hsupa_detail))
133             TelephonyManager.NETWORK_TYPE_HSPA -> arrayOf(getString(R.string.hspa), getString(R.string.hspa_detail))
134             TelephonyManager.NETWORK_TYPE_IDEN -> arrayOf(getString(R.string.iden), getString(R.string.iden_detail))
135             TelephonyManager.NETWORK_TYPE_EVDO_B -> arrayOf(getString(R.string.evdo_b), getString(R.string.evdo_b_detail))
136             TelephonyManager.NETWORK_TYPE_LTE -> arrayOf(getString(R.string.lte), getString(R.string.lte_detail))
137             TelephonyManager.NETWORK_TYPE_EHRPD -> arrayOf(getString(R.string.ehrpd), getString(R.string.ehrpd_detail))
138             TelephonyManager.NETWORK_TYPE_HSPAP -> arrayOf(getString(R.string.hspap), getString(R.string.hspap_detail))
139             TelephonyManager.NETWORK_TYPE_GSM -> arrayOf(getString(R.string.gsm), getString(R.string.gsm_detail))
140             TelephonyManager.NETWORK_TYPE_TD_SCDMA -> arrayOf(getString(R.string.td_scdma), getString(R.string.td_scdma_detail))
141             TelephonyManager.NETWORK_TYPE_IWLAN -> arrayOf(getString(R.string.iwlan), getString(R.string.iwlan_detail))
142             TelephonyManager.NETWORK_TYPE_NR -> arrayOf(getString(R.string.nr), getString(R.string.nr_detail))
143             else -> arrayOf(getString(R.string.error), getString(R.string.error))
144         }
145     }
146
147     private fun getAdditionalNetworkInfo(overrideNetworkType: Int) : Array<String> {
148         // Return the string that corresponds to the override network type.
149         return when(overrideNetworkType) {
150             TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE -> arrayOf(getString(R.string.none), "")
151             TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA -> arrayOf(getString(R.string.lte_ca), getString(R.string.lte_ca_detail))
152             TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO -> arrayOf(getString(R.string.lte_advanced_pro), getString(R.string.lte_advanced_pro_detail))
153             TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA -> arrayOf(getString(R.string.lte_nr_nsa), getString(R.string.lte_nr_nsa_detail))
154             TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE -> arrayOf(getString(R.string.lte_nr_nsa_mmwave), getString(R.string.lte_nr_nsa_mmwave_detail))
155             else -> arrayOf(getString(R.string.error), "")
156         }
157     }
158 }