-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSpellingActivity.java
More file actions
220 lines (185 loc) · 6.96 KB
/
SpellingActivity.java
File metadata and controls
220 lines (185 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* Copyright (c) 2012, BuildmLearn Contributors listed at http://buildmlearn.org/people/
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the BuildmLearn nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
package com.buildmlearnstore.activities;
import java.util.ArrayList;
import java.util.Locale;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockActivity;
import com.buildmlearnstore.model.SpellingsModel;
import com.buildmlearnstore.model.WordModel;
public class SpellingActivity extends SherlockActivity implements
TextToSpeech.OnInitListener {
private TextToSpeech textToSpeech;
private ArrayList<WordModel> mWordList;
private int count=0;
private AlertDialog mAlert;
private TextView mTv_WordNumber;
private Button mBtn_Spell, mBtn_Skip;
private EditText mEt_Spelling;
private SeekBar mSb_SpeechRate;
private SpellingsModel mSpellingModel;
private int totalCorrect,totalWorng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spelling);
mBtn_Spell = (Button) findViewById(R.id.btn_ready);
mBtn_Skip = (Button) findViewById(R.id.btn_skip);
mSb_SpeechRate = (SeekBar) findViewById(R.id.sb_speech);
mTv_WordNumber = (TextView) findViewById(R.id.tv_word_number);
textToSpeech = new TextToSpeech(this, this);
mSpellingModel=SpellingsModel.getInstance();
mWordList = mSpellingModel.getSpellingsList();
count = mSpellingModel.getActiveCount();
totalCorrect=mSpellingModel.getTotalCorrect();
totalWorng=mSpellingModel.getTotalWrong();
mTv_WordNumber.setText("Word #" + (count + 1) + " of "
+ mWordList.size());
}
public void click(View view) {
switch (view.getId()) {
case R.id.btn_skip:
if (count < mWordList.size() - 1) {
count++;
mSpellingModel.setActiveCount(count);
mTv_WordNumber.setText("Word #" + (count + 1) + " of "
+ mWordList.size());
mBtn_Spell.setEnabled(false);
mBtn_Skip.setEnabled(false);
mBtn_Skip.setTextColor(Color.WHITE);
mBtn_Spell.setTextColor(Color.WHITE);
} else {
Intent resultIntent = new Intent(this, ScoreActivity.class);
resultIntent.putExtra("Activity",1);// 0: Quiz Template and 1: Spellings Template
startActivity(resultIntent);
finish();
}
break;
case R.id.btn_speak:
convertTextToSpeech(mWordList.get(count).getWord());
mBtn_Spell.setEnabled(true);
mBtn_Skip.setEnabled(true);
mBtn_Skip.setTextColor(Color.RED);
mBtn_Spell.setTextColor(Color.GREEN);
// mBtn_Spell.setBackgroundColor(Color.GREEN);
// mBtn_Skip.setBackgroundColor(Color.RED);
break;
case R.id.btn_ready:
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(
R.layout.dialog_spellinginput, null);
Builder builder = new Builder(this);
mAlert = builder.create();
mAlert.setCancelable(true);
mAlert.setView(textEntryView, 10, 10, 10, 10);
if (mAlert != null && !mAlert.isShowing()) {
mAlert.show();
}
mEt_Spelling = (EditText) mAlert.findViewById(R.id.et_spelling);
Button mBtn_Submit = (Button) mAlert.findViewById(R.id.btn_submit);
mBtn_Submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
submit();
}
});
break;
}
}
/**
* Releases the resources used by the TextToSpeech engine. It is good
* practice for instance to call this method in the onDestroy() method of an
* Activity so the TextToSpeech engine can be cleanly stopped.
*
* @see android.app.Activity#onDestroy()
*/
@Override
public void onDestroy() {
super.onDestroy();
textToSpeech.shutdown();
}
/**
* Speaks the string using the specified queuing strategy and speech
* parameters.
*/
private void convertTextToSpeech(String text) {
float speechRate = getProgressValue(mSb_SpeechRate.getProgress());
textToSpeech.setSpeechRate(speechRate);
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("error", "This Language is not supported");
}
} else {
Log.e("error", "Initilization Failed!");
}
}
public void submit() {
String input = mEt_Spelling.getText().toString().trim();
if (input == null || input.length() == 0) {
Toast.makeText(SpellingActivity.this, "Please enter the spelling",
Toast.LENGTH_SHORT).show();
} else {
mAlert.dismiss();
boolean isCorrect = false;
if (mEt_Spelling.getText().toString()
.equalsIgnoreCase(mWordList.get(count).getWord())) {
isCorrect = true;
mSpellingModel.setTotalCorrect(totalCorrect+1);
} else {
mSpellingModel.setTotalWrong(totalWorng+1);
}
Intent wordInfoIntent = new Intent(SpellingActivity.this,
WordInfoActivity.class);
wordInfoIntent.putExtra("result", isCorrect);
wordInfoIntent.putExtra("index", count);
wordInfoIntent.putExtra("word", input);
startActivity(wordInfoIntent);
finish();
}
}
private float getProgressValue(int percent) {
float temp = ((float) percent / 100);
float per = temp * 2;
return per;
}
}