Android Open Source - Android-Wizard-Framework Background Validation






From Project

Back to project page Android-Wizard-Framework.

License

The source code is released under:

MIT License

If you think the Android project Android-Wizard-Framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.hps.wizard.sample.states;
//from   ww w .  j  a  v  a  2  s . co  m
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;

import com.hps.wizard.StateFragment;
import com.hps.wizard.ValidationAsyncTask;
import com.hps.wizard.AbstractWizardActivity;
import com.hps.wizard.sample.R;

/**
 * This state simulates the ability to have validation run on a background thread. For instance, credentials may need to be verified against a server.
 */
public class BackgroundValidation extends StateFragment {
  /**
   * A simple task for simulating background processing. It counts down from start to zero and then passes or fails based on the flag given to the
   * constructor.
   */
  private class BackgroundTask extends ValidationAsyncTask {
    private int start;
    private Boolean shouldPass;

    /**
     * @param start
     *            the starting value
     * @param shouldPass
     *            true if validation should pass, false otherwise.
     */
    public BackgroundTask(int start, boolean shouldPass) {
      super();
      this.start = start;
      this.shouldPass = shouldPass;
    }

    @Override
    protected Boolean doInBackground(Object... params) {
      // Count down to zero, updating the progress as we go.
      for (int i = start; i > 0; i--) {
        try {
          publishProgress("" + i);
          Thread.sleep(500);
        } catch (InterruptedException e) {
          // ignore it
        }
      }
      return shouldPass;
    }
  }

  /**
   * Keys for saving the state of the UI across orientation changes.
   */
  private static final String SLIDER_VALUE = "sv";
  private static final String PASS_VALIDATION = "pv";
  private static final String CAN_GO_FORWARD = "cgf";

  private SeekBar durationSlider;
  private CheckBox passCheckBox;
  private boolean letUsGoForward;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_state_background_validation, container, false);

    durationSlider = (SeekBar) v.findViewById(R.id.durationSlider);
    passCheckBox = (CheckBox) v.findViewById(R.id.passCheckBox);

    /**
     * Make it so they have to press the button to enable the next button.
     */
    Button primer = (Button) v.findViewById(R.id.primerButton);
    primer.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        letUsGoForward = true;
        getWizardActivity().enableButton(AbstractWizardActivity.NEXT_BUTTON, true, BackgroundValidation.this);
      }
    });

    TextView descriptiveText = (TextView) v.findViewById(R.id.descriptiveText);
    CharSequence styledText = Html.fromHtml(getString(R.string.background_validation_instructions));
    descriptiveText.setText(styledText);

    Bundle arguments = getArguments();
    restoreFromState(arguments);

    return v;
  }

  /**
   * Restore saved data if there is any.
   * 
   * @param arguments
   *            The saved state as generated by {@link #getBundleToSave()} and passed to this fragment by the {@link AbstractWizardActivity}, or null if there isn't
   *            one.
   */
  private void restoreFromState(Bundle arguments) {
    if (arguments != null) {
      int progress = arguments.getInt(SLIDER_VALUE, -1);
      if (progress != -1) {
        durationSlider.setProgress(progress);
      }

      boolean passValidation = arguments.getBoolean(PASS_VALIDATION);
      passCheckBox.setChecked(passValidation);

      letUsGoForward = arguments.getBoolean(CAN_GO_FORWARD);
      if (letUsGoForward) {
        getWizardActivity().enableButton(AbstractWizardActivity.NEXT_BUTTON, letUsGoForward, this);
      }
    }
  }

  @Override
  public String getTitle() {
    return "Background Validation";
  }

  @Override
  public String getPreviousButtonLabel() {
    return getWizardActivity().getString(R.string.wizard_previous);
  }

  @Override
  public StateDefinition getNextState() {
    /**
     * The next state is always Seuss.
     */
    StateDefinition def = new StateDefinition(Seuss.class, null);

    return def;
  }

  @Override
  public boolean validate() {
    /**
     * All background validation is handled by the async task, so if this ever gets called we're in trouble.
     */
    return false;
  }

  @Override
  public boolean shouldValidateInBackground() {
    /**
     * Return true here to force background processing.
     */
    return true;
  }

  @Override
  protected boolean canGoForward() {
    /**
     * We canonly go forward if the button has been pressed.
     */
    return letUsGoForward;
  }

  @Override
  public Bundle getSavedInstanceState() {
    Bundle bundle = new Bundle();
    if (durationSlider != null) {
      bundle.putInt(SLIDER_VALUE, durationSlider.getProgress());
    }

    if (passCheckBox != null) {
      bundle.putBoolean(PASS_VALIDATION, passCheckBox.isChecked());
    }

    bundle.putBoolean(CAN_GO_FORWARD, letUsGoForward);

    return bundle;
  }

  @Override
  public ValidationAsyncTask getValidatorTask() {
    // make sure we're no longer allowed to go forward
    letUsGoForward = false;

    // create the task to be run
    int start = durationSlider.getProgress();
    boolean shouldPass = passCheckBox.isChecked();
    ValidationAsyncTask validator = new BackgroundTask(start, shouldPass);

    return validator;
  }

}




Java Source Code List

com.hps.wizard.AbstractWizardActivity.java
com.hps.wizard.StateFragment.java
com.hps.wizard.TaskCallback.java
com.hps.wizard.TaskFragment.java
com.hps.wizard.ValidationAsyncTask.java
com.hps.wizard.WizardActivity.java
com.hps.wizard.WizardDialog.java
com.hps.wizard.sample.activities.MainActivity.java
com.hps.wizard.sample.states.AreYouSure.java
com.hps.wizard.sample.states.BackgroundValidation.java
com.hps.wizard.sample.states.Choice.java
com.hps.wizard.sample.states.Instructions.java
com.hps.wizard.sample.states.MuppetShow.java
com.hps.wizard.sample.states.Results.java
com.hps.wizard.sample.states.SesameStreet.java
com.hps.wizard.sample.states.Seuss.java