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

Commit

Permalink
Update engine's location requests (#215)
Browse files Browse the repository at this point in the history
* Add equals & hashCode to LocationRequest

* remove location request and update engine when location updates removed

* Add sample for multiple clients w/diff request priorities

* newline

* batch request removal and improve request management
  • Loading branch information
sarahsnow1 authored and msmollin committed Jun 16, 2017
1 parent a7161c3 commit 40c3a4c
Show file tree
Hide file tree
Showing 22 changed files with 849 additions and 59 deletions.
1 change: 1 addition & 0 deletions lost-sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<activity android:name=".LocationAvailabilityActivity"/>
<activity android:name=".MultipleLocationListenerMultipleClientsActivity"/>
<activity android:name=".MultipleLocationListenerSingleClientActivity"/>
<activity android:name=".MultiplePriorityMultipleClientsActivity"/>

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

import com.mapzen.android.lost.api.LocationListener;
import com.mapzen.android.lost.api.LocationRequest;
import com.mapzen.android.lost.api.LocationServices;
import com.mapzen.android.lost.api.LostApiClient;

import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

/**
* Demonstrates two {@link LostApiClient}s receiving location updates with different request
* priorities.
*/
public class MultiplePriorityMultipleClientsActivity extends LostApiClientActivity {

private static final String TAG = MultiplePriorityMultipleClientsActivity.class.getSimpleName();

LostApiClient otherClient;

LocationListener noPowerListener = new LocationListener() {
@Override public void onLocationChanged(Location location) {
Log.d(TAG, "No Power Client Location:" + location.getLongitude() + " " +
location.getLatitude());
}
};

LocationListener highPriorityListener = new LocationListener() {
@Override public void onLocationChanged(Location location) {
Log.d(TAG, "High Priority Client Location:" + location.getLongitude() + " " +
location.getLatitude());
}
};

@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiple_clients_requests);
createOtherClient();
setupBtns();
}

@Override public void onConnected() {
super.onConnected();
requestNoPowerUpdates();
}

private void setupBtns() {
findViewById(R.id.connect_no_power_btn).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
client.connect();
}
});
findViewById(R.id.connect_high_priority_btn).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
otherClient.connect();
}
});
findViewById(R.id.rm_no_power_btn).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
LocationServices.FusedLocationApi.removeLocationUpdates(client, noPowerListener);
}
});
findViewById(R.id.rm_high_priority_btn).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
LocationServices.FusedLocationApi.removeLocationUpdates(otherClient, highPriorityListener);
}
});
}

private void requestNoPowerUpdates() {
LocationRequest request = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_NO_POWER)
.setFastestInterval(0)
.setSmallestDisplacement(0)
.setInterval(1000); // 1 sec
LocationServices.FusedLocationApi.requestLocationUpdates(client, request, noPowerListener);
}

private void createOtherClient() {
otherClient = new LostApiClient.Builder(this)
.addConnectionCallbacks(new LostApiClient.ConnectionCallbacks() {

@Override public void onConnected() {
Log.d("Test", "High priority onConnected");

LocationRequest request = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setFastestInterval(0)
.setSmallestDisplacement(0)
.setInterval(1000); // 1 sec

LocationServices.FusedLocationApi.requestLocationUpdates(otherClient, request,
highPriorityListener);
}

@Override public void onConnectionSuspended() {

}
})
.build();
}
}
3 changes: 3 additions & 0 deletions lost-sample/src/main/java/com/example/lost/SamplesList.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ private SamplesList() {
new Sample(R.string.sample_multiple_clients_diff_intervals_title,
R.string.sample_multiple_clients_diff_intervals_description,
MultipleLocationListenerMultipleClientsActivity.class),
new Sample(R.string.sample_multiple_clients_priorities_title,
R.string.sample_multiple_clients_priorities_description,
MultiplePriorityMultipleClientsActivity.class),
new Sample(R.string.sample_single_client_diff_intervals_title,
R.string.sample_single_client_diff_intervals_description,
MultipleLocationListenerSingleClientActivity.class),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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:text="Connect no power client"
android:id="@+id/connect_no_power_btn"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect high priority client"
android:id="@+id/connect_high_priority_btn"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rm no power requests"
android:id="@+id/rm_no_power_btn"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rm high priority requests"
android:id="@+id/rm_high_priority_btn"
/>


</LinearLayout>
2 changes: 2 additions & 0 deletions lost-sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,7 @@
<string name="sample_multiple_clients_diff_intervals_description">Demonstrates how multiple clients can request location updates at different fastest intervals</string>
<string name="sample_single_client_diff_intervals_title">Single Client w/different Intervals</string>
<string name="sample_single_client_diff_intervals_description">Demonstrates how a single client can request location updates at different fastest intervals</string>
<string name="sample_multiple_clients_priorities_title">Multiple Clients w/different request priorities</string>
<string name="sample_multiple_clients_priorities_description">Demonstrates multiple clients receiving location requests for different location priorities</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface IFusedLocationProviderService {

void requestLocationUpdates(in LocationRequest request);

void removeLocationUpdates();
void removeLocationUpdates(in List<LocationRequest> requests);

void setMockMode(boolean isMockMode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public static LocationRequest create() {
return new LocationRequest();
}

public LocationRequest(LocationRequest incoming) {
this.setInterval(incoming.getInterval());
this.setFastestInterval(incoming.getFastestInterval());
this.setSmallestDisplacement(incoming.getSmallestDisplacement());
this.setPriority(incoming.getPriority());
}

public long getInterval() {
return interval;
}
Expand Down Expand Up @@ -73,6 +80,38 @@ public LocationRequest setPriority(int priority) {
return this;
}

@Override public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

LocationRequest that = (LocationRequest) o;

if (interval != that.interval) {
return false;
}
if (fastestInterval != that.fastestInterval) {
return false;
}
if (Float.compare(that.smallestDisplacement, smallestDisplacement) != 0) {
return false;
}
return priority == that.priority;
}

@Override public int hashCode() {
int result = (int) (interval ^ (interval >>> 32));
result = 31 * result + (int) (fastestInterval ^ (fastestInterval >>> 32));
result =
31 * result + (smallestDisplacement != +0.0f ? Float.floatToIntBits(smallestDisplacement)
: 0);
result = 31 * result + priority;
return result;
}

@Override public int describeContents() {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mapzen.android.lost.internal.FusedLocationServiceConnectionManager;
import com.mapzen.android.lost.internal.GeofencingApiImpl;
import com.mapzen.android.lost.internal.GeofencingServiceIntentFactory;
import com.mapzen.android.lost.internal.LostRequestManager;
import com.mapzen.android.lost.internal.PendingIntentIdGenerator;
import com.mapzen.android.lost.internal.SettingsApiImpl;

Expand All @@ -19,7 +20,7 @@ public class LocationServices {
*/
public static final FusedLocationProviderApi FusedLocationApi =
new FusedLocationProviderApiImpl(new FusedLocationServiceConnectionManager(),
new FusedLocationServiceCallbackManager());
new FusedLocationServiceCallbackManager(), LostRequestManager.shared());

/**
* Entry point for APIs concerning geofences.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.mapzen.android.lost.internal;

import com.mapzen.android.lost.api.LostApiClient;


/**
* Wraps a {@link LostApiClient} to either a {@link com.mapzen.android.lost.api.LocationListener},
* {@link android.app.PendingIntent}, or {@link com.mapzen.android.lost.api.LocationCallback}.
*/
class ClientCallbackWrapper {

private LostApiClient client;
private Object callback;

public <T> ClientCallbackWrapper(LostApiClient client, T callback) {
this.client = client;
this.callback = callback;
}

@Override public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

ClientCallbackWrapper wrapper = (ClientCallbackWrapper) o;

if (!client.equals(wrapper.client)) {
return false;
}
return callback.equals(wrapper.callback);
}

@Override public int hashCode() {
int result = client.hashCode();
result = 31 * result + callback.hashCode();
return result;
}
}
Loading

0 comments on commit 40c3a4c

Please sign in to comment.