Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.attilapalfi.exceptional.services;
- import javax.inject.Inject;
- import android.app.Service;
- import android.content.Context;
- import android.content.Intent;
- import android.location.Criteria;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.widget.Toast;
- import com.attilapalfi.exceptional.R;
- import com.attilapalfi.exceptional.dependency_injection.Injector;
- /**
- * Created by Attila on 2015-06-20.
- */
- public class LocationService extends Service implements LocationListener {
- private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 100; // 100 meters
- private static final long MIN_TIME_BETWEEN_UPDATES = 1000 * 10; // 10 seconds
- @Inject Context context;
- private LocationManager locationManager;
- private Location location;
- private String currentProvider;
- private boolean locationEnabled = false;
- public LocationService( ) {
- Injector.INSTANCE.getApplicationComponent().inject( this );
- locationManager = (LocationManager) context.getSystemService( LOCATION_SERVICE );
- initLocationManager();
- }
- public Location getLocation( ) throws LocationException {
- initLocationManager();
- handleDisabledLocationServices();
- location = locationManager.getLastKnownLocation( currentProvider );
- handleNullLocation();
- return location;
- }
- private void initLocationManager( ) {
- Criteria criteria = new Criteria();
- criteria.setPowerRequirement( Criteria.POWER_LOW );
- currentProvider = locationManager.getBestProvider( criteria, true );
- locationEnabled = currentProvider != null;
- if ( locationEnabled ) {
- locationManager.requestLocationUpdates(
- currentProvider,
- MIN_TIME_BETWEEN_UPDATES,
- MIN_DISTANCE_CHANGE_FOR_UPDATES, this );
- }
- }
- private void handleDisabledLocationServices( ) throws LocationException {
- if ( !locationEnabled ) {
- Toast.makeText( context, "Enable location services and restart the application, to throw exceptions.", Toast.LENGTH_LONG ).show();
- throw new LocationException( "Location services is not enabled." );
- }
- }
- private void handleNullLocation( ) throws LocationException {
- if ( location == null ) {
- Toast.makeText( context, "Location services is enabled, but location was null.", Toast.LENGTH_LONG ).show();
- throw new LocationException( "Location services is enabled, but location was null." );
- }
- }
- public void stopUsingGps( ) {
- if ( locationManager != null ) {
- locationManager.removeUpdates( this );
- }
- }
- @Override
- public void onLocationChanged( Location location ) {
- this.location = location;
- }
- @Override
- public void onProviderDisabled( String provider ) {
- }
- @Override
- public void onProviderEnabled( String provider ) {
- }
- @Override
- public void onStatusChanged( String provider, int status, Bundle extras ) {
- }
- @Override
- public IBinder onBind( Intent arg0 ) {
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment