diff options
author | Bilal Alsharifi <bilal.alsharifi@gmail.com> | 2019-08-21 15:56:40 -0400 |
---|---|---|
committer | Bilal Alsharifi <bilal.alsharifi@gmail.com> | 2019-08-21 15:56:40 -0400 |
commit | 27d4a441a228705117ca5a636f226fba479b462c (patch) | |
tree | 9919b1c7ce0a741a40b0ced86b36eba91c625e9e | |
parent | b7039e3c32488c67ad46e13bbf48137357f3e125 (diff) | |
download | sdl_android-27d4a441a228705117ca5a636f226fba479b462c.tar.gz |
Add blocking operations support
6 files changed, 80 insertions, 17 deletions
diff --git a/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/AsynchronousOperation.java b/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/AsynchronousOperation.java index 4ba98b384..e3d4ecc1f 100644 --- a/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/AsynchronousOperation.java +++ b/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/AsynchronousOperation.java @@ -37,20 +37,27 @@ package com.smartdevicelink.managers.screen.choiceset; import com.smartdevicelink.util.DebugTool; class AsynchronousOperation implements Runnable { + private static final String TAG = "AsynchronousOperation - "; + private Thread thread; + private Object lock; + private Boolean blocked; private Boolean executing; private Boolean finished; AsynchronousOperation() { + blocked = false; executing = false; finished = false; } @Override public void run() { - DebugTool.logInfo("Starting operation: " + toString()); + thread = Thread.currentThread(); + lock = new Object(); + DebugTool.logInfo(TAG + "Starting: " + toString()); if (isCancelled()) { finished = true; - DebugTool.logInfo("Operation was cancelled: " + toString()); + DebugTool.logInfo(TAG + "Operation was cancelled: " + toString()); return; } @@ -58,30 +65,55 @@ class AsynchronousOperation implements Runnable { } void finishOperation() { + unblock(); executing = false; finished = true; - DebugTool.logInfo("Finishing operation: " + toString()); + DebugTool.logInfo(TAG + "Finishing: " + toString()); } - public Boolean isAsynchronous() { - return true; - } - - public Boolean isExecuting() { + Boolean isExecuting() { return executing; } - public Boolean isFinished() { + Boolean isFinished() { return finished; } - public Boolean isCancelled() { - return Thread.currentThread().isInterrupted(); + void cancel(){ + thread.interrupt(); } + Boolean isCancelled() { + return thread.isInterrupted(); + } + + void block(){ + if (!blocked && !finished) { + blocked = true; + DebugTool.logInfo(TAG + "Blocking: " + toString()); + try { + synchronized (lock) { + lock.wait(); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + void unblock(){ + if (blocked) { + blocked = false; + DebugTool.logInfo(TAG + "Unblocking: " + toString()); + thread.interrupt(); + synchronized (lock) { + lock.notify(); + } + } + } @Override public String toString() { - return "Executing: " + executing + ", finished: " + finished + "."; + return this.getClass().getSimpleName() + " (OpThread:" + thread.getName() + ", currentThread:" + Thread.currentThread().getName() + ", blocked:" + blocked + ", executing:" + executing + ", finished:" + finished + ")"; } } diff --git a/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/CheckChoiceVROptionalOperation.java b/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/CheckChoiceVROptionalOperation.java index 4ac5cb659..1c427c16b 100644 --- a/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/CheckChoiceVROptionalOperation.java +++ b/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/CheckChoiceVROptionalOperation.java @@ -47,21 +47,24 @@ import com.smartdevicelink.util.DebugTool; import java.lang.ref.WeakReference; import java.util.Collections; -class CheckChoiceVROptionalOperation implements Runnable { +class CheckChoiceVROptionalOperation extends AsynchronousOperation { private CheckChoiceVROptionalInterface checkChoiceVROptionalInterface; private WeakReference<ISdl> internalInterface; private boolean isVROptional; CheckChoiceVROptionalOperation(ISdl internalInterface, CheckChoiceVROptionalInterface checkChoiceVROptionalInterface){ + super(); this.internalInterface = new WeakReference<>(internalInterface); this.checkChoiceVROptionalInterface = checkChoiceVROptionalInterface; } @Override public void run() { + CheckChoiceVROptionalOperation.super.run(); DebugTool.logInfo("Choice Operation: Executing check vr optional operation"); sendTestChoiceNoVR(); + block(); } /** @@ -117,6 +120,8 @@ class CheckChoiceVROptionalOperation implements Runnable { if (checkChoiceVROptionalInterface != null){ checkChoiceVROptionalInterface.onError(response.getInfo()); } + + CheckChoiceVROptionalOperation.super.finishOperation(); } } @@ -127,6 +132,8 @@ class CheckChoiceVROptionalOperation implements Runnable { if (checkChoiceVROptionalInterface != null){ checkChoiceVROptionalInterface.onError(info); } + + CheckChoiceVROptionalOperation.super.finishOperation(); } }); @@ -147,6 +154,8 @@ class CheckChoiceVROptionalOperation implements Runnable { if (checkChoiceVROptionalInterface != null){ checkChoiceVROptionalInterface.onCheckChoiceVROperationComplete(isVROptional); } + + CheckChoiceVROptionalOperation.super.finishOperation(); } @Override @@ -155,6 +164,8 @@ class CheckChoiceVROptionalOperation implements Runnable { if (checkChoiceVROptionalInterface != null){ checkChoiceVROptionalInterface.onError(info); } + + CheckChoiceVROptionalOperation.super.finishOperation(); } }); if (internalInterface.get() != null){ diff --git a/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/DeleteChoicesOperation.java b/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/DeleteChoicesOperation.java index fdb01ae38..2fc6c314d 100644 --- a/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/DeleteChoicesOperation.java +++ b/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/DeleteChoicesOperation.java @@ -48,13 +48,14 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; -class DeleteChoicesOperation implements Runnable { +class DeleteChoicesOperation extends AsynchronousOperation { private WeakReference<ISdl> internalInterface; private HashSet<ChoiceCell> cellsToDelete; private CompletionListener completionListener; DeleteChoicesOperation(ISdl internalInterface, HashSet<ChoiceCell> cellsToDelete, CompletionListener completionListener){ + super(); this.internalInterface = new WeakReference<>(internalInterface); this.cellsToDelete = cellsToDelete; this.completionListener = completionListener; @@ -62,8 +63,10 @@ class DeleteChoicesOperation implements Runnable { @Override public void run() { + DeleteChoicesOperation.super.run(); DebugTool.logInfo("Choice Operation: Executing delete choices operation"); sendDeletions(); + block(); } private void sendDeletions(){ @@ -84,6 +87,8 @@ class DeleteChoicesOperation implements Runnable { completionListener.onComplete(true); } DebugTool.logInfo("Successfully deleted choices"); + + DeleteChoicesOperation.super.finishOperation(); } @Override @@ -92,6 +97,8 @@ class DeleteChoicesOperation implements Runnable { completionListener.onComplete(false); } DebugTool.logError("Failed to delete choice: " + info + " | Corr ID: " + correlationId); + + DeleteChoicesOperation.super.finishOperation(); } @Override diff --git a/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/PreloadChoicesOperation.java b/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/PreloadChoicesOperation.java index f3d708998..99cd91a96 100644 --- a/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/PreloadChoicesOperation.java +++ b/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/PreloadChoicesOperation.java @@ -62,7 +62,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; -class PreloadChoicesOperation implements Runnable { +class PreloadChoicesOperation extends AsynchronousOperation { private WeakReference<ISdl> internalInterface; private WeakReference<FileManager> fileManager; @@ -74,6 +74,7 @@ class PreloadChoicesOperation implements Runnable { PreloadChoicesOperation(ISdl internalInterface, FileManager fileManager, DisplayCapabilities displayCapabilities, Boolean isVROptional, HashSet<ChoiceCell> cellsToPreload, CompletionListener listener){ + super(); this.internalInterface = new WeakReference<>(internalInterface); this.fileManager = new WeakReference<>(fileManager); this.displayCapabilities = displayCapabilities; @@ -84,6 +85,7 @@ class PreloadChoicesOperation implements Runnable { @Override public void run() { + PreloadChoicesOperation.super.run(); DebugTool.logInfo("Choice Operation: Executing preload choices operation"); preloadCellArtworks(new CompletionListener() { @Override @@ -91,6 +93,7 @@ class PreloadChoicesOperation implements Runnable { preloadCells(); } }); + block(); } void removeChoicesFromUpload(HashSet<ChoiceCell> choices){ @@ -161,11 +164,15 @@ class PreloadChoicesOperation implements Runnable { isRunning = false; DebugTool.logInfo("Finished pre loading choice cells"); completionListener.onComplete(true); + + PreloadChoicesOperation.super.finishOperation(); } @Override public void onError(int correlationId, Result resultCode, String info) { DebugTool.logError("There was an error uploading a choice cell: "+ info + " resultCode: " + resultCode); + + PreloadChoicesOperation.super.finishOperation(); } @Override diff --git a/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/PresentChoiceSetOperation.java b/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/PresentChoiceSetOperation.java index 40672a2df..3ca7af286 100644 --- a/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/PresentChoiceSetOperation.java +++ b/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/PresentChoiceSetOperation.java @@ -103,6 +103,7 @@ class PresentChoiceSetOperation extends AsynchronousOperation { DebugTool.logInfo("Choice Operation: Executing present choice set operation"); addListeners(); start(); + block(); } private void start(){ @@ -268,7 +269,7 @@ class PresentChoiceSetOperation extends AsynchronousOperation { } } else { DebugTool.logInfo("Canceling a choice set that has not yet been sent to Core"); - Thread.currentThread().interrupt(); + this.cancel(); } } diff --git a/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/PresentKeyboardOperation.java b/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/PresentKeyboardOperation.java index 188016129..ee5552fbe 100644 --- a/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/PresentKeyboardOperation.java +++ b/base/src/main/java/com/smartdevicelink/managers/screen/choiceset/PresentKeyboardOperation.java @@ -71,6 +71,7 @@ class PresentKeyboardOperation extends AsynchronousOperation { SdlMsgVersion sdlMsgVersion; PresentKeyboardOperation(ISdl internalInterface, KeyboardProperties originalKeyboardProperties, String initialText, KeyboardProperties customConfig, KeyboardListener keyboardListener, Integer cancelID){ + super(); this.internalInterface = new WeakReference<>(internalInterface); this.keyboardListener = keyboardListener; this.originalKeyboardProperties = originalKeyboardProperties; @@ -87,6 +88,7 @@ class PresentKeyboardOperation extends AsynchronousOperation { DebugTool.logInfo("Keyboard Operation: Executing present keyboard operation"); addListeners(); start(); + block(); } private void start(){ @@ -179,7 +181,7 @@ class PresentKeyboardOperation extends AsynchronousOperation { } } else { DebugTool.logInfo("Canceling a keyboard that has not yet been sent to Core."); - Thread.currentThread().interrupt(); + this.cancel(); } } @@ -215,6 +217,9 @@ class PresentKeyboardOperation extends AsynchronousOperation { @Override public void onError(int correlationId, Result resultCode, String info) { + if (listener != null){ + listener.onComplete(false); + } DebugTool.logError("Error Setting keyboard properties in present keyboard operation - choice manager - " + info); super.onError(correlationId, resultCode, info); } |