andreich

LocationHelper

Aug 11th, 2013
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. public class LocationListener extends ILocationListener
  2. {
  3.     private final static int TIME_OUT = 100 * 1000;
  4.     private final static long HALF_MINUTE = 30 * 1000;
  5.  
  6.     private static LocationListener listenerWithBackgrounProcess;
  7.  
  8.     private Location mLocation;
  9.     private final LocationManager manager;
  10.     private FindLocation mFindLocation;
  11.  
  12.     LocationListener mLocationListener = new LocationListenerImpl();
  13.  
  14.     private LocationListener(final Context context)
  15.     {
  16.         log("constructor");
  17.         manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  18.     }
  19.  
  20.     public static LocationListener getInstance(final Context context)
  21.     {
  22.         if (listenerWithBackgrounProcess == null)
  23.             listenerWithBackgrounProcess = new LocationListener(context);
  24.         return listenerWithBackgrounProcess;
  25.     }
  26.  
  27.  
  28.    
  29.     private void startFind()
  30.     {
  31.         log("startFind");
  32.  
  33.         initLoacationILstener();
  34.         if (mFindLocation != null && !mFindLocation.isCancelled())
  35.             mFindLocation.cancel(true);
  36.  
  37.         mFindLocation = new FindLocation();
  38.         mFindLocation.execute();
  39.  
  40.     }
  41.  
  42.     private boolean hasLastKnownLocation()
  43.     {
  44.         log("check hasLastKnownLocation");
  45.         Location bestLastKnown = null;
  46.         List<String> providers = manager.getAllProviders();
  47.         int i = 0;
  48.         for (; i < providers.size(); i++)
  49.         {
  50.             Location temp = manager.getLastKnownLocation(providers.get(i));
  51.             if (temp != null)
  52.             {
  53.                 bestLastKnown = temp;
  54.                 break;
  55.             }
  56.         }
  57.  
  58.         if (bestLastKnown != null)
  59.         {
  60.             for (int j = i; j < providers.size(); j++)
  61.             {
  62.                 Location temp = manager.getLastKnownLocation(providers.get(j));
  63.                 if (temp != null && temp.getTime() < bestLastKnown.getTime())
  64.                     bestLastKnown = temp;
  65.             }
  66.  
  67.             if (bestLastKnown.getTime() <= HALF_MINUTE)
  68.             {
  69.                 log("hasLastKnownLocation || time:" + String.valueOf(System.currentTimeMillis() - bestLastKnown.getTime()));
  70.                 if (locationRunnable != null)
  71.                     locationRunnable.locationUpdate(bestLastKnown);
  72.                 return true;
  73.             }
  74.         }
  75.  
  76.         return false;
  77.     }
  78.  
  79.  
  80.     private Location endFind()
  81.     {
  82.         long sec = System.currentTimeMillis();
  83.         //time out 20 sec
  84.         while (this.mLocation == null && System.currentTimeMillis() - sec < TIME_OUT)
  85.         {}
  86.         log("endFind");
  87.         return this.mLocation;
  88.     }
  89.  
  90.  
  91.     @Override
  92.     public void enableMyLocation()
  93.     {
  94.         mLocation = null;
  95.         log("enableMyLocation");
  96.         if (!hasLastKnownLocation())
  97.             startFind();
  98.     }
  99.  
  100.  
  101.     @Override
  102.     public void disableMyLocation()
  103.     {
  104.         log("disableMyLocation");
  105.         if (mFindLocation != null && !mFindLocation.isCancelled())
  106.             mFindLocation.cancel(true);
  107.  
  108.  
  109.         manager.removeUpdates(mLocationListener);
  110.  
  111.     }
  112.  
  113.      private void initLoacationILstener()
  114.     {
  115.         List<String> providers = manager.getProviders(true);
  116.         for (String provider : providers)
  117.         {
  118.             if (manager.isProviderEnabled(provider))
  119.             {
  120.                 manager.requestLocationUpdates(provider, 0, 0, mLocationListener);
  121.                 log("init providers || " + provider);
  122.             }
  123.         }
  124.     }
  125.  
  126.  
  127.     private class LocationListenerImpl implements LocationListener
  128.     {
  129.         private LocationListenerImpl()
  130.         {
  131.         }
  132.  
  133.         @Override
  134.         public void onLocationChanged(Location location)
  135.         {
  136.             log("onLocationChanged");
  137.             LocationListenerWithBackgroundProcess.this.mLocation = location;
  138.             if (locationRunnable != null)
  139.                 locationRunnable.locationUpdate(location);
  140.             disableMyLocation();
  141.         }
  142.  
  143.         @Override
  144.         public void onProviderDisabled(String provider)
  145.         {
  146.         }
  147.  
  148.         @Override
  149.         public void onProviderEnabled(String provider)
  150.         {
  151.         }
  152.  
  153.         @Override
  154.         public void onStatusChanged(String provider, int status, Bundle extras)
  155.         {
  156.         }
  157.     }
  158.  
  159.  
  160.     private class FindLocation extends AsyncTask<Void, Void, Location>
  161.     {
  162.  
  163.         @Override
  164.         protected Location doInBackground(final Void... params)
  165.         {
  166.             return endFind();
  167.         }
  168.  
  169.         @Override
  170.         protected void onPostExecute(final Location location)
  171.         {
  172.             if (locationRunnable != null)
  173.                 locationRunnable.locationUpdate(location);
  174.             disableMyLocation();
  175.         }
  176.     }
  177. }
Add Comment
Please, Sign In to add comment