All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.mobile.android.location.AndroidLocationFinder Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * $Id: AndroidLocationFinder.java,v c4b8f1113ba0 2010/06/08 14:34:58 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.mobile.android.location;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.List;
import java.io.IOException;
import org.openide.util.Lookup;
import org.openide.util.lookup.ServiceProvider;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import it.tidalwave.geo.Coordinate;
import it.tidalwave.geo.Range;
import it.tidalwave.mobile.location.LocationPreferences;
import it.tidalwave.mobile.location.LocationFinder;
import static it.tidalwave.mobile.location.LocationFinder.State.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
@ServiceProvider(service=LocationFinder.class)
public class AndroidLocationFinder extends LocationFinder
  {
    @Nonnull
    private final Context context = Lookup.getDefault().lookup(Context.class);

    @Nonnull
    private final LocationManager locationManager = Lookup.getDefault().lookup(LocationManager.class);
    
    @Nonnull
    private final LocationPreferences locationPreferences = Lookup.getDefault().lookup(LocationPreferences.class);

    @Nonnull
    private final SharedPreferences sharedPreferences = Lookup.getDefault().lookup(SharedPreferences.class);

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private final OnSharedPreferenceChangeListener preferencesListener = new OnSharedPreferenceChangeListener()
      {
        public void onSharedPreferenceChanged (final @Nonnull SharedPreferences sharedPreferences,
                                               final @Nonnull String string)
          {
            setAddressSearchEnabled(locationPreferences.isReverseGeocodingEnabled());
          }
      };

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private final LocationListener locationListener = new LocationListener()
      {
        public void onLocationChanged (final @Nonnull Location location)
          {
            setRange(toRange(location));
            setProviderName(location.getProvider());
            locationManager.removeUpdates(locationListener);

            if (addressSearchEnabled)
              {
                findAddress(); // FIXME: on a background thread?
              }
            else
              {
                setState(State.IDLE);
              }
          }

        public void onStatusChanged (final @Nonnull String providerName,
                                     final int status,
                                     final @Nonnull Bundle bundle)
          {
            setProviderName(providerName);
            setProviderStatus(status);
          }

        public void onProviderEnabled (final @Nonnull String providerName)
          {
            setProviderName(providerName);
          }

        public void onProviderDisabled (final @Nonnull String providerName)
          {
            setProviderName(providerName);
          }
      };

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public AndroidLocationFinder()
      {
        sharedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void start()
      {
        setRange(null);
        setAddress(null);
        setAddressSearchEnabled(locationPreferences.isReverseGeocodingEnabled());
        setState(IDLE);
        
//        new Thread()
//          {
//            @Override
//            public void run()
//              {
                setProviderName(locationManager.getBestProvider((Criteria)locationPreferences.getCriteria(), true));

                if (providerName != null)
                  {
//                    final Location location = locationManager.getLastKnownLocation(providerName);
//                    setLocation(location);
//
//                    if (location != null)
//                      {
//                        findAddress();
//                      }

                    final long period = 1000;
                    final float minDistance = 10.0f;
                    locationManager.requestLocationUpdates(providerName, period, minDistance, locationListener);
                    setState(COMPUTING_RANGE);
                  }
//              }
//          }.start();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void stop()
      {
        locationManager.removeUpdates(locationListener);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @CheckForNull
    private static Range toRange (final @CheckForNull Location location)
      {
        if (location == null)
          {
            return null;
          }
        else
          {
            final Coordinate coordinate = new Coordinate(location.getLatitude(), location.getLongitude(), location.getAltitude());
            return Range.on(coordinate).withRadius(location.getAccuracy());
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void findAddress()
      {
        new Thread()
          {
            @Override
            public void run()
              {
                try
                  {
                    setState(SEARCHING_FOR_ADDRESS);
                    final Geocoder geocoder = new Geocoder(context);
                    final Coordinate coordinate = range.getCoordinate();
                    final List
addresses = geocoder.getFromLocation(coordinate.getLatitude(), coordinate.getLongitude(), 1); if (!addresses.isEmpty()) { address = addresses.get(0); } } catch (IOException e) { // TODO: log } finally { setState(IDLE); } } }.start(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy