Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
gmscompat: fix handling of app install confirmation in non-UI processes
Browse files Browse the repository at this point in the history
See "fix handling of app install confirmation in non-UI processes" commit in GmsCompatApp.
  • Loading branch information
muhomorr authored and thestinger committed Aug 9, 2023
1 parent 3d2675c commit 679fee8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
28 changes: 28 additions & 0 deletions core/java/com/android/internal/gmscompat/BinderGca2Gms.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.android.internal.gmscompat;

import android.app.Activity;
import android.app.compat.gms.GmsCompat;
import android.content.ContentResolver;
import android.content.ContentValues;
Expand All @@ -11,6 +12,12 @@
import android.util.ArraySet;
import android.util.Log;

import com.android.internal.gmscompat.util.GmcActivityUtils;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

import static com.android.internal.gmscompat.GmsHooks.inPersistentGmsCoreProcess;
import static com.android.internal.gmscompat.GmsInfo.PACKAGE_GMS_CORE;

Expand Down Expand Up @@ -87,4 +94,25 @@ private static void updatePhenotype(SQLiteOpenHelper phenotypeDb, GmsCompatConfi
GmsCompat.appContext().sendBroadcast(i);
}
}

@Override
public boolean startActivityIfVisible(Intent intent) {
Callable<Boolean> callable = () -> {
Activity activity = GmcActivityUtils.getMostRecentVisibleActivity();
if (activity == null) {
return false;
}
activity.startActivity(intent);
return true;
};
var task = new FutureTask<>(callable);
// getMostRecentVisibleActivity() needs to be called from main thread to avoid races
GmsCompat.appContext().getMainThreadHandler().post(task);
try {
return task.get().booleanValue();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
return false;
}
}
}
3 changes: 3 additions & 0 deletions core/java/com/android/internal/gmscompat/IGca2Gms.aidl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.android.internal.gmscompat;

import android.app.ApplicationErrorReport;
import android.app.PendingIntent;
import android.content.Intent;

import com.android.internal.gmscompat.GmsCompatConfig;

Expand All @@ -10,4 +11,6 @@ interface IGca2Gms {
// intentionally not oneway to simplify code in GmsCompatApp
void updateConfig(in GmsCompatConfig newConfig);
void invalidateConfigCaches();

boolean startActivityIfVisible(in Intent intent);
}
5 changes: 3 additions & 2 deletions core/java/com/android/internal/gmscompat/IGms2Gca.aidl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.android.internal.gmscompat;
import android.app.ApplicationErrorReport;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.IContentObserver;
import android.os.BinderDef;

Expand All @@ -17,8 +18,8 @@ interface IGms2Gca {

@nullable BinderDef maybeGetBinderDef(String callerPkg, int processState, String ifaceName);

oneway void showPlayStorePendingUserActionNotification(@nullable String pkgName);
oneway void dismissPlayStorePendingUserActionNotification();
oneway void onPlayStorePendingUserAction(in Intent actionIntent, @nullable String pkgName);
@nullable Intent maybeGetPlayStorePendingUserActionIntent();

oneway void showPlayStoreMissingObbPermissionNotification();

Expand Down
36 changes: 13 additions & 23 deletions core/java/com/android/internal/gmscompat/PlayStoreHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,9 @@
public final class PlayStoreHooks {
private static final String TAG = "GmsCompat/PlayStore";

// accessed only from the main thread, no need for synchronization
static ArrayDeque<Intent> pendingConfirmationIntents;
static PackageManager packageManager;

public static void init() {
pendingConfirmationIntents = new ArrayDeque<>();

obbDir = Environment.getExternalStorageDirectory().getPath() + "/Android/obb";
playStoreObbDir = obbDir + '/' + GmsInfo.PACKAGE_PLAY_STORE;
File.mkdirsFailedHook = PlayStoreHooks::mkdirsFailed;
Expand Down Expand Up @@ -100,15 +96,14 @@ public static IntentSender wrapCommitStatusReceiver(PackageInstaller.Session ses
}

public static void onActivityResumed(Activity activity) {
if (pendingConfirmationIntents.size() != 0) {
Intent i = pendingConfirmationIntents.removeLast();
activity.startActivity(i);

try {
GmsCompatApp.iGms2Gca().dismissPlayStorePendingUserActionNotification();
} catch (RemoteException e) {
GmsCompatApp.callFailed(e);
}
Intent pendingActionIntent;
try {
pendingActionIntent = GmsCompatApp.iGms2Gca().maybeGetPlayStorePendingUserActionIntent();
} catch (RemoteException e) {
throw GmsCompatApp.callFailed(e);
}
if (pendingActionIntent != null) {
activity.startActivity(pendingActionIntent);
}
}

Expand Down Expand Up @@ -155,17 +150,12 @@ public void onReceive(Context receiverContext, Intent intent) {
}
}

Activity activity = GmcActivityUtils.getMostRecentVisibleActivity();
if (activity != null) {
activity.startActivity(confirmationIntent);
} else {
pendingConfirmationIntents.addLast(confirmationIntent);
try {
GmsCompatApp.iGms2Gca().showPlayStorePendingUserActionNotification(packageName);
} catch (RemoteException e) {
GmsCompatApp.callFailed(e);
}
try {
GmsCompatApp.iGms2Gca().onPlayStorePendingUserAction(confirmationIntent, packageName);
} catch (RemoteException e) {
GmsCompatApp.callFailed(e);
}

// confirmationIntent has a PendingIntent to this instance, don't unregister yet
return;
}
Expand Down

0 comments on commit 679fee8

Please sign in to comment.