Android Open Source - makler Abstract Activity






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;
//  www .ja va2 s .  c o  m
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

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.gpw.service.QuotesListener;
import pl.net.newton.Makler.gpw.service.QuotesService;
import pl.net.newton.Makler.history.service.HistoryListener;
import pl.net.newton.Makler.history.service.HistoryService;
import pl.net.newton.Makler.service.ServiceManager;
import pl.net.newton.Makler.common.Configuration;
import pl.net.newton.Makler.db.service.SqlProvider;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.WindowManager.BadTokenException;

public abstract class AbstractActivity extends Activity {
  private static final String TAG = "Makler";

  protected Handler mHandler;

  protected Configuration config;

  protected QuotesService quotesService;

  private HistoryService historyService;

  protected ExecutorService executor = Executors.newCachedThreadPool();

  private ProgressDialog progressWindow;

  private AlertDialog excDialog;

  protected ServiceManager serviceManager;

  private SQLiteDatabase sqlDb;

  private AtomicBoolean uiInitialized = new AtomicBoolean(false);

  private final ServiceConnection quotesConnection = new ServiceConnection() {
    public void onServiceDisconnected(ComponentName name) {
      quotesService = null;
    }

    public void onServiceConnected(ComponentName name, IBinder service) {
      quotesService = ((QuotesService.LocalBinder) service).getService();
      if (AbstractActivity.this instanceof QuotesListener) {
        QuotesListener listener = (QuotesListener) AbstractActivity.this;
        quotesService.register(listener);
      }
      quotesService.setUpdates(updatesEnabled());
      quotesService.setIsInForeground(true);
      servicesInitialized();
    }
  };

  private final ServiceConnection historyConnection = new ServiceConnection() {
    public void onServiceDisconnected(ComponentName name) {
      historyService = null;
    }

    public void onServiceConnected(ComponentName name, IBinder service) {
      historyService = ((HistoryService.LocalBinder) service).getService();
      if (AbstractActivity.this instanceof HistoryListener) {
        HistoryListener listener = (HistoryListener) AbstractActivity.this;
        historyService.register(listener);
      }
      servicesInitialized();
    }
  };

  private ServiceConnection sqlConnection = new ServiceConnection() {
    public void onServiceDisconnected(ComponentName name) {
      sqlDb = null;
    }

    public void onServiceConnected(ComponentName name, IBinder service) {
      SqlProvider sqlProvider = ((SqlProvider.LocalBinder) service).getService();
      sqlDb = sqlProvider.getSql();
      servicesInitialized();
    }
  };

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, this.getClass().getName() + " - onCreate");

    serviceManager = new ServiceManager(this);

    bindService(serviceManager.getQuotesServiceIntent(), quotesConnection, BIND_AUTO_CREATE);
    bindService(serviceManager.getSqlProviderIntent(), sqlConnection, BIND_AUTO_CREATE);
    bindService(serviceManager.getHistoryServiceIntent(), historyConnection, BIND_AUTO_CREATE);

    config = new Configuration(this);

    mHandler = new Handler();
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, this.getClass().getName() + " - onDestroy");
    if (historyService != null && this instanceof HistoryListener) {
      historyService.unregister((HistoryListener) this);
    }
    unbindService(quotesConnection);
    unbindService(sqlConnection);
    unbindService(historyConnection);

    if (excDialog != null && excDialog.isShowing()) {
      excDialog.cancel();
    }
    if (progressWindow != null && progressWindow.isShowing()) {
      progressWindow.cancel();
    }
  }

  @Override
  public void onPause() {
    super.onPause();
    Log.d(TAG, this.getClass().getName() + " - onPause");
    if (quotesService != null) {
      quotesService.setUpdates(true);
      quotesService.setIsInForeground(false);
    }
  }

  @Override
  public void onResume() {
    super.onResume();
    Log.d(TAG, this.getClass().getName() + " - onResume");
    if (quotesService != null) {
      quotesService.setUpdates(updatesEnabled());
      quotesService.setIsInForeground(true);
    }
  }

  @Override
  public void onStart() {
    super.onStart();
    Log.d(TAG, this.getClass().getName() + " - onStart");
  }

  @Override
  public void onStop() {
    super.onStop();
    Log.d(TAG, this.getClass().getName() + " - onStop");
  }

  public void showProgressWindow() {
    mHandler.post(new Runnable() {
      public void run() {
        if (progressWindow != null && progressWindow.isShowing()) {
          return;
        }
        Log.d(TAG, "pokazywanie okna - prosimy czeka?");
        try {
          progressWindow = ProgressDialog.show(AbstractActivity.this, "Prosz? czeka?.",
              "Trwa komunikacja z serwerem.", true);
        } catch (BadTokenException e) {
          Log.e(TAG, "b??d przy wy?wietlaniu ProgressDialog", e);
        }
      }
    });
  }

  public void hideProgressWindow() {
    mHandler.post(new Runnable() {
      public void run() {
        if (progressWindow != null && progressWindow.isShowing()) {
          Log.d(TAG, "ukrywanie okna - prosimy czeka?");
          progressWindow.cancel();
          progressWindow = null;
        }
      }
    });
  }

  public void showMessage(final String msg) {
    mHandler.post(new Runnable() {
      public void run() {
        if (excDialog != null && excDialog.isShowing()) {
          Log.d(TAG, "Okienko jest ju? pokazywane");
          return;
        }
        try {
          AlertDialog.Builder builder = new AlertDialog.Builder(AbstractActivity.this);
          excDialog = builder.setMessage(msg).setCancelable(true).setTitle("Wyst?pi? b??d").show();
        } catch (Exception e) {
          Log.d(TAG, "Wyj?tek przy pokazywaniu okienka", e);
        }
      }
    });
  }

  protected boolean updatesEnabled() {
    return true;
  }

  private synchronized void servicesInitialized() {
    if (sqlDb != null && historyService != null && !uiInitialized.getAndSet(true)) {
      mHandler.post(new Runnable() {
        public void run() {
          initUi(sqlDb, historyService);
        }
      });
    }
  }

  protected void handleException(Exception e) {
    String message = "B??d: " + e.toString();
    if (e instanceof GpwException) {
      if (e.getMessage() != null) {
        message = e.getMessage();
      } else if (e.getCause() != null) {
        message = String.format("Wyst?pi? b??d z powodu " + e.getCause().toString());
      }
    }
    showMessage(message);
    Log.e(TAG, "exception", e);
  }

  protected void perform(final ProcessPerformer performer, final boolean setGpwProvider) {
    executor.execute(new PerformerRunnable(performer));
  }

  protected void onServiceDisconnected(ComponentName name) {
    // do nothing
  }

  private final class PerformerRunnable implements Runnable {
    private final ProcessPerformer performer;

    private PerformerRunnable(ProcessPerformer performer) {
      this.performer = performer;
    }

    public void run() {
      boolean success = false;
      boolean result = false;
      showProgressWindow();
      try {
        result = performer.perform(new DefaultQuotesReceiver(AbstractActivity.this));
        success = true;
      } catch (Exception e) {
        handleException(e);
      }
      hideProgressWindow();

      final boolean finalResult = result;
      if (success) {
        mHandler.post(new Runnable() {
          public void run() {
            performer.showResults(finalResult);
          }
        });
      }
    }
  }

  protected static interface ProcessPerformer {
    public boolean perform(QuotesReceiver quotesReceiver) throws GpwException;

    public void showResults(boolean result);
  }

  protected abstract void initUi(SQLiteDatabase sqlDb, HistoryService historyService);
}




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