Skip to content
This repository has been archived by the owner on Apr 23, 2023. It is now read-only.

Location provider enabled/disabled #137

Merged
merged 8 commits into from
Nov 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lost-sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
<activity android:name=".SettingsApiActivity"/>
<activity android:name=".LocationListenerActivity"/>
<activity android:name=".PendingIntentActivity"/>

<activity android:name=".LocationAvailabilityActivity"/>

<service
android:name=".GeofenceIntentService"
android:exported="false">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.example.lost;

import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.mapzen.android.lost.api.LocationAvailability;
import com.mapzen.android.lost.api.LocationCallback;
import com.mapzen.android.lost.api.LocationRequest;
import com.mapzen.android.lost.api.LocationResult;
import com.mapzen.android.lost.api.LocationServices;
import com.mapzen.android.lost.api.LostApiClient;

import static com.mapzen.android.lost.api.LocationRequest.PRIORITY_HIGH_ACCURACY;

public class LocationAvailabilityActivity extends AppCompatActivity {

LostApiClient client;

LocationCallback callback = new LocationCallback() {
@Override
public void onLocationAvailability(LocationAvailability locationAvailability) {
boolean isAvailable = locationAvailability.isLocationAvailable();
Toast.makeText(LocationAvailabilityActivity.this, isAvailable ?
R.string.location_available : R.string.location_unavailable,
Toast.LENGTH_SHORT).show();
}

@Override
public void onLocationResult(LocationResult result) {

}
};

@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_availability);

setupLocationAvailabilityBtn();
}

private void setupLocationAvailabilityBtn() {
Button connect = (Button) findViewById(R.id.check_availability);
connect.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (client == null || !client.isConnected()) {
connect();
} else {
checkLocationAvailability();
}
}
});
}

private void connect() {
client = new LostApiClient.Builder(this).addConnectionCallbacks(
new LostApiClient.ConnectionCallbacks() {
@Override
public void onConnected() {
checkLocationAvailability();
}

@Override
public void onConnectionSuspended() {

}
}).build();
client.connect();
}

private void checkLocationAvailability() {
LocationAvailability availability =
LocationServices.FusedLocationApi.getLocationAvailability(client);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While exercising a call to FusedLocationApi.getLocationAvailability(client) is good we should also verify that LocationCallback is properly invoked when location updates have been requested and provider status changes occur.

https://github.com/mapzen/lost/blob/master/lost/src/main/java/com/mapzen/android/lost/api/FusedLocationProviderApi.java#L100-L101

This is the use case that would more suitably replace LocationListener.onProviderEnabled(String provider) and LocationListener.onProviderDisabled(String provider) since the client app is relying on a push style notification rather than actively polling the provider status.

boolean isAvailable = availability.isLocationAvailable();
Toast.makeText(LocationAvailabilityActivity.this, isAvailable ? R.string.location_available
: R.string.location_unavailable, Toast.LENGTH_SHORT).show();

final LocationRequest locationRequest = LocationRequest.create()
.setPriority(PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(client, locationRequest, callback,
Looper.myLooper());

}
}
4 changes: 3 additions & 1 deletion lost-sample/src/main/java/com/example/lost/SamplesList.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ private SamplesList() {
new Sample(R.string.sample_location_listener_title,
R.string.sample_location_listener_description, LocationListenerActivity.class),
new Sample(R.string.sample_pending_intent_title,
R.string.sample_pending_intent_description, PendingIntentActivity.class)
R.string.sample_pending_intent_description, PendingIntentActivity.class),
new Sample(R.string.sample_location_availability_title,
R.string.sample_location_availability_description, LocationAvailabilityActivity.class)
};
}
12 changes: 12 additions & 0 deletions lost-sample/src/main/res/layout/activity_location_availability.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check_availability"
android:text="@string/check_availability"/>

</LinearLayout>
5 changes: 5 additions & 0 deletions lost-sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,10 @@
<string name="sample_pending_intent_title">PendingIntent</string>
<string name="sample_pending_intent_description">Demonstrates how to use a PendingIntent to receive location updates</string>
<string name="requested">Requested</string>
<string name="sample_location_availability_title">Location Availability</string>
<string name="sample_location_availability_description">Demonstrates how to determine if location requested is available</string>
<string name="check_availability">Check Availability</string>
<string name="location_available">Location Available</string>
<string name="location_unavailable">Location NOT Available</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,28 @@ public interface LocationListener {
void onLocationChanged(Location location);

/**
* Called when a location provider is disabled.
* Called when a location provider is disabled. You will only receive updates for the priority
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

* level set in the location request used to register for updates in
* {@link FusedLocationProviderApi#requestLocationUpdates(LostApiClient, LocationRequest,
* LocationListener)}. Ie. {@link LocationRequest#PRIORITY_HIGH_ACCURACY} will invoke this method
* for gps and network changes but {@link LocationRequest#PRIORITY_BALANCED_POWER_ACCURACY} will
* only invoke it for network changes. This method will be removed in the next major release, it
* is recommended that you use {@link LocationAvailability} instead.
* @param provider the disabled provider.
*/
@Deprecated
void onProviderDisabled(String provider);

/**
* Called when a location provider is enabled.
* Called when a location provider is enabled. You will only receive updates for the priority
* level set in the location request used to register for updates in
* {@link FusedLocationProviderApi#requestLocationUpdates(LostApiClient, LocationRequest,
* LocationListener)}. Ie. {@link LocationRequest#PRIORITY_HIGH_ACCURACY} will invoke this method
* for gps and network changes but {@link LocationRequest#PRIORITY_BALANCED_POWER_ACCURACY} will
* only invoke it for network changes. This method will be removed in the next major release, it
* is recommended that you use {@link LocationAvailability} instead.
* @param provider the enabled provider.
*/
@Deprecated
void onProviderEnabled(String provider);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import android.app.PendingIntent;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.RequiresPermission;

Expand Down Expand Up @@ -143,6 +145,28 @@ public void reportProviderDisabled(String provider) {
public void reportProviderEnabled(String provider) {
clientManager.reportProviderEnabled(provider);
notifyLocationAvailabilityChanged();
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
manager.requestSingleUpdate(provider, new android.location.LocationListener() {
@Override
public void onLocationChanged(Location location) {
notifyLocationAvailabilityChanged();
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}
}, Looper.myLooper());
}

public Map<LostApiClient, Set<LocationListener>> getLocationListeners() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package com.mapzen.android.lost.internal;

import android.app.IntentService;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Environment;
import android.os.Looper;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.mapzen.android.lost.api.LocationAvailability;
import com.mapzen.android.lost.api.LocationCallback;
import com.mapzen.android.lost.api.LocationListener;
Expand All @@ -8,38 +20,23 @@
import com.mapzen.android.lost.api.LostApiClient;
import com.mapzen.android.lost.api.PendingResult;
import com.mapzen.android.lost.api.Status;
import com.mapzen.android.lost.shadows.LostShadowLocationManager;
import com.mapzen.lost.BuildConfig;

import com.google.common.base.Charsets;
import com.google.common.io.Files;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.internal.ShadowExtractor;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.shadows.ShadowEnvironment;
import org.robolectric.shadows.ShadowLocationManager;
import org.robolectric.shadows.ShadowLooper;
import org.robolectric.util.ReflectionHelpers;

import android.app.IntentService;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Environment;
import android.os.Looper;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static android.content.Context.LOCATION_SERVICE;
Expand All @@ -56,23 +53,25 @@

@RunWith(RobolectricGradleTestRunner.class)
@SuppressWarnings("MissingPermission")
@Config(constants = BuildConfig.class, sdk = 21, manifest = Config.NONE)
@Config(constants = BuildConfig.class, sdk = 21, manifest = Config.NONE, shadows = {
LostShadowLocationManager.class})
public class FusedLocationProviderServiceImplTest {
private LostApiClient client;
private FusedLocationProviderServiceImpl api;
private LocationManager locationManager;
private ShadowLocationManager shadowLocationManager;
private LostShadowLocationManager shadowLocationManager;
private LostApiClient otherClient;
private ClientManager clientManager;

@Before public void setUp() throws Exception {

mockService();
client = new LostApiClient.Builder(mock(Context.class)).build();
otherClient = new LostApiClient.Builder(mock(Context.class)).build();
clientManager = LostClientManager.shared();
api = new FusedLocationProviderServiceImpl(application, clientManager);
locationManager = (LocationManager) application.getSystemService(LOCATION_SERVICE);
shadowLocationManager = shadowOf(locationManager);
shadowLocationManager = (LostShadowLocationManager) ShadowExtractor.extract(locationManager);
client.connect();
}

Expand Down Expand Up @@ -525,7 +524,6 @@ private File getTestGpxTrace() throws IOException {
PendingIntent pendingIntent1 = PendingIntent.getService(application, 0, intent, 0);
PendingIntent pendingIntent2 = PendingIntent.getService(application, 0, intent, 0);
api.requestLocationUpdates(client, request, pendingIntent1);
clearShadowLocationListeners();
api.requestLocationUpdates(client, request, pendingIntent2);

api.removeLocationUpdates(client, pendingIntent2);
Expand Down Expand Up @@ -653,8 +651,7 @@ private File getTestGpxTrace() throws IOException {
new TestLocationListener());

api.shutdown();
LocationManager lm = (LocationManager) application.getSystemService(LOCATION_SERVICE);
assertThat(shadowOf(lm).getRequestLocationUpdateListeners()).isEmpty();
assertThat(shadowLocationManager.getRequestLocationUpdateListeners()).isEmpty();
}

@Test public void shutdown_shouldClearListeners() {
Expand Down Expand Up @@ -997,18 +994,6 @@ private File getTestGpxTrace() throws IOException {
assertThat(otherCallback.getStatus().getStatusCode()).isEqualTo(Status.SUCCESS);
}

/**
* Due to a bug in Robolectric that allows the same location listener to be registered twice,
* we need to manually clear the `ShadowLocationManager` to prevent duplicate listeners.
*
* @see <a href="https://github.com/robolectric/robolectric/issues/2603">
* ShadowLocationManager should not allow duplicate listeners</a>
*/
private void clearShadowLocationListeners() {
Map<String, List> map = ReflectionHelpers.getField(shadowLocationManager, "locationListeners");
map.clear();
}

public class TestService extends IntentService {
public TestService() {
super("test");
Expand Down
Loading