Skip to content

Commit

Permalink
Replace getCallingActivity() with getLaunchedFromPackage()
Browse files Browse the repository at this point in the history
getLaunchedFromPackage() reports who launched this Activity or built
PendingIntent used to launch it, whereas getCallingActivity() reports
who will get result of Activity.

Bug: 316891059
Test: robotest, manual
(cherry picked from commit ddc11bc03ab48e885f652b89df5f92ff283bcd4a)
(cherry picked from commit 8bdbb580da847d82f16fb57883a01a5e65ffa696)
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:c7a8127d3bb6010617e507c03f7207dd50082953)
Merged-In: If97018c2741caef622f0596bbfeaa42ef1788b78
Change-Id: If97018c2741caef622f0596bbfeaa42ef1788b78
  • Loading branch information
Jason Chiu authored and aoleary committed Sep 17, 2024
1 parent c9daade commit 5174cf0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/com/android/settings/search/SearchFeatureProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public interface SearchFeatureProvider {
* @throws IllegalArgumentException when caller is null
* @throws SecurityException when caller is not allowed to launch search result page
*/
void verifyLaunchSearchResultPageCaller(Context context, @NonNull ComponentName caller)
void verifyLaunchSearchResultPageCaller(@NonNull Context context, @NonNull String callerPackage)
throws SecurityException, IllegalArgumentException;

/**
Expand Down
18 changes: 8 additions & 10 deletions src/com/android/settings/search/SearchFeatureProviderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

package com.android.settings.search;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.android.settingslib.search.SearchIndexableResources;
import com.android.settingslib.search.SearchIndexableResourcesMobile;

Expand All @@ -32,21 +33,18 @@
*/
public class SearchFeatureProviderImpl implements SearchFeatureProvider {

private static final String TAG = "SearchFeatureProvider";

private SearchIndexableResources mSearchIndexableResources;

@Override
public void verifyLaunchSearchResultPageCaller(Context context, ComponentName caller) {
if (caller == null) {
public void verifyLaunchSearchResultPageCaller(@NonNull Context context,
@NonNull String callerPackage) {
if (TextUtils.isEmpty(callerPackage)) {
throw new IllegalArgumentException("ExternalSettingsTrampoline intents "
+ "must be called with startActivityForResult");
}
final String packageName = caller.getPackageName();
final boolean isSettingsPackage = TextUtils.equals(packageName, context.getPackageName())
|| TextUtils.equals(getSettingsIntelligencePkgName(context), packageName);
final boolean isAllowlistedPackage =
isSignatureAllowlisted(context, caller.getPackageName());
final boolean isSettingsPackage = TextUtils.equals(callerPackage, context.getPackageName())
|| TextUtils.equals(getSettingsIntelligencePkgName(context), callerPackage);
final boolean isAllowlistedPackage = isSignatureAllowlisted(context, callerPackage);
if (isSettingsPackage || isAllowlistedPackage) {
return;
}
Expand Down
13 changes: 6 additions & 7 deletions src/com/android/settings/search/SearchResultTrampoline.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.android.settings.SettingsActivity.EXTRA_SHOW_FRAGMENT_TAB;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
Expand Down Expand Up @@ -51,11 +50,11 @@ public class SearchResultTrampoline extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

final ComponentName callingActivity = getCallingActivity();
final String callerPackage = getLaunchedFromPackage();
// First make sure caller has privilege to launch a search result page.
FeatureFactory.getFactory(this)
.getSearchFeatureProvider()
.verifyLaunchSearchResultPageCaller(this, callingActivity);
.verifyLaunchSearchResultPageCaller(this, callerPackage);
// Didn't crash, proceed and launch the result as a subsetting.
Intent intent = getIntent();
final String highlightMenuKey = intent.getStringExtra(
Expand Down Expand Up @@ -99,7 +98,7 @@ protected void onCreate(Bundle savedInstanceState) {

if (!ActivityEmbeddingUtils.isEmbeddingActivityEnabled(this)) {
startActivity(intent);
} else if (isSettingsIntelligence(callingActivity)) {
} else if (isSettingsIntelligence(callerPackage)) {
if (FeatureFlagUtils.isEnabled(this, FeatureFlags.SETTINGS_SEARCH_ALWAYS_EXPAND)) {
startActivity(SettingsActivity.getTrampolineIntent(intent, highlightMenuKey)
.setClass(this, DeepLinkHomepageActivityInternal.class)
Expand Down Expand Up @@ -132,9 +131,9 @@ protected void onCreate(Bundle savedInstanceState) {
finish();
}

private boolean isSettingsIntelligence(ComponentName callingActivity) {
return callingActivity != null && TextUtils.equals(
callingActivity.getPackageName(),
private boolean isSettingsIntelligence(String callerPackage) {
return TextUtils.equals(
callerPackage,
FeatureFactory.getFactory(this).getSearchFeatureProvider()
.getSettingsIntelligencePkgName(this));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.google.common.truth.Truth.assertThat;

import android.app.settings.SettingsEnums;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ResolveInfo;
Expand Down Expand Up @@ -131,20 +130,22 @@ public void verifyLaunchSearchResultPageCaller_nullCaller_shouldCrash() {

@Test(expected = SecurityException.class)
public void verifyLaunchSearchResultPageCaller_badCaller_shouldCrash() {
final ComponentName cn = new ComponentName("pkg", "class");
mProvider.verifyLaunchSearchResultPageCaller(mActivity, cn);
final String packageName = "pkg";

mProvider.verifyLaunchSearchResultPageCaller(mActivity, packageName);
}

@Test
public void verifyLaunchSearchResultPageCaller_settingsCaller_shouldNotCrash() {
final ComponentName cn = new ComponentName(mActivity.getPackageName(), "class");
mProvider.verifyLaunchSearchResultPageCaller(mActivity, cn);
final String packageName = mActivity.getPackageName();

mProvider.verifyLaunchSearchResultPageCaller(mActivity, packageName);
}

@Test
public void verifyLaunchSearchResultPageCaller_settingsIntelligenceCaller_shouldNotCrash() {
final String packageName = mProvider.getSettingsIntelligencePkgName(mActivity);
final ComponentName cn = new ComponentName(packageName, "class");
mProvider.verifyLaunchSearchResultPageCaller(mActivity, cn);

mProvider.verifyLaunchSearchResultPageCaller(mActivity, packageName);
}
}

0 comments on commit 5174cf0

Please sign in to comment.