Skip to content

Commit 16b9b24

Browse files
committed
Nearby: Do not connect to Google's EN service
1 parent 0284918 commit 16b9b24

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

play-services-base/src/main/java/org/microg/gms/common/GmsClient.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,23 @@ public abstract class GmsClient<I extends IInterface> implements ApiClient {
4646
protected ConnectionState state = ConnectionState.NOT_CONNECTED;
4747
private ServiceConnection serviceConnection;
4848
private I serviceInterface;
49-
private String actionString;
49+
private final String actionString;
50+
private final boolean requireMicrog;
5051

5152
protected int serviceId = -1;
5253
protected Account account = null;
5354
protected Bundle extras = new Bundle();
5455

5556
public GmsClient(Context context, ConnectionCallbacks callbacks, OnConnectionFailedListener connectionFailedListener, String actionString) {
57+
this(context, callbacks, connectionFailedListener, actionString, false);
58+
}
59+
60+
public GmsClient(Context context, ConnectionCallbacks callbacks, OnConnectionFailedListener connectionFailedListener, String actionString, boolean requireMicrog) {
5661
this.context = context;
5762
this.callbacks = callbacks;
5863
this.connectionFailedListener = connectionFailedListener;
5964
this.actionString = actionString;
65+
this.requireMicrog = requireMicrog;
6066
}
6167

6268
protected void onConnectedToBroker(IGmsServiceBroker broker, GmsCallbacks callbacks) throws RemoteException {
@@ -84,7 +90,7 @@ public synchronized void connect() {
8490
MultiConnectionKeeper.getInstance(context).unbind(actionString, serviceConnection);
8591
}
8692
serviceConnection = new GmsServiceConnection();
87-
if (!MultiConnectionKeeper.getInstance(context).bind(actionString, serviceConnection)) {
93+
if (!MultiConnectionKeeper.getInstance(context).bind(actionString, serviceConnection, requireMicrog)) {
8894
state = ConnectionState.ERROR;
8995
handleConnectionFailed();
9096
}

play-services-base/src/main/java/org/microg/gms/common/MultiConnectionKeeper.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
import android.content.Context;
2222
import android.content.Intent;
2323
import android.content.ServiceConnection;
24+
import android.content.pm.ApplicationInfo;
25+
import android.content.pm.PackageManager;
26+
import android.content.pm.PermissionInfo;
27+
import android.content.pm.ResolveInfo;
2428
import android.os.IBinder;
2529
import android.util.Log;
2630

@@ -52,7 +56,11 @@ public synchronized static MultiConnectionKeeper getInstance(Context context) {
5256
}
5357

5458
public synchronized boolean bind(String action, ServiceConnection connection) {
55-
Log.d(TAG, "bind(" + action + ", " + connection + ")");
59+
return bind(action, connection, false);
60+
}
61+
62+
public synchronized boolean bind(String action, ServiceConnection connection, boolean requireMicrog) {
63+
Log.d(TAG, "bind(" + action + ", " + connection + ", " + requireMicrog + ")");
5664
Connection con = connections.get(action);
5765
if (con != null) {
5866
if (!con.forwardsConnection(connection)) {
@@ -61,7 +69,7 @@ public synchronized boolean bind(String action, ServiceConnection connection) {
6169
con.bind();
6270
}
6371
} else {
64-
con = new Connection(action);
72+
con = new Connection(action, requireMicrog);
6573
con.addConnectionForward(connection);
6674
con.bind();
6775
connections.put(action, con);
@@ -83,6 +91,7 @@ public synchronized void unbind(String action, ServiceConnection connection) {
8391

8492
public class Connection {
8593
private final String actionString;
94+
private final boolean requireMicrog;
8695
private final Set<ServiceConnection> connectionForwards = new HashSet<ServiceConnection>();
8796
private boolean bound = false;
8897
private boolean connected = false;
@@ -116,7 +125,12 @@ public void onServiceDisconnected(ComponentName componentName) {
116125
};
117126

118127
public Connection(String actionString) {
128+
this(actionString, false);
129+
}
130+
131+
public Connection(String actionString, boolean requireMicrog) {
119132
this.actionString = actionString;
133+
this.requireMicrog = requireMicrog;
120134
}
121135

122136
@SuppressLint("InlinedApi")
@@ -125,14 +139,23 @@ public void bind() {
125139
Intent gmsIntent = new Intent(actionString).setPackage(GMS_PACKAGE_NAME);
126140
Intent selfIntent = new Intent(actionString).setPackage(context.getPackageName());
127141
Intent intent;
128-
if (context.getPackageManager().resolveService(gmsIntent, 0) == null) {
142+
ResolveInfo resolveInfo;
143+
if ((resolveInfo = context.getPackageManager().resolveService(gmsIntent, 0)) == null) {
129144
Log.w(TAG, "No GMS service found for " + actionString);
130145
if (context.getPackageManager().resolveService(selfIntent, 0) != null) {
131-
Log.d(TAG, "Found service for "+actionString+" in self package, using it instead");
146+
Log.d(TAG, "Found service for " + actionString + " in self package, using it instead");
132147
intent = selfIntent;
133148
} else {
134149
return;
135150
}
151+
} else if (requireMicrog && !isMicrog(resolveInfo)) {
152+
Log.w(TAG, "GMS service found for " + actionString + " but looks not like microG");
153+
if (context.getPackageManager().resolveService(selfIntent, 0) != null) {
154+
Log.d(TAG, "Found service for " + actionString + " in self package, using it instead");
155+
intent = selfIntent;
156+
} else {
157+
intent = gmsIntent;
158+
}
136159
} else {
137160
intent = gmsIntent;
138161
}
@@ -146,6 +169,17 @@ public void bind() {
146169
}
147170
}
148171

172+
public boolean isMicrog(ResolveInfo resolveInfo) {
173+
if (resolveInfo == null || resolveInfo.serviceInfo == null) return false;
174+
if (resolveInfo.serviceInfo.name.startsWith("org.microg.")) return true;
175+
try {
176+
PermissionInfo info = context.getPackageManager().getPermissionInfo("org.microg.gms.EXTENDED_ACCESS", 0);
177+
return info.packageName.equals(resolveInfo.serviceInfo.packageName);
178+
} catch (PackageManager.NameNotFoundException e) {
179+
return false;
180+
}
181+
}
182+
149183
public boolean isBound() {
150184
return bound;
151185
}

play-services-nearby/src/main/java/org/microg/gms/nearby/ExposureNotificationApiClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
public class ExposureNotificationApiClient extends GmsClient<INearbyExposureNotificationService> {
3737
public ExposureNotificationApiClient(Context context, ConnectionCallbacks callbacks, OnConnectionFailedListener connectionFailedListener) {
38-
super(context, callbacks, connectionFailedListener, GmsService.NEARBY_EXPOSURE.ACTION);
38+
super(context, callbacks, connectionFailedListener, GmsService.NEARBY_EXPOSURE.ACTION, true);
3939
serviceId = GmsService.NEARBY_EXPOSURE.SERVICE_ID;
4040
}
4141

0 commit comments

Comments
 (0)