2
2
3
3
import java .util .List ;
4
4
5
+ import android .Manifest ;
5
6
import android .app .Activity ;
7
+ import android .content .pm .PackageManager ;
6
8
import android .os .Bundle ;
7
9
import android .telephony .CellLocation ;
8
10
import android .telephony .NeighboringCellInfo ;
15
17
import android .widget .ProgressBar ;
16
18
import android .widget .TextView ;
17
19
18
- public class PhoneStateSampleActivity extends Activity {
20
+ import androidx .core .app .ActivityCompat ;
21
+
22
+ public class TelephonyManagerDemo extends Activity {
19
23
20
24
private static final String APP_NAME = "SignalLevelSample" ;
21
25
private static final int EXCELLENT_LEVEL = 75 ;
@@ -32,10 +36,10 @@ public class PhoneStateSampleActivity extends Activity {
32
36
private static final int INFO_DATA_DIRECTION_INDEX = 6 ;
33
37
private static final int INFO_DEVICE_INFO_INDEX = 7 ;
34
38
35
- private static final int [] info_ids = { R .id .serviceState_info ,
39
+ private static final int [] info_ids = {R .id .serviceState_info ,
36
40
R .id .cellLocation_info , R .id .callState_info ,
37
41
R .id .connectionState_info , R .id .signalLevel , R .id .signalLevelInfo ,
38
- R .id .dataDirection , R .id .device_info };
42
+ R .id .dataDirection , R .id .device_info };
39
43
40
44
@ Override
41
45
public void onCreate (Bundle savedInstanceState ) {
@@ -102,21 +106,21 @@ private int getDataDirectionRes(int direction) {
102
106
int resid = R .drawable .data_none ;
103
107
104
108
switch (direction ) {
105
- case TelephonyManager .DATA_ACTIVITY_IN :
106
- resid = R .drawable .data_in ;
107
- break ;
108
- case TelephonyManager .DATA_ACTIVITY_OUT :
109
- resid = R .drawable .data_out ;
110
- break ;
111
- case TelephonyManager .DATA_ACTIVITY_INOUT :
112
- resid = R .drawable .data_both ;
113
- break ;
114
- case TelephonyManager .DATA_ACTIVITY_NONE :
115
- resid = R .drawable .data_none ;
116
- break ;
117
- default :
118
- resid = R .drawable .data_none ;
119
- break ;
109
+ case TelephonyManager .DATA_ACTIVITY_IN :
110
+ resid = R .drawable .data_in ;
111
+ break ;
112
+ case TelephonyManager .DATA_ACTIVITY_OUT :
113
+ resid = R .drawable .data_out ;
114
+ break ;
115
+ case TelephonyManager .DATA_ACTIVITY_INOUT :
116
+ resid = R .drawable .data_both ;
117
+ break ;
118
+ case TelephonyManager .DATA_ACTIVITY_NONE :
119
+ resid = R .drawable .data_none ;
120
+ break ;
121
+ default :
122
+ resid = R .drawable .data_none ;
123
+ break ;
120
124
}
121
125
return resid ;
122
126
}
@@ -136,22 +140,32 @@ private void startSignalLevelListener() {
136
140
137
141
private void displayTelephonyInfo () {
138
142
TelephonyManager tm = (TelephonyManager ) getSystemService (TELEPHONY_SERVICE );
143
+ if (ActivityCompat .checkSelfPermission (this , Manifest .permission .ACCESS_FINE_LOCATION ) != PackageManager .PERMISSION_GRANTED ) {
144
+ // TODO: Consider calling
145
+ // ActivityCompat#requestPermissions
146
+ // here to request the missing permissions, and then overriding
147
+ // public void onRequestPermissionsResult(int requestCode, String[] permissions,
148
+ // int[] grantResults)
149
+ // to handle the case where the user grants the permission. See the documentation
150
+ // for ActivityCompat#requestPermissions for more details.
151
+ return ;
152
+ }
139
153
GsmCellLocation loc = (GsmCellLocation ) tm .getCellLocation ();
140
154
// Reorganize it to do one getSomeData, logString it in pairs, with code guards
141
155
if (loc == null ) {
142
156
return ;
143
157
}
144
158
int cellid = loc .getCid ();
145
159
int lac = loc .getLac ();
146
- String deviceid = tm .getDeviceId ();
147
- String phonenumber = tm .getLine1Number ();
148
- String softwareversion = tm .getDeviceSoftwareVersion ();
160
+ String deviceid = null ; // tm.getDeviceId();
161
+ String phonenumber = null ; // tm.getLine1Number();
162
+ String softwareversion = null ; // tm.getDeviceSoftwareVersion();
149
163
String operatorname = tm .getNetworkOperatorName ();
150
164
String simcountrycode = tm .getSimCountryIso ();
151
165
String simoperator = tm .getSimOperatorName ();
152
- String simserialno = tm .getSimSerialNumber ();
153
- String subscriberid = tm .getSubscriberId ();
154
- String networktype = getNetworkTypeString (tm .getNetworkType ());
166
+ String simserialno = null ; // tm.getSimSerialNumber();
167
+ String subscriberid = null ; // tm.getSubscriberId();
168
+ String networktype = null ; // getNetworkTypeString(tm.getNetworkType());
155
169
String phonetype = getPhoneTypeString (tm .getPhoneType ());
156
170
logString ("CellID: " + cellid );
157
171
logString ("LAC: " + lac );
@@ -176,7 +190,7 @@ private void displayTelephonyInfo() {
176
190
deviceinfo += ("Subscriber ID: " + subscriberid + "\n " );
177
191
deviceinfo += ("Network Type: " + networktype + "\n " );
178
192
deviceinfo += ("Phone Type: " + phonetype + "\n " );
179
- List <NeighboringCellInfo > cellinfo = tm .getNeighboringCellInfo ();
193
+ List <NeighboringCellInfo > cellinfo = null ; // tm.getNeighboringCellInfo();
180
194
if (null != cellinfo ) {
181
195
for (NeighboringCellInfo info : cellinfo ) {
182
196
deviceinfo += ("\t CellID: " + info .getCid () + ", RSSI: "
@@ -189,34 +203,34 @@ private void displayTelephonyInfo() {
189
203
private String getNetworkTypeString (int type ) {
190
204
String typeString = "Unknown" ;
191
205
switch (type ) {
192
- case TelephonyManager .NETWORK_TYPE_EDGE :
193
- typeString = "EDGE" ;
194
- break ;
195
- case TelephonyManager .NETWORK_TYPE_GPRS :
196
- typeString = "GPRS" ;
197
- break ;
198
- case TelephonyManager .NETWORK_TYPE_UMTS :
199
- typeString = "UMTS" ;
200
- break ;
201
- default :
202
- typeString = "UNKNOWN" ;
203
- break ;
206
+ case TelephonyManager .NETWORK_TYPE_EDGE :
207
+ typeString = "EDGE" ;
208
+ break ;
209
+ case TelephonyManager .NETWORK_TYPE_GPRS :
210
+ typeString = "GPRS" ;
211
+ break ;
212
+ case TelephonyManager .NETWORK_TYPE_UMTS :
213
+ typeString = "UMTS" ;
214
+ break ;
215
+ default :
216
+ typeString = "UNKNOWN" ;
217
+ break ;
204
218
}
205
219
return typeString ;
206
220
}
207
221
208
222
private String getPhoneTypeString (int type ) {
209
223
String typeString = "Unknown" ;
210
224
switch (type ) {
211
- case TelephonyManager .PHONE_TYPE_GSM :
212
- typeString = "GSM" ;
213
- break ;
214
- case TelephonyManager .PHONE_TYPE_NONE :
215
- typeString = "UNKNOWN" ;
216
- break ;
217
- default :
218
- typeString = "UNKNOWN" ;
219
- break ;
225
+ case TelephonyManager .PHONE_TYPE_GSM :
226
+ typeString = "GSM" ;
227
+ break ;
228
+ case TelephonyManager .PHONE_TYPE_NONE :
229
+ typeString = "UNKNOWN" ;
230
+ break ;
231
+ default :
232
+ typeString = "UNKNOWN" ;
233
+ break ;
220
234
}
221
235
return typeString ;
222
236
}
@@ -230,6 +244,17 @@ private int logString(String message) {
230
244
@ Override
231
245
public void onCallForwardingIndicatorChanged (boolean cfi ) {
232
246
Log .i (APP_NAME , "onCallForwardingIndicatorChanged " + cfi );
247
+ if (ActivityCompat .checkSelfPermission (TelephonyManagerDemo .this ,
248
+ Manifest .permission .READ_PHONE_STATE ) != PackageManager .PERMISSION_GRANTED ) {
249
+ // TODO: Consider calling
250
+ // ActivityCompat#requestPermissions
251
+ // here to request the missing permissions, and then overriding
252
+ // public void onRequestPermissionsResult(int requestCode, String[] permissions,
253
+ // int[] grantResults)
254
+ // to handle the case where the user grants the permission. See the documentation
255
+ // for ActivityCompat#requestPermissions for more details.
256
+ return ;
257
+ }
233
258
super .onCallForwardingIndicatorChanged (cfi );
234
259
}
235
260
0 commit comments