/**
* Process - Acitivity of AndroidExam
* Copyright (C) 2011 CFuture . Chen Yong Jia
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.lurencun.androidexam.ui;
import java.io.File;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.os.Vibrator;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.lurencun.androidexam.R;
import com.lurencun.androidexam.db.DBAccess;
import com.lurencun.androidexam.entity.AnswerEntity;
import com.lurencun.androidexam.entity.ProfileEntity;
import com.lurencun.androidexam.entity.SubjectEntity;
import com.lurencun.androidexam.util.FilpperCallback;
import com.lurencun.androidexam.util.MyViewFilpper;
import com.lurencun.androidexam.util.CommonSetting;
import com.lurencun.androidexam.xml.ParseException;
import com.lurencun.androidexam.xml.XmlParser;
public class Process extends Activity implements FilpperCallback,OnClickListener{
/**
* 统计用到的成员变量
*/
private long baseLine = 0L;
private long elapsedTime = 0L;
private int score = 0;
private String fileId ;
private int progressValue = 1;
private int totalSubjectCount = 0;
private String currentFileName;
private int hadAnsweredCount = 0;
private int currentSubjectLocation = 0;
/**
* 因跨成员方法使用数据而设置的组件引用
*/
private ProgressBar progressBar = null;
private Chronometer chronometer = null;
private MyViewFilpper subjectFilpper = null;
private List<SubjectEntity> subjects = null;
private List<AnswerEntity> currentAnswerGroup = null;
/**
* 题目状态标识
*/
private boolean isNewSubject = false;
private boolean hadChosenAnswer = false;
/**
* 震动效果
*/
private Vibrator vibrator;
private long[] pattern = { 10,50};
private int iconOffset = 40;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.process);
currentFileName = getIntent().getStringExtra(CommonSetting.FileNameTag);
vibrator = (Vibrator)getSystemService(ContextWrapper.VIBRATOR_SERVICE);
fillHeader();
fillSubjects();
}
/**
* 填充考卷信息头
* @param path
*/
private void fillHeader(){
final TextView titleTextView = (TextView)findViewById(R.id.textpaperTitle);
final TextView totalTimeTextView = (TextView)findViewById(R.id.testpaperInfoTotalTime);
chronometer = (Chronometer)findViewById(R.id.testpaperInfoElapsedTime);
final TextView totalScoreTextView = (TextView)findViewById(R.id.testpaperInfoTotalScore);
progressBar = (ProgressBar)findViewById(R.id.testProgressBar);
final ImageButton answerButton = (ImageButton)findViewById(R.id.submitButton);
try {
ProfileEntity pe = XmlParser.getProfile(new File(CommonSetting.SDCardDiretory + currentFileName));
titleTextView.setText(String.format(getResources().getString(R.string.titleTag), pe.getTitle()));
totalTimeTextView.setText(getResources().getString(R.string.infoTotalTimeTag)+pe.getTotalTime());
totalScoreTextView.setText(getResources().getString(R.string.infoTotalScoreTag)+pe.getTotalScore());
chronometer.setFormat(getResources().getString(R.string.infoElapsedTimeTag)+"%s");
baseLine = SystemClock.elapsedRealtime();
progressBar.setProgress(30);
chronometer.setBase(baseLine);
chronometer.start();
fileId = String.valueOf(pe.getFileId());
} catch (ParseException e) {
Toast.makeText(this,getResources().getString(R.string.parseError),Toast.LENGTH_SHORT).show();
}
answerButton.setOnClickListener(this);
}
/**
* 填充题目
* <p>
* 目前实现的是,把题目全部从XML文件中读出,保存到subjects引用中。
* 题目动态生成,显示样式在R.layout.processitem中设置,由MyViewFilpper显示。
* </p>
*/
private void fillSubjects(){
subjectFilpper = (MyViewFilpper)findViewById(R.id.subjectViewFilpper);
try {
subjects = XmlParser.getSubjects(new File(CommonSetting.SDCardDiretory + currentFileName));
} catch (ParseException e) {
Toast.makeText(this,getResources().getString(R.string.parseError),Toast.LENGTH_SHORT).show();
}
for(SubjectEntity se : subjects){
++totalSubjectCount;
LinearLayout item = new LinearLayout(Process.this);
item.addView(View.inflate(Process.this, R.layout.processitem, null));
final TextView sContentView = (TextView)item.findViewById(R.id.subjectContent);
final TextView sIndexView = (TextView)item.findViewById(R.id.subjectIndex);
final String sContent = se.getContent();
int fontsize = sContent.length() > getResources().getInteger(R.integer.subjectContentMaxLength)
? getResources().getInteger(R.integer.subjectContentSmallFont)
: getResources().getInteger(R.integer.subjectContentNormalFont);
sContentView.setTextSize(fontsize);
sContentView.setText(sContent);
sContentView.setWidth(480);
sIndexView.setText(String.format(getResources().getString(R.string.subjectIndex), se.getIndex()+1));
LinearLayout answerLayout=(LinearLayout)item.findViewById(R.id.answerLayout);
createAnswerView(answerLayout,se.getType(),se.getAnswers());
subjectFilpper.addView(item);
}
updateProgress(1);
subjectFilpper.setTotalItemCounts(totalSubjectCount);
subjectFilpper.setCallback(this);
DBAccess.createProfile(this,fileId, totalSubjectCount);
ImageButton leftButton = (ImageButton)findViewById(R.id.leftButton);
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
subjectFilpper.showPrevious();
}
});
ImageButton rightButton = (ImageButton)findViewById(R.id.rightButton);
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
subjectFilpper.showNext();
}
});
}
/**
* 根据题目的类型,创建不同的答案。
* @param ll 答案区域的LinearLayout
* @param type 答案类型
*/
private void createAnswerView(LinearLayout ll,int type,List<AnswerEntity> answers){
switch(type){
case SubjectEntity.SINGLE:
createRadioGroup(ll,answers);
break;
case SubjectEntity.MULTLCHECK:
createMutilChecked(ll,answers);
break;
case SubjectEntity.TRUEORFALSE:
createTrueOrFalse(ll,answers);
break;
}
}
/**
* 当题目改变时,此方法被调用
* <p>
* 在此方法中,修改当前题目的答案选择情况。
* 标识是否为还没回答的题目。
* </p>
*/
@Override
public void subjectChange(int location) {
currentSubjectLocation = location;
hadChosenAnswer = false;
isNewSubject = true;
currentAnswerGroup = subjects.get(currentSubjectLocation).getAnswers();
for(AnswerEntity ao : currentAnswerGroup){
if(ao.isSelected()){
hadChosenAnswer = true;
isNewSubject