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

Multiple Processes #232

Merged
merged 5 commits into from
Jul 6, 2017
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 @@ -38,6 +38,7 @@
<activity android:name=".MultipleLocationListenerMultipleClientsActivity"/>
<activity android:name=".MultipleLocationListenerSingleClientActivity"/>
<activity android:name=".MultiplePriorityMultipleClientsActivity"/>
<activity android:name=".MultipleProcessesActivity"/>

<service
android:name=".GeofenceIntentService"
Expand All @@ -48,7 +49,7 @@
<action android:name="com.mapzen.lost.intent.action.PENDING_INTENT_SERVICE"/>
</intent-filter>
</service>

<service android:name=".ListenerService" android:process=":listenerservice"/>

</application>
</manifest>
57 changes: 57 additions & 0 deletions lost-sample/src/main/java/com/example/lost/ListenerService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
* Service running in separate process to request location updates.
*/
public class ListenerService extends Service {

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

LostApiClient client;
LocationListener listener = new LocationListener() {
@Override public void onLocationChanged(Location location) {
Log.d(TAG, "Location Changed");
}
};

@Nullable @Override public IBinder onBind(Intent intent) {
return null;
}

@Override public void onCreate() {
super.onCreate();
client = new LostApiClient.Builder(this).addConnectionCallbacks(
new LostApiClient.ConnectionCallbacks() {
@Override public void onConnected() {
LocationRequest request = LocationRequest.create();
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
request.setInterval(100);

LocationServices.FusedLocationApi.requestLocationUpdates(client, request, listener);
}

@Override public void onConnectionSuspended() {

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

@Override public void onDestroy() {
super.onDestroy();
LocationServices.FusedLocationApi.removeLocationUpdates(client, listener);
client.disconnect();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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 android.content.Intent;
import android.location.Location;
import android.util.Log;
import android.widget.Toast;

/**
* Demonstrates two different processes requesting location updates.
*/
public class MultipleProcessesActivity extends PendingIntentActivity {

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

LocationListener listener = new LocationListener() {
@Override public void onLocationChanged(Location location) {
Log.d(TAG, "Location Changed");
}
};

@Override protected void onResume() {
super.onResume();
Intent intent = new Intent(this, ListenerService.class);
startService(intent);
}

@Override protected void onPause() {
super.onPause();
Intent intent = new Intent(this, ListenerService.class);
stopService(intent);
}

void requestLocationUpdates() {
LocationRequest request = LocationRequest.create();
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
request.setInterval(100);

LocationServices.FusedLocationApi.requestLocationUpdates(client, request, listener);

Toast.makeText(this, R.string.requested, Toast.LENGTH_SHORT).show();
}

void unregisterAndDisconnectClient() {
LocationServices.FusedLocationApi.removeLocationUpdates(client, listener);
disconnect();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ private void setupDisconnectBtn() {
Button disconnect = (Button) findViewById(R.id.disconnect);
disconnect.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
LocationServices.FusedLocationApi.removeLocationUpdates(client, pendingIntent);
disconnect();
unregisterAndDisconnectClient();
}
});
}

private void requestLocationUpdates() {
void requestLocationUpdates() {
LocationRequest request = LocationRequest.create();
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
request.setInterval(100);
Expand All @@ -67,4 +66,9 @@ private void requestLocationUpdates() {

Toast.makeText(PendingIntentActivity.this, R.string.requested, Toast.LENGTH_SHORT).show();
}

void unregisterAndDisconnectClient() {
LocationServices.FusedLocationApi.removeLocationUpdates(client, pendingIntent);
disconnect();
}
}
2 changes: 2 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 @@ -28,6 +28,8 @@ private SamplesList() {
MultipleLocationListenerSingleClientActivity.class),
new Sample(R.string.sample_pending_intent_title,
R.string.sample_pending_intent_description, PendingIntentActivity.class),
new Sample(R.string.sample_multiple_process_title,
R.string.sample_multiple_process_description, MultipleProcessesActivity.class),
new Sample(R.string.sample_location_availability_title,
R.string.sample_location_availability_description, LocationAvailabilityActivity.class)
};
Expand Down
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 @@ -82,5 +82,7 @@
<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>
<string name="sample_multiple_process_title">Multiple processes w/different clients</string>
<string name="sample_multiple_process_description">Demonstrates using lost in the main app process as well as in a service running in its own process</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.mapzen.android.lost.api.LocationAvailability;

interface IFusedLocationProviderCallback {

long pid();

void onLocationChanged(in Location location);

void onLocationAvailabilityChanged(in LocationAvailability locationAvailability);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import com.mapzen.android.lost.internal.IFusedLocationProviderCallback;

interface IFusedLocationProviderService {

void init(in IFusedLocationProviderCallback callback);
void add(in IFusedLocationProviderCallback callback);

void remove(in IFusedLocationProviderCallback callback);

Location getLastLocation();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.mapzen.android.lost.api;

import com.mapzen.android.lost.internal.PidReader;

import android.os.Parcel;
import android.os.Parcelable;

import static android.os.Process.myPid;

public final class LocationRequest implements Parcelable {
public static final int PRIORITY_HIGH_ACCURACY = 0x00000064;
public static final int PRIORITY_BALANCED_POWER_ACCURACY = 0x00000066;
Expand All @@ -17,19 +21,40 @@ public final class LocationRequest implements Parcelable {
private long fastestInterval = DEFAULT_FASTEST_INTERVAL_IN_MS;
private float smallestDisplacement = DEFAULT_SMALLEST_DISPLACEMENT_IN_METERS;
private int priority = PRIORITY_BALANCED_POWER_ACCURACY;
private PidReader pidReader = new PidReader() {
@Override public long getPid() {
return myPid();
}
};
long pid;

private LocationRequest() {
commonInit();
}

private LocationRequest(PidReader reader) {
pidReader = reader;
commonInit();
}

private void commonInit() {
pid = pidReader.getPid();
}

public static LocationRequest create() {
return new LocationRequest();
}

public static LocationRequest create(PidReader reader) {
return new LocationRequest(reader);
}

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

public long getInterval() {
Expand Down Expand Up @@ -90,6 +115,9 @@ public LocationRequest setPriority(int priority) {

LocationRequest that = (LocationRequest) o;

if (pid != that.pid) {
return false;
}
if (interval != that.interval) {
return false;
}
Expand All @@ -109,6 +137,7 @@ public LocationRequest setPriority(int priority) {
31 * result + (smallestDisplacement != +0.0f ? Float.floatToIntBits(smallestDisplacement)
: 0);
result = 31 * result + priority;
result = 31 * result + (int) pid;
return result;
}

Expand All @@ -121,13 +150,15 @@ public LocationRequest setPriority(int priority) {
dest.writeLong(this.fastestInterval);
dest.writeFloat(this.smallestDisplacement);
dest.writeInt(this.priority);
dest.writeLong(this.pid);
}

protected LocationRequest(Parcel in) {
this.interval = in.readLong();
this.fastestInterval = in.readLong();
this.smallestDisplacement = in.readFloat();
this.priority = in.readInt();
this.pid = in.readLong();
}

public static final Parcelable.Creator<LocationRequest> CREATOR =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.Map;
import java.util.Set;

import static android.os.Process.myPid;

/**
* Implementation of the {@link FusedLocationProviderApi}.
*/
Expand All @@ -41,8 +43,13 @@ public class FusedLocationProviderApiImpl extends ApiImpl

IFusedLocationProviderService service;

private IFusedLocationProviderCallback.Stub remoteCallback
IFusedLocationProviderCallback.Stub remoteCallback
= new IFusedLocationProviderCallback.Stub() {

public long pid() throws RemoteException {
return myPid();
}

public void onLocationChanged(final Location location) throws RemoteException {

new Handler(Looper.getMainLooper()).post(new Runnable() {
Expand Down Expand Up @@ -295,7 +302,7 @@ FusedLocationServiceConnectionManager getServiceConnectionManager() {
void registerRemoteCallback() {
if (service != null) {
try {
service.init(remoteCallback);
service.add(remoteCallback);
} catch (RemoteException e) {
Log.e(TAG, "Error occurred trying to register remote callback", e);
}
Expand All @@ -305,7 +312,7 @@ void registerRemoteCallback() {
void unregisterRemoteCallback() {
if (service != null) {
try {
service.init(null);
service.remove(remoteCallback);
} catch (RemoteException e) {
Log.e(TAG, "Error occurred trying to unregister remote callback", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ public class FusedLocationProviderService extends Service {

private final IFusedLocationProviderService.Stub binder =
new IFusedLocationProviderService.Stub() {
@Override public void init(IFusedLocationProviderCallback callback) throws RemoteException {
delegate.init(callback);
@Override public void add(IFusedLocationProviderCallback callback) throws RemoteException {
delegate.add(callback);
}

@Override public void remove(IFusedLocationProviderCallback callback) throws
RemoteException {
delegate.remove(callback);
}

@Override public Location getLastLocation() throws RemoteException {
Expand Down
Loading