22
33import java .util .List ;
44
5+ import android .Manifest ;
56import android .app .Activity ;
7+ import android .content .pm .PackageManager ;
68import android .os .Bundle ;
79import android .telephony .CellLocation ;
810import android .telephony .NeighboringCellInfo ;
1517import android .widget .ProgressBar ;
1618import android .widget .TextView ;
1719
18- public class PhoneStateSampleActivity extends Activity {
20+ import androidx .core .app .ActivityCompat ;
21+
22+ public class TelephonyManagerDemo extends Activity {
1923
2024 private static final String APP_NAME = "SignalLevelSample" ;
2125 private static final int EXCELLENT_LEVEL = 75 ;
@@ -32,10 +36,10 @@ public class PhoneStateSampleActivity extends Activity {
3236 private static final int INFO_DATA_DIRECTION_INDEX = 6 ;
3337 private static final int INFO_DEVICE_INFO_INDEX = 7 ;
3438
35- private static final int [] info_ids = { R .id .serviceState_info ,
39+ private static final int [] info_ids = {R .id .serviceState_info ,
3640 R .id .cellLocation_info , R .id .callState_info ,
3741 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 };
3943
4044 @ Override
4145 public void onCreate (Bundle savedInstanceState ) {
@@ -102,21 +106,21 @@ private int getDataDirectionRes(int direction) {
102106 int resid = R .drawable .data_none ;
103107
104108 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 ;
120124 }
121125 return resid ;
122126 }
@@ -136,22 +140,32 @@ private void startSignalLevelListener() {
136140
137141 private void displayTelephonyInfo () {
138142 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+ }
139153 GsmCellLocation loc = (GsmCellLocation ) tm .getCellLocation ();
140154 // Reorganize it to do one getSomeData, logString it in pairs, with code guards
141155 if (loc == null ) {
142156 return ;
143157 }
144158 int cellid = loc .getCid ();
145159 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();
149163 String operatorname = tm .getNetworkOperatorName ();
150164 String simcountrycode = tm .getSimCountryIso ();
151165 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());
155169 String phonetype = getPhoneTypeString (tm .getPhoneType ());
156170 logString ("CellID: " + cellid );
157171 logString ("LAC: " + lac );
@@ -176,7 +190,7 @@ private void displayTelephonyInfo() {
176190 deviceinfo += ("Subscriber ID: " + subscriberid + "\n " );
177191 deviceinfo += ("Network Type: " + networktype + "\n " );
178192 deviceinfo += ("Phone Type: " + phonetype + "\n " );
179- List <NeighboringCellInfo > cellinfo = tm .getNeighboringCellInfo ();
193+ List <NeighboringCellInfo > cellinfo = null ; // tm.getNeighboringCellInfo();
180194 if (null != cellinfo ) {
181195 for (NeighboringCellInfo info : cellinfo ) {
182196 deviceinfo += ("\t CellID: " + info .getCid () + ", RSSI: "
@@ -189,34 +203,34 @@ private void displayTelephonyInfo() {
189203 private String getNetworkTypeString (int type ) {
190204 String typeString = "Unknown" ;
191205 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 ;
204218 }
205219 return typeString ;
206220 }
207221
208222 private String getPhoneTypeString (int type ) {
209223 String typeString = "Unknown" ;
210224 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 ;
220234 }
221235 return typeString ;
222236 }
@@ -230,6 +244,17 @@ private int logString(String message) {
230244 @ Override
231245 public void onCallForwardingIndicatorChanged (boolean cfi ) {
232246 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+ }
233258 super .onCallForwardingIndicatorChanged (cfi );
234259 }
235260
0 commit comments