Android Open Source - PalmaBici Main Activity






From Project

Back to project page PalmaBici.

License

The source code is released under:

GNU General Public License

If you think the Android project PalmaBici 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

/*
 * Copyright 2012 Sergio Garcia Villalonga (yayalose@gmail.com)
 */* ww w.  j a  va 2s .  com*/
 * This file is part of PalmaBici.
 *
 *    PalmaBici is free software: you can redistribute it and/or modify
 *    it under the terms of the Affero GNU General Public License version 3
 *    as published by the Free Software Foundation.
 *
 *    PalmaBici is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Affero GNU General Public License for more details
 *    (https://www.gnu.org/licenses/agpl-3.0.html).
 *    
 */

package com.poguico.palmabici;

import java.util.Calendar;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.poguico.palmabici.map.OpenStreetMapConstants;
import com.poguico.palmabici.notification.NotificationManager;
import com.poguico.palmabici.network.synchronizer.NetworkSynchronizer;
import com.poguico.palmabici.network.synchronizer.NetworkSynchronizer.NetworkSynchronizationState;
import com.poguico.palmabici.util.Formatter;
import com.poguico.palmabici.util.NetworkInformation;
import com.poguico.palmabici.widgets.NewFeaturesDialog;
import com.poguico.palmabici.widgets.SidebarMenu;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;

public class MainActivity extends    SherlockFragmentActivity
                          implements SynchronizableElement,OpenStreetMapConstants{
  
  private ProgressDialog       dialog;  
  private SharedPreferences    conf = null;
  private NetworkSynchronizer  synchronizer;
  private NetworkInformation   network;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
      int savedVersionNumber;
      SharedPreferences sharedPref;
      PackageInfo pi;
      String initPrefs     = "palmabici_init_prefs";
      String versionNumber = "version_number";
      int currentVersionNumber        = 0;
      super.onCreate(savedInstanceState);
      synchronizer = NetworkSynchronizer.getInstance(this.getApplicationContext());
      network = NetworkInformation.getInstance(this.getApplicationContext());
      conf = PreferenceManager.getDefaultSharedPreferences(this);
      
      synchronizer.addSynchronizableElement(this);
      
      sharedPref = getSharedPreferences(initPrefs, Context.MODE_PRIVATE);
      savedVersionNumber = sharedPref.getInt(versionNumber, 0);
      try {
            pi = getPackageManager().getPackageInfo(getPackageName(), 0);
            currentVersionNumber = pi.versionCode;
        } catch (Exception e) {}
 
        if (currentVersionNumber > savedVersionNumber) {
          new NewFeaturesDialog(this).show();
            Editor editor   = sharedPref.edit();
            editor.putInt(versionNumber, currentVersionNumber);
            editor.commit();
        }

        setContentView(R.layout.main);
        SidebarMenu.setDrawer(this);
    }
    
    @Override
  public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
      getSupportMenuInflater().inflate(R.menu.main, menu);
      return true;
  }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                break;

            case R.id.menu_refresh:
              NetworkSynchronizationState syncState;
              dialog = ProgressDialog.show(this, "",
                  getString(R.string.refresh_ongoing), true);
              NetworkSynchronizer synchronizer =
                  NetworkSynchronizer.getInstance(this);
              syncState = synchronizer.sync(true);
              if (syncState == NetworkSynchronizationState.ERROR) {
            onUnsuccessfulNetworkSynchronization();
          }
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    
    @Override
  protected void onResume() {
      NetworkSynchronizationState syncState;
    super.onResume();
    
    if (conf.getBoolean("autoupdate", true)) {
      syncState = synchronizer.sync(false);
      
      if (syncState == NetworkSynchronizationState.UPDATED) {
        showLastUpdateTime(false);
      } else if (syncState == NetworkSynchronizationState.ERROR) {
        onUnsuccessfulNetworkSynchronization();
      } else if (syncState == NetworkSynchronizationState.UPDATING) {
        dialog = ProgressDialog.show(this, "",
                  getString(R.string.refresh_ongoing), true);
      }
    }
  }
    
  @Override
  protected void onDestroy() {
    if (dialog != null) {
      dialog.dismiss();
    }
    
    synchronizer.detachSynchronizableElement(this);
    super.onDestroy();
  }
  
  @Override
  public void onSuccessfulNetworkSynchronization() {
      TextView updateTime = (TextView) findViewById(R.id.lastUpdatedLabel);
      
      if (dialog != null)
        dialog.hide();
      NotificationManager.showMessage(updateTime,
           getString(R.string.refresh_succesful),
         3000,
         this,
         false);
  }

  @Override
  public void onUnsuccessfulNetworkSynchronization() {
    if (dialog != null)
        dialog.hide();
    
    TextView updateTime = (TextView) findViewById(R.id.lastUpdatedLabel);
    NotificationManager.showMessage(updateTime,
                     getString(R.string.connectivity_error),
                     3000,
                     this,
                     true);
    showLastUpdateTime(true);
  }

  @Override
  public void onLocationSynchronization() {}

  @Override
  public FragmentActivity getSynchronizableActivity() {
    return this;
  }
  
  private void showLastUpdateTime(boolean forceShow) {
    long lastUpdateTime = network.getLastUpdateTime();
    long delay = Calendar.getInstance().getTimeInMillis() - lastUpdateTime;
    
    if (forceShow || delay > 60000) {
      TextView updateTime = (TextView) findViewById(R.id.lastUpdatedLabel);
      NotificationManager.showMessage(updateTime,
           Formatter.formatLastUpdated(lastUpdateTime, this),
           3000,
           this,
           false);
    }
  }
}




Java Source Code List

com.poguico.palmabici.DatabaseManager.java
com.poguico.palmabici.MainActivity.java
com.poguico.palmabici.PreferencesActivity.java
com.poguico.palmabici.ShareActivity.java
com.poguico.palmabici.SynchronizableElement.java
com.poguico.palmabici.WelcomeActivity.java
com.poguico.palmabici.map.OpenStreetMapConstants.java
com.poguico.palmabici.map.ResourceProxyImpl.java
com.poguico.palmabici.map.StationMapFragment.java
com.poguico.palmabici.network.synchronizer.NetworkStationAlarm.java
com.poguico.palmabici.network.synchronizer.NetworkSyncCallback.java
com.poguico.palmabici.network.synchronizer.NetworkSynchronizerTask.java
com.poguico.palmabici.network.synchronizer.NetworkSynchronizer.java
com.poguico.palmabici.notification.NotificationManager.java
com.poguico.palmabici.parsers.Parser.java
com.poguico.palmabici.synchronizers.LocationSynchronizer.java
com.poguico.palmabici.util.BikeLane.java
com.poguico.palmabici.util.Formatter.java
com.poguico.palmabici.util.NetworkInformation.java
com.poguico.palmabici.util.Station.java
com.poguico.palmabici.widgets.CreditsDialog.java
com.poguico.palmabici.widgets.NewFeaturesDialog.java
com.poguico.palmabici.widgets.SidebarMenu.java
com.poguico.palmabici.widgets.StationInfoWidget.java