77import android .support .annotation .NonNull ;
88import android .support .v4 .app .ActivityCompat ;
99import android .support .v4 .content .ContextCompat ;
10+ import android .view .Gravity ;
1011import android .view .Menu ;
1112import android .view .MenuInflater ;
1213import android .view .MenuItem ;
1314import android .view .View ;
15+ import android .widget .AbsListView ;
1416import android .widget .AdapterView ;
1517import android .widget .ArrayAdapter ;
1618import android .widget .Button ;
19+ import android .widget .ListView ;
1720import android .widget .Spinner ;
21+ import android .widget .TextView ;
1822
1923import com .chattylabs .sdk .android .common .PermissionsHelper ;
2024import com .chattylabs .sdk .android .common .Tag ;
2832import com .chattylabs .sdk .android .voice .GoogleSpeechSynthesizer ;
2933import com .chattylabs .sdk .android .voice .VoiceMatch ;
3034import com .chattylabs .sdk .android .voice .VoiceMessage ;
35+ import com .chattylabs .sdk .android .voice .VoiceMismatch ;
3136import com .chattylabs .sdk .android .voice .VoiceNode ;
3237
3338import org .json .JSONArray ;
3641
3742import java .io .IOException ;
3843import java .io .InputStream ;
44+ import java .util .ArrayList ;
3945import java .util .Arrays ;
4046import java .util .LinkedHashMap ;
4147import java .util .List ;
4450
4551import dagger .android .support .DaggerAppCompatActivity ;
4652
47- public class MainActivity extends DaggerAppCompatActivity
53+ public class BuildFromJsonActivity extends DaggerAppCompatActivity
4854 implements ActivityCompat .OnRequestPermissionsResultCallback {
4955
50- private static final String TAG = Tag .make (ConversationCreatorActivity .class );
56+ private static final String TAG = Tag .make (CustomizeTheComponentActivity .class );
5157
5258 private static final String ANDROID = "Android" ;
5359 private static final String GOOGLE = "Google" ;
@@ -58,19 +64,21 @@ public class MainActivity extends DaggerAppCompatActivity
5864 addonMap .put (1 , GOOGLE );
5965 }
6066
61- private static String ADDON_TYPE = addonMap . get ( 0 ) ;
67+ private static String ADDON_TYPE = ANDROID ;
6268
6369 @ Inject ConversationalFlowComponent component ;
70+ private ThreadUtils .SerialThread serialThread ;
6471
6572 private Button proceed ;
6673 private Spinner addonSpinner ;
74+ private ListView conversationListView ;
6775 private ArrayAdapter <String > addonAdapter ;
68- private ThreadUtils . SerialThread serialThread ;
76+ private ArrayAdapter < String > listViewAdapter ;
6977
7078 @ Override
7179 public boolean onCreateOptionsMenu (Menu menu ) {
7280 MenuInflater inflater = getMenuInflater ();
73- inflater .inflate (R .menu .demo , menu );
81+ inflater .inflate (R .menu .demos , menu );
7482 return true ;
7583 }
7684
@@ -79,11 +87,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
7987 switch (item .getItemId ()) {
8088 case R .id .demo_conversation :
8189 ContextCompat .startActivity (this ,
82- new Intent (this , ConversationCreatorActivity .class ), null );
90+ new Intent (this , CustomizeTheComponentActivity .class ), null );
8391 return true ;
8492 case R .id .demo_components :
8593 ContextCompat .startActivity (this ,
86- new Intent (this , MainActivity .class ), null );
94+ new Intent (this , BuildFromJsonActivity .class ), null );
8795 return true ;
8896 default :
8997 return super .onOptionsItemSelected (item );
@@ -93,7 +101,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
93101 @ Override
94102 protected void onCreate (Bundle savedInstanceState ) {
95103 super .onCreate (savedInstanceState );
96- setContentView (R .layout .activity_main );
104+ setContentView (R .layout .activity_build_from_json );
97105
98106 initViews ();
99107 serialThread = ThreadUtils .newSerialThread ();
@@ -109,16 +117,28 @@ public void onDestroy() {
109117 }
110118
111119 private void initViews () {
112- addonSpinner = findViewById (R .id .addon );
113120 proceed = findViewById (R .id .proceed );
114121 proceed .setOnClickListener (v -> {
115122 loadConversation ();
116123 });
117124
125+ conversationListView = findViewById (R .id .conversation );
126+ listViewAdapter = new ArrayAdapter <>(this , R .layout .item_block ,
127+ R .id .conversation_item_text ,
128+ new ArrayList <>());
129+ listViewAdapter .setNotifyOnChange (true );
130+ TextView emptyView = new TextView (this );
131+ emptyView .setText ("Choose an addon and press on proceed" );
132+ emptyView .setLayoutParams (new AbsListView .LayoutParams (
133+ AbsListView .LayoutParams .WRAP_CONTENT ,
134+ AbsListView .LayoutParams .WRAP_CONTENT , Gravity .CENTER ));
135+ conversationListView .setAdapter (listViewAdapter );
136+ conversationListView .setEmptyView (emptyView );
137+
138+ addonSpinner = findViewById (R .id .addon );
118139 // Create an ArrayAdapter of the addons
119140 List <String > addonList = Arrays .asList (addonMap .values ().toArray (new String [addonMap .size ()]));
120- addonAdapter = new ArrayAdapter <>(this , android .R .layout .simple_spinner_item ,
121- addonList );
141+ addonAdapter = new ArrayAdapter <>(this , android .R .layout .simple_spinner_item , addonList );
122142 addonAdapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
123143 addonSpinner .setAdapter (addonAdapter );
124144 addonSpinner .setSelection (0 );
@@ -146,33 +166,68 @@ private void setup() {
146166
147167 @ SuppressLint ("MissingPermission" )
148168 private void loadConversation () {
169+ listViewAdapter .clear ();
170+ listViewAdapter .notifyDataSetChanged ();
149171 Conversation conversation = component .create (this );
150172 Flow flow = conversation .prepare ();
151173 try {
174+ String dots = ". . ." ;
152175 VoiceNode firstNode = null ;
153176 VoiceNode lastNode = null ;
154177 JSONArray array = new JSONArray (loadJSONFromAsset ());
155178 for (int a = 0 , size = array .length (); a < size ; a ++) {
156179
157180 JSONObject object = array .getJSONObject (a );
158181
159- JSONArray jsonArray = object .getJSONArray ("results" );
160- String [] stringArray = new String [jsonArray .length ()];
161- for (int i = 0 ; i < jsonArray .length (); i ++) {
162- stringArray [i ]= jsonArray .getString (i );
163- }
164-
182+ String text = object .getString ("message" );
165183 VoiceMessage message = VoiceMessage .newBuilder ()
166- .setText (object .getString ("message" )).build ();
167- VoiceMatch match = VoiceMatch .newBuilder ()
168- .setExpectedResults (stringArray ).build ();
184+ .setText (text )
185+ .setOnReady (() -> {
186+ runOnUiThread (() -> {
187+ listViewAdapter .add (text );
188+ });
189+ }).build ();
190+
191+ VoiceMatch matches = null ;
192+ VoiceMismatch noMatches = null ;
193+ if (object .has ("results" )) {
194+ JSONArray jsonArray = object .getJSONArray ("results" );
195+ String [] stringArray = new String [jsonArray .length ()];
196+ for (int i = 0 ; i < jsonArray .length (); i ++) {
197+ stringArray [i ] = jsonArray .getString (i );
198+ }
199+ matches = VoiceMatch .newBuilder ()
200+ .setOnReady (() -> {
201+ listViewAdapter .add (dots );
202+ })
203+ .setExpectedResults (stringArray )
204+ .setOnMatched (strings -> {
205+ listViewAdapter .remove (dots );
206+ if (strings != null ) {
207+ listViewAdapter .add (strings .get (0 ));
208+ conversation .next ();
209+ }
210+ })
211+ .build ();
212+ noMatches = VoiceMismatch .newBuilder ()
213+ .setOnNotMatched (strings -> {
214+ listViewAdapter .remove (dots );
215+ listViewAdapter .add ("You said: " + strings );
216+ listViewAdapter .add ("I did not expect that. Please try again!" );
217+ }).build ();
218+ }
169219
170220 conversation .addNode (message );
171- conversation .addNode (match );
221+ if (matches != null ) {
222+ conversation .addNode (matches );
223+ conversation .addNode (noMatches );
224+ }
172225 if (lastNode != null ) flow .from (lastNode ).to (message );
173226 if (firstNode == null ) firstNode = message ;
174- flow .from (message ).to (match );
175- lastNode = match ;
227+ if (matches != null ) {
228+ flow .from (message ).to (matches , noMatches );
229+ }
230+ lastNode = matches != null ? matches : message ;
176231 }
177232
178233 conversation .start (firstNode );
@@ -185,7 +240,7 @@ private void loadConversation() {
185240 public String loadJSONFromAsset () {
186241 String json = null ;
187242 try {
188- InputStream is = getAssets ().open ( "conversation.json" );
243+ InputStream is = getResources ().openRawResource ( R . raw . demo_conversation );
189244 int size = is .available ();
190245 byte [] buffer = new byte [size ];
191246 is .read (buffer );
0 commit comments