Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit aec1ad93 authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

Add Geofencing API client code

parent 3eac7fc7
Loading
Loading
Loading
Loading
Compare c61a147d to 3f1fa813
Original line number Diff line number Diff line
Subproject commit c61a147de29456270bce1203c9f6700268b068c5
Subproject commit 3f1fa81390d67053d3aa50b0bc3537953396bcf6
+28 −0
Original line number Diff line number Diff line
@@ -16,5 +16,33 @@

package com.google.android.gms.location;

import android.app.PendingIntent;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;

import java.util.List;

/**
 * The main entry point for interacting with the geofencing APIs.
 * <p>
 * The methods must be used in conjunction with a GoogleApiClient. E.g.
 * <pre>
 *  new GoogleApiClient.Builder(context)
 *          .addApi(LocationServices.API)
 *          .addConnectionCallbacks(this)
 *          .addOnConnectionFailedListener(this)
 *          .build()
 * </pre>
 */
public interface GeofencingApi {
    PendingResult<Status> addGeofences(GoogleApiClient client, GeofencingRequest geofencingRequest, PendingIntent pendingIntent);

    @Deprecated
    PendingResult<Status> addGeofences(GoogleApiClient client, List<Geofence> geofences, PendingIntent pendingIntent);

    PendingResult<Status> removeGeofences(GoogleApiClient client, List<String> geofenceRequestIds);

    PendingResult<Status> removeGeofences(GoogleApiClient client, PendingIntent pendingIntent);
}
+0 −1
Original line number Diff line number Diff line
@@ -137,5 +137,4 @@ public class FusedLocationProviderApiImpl implements FusedLocationProviderApi {
    private interface Runnable {
        void run(LocationClientImpl client) throws RemoteException;
    }

}
+94 −0
Original line number Diff line number Diff line
@@ -16,7 +16,101 @@

package org.microg.gms.location;

import android.app.PendingIntent;
import android.os.RemoteException;
import android.support.annotation.NonNull;
import android.util.Log;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingApi;
import com.google.android.gms.location.GeofencingRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.internal.IGeofencerCallbacks;
import com.google.android.gms.location.internal.ParcelableGeofence;

import org.microg.gms.common.GmsConnector;

import java.util.ArrayList;
import java.util.List;

public class GeofencingApiImpl implements GeofencingApi {
    @Override
    public PendingResult<Status> addGeofences(GoogleApiClient client, final GeofencingRequest geofencingRequest, final PendingIntent pendingIntent) {
        return callGeofencer(client, new Runnable() {
            @Override
            public void run(LocationClientImpl client, IGeofencerCallbacks callbacks) throws RemoteException {
                client.addGeofences(geofencingRequest, pendingIntent, callbacks);
            }
        });
    }

    @Override
    public PendingResult<Status> addGeofences(GoogleApiClient client, final List<Geofence> geofences, final PendingIntent pendingIntent) {
        final List<ParcelableGeofence> geofenceList = new ArrayList<ParcelableGeofence>();
        for (Geofence geofence : geofences) {
            if (geofence instanceof ParcelableGeofence) geofenceList.add((ParcelableGeofence) geofence);
        }
        return callGeofencer(client, new Runnable() {
            @Override
            public void run(LocationClientImpl client, IGeofencerCallbacks callbacks) throws RemoteException {
                client.addGeofences(geofenceList, pendingIntent, callbacks);
            }
        });
    }

    @Override
    public PendingResult<Status> removeGeofences(GoogleApiClient client, final List<String> geofenceRequestIds) {
        return callGeofencer(client, new Runnable() {
            @Override
            public void run(LocationClientImpl client, IGeofencerCallbacks callbacks) throws RemoteException {
                client.removeGeofences(geofenceRequestIds, callbacks);
            }
        });
    }

    @Override
    public PendingResult<Status> removeGeofences(GoogleApiClient client, final PendingIntent pendingIntent) {
        return callGeofencer(client, new Runnable() {
            @Override
            public void run(LocationClientImpl client, IGeofencerCallbacks callbacks) throws RemoteException {
                client.removeGeofences(pendingIntent, callbacks);
            }
        });
    }

    @NonNull
    private IGeofencerCallbacks.Stub createGeofencerCallbacks(final GmsConnector.Callback.ResultProvider<Status> resultProvider) {
        return new IGeofencerCallbacks.Stub(){
            @Override
            public void onAddGeofenceResult(int statusCode, String[] requestIds) throws RemoteException {
                resultProvider.onResultAvailable(new Status(statusCode));
            }

            @Override
            public void onRemoveGeofencesByRequestIdsResult(int statusCode, String[] requestIds) throws RemoteException {
                resultProvider.onResultAvailable(new Status(statusCode));
            }

            @Override
            public void onRemoveGeofencesByPendingIntentResult(int statusCode, PendingIntent pendingIntent) throws RemoteException {
                resultProvider.onResultAvailable(new Status(statusCode));
            }
        };
    }

    private PendingResult<Status> callGeofencer(GoogleApiClient client, final Runnable runnable) {
        return GmsConnector.call(client, LocationServices.API, new GmsConnector.Callback<LocationClientImpl, Status>() {
            @Override
            public void onClientAvailable(LocationClientImpl client, ResultProvider<Status> resultProvider) throws RemoteException {
                runnable.run(client, createGeofencerCallbacks(resultProvider));
            }
        });
    }

    private interface Runnable {
        void run(LocationClientImpl client, IGeofencerCallbacks callbacks) throws RemoteException;
    }
}
+38 −2
Original line number Diff line number Diff line
@@ -25,14 +25,18 @@ import android.os.RemoteException;
import android.util.Log;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.GeofencingRequest;
import com.google.android.gms.location.ILocationListener;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.internal.IGeofencerCallbacks;
import com.google.android.gms.location.internal.ParcelableGeofence;

import org.microg.gms.common.api.GoogleApiClientImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LocationClientImpl extends GoogleLocationManagerClient {
@@ -55,6 +59,38 @@ public class LocationClientImpl extends GoogleLocationManagerClient {
        return null;
    }

    public void addGeofences(GeofencingRequest request, PendingIntent pendingIntent, IGeofencerCallbacks callbacks) throws RemoteException {
        if (nativeLocation != null) {
            nativeLocation.addGeofences(request, pendingIntent, callbacks);
        } else {
            getServiceInterface().addGeofences(request, pendingIntent, callbacks);
        }
    }

    public void addGeofences(List<ParcelableGeofence> request, PendingIntent pendingIntent, IGeofencerCallbacks callbacks) throws RemoteException {
        if (nativeLocation != null) {
            nativeLocation.addGeofences(request, pendingIntent, callbacks);
        } else {
            getServiceInterface().addGeofencesList(request, pendingIntent, callbacks, getContext().getPackageName());
        }
    }

    public void removeGeofences(List<String> geofenceRequestIds, IGeofencerCallbacks callbacks) throws RemoteException {
        if (nativeLocation != null) {
            nativeLocation.removeGeofences(geofenceRequestIds, callbacks);
        } else {
            getServiceInterface().removeGeofencesById(geofenceRequestIds.toArray(new String[geofenceRequestIds.size()]), callbacks, getContext().getPackageName());
        }
    }

    public void removeGeofences(PendingIntent pendingIntent, IGeofencerCallbacks callbacks) throws RemoteException {
        if (nativeLocation != null) {
            nativeLocation.removeGeofences(pendingIntent, callbacks);
        } else {
            getServiceInterface().removeGeofencesByIntent(pendingIntent, callbacks, getContext().getPackageName());
        }
    }

    public Location getLastLocation() throws RemoteException {
        Log.d(TAG, "getLastLocation()");
        if (nativeLocation != null) {
Loading