Guest User

Untitled

a guest
Sep 20th, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. package com.attilapalfi.exceptional.services;
  2.  
  3. import javax.inject.Inject;
  4.  
  5. import android.app.Service;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.location.Criteria;
  9. import android.location.Location;
  10. import android.location.LocationListener;
  11. import android.location.LocationManager;
  12. import android.os.Bundle;
  13. import android.os.IBinder;
  14. import android.widget.Toast;
  15. import com.attilapalfi.exceptional.R;
  16. import com.attilapalfi.exceptional.dependency_injection.Injector;
  17.  
  18. /**
  19.  * Created by Attila on 2015-06-20.
  20.  */
  21. public class LocationService extends Service implements LocationListener {
  22.     private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 100; // 100 meters
  23.     private static final long MIN_TIME_BETWEEN_UPDATES = 1000 * 10; // 10 seconds
  24.  
  25.     @Inject Context context;
  26.     private LocationManager locationManager;
  27.     private Location location;
  28.     private String currentProvider;
  29.     private boolean locationEnabled = false;
  30.  
  31.     public LocationService( ) {
  32.         Injector.INSTANCE.getApplicationComponent().inject( this );
  33.         locationManager = (LocationManager) context.getSystemService( LOCATION_SERVICE );
  34.         initLocationManager();
  35.     }
  36.  
  37.     public Location getLocation( ) throws LocationException {
  38.         initLocationManager();
  39.         handleDisabledLocationServices();
  40.         location = locationManager.getLastKnownLocation( currentProvider );
  41.         handleNullLocation();
  42.         return location;
  43.     }
  44.  
  45.     private void initLocationManager( ) {
  46.         Criteria criteria = new Criteria();
  47.         criteria.setPowerRequirement( Criteria.POWER_LOW );
  48.         currentProvider = locationManager.getBestProvider( criteria, true );
  49.         locationEnabled = currentProvider != null;
  50.         if ( locationEnabled ) {
  51.             locationManager.requestLocationUpdates(
  52.                     currentProvider,
  53.                     MIN_TIME_BETWEEN_UPDATES,
  54.                     MIN_DISTANCE_CHANGE_FOR_UPDATES, this );
  55.         }
  56.     }
  57.  
  58.     private void handleDisabledLocationServices( ) throws LocationException {
  59.         if ( !locationEnabled ) {
  60.             Toast.makeText( context, "Enable location services and restart the application, to throw exceptions.", Toast.LENGTH_LONG ).show();
  61.             throw new LocationException( "Location services is not enabled." );
  62.         }
  63.     }
  64.  
  65.     private void handleNullLocation( ) throws LocationException {
  66.         if ( location == null ) {
  67.             Toast.makeText( context, "Location services is enabled, but location was null.", Toast.LENGTH_LONG ).show();
  68.             throw new LocationException( "Location services is enabled, but location was null." );
  69.         }
  70.     }
  71.  
  72.     public void stopUsingGps( ) {
  73.         if ( locationManager != null ) {
  74.             locationManager.removeUpdates( this );
  75.         }
  76.     }
  77.  
  78.     @Override
  79.     public void onLocationChanged( Location location ) {
  80.         this.location = location;
  81.     }
  82.  
  83.     @Override
  84.     public void onProviderDisabled( String provider ) {
  85.     }
  86.  
  87.     @Override
  88.     public void onProviderEnabled( String provider ) {
  89.     }
  90.  
  91.     @Override
  92.     public void onStatusChanged( String provider, int status, Bundle extras ) {
  93.     }
  94.  
  95.     @Override
  96.     public IBinder onBind( Intent arg0 ) {
  97.         return null;
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment