Skip to content

Commit ee4af3e

Browse files
committed
Add a few permissions checks for the new foreground service location permission we're using. I haven't seen issues with these, but just in case. Worst case, they're unecessary and the checks will just be bypassed because they're already granted. Best case, they will prompt someone missing a necessary permission and get them into a good state.
1 parent ed86520 commit ee4af3e

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

android-src/KV4PHT/app/src/main/java/com/vagell/kv4pht/radio/RadioAudioService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ public void setAprsBeaconPosition(boolean enabled) {
341341
}
342342
}
343343

344+
public boolean getAprsBeaconPosition() {
345+
return this.aprsBeaconPosition;
346+
}
347+
344348
private void startBeaconScheduler() {
345349
if (beaconScheduler == null || beaconScheduler.isShutdown()) {
346350
beaconScheduler = Executors.newSingleThreadScheduledExecutor();

android-src/KV4PHT/app/src/main/java/com/vagell/kv4pht/ui/MainActivity.java

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ kv4p HT (see http://kv4p.com)
4545
import android.util.Log;
4646
import android.view.ContextThemeWrapper;
4747
import android.view.KeyEvent;
48-
import android.view.Menu;
4948
import android.view.MenuItem;
5049
import android.view.MotionEvent;
5150
import android.view.View;
@@ -67,7 +66,6 @@ kv4p HT (see http://kv4p.com)
6766
import androidx.core.app.ActivityCompat;
6867
import androidx.core.app.NotificationCompat;
6968
import androidx.core.content.ContextCompat;
70-
import androidx.core.view.MenuCompat;
7169
import androidx.databinding.DataBindingUtil;
7270
import androidx.lifecycle.Observer;
7371
import androidx.lifecycle.ViewModelProvider;
@@ -130,7 +128,8 @@ public class MainActivity extends AppCompatActivity {
130128
// Android permission stuff
131129
private static final int REQUEST_AUDIO_PERMISSION_CODE = 1;
132130
private static final int REQUEST_NOTIFICATIONS_PERMISSION_CODE = 2;
133-
private static final int REQUEST_LOCATION_PERMISSION_CODE = 3;
131+
private static final int REQUEST_FINE_LOCATION_PERMISSION_CODE = 3;
132+
private static final int REQUEST_FOREGROUND_SERVICE_LOCATION_PERMISSION_CODE = 4;
134133
private static final String ACTION_USB_PERMISSION = "com.vagell.kv4pht.USB_PERMISSION";
135134

136135
// Radio params and related settings
@@ -1027,7 +1026,9 @@ private void applyAprsSettings(Map<String, String> settings) {
10271026
if (beacon != null && radioAudioService != null) {
10281027
boolean beaconEnabled = Boolean.parseBoolean(beacon);
10291028
threadPoolExecutor.execute(() -> radioAudioService.setAprsBeaconPosition(beaconEnabled));
1030-
if (beaconEnabled) requestPositionPermissions();
1029+
if (beaconEnabled) {
1030+
requestFinePositionPermissions();
1031+
}
10311032
}
10321033
}
10331034

@@ -1397,7 +1398,7 @@ public void onClick(DialogInterface dialogInterface, int i) {
13971398
}
13981399
}
13991400

1400-
protected void requestPositionPermissions() {
1401+
protected void requestFinePositionPermissions() {
14011402
// Check that the user allows our app to get position, otherwise ask for the permission.
14021403
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
14031404
// Should we show an explanation?
@@ -1412,7 +1413,7 @@ protected void requestPositionPermissions() {
14121413
public void onClick(DialogInterface dialogInterface, int i) {
14131414
ActivityCompat.requestPermissions(MainActivity.this,
14141415
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
1415-
REQUEST_LOCATION_PERMISSION_CODE);
1416+
REQUEST_FINE_LOCATION_PERMISSION_CODE);
14161417
}
14171418
})
14181419
.create()
@@ -1421,9 +1422,14 @@ public void onClick(DialogInterface dialogInterface, int i) {
14211422
} else {
14221423
ActivityCompat.requestPermissions(this,
14231424
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
1424-
REQUEST_LOCATION_PERMISSION_CODE);
1425+
REQUEST_FINE_LOCATION_PERMISSION_CODE);
14251426
}
14261427
}
1428+
1429+
// If APRS beaconing is enabled, also request foreground service location permission.
1430+
if (radioAudioService != null && radioAudioService.getAprsBeaconPosition()) {
1431+
requestForegroundServiceLocationPermissions();
1432+
}
14271433
}
14281434

14291435
protected void requestNotificationPermissions() {
@@ -1458,6 +1464,38 @@ public void onClick(DialogInterface dialogInterface, int i) {
14581464
}
14591465
}
14601466

1467+
protected void requestForegroundServiceLocationPermissions() {
1468+
if (ContextCompat.checkSelfPermission(this,
1469+
Manifest.permission.FOREGROUND_SERVICE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
1470+
1471+
// Should we show an explanation?
1472+
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
1473+
Manifest.permission.FOREGROUND_SERVICE_LOCATION)) {
1474+
1475+
new AlertDialog.Builder(this)
1476+
.setTitle("Permission needed")
1477+
.setMessage("This app needs to know your location for APRS beaconing")
1478+
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
1479+
@Override
1480+
public void onClick(DialogInterface dialogInterface, int i) {
1481+
ActivityCompat.requestPermissions(MainActivity.this,
1482+
new String[]{Manifest.permission.FOREGROUND_SERVICE_LOCATION},
1483+
REQUEST_FOREGROUND_SERVICE_LOCATION_PERMISSION_CODE);
1484+
}
1485+
})
1486+
.create()
1487+
.show();
1488+
1489+
} else {
1490+
ActivityCompat.requestPermissions(this,
1491+
new String[]{Manifest.permission.FOREGROUND_SERVICE_LOCATION},
1492+
REQUEST_FOREGROUND_SERVICE_LOCATION_PERMISSION_CODE);
1493+
}
1494+
} else {
1495+
// Permission has already been granted
1496+
}
1497+
}
1498+
14611499
@Override
14621500
public void onRequestPermissionsResult(int requestCode,
14631501
@NonNull String[] permissions, @NonNull int[] grantResults) {
@@ -1485,16 +1523,33 @@ public void onRequestPermissionsResult(int requestCode,
14851523
}
14861524
return;
14871525
}
1488-
case REQUEST_LOCATION_PERMISSION_CODE: {
1526+
case REQUEST_FINE_LOCATION_PERMISSION_CODE: {
14891527
// If request is cancelled, the result arrays are empty.
14901528
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
14911529
// Permission granted.
1530+
// If APRS beaconing is enabled, also request foreground service location permission.
1531+
if (radioAudioService != null && radioAudioService.getAprsBeaconPosition()) {
1532+
requestForegroundServiceLocationPermissions();
1533+
}
14921534
} else {
14931535
// Permission denied
14941536
Log.d("DEBUG", "Warning: Need fine location permission to include in APRS messages (user turned this setting on)");
14951537
}
14961538
return;
14971539
}
1540+
case REQUEST_FOREGROUND_SERVICE_LOCATION_PERMISSION_CODE: {
1541+
// If request is cancelled, the result arrays are empty.
1542+
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
1543+
// Permission granted.
1544+
} else {
1545+
// Permission denied
1546+
Log.d("DEBUG", "Warning: Need foreground service location permission for APRS beaconing (user turned this setting on)");
1547+
if (radioAudioService != null) {
1548+
radioAudioService.setAprsBeaconPosition(false); // Hack to prevent crashing, though the setting will still be on.
1549+
}
1550+
}
1551+
return;
1552+
}
14981553
}
14991554
}
15001555

@@ -1697,7 +1752,7 @@ public void singleBeaconButtonClicked(View view) {
16971752
}
16981753

16991754
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
1700-
requestPositionPermissions();
1755+
requestFinePositionPermissions();
17011756
return;
17021757
}
17031758

0 commit comments

Comments
 (0)