Skip to content

Commit

Permalink
Bugfix (Android14): for recording Location/GPS and Nearby Devices/Blu…
Browse files Browse the repository at this point in the history
…etooth are mandatory due to Android.

Fixes #1749.
  • Loading branch information
dennisguse committed Oct 29, 2023
1 parent c510bb6 commit cb48d7f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

private void requestRequiredPermissions() {
PermissionRequester.ALL.requestPermissionsIfNeeded(this, this, null, (requester) -> Toast.makeText(this, R.string.permission_gps_failed, Toast.LENGTH_LONG).show());
PermissionRequester.ALL.requestPermissionsIfNeeded(this, this, null, (requester) -> Toast.makeText(this, R.string.permission_recording_failed, Toast.LENGTH_LONG).show());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.util.Pair;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
Expand All @@ -36,6 +37,7 @@
import java.io.StringWriter;
import java.time.Duration;

import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.data.models.Distance;
import de.dennisguse.opentracks.data.models.Marker;
import de.dennisguse.opentracks.data.models.Track;
Expand All @@ -45,6 +47,7 @@
import de.dennisguse.opentracks.services.handlers.GpsStatusValue;
import de.dennisguse.opentracks.services.handlers.TrackPointCreator;
import de.dennisguse.opentracks.settings.PreferencesUtils;
import de.dennisguse.opentracks.util.PermissionRequester;
import de.dennisguse.opentracks.util.SystemUtils;

public class TrackRecordingService extends Service implements TrackPointCreator.Callback, SharedPreferences.OnSharedPreferenceChangeListener, TrackRecordingManager.IdleObserver {
Expand Down Expand Up @@ -199,6 +202,14 @@ private void startSensors() {
wakeLock = SystemUtils.acquireWakeLock(this, wakeLock);
trackPointCreator.start(this, handler);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
if (!PermissionRequester.RECORDING.hasPermission(this)) {
Toast.makeText(this, R.string.permission_recording_failed, Toast.LENGTH_LONG).show();
return;
}
}

startForeground(TrackRecordingServiceNotificationManager.NOTIFICATION_ID, notificationManager.setGPSonlyStarted(this), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION + ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE);
} else {
startForeground(TrackRecordingServiceNotificationManager.NOTIFICATION_ID, notificationManager.setGPSonlyStarted(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,25 @@ private void requestPermission(ActivityResultCaller context, @Nullable Runnable
}

private static final List<String> ALL_PERMISSIONS;
private static final List<String> RECORDING_PERMISSIONS;

static {
ArrayList<String> all = new ArrayList<>(GPS_PERMISSION);
all.addAll(BLUETOOTH_PERMISSIONS);
all.addAll(NOTIFICATION_PERMISSIONS);
ArrayList<String> recording = new ArrayList<>(GPS_PERMISSION);
recording.addAll(BLUETOOTH_PERMISSIONS);

ALL_PERMISSIONS = Collections.unmodifiableList(all);
RECORDING_PERMISSIONS = Collections.unmodifiableList(recording);

recording.addAll(NOTIFICATION_PERMISSIONS);

ALL_PERMISSIONS = Collections.unmodifiableList(recording);
}

public final static PermissionRequester GPS = new PermissionRequester(GPS_PERMISSION);
public final static PermissionRequester BLUETOOTH = new PermissionRequester(BLUETOOTH_PERMISSIONS);
public final static PermissionRequester NOTIFICATION = new PermissionRequester(NOTIFICATION_PERMISSIONS);

public final static PermissionRequester ALL = new PermissionRequester(ALL_PERMISSIONS);
public final static PermissionRequester RECORDING = new PermissionRequester(RECORDING_PERMISSIONS);

public interface RejectedCallback {
void rejected(PermissionRequester permissionRequester);
Expand Down
1 change: 1 addition & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ limitations under the License.
<string name="recording_service">Recording service</string>
<!-- Permission -->
<string name="permission_gps_failed">OpenTracks requires permission to use GPS.</string>
<string name="permission_recording_failed">OpenTracks requires permission to access your location as well as nearby devices.</string>
<string name="permission_bluetooth_failed">OpenTracks requires permission to use Bluetooth.</string>
<string name="permission_bluetooth_failed_rejected">You manually need to grant OpenTracks access to Nearby Devices in system settings.</string>
<!-- Search -->
Expand Down

0 comments on commit cb48d7f

Please sign in to comment.