summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author kholoud mohamed <kholoudm@google.com> 2020-04-21 14:45:57 +0100
committer kholoud mohamed <kholoudm@google.com> 2020-04-21 18:06:03 +0100
commit149e0fe89e59e92f63c40c75c3f4aec1dd38210a (patch)
treea19dcc8adef3d5143f03eb934b9d42208b4a9044
parenta16b8b31f8c230cbe74af653a662d68e9c2ea5ca (diff)
Fix crash in IntentForwarderActivity
The crash was caused by trying to resolve an instant app which is not allowed on the main thread, fixed it by resolving on a background thread. Fixes: 146141583 Test: atest IntentForwarderActivityTest Test: atest ResolverActivityTest Test: atest ChooserActivityTest Change-Id: I250d682cf90870c7360eb66529be8c91fac266b1
-rw-r--r--core/java/com/android/internal/app/IntentForwarderActivity.java79
-rw-r--r--core/tests/coretests/src/com/android/internal/app/IntentForwarderActivityTest.java12
2 files changed, 62 insertions, 29 deletions
diff --git a/core/java/com/android/internal/app/IntentForwarderActivity.java b/core/java/com/android/internal/app/IntentForwarderActivity.java
index 3eb0923363f2..2c4892561ddd 100644
--- a/core/java/com/android/internal/app/IntentForwarderActivity.java
+++ b/core/java/com/android/internal/app/IntentForwarderActivity.java
@@ -50,6 +50,9 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
/**
* This is used in conjunction with
@@ -74,11 +77,13 @@ public class IntentForwarderActivity extends Activity {
private Injector mInjector;
private MetricsLogger mMetricsLogger;
+ protected ExecutorService mExecutorService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInjector = createInjector();
+ mExecutorService = Executors.newSingleThreadExecutor();
Intent intentReceived = getIntent();
String className = intentReceived.getComponent().getClassName();
@@ -118,30 +123,9 @@ public class IntentForwarderActivity extends Activity {
mInjector.getIPackageManager(), getContentResolver());
if (newIntent != null) {
newIntent.prepareToLeaveUser(callingUserId);
-
- final ResolveInfo ri = mInjector.resolveActivityAsUser(newIntent, MATCH_DEFAULT_ONLY,
- targetUserId);
- try {
- startActivityAsCaller(newIntent, null, null, false, targetUserId);
- } catch (RuntimeException e) {
- int launchedFromUid = -1;
- String launchedFromPackage = "?";
- try {
- launchedFromUid = ActivityTaskManager.getService().getLaunchedFromUid(
- getActivityToken());
- launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage(
- getActivityToken());
- } catch (RemoteException ignored) {
- }
-
- Slog.wtf(TAG, "Unable to launch as UID " + launchedFromUid + " package "
- + launchedFromPackage + ", while running in "
- + ActivityThread.currentProcessName(), e);
- }
-
- if (shouldShowDisclosure(ri, intentReceived)) {
- mInjector.showToast(userMessageId, Toast.LENGTH_LONG);
- }
+ maybeShowDisclosureAsync(intentReceived, newIntent, targetUserId, userMessageId);
+ CompletableFuture.runAsync(() -> startActivityAsCaller(
+ newIntent, targetUserId), mExecutorService);
} else {
Slog.wtf(TAG, "the intent: " + intentReceived + " cannot be forwarded from user "
+ callingUserId + " to user " + targetUserId);
@@ -149,6 +133,44 @@ public class IntentForwarderActivity extends Activity {
finish();
}
+ private void maybeShowDisclosureAsync(
+ Intent intentReceived, Intent newIntent, int userId, int messageId) {
+ final CompletableFuture<ResolveInfo> resolveInfoFuture =
+ mInjector.resolveActivityAsUser(newIntent, MATCH_DEFAULT_ONLY, userId);
+ resolveInfoFuture.thenAcceptAsync(ri -> {
+ if (shouldShowDisclosure(ri, intentReceived)) {
+ mInjector.showToast(messageId, Toast.LENGTH_LONG);
+ }
+ }, getApplicationContext().getMainExecutor());
+ }
+
+ private void startActivityAsCaller(Intent newIntent, int userId) {
+ try {
+ startActivityAsCaller(
+ newIntent,
+ /* options= */ null,
+ /* permissionToken= */ null,
+ /* ignoreTargetSecurity= */ false,
+ userId);
+ } catch (RuntimeException e) {
+ int launchedFromUid = -1;
+ String launchedFromPackage = "?";
+ try {
+ launchedFromUid = ActivityTaskManager.getService().getLaunchedFromUid(
+ getActivityToken());
+ launchedFromPackage = ActivityTaskManager.getService()
+ .getLaunchedFromPackage(getActivityToken());
+ } catch (RemoteException ignored) {
+ }
+
+ Slog.wtf(TAG, "Unable to launch as UID " + launchedFromUid + " package "
+ + launchedFromPackage + ", while running in "
+ + ActivityThread.currentProcessName(), e);
+ } finally {
+ mExecutorService.shutdown();
+ }
+ }
+
private void launchChooserActivityWithCorrectTab(Intent intentReceived, String className) {
// When showing the sharesheet, instead of forwarding to the other profile,
// we launch the sharesheet in the current user and select the other tab.
@@ -322,8 +344,11 @@ public class IntentForwarderActivity extends Activity {
}
@Override
- public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
- return getPackageManager().resolveActivityAsUser(intent, flags, userId);
+ @Nullable
+ public CompletableFuture<ResolveInfo> resolveActivityAsUser(
+ Intent intent, int flags, int userId) {
+ return CompletableFuture.supplyAsync(
+ () -> getPackageManager().resolveActivityAsUser(intent, flags, userId));
}
@Override
@@ -339,7 +364,7 @@ public class IntentForwarderActivity extends Activity {
PackageManager getPackageManager();
- ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId);
+ CompletableFuture<ResolveInfo> resolveActivityAsUser(Intent intent, int flags, int userId);
void showToast(@StringRes int messageId, int duration);
}
diff --git a/core/tests/coretests/src/com/android/internal/app/IntentForwarderActivityTest.java b/core/tests/coretests/src/com/android/internal/app/IntentForwarderActivityTest.java
index 5424b6f19038..43590bae6770 100644
--- a/core/tests/coretests/src/com/android/internal/app/IntentForwarderActivityTest.java
+++ b/core/tests/coretests/src/com/android/internal/app/IntentForwarderActivityTest.java
@@ -68,6 +68,8 @@ import org.mockito.MockitoAnnotations;
import java.util.ArrayList;
import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
@RunWith(AndroidJUnit4.class)
public class IntentForwarderActivityTest {
@@ -633,6 +635,11 @@ public class IntentForwarderActivityTest {
public void onCreate(@Nullable Bundle savedInstanceState) {
getIntent().setComponent(sComponentName);
super.onCreate(savedInstanceState);
+ try {
+ mExecutorService.awaitTermination(/* timeout= */ 30, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
}
@Override
@@ -671,7 +678,8 @@ public class IntentForwarderActivityTest {
}
@Override
- public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
+ public CompletableFuture<ResolveInfo> resolveActivityAsUser(
+ Intent intent, int flags, int userId) {
ActivityInfo activityInfo = new ActivityInfo();
activityInfo.packageName = sPackageName;
activityInfo.name = sActivityName;
@@ -680,7 +688,7 @@ public class IntentForwarderActivityTest {
ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.activityInfo = activityInfo;
- return resolveInfo;
+ return CompletableFuture.completedFuture(resolveInfo);
}
@Override