Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.11 KB | None | 0 0
  1. package com.ht.welcomehuman.presentation;
  2.  
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.support.v4.view.ViewPager;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.TextView;
  12.  
  13. import com.aldebaran.qi.Consumer;
  14. import com.aldebaran.qi.Function;
  15. import com.aldebaran.qi.Future;
  16. import com.aldebaran.qi.sdk.Qi;
  17. import com.aldebaran.qi.sdk.QiContext;
  18. import com.aldebaran.qi.sdk.QiSDK;
  19. import com.aldebaran.qi.sdk.RobotLifecycleCallbacks;
  20. import com.aldebaran.qi.sdk.builder.GoToBuilder;
  21. import com.aldebaran.qi.sdk.builder.ListenBuilder;
  22. import com.aldebaran.qi.sdk.builder.PhraseSetBuilder;
  23. import com.aldebaran.qi.sdk.builder.SayBuilder;
  24. import com.aldebaran.qi.sdk.object.actuation.Frame;
  25. import com.aldebaran.qi.sdk.object.actuation.FreeFrame;
  26. import com.aldebaran.qi.sdk.object.actuation.GoTo;
  27. import com.aldebaran.qi.sdk.object.conversation.Listen;
  28. import com.aldebaran.qi.sdk.object.conversation.ListenResult;
  29. import com.aldebaran.qi.sdk.object.conversation.Phrase;
  30. import com.aldebaran.qi.sdk.object.conversation.PhraseSet;
  31. import com.aldebaran.qi.sdk.object.conversation.Say;
  32. import com.ht.welcomehuman.R;
  33. import com.ht.welcomehuman.base.BaseActivity;
  34. import com.ht.welcomehuman.commons.core.PepperApp;
  35. import com.ht.welcomehuman.commons.models.ContentModel;
  36. import com.ht.welcomehuman.commons.models.SlideModel;
  37. import com.ht.welcomehuman.commons.models.SpeechModel;
  38. import com.ht.welcomehuman.main.MainActivity;
  39.  
  40. import java.util.ArrayList;
  41. import java.util.List;
  42.  
  43. import butterknife.BindView;
  44. import butterknife.ButterKnife;
  45.  
  46. /**
  47. * Created by Matija on 20-Mar-18.
  48. */
  49.  
  50. public class PresentationActivity extends BaseActivity implements RobotLifecycleCallbacks, Say.OnStartedListener {
  51.  
  52. @BindView(R.id.view_pager)
  53. ViewPager viewPager;
  54.  
  55. private TextView noText, yesText;
  56. private ContentModel contentModel;
  57. private ArrayList<SlideModel> slideModels;
  58. private ArrayList<SpeechModel> speechesList = new ArrayList<>();
  59. private ArrayList<String> imagesList = new ArrayList<>();
  60. private PresentationAdapter mAdapter;
  61. private QiContext mQiContext;
  62. private GoTo goTo;
  63. private FreeFrame locationFreeFrame;
  64. private int currentSpeech, currentSpeechModel, backToStart = 0;
  65. private SpeechModel speech;
  66. private Dialog dialog;
  67. private List<Phrase> firstPhrase, secondPhrase;
  68. private Listen listen;
  69. private Future<ListenResult> listenResultFuture;
  70.  
  71. @Override
  72. protected void onCreate(Bundle savedInstanceState) {
  73. super.onCreate(savedInstanceState);
  74. setContentView(R.layout.activity_presentation);
  75. ButterKnife.bind(this);
  76. QiSDK.register(this, this);
  77. contentModel = PepperApp.getContentModel();
  78. speechesList = findAllSpeeches(contentModel);
  79. imagesList = findAllImages(contentModel);
  80. goToLocation();
  81. }
  82.  
  83. public void goToLocation() {
  84. locationFreeFrame = contentModel.getFreeFrame();//savedLocations.get(location);
  85. // Extract the Frame asynchronously.
  86. Future<Frame> frameFuture = locationFreeFrame.async().frame();
  87. frameFuture.andThenCompose(new Function<Frame, Future<Void>>() {
  88. @Override
  89. public Future<Void> execute(Frame frame) throws Throwable {
  90. // Create a GoTo action.
  91. goTo = GoToBuilder.with(mQiContext)
  92. .withFrame(frame)
  93. .build();
  94.  
  95. // Display text when the GoTo action starts.
  96. goTo.setOnStartedListener(new GoTo.OnStartedListener() {
  97. @Override
  98. public void onStarted() {
  99. String message = "Moving...";
  100. Log.i("TEST", message);
  101. }
  102. });
  103.  
  104. // Execute the GoTo action asynchronously.
  105. return goTo.async().run();
  106. }
  107. }).thenConsume(new Consumer<Future<Void>>() {
  108. @Override
  109. public void consume(Future<Void> future) throws Throwable {
  110. if (future.isSuccess()) {
  111. Log.e("Test", "Go to location success");
  112. Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
  113. .withText(getString(R.string.we_came_to_location) + contentModel.getTitle()) // Specify the action parameters.
  114. .buildAsync();
  115. sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
  116. @Override
  117. public void consume(Future<Say> future) throws Throwable {
  118. if (future.isSuccess()) {
  119. Say say = future.getValue();
  120. Future<Void> sayHello = say.async().run();
  121. sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
  122. @Override
  123. public void consume(Void say) throws Throwable {
  124. dialogQuestion(PresentationActivity.this);
  125. }
  126. }));
  127. }
  128. }
  129. }));
  130. } else if (future.hasError()) {
  131. Log.e("Test", "Go to location error", future.getError());
  132. goToLocation();
  133. }
  134. }
  135. });
  136. }
  137.  
  138. private void goToStartLocation() {
  139. Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
  140. .withText(getString(R.string.robot_said_start_position)) // Specify the action parameters.
  141. .buildAsync();
  142. sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
  143. @Override
  144. public void consume(Future<Say> future) throws Throwable {
  145. if (future.isSuccess()) {
  146. // Access the value with stringFuture.get().
  147. Say say = future.getValue();
  148. Future<Void> sayHello = say.async().run();
  149. sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
  150. @Override
  151. public void consume(Void say) throws Throwable {
  152. startLocationMethod();
  153. }
  154. }));
  155. }
  156. }
  157. }));
  158. }
  159.  
  160. private void startLocationMethod() {
  161. locationFreeFrame = PepperApp.getStartPosition();//savedLocations.get(location);
  162. // Extract the Frame asynchronously.
  163. Future<Frame> frameFuture = locationFreeFrame.async().frame();
  164. frameFuture.andThenCompose(new Function<Frame, Future<Void>>() {
  165. @Override
  166. public Future<Void> execute(Frame frame) throws Throwable {
  167. // Create a GoTo action.
  168. goTo = GoToBuilder.with(mQiContext)
  169. .withFrame(frame)
  170. .build();
  171.  
  172. // Display text when the GoTo action starts.
  173. goTo.setOnStartedListener(new GoTo.OnStartedListener() {
  174. @Override
  175. public void onStarted() {
  176. String message = "Moving...";
  177. Log.i("TEST", message);
  178. }
  179. });
  180.  
  181. // Execute the GoTo action asynchronously.
  182. return goTo.async().run();
  183. }
  184. }).thenConsume(new Consumer<Future<Void>>() {
  185. @Override
  186. public void consume(Future<Void> future) throws Throwable {
  187. if (future.isSuccess()) {
  188. Intent i = new Intent(PresentationActivity.this, MainActivity.class);
  189. startActivity(i);
  190. finish();
  191. } else if (future.hasError()) {
  192. Log.e("Test", "Go to location error", future.getError());
  193. goToStartLocation();
  194. }
  195. }
  196. });
  197. }
  198.  
  199. public void setAdapterData() {
  200. mAdapter = new PresentationAdapter(this, imagesList);
  201. viewPager.setAdapter(mAdapter);
  202. viewPager.setOffscreenPageLimit(10);
  203. }
  204.  
  205. public void startNextSpeech() {
  206. if (currentSpeechModel >= speechesList.size()) {
  207. dialogQuestionWhenItsOver(PresentationActivity.this);
  208. }
  209. if (!speechesList.isEmpty() && speechesList.size() > currentSpeechModel) {
  210. callbackSpeechOver();
  211. speech = speechesList.get(currentSpeechModel);
  212. nextItemInViewPager(currentSpeechModel);
  213. Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
  214. .withText(speech.getSpeechText()) // Specify the action parameters.
  215. .buildAsync();
  216. sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
  217. @Override
  218. public void consume(Future<Say> future) throws Throwable {
  219. if (future.isSuccess()) {
  220. Say say = future.getValue();
  221. Future<Void> sayHello = say.async().run();
  222. sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
  223. @Override
  224. public void consume(Void say) throws Throwable {
  225. currentSpeechModel++;
  226. final Handler handler = new Handler();
  227. handler.postDelayed(new Runnable() {
  228. @Override
  229. public void run() {
  230. startNextSpeech();
  231. }
  232. }, 2000);
  233. }
  234. }));
  235. }
  236. }
  237. }));
  238. }
  239.  
  240. }
  241.  
  242. public void callbackSpeechOver() {
  243. currentSpeech++;
  244. }
  245.  
  246. public void dialogQuestionWhenItsOver(Context context) {
  247. dialog = new Dialog(context, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
  248. dialog.setContentView(R.layout.dialog_question_over);
  249. dialog.setTitle(getString(R.string.question));
  250. yesText = (TextView) dialog.findViewById(R.id.yes);
  251. noText = (TextView) dialog.findViewById(R.id.no);
  252. yesText.setOnClickListener(new View.OnClickListener() {
  253. @Override
  254. public void onClick(View v) {
  255. listenResultFuture.cancel(true);
  256. Intent i = new Intent(PresentationActivity.this, MainActivity.class);
  257. startActivity(i);
  258. finish();
  259. dialog.dismiss();
  260. }
  261. });
  262. noText.setOnClickListener(new View.OnClickListener() {
  263. @Override
  264. public void onClick(View v) {
  265. listenResultFuture.cancel(true);
  266. Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
  267. .withText(getString(R.string.robot_said_start_position)) // Specify the action parameters.
  268. .buildAsync();
  269. sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
  270. @Override
  271. public void consume(Future<Say> future) throws Throwable {
  272. if (future.isSuccess()) {
  273. // Access the value with stringFuture.get().
  274. Say say = future.getValue();
  275. Future<Void> sayHello = say.async().run();
  276. sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
  277. @Override
  278. public void consume(Void say) throws Throwable {
  279. goToStartLocation();
  280. }
  281. }));
  282. }
  283. }
  284. }));
  285. }
  286. });
  287. dialog.show();
  288. dialog.setCancelable(false);
  289.  
  290. Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
  291. .withText(getString(R.string.question)) // Specify the action parameters.
  292. .buildAsync();
  293. sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
  294. @Override
  295. public void consume(Future<Say> future) throws Throwable {
  296. if (future.isSuccess()) {
  297. // Access the value with stringFuture.get().
  298. Say say = future.getValue();
  299. Future<Void> sayHello = say.async().run();
  300. sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
  301. @Override
  302. public void consume(Void say) throws Throwable {
  303.  
  304. }
  305. }));
  306. }
  307. }
  308. }));
  309.  
  310.  
  311. listenResultFuture = listen.async().run();
  312. listenResultFuture.thenConsume(Qi.onUiThread(new Consumer<Future<ListenResult>>() {
  313. @Override
  314. public void consume(Future<ListenResult> listenResultFuture) throws Throwable {
  315. ListenResult listenResult = listenResultFuture.getValue();
  316. if (listenResult.getHeardPhrase().getText().equals("wait")) {
  317. Intent i = new Intent(PresentationActivity.this, MainActivity.class);
  318. startActivity(i);
  319. finish();
  320. dialog.dismiss();
  321. } else if (listenResult.getHeardPhrase().getText().equals("start position")) {
  322. Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
  323. .withText(getString(R.string.robot_said_start_position)) // Specify the action parameters.
  324. .buildAsync();
  325. sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
  326. @Override
  327. public void consume(Future<Say> future) throws Throwable {
  328. if (future.isSuccess()) {
  329. Say say = future.getValue();
  330. Future<Void> sayHello = say.async().run();
  331. sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
  332. @Override
  333. public void consume(Void say) throws Throwable {
  334. goToStartLocation();
  335. }
  336. }));
  337. }
  338. }
  339. }));
  340. }
  341. }
  342. }));
  343. }
  344.  
  345. public void dialogQuestion(Context context) {
  346. dialog = new Dialog(context, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
  347. dialog.setContentView(R.layout.dialog_question);
  348. dialog.setTitle(getString(R.string.do_you_wanna_go_back_to_start_position));
  349. yesText = (TextView) dialog.findViewById(R.id.yes);
  350. noText = (TextView) dialog.findViewById(R.id.no);
  351. yesText.setOnClickListener(new View.OnClickListener() {
  352. @Override
  353. public void onClick(View v) {
  354. listenResultFuture.cancel(true);
  355. setAdapterData();
  356. startNextSpeech();
  357. dialog.dismiss();
  358. }
  359. });
  360. noText.setOnClickListener(new View.OnClickListener() {
  361. @Override
  362. public void onClick(View v) {
  363. listenResultFuture.cancel(true);
  364. Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
  365. .withText(getString(R.string.robot_said_start_position)) // Specify the action parameters.
  366. .buildAsync();
  367. sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
  368. @Override
  369. public void consume(Future<Say> future) throws Throwable {
  370. if (future.isSuccess()) {
  371. // Access the value with stringFuture.get().
  372. Say say = future.getValue();
  373. Future<Void> sayHello = say.async().run();
  374. sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
  375. @Override
  376. public void consume(Void say) throws Throwable {
  377. goToStartLocation();
  378. }
  379. }));
  380. }
  381. }
  382. }));
  383. }
  384. });
  385. dialog.show();
  386. dialog.setCancelable(false);
  387.  
  388. Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
  389. .withText(getString(R.string.do_you_wanna_go_back_to_start_position)) // Specify the action parameters.
  390. .buildAsync();
  391. sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
  392. @Override
  393. public void consume(Future<Say> future) throws Throwable {
  394. if (future.isSuccess()) {
  395. // Access the value with stringFuture.get().
  396. Say say = future.getValue();
  397. Future<Void> sayHello = say.async().run();
  398. sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
  399. @Override
  400. public void consume(Void say) throws Throwable {
  401.  
  402. }
  403. }));
  404. }
  405. }
  406. }));
  407.  
  408.  
  409. listenResultFuture = listen.async().run();
  410. listenResultFuture.thenConsume(Qi.onUiThread(new Consumer<Future<ListenResult>>() {
  411. @Override
  412. public void consume(Future<ListenResult> listenResultFuture) throws Throwable {
  413. ListenResult listenResult = listenResultFuture.getValue();
  414. if (listenResult.getHeardPhrase().getText().equals("presentation")) {
  415. setAdapterData();
  416. startNextSpeech();
  417. dialog.dismiss();
  418. } else if (listenResult.getHeardPhrase().getText().equals("start position")) {
  419. Future<Say> sayActionFuture = SayBuilder.with(mQiContext) // Create a builder with the QiContext.
  420. .withText(getString(R.string.robot_said_start_position)) // Specify the action parameters.
  421. .buildAsync();
  422. sayActionFuture.thenConsume(Qi.onUiThread(new Consumer<Future<Say>>() {
  423. @Override
  424. public void consume(Future<Say> future) throws Throwable {
  425. if (future.isSuccess()) {
  426. // Access the value with stringFuture.get().
  427. Say say = future.getValue();
  428. Future<Void> sayHello = say.async().run();
  429. sayHello.andThenConsume(Qi.onUiThread(new Consumer<Void>() {
  430. @Override
  431. public void consume(Void say) throws Throwable {
  432. goToStartLocation();
  433. }
  434. }));
  435. }
  436. }
  437. }));
  438. }
  439. }
  440. }));
  441. }
  442.  
  443. public void nextItemInViewPager(final int i) {
  444. viewPager.post(new Runnable() {
  445. @Override
  446. public void run() {
  447. viewPager.setCurrentItem(i);
  448. }
  449. });
  450. }
  451.  
  452. @Override
  453. public void onRobotFocusGained(QiContext qiContext) {
  454. mQiContext = qiContext;
  455. PhraseSet phraseSetYes = PhraseSetBuilder.with(mQiContext) // Create the builder using the QiContext.
  456. .withTexts("presentation") // Add the phrases Pepper will listen to.
  457. .build(); // Build the PhraseSet.
  458.  
  459. // Create the PhraseSet 2.
  460. PhraseSet phraseSetNo = PhraseSetBuilder.with(mQiContext) // Create the builder using the QiContext.
  461. .withTexts("start position") // Add the phrases Pepper will listen to.
  462. .build(); // Build the PhraseSet.
  463.  
  464. // Create a new listen action.
  465. listen = ListenBuilder.with(mQiContext) // Create the builder with the QiContext.
  466. .withPhraseSet(phraseSetYes, phraseSetNo) // Set the PhraseSets to listen to.
  467. .build(); // Build the listen action.
  468. }
  469.  
  470. @Override
  471. public void onRobotFocusLost() {
  472.  
  473. }
  474.  
  475. @Override
  476. public void onRobotFocusRefused(String reason) {
  477.  
  478. }
  479.  
  480. @Override
  481. public void onStarted() {
  482.  
  483. }
  484.  
  485. private ArrayList<SpeechModel> findAllSpeeches(ContentModel contentModels) {
  486. ArrayList<SpeechModel> speeches = new ArrayList();
  487. for (int j = 0; j < contentModels.getSlideModels().size(); j++) {
  488. speeches.add(new SpeechModel(contentModels.getSlideModels().get(j).getSpeech(), j));
  489. }
  490. return speeches;
  491. }
  492.  
  493. private ArrayList<String> findAllImages(ContentModel contentModel) {
  494. ArrayList<String> imagesList = new ArrayList();
  495. for (int j = 0; j < contentModel.getSlideModels().size(); j++) {
  496. imagesList.add(contentModel.getSlideModels().get(j).getMediaModel().getImage());
  497. }
  498. return imagesList;
  499. }
  500. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement