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

Unverified Commit 16b9b243 authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

Nearby: Do not connect to Google's EN service

parent 02849180
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -46,17 +46,23 @@ public abstract class GmsClient<I extends IInterface> implements ApiClient {
    protected ConnectionState state = ConnectionState.NOT_CONNECTED;
    private ServiceConnection serviceConnection;
    private I serviceInterface;
    private String actionString;
    private final String actionString;
    private final boolean requireMicrog;

    protected int serviceId = -1;
    protected Account account = null;
    protected Bundle extras = new Bundle();

    public GmsClient(Context context, ConnectionCallbacks callbacks, OnConnectionFailedListener connectionFailedListener, String actionString) {
        this(context, callbacks, connectionFailedListener, actionString, false);
    }

    public GmsClient(Context context, ConnectionCallbacks callbacks, OnConnectionFailedListener connectionFailedListener, String actionString, boolean requireMicrog) {
        this.context = context;
        this.callbacks = callbacks;
        this.connectionFailedListener = connectionFailedListener;
        this.actionString = actionString;
        this.requireMicrog = requireMicrog;
    }

    protected void onConnectedToBroker(IGmsServiceBroker broker, GmsCallbacks callbacks) throws RemoteException {
@@ -84,7 +90,7 @@ public abstract class GmsClient<I extends IInterface> implements ApiClient {
            MultiConnectionKeeper.getInstance(context).unbind(actionString, serviceConnection);
        }
        serviceConnection = new GmsServiceConnection();
        if (!MultiConnectionKeeper.getInstance(context).bind(actionString, serviceConnection)) {
        if (!MultiConnectionKeeper.getInstance(context).bind(actionString, serviceConnection, requireMicrog)) {
            state = ConnectionState.ERROR;
            handleConnectionFailed();
        }
+38 −4
Original line number Diff line number Diff line
@@ -21,6 +21,10 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import android.content.pm.ResolveInfo;
import android.os.IBinder;
import android.util.Log;

@@ -52,7 +56,11 @@ public class MultiConnectionKeeper {
    }

    public synchronized boolean bind(String action, ServiceConnection connection) {
        Log.d(TAG, "bind(" + action + ", " + connection + ")");
        return bind(action, connection, false);
    }

    public synchronized boolean bind(String action, ServiceConnection connection, boolean requireMicrog) {
        Log.d(TAG, "bind(" + action + ", " + connection + ", " + requireMicrog + ")");
        Connection con = connections.get(action);
        if (con != null) {
            if (!con.forwardsConnection(connection)) {
@@ -61,7 +69,7 @@ public class MultiConnectionKeeper {
                    con.bind();
            }
        } else {
            con = new Connection(action);
            con = new Connection(action, requireMicrog);
            con.addConnectionForward(connection);
            con.bind();
            connections.put(action, con);
@@ -83,6 +91,7 @@ public class MultiConnectionKeeper {

    public class Connection {
        private final String actionString;
        private final boolean requireMicrog;
        private final Set<ServiceConnection> connectionForwards = new HashSet<ServiceConnection>();
        private boolean bound = false;
        private boolean connected = false;
@@ -116,7 +125,12 @@ public class MultiConnectionKeeper {
        };

        public Connection(String actionString) {
            this(actionString, false);
        }

        public Connection(String actionString, boolean requireMicrog) {
            this.actionString = actionString;
            this.requireMicrog = requireMicrog;
        }

        @SuppressLint("InlinedApi")
@@ -125,7 +139,8 @@ public class MultiConnectionKeeper {
            Intent gmsIntent = new Intent(actionString).setPackage(GMS_PACKAGE_NAME);
            Intent selfIntent = new Intent(actionString).setPackage(context.getPackageName());
            Intent intent;
            if (context.getPackageManager().resolveService(gmsIntent, 0) == null) {
            ResolveInfo resolveInfo;
            if ((resolveInfo = context.getPackageManager().resolveService(gmsIntent, 0)) == null) {
                Log.w(TAG, "No GMS service found for " + actionString);
                if (context.getPackageManager().resolveService(selfIntent, 0) != null) {
                    Log.d(TAG, "Found service for " + actionString + " in self package, using it instead");
@@ -133,6 +148,14 @@ public class MultiConnectionKeeper {
                } else {
                    return;
                }
            } else if (requireMicrog && !isMicrog(resolveInfo)) {
                Log.w(TAG, "GMS service found for " + actionString + " but looks not like microG");
                if (context.getPackageManager().resolveService(selfIntent, 0) != null) {
                    Log.d(TAG, "Found service for " + actionString + " in self package, using it instead");
                    intent = selfIntent;
                } else {
                    intent = gmsIntent;
                }
            } else {
                intent = gmsIntent;
            }
@@ -146,6 +169,17 @@ public class MultiConnectionKeeper {
            }
        }

        public boolean isMicrog(ResolveInfo resolveInfo) {
            if (resolveInfo == null || resolveInfo.serviceInfo == null) return false;
            if (resolveInfo.serviceInfo.name.startsWith("org.microg.")) return true;
            try {
                PermissionInfo info = context.getPackageManager().getPermissionInfo("org.microg.gms.EXTENDED_ACCESS", 0);
                return info.packageName.equals(resolveInfo.serviceInfo.packageName);
            } catch (PackageManager.NameNotFoundException e) {
                return false;
            }
        }

        public boolean isBound() {
            return bound;
        }
+1 −1
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ import org.microg.gms.common.api.OnConnectionFailedListener;

public class ExposureNotificationApiClient extends GmsClient<INearbyExposureNotificationService> {
    public ExposureNotificationApiClient(Context context, ConnectionCallbacks callbacks, OnConnectionFailedListener connectionFailedListener) {
        super(context, callbacks, connectionFailedListener, GmsService.NEARBY_EXPOSURE.ACTION);
        super(context, callbacks, connectionFailedListener, GmsService.NEARBY_EXPOSURE.ACTION, true);
        serviceId = GmsService.NEARBY_EXPOSURE.SERVICE_ID;
    }