Android Open Source - PalmaBici Location Synchronizer






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)
 *// w ww . ja va  2  s  .c  om
 * 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.synchronizers;

import java.util.ArrayList;

import com.poguico.palmabici.SynchronizableElement;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class LocationSynchronizer {
  private static LocationSynchronizer instance = null;
  
  private Location         location = null;
  private LocationManager  manager;
  private LocationListener listener;
  
  private static ArrayList<SynchronizableElement> synchronizableElements;
  
  public synchronized static LocationSynchronizer getInstance (SynchronizableElement activity) {
    if (instance == null) {
      instance = new LocationSynchronizer(activity.getSynchronizableActivity());
    }    
    instance.addSynchronizableElement(activity);
    
    return instance;
  }
  
  public LocationSynchronizer (FragmentActivity context) {    
    manager  = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    listener = new LocationListener() {

      @Override
      public void onLocationChanged(Location l) {
        location = l;
        updateViews();
      }
      
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {}
      
      @Override
      public void onProviderEnabled(String provider) {}
      
      @Override
      public void onProviderDisabled(String provider) {
        if ((provider.equals(LocationManager.GPS_PROVIDER) &&
            !manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) ||
          (provider.equals(LocationManager.NETWORK_PROVIDER) &&
            !manager.isProviderEnabled(LocationManager.GPS_PROVIDER))) {
          location = null;
          updateViews();
        }          
      }
    };
    
    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000L, 0, listener);
    manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000L, 0, listener);
    
    if (location == null)
      location =  manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    
    if (location == null)
      location =  manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        
    synchronizableElements = new ArrayList<SynchronizableElement>();
  }
  
  private synchronized void updateViews () {
    for (SynchronizableElement activity : synchronizableElements)
      activity.onLocationSynchronization();
  }
  
  public synchronized void addSynchronizableElement(SynchronizableElement element) {
    if (!synchronizableElements.contains(element)) { 
      synchronizableElements.add(element);
    }
  }
  
  public synchronized void detachSynchronizableElement(SynchronizableElement element) {
    synchronizableElements.remove(element);
  }
  
  public Location getLocation () {
    return location;
  }
}




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