@Override protected String doInBackground(Void... aNothing) { String res = null; try { res = getImage(); } catch (Exception e) { Log.i(TAG, "Problem while downloading image", e); } return res; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle aArgs = getArguments(); if (aArgs != null) { mTask = GlobalData.tasks.get(aArgs.getInt(GlobalData.TASK_ID)); } else { Log.w(TAG, "There is no arguments"); mTask = GlobalData.tasks.get(0); } }
// Only allowed in MODE_VIEW_TASK and MODE_VERIFICATION public void checkAnswer() { if (mTask.getCategory().charAt(0) == 'A') { checkAnswerA(); } else if (mTask.getCategory().charAt(0) == 'B') { checkAnswerB(); } else if (mTask.getCategory().charAt(0) == 'C') { checkAnswerC(); } else { Log.e( TAG, "Invalid category \"" + mTask.getCategory() + "\" for task № " + String.valueOf(mTask.getId())); } }
@Override public View onCreateView( LayoutInflater aInflater, ViewGroup aContainer, Bundle aSavedInstanceState) { View aView = aInflater.inflate(R.layout.task_page_item, aContainer, false); // Get controls mTaskHeaderView = (TextView) aView.findViewById(R.id.taskHeaderTextView); mTaskStatusView = (TextView) aView.findViewById(R.id.taskStatusTextView); mTaskViewAnimator = (ViewAnimator) aView.findViewById(R.id.taskViewAnimator); mRetryButton = (Button) aView.findViewById(R.id.retryButton); mTaskWebView = (TaskWebView) aView.findViewById(R.id.taskWebView); mAnswerTextView = (TextView) aView.findViewById(R.id.answerTextView); mBottomLayout = (RelativeLayout) aView.findViewById(R.id.bottomLayout); mAnswerEditText = (EditText) aView.findViewById(R.id.answerEditText); mAnswerButton = (Button) aView.findViewById(R.id.answerButton); // Set listeners mRetryButton.setOnClickListener(this); mAnswerButton.setOnClickListener(this); // Initialize controls mTaskHeaderView.setText( getString(R.string.task_header, mTask.getCategory(), mTask.getId() + 1)); updateStatus(); mTaskWebView.setInitialScale(30); WebSettings aSettings = mTaskWebView.getSettings(); aSettings.setBuiltInZoomControls(true); aSettings.setSupportZoom(true); aSettings.setUseWideViewPort(true); downloadImage(); switch (getCalculateActivity().getMode()) { case CalculateActivity.MODE_VIEW_TASK: mAnswerTextView.setVisibility(View.GONE); break; case CalculateActivity.MODE_TEST_TASK: case CalculateActivity.MODE_VERIFICATION: mAnswerTextView.setVisibility(View.GONE); mAnswerButton.setVisibility(View.GONE); break; case CalculateActivity.MODE_VIEW_RESULT: mBottomLayout.setVisibility(View.GONE); mAnswerTextView.setText(getString(R.string.answer, mTask.getAnswer())); break; } if (mTask.getCategory().charAt(0) == 'A') { mAnswerEditText.setRawInputType( InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL); mAnswerEditText.setSingleLine(true); } else if (mTask.getCategory().charAt(0) == 'B') { if (GlobalData.selectedLesson.getId().equals("russian")) { mAnswerEditText.setRawInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL); } else { mAnswerEditText.setRawInputType( InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL); } mAnswerEditText.setSingleLine(true); } else if (mTask.getCategory().charAt(0) == 'C') { mAnswerEditText.setRawInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL | InputType.TYPE_TEXT_FLAG_MULTI_LINE); mAnswerEditText.setSingleLine(false); } else { Log.e( TAG, "Invalid category \"" + mTask.getCategory() + "\" for task № " + String.valueOf(mTask.getId())); } return aView; }
private String getImage() throws IOException { String aFileName = GlobalData.selectedLesson.getId() + "/" + String.valueOf(mTask.getId() + 1) + ".png"; if (new File(GlobalData.PATH_ON_SD_CARD + aFileName).exists()) { Drawable aDrawable = Drawable.createFromPath(GlobalData.PATH_ON_SD_CARD + aFileName); if (aDrawable != null) { return "file://" + GlobalData.PATH_ON_SD_CARD + aFileName; } else { Log.i(TAG, "Invalid file on sdcard: " + GlobalData.PATH_ON_SD_CARD + aFileName); } } if (Utils.checkWifiOrNet(getCalculateActivity())) { // Download file URL aUrl = new URL(GlobalData.PATH_ON_NET + aFileName); HttpURLConnection aConnection = (HttpURLConnection) aUrl.openConnection(); aConnection.setReadTimeout(300000); aConnection.setConnectTimeout(300000); aConnection.setRequestMethod("GET"); aConnection.setDoInput(true); aConnection.connect(); InputStream in = aConnection.getInputStream(); boolean aFromInternet = true; try { byte[] aBuffer = new byte[4096]; new File(GlobalData.PATH_ON_SD_CARD + GlobalData.selectedLesson.getId()).mkdirs(); new File(GlobalData.PATH_ON_SD_CARD + ".nomedia").createNewFile(); FileOutputStream aNewFile = new FileOutputStream(GlobalData.PATH_ON_SD_CARD + aFileName); do { int aBytes = in.read(aBuffer); if (aBytes <= 0) { break; } aNewFile.write(aBuffer, 0, aBytes); } while (true); aNewFile.close(); aFromInternet = false; } catch (Exception e) { Log.w(TAG, "Problem while saving image on sd card", e); } try { in.close(); } catch (Exception e) { Log.w(TAG, "Problem while saving image on sd card", e); } if (aFromInternet) { return GlobalData.PATH_ON_NET + aFileName; } else { Drawable aDrawable = Drawable.createFromPath(GlobalData.PATH_ON_SD_CARD + aFileName); if (aDrawable != null) { return "file://" + GlobalData.PATH_ON_SD_CARD + aFileName; } else { Log.w( TAG, "Invalid file on SD card after downloading: " + GlobalData.PATH_ON_SD_CARD + aFileName); } } } return null; }