示例#1
0
  /**
   * Creates a new Survey component.
   *
   * @param container container the component will be placed in
   * @throws IOException
   */
  public Survey(ComponentContainer container) throws IOException {
    super(container);
    mainUI = container.$form();
    exportRoot =
        new java.io.File(Environment.getExternalStorageDirectory(), mainUI.getPackageName())
            + java.io.File.separator
            + "export";

    JsonParser parse = new JsonParser();
    webview = new WebView(container.$context());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setFocusable(true);
    webview.setVerticalScrollBarEnabled(true);

    container.$add(this);

    webview.setOnTouchListener(
        new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
              case MotionEvent.ACTION_UP:
                if (!v.hasFocus()) {
                  v.requestFocus();
                }
                break;
            }
            return false;
          }
        });

    // set the initial default properties. Height and Width
    // will be fill-parent, which will be the default for the web viewer.

    Width(LENGTH_FILL_PARENT);
    Height(LENGTH_FILL_PARENT);

    // set default survey style
    style = TEXTBOX; // default style

    // see if the Survey is created by someone tapping on a notification!
    // create the Survey and load it in the webviewer
    /* e.g.
     * value = {
     * "style": "multipleChoice",
     * "question": "What is your favorite food"
     * "options": ["apple", "banana", "strawberry", "orange"],
     * "surveyGroup": "MIT-food-survey"
     * }
     *
     */
    initValues = container.$form().getSurveyStartValues();
    Log.i(TAG, "startVal Suvey:" + initValues.toString());
    if (initValues != "") {

      JsonObject values = (JsonObject) parse.parse(initValues);
      this.style = values.get("style").getAsString();
      this.question = values.get("question").getAsString();
      this.surveyGroup = values.get("surveyGroup").getAsString();
      ArrayList<String> arrOptions = new ArrayList<String>();
      JsonArray _options = values.get("options").getAsJsonArray();
      for (int i = 0; i < _options.size(); i++) {
        arrOptions.add(_options.get(i).getAsString());
      }

      this.options = arrOptions;
      this.styleFromIntent = values.get("style").getAsString();
      Log.i(TAG, "Survey component got created");
    }
  }