Skip to content

Commit e762c05

Browse files
committed
Add some permissions checks; remove one category from listening
1 parent b8130a0 commit e762c05

File tree

1 file changed

+61
-41
lines changed

1 file changed

+61
-41
lines changed

Diff for: TelephonyManager/app/src/main/java/com/example/telephonymanager/TelephonyManagerDemo.java

+61-41
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private void startSignalLevelListener() {
129129
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
130130
int events = PhoneStateListener.LISTEN_SIGNAL_STRENGTH
131131
| PhoneStateListener.LISTEN_DATA_ACTIVITY
132-
| PhoneStateListener.LISTEN_CELL_LOCATION
132+
// | PhoneStateListener.LISTEN_CELL_LOCATION
133133
| PhoneStateListener.LISTEN_CALL_STATE
134134
| PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR
135135
| PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
@@ -262,15 +262,15 @@ public void onCallForwardingIndicatorChanged(boolean cfi) {
262262
public void onCallStateChanged(int state, String incomingNumber) {
263263
String callState = "UNKNOWN";
264264
switch (state) {
265-
case TelephonyManager.CALL_STATE_IDLE:
266-
callState = "IDLE";
267-
break;
268-
case TelephonyManager.CALL_STATE_RINGING:
269-
callState = "Ringing (" + incomingNumber + ")";
270-
break;
271-
case TelephonyManager.CALL_STATE_OFFHOOK:
272-
callState = "Offhook";
273-
break;
265+
case TelephonyManager.CALL_STATE_IDLE:
266+
callState = "IDLE";
267+
break;
268+
case TelephonyManager.CALL_STATE_RINGING:
269+
callState = "Ringing (" + incomingNumber + ")";
270+
break;
271+
case TelephonyManager.CALL_STATE_OFFHOOK:
272+
callState = "Offhook";
273+
break;
274274
}
275275
setTextViewText(info_ids[INFO_CALL_STATE_INDEX], callState);
276276
Log.i(APP_NAME, "onCallStateChanged " + callState);
@@ -283,28 +283,38 @@ public void onCellLocationChanged(CellLocation location) {
283283
setTextViewText(info_ids[INFO_CELL_LOCATION_INDEX], locationString);
284284

285285
Log.i(APP_NAME, "onCellLocationChanged " + locationString);
286+
if (ActivityCompat.checkSelfPermission(TelephonyManagerDemo.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
287+
// TODO: Consider calling
288+
// ActivityCompat#requestPermissions
289+
// here to request the missing permissions, and then overriding
290+
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
291+
// int[] grantResults)
292+
// to handle the case where the user grants the permission. See the documentation
293+
// for ActivityCompat#requestPermissions for more details.
294+
return;
295+
}
286296
super.onCellLocationChanged(location);
287297
}
288298

289299
@Override
290300
public void onDataActivity(int direction) {
291301
String directionString = "none";
292302
switch (direction) {
293-
case TelephonyManager.DATA_ACTIVITY_IN:
294-
directionString = "IN";
295-
break;
296-
case TelephonyManager.DATA_ACTIVITY_OUT:
297-
directionString = "OUT";
298-
break;
299-
case TelephonyManager.DATA_ACTIVITY_INOUT:
300-
directionString = "INOUT";
301-
break;
302-
case TelephonyManager.DATA_ACTIVITY_NONE:
303-
directionString = "NONE";
304-
break;
305-
default:
306-
directionString = "UNKNOWN: " + direction;
307-
break;
303+
case TelephonyManager.DATA_ACTIVITY_IN:
304+
directionString = "IN";
305+
break;
306+
case TelephonyManager.DATA_ACTIVITY_OUT:
307+
directionString = "OUT";
308+
break;
309+
case TelephonyManager.DATA_ACTIVITY_INOUT:
310+
directionString = "INOUT";
311+
break;
312+
case TelephonyManager.DATA_ACTIVITY_NONE:
313+
directionString = "NONE";
314+
break;
315+
default:
316+
directionString = "UNKNOWN: " + direction;
317+
break;
308318
}
309319

310320
setDataDirection(info_ids[INFO_DATA_DIRECTION_INDEX], direction);
@@ -316,22 +326,22 @@ public void onDataActivity(int direction) {
316326
public void onDataConnectionStateChanged(int state) {
317327
String connectionState = "Unknown";
318328
switch (state) {
319-
case TelephonyManager.DATA_CONNECTED:
320-
connectionState = "Connected";
321-
break;
322-
case TelephonyManager.DATA_CONNECTING:
323-
connectionState = "Connecting";
324-
break;
325-
case TelephonyManager.DATA_DISCONNECTED:
326-
connectionState = "Disconnected";
327-
break;
328-
case TelephonyManager.DATA_SUSPENDED:
329-
connectionState = "Suspended";
330-
break;
331-
default:
332-
333-
connectionState = "Unknown: " + state;
334-
break;
329+
case TelephonyManager.DATA_CONNECTED:
330+
connectionState = "Connected";
331+
break;
332+
case TelephonyManager.DATA_CONNECTING:
333+
connectionState = "Connecting";
334+
break;
335+
case TelephonyManager.DATA_DISCONNECTED:
336+
connectionState = "Disconnected";
337+
break;
338+
case TelephonyManager.DATA_SUSPENDED:
339+
connectionState = "Suspended";
340+
break;
341+
default:
342+
343+
connectionState = "Unknown: " + state;
344+
break;
335345
}
336346

337347
setTextViewText(info_ids[INFO_CONNECTION_STATE_INDEX],
@@ -345,6 +355,16 @@ public void onDataConnectionStateChanged(int state) {
345355
@Override
346356
public void onMessageWaitingIndicatorChanged(boolean mwi) {
347357
Log.i(APP_NAME, "onMessageWaitingIndicatorChanged " + mwi);
358+
if (ActivityCompat.checkSelfPermission(TelephonyManagerDemo.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
359+
// TODO: Consider calling
360+
// ActivityCompat#requestPermissions
361+
// here to request the missing permissions, and then overriding
362+
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
363+
// int[] grantResults)
364+
// to handle the case where the user grants the permission. See the documentation
365+
// for ActivityCompat#requestPermissions for more details.
366+
return;
367+
}
348368
super.onMessageWaitingIndicatorChanged(mwi);
349369
}
350370

0 commit comments

Comments
 (0)