Android Open Source - makler Alerts






From Project

Back to project page makler.

License

The source code is released under:

GNU General Public License

If you think the Android project makler 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 pl.net.newton.Makler.ui;
//from   w ww .ja v  a2s  .  c o  m
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import pl.net.newton.Makler.R;
import pl.net.newton.Makler.db.alert.Alert;
import pl.net.newton.Makler.db.alert.AlertBuilder;
import pl.net.newton.Makler.db.alert.AlertsDao;
import pl.net.newton.Makler.db.alert.Event;
import pl.net.newton.Makler.db.alert.Subject;
import pl.net.newton.Makler.db.quote.Quote;
import pl.net.newton.Makler.db.quote.QuoteField;
import pl.net.newton.Makler.db.quote.QuotesDao;
import pl.net.newton.Makler.history.service.HistoryService;
import pl.net.newton.Makler.ui.adapter.AlertsAdapter;
import pl.net.newton.Makler.ui.adapter.QuotesAdapter;

public class Alerts extends AbstractActivity implements OnClickListener, OnItemSelectedListener {

  private static final int EDIT_RESULT = 100;

  private ListView alertList;

  private List<Alert> alerts;

  private Quote quote;

  private AlertsDao alertsDb;

  private QuotesDao quotesDb;

  private String symbol;

  private int alertId;

  private Spinner subjectSpinner;

  private Spinner eventSpinner;

  private EditText valueEditText;

  private CheckBox percentCheckBox;

  private ArrayAdapter<CharSequence> eventQuoteAdapter, eventNonQuoteAdapter;

  private boolean eventInitialized;

  private Alert alert;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    symbol = getIntent().getStringExtra("symbol");
    alertId = getIntent().getIntExtra("alertId", 0);

    setContentView(R.layout.alerts);

    Button btn = (Button) findViewById(R.id.addAlert);
    btn.setOnClickListener(this);
    if (alertId != 0) {
      btn.setText("Modyfikuj");
    }
    alertList = (ListView) findViewById(R.id.alertList);
    alertList.setOnCreateContextMenuListener(this);

    subjectSpinner = (Spinner) findViewById(R.id.alertSubject);
    eventSpinner = (Spinner) findViewById(R.id.alertEvent);
    valueEditText = (EditText) findViewById(R.id.alertValue);
    percentCheckBox = (CheckBox) findViewById(R.id.alertPercent);

    eventQuoteAdapter = ArrayAdapter.createFromResource(this, R.array.alert_events_quote_strings,
        android.R.layout.simple_spinner_item);
    eventQuoteAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    eventNonQuoteAdapter = ArrayAdapter.createFromResource(this, R.array.alert_events_non_quote_strings,
        android.R.layout.simple_spinner_item);
    eventNonQuoteAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    subjectSpinner.setOnItemSelectedListener(this);
    eventSpinner.setOnItemSelectedListener(this);

    registerForContextMenu(alertList);
    populateSubjectSpinner();
    populateEventsSpinner();
    setTitle("Alerty");
  }

  private void populateSubjectSpinner() {
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.alert_subjects_strings, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    subjectSpinner.setAdapter(adapter);
  }

  private void populateEventsSpinner() {
    Subject subject = Subject.getFromLabel(this, (String) subjectSpinner.getSelectedItem());
    if (subject == Subject.KURS) {
      eventSpinner.setAdapter(eventQuoteAdapter);
    } else {
      eventSpinner.setAdapter(eventNonQuoteAdapter);
    }
  }

  public void onClick(View button) {
    Subject subject = Subject.getFromLabel(this, (String) subjectSpinner.getSelectedItem());
    Event event = Event.getFromLabel(this, (String) eventSpinner.getSelectedItem());
    String value = valueEditText.getText().toString();
    Boolean percent = percentCheckBox.isChecked();

    if (button.getId() == R.id.addAlert) {
      if (value.length() == 0) {
        return;
      }

      AlertBuilder builder = new AlertBuilder();
      builder.setQuote(quote).setSubject(subject).setEvent(event).setValue(new BigDecimal(value))
          .setPercent(percent);
      if (event.isBaseValueRequired()) {
        builder.setBaseValue(subject.getValue(quote));
      }
      if (this.alertId != 0) {
        builder.setId(alertId).setUsed(false);
        Alert newAlert = builder.build();
        alertsDb.update(newAlert);
        finish();
        return;
      } else {
        Alert newAlert = builder.build();
        if (alertsDb.create(newAlert)) {
          subjectSpinner.setSelection(0);
          eventSpinner.setSelection(0);
          valueEditText.setText("");
          percentCheckBox.setChecked(false);
        } else {
          showMessage("Nie uda?o si? doda? alertu");
        }
      }
      refreshList();
    }
  }

  public void refreshList() {
    alerts = alertsDb.getByQuote(quote);
    alertList.setAdapter(new AlertsAdapter(this, alerts));
    View label = findViewById(R.id.alertListLabel);
    if (alerts.isEmpty()) {
      label.setVisibility(View.INVISIBLE);
    } else {
      label.setVisibility(View.VISIBLE);
    }
  }

  @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    menu.add(0, ContextMenuItem.EDIT, 0, "Edytuj");
    menu.add(0, ContextMenuItem.DELETE, 0, "Usu?");
  }

  @Override
  public boolean onContextItemSelected(MenuItem item) {
    final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
        .getMenuInfo();
    final Alert selectedAlert = alerts.get(info.position);
    final int id = item.getItemId();

    if (id == ContextMenuItem.EDIT) {
      Intent intent = new Intent(this, getClass());
      intent.putExtra("alertId", selectedAlert.getId());
      startActivityForResult(intent, EDIT_RESULT);
    } else if (id == ContextMenuItem.DELETE) {
      alertsDb.delete(selectedAlert.getId());
      refreshList();
    }
    return true;
  }

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == EDIT_RESULT) {
      refreshList();
    }
  }

  private static final class ContextMenuItem {
    static final int DELETE = 0;

    static final int EDIT = 1;

    private ContextMenuItem() {
    }
  }

  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if (parent.getId() == R.id.alertSubject) {
      populateEventsSpinner();

      if (alertId != 0 && !eventInitialized) {
        int eventPos;
        if (alert.getSubject() == Subject.KURS) {
          eventPos = eventQuoteAdapter.getPosition(alert.getEvent().getLabel(this));
        } else {
          eventPos = eventNonQuoteAdapter.getPosition(alert.getEvent().getLabel(this));
        }
        eventSpinner.setSelection(eventPos);
        eventInitialized = true;
      }
    }
    setCheckboxEnabled();
  }

  private void setCheckboxEnabled() {
    Event event = Event.getFromLabel(this, (String) eventSpinner.getSelectedItem());
    if (event == Event.SPA_O || event == Event.WZR_O) {
      percentCheckBox.setEnabled(true);
    } else {
      percentCheckBox.setEnabled(false);
    }
  }

  @Override
  public void onNothingSelected(AdapterView<?> view) {
    // do nothing
  }

  private void populateQuoteItem() {
    List<Quote> quotes = new ArrayList<Quote>();
    quotes.add(quote);
    QuotesAdapter adapter = new QuotesAdapter(this, quotes);
    ViewGroup layout = (ViewGroup) findViewById(R.id.alertQuoteItem);
    layout.addView(adapter.getView(0, null, null), 0);
  }

  @Override
  protected void initUi(SQLiteDatabase sqlDb, HistoryService historyService) {
    this.quotesDb = new QuotesDao(sqlDb, this);
    this.alertsDb = new AlertsDao(sqlDb, this);
    if (alertId != 0) {
      setAlertData(alertId);
    } else {
      setSymbolData(symbol);
    }
    populateQuoteItem();
    if (alertId == 0) {
      refreshList();
    } else {
      findViewById(R.id.alertListLabel).setVisibility(View.GONE);
    }
  }

  private void setSymbolData(String symbol) {
    this.quote = quotesDb.getQuoteBySymbol(symbol);
    TextView txt = (TextView) findViewById(R.id.alertQuoteName);
    txt.setText(quote.get(QuoteField.NAME));
  }

  private void setAlertData(int alertId) {
    alert = alertsDb.getById(alertId);
    this.quote = alert.getQuote();

    TextView txt = (TextView) findViewById(R.id.alertQuoteName);
    txt.setText(quote.get(QuoteField.NAME));

    eventInitialized = false;
    subjectSpinner.setSelection(alert.getSubject().ordinal());
    valueEditText.setText(alert.getAlertValue().getValue().toString());
    percentCheckBox.setChecked(alert.getAlertValue().isPercent());
    setCheckboxEnabled();
  }
}




Java Source Code List

pl.net.newton.Makler.common.Configuration.java
pl.net.newton.Makler.common.DateFormatUtils.java
pl.net.newton.Makler.common.GpwUtils.java
pl.net.newton.Makler.common.LocaleUtils.java
pl.net.newton.Makler.common.NumberFormatUtils.java
pl.net.newton.Makler.db.Constants.java
pl.net.newton.Makler.db.DbHelper.java
pl.net.newton.Makler.db.SqlConnection.java
pl.net.newton.Makler.db.alert.AlertBuilder.java
pl.net.newton.Makler.db.alert.AlertChecker.java
pl.net.newton.Makler.db.alert.AlertValue.java
pl.net.newton.Makler.db.alert.Alert.java
pl.net.newton.Makler.db.alert.AlertsDao.java
pl.net.newton.Makler.db.alert.Event.java
pl.net.newton.Makler.db.alert.Subject.java
pl.net.newton.Makler.db.quote.QuoteField.java
pl.net.newton.Makler.db.quote.Quote.java
pl.net.newton.Makler.db.quote.QuotesDao.java
pl.net.newton.Makler.db.service.SqlProvider.java
pl.net.newton.Makler.db.symbol.SymbolBuilder.java
pl.net.newton.Makler.db.symbol.Symbol.java
pl.net.newton.Makler.db.symbol.SymbolsDb.java
pl.net.newton.Makler.db.wallet.WalletDb.java
pl.net.newton.Makler.db.wallet.WalletItemBuilder.java
pl.net.newton.Makler.db.wallet.WalletItem.java
pl.net.newton.Makler.gpw.DefaultQuotesReceiver.java
pl.net.newton.Makler.gpw.QuotesReceiver.java
pl.net.newton.Makler.gpw.ex.GpwException.java
pl.net.newton.Makler.gpw.service.QuotesListener.java
pl.net.newton.Makler.gpw.service.QuotesService.java
pl.net.newton.Makler.gpw.service.UpdatingThread.java
pl.net.newton.Makler.history.BossaProvider.java
pl.net.newton.Makler.history.ByteArrayUtils.java
pl.net.newton.Makler.history.Cache.java
pl.net.newton.Makler.history.ChannelTools.java
pl.net.newton.Makler.history.EntryListWithIndexes.java
pl.net.newton.Makler.history.EntryList.java
pl.net.newton.Makler.history.HistoryFilter.java
pl.net.newton.Makler.history.HistoryProvider.java
pl.net.newton.Makler.history.service.HistoryListener.java
pl.net.newton.Makler.history.service.HistoryService.java
pl.net.newton.Makler.httpClient.Connector.java
pl.net.newton.Makler.receivers.QuotesAlarmReceiver.java
pl.net.newton.Makler.receivers.StartupReceiver.java
pl.net.newton.Makler.service.ServiceManager.java
pl.net.newton.Makler.ui.About.java
pl.net.newton.Makler.ui.AbstractActivity.java
pl.net.newton.Makler.ui.Alerts.java
pl.net.newton.Makler.ui.FullScreenGraph.java
pl.net.newton.Makler.ui.Preferences.java
pl.net.newton.Makler.ui.QuoteDetails.java
pl.net.newton.Makler.ui.Quotes.java
pl.net.newton.Makler.ui.Symbols.java
pl.net.newton.Makler.ui.WalletForm.java
pl.net.newton.Makler.ui.WalletItemCalculator.java
pl.net.newton.Makler.ui.Wallet.java
pl.net.newton.Makler.ui.adapter.AlertsAdapter.java
pl.net.newton.Makler.ui.adapter.QuotesAdapter.java
pl.net.newton.Makler.ui.adapter.SymbolsAdapter.java
pl.net.newton.Makler.ui.adapter.WalletAdapter.java
pl.net.newton.Makler.ui.graph.GraphView.java
pl.net.newton.Makler.ui.graph.MaklerGraphicalView.java