Ejemplo n.º 1
0
  private void displayDateInterval() {

    if (dateSelector != null) {
      setHeaderTextLeft(Util.formatDateForView(dateSelector));
      //			setHeaderTextLeftColor(getBaseContext().getResources().getColor(R.color.wheat1));
      return;
    }

    if (dateRange != null) {
      // due to adding or removing items (which might influence the displayed
      // date range) always re-query the current range (using id)
      dateRange = DateIntervalTable.getDateInterval(dbCon, dateRange.id);
    }

    // dateRange might be null here, e.g. when a certain date interval is
    // currently used in the expense list UI, but gets deleted while being
    // in use; or it has not yet been set at all
    if (dateRange == null) {
      dateRange = DateIntervalTable.getDefaultDateInterval(dbCon);
    }

    boolean notFiltered = DateIntervalTable.isFullDateInterval(dbCon, dateRange.id);
    int color = 0;

    if (notFiltered) {
      // no date filter is active
      // display the eldest and the newest date
      color = getBaseContext().getResources().getColor(R.color.white);
    } else {
      color = getBaseContext().getResources().getColor(R.color.white);
    }

    String interval = null;

    if (dateRange.from != null && dateRange.to != null && dateRange.from.equals(dateRange.to)) {
      // same dates --> display only one
      interval = Util.formatDateForView(dateRange.from);
    } else if (dateRange.from == null || dateRange.to == null) {
      // one or both dates are null --> display only the given one (if any)
      interval = Util.formatDateForView(dateRange.from) + Util.formatDateForView(dateRange.to);
    } else {
      interval =
          Util.formatDateForView(dateRange.from) + " - " + Util.formatDateForView(dateRange.to);
    }

    setHeaderTextLeft(interval);
    setHeaderTextLeftColor(color);
  }
Ejemplo n.º 2
0
  @Override
  protected Dialog onCreateDialog(int id, final Bundle b) {

    // dialog might need to be re-created - e.g. when screen is turned
    // from portrait to landscape - then db and dbCon can be null and
    // need to be initialized

    if (db == null) {
      db = new Database(this);
    }

    if (dbCon == null) {
      dbCon = db.getReadableDatabase();
    }

    Dialog d = null;

    switch (id) {
      case DIALOG_ID_FILTER_SELECTION:
        final Expense expense = ExpenseTable.getExpense(dbCon, b.getLong(ITEM_ID));

        String categoryFilter = expense.category;
        String descriptionFilter = expense.description;
        String typeFilter = Util.formatTypeForView(getListView().getContext(), expense.type);
        String dateFilter = Util.formatDateForView(expense.date);

        ArrayList<String> filterDialogItems_list = new ArrayList<String>();
        ArrayList<Boolean> filterDialogCheckmarks_list = new ArrayList<Boolean>();

        final int[] position = new int[] {-1, -1, -1, -1};
        int pos = -1;

        if (typeFilter != null && typeFilter.length() != 0) {
          filterDialogItems_list.add(typeFilter);
          filterDialogCheckmarks_list.add(typeSelector != null);
          position[0] = ++pos;
        }
        if (dateFilter != null && dateFilter.length() != 0) {
          filterDialogItems_list.add(dateFilter);
          filterDialogCheckmarks_list.add(dateSelector != null);
          position[1] = ++pos;
        }
        if (categoryFilter != null && categoryFilter.length() != 0) {
          filterDialogItems_list.add(categoryFilter);
          filterDialogCheckmarks_list.add(categorySelector != null);
          position[2] = ++pos;
        }
        if (descriptionFilter != null && descriptionFilter.length() != 0) {
          filterDialogItems_list.add(descriptionFilter);
          filterDialogCheckmarks_list.add(descriptionSelector != null);
          position[3] = ++pos;
        }

        final String[] filterDialogItems = filterDialogItems_list.toArray(new String[] {});
        final boolean[] filterDialogCheckmarks = new boolean[filterDialogCheckmarks_list.size()];

        for (int i = 0; i < filterDialogCheckmarks_list.size(); i++) {
          filterDialogCheckmarks[i] = filterDialogCheckmarks_list.get(i).booleanValue();
        }

        d =
            new AlertDialog.Builder(this)
                .setTitle(R.string.expense_filterDialog_title)
                .setOnCancelListener(
                    new DialogInterface.OnCancelListener() {
                      public void onCancel(DialogInterface dialog) {
                        removeDialog(DIALOG_ID_FILTER_SELECTION);
                      }
                    })
                .setMultiChoiceItems(
                    filterDialogItems,
                    filterDialogCheckmarks,
                    new DialogInterface.OnMultiChoiceClickListener() {
                      public void onClick(
                          DialogInterface dialog, int whichButton, boolean isChecked) {
                        if (whichButton == position[2]) { // category
                          if (isChecked) {
                            categorySelector = expense.category;
                          } else {
                            categorySelector = null;
                          }
                        } else if (whichButton == position[0]) { // type
                          if (isChecked) {
                            typeSelector = expense.type;
                          } else {
                            typeSelector = null;
                          }
                        } else if (whichButton == position[3]) { // description
                          if (isChecked) {
                            descriptionSelector = expense.description;
                          } else {
                            descriptionSelector = null;
                          }
                        } else if (whichButton == position[1]) { // date
                          if (isChecked) {
                            dateSelector = expense.date;
                          } else {
                            dateSelector = null;
                          }
                        }
                      }
                    })
                .setPositiveButton(
                    R.string.button_ok,
                    new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                        refresh();
                        removeDialog(DIALOG_ID_FILTER_SELECTION);
                      }
                    })
                .create();

        break;

      case DIALOG_ID_DATE_INTERVAL_SELECTION:
        final DateIntervals dateIntervals = DateIntervalTable.getDateIntervals(dbCon);
        d =
            new AlertDialog.Builder(this)
                .setTitle(R.string.dateInterval_filterDialog_title)
                .setOnCancelListener(
                    new DialogInterface.OnCancelListener() {
                      public void onCancel(DialogInterface dialog) {
                        removeDialog(DIALOG_ID_DATE_INTERVAL_SELECTION);
                      }
                    })
                .setItems(
                    dateIntervals.names,
                    new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichPosition) {
                        dateRange.id = dateIntervals.ids[whichPosition];
                        // date range was set - so reset dateSelector (if set)
                        dateSelector = null;
                        refresh();
                        removeDialog(DIALOG_ID_DATE_INTERVAL_SELECTION);
                      }
                    })
                .setPositiveButton(
                    R.string.button_add,
                    new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                        // prepare data for hand-over to update-activity
                        Intent intent = new Intent(ExpenseListUI.this, DateIntervalUI.class);
                        intent.putExtra(DateIntervalUI.PARAM_MODE, DateIntervalUI.MODE_ADD);
                        // start update-activity
                        startActivityForResult(intent, REQUEST_CODE_ADD_DATE_INTERVAL);
                        removeDialog(DIALOG_ID_DATE_INTERVAL_SELECTION);
                      }
                    })
                .create();
        break;

      case DIALOG_ID_SEND_MAIL:
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.dialog_send_list, null);

        d =
            new AlertDialog.Builder(this)
                //					.setIcon(R.drawable.alert_dialog_icon)
                .setTitle(R.string.sendList_dialog_title)
                .setOnCancelListener(
                    new DialogInterface.OnCancelListener() {
                      public void onCancel(DialogInterface dialog) {
                        removeDialog(DIALOG_ID_SEND_MAIL);
                      }
                    })
                .setView(textEntryView)
                .setPositiveButton(
                    R.string.sendList_dialog_button_send,
                    new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {

                        // get mail address from UI
                        EditText adr = (EditText) textEntryView.findViewById(R.id.username_edit);
                        RadioButton qifRadioButton =
                            (RadioButton) textEntryView.findViewById(R.id.mail_format_qif);
                        RadioButton htmlRadioButton =
                            (RadioButton) textEntryView.findViewById(R.id.mail_format_html);
                        RadioButton xmlRadioButton =
                            (RadioButton) textEntryView.findViewById(R.id.mail_format_xml);
                        RadioButton csvRadioButton =
                            (RadioButton) textEntryView.findViewById(R.id.mail_format_csv);

                        String mailAddress = adr.getEditableText().toString();

                        String mailFormat = null;
                        if (qifRadioButton.isChecked()) {
                          mailFormat = QIF;
                        } else if (htmlRadioButton.isChecked()) {
                          mailFormat = HTML;
                        } else if (xmlRadioButton.isChecked()) {
                          mailFormat = XML;
                        } else if (csvRadioButton.isChecked()) {
                          mailFormat = CSV;
                        }

                        // store address (private mode) for later re-use
                        SharedPreferences settings = getPreferences(MODE_PRIVATE);
                        SharedPreferences.Editor prefEditor = settings.edit();
                        prefEditor.putString(PREF_MAIL_ADDRESS, mailAddress);
                        prefEditor.putString(PREF_MAIL_FORMAT, mailFormat);
                        prefEditor.commit();

                        String mimeType = "";

                        if (qifRadioButton.isChecked()) {
                          convertedList = Util.convertListToQifFile(getListView());
                          mimeType = "text/plain; charset=ISO-8859-1";
                        } else if (htmlRadioButton.isChecked()) {
                          convertedList = Util.convertListToHtmlFile(getListView());
                          mimeType = "text/html; charset=UTF-8";
                        } else if (xmlRadioButton.isChecked()) {
                          convertedList = Util.convertListToXmlFile(getListView());
                          mimeType = "text/xml; charset=UTF-8";
                        } else if (csvRadioButton.isChecked()) {
                          convertedList = Util.convertListToCsvFile(getListView());
                          mimeType = "text/plain; charset=UTF-8";
                        } else {
                          // should not happen
                          convertedList = null;
                        }

                        if (convertedList != null) {

                          Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                          emailIntent.setType(mimeType);
                          emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {mailAddress});
                          emailIntent.putExtra(
                              Intent.EXTRA_SUBJECT, getResources().getString(R.string.app_name));
                          emailIntent.putExtra(
                              Intent.EXTRA_TEXT,
                              getResources().getString(R.string.sendList_eMail_subject));
                          emailIntent.putExtra(
                              Intent.EXTRA_STREAM,
                              Uri.parse("file://" + convertedList.getAbsolutePath()));

                          startActivityForResult(
                              Intent.createChooser(
                                  emailIntent,
                                  getResources().getString(R.string.sendList_dialog_title)),
                              REQUEST_CODE_SEND_MAIL);
                        } else {
                          // present toast: file conversion failed
                        }

                        removeDialog(DIALOG_ID_SEND_MAIL);
                      }
                    })
                .setNegativeButton(
                    R.string.button_cancel,
                    new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                        removeDialog(DIALOG_ID_SEND_MAIL);
                      }
                    })
                .create();

        EditText adr = (EditText) textEntryView.findViewById(R.id.username_edit);
        RadioButton qifRadioButton = (RadioButton) textEntryView.findViewById(R.id.mail_format_qif);
        RadioButton htmlRadioButton =
            (RadioButton) textEntryView.findViewById(R.id.mail_format_html);
        RadioButton csvRadioButton = (RadioButton) textEntryView.findViewById(R.id.mail_format_csv);
        RadioButton xmlRadioButton = (RadioButton) textEntryView.findViewById(R.id.mail_format_xml);

        SharedPreferences settings = getPreferences(MODE_PRIVATE);

        // check if mail address already had been entered before
        // if yes, re-use the known address
        String mailAddress = settings.getString(PREF_MAIL_ADDRESS, "");
        adr.setText(mailAddress);

        // check if mail format already had been entered before
        // if yes, re-use the known format
        String mailFormat = settings.getString(PREF_MAIL_FORMAT, HTML);
        if (HTML.equals(mailFormat)) {
          htmlRadioButton.setChecked(true);
        } else if (XML.equals(mailFormat)) {
          xmlRadioButton.setChecked(true);
        } else if (CSV.equals(mailFormat)) {
          csvRadioButton.setChecked(true);
        } else if (QIF.equals(mailFormat)) {
          qifRadioButton.setChecked(true);
        }

        break;

      default:
        d = super.onCreateDialog(id, b);
        break;
    }
    return d;
  }