Merge "Fix crash in IntentForwarderActivity" into rvc-dev am: f9a02be500 am: 25d9d08a8a
Change-Id: I02ccda8fdd98dcbb971de69052972d4285314231
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user