Guest User

LocationHelper

a guest
Aug 9th, 2013
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. public class LocationHelper
  2. {
  3.     public interface RefreshInterface
  4.     {
  5.         public void preExecute();
  6.         public boolean isLocationValid(Location location);
  7.         public void postExecute(Location location);
  8.     }
  9.  
  10.     private static class RefreshLocation implements LocationListener
  11.     {
  12.         private long timeout = 0; // 20 vterin
  13.         private long startTime = 0;
  14.         private boolean finished = false;
  15.        
  16.         private Handler handler = new Handler();
  17.         private Runnable r = null;
  18.  
  19.         private RefreshInterface iface = null;
  20.  
  21.         private RefreshLocation(RefreshInterface iface, long timeout)
  22.         {
  23.             this.startTime = System.currentTimeMillis();
  24.             this.iface = iface;
  25.             this.timeout = timeout;
  26.         }
  27.  
  28.         private void refresh()
  29.         {
  30.             if(this.iface instanceof RefreshInterface)
  31.             {
  32.                 this.iface.preExecute();
  33.             }
  34.  
  35.             r = new Runnable() {
  36.                 @Override
  37.                 public void run()
  38.                 {
  39.                     if(!finished)
  40.                     {
  41.                         finished = true;
  42.                         getLocationManager().removeUpdates(RefreshLocation.this);
  43.                         if(iface instanceof RefreshInterface)
  44.                         {
  45.                             iface.postExecute(null);
  46.                         }
  47.                     }
  48.                 }
  49.             };
  50.  
  51.             String bp = getBestProvider();
  52.             Location lp = getLocationManager().getLastKnownLocation(bp);
  53.             if(lp instanceof Location && lp.getTime() + 2*60*1000 > System.currentTimeMillis())
  54.             {
  55.                 if(iface instanceof RefreshInterface && iface.isLocationValid(lp))
  56.                 {
  57.                     iface.postExecute(lp);
  58.                     return;
  59.                 }
  60.             }
  61.  
  62.             if(getLocationManager().isProviderEnabled(bp))
  63.             {
  64.                 if (getLocationManager().isProviderEnabled(LocationManager.GPS_PROVIDER)) {
  65.                     getLocationManager().requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);
  66.                 }
  67.                 if (getLocationManager().isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
  68.                     getLocationManager().requestSingleUpdate(LocationManager.NETWORK_PROVIDER, this, null);
  69.                 }
  70.                 finished = false;
  71.                 handler.postDelayed(r, timeout);
  72.             }
  73.             else
  74.             {
  75.                 if(iface instanceof RefreshInterface)
  76.                 {
  77.                     iface.postExecute(null);
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment