Created
April 2, 2013 17:53
-
-
Save FoamyGuy/5294505 to your computer and use it in GitHub Desktop.
Been There Done That QuizGameActivity modification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FetchImageTask extends AsyncTask<String, Void, Drawable>{ | |
private ImageSwitcher outputImg; | |
public FetchImageTask(ImageSwitcher is){ | |
outputImg = is; | |
} | |
@Override | |
protected Drawable doInBackground(String... url) { | |
try { | |
URL imageUrl = new URL(url[0]); | |
InputStream stream = imageUrl.openStream(); | |
Bitmap bitmap = BitmapFactory.decodeStream(stream); | |
Drawable image = new BitmapDrawable(outputImg.getContext().getResources(), bitmap); | |
return image; | |
} catch (MalformedURLException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(Drawable img){ | |
outputImg.setImageDrawable(img); | |
} | |
} | |
/**********************************************************/ | |
public class QuizGameActivity extends QuizActivity { | |
SharedPreferences mGameSettings; | |
//declare the editor up here and keep a reference to it, instead of creating | |
//a new one each time. | |
SharedPreferences.Editor editor; | |
Hashtable<Integer, Question> mQuestions; | |
private TextSwitcher mQuestionText; | |
private ImageSwitcher mQuestionImage; | |
private TextView scoreTxt; | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.game); | |
// Set up Text Switcher | |
mQuestionText = (TextSwitcher) findViewById(R.id.TextSwitcher_QuestionText); | |
mQuestionText.setFactory(new MyTextSwitcherFactory()); | |
// Set up Image Switcher | |
mQuestionImage = (ImageSwitcher) findViewById(R.id.ImageSwitcher_QuestionImage); | |
mQuestionImage.setFactory(new MyImageSwitcherFactory()); | |
scoreTxt = (TextView)findViewById(R.id.scoreTxt); | |
// Retrieve the shared preferences and editor. | |
mGameSettings = getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE); | |
editor = mGameSettings.edit(); | |
scoreTxt.setText(String.valueOf(mGameSettings.getInt(GAME_PREFERENCES_SCORE, 0))); | |
// Initialize question batch | |
mQuestions = new Hashtable<Integer, Question>(QUESTION_BATCH_SIZE); | |
// Load the questions | |
int startingQuestionNumber = mGameSettings.getInt(GAME_PREFERENCES_CURRENT_QUESTION, 0); | |
// If we're at the beginning of the quiz, initialize the Shared preferences | |
if (startingQuestionNumber == 0) { | |
//Editor editor = mGameSettings.edit(); | |
editor.putInt(GAME_PREFERENCES_CURRENT_QUESTION, 1); | |
editor.commit(); | |
startingQuestionNumber = 1; | |
} | |
try { | |
loadQuestionBatch(startingQuestionNumber); | |
} catch (Exception e) { | |
Log.e(DEBUG_TAG, "Loading initial question batch failed", e); | |
} | |
// If the question was loaded properly, display it | |
if (mQuestions.containsKey(startingQuestionNumber) == true) { | |
// Set the text of the textswitcher | |
mQuestionText.setCurrentText(getQuestionText(startingQuestionNumber)); | |
// Set the image of the imageswitcher | |
//Drawable image = getQuestionImageDrawable(startingQuestionNumber); | |
//mQuestionImage.setImageDrawable(image); | |
loadImage(startingQuestionNumber); | |
} else { | |
// Tell the user we don't have any new questions at this time | |
handleNoQuestions(); | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
super.onCreateOptionsMenu(menu); | |
getMenuInflater().inflate(R.menu.gameoptions, menu); | |
menu.findItem(R.id.help_menu_item).setIntent( | |
new Intent(this, QuizHelpActivity.class)); | |
menu.findItem(R.id.settings_menu_item).setIntent( | |
new Intent(this, QuizSettingsActivity.class)); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
super.onOptionsItemSelected(item); | |
startActivity(item.getIntent()); | |
return true; | |
} | |
public void onNoButton(View v) { | |
handleAnswerAndShowNextQuestion(false); | |
} | |
public void onYesButton(View v) { | |
handleAnswerAndShowNextQuestion(true); | |
} | |
/** | |
* | |
* Helper method to record the answer the user gave and load up the next question. | |
* | |
* @param bAnswer | |
* The answer the user gave | |
*/ | |
private void handleAnswerAndShowNextQuestion(boolean bAnswer) { | |
int curScore = mGameSettings.getInt(GAME_PREFERENCES_SCORE, 0); | |
int nextQuestionNumber = mGameSettings.getInt(GAME_PREFERENCES_CURRENT_QUESTION, 1) + 1; | |
//No need to get another reference, we can re-use the one we have from onCreate | |
//Editor editor = mGameSettings.edit(); | |
editor.putInt(GAME_PREFERENCES_CURRENT_QUESTION, nextQuestionNumber); | |
// Log the number of "yes" answers only | |
if (bAnswer == true) { | |
editor.putInt(GAME_PREFERENCES_SCORE, curScore + 1); | |
scoreTxt.setText(String.valueOf(curScore + 1)); | |
} | |
editor.commit(); | |
if (mQuestions.containsKey(nextQuestionNumber) == false) { | |
// Load next batch | |
try { | |
loadQuestionBatch(nextQuestionNumber); | |
} catch (Exception e) { | |
Log.e(DEBUG_TAG, "Loading updated question batch failed", e); | |
} | |
} | |
if (mQuestions.containsKey(nextQuestionNumber) == true) { | |
// Update question text | |
TextSwitcher questionTextSwitcher = (TextSwitcher) findViewById(R.id.TextSwitcher_QuestionText); | |
questionTextSwitcher.setText(getQuestionText(nextQuestionNumber)); | |
// Update question image | |
//Drawable image = getQuestionImageDrawable(nextQuestionNumber); | |
//mQuestionImage.setImageDrawable(image); | |
loadImage(nextQuestionNumber); | |
} else { | |
// Tell the user we don't have any new questions at this time | |
handleNoQuestions(); | |
} | |
} | |
/** | |
* Helper method to configure the question screen when no questions were found. | |
* Could be called for a variety of error cases, including no new questions, IO failures, or parser failures. | |
*/ | |
private void handleNoQuestions() { | |
TextSwitcher questionTextSwitcher = (TextSwitcher) findViewById(R.id.TextSwitcher_QuestionText); | |
questionTextSwitcher.setText(getResources().getText(R.string.no_questions)); | |
mQuestionImage.setImageResource(R.drawable.noquestion); | |
// Disable yes button | |
Button yesButton = (Button) findViewById(R.id.Button_Yes); | |
yesButton.setEnabled(false); | |
// Disable no button | |
Button noButton = (Button) findViewById(R.id.Button_No); | |
noButton.setEnabled(false); | |
} | |
/** | |
* Returns a {@code String} representing the text for a particular question number | |
* | |
* @param questionNumber | |
* The question number to get the text for | |
* @return The text of the question, or null if {@code questionNumber} not found | |
*/ | |
private String getQuestionText(Integer questionNumber) { | |
String text = null; | |
Question curQuestion = (Question) mQuestions.get(questionNumber); | |
if (curQuestion != null) { | |
text = curQuestion.mText; | |
} | |
return text; | |
} | |
/** | |
* Returns a {@code String} representing the URL to an image for a particular question | |
* | |
* @param questionNumber | |
* The question to get the URL for | |
* @return A {@code String} for the URL or null if none found | |
*/ | |
private String getQuestionImageUrl(Integer questionNumber) { | |
String url = null; | |
Question curQuestion = (Question) mQuestions.get(questionNumber); | |
if (curQuestion != null) { | |
url = curQuestion.mImageUrl; | |
} | |
return url; | |
} | |
/** | |
* Retrieves a {@code Drawable} object for a particular question | |
* | |
* @param questionNumber | |
* The question number to get the {@code Drawable} for | |
* @return A {@code Drawable} for the particular question, or a placeholder image if the loading failed or the question doesn't exist | |
*/ | |
private Drawable getQuestionImageDrawable(int questionNumber) { | |
Drawable image; | |
URL imageUrl; | |
try { | |
// Create a Drawable by decoding a stream from a remote URL | |
imageUrl = new URL(getQuestionImageUrl(questionNumber)); | |
InputStream stream = imageUrl.openStream(); | |
Bitmap bitmap = BitmapFactory.decodeStream(stream); | |
image = new BitmapDrawable(getResources(), bitmap); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
Log.e(DEBUG_TAG, "Decoding Bitmap stream failed"); | |
image = getResources().getDrawable(R.drawable.noquestion); | |
} | |
return image; | |
} | |
private void loadImage(int questionNumber){ | |
FetchImageTask fit = new FetchImageTask(mQuestionImage); | |
fit.execute(getQuestionImageUrl(questionNumber)); | |
} | |
/** | |
* Loads the XML into the {@see mQuestions} class member variable | |
* | |
* @param startQuestionNumber | |
* TODO: currently unused | |
* @throws XmlPullParserException | |
* Thrown if XML parsing errors | |
* @throws IOException | |
* Thrown if errors loading XML | |
*/ | |
private void loadQuestionBatch(int startQuestionNumber) throws XmlPullParserException, IOException { | |
// Remove old batch | |
mQuestions.clear(); | |
// TODO: Contact the server and retrieve a batch of question data, beginning at startQuestionNumber | |
XmlResourceParser questionBatch; | |
// BEGIN MOCK QUESTIONS | |
if (startQuestionNumber < 16) { | |
questionBatch = getResources().getXml(R.xml.samplequestions); | |
} else { | |
questionBatch = getResources().getXml(R.xml.samplequestions2); | |
} | |
// END MOCK QUESTIONS | |
// Parse the XML | |
int eventType = -1; | |
// Find Score records from XML | |
while (eventType != XmlResourceParser.END_DOCUMENT) { | |
if (eventType == XmlResourceParser.START_TAG) { | |
// Get the name of the tag (eg questions or question) | |
String strName = questionBatch.getName(); | |
if (strName.equals(XML_TAG_QUESTION)) { | |
String questionNumber = questionBatch.getAttributeValue(null, XML_TAG_QUESTION_ATTRIBUTE_NUMBER); | |
Integer questionNum = new Integer(questionNumber); | |
String questionText = questionBatch.getAttributeValue(null, XML_TAG_QUESTION_ATTRIBUTE_TEXT); | |
String questionImageUrl = questionBatch.getAttributeValue(null, XML_TAG_QUESTION_ATTRIBUTE_IMAGEURL); | |
// Save data to our hashtable | |
mQuestions.put(questionNum, new Question(questionNum, questionText, questionImageUrl)); | |
} | |
} | |
eventType = questionBatch.next(); | |
} | |
} | |
/** | |
* A switcher factory for use with the question image. Creates the next | |
* {@code ImageView} object to animate to | |
* | |
*/ | |
private class MyImageSwitcherFactory implements ViewSwitcher.ViewFactory { | |
public View makeView() { | |
ImageView imageView = (ImageView) LayoutInflater.from( | |
getApplicationContext()).inflate( | |
R.layout.image_switcher_view, mQuestionImage, false); | |
return imageView; | |
} | |
} | |
/** | |
* A switcher factory for use with the question text. Creates the next | |
* {@code TextView} object to animate to | |
* | |
*/ | |
private class MyTextSwitcherFactory implements ViewSwitcher.ViewFactory { | |
public View makeView() { | |
TextView textView = (TextView) LayoutInflater.from( | |
getApplicationContext()).inflate( | |
R.layout.text_switcher_view, mQuestionText, false); | |
return textView; | |
} | |
} | |
/** | |
* Object to manage the data for a single quiz question | |
* | |
*/ | |
private class Question { | |
@SuppressWarnings("unused") | |
int mNumber; | |
String mText; | |
String mImageUrl; | |
/** | |
* | |
* Constructs a new question object | |
* | |
* @param questionNum | |
* The number of this question | |
* @param questionText | |
* The text for this question | |
* @param questionImageUrl | |
* A valid image Url to display with this question | |
*/ | |
public Question(int questionNum, String questionText, String questionImageUrl) { | |
mNumber = questionNum; | |
mText = questionText; | |
mImageUrl = questionImageUrl; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment