Example #1
0
 public void arriveOnPage() {
   ArrayList<ConnectionBean> connections = new ArrayList<ConnectionBean>();
   ConnectionBean.getAll(
       database.getReadableDatabase(),
       ConnectionBean.GEN_TABLE_NAME,
       connections,
       ConnectionBean.newInstance);
   Collections.sort(connections);
   connections.add(0, new ConnectionBean(this));
   int connectionIndex = 0;
   if (connections.size() > 1) {
     MostRecentBean mostRecent = getMostRecent(database.getReadableDatabase());
     if (mostRecent != null) {
       for (int i = 1; i < connections.size(); ++i) {
         if (connections.get(i).get_Id() == mostRecent.getConnectionId()) {
           connectionIndex = i;
           break;
         }
       }
     }
   }
   spinnerConnection.setAdapter(
       new ArrayAdapter<ConnectionBean>(
           this,
           R.layout.connection_list_entry,
           connections.toArray(new ConnectionBean[connections.size()])));
   spinnerConnection.setSelection(connectionIndex, false);
   selected = connections.get(connectionIndex);
   updateViewFromSelected();
   IntroTextDialog.showIntroTextIfNecessary(this, database, isFree && startingOrHasPaused);
   startingOrHasPaused = false;
 }
  public void createUserInDatabase(View view) {
    Database db = new Database(this, "DATABASE", null, 1);
    SQLiteDatabase database = db.getReadableDatabase();

    String usernameString = ((EditText) findViewById(R.id.userNameCreateUser)).getText().toString();
    String passwordString = ((EditText) findViewById(R.id.passwordCreateUser)).getText().toString();
    String firstNameString =
        ((EditText) findViewById(R.id.firstNameCreateUser)).getText().toString();
    String lastNameString = ((EditText) findViewById(R.id.lastNameCreateUser)).getText().toString();
    String retypePasswordString =
        ((EditText) findViewById(R.id.retypePasswordCreateUser)).getText().toString();
    if (usernameString.trim().length() < 5) {
      Toast.makeText(this, "Please have atleast 5 characters in username.", Toast.LENGTH_SHORT)
          .show();
      return;
    }

    if (passwordString.trim().length() < 8) {
      Toast.makeText(this, "Please have atleast 8 characters in password.", Toast.LENGTH_SHORT)
          .show();
      return;
    }

    if (firstNameString.trim().length() == 0 || lastNameString.trim().length() == 0) {
      Toast.makeText(this, "Please enter your first name and last name.", Toast.LENGTH_SHORT)
          .show();
      return;
    }

    if (passwordString.equals(retypePasswordString) == false) {
      Toast.makeText(this, "Passwords donot match.", Toast.LENGTH_SHORT).show();
      return;
    }

    String insertUserSQL = "INSERT INTO User VALUES(?,?,?,?)";

    String checkUserSQL = "SELECT * from User where Username = ?";
    if (database.rawQuery(checkUserSQL, new String[] {usernameString}).getCount() == 0) {

      database.execSQL(
          insertUserSQL,
          new Object[] {usernameString, passwordString, firstNameString, lastNameString});

      Toast.makeText(this, "User created", Toast.LENGTH_SHORT).show();
      CreateUser.this.finish();
      database.close();
      startActivity(new Intent(this, LoginActivity.class));

    } else {
      Toast.makeText(this, "Username is taken. Use different username", Toast.LENGTH_LONG).show();
      database.close();
    }
  }
Example #3
0
  private void refresh() {

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

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

    displayDateInterval();
    displayAmount();
    displayItems();
  }
Example #4
0
  @Override
  public Cursor query(
      @NonNull Uri uri,
      String[] projection,
      String selection,
      String[] selectionArgs,
      String sortOrder) {
    final SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

    switch (mMatcher.match(uri)) {
      case MAPS:
        queryBuilder.setTables(Database.Maps.TABLE_NAME);
        break;
      case MAPS_ID:
        queryBuilder.setTables(Database.Maps.TABLE_NAME);
        queryBuilder.appendWhere(Database.Maps.ID + "=" + uri.getLastPathSegment());
        break;
      case META:
        queryBuilder.setTables(Database.Meta.TABLE_NAME);
        break;
      case META_ID:
        queryBuilder.setTables(Database.Meta.TABLE_NAME);
        queryBuilder.appendWhere(Database.Meta.ID + "=" + uri.getLastPathSegment());
        break;
      case READINGS:
        queryBuilder.setTables(Database.Readings.TABLE_NAME);
        break;
      case READINGS_ID:
        queryBuilder.setTables(Database.Readings.TABLE_NAME);
        queryBuilder.appendWhere(Database.Readings.ID + "=" + uri.getLastPathSegment());
        break;
      case PROBES:
        queryBuilder.setTables(Database.Probes.TABLE_NAME);
        break;
      case PROBES_ID:
        queryBuilder.setTables(Database.Probes.TABLE_NAME);
        queryBuilder.appendWhere(Database.Probes.ID + "=" + uri.getLastPathSegment());
        break;
      default:
        throw new IllegalArgumentException("Unmatchable URI " + uri);
    }

    final Cursor cursor =
        queryBuilder.query(
            mDb.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
    cursor.setNotificationUri(getContext().getContentResolver(), uri);

    return cursor;
  }
 private void sendSpecialKeyAgain() {
   if (lastSentKey == null || lastSentKey.get_Id() != connection.getLastMetaKeyId()) {
     ArrayList<MetaKeyBean> keys = new ArrayList<MetaKeyBean>();
     Cursor c =
         database
             .getReadableDatabase()
             .rawQuery(
                 MessageFormat.format(
                     "SELECT * FROM {0} WHERE {1} = {2}",
                     MetaKeyBean.GEN_TABLE_NAME,
                     MetaKeyBean.GEN_FIELD__ID,
                     connection.getLastMetaKeyId()),
                 MetaKeyDialog.EMPTY_ARGS);
     MetaKeyBean.Gen_populateFromCursor(c, keys, MetaKeyBean.NEW);
     c.close();
     if (keys.size() > 0) {
       lastSentKey = keys.get(0);
     } else {
       lastSentKey = null;
     }
   }
   if (lastSentKey != null) canvas.getKeyboard().sendMetaKey(lastSentKey);
 }
  static ConnectionBean createLoadFromUri(Uri dataUri, Context ctx) {
    ConnectionBean connection = new ConnectionBean(ctx);
    if (dataUri == null) return connection;
    Database database = new Database(ctx);
    String host = dataUri.getHost();

    // Intent generated by connection shortcut widget
    if (host != null && host.startsWith(Constants.CONNECTION)) {
      int port = 0;
      int idx = host.indexOf(':');

      if (idx != -1) {
        try {
          port = Integer.parseInt(host.substring(idx + 1));
        } catch (NumberFormatException nfe) {
        }
        host = host.substring(0, idx);
      }

      if (connection.Gen_read(database.getReadableDatabase(), port)) {
        MostRecentBean bean = getMostRecent(database.getReadableDatabase());
        if (bean != null) {
          bean.setConnectionId(connection.get_Id());
          bean.Gen_update(database.getWritableDatabase());
          database.close();
        }
      }
      return connection;
    }

    // search based on nickname
    SQLiteDatabase queryDb = database.getReadableDatabase();
    String connectionName = dataUri.getQueryParameter(Constants.PARAM_CONN_NAME);
    Cursor nickCursor = null;
    if (connectionName != null)
      nickCursor =
          queryDb.query(
              GEN_TABLE_NAME,
              new String[] {GEN_FIELD__ID},
              GEN_FIELD_NICKNAME + " = ?",
              new String[] {connectionName},
              null,
              null,
              null);
    if (nickCursor != null && nickCursor.moveToFirst()) {
      // there could be many values, so we will just pick one
      Log.i(
          TAG,
          String.format(Locale.US, "Loding connection info from nickname: %s", connectionName));
      connection.Gen_populate(nickCursor, connection.Gen_columnIndices(nickCursor));
      nickCursor.close();
      database.close();
      return connection;
    }
    if (nickCursor != null) nickCursor.close();

    // search based on hostname
    Cursor hostCursor = null;
    if (host != null)
      hostCursor =
          queryDb.query(
              GEN_TABLE_NAME,
              new String[] {GEN_FIELD__ID},
              GEN_FIELD_ADDRESS + " = ?",
              new String[] {host},
              null,
              null,
              null);
    if (hostCursor != null && hostCursor.moveToFirst()) {
      Log.i(TAG, String.format(Locale.US, "Loding connection info from hostname: %s", host));
      connection.Gen_populate(hostCursor, connection.Gen_columnIndices(hostCursor));
      hostCursor.close();
      database.close();
      return connection;
    }
    if (hostCursor != null) hostCursor.close();
    database.close();
    return connection;
  }
Example #7
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;
  }