2121import android .content .Context ;
2222import android .content .Intent ;
2323import 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 ;
2428import android .os .IBinder ;
2529import 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 }
0 commit comments