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

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

Add package name to location requests when possible, fix incomplete permission check

parent c927f1a8
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package org.microg.gms.common;

import android.app.ActivityManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.pm.PackageInfo;
@@ -119,6 +120,16 @@ public class PackageUtils {
        return null;
    }

    @Nullable
    public static String packageFromProcessId(Context context, int pid) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (manager == null) return null;
        for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
            if (processInfo.pid == pid) return processInfo.processName;
        }
        return null;
    }

    @SuppressWarnings("deprecation")
    public static String packageFromPendingIntent(PendingIntent pi) {
        if (pi == null) return null;
+7 −2
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package org.microg.gms.common;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.R;
@@ -53,8 +54,12 @@ public class Utils {
    }

    public static boolean hasSelfPermissionOrNotify(Context context, String permission) {
        if (ContextCompat.checkSelfPermission(context, permission) != PERMISSION_GRANTED) {
        if (context.checkCallingOrSelfPermission(permission) != PERMISSION_GRANTED) {
            try {
                Toast.makeText(context, context.getString(R.string.lacking_permission_toast, permission), Toast.LENGTH_SHORT).show();
            } catch (RuntimeException e) {
                Log.w("GmsUtils", "Lacking permission to " + permission + " for pid:" + android.os.Process.myPid() + " uid:" + android.os.Process.myUid());
            }
            return false;
        }
        return true;
+6 −1
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.internal.FusedLocationProviderResult;
import com.google.android.gms.location.internal.LocationRequestUpdateData;

import org.microg.gms.common.PackageUtils;
import org.microg.gms.common.Utils;

import java.util.ArrayList;
@@ -42,6 +43,7 @@ import static com.google.android.gms.location.LocationRequest.PRIORITY_HIGH_ACCU
import static com.google.android.gms.location.LocationRequest.PRIORITY_NO_POWER;

public class GoogleLocationManager implements LocationChangeListener {
    private static final String TAG = "GmsLocManager";
    private static final String MOCK_PROVIDER = "mock";
    private static final long SWITCH_ON_FRESHNESS_CLIFF_MS = 30000; // 30 seconds
    private static final String ACCESS_MOCK_LOCATION = "android.permission.ACCESS_MOCK_LOCATION";
@@ -155,8 +157,11 @@ public class GoogleLocationManager implements LocationChangeListener {
    }

    public void updateLocationRequest(LocationRequestUpdateData data) {
        String packageName = null;
        if (data.pendingIntent != null)
            packageName = PackageUtils.packageFromPendingIntent(data.pendingIntent);
        if (data.opCode == LocationRequestUpdateData.REQUEST_UPDATES) {
            requestLocationUpdates(new LocationRequestHelper(context, hasFineLocationPermission(), hasCoarseLocationPermission(), null, data));
            requestLocationUpdates(new LocationRequestHelper(context, hasFineLocationPermission(), hasCoarseLocationPermission(), packageName, data));
            if (data.fusedLocationProviderCallback != null) {
                try {
                    data.fusedLocationProviderCallback.onFusedLocationProviderResult(FusedLocationProviderResult.SUCCESS);
+19 −7
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package org.microg.gms.location;
import android.app.PendingIntent;
import android.content.Context;
import android.location.Location;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
@@ -52,6 +53,8 @@ import com.google.android.gms.location.places.internal.PlacesParams;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;

import org.microg.gms.common.PackageUtils;

import java.util.Arrays;
import java.util.List;

@@ -75,23 +78,27 @@ public class GoogleLocationManagerServiceImpl extends IGoogleLocationManagerServ
    public void addGeofencesList(List<ParcelableGeofence> geofences, PendingIntent pendingIntent,
                                 IGeofencerCallbacks callbacks, String packageName) throws RemoteException {
        Log.d(TAG, "addGeofencesList: " + geofences);
        PackageUtils.checkPackageUid(context, packageName, Binder.getCallingUid());
    }

    @Override
    public void removeGeofencesByIntent(PendingIntent pendingIntent, IGeofencerCallbacks callbacks,
                                        String packageName) throws RemoteException {
        Log.d(TAG, "removeGeofencesByIntent: " + pendingIntent);
        PackageUtils.checkPackageUid(context, packageName, Binder.getCallingUid());
    }

    @Override
    public void removeGeofencesById(String[] geofenceRequestIds, IGeofencerCallbacks callbacks,
                                    String packageName) throws RemoteException {
        Log.d(TAG, "removeGeofencesById: " + Arrays.toString(geofenceRequestIds));
        PackageUtils.checkPackageUid(context, packageName, Binder.getCallingUid());
    }

    @Override
    public void removeAllGeofences(IGeofencerCallbacks callbacks, String packageName) throws RemoteException {
        Log.d(TAG, "removeAllGeofences");
        PackageUtils.checkPackageUid(context, packageName, Binder.getCallingUid());
    }

    @Override
@@ -108,6 +115,7 @@ public class GoogleLocationManagerServiceImpl extends IGoogleLocationManagerServ
    @Override
    public ActivityRecognitionResult getLastActivity(String packageName) throws RemoteException {
        Log.d(TAG, "getLastActivity");
        PackageUtils.checkPackageUid(context, packageName, Binder.getCallingUid());
        return null;
    }

@@ -138,35 +146,35 @@ public class GoogleLocationManagerServiceImpl extends IGoogleLocationManagerServ
    @Override
    public Location getLastLocation() throws RemoteException {
        Log.d(TAG, "getLastLocation");
        return getLocationManager().getLastLocation(null);
        return getLocationManager().getLastLocation(PackageUtils.packageFromProcessId(context, Binder.getCallingPid()));
    }

    @Override
    public void requestLocationUpdatesWithListener(LocationRequest request,
                                                   final ILocationListener listener) throws RemoteException {
        Log.d(TAG, "requestLocationUpdatesWithListener: " + request);
        getLocationManager().requestLocationUpdates(request, listener, null);
        getLocationManager().requestLocationUpdates(request, listener, PackageUtils.packageFromProcessId(context, Binder.getCallingPid()));
    }

    @Override
    public void requestLocationUpdatesWithIntent(LocationRequest request,
                                                 PendingIntent callbackIntent) throws RemoteException {
        Log.d(TAG, "requestLocationUpdatesWithIntent: " + request);
        getLocationManager().requestLocationUpdates(request, callbackIntent, null);
        getLocationManager().requestLocationUpdates(request, callbackIntent, PackageUtils.packageFromPendingIntent(callbackIntent));
    }

    @Override
    public void removeLocationUpdatesWithListener(ILocationListener listener)
            throws RemoteException {
        Log.d(TAG, "removeLocationUpdatesWithListener: " + listener);
        getLocationManager().removeLocationUpdates(listener, null);
        getLocationManager().removeLocationUpdates(listener, PackageUtils.packageFromProcessId(context, Binder.getCallingPid()));
    }

    @Override
    public void removeLocationUpdatesWithIntent(PendingIntent callbackIntent)
            throws RemoteException {
        Log.d(TAG, "removeLocationUpdatesWithIntent: " + callbackIntent);
        getLocationManager().removeLocationUpdates(callbackIntent, null);
        getLocationManager().removeLocationUpdates(callbackIntent, PackageUtils.packageFromPendingIntent(callbackIntent));
    }

    @Override
@@ -226,12 +234,14 @@ public class GoogleLocationManagerServiceImpl extends IGoogleLocationManagerServ
    public void requestLocationUpdatesWithPackage(LocationRequest request, ILocationListener listener,
                                                  String packageName) throws RemoteException {
        Log.d(TAG, "requestLocationUpdatesWithPackage: " + request);
        PackageUtils.checkPackageUid(context, packageName, Binder.getCallingUid());
        getLocationManager().requestLocationUpdates(request, listener, packageName);
    }

    @Override
    public Location getLastLocationWithPackage(String packageName) throws RemoteException {
        Log.d(TAG, "getLastLocationWithPackage: " + packageName);
        PackageUtils.checkPackageUid(context, packageName, Binder.getCallingUid());
        return getLocationManager().getLastLocation(packageName);
    }

@@ -248,6 +258,7 @@ public class GoogleLocationManagerServiceImpl extends IGoogleLocationManagerServ
    @Override
    public LocationAvailability getLocationAvailabilityWithPackage(String packageName) throws RemoteException {
        Log.d(TAG, "getLocationAvailabilityWithPackage: " + packageName);
        PackageUtils.checkPackageUid(context, packageName, Binder.getCallingUid());
        return new LocationAvailability();
    }

@@ -295,6 +306,7 @@ public class GoogleLocationManagerServiceImpl extends IGoogleLocationManagerServ
    @Override
    public void requestLocationSettingsDialog(LocationSettingsRequest settingsRequest, ISettingsCallbacks callback, String packageName) throws RemoteException {
        Log.d(TAG, "requestLocationSettingsDialog: " + settingsRequest);
        PackageUtils.checkPackageUid(context, packageName, Binder.getCallingUid());
        callback.onLocationSettingsResult(new LocationSettingsResult(new LocationSettingsStates(true, true, false, true, true, false), Status.CANCELED));
    }

@@ -302,14 +314,14 @@ public class GoogleLocationManagerServiceImpl extends IGoogleLocationManagerServ
    public void requestLocationUpdatesInternalWithListener(LocationRequestInternal request,
                                                           ILocationListener listener) throws RemoteException {
        Log.d(TAG, "requestLocationUpdatesInternalWithListener: " + request);
        getLocationManager().requestLocationUpdates(request.request, listener, null);
        getLocationManager().requestLocationUpdates(request.request, listener, PackageUtils.packageFromProcessId(context, Binder.getCallingPid()));
    }

    @Override
    public void requestLocationUpdatesInternalWithIntent(LocationRequestInternal request,
                                                         PendingIntent callbackIntent) throws RemoteException {
        Log.d(TAG, "requestLocationUpdatesInternalWithIntent: " + request);
        getLocationManager().requestLocationUpdates(request.request, callbackIntent, null);
        getLocationManager().requestLocationUpdates(request.request, callbackIntent, PackageUtils.packageFromPendingIntent(callbackIntent));
    }

    @Override