Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ht.welcomehuman.presentation;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Handler;
- import android.support.v4.view.ViewPager;
- import android.util.Log;
- import android.view.View;
- import android.widget.TextView;
- import com.aldebaran.qi.Consumer;
- import com.aldebaran.qi.Function;
- import com.aldebaran.qi.Future;
- import com.aldebaran.qi.sdk.Qi;
- import com.aldebaran.qi.sdk.QiContext;
- import com.aldebaran.qi.sdk.QiSDK;
- import com.aldebaran.qi.sdk.RobotLifecycleCallbacks;
- import com.aldebaran.qi.sdk.builder.GoToBuilder;
- import com.aldebaran.qi.sdk.builder.ListenBuilder;
- import com.aldebaran.qi.sdk.builder.PhraseSetBuilder;
- import com.aldebaran.qi.sdk.builder.SayBuilder;
- import com.aldebaran.qi.sdk.object.actuation.Frame;
- import com.aldebaran.qi.sdk.object.actuation.FreeFrame;
- import com.aldebaran.qi.sdk.object.actuation.GoTo;
- import com.aldebaran.qi.sdk.object.conversation.Listen;
- import com.aldebaran.qi.sdk.object.conversation.ListenResult;
- import com.aldebaran.qi.sdk.object.conversation.Phrase;
- import com.aldebaran.qi.sdk.object.conversation.PhraseSet;
- import com.aldebaran.qi.sdk.object.conversation.Say;
- import com.ht.welcomehuman.R;
- import com.ht.welcomehuman.base.BaseActivity;
- import com.ht.welcomehuman.commons.core.PepperApp;
- import com.ht.welcomehuman.commons.models.ContentModel;
- import com.ht.welcomehuman.commons.models.SlideModel;
- import com.ht.welcomehuman.commons.models.SpeechModel;
- import com.ht.welcomehuman.main.MainActivity;
- import java.util.ArrayList;
- import java.util.List;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- /**
- * Created by Matija on 20-Mar-18.
- */
- public class PresentationActivity extends BaseActivity implements RobotLifecycleCallbacks, Say.OnStartedListener {
- @BindView(R.id.view_pager)
- ViewPager viewPager;
- private TextView noText, yesText;
- private ContentModel contentModel;
- private ArrayList<SlideModel> slideModels;
- private ArrayList<SpeechModel> speechesList = new ArrayList<>();
- private ArrayList<String> imagesList = new ArrayList<>();
- private PresentationAdapter mAdapter;
- private QiContext mQiContext;
- private GoTo goTo;
- private FreeFrame locationFreeFrame;
- private int currentSpeech, currentSpeechModel, backToStart = 0;
- private SpeechModel speech;
- private Dialog dialog;
- private List<Phrase> firstPhrase, secondPhrase;
- private Listen listen;
- private Future<ListenResult> listenResultFuture;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_presentation);
- ButterKnife.bind(this);
- QiSDK.register(this, this);
- contentModel = PepperApp.getContentModel();
- speechesList = findAllSpeeches(contentModel);
- imagesList = findAllImages(contentModel);
- goToLocation();
- }
- public void goToLocation() {
- locationFreeFrame = contentModel.getFreeFrame();//savedLocations.get(location);
- // Extract the Frame asynchronously.
- Future<Frame> frameFuture = locationFreeFrame.async().frame();
- frameFuture.andThenCompose(new Function<Frame, Future<Void>>() {
- @Override
- public Future<Void> execute(Frame frame) throws Throwable {
- // Create a GoTo action.
- goTo = GoToBuilder.with(mQiContext)
- .withFrame(frame)
- .build();
- // Display text when the GoTo action starts.
- goTo.setOnStartedListener(new GoTo.OnStartedListener() {
- @Override
- public void onStarted() {
- String message = "Moving...";
- Log.i("TEST", message);
- }
- });
- // Execute the GoTo action asynchronously.
- return goTo.async().run();
- }
- }).thenConsume(new Consumer<Future<Void>>() {
- @Override
- public void consume(Future<Void> future) throws Throwable {
- if (future.isSuccess()) {
- Log.e("Test", "Go to location success");
- Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
- .withText(getString(R.string.we_came_to_location) + contentModel.getTitle()) // Specify the action parameters.
- .buildAsync();
- sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
- @Override
- public void consume(Future<Say> future) throws Throwable {
- if (future.isSuccess()) {
- Say say = future.getValue();
- Future<Void> sayHello = say.async().run();
- sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
- @Override
- public void consume(Void say) throws Throwable {
- dialogQuestion(PresentationActivity.this);
- }
- }));
- }
- }
- }));
- } else if (future.hasError()) {
- Log.e("Test", "Go to location error", future.getError());
- goToLocation();
- }
- }
- });
- }
- private void goToStartLocation() {
- Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
- .withText(getString(R.string.robot_said_start_position)) // Specify the action parameters.
- .buildAsync();
- sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
- @Override
- public void consume(Future<Say> future) throws Throwable {
- if (future.isSuccess()) {
- // Access the value with stringFuture.get().
- Say say = future.getValue();
- Future<Void> sayHello = say.async().run();
- sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
- @Override
- public void consume(Void say) throws Throwable {
- startLocationMethod();
- }
- }));
- }
- }
- }));
- }
- private void startLocationMethod() {
- locationFreeFrame = PepperApp.getStartPosition();//savedLocations.get(location);
- // Extract the Frame asynchronously.
- Future<Frame> frameFuture = locationFreeFrame.async().frame();
- frameFuture.andThenCompose(new Function<Frame, Future<Void>>() {
- @Override
- public Future<Void> execute(Frame frame) throws Throwable {
- // Create a GoTo action.
- goTo = GoToBuilder.with(mQiContext)
- .withFrame(frame)
- .build();
- // Display text when the GoTo action starts.
- goTo.setOnStartedListener(new GoTo.OnStartedListener() {
- @Override
- public void onStarted() {
- String message = "Moving...";
- Log.i("TEST", message);
- }
- });
- // Execute the GoTo action asynchronously.
- return goTo.async().run();
- }
- }).thenConsume(new Consumer<Future<Void>>() {
- @Override
- public void consume(Future<Void> future) throws Throwable {
- if (future.isSuccess()) {
- Intent i = new Intent(PresentationActivity.this, MainActivity.class);
- startActivity(i);
- finish();
- } else if (future.hasError()) {
- Log.e("Test", "Go to location error", future.getError());
- goToStartLocation();
- }
- }
- });
- }
- public void setAdapterData() {
- mAdapter = new PresentationAdapter(this, imagesList);
- viewPager.setAdapter(mAdapter);
- viewPager.setOffscreenPageLimit(10);
- }
- public void startNextSpeech() {
- if (currentSpeechModel >= speechesList.size()) {
- dialogQuestionWhenItsOver(PresentationActivity.this);
- }
- if (!speechesList.isEmpty() && speechesList.size() > currentSpeechModel) {
- callbackSpeechOver();
- speech = speechesList.get(currentSpeechModel);
- nextItemInViewPager(currentSpeechModel);
- Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
- .withText(speech.getSpeechText()) // Specify the action parameters.
- .buildAsync();
- sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
- @Override
- public void consume(Future<Say> future) throws Throwable {
- if (future.isSuccess()) {
- Say say = future.getValue();
- Future<Void> sayHello = say.async().run();
- sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
- @Override
- public void consume(Void say) throws Throwable {
- currentSpeechModel++;
- final Handler handler = new Handler();
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- startNextSpeech();
- }
- }, 2000);
- }
- }));
- }
- }
- }));
- }
- }
- public void callbackSpeechOver() {
- currentSpeech++;
- }
- public void dialogQuestionWhenItsOver(Context context) {
- dialog = new Dialog(context, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
- dialog.setContentView(R.layout.dialog_question_over);
- dialog.setTitle(getString(R.string.question));
- yesText = (TextView) dialog.findViewById(R.id.yes);
- noText = (TextView) dialog.findViewById(R.id.no);
- yesText.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- listenResultFuture.cancel(true);
- Intent i = new Intent(PresentationActivity.this, MainActivity.class);
- startActivity(i);
- finish();
- dialog.dismiss();
- }
- });
- noText.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- listenResultFuture.cancel(true);
- Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
- .withText(getString(R.string.robot_said_start_position)) // Specify the action parameters.
- .buildAsync();
- sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
- @Override
- public void consume(Future<Say> future) throws Throwable {
- if (future.isSuccess()) {
- // Access the value with stringFuture.get().
- Say say = future.getValue();
- Future<Void> sayHello = say.async().run();
- sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
- @Override
- public void consume(Void say) throws Throwable {
- goToStartLocation();
- }
- }));
- }
- }
- }));
- }
- });
- dialog.show();
- dialog.setCancelable(false);
- Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
- .withText(getString(R.string.question)) // Specify the action parameters.
- .buildAsync();
- sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
- @Override
- public void consume(Future<Say> future) throws Throwable {
- if (future.isSuccess()) {
- // Access the value with stringFuture.get().
- Say say = future.getValue();
- Future<Void> sayHello = say.async().run();
- sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
- @Override
- public void consume(Void say) throws Throwable {
- }
- }));
- }
- }
- }));
- listenResultFuture = listen.async().run();
- listenResultFuture.thenConsume(Qi.onUiThread(new Consumer<Future<ListenResult>>() {
- @Override
- public void consume(Future<ListenResult> listenResultFuture) throws Throwable {
- ListenResult listenResult = listenResultFuture.getValue();
- if (listenResult.getHeardPhrase().getText().equals("wait")) {
- Intent i = new Intent(PresentationActivity.this, MainActivity.class);
- startActivity(i);
- finish();
- dialog.dismiss();
- } else if (listenResult.getHeardPhrase().getText().equals("start position")) {
- Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
- .withText(getString(R.string.robot_said_start_position)) // Specify the action parameters.
- .buildAsync();
- sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
- @Override
- public void consume(Future<Say> future) throws Throwable {
- if (future.isSuccess()) {
- Say say = future.getValue();
- Future<Void> sayHello = say.async().run();
- sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
- @Override
- public void consume(Void say) throws Throwable {
- goToStartLocation();
- }
- }));
- }
- }
- }));
- }
- }
- }));
- }
- public void dialogQuestion(Context context) {
- dialog = new Dialog(context, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
- dialog.setContentView(R.layout.dialog_question);
- dialog.setTitle(getString(R.string.do_you_wanna_go_back_to_start_position));
- yesText = (TextView) dialog.findViewById(R.id.yes);
- noText = (TextView) dialog.findViewById(R.id.no);
- yesText.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- listenResultFuture.cancel(true);
- setAdapterData();
- startNextSpeech();
- dialog.dismiss();
- }
- });
- noText.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- listenResultFuture.cancel(true);
- Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
- .withText(getString(R.string.robot_said_start_position)) // Specify the action parameters.
- .buildAsync();
- sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
- @Override
- public void consume(Future<Say> future) throws Throwable {
- if (future.isSuccess()) {
- // Access the value with stringFuture.get().
- Say say = future.getValue();
- Future<Void> sayHello = say.async().run();
- sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
- @Override
- public void consume(Void say) throws Throwable {
- goToStartLocation();
- }
- }));
- }
- }
- }));
- }
- });
- dialog.show();
- dialog.setCancelable(false);
- Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
- .withText(getString(R.string.do_you_wanna_go_back_to_start_position)) // Specify the action parameters.
- .buildAsync();
- sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
- @Override
- public void consume(Future<Say> future) throws Throwable {
- if (future.isSuccess()) {
- // Access the value with stringFuture.get().
- Say say = future.getValue();
- Future<Void> sayHello = say.async().run();
- sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
- @Override
- public void consume(Void say) throws Throwable {
- }
- }));
- }
- }
- }));
- listenResultFuture = listen.async().run();
- listenResultFuture.thenConsume(Qi.onUiThread(new Consumer<Future<ListenResult>>() {
- @Override
- public void consume(Future<ListenResult> listenResultFuture) throws Throwable {
- ListenResult listenResult = listenResultFuture.getValue();
- if (listenResult.getHeardPhrase().getText().equals("presentation")) {
- setAdapterData();
- startNextSpeech();
- dialog.dismiss();
- } else if (listenResult.getHeardPhrase().getText().equals("start position")) {
- Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
- .withText(getString(R.string.robot_said_start_position)) // Specify the action parameters.
- .buildAsync();
- sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
- @Override
- public void consume(Future<Say> future) throws Throwable {
- if (future.isSuccess()) {
- // Access the value with stringFuture.get().
- Say say = future.getValue();
- Future<Void> sayHello = say.async().run();
- sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
- @Override
- public void consume(Void say) throws Throwable {
- goToStartLocation();
- }
- }));
- }
- }
- }));
- }
- }
- }));
- }
- public void nextItemInViewPager(final int i) {
- viewPager.post(new Runnable() {
- @Override
- public void run() {
- viewPager.setCurrentItem(i);
- }
- });
- }
- @Override
- public void onRobotFocusGained(QiContext qiContext) {
- mQiContext = qiContext;
- PhraseSet phraseSetYes = PhraseSetBuilder.with(mQiContext) // Create the builder using the QiContext.
- .withTexts("presentation") // Add the phrases Pepper will listen to.
- .build(); // Build the PhraseSet.
- // Create the PhraseSet 2.
- PhraseSet phraseSetNo = PhraseSetBuilder.with(mQiContext) // Create the builder using the QiContext.
- .withTexts("start position") // Add the phrases Pepper will listen to.
- .build(); // Build the PhraseSet.
- // Create a new listen action.
- listen = ListenBuilder.with(mQiContext) // Create the builder with the QiContext.
- .withPhraseSet(phraseSetYes, phraseSetNo) // Set the PhraseSets to listen to.
- .build(); // Build the listen action.
- }
- @Override
- public void onRobotFocusLost() {
- }
- @Override
- public void onRobotFocusRefused(String reason) {
- }
- @Override
- public void onStarted() {
- }
- private ArrayList<SpeechModel> findAllSpeeches(ContentModel contentModels) {
- ArrayList<SpeechModel> speeches = new ArrayList();
- for (int j = 0; j < contentModels.getSlideModels().size(); j++) {
- speeches.add(new SpeechModel(contentModels.getSlideModels().get(j).getSpeech(), j));
- }
- return speeches;
- }
- private ArrayList<String> findAllImages(ContentModel contentModel) {
- ArrayList<String> imagesList = new ArrayList();
- for (int j = 0; j < contentModel.getSlideModels().size(); j++) {
- imagesList.add(contentModel.getSlideModels().get(j).getMediaModel().getImage());
- }
- return imagesList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement