Java Code Examples for android.location.LocationListener#onLocationChanged()

The following examples show how to use android.location.LocationListener#onLocationChanged() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AndroidLocationEngineImplTest.java    From mapbox-events-android with MIT License 5 votes vote down vote up
@Test
public void createListener() {
  LocationEngineCallback<LocationEngineResult> callback = mock(LocationEngineCallback.class);
  LocationListener locationListener = androidLocationEngineImpl.createListener(callback);
  Location mockLocation = getMockLocation(LATITUDE, LONGITUDE);
  locationListener.onLocationChanged(mockLocation);
  ArgumentCaptor<LocationEngineResult> argument = ArgumentCaptor.forClass(LocationEngineResult.class);
  verify(callback).onSuccess(argument.capture());

  LocationEngineResult result = argument.getValue();
  assertThat(result.getLastLocation()).isSameAs(mockLocation);
}
 
Example 2
Source File: MapboxFusedLocationEngineImplTest.java    From mapbox-events-android with MIT License 5 votes vote down vote up
@Test
public void createListener() {
  LocationEngineCallback<LocationEngineResult> callback = mock(LocationEngineCallback.class);
  LocationListener locationListener = mapboxFusedLocationEngineImpl.createListener(callback);
  Location mockLocation = getMockLocation(LATITUDE, LONGITUDE);
  locationListener.onLocationChanged(mockLocation);
  ArgumentCaptor<LocationEngineResult> argument = ArgumentCaptor.forClass(LocationEngineResult.class);
  verify(callback).onSuccess(argument.capture());

  LocationEngineResult result = argument.getValue();
  assertThat(result.getLastLocation()).isSameAs(mockLocation);
}
 
Example 3
Source File: GPSServiceCommandTest.java    From JayPS-AndroidApp with MIT License 5 votes vote down vote up
@SmallTest
public void testBroadcastEventOnLocationChange() throws Exception {
    when(_mockLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)).thenReturn(true);
    when(_mockDataStore.getFirstLocationLattitude()).thenReturn(1.0f);
    when(_mockDataStore.getFirstLocationLongitude()).thenReturn(3.0f);
    long ts = 1200000000000l;
    when(_mockTime.getCurrentTimeMilliseconds()).thenReturn(ts);

    _serviceCommand.execute(_app);
    _serviceCommand.onGPSChangeState(new GPSChangeState(BaseChangeState.State.START));

    ArgumentCaptor<LocationListener> locationListenerCaptor = ArgumentCaptor.forClass(LocationListener.class);
    verify(_mockLocationManager,timeout(1000).times(1)).requestLocationUpdates(
            anyString(),
            anyLong(),
            anyFloat(),
            locationListenerCaptor.capture());

    Location location = new Location("location");
    LocationListener listenerArgument = locationListenerCaptor.getValue();
    when(_mockTime.getCurrentTimeMilliseconds()).thenReturn(ts+10000);
    listenerArgument.onLocationChanged(location);

    ArgumentCaptor<NewLocation> captor = ArgumentCaptor.forClass(NewLocation.class);
    verify(_bus,timeout(1000).atLeast(1)).post(captor.capture());

    int nb = 0;
    for (int i = 0; i < captor.getAllValues().size(); i++) {
        try {
            NewLocation newLocation = captor.getAllValues().get(i);
            nb++;
        } catch (ClassCastException e) {
            // other type
        }
    }
    assertEquals(2, nb);
    // 2: one with onGPSChangeState (saved one), one with onLocationChanged
}
 
Example 4
Source File: InstrumentationService.java    From sana.mobile with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@SuppressLint("NewApi")
public void handleMessage(Message msg) {
    Log.i(TAG, "msg obj = " + String.valueOf(msg.obj));
    switch (msg.what) {
        case MSG_GET_LOCATION:
            Uri uri = Uri.parse(msg.obj.toString());
            if (msg.getData() != null) {
                //Intent reply = msg.getData().getParcelable(Intent.EXTRA_INTENT);
                PendingIntent replyTo = msg.getData().getParcelable(Intent.EXTRA_INTENT);
                //Log.d(TAG, "replyTo: " + String.valueOf(replyTo));
                LocationListener listener = getListener(null, uri, msg.arg1, msg.getData());
                try {
                    Criteria criteria = new Criteria();
                    if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.FROYO) {
                        criteria.setPowerRequirement(Criteria.POWER_HIGH);
                        criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
                    } else {
                        criteria.setAccuracy(Criteria.ACCURACY_FINE);
                    }
                    List<String> providers = locationManager.getProviders(criteria, true);
                    for (String provider : providers) {
                        Log.d(TAG, "Using location provider: " + provider);
                        locationManager.requestLocationUpdates(provider, 0, 0, listener);
                    }
                    //if(TextUtils.isEmpty(provider))
                    //	throw new IllegalArgumentException("No location providers available");
                    // add to our listeners so that we can clean up later if necessary
                    //replies.put(msg.arg1, replyTo);
                    if (providers.size() == 0) {
                        Location nullLocation = new Location(LocationManager.GPS_PROVIDER);
                        nullLocation.setAccuracy(0);
                        nullLocation.setLatitude(0);
                        nullLocation.setLongitude(0);
                        listener.onLocationChanged(nullLocation);
                    } else {
                        listeners.put(msg.arg1, listener);
                    }
                } catch (Exception e) {
                    Log.e(TAG, "Error getting location updates: " + e.getMessage());
                    e.printStackTrace();
                    removeListener(msg.arg1);
                }
            } else {
                Log.w(TAG, "no replyTo in original intent sent to InstrumentationService");
                removeListener(msg.arg1);
            }
            break;
        default:
            Log.w(TAG, "Unknown message! Message = " + msg.what);
            removeListener(msg.arg1);
    }
}