it.tidalwave.mobile.location.LocationFinder 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: LocationFinder.java,v c4b8f1113ba0 2010/06/08 14:34:58 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.mobile.location;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.geo.Range;
/***********************************************************************************************************************
*
* FIXME: wrap Location and Address with something not depending on Android.
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public abstract class LocationFinder
{
public static final String PROP_PROVIDER_NAME = "providerName";
public static final String PROP_PROVIDER_STATUS = "providerStatus";
public static final String PROP_RANGE = "range";
public static final String PROP_ADDRESS = "address";
public static final String PROP_ADDRESS_SEARCH_ENABLED = "addressSearchEnabled";
public static final String PROP_STATE = "state";
public static enum State
{
IDLE, COMPUTING_RANGE, SEARCHING_FOR_ADDRESS
}
protected final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
@CheckForNull
protected String providerName;
protected int providerStatus;
@CheckForNull
protected Range range;
@CheckForNull
protected Object address;
protected boolean addressSearchEnabled;
protected State state = State.IDLE;
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void addPropertyChangeListener (final @Nonnull PropertyChangeListener listener)
{
propertyChangeSupport.addPropertyChangeListener(listener);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void removePropertyChangeListener (final @Nonnull PropertyChangeListener listener)
{
propertyChangeSupport.removePropertyChangeListener(listener);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public String getProviderName()
{
return providerName;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public int getProviderStatus()
{
return providerStatus;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public Range getRange()
throws NotFoundException
{
return NotFoundException.throwWhenNull(range, "Unknown coordinates");
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@CheckForNull
public Object getAddress()
{
return address;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public State getState()
{
return state;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void setAddressSearchEnabled (final boolean addressSearchEnabled)
{
final boolean oldAddressSearchEnabled = this.addressSearchEnabled;
this.addressSearchEnabled = addressSearchEnabled;
propertyChangeSupport.firePropertyChange(PROP_ADDRESS_SEARCH_ENABLED, oldAddressSearchEnabled, addressSearchEnabled);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public boolean isAddressSearchEnabled()
{
return addressSearchEnabled;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public abstract void start();
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public abstract void stop();
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public abstract void findAddress();
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected void setProviderName (final @Nonnull String providerName)
{
final String oldProviderName = this.providerName;
this.providerName = providerName;
propertyChangeSupport.firePropertyChange(PROP_PROVIDER_NAME, oldProviderName, providerName);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected void setProviderStatus (final int providerStatus)
{
final int oldProviderStatus = this.providerStatus;
this.providerStatus = providerStatus;
propertyChangeSupport.firePropertyChange(PROP_PROVIDER_STATUS, oldProviderStatus, providerStatus);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected void setState (final @Nonnull State state)
{
final State oldState = this.state;
this.state = state;
propertyChangeSupport.firePropertyChange(PROP_STATE, oldState, state);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected void setRange (final @Nullable Range range)
{
final Range oldRange = this.range;
this.range = range;
propertyChangeSupport.firePropertyChange(PROP_RANGE, oldRange, range);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected void setAddress (final @Nonnull Object address)
{
final Object oldAddress = this.address;
this.address = address;
propertyChangeSupport.firePropertyChange(PROP_ADDRESS, oldAddress, address);
}
}