Skip to content

Commit

Permalink
add support for focus/modes in the SGM
Browse files Browse the repository at this point in the history
  • Loading branch information
CaydenPierce committed Apr 2, 2023
1 parent adc7b76 commit db74b56
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void onComplete(@NonNull Task<String> task) {
EventBus.getDefault().post(new TranslateSuccessEvent(task.getResult()));
} else {
translatedText.setValue(new ResultOrError(null, task.getException()));
task.getException().printStackTrace();
// task.getException().printStackTrace();
}
// Update the list of downloaded models as more may have been
// automatically downloaded due to requested translation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.util.Log;

import com.teamopensmartglasses.sgmlib.FocusStates;
import com.teamopensmartglasses.translate.R;
import com.teamopensmartglasses.translate.events.ChangeSourceLanguageEvent;
import com.teamopensmartglasses.translate.events.ChangeTargetLanguageEvent;
Expand All @@ -22,8 +23,7 @@ public class TranslationService extends SmartGlassesAndroidService {

//our instance of the SGM library
public SGMLib sgmLib;
public boolean newScreen = true;
public String previousTranslation = "";
public FocusStates focusState;
TranslationBackend translationBackend;
public TranslationService(){
super(MainActivity.class,
Expand All @@ -37,6 +37,8 @@ public TranslationService(){
public void onCreate() {
super.onCreate();

focusState = FocusStates.OUT_FOCUS;

//Create SGMLib instance with context: this
sgmLib = new SGMLib(this);

Expand Down Expand Up @@ -69,34 +71,51 @@ public void onDestroy(){

public void processTranscriptionCallback(String transcript, long timestamp, boolean isFinal){
Log.d(TAG, "PROCESS TRANSCRIPTION CALLBACK. IS IT FINAL? " + isFinal + " " + transcript);
if(isFinal) translateText(transcript);

//don't execute if we're not in focus
if (!focusState.equals(FocusStates.IN_FOCUS)){
return;
}

if(isFinal){
translateText(transcript);
}
}

public void translateCommandCallback(String args, long commandTriggeredTime){
Log.d("TAG","Translation callback called");

//StartScrollingText lets us aquire SGM's mode
sgmLib.startScrollingText("Translation: ");
//request to be the in focus app so we can continue to show transcripts
sgmLib.requestFocus(this::focusChangedCallback);

//StartScrollingText to show our translation
// sgmLib.startScrollingText("Translation: ");

//Subscribe to transcription stream
sgmLib.subscribe(DataStreamType.TRANSCRIPTION_ENGLISH_STREAM, this::processTranscriptionCallback);
}

public void focusChangedCallback(FocusStates focusState){
Log.d(TAG, "Focus callback called with state: " + focusState);
this.focusState = focusState;

//StartScrollingText to show our translation
if (focusState.equals(FocusStates.IN_FOCUS)) {
sgmLib.startScrollingText("Translation: ");
}
}

public void translateText(String text){
translationBackend.sourceText.setValue(text);
translationBackend.translate();
}

@Subscribe
public void onTranslateSuccess(TranslateSuccessEvent event){

if(sgmLib == null) return;

if(newScreen) {
newScreen = false;
//String sourceCode = translationBackend.sourceLang.getValue().getCode();
//String targetCode = translationBackend.targetLang.getValue().getCode();
//sgmLib.startScrollingText("Translation: ");
if (sgmLib == null){
return;
}

sgmLib.pushScrollingText(event.message);
}

Expand Down

0 comments on commit db74b56

Please sign in to comment.