Android Open Source - DroidAlone Location Updater






From Project

Back to project page DroidAlone.

License

The source code is released under:

Apache License

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

/*
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// w ww . jav a  2  s.c  o  m
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.*/

package com.fede.Utilities;

import android.content.Context;
import android.location.*;
import android.os.Bundle;
import android.os.SystemClock;
import com.fede.MessageException.LocationNotFoundException;
import com.fede.R;

import java.io.IOException;
import java.util.List;
import java.util.Locale;



public class LocationUpdater {
  private LocationManager mLManager;
  private String mLProvider;
  private Context mContext;
  static final long maxAgeNetworkMilliSeconds = 1000 * 60 * 10;  // 10 minutes
  
  private Location findLocation(int sleepTime){
    SystemClock.sleep(sleepTime);
    Location here = mLManager.getLastKnownLocation(mLProvider);
        if(here == null){
            return null;
        }
        
    long now = System.currentTimeMillis();
    long locationTime = here.getTime();
    
    if (locationTime < now - maxAgeNetworkMilliSeconds){
      return null;
    }
    return here;
  }
  
  private String locationFromCoordinates(Location l){
    return "lat " + String.valueOf(l.getLatitude()) + " lon " + String.valueOf(l.getLongitude());
  }
  
  public String getLocation() throws LocationNotFoundException{

    LocationListener l = new LocationListener(){
      @Override
      public void onLocationChanged(Location location) {
      }

      @Override
      public void onProviderDisabled(String provider){}
      
      @Override
      public void onProviderEnabled(String provider) {
      }

      @Override
      public void onStatusChanged(String provider, int status,
          Bundle extras) {}  
    };
    
    mLManager.requestLocationUpdates(mLProvider, 1000, 0, l);

    Location here = findLocation(5000);
    
    if(here == null){  // I try again
      here = findLocation(5000);  
    }
    if(here == null){
      throw new LocationNotFoundException(mContext.getString(R.string.location_not_found), mContext);
    }
    
    mLManager.removeUpdates(l);    
  
    List<Address> addresses;
    Geocoder gc = new Geocoder(mContext, Locale.getDefault());
    try {
      addresses = gc.getFromLocation(here.getLatitude(),
                      here.getLongitude(), 2);
      if (addresses.size() == 0){        
        SystemClock.sleep(1000);
        addresses = gc.getFromLocation(here.getLatitude(),
            here.getLongitude(), 2);
      }
      
    } catch (IOException e) {
       return locationFromCoordinates(here);
    }
    if (addresses.size() == 0){
      return locationFromCoordinates(here);      
    }
    
    Address a = addresses.get(0);
    
    
    String res = String.format("%s - %s-%s-%s-%s", locationFromCoordinates(here), a.getCountryName(), a.getLocality(), a.getAddressLine(0), a.getFeatureName());
    return res;
  }
  
  
  
  public LocationUpdater(Context c) throws LocationNotFoundException {
    mLManager = (LocationManager)c.getSystemService(Context.LOCATION_SERVICE);
    if(!mLManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
      throw new LocationNotFoundException(c.getString(R.string.location_not_found), c);
    }

    mLProvider = LocationManager.NETWORK_PROVIDER;
    mContext = c;      
  }
  
  
}




Java Source Code List

com.fede.ActiveState.java
com.fede.BootReceiver.java
com.fede.CommandSms.java
com.fede.DroidAloneApplication.java
com.fede.DroidContentProviderClient.java
com.fede.DroidContentProvider.java
com.fede.EventForwarder.java
com.fede.FirstActivity.java
com.fede.GMailSender.java
com.fede.HomeAloneHelp.java
com.fede.HomeAlonePreferences.java
com.fede.HomeAloneService.java
com.fede.InactiveState.java
com.fede.IncomingCallReceiver.java
com.fede.IncomingSmsReceiver.java
com.fede.NameNotFoundException.java
com.fede.OkDialogInterface.java
com.fede.SendInterface.java
com.fede.ServiceState.java
com.fede.TestStubInterface.java
com.fede.MessageException.CommandParseException.java
com.fede.MessageException.ForwardingDisabledException.java
com.fede.MessageException.InvalidCommandException.java
com.fede.MessageException.InvalidPasswordException.java
com.fede.MessageException.LocationNotFoundException.java
com.fede.Utilities.GeneralUtils.java
com.fede.Utilities.LocationUpdater.java
com.fede.Utilities.PrefUtils.java
com.fede.wizard.MailWizard.java
com.fede.wizard.SmsWizard.java
com.fede.wizard.StartWizard.java