Initial commit.
[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.TextView
30
31 import androidx.appcompat.app.AppCompatActivity
32 import androidx.core.app.ActivityCompat
33
34 import com.stoutner.privacycell.R
35
36 class PrivacyCell : AppCompatActivity() {
37     override fun onCreate(savedInstanceState: Bundle?) {
38         // Run the default commands.
39         super.onCreate(savedInstanceState)
40
41         // Set the content view.
42         setContentView(R.layout.privacy_cell_linearlayout)
43
44         // Get handles for the views.
45         val voiceNetworkTextView = findViewById<TextView>(R.id.voice_network)
46         val dataNetworkTextView = findViewById<TextView>(R.id.data_network)
47         val additionalNetworkInfoTextView = findViewById<TextView>(R.id.additional_network_info)
48         val secureFromStingrayTextView = findViewById<TextView>(R.id.secure_from_stingray)
49
50         // TODO.
51         if (ActivityCompat.checkSelfPermission(
52                 this,
53                 Manifest.permission.READ_PHONE_STATE
54             ) != PackageManager.PERMISSION_GRANTED
55         ) {
56             // TODO: Consider calling
57             //    ActivityCompat#requestPermissions
58             // here to request the missing permissions, and then overriding
59             //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
60             //                                          int[] grantResults)
61             // to handle the case where the user grants the permission. See the documentation
62             // for ActivityCompat#requestPermissions for more details.
63             return
64         }
65
66         // Get a handle for the the telephone Manager.
67         val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
68
69         // Get the voice network type.
70         val voiceNetworkType = getNetworkType(telephonyManager.voiceNetworkType)
71
72         // Populate the voice network text view.
73         voiceNetworkTextView.text = getString(R.string.voice_network, voiceNetworkType)
74
75         // Listen to changes in the cell network state.
76         telephonyManager.listen(object : PhoneStateListener() {
77             override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {
78                 // Get the strings that correspond to the network information.
79                 val dataNetworkType = getNetworkType(telephonyDisplayInfo.networkType)
80                 val additionalNetworkInfo = getAdditionalNetworkInfo(telephonyDisplayInfo.overrideNetworkType)
81
82                 // Populate the network text views.
83                 dataNetworkTextView.text = getString(R.string.data_network, dataNetworkType)
84                 additionalNetworkInfoTextView.text = getString(R.string.additional_network_info, additionalNetworkInfo)
85
86                 // Populate the secure from stingray text view.  <https://source.android.com/devices/tech/connect/acts-5g-testing>
87                 if (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_NR) {
88                     secureFromStingrayTextView.text = getString(R.string.secure_from_stingray)
89                 } else {
90                     secureFromStingrayTextView.text = getString(R.string.not_secure_from_stingray)
91                 }
92             }
93         }, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED)
94     }
95
96     private fun getNetworkType(networkType: Int) : String {
97         // Return the string that corresponds to the network type.
98         return when(networkType) {
99             TelephonyManager.NETWORK_TYPE_UNKNOWN -> getString(R.string.unknown)
100             TelephonyManager.NETWORK_TYPE_GPRS -> getString(R.string.gprs)
101             TelephonyManager.NETWORK_TYPE_EDGE -> getString(R.string.edge)
102             TelephonyManager.NETWORK_TYPE_UMTS -> getString(R.string.umts)
103             TelephonyManager.NETWORK_TYPE_CDMA -> getString(R.string.cdma)
104             TelephonyManager.NETWORK_TYPE_EVDO_0 -> getString(R.string.evdo_0)
105             TelephonyManager.NETWORK_TYPE_EVDO_A -> getString(R.string.evdo_a)
106             TelephonyManager.NETWORK_TYPE_1xRTT -> getString(R.string.rtt)
107             TelephonyManager.NETWORK_TYPE_HSDPA -> getString(R.string.hsdpa)
108             TelephonyManager.NETWORK_TYPE_HSUPA -> getString(R.string.hsupa)
109             TelephonyManager.NETWORK_TYPE_HSPA -> getString(R.string.hspa)
110             TelephonyManager.NETWORK_TYPE_IDEN -> getString(R.string.iden)
111             TelephonyManager.NETWORK_TYPE_EVDO_B -> getString(R.string.evdo_b)
112             TelephonyManager.NETWORK_TYPE_LTE -> getString(R.string.lte)
113             TelephonyManager.NETWORK_TYPE_EHRPD -> getString(R.string.ehrpd)
114             TelephonyManager.NETWORK_TYPE_HSPAP -> getString(R.string.hspap)
115             TelephonyManager.NETWORK_TYPE_GSM -> getString(R.string.gsm)
116             TelephonyManager.NETWORK_TYPE_TD_SCDMA -> getString(R.string.td_scdma)
117             TelephonyManager.NETWORK_TYPE_IWLAN -> getString(R.string.iwlan)
118             TelephonyManager.NETWORK_TYPE_NR -> getString(R.string.nr)
119             else -> getString(R.string.error)
120         }
121     }
122
123     private fun getAdditionalNetworkInfo(overrideNetworkType: Int) : String {
124         // Return the string that corresponds to the override network type.
125         return when(overrideNetworkType) {
126             TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE -> getString(R.string.none)
127             TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA -> getString(R.string.lte_ca)
128             TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO -> getString(R.string.lte_advanced_pro)
129             TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA -> getString(R.string.lte_nr_nsa)
130             TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE -> getString(R.string.lte_nr_nsa_mmwave)
131             else -> getString(R.string.error)
132         }
133     }
134 }