Exemplo n.º 1
0
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // make sure the orientation can't be changed once this activity started
    int currentOrientation = getResources().getConfiguration().orientation;
    if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    } else {
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
      sectionName = extras.getString(LibraryConstants.PREFS_KEY_FORM_NAME);
      sectionObjectString = extras.getString(LibraryConstants.PREFS_KEY_FORM_JSON);
      latitude = extras.getDouble(LibraryConstants.LATITUDE);
      longitude = extras.getDouble(LibraryConstants.LONGITUDE);
      elevation = extras.getDouble(LibraryConstants.ELEVATION);
    }

    try {
      if (sectionObjectString == null) {
        sectionObject = TagsManager.getInstance(this).getSectionByName(sectionName);
        // copy the section object, which will be kept around along the activity
        sectionObjectString = sectionObject.toString();
      }

      sectionObject = new JSONObject(sectionObjectString);
      formNames4Section = TagsManager.getFormNames4Section(sectionObject);
    } catch (Exception e) {
      e.printStackTrace();
    }
    setContentView(R.layout.form);
  }
Exemplo n.º 2
0
  private void saveAction() throws Exception {
    // if in landscape mode store last inserted info, since that fragment has not been stored
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
      FragmentDetail detailFragment =
          (FragmentDetail) getSupportFragmentManager().findFragmentById(R.id.detailFragment);
      if (detailFragment != null) {
        detailFragment.storeFormItems(false);
      }
    }

    // extract and check constraints
    List<String> availableFormNames = TagsManager.getFormNames4Section(sectionObject);
    String label = null;
    for (String formName : availableFormNames) {
      JSONObject formObject = TagsManager.getForm4Name(formName, sectionObject);

      JSONArray formItemsArray = TagsManager.getFormItems(formObject);

      int length = formItemsArray.length();
      String value = null;
      for (int i = 0; i < length; i++) {
        JSONObject jsonObject = formItemsArray.getJSONObject(i);

        String key = "-";
        if (jsonObject.has(TAG_KEY)) key = jsonObject.getString(TAG_KEY).trim();

        if (jsonObject.has(TAG_VALUE)) {
          value = jsonObject.getString(TAG_VALUE).trim();
        }
        if (jsonObject.has(TAG_ISLABEL)) {
          String isLabelStr = jsonObject.getString(TAG_ISLABEL).trim();
          boolean isLabel = Boolean.parseBoolean(isLabelStr);
          if (isLabel) label = value;
        }

        // inject latitude
        if (key.equals(LibraryConstants.LATITUDE)) {
          String latitudeString = String.valueOf(latitude);
          value = latitudeString;
          jsonObject.put(TAG_VALUE, latitudeString);
        }
        // inject longitude
        if (key.equals(LibraryConstants.LONGITUDE)) {
          String longitudeString = String.valueOf(longitude);
          value = longitudeString;
          jsonObject.put(TAG_VALUE, longitudeString);
        }

        Constraints constraints = FormUtilities.handleConstraints(jsonObject, null);
        if (value == null || !constraints.isValid(value)) {
          String constraintDescription = constraints.getDescription();
          String validfieldMsg = getString(R.string.form_field_check);
          String msg = Utilities.format(validfieldMsg, key, formName, constraintDescription);
          Utilities.messageDialog(this, msg, null);
          return;
        }
      }
    }

    // finally store data
    String sectionObjectString = sectionObject.toString();
    Date sqlDate = new Date(System.currentTimeMillis());
    String timestamp = LibraryConstants.TIME_FORMATTER_SQLITE.format(sqlDate);

    if (label == null) {
      label = sectionName;
    }
    String[] formDataArray = { //
      String.valueOf(longitude), //
      String.valueOf(latitude), //
      String.valueOf(elevation), //
      timestamp, //
      label, //
      "POI", //
      sectionObjectString
    };
    Intent intent = getIntent();
    intent.putExtra(LibraryConstants.PREFS_KEY_FORM, formDataArray);
    setResult(Activity.RESULT_OK, intent);
    finish();
  }