Android Open Source - makler Quotes Service






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.gpw.service;
//from  w  w  w.  ja  v  a 2  s. c  o  m
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import pl.net.newton.Makler.db.alert.AlertChecker;
import pl.net.newton.Makler.db.alert.AlertsDao;
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.db.service.SqlProvider;
import pl.net.newton.Makler.gpw.DefaultQuotesReceiver;
import pl.net.newton.Makler.gpw.QuotesReceiver;
import pl.net.newton.Makler.gpw.ex.GpwException;
import pl.net.newton.Makler.common.Configuration;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.sqlite.SQLiteDatabase;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class QuotesService extends Service {
  public static enum StartIntent {
    DOWNLOAD_QUOTES, START_UPDATER_THREAD
  }

  static final String TAG = "MaklerQuotesServiceImpl";

  private final UpdatingThread updatingThread = new UpdatingThread(this);

  private final LocalBinder binder = new LocalBinder();

  private QuotesReceiver quotesReceiver;

  List<QuotesListener> listeners = Collections.synchronizedList(new ArrayList<QuotesListener>());

  Configuration config;

  private SQLiteDatabase sql;

  private StartIntent startIntent;

  private Executor executor = Executors.newFixedThreadPool(1);

  private QuotesDao quotesDb;

  private AlertChecker alertChecker;

  private volatile boolean updatesEnabled = true;

  volatile boolean foreground = false;

  private ServiceConnection sqlConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName name, IBinder service) {
      gotSql(((SqlProvider.LocalBinder) service).getService().getSql());
    }

    public void onServiceDisconnected(ComponentName name) {
      sql = null;
    }
  };

  @Override
  public void onCreate() {
    super.onCreate();
    Log.d(TAG, this.getClass().getName() + " - onCreate");
    config = new Configuration(this);
    quotesReceiver = new DefaultQuotesReceiver(this);
    bindService(new Intent(this, SqlProvider.class), sqlConnection, BIND_AUTO_CREATE);
  }

  private void gotSql(SQLiteDatabase sql) {
    this.sql = sql;
    this.quotesDb = new QuotesDao(sql, this);
    this.alertChecker = new AlertChecker(this, new AlertsDao(sql, this), config);
    utilizeIntent();
  }

  @Override
  public synchronized int onStartCommand(Intent intent, int flags, int startId) {
    if (intent == null) {
      return START_NOT_STICKY;
    }
    String stringIntent = intent.getStringExtra(StartIntent.class.getName());
    if (stringIntent != null) {
      startIntent = StartIntent.valueOf(stringIntent);
    } else {
      startIntent = null;
    }
    if (sql != null) {
      utilizeIntent();
    }
    return START_NOT_STICKY;
  }

  private synchronized void utilizeIntent() {
    StartIntent intent = startIntent;
    startIntent = null;
    if (intent == null) {
      return;
    }
    switch (intent) {
      case DOWNLOAD_QUOTES:
        downloadQuotesAndReturn();
        break;

      case START_UPDATER_THREAD:
        updatingThread.startThread();
        break;

      default:
        Log.e(TAG, "Invalid intent value: " + intent);
        break;
    }
  }

  @Override
  public IBinder onBind(Intent intent) {
    return binder;
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    try {
      updatingThread.stopThread();
    } catch (Exception e) {
      Log.e(TAG, "error in destroying QuotesService", e);
    }
    unbindService(sqlConnection);
  }

  public void register(QuotesListener listener) {
    listeners.add(listener);
  }

  public void unregister(QuotesListener listener) {
    listeners.remove(listener);
  }

  public void updateQuotes() throws GpwException {
    if (!updatesEnabled) {
      return;
    }
    synchronized (quotesReceiver) {
      Log.d(TAG, "updating quotes");
      List<Quote> quotes = quotesDb.getQuotes(true);
      List<String> symbols = new ArrayList<String>();
      for (Quote q : quotes) {
        symbols.add(q.get(QuoteField.SYMBOL));
      }

      List<Quote> newQuotes = quotesReceiver.getQuotesBySymbols(symbols);
      if (newQuotes != null) {
        int qSize = quotes.size();
        int nSize = newQuotes.size();
        for (int i = 0; i < qSize && i < nSize; i++) {
          if (newQuotes.get(i) != null) {
            quotesDb.updateQuote(newQuotes.get(i));
          }
        }
        alertChecker.checkAlerts();
      }
    }
  }

  public void setUpdates(boolean enabled) {
    updatesEnabled = enabled;
  }

  public void setIsInForeground(boolean enabled) {
    foreground = enabled;
  }

  private void downloadQuotesAndReturn() {
    executor.execute(new Runnable() {
      public void run() {
        try {
          updateQuotes();
          stopSelf();
        } catch (Exception e) {
          Log.e(TAG, "Can't update quotes", e);
        }
      }
    });
  }

  public class LocalBinder extends Binder {
    public QuotesService getService() {
      return QuotesService.this;
    }
  }

}




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