forked from upstream-mirrors/OpenTracks
Bug: PublicAPI needs to call startForegroundService (SDK API26+).
Fixes #1301.
This commit is contained in:
@@ -241,7 +241,7 @@ public class TrackListActivity extends AbstractTrackDeleteActivity implements Co
|
|||||||
startActivity(newIntent);
|
startActivity(newIntent);
|
||||||
|
|
||||||
connection.unbind(this);
|
connection.unbind(this);
|
||||||
}).startAndBind(this);
|
}).startAndBind(this, true);
|
||||||
});
|
});
|
||||||
viewBinding.trackListFabAction.setOnLongClickListener((view) -> {
|
viewBinding.trackListFabAction.setOnLongClickListener((view) -> {
|
||||||
if (!recordingStatus.isRecording()) {
|
if (!recordingStatus.isRecording()) {
|
||||||
|
|||||||
@@ -213,7 +213,7 @@ public class TrackRecordedActivity extends AbstractTrackDeleteActivity implement
|
|||||||
|
|
||||||
connection.unbind(this);
|
connection.unbind(this);
|
||||||
finish();
|
finish();
|
||||||
}).startAndBind(this);
|
}).startAndBind(this, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ public class TrackStoppedActivity extends AbstractTrackDeleteActivity implements
|
|||||||
|
|
||||||
connection.unbind(this);
|
connection.unbind(this);
|
||||||
finish();
|
finish();
|
||||||
}).startAndBind(this);
|
}).startAndBind(this, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public abstract class AbstractAPIActivity extends AppCompatActivity {
|
|||||||
if (PreferencesUtils.isPublicAPIenabled()) {
|
if (PreferencesUtils.isPublicAPIenabled()) {
|
||||||
Log.i(TAG, "Received and trying to execute requested action.");
|
Log.i(TAG, "Received and trying to execute requested action.");
|
||||||
new TrackRecordingServiceConnection(serviceConnectedCallback)
|
new TrackRecordingServiceConnection(serviceConnectedCallback)
|
||||||
.startAndBind(this);
|
.startAndBind(this, true);
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(this, getString(R.string.settings_public_api_disabled_toast), Toast.LENGTH_LONG).show();
|
Toast.makeText(this, getString(R.string.settings_public_api_disabled_toast), Toast.LENGTH_LONG).show();
|
||||||
Log.w(TAG, "Public API is disabled; ignoring request.");
|
Log.w(TAG, "Public API is disabled; ignoring request.");
|
||||||
|
|||||||
@@ -129,7 +129,8 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
|||||||
trackRecordingManager = null;
|
trackRecordingManager = null;
|
||||||
|
|
||||||
// Reverse order from onCreate
|
// Reverse order from onCreate
|
||||||
showNotification(false); //TODO Why?
|
stopForeground(true);
|
||||||
|
|
||||||
notificationManager.stop();
|
notificationManager.stop();
|
||||||
notificationManager = null;
|
notificationManager = null;
|
||||||
|
|
||||||
@@ -205,7 +206,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
|||||||
private void startSensors() {
|
private void startSensors() {
|
||||||
wakeLock = SystemUtils.acquireWakeLock(this, wakeLock);
|
wakeLock = SystemUtils.acquireWakeLock(this, wakeLock);
|
||||||
trackPointCreator.start(this, handler);
|
trackPointCreator.start(this, handler);
|
||||||
showNotification(true);
|
startForeground(TrackRecordingServiceNotificationManager.NOTIFICATION_ID, notificationManager.setGPSonlyStarted(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void endCurrentTrack() {
|
public void endCurrentTrack() {
|
||||||
@@ -247,7 +248,8 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
|||||||
if (!isRecording()) return;
|
if (!isRecording()) return;
|
||||||
|
|
||||||
trackPointCreator.stop();
|
trackPointCreator.stop();
|
||||||
showNotification(false);
|
stopForeground(true);
|
||||||
|
notificationManager.cancelNotification();
|
||||||
wakeLock = SystemUtils.releaseWakeLock(wakeLock);
|
wakeLock = SystemUtils.releaseWakeLock(wakeLock);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,19 +275,6 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showNotification(boolean isGpsStarted) {
|
|
||||||
if (isRecording()) {
|
|
||||||
startForeground(TrackRecordingServiceNotificationManager.NOTIFICATION_ID, notificationManager.setRecording(this, recordingStatus.getTrackId()));
|
|
||||||
}
|
|
||||||
if (!isRecording() && isGpsStarted) {
|
|
||||||
startForeground(TrackRecordingServiceNotificationManager.NOTIFICATION_ID, notificationManager.setGPSonlyStarted(this));
|
|
||||||
}
|
|
||||||
if (!isRecording() && !isGpsStarted) {
|
|
||||||
stopForeground(true);
|
|
||||||
notificationManager.cancelNotification();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Marker.Id insertMarker(String name, String category, String description, String photoUrl) {
|
public Marker.Id insertMarker(String name, String category, String description, String photoUrl) {
|
||||||
if (!isRecording()) {
|
if (!isRecording()) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
+10
-3
@@ -28,6 +28,7 @@ import android.widget.Toast;
|
|||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
import de.dennisguse.opentracks.BuildConfig;
|
import de.dennisguse.opentracks.BuildConfig;
|
||||||
import de.dennisguse.opentracks.R;
|
import de.dennisguse.opentracks.R;
|
||||||
@@ -66,15 +67,21 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts and binds the service.
|
* Starts and binds the service.
|
||||||
|
*
|
||||||
|
* @param foreground is the service expected to call `startForeground()`?
|
||||||
*/
|
*/
|
||||||
public void startAndBind(Context context) {
|
public void startAndBind(Context context, boolean foreground) {
|
||||||
if (trackRecordingService != null) {
|
if (trackRecordingService != null) {
|
||||||
// Service is already started and bound.
|
// Service is already started and bound.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.i(TAG, "Starting the service.");
|
Log.i(TAG, "Starting the service.");
|
||||||
context.startService(new Intent(context, TrackRecordingService.class));
|
if (foreground) {
|
||||||
|
ContextCompat.startForegroundService(context, new Intent(context, TrackRecordingService.class));
|
||||||
|
} else {
|
||||||
|
context.startService(new Intent(context, TrackRecordingService.class));
|
||||||
|
}
|
||||||
|
|
||||||
startConnection(context);
|
startConnection(context);
|
||||||
}
|
}
|
||||||
@@ -88,7 +95,7 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death
|
|||||||
@Deprecated
|
@Deprecated
|
||||||
public void startAndBindWithCallback(Context context) {
|
public void startAndBindWithCallback(Context context) {
|
||||||
if (trackRecordingService == null) {
|
if (trackRecordingService == null) {
|
||||||
startAndBind(context);
|
startAndBind(context, false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (callback != null) {
|
if (callback != null) {
|
||||||
|
|||||||
+1
@@ -68,6 +68,7 @@ class TrackRecordingServiceNotificationManager implements SharedPreferences.OnSh
|
|||||||
}
|
}
|
||||||
|
|
||||||
void stop() {
|
void stop() {
|
||||||
|
cancelNotification();
|
||||||
PreferencesUtils.unregisterOnSharedPreferenceChangeListener(this);
|
PreferencesUtils.unregisterOnSharedPreferenceChangeListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user