Match direct boot unaware receivers for Partner

In Partner.java, since we don't actually execute code in the package,
it is safe to match direct boot unaware receivers as well.

Test: ./gradlew connectedAndroidTest test
Bug: 36984206
Change-Id: I5ad017458db7d4af2d5a753a3f7663721291402e
(cherry picked from commit a3f1dc094f)
This commit is contained in:
Maurice Lam 2017-04-10 15:42:09 -07:00
parent 7204767830
commit 6fa591591a
2 changed files with 52 additions and 13 deletions

View file

@ -24,9 +24,13 @@ import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.support.annotation.VisibleForTesting;
import android.util.Log;
import java.util.List;
/**
* Utilities to discover and interact with partner customizations. An overlay package is one that
* registers the broadcast receiver for {@code com.android.setupwizard.action.PARTNER_CUSTOMIZATION}
@ -36,11 +40,12 @@ import android.util.Log;
* <p>Derived from {@code com.android.launcher3/Partner.java}
*/
public class Partner {
private static final String TAG = "(SUW) Partner";
/** Marker action used to discover partner */
private static final String
ACTION_PARTNER_CUSTOMIZATION = "com.android.setupwizard.action.PARTNER_CUSTOMIZATION";
private static final String ACTION_PARTNER_CUSTOMIZATION =
"com.android.setupwizard.action.PARTNER_CUSTOMIZATION";
private static boolean sSearched = false;
private static Partner sPartner;
@ -113,7 +118,20 @@ public class Partner {
if (!sSearched) {
PackageManager pm = context.getPackageManager();
final Intent intent = new Intent(ACTION_PARTNER_CUSTOMIZATION);
for (ResolveInfo info : pm.queryBroadcastReceivers(intent, 0)) {
List<ResolveInfo> receivers;
if (VERSION.SDK_INT >= VERSION_CODES.N) {
receivers = pm.queryBroadcastReceivers(
intent,
PackageManager.MATCH_SYSTEM_ONLY
| PackageManager.MATCH_DIRECT_BOOT_AWARE
| PackageManager.MATCH_DIRECT_BOOT_UNAWARE);
} else {
// On versions before N, direct boot doesn't exist. And the MATCH_SYSTEM_ONLY flag
// doesn't exist so we filter for system apps in code below.
receivers = pm.queryBroadcastReceivers(intent, 0);
}
for (ResolveInfo info : receivers) {
if (info.activityInfo == null) {
continue;
}

View file

@ -33,6 +33,8 @@ import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import com.android.setupwizardlib.BuildConfig;
import com.android.setupwizardlib.R;
@ -48,9 +50,10 @@ import org.robolectric.res.builder.DefaultPackageManager;
import org.robolectric.shadows.ShadowResources;
import java.util.Arrays;
import java.util.Collections;
@RunWith(SuwLibRobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
@Config(constants = BuildConfig.class, sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK })
public class PartnerTest {
private static final String ACTION_PARTNER_CUSTOMIZATION =
@ -77,8 +80,8 @@ public class PartnerTest {
mPackageManager.addResolveInfoForIntent(
new Intent(ACTION_PARTNER_CUSTOMIZATION),
Arrays.asList(
createResolveInfo("foo.bar", false),
createResolveInfo("test.partner.package", true))
createResolveInfo("foo.bar", false, true),
createResolveInfo("test.partner.package", true, true))
);
Partner partner = Partner.get(mContext);
@ -96,8 +99,8 @@ public class PartnerTest {
mPackageManager.addResolveInfoForIntent(
new Intent(ACTION_PARTNER_CUSTOMIZATION),
Arrays.asList(
createResolveInfo("foo.bar", false),
createResolveInfo("test.partner.package", false))
createResolveInfo("foo.bar", false, true),
createResolveInfo("test.partner.package", false, true))
);
Partner partner = Partner.get(mContext);
@ -113,8 +116,8 @@ public class PartnerTest {
mPackageManager.addResolveInfoForIntent(
new Intent(ACTION_PARTNER_CUSTOMIZATION),
Arrays.asList(
createResolveInfo("foo.bar", false),
createResolveInfo("test.partner.package", true))
createResolveInfo("foo.bar", false, true),
createResolveInfo("test.partner.package", true, true))
);
ResourceEntry entry = Partner.getResourceEntry(mContext, R.integer.suwTransitionDuration);
@ -128,8 +131,8 @@ public class PartnerTest {
mPackageManager.addResolveInfoForIntent(
new Intent(ACTION_PARTNER_CUSTOMIZATION),
Arrays.asList(
createResolveInfo("foo.bar", false),
createResolveInfo("test.partner.package", true))
createResolveInfo("foo.bar", false, true),
createResolveInfo("test.partner.package", true, true))
);
ResourceEntry entry = Partner.getResourceEntry(mContext, R.color.suw_color_accent_dark);
@ -138,7 +141,22 @@ public class PartnerTest {
assertFalse("Partner value should come from fallback", entry.isOverlay);
}
private ResolveInfo createResolveInfo(String packageName, boolean isSystem) {
@Test
public void testNotDirectBootAware() {
mPackageManager.addResolveInfoForIntent(
new Intent(ACTION_PARTNER_CUSTOMIZATION),
Collections.singletonList(createResolveInfo("test.partner.package", true, false)));
ResourceEntry entry = Partner.getResourceEntry(mContext, R.color.suw_color_accent_dark);
int partnerValue = entry.resources.getColor(entry.id);
assertEquals("Partner value should default to 0xff448aff", 0xff448aff, partnerValue);
assertFalse("Partner value should come from fallback", entry.isOverlay);
}
private ResolveInfo createResolveInfo(
String packageName,
boolean isSystem,
boolean directBootAware) {
ResolveInfo info = new ResolveInfo();
info.resolvePackageName = packageName;
ActivityInfo activityInfo = new ActivityInfo();
@ -148,6 +166,9 @@ public class PartnerTest {
activityInfo.applicationInfo = appInfo;
activityInfo.packageName = packageName;
activityInfo.name = packageName;
if (VERSION.SDK_INT >= VERSION_CODES.N) {
activityInfo.directBootAware = directBootAware;
}
info.activityInfo = activityInfo;
return info;
}