Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
[android] Properly synchronize saver service
Carsten Hartenfels20b5794d808a
diff --git a/packaging/android/apk/src/org/krita/android/DocumentSaverService.java b/packaging/android/apk/src/org/krita/android/DocumentSaverService.javaindex acf8d276f3..19718331b2 100644--- a/packaging/android/apk/src/org/krita/android/DocumentSaverService.java+++ b/packaging/android/apk/src/org/krita/android/DocumentSaverService.java@@ -35,8 +35,9 @@ public class DocumentSaverService extends Service {private static final String CANCEL_SAVING = "CANCEL_SAVING";private boolean mStartedInForeground = false;+ private boolean mSaveAgain = false;private boolean mKillProcess = false;- private Thread mDocSaverThread;+ private Thread mDocSaverThread = null;@Overridepublic void onCreate() {@@ -100,43 +101,88 @@ public class DocumentSaverService extends Service {mStartedInForeground = true;}- @Override- public int onStartCommand(Intent intent, int flags, int startId) {- Log.i(TAG, "[onStartCommand]");+ private synchronized void resetSaveAgain() {+ Log.i(TAG, "Start saving, foreground " + mStartedInForeground + " again " + mSaveAgain + " kill " + mKillProcess);+ mSaveAgain = false;+ }- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {- tryStartServiceInForegroundS();- } else {- tryStartServiceInForeground();+ private synchronized boolean shouldSaveAgain() {+ Log.i(TAG, "Saving done, foreground " + mStartedInForeground + " again " + mSaveAgain + " kill " + mKillProcess);+ return mSaveAgain && !mKillProcess;+ }++ private synchronized void finishSaverThread() {+ if (mStartedInForeground) {+ mStartedInForeground = false;+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {+ stopForeground(STOP_FOREGROUND_REMOVE);+ } else {+ stopForeground(true);+ }}+ mDocSaverThread = null;+ }- if (intent.getBooleanExtra(START_SAVING, false)) {- Log.i(TAG, "Starting Auto Save");- // let's not block the Android UI thread if we return to the app quickly+ private void runSaverThread() {+ do {+ resetSaveAgain();+ JNIWrappers.saveState();+ } while (shouldSaveAgain());+ finishSaverThread();+ }++ private synchronized void startSaving() {+ if (mDocSaverThread == null) {+ // Promote this to a so-called foreground service. This will keep+ // the service running even when the application isn't in the+ // foreground. For further foreground/background confusion, my+ // Samsung tablet shows the notification for this service under a+ // (pretty well-hidden) section called "background activities".+ if (mStartedInForeground) {+ Log.w(TAG, "Already started in foreground, should not happen");+ } else {+ Log.i(TAG, "Attempting to promote to foreground service");+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {+ tryStartServiceInForegroundS();+ } else {+ tryStartServiceInForeground();+ }+ }+ // No matter if foregrounding succeeded or not, we spawn a thread to+ // do the saving on. That will take care of un-foregrounding the+ // service and nulling the member variable once it's finished.+ Log.i(TAG, "Starting save thread");mDocSaverThread =new Thread(new Runnable() {@Overridepublic void run() {- Log.i(TAG, "... calling JNIWrappers.saveState()");- JNIWrappers.saveState();- Log.i(TAG, "... exited JNIWrappers.saveState(), started in foreground: " + mStartedInForeground);- if (mStartedInForeground) {- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {- stopForeground(STOP_FOREGROUND_REMOVE);- } else {- stopForeground(true);- }- // reset the value- mStartedInForeground = false;- }+ runSaverThread();}});mDocSaverThread.start();+ } else {+ Log.i(TAG, "Save thread already running, setting save again flag");+ mSaveAgain = true;+ }+ }++ @Override+ public int onStartCommand(Intent intent, int flags, int startId) {+ Log.i(TAG, "[onStartCommand]");+ if (intent.getBooleanExtra(START_SAVING, false)) {+ startSaving();+} else if (intent.getBooleanExtra(KILL_PROCESS, false)) {Log.i(TAG, "kill process command received");- mKillProcess = true;- if (mStartedInForeground) {++ boolean needsThread;+ synchronized (this) {+ mKillProcess = true;+ needsThread = mStartedInForeground;+ }++ if (needsThread) {// This can be async, because foreground service are promised to run beyond process death.// This has to be async, because we have to handle cancelling action.new Thread(new Runnable() {@@ -154,6 +200,7 @@ public class DocumentSaverService extends Service {waitForSaving();stopSelf();}+} else if (intent.getBooleanExtra(CANCEL_SAVING, false)) {Log.i(TAG, "cancel saving command received");// without this Android will think we crashed@@ -161,7 +208,11 @@ public class DocumentSaverService extends Service {// TODO: Think about atomicity, which Qt doesn't support for AndroidkillProcess(myPid());++ } else {+ Log.i(TAG, "nothing to do");}+return START_NOT_STICKY;}@@ -179,11 +230,16 @@ public class DocumentSaverService extends Service {super.onDestroy();}- private void waitForSaving() {+ private synchronized Thread getThreadToWaitOn() {Log.i(TAG, "[waitForSaving]: mStartedInForeground: " + mStartedInForeground);- if (mDocSaverThread != null) {+ return mDocSaverThread;+ }++ private void waitForSaving() {+ Thread thread = getThreadToWaitOn();+ if (thread != null) {try {- mDocSaverThread.join();+ thread.join();} catch (InterruptedException e) {Log.e(TAG, "Saving Interrupted :" + e.getMessage());}