1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.server.wm.jetpack.signed; 18 19 import static android.server.wm.jetpack.utils.ActivityEmbeddingUtil.EMBEDDED_ACTIVITY_ID; 20 import static android.server.wm.jetpack.utils.ActivityEmbeddingUtil.createSplitPairRuleBuilder; 21 import static android.server.wm.jetpack.utils.ActivityEmbeddingUtil.startActivityCrossUidInSplit; 22 import static android.server.wm.jetpack.utils.ExtensionUtil.assumeExtensionSupportedDevice; 23 import static android.server.wm.jetpack.utils.ExtensionUtil.getWindowExtensions; 24 import static android.server.wm.jetpack.utils.WindowManagerJetpackTestBase.EXTRA_EMBED_ACTIVITY; 25 import static android.server.wm.jetpack.utils.WindowManagerJetpackTestBase.EXTRA_SPLIT_RATIO; 26 27 import static org.junit.Assume.assumeNotNull; 28 29 import android.app.Activity; 30 import android.content.ComponentName; 31 import android.content.Intent; 32 import android.os.Bundle; 33 import android.server.wm.jetpack.utils.TestActivityKnownEmbeddingCerts; 34 import android.server.wm.jetpack.utils.TestValueCountConsumer; 35 36 import androidx.window.extensions.WindowExtensions; 37 import androidx.window.extensions.embedding.ActivityEmbeddingComponent; 38 import androidx.window.extensions.embedding.SplitAttributes; 39 import androidx.window.extensions.embedding.SplitInfo; 40 import androidx.window.extensions.embedding.SplitPairRule; 41 42 import org.junit.AssumptionViolatedException; 43 44 import java.util.Collections; 45 import java.util.List; 46 47 /** 48 * A test activity that attempts to embed {@link TestActivityKnownEmbeddingCerts} when created. 49 * The app it belongs to is signed with a certificate that is recognized by the target app as a 50 * trusted embedding host. 51 */ 52 public class SignedEmbeddingActivity extends Activity { 53 54 @Override onCreate(Bundle savedInstanceState)55 protected void onCreate(Bundle savedInstanceState) { 56 super.onCreate(savedInstanceState); 57 58 if (getIntent().getBooleanExtra(EXTRA_EMBED_ACTIVITY, false)) { 59 startActivityInSplit(); 60 } 61 } 62 63 @Override onNewIntent(Intent intent)64 protected void onNewIntent(Intent intent) { 65 super.onNewIntent(intent); 66 67 if (intent.getBooleanExtra(EXTRA_EMBED_ACTIVITY, false)) { 68 startActivityInSplit(); 69 } 70 } 71 startActivityInSplit()72 void startActivityInSplit() { 73 ActivityEmbeddingComponent embeddingComponent; 74 try { 75 assumeExtensionSupportedDevice(); 76 WindowExtensions windowExtensions = getWindowExtensions(); 77 assumeNotNull(windowExtensions); 78 embeddingComponent = windowExtensions.getActivityEmbeddingComponent(); 79 assumeNotNull(embeddingComponent); 80 } catch (AssumptionViolatedException e) { 81 // Embedding not supported 82 finish(); 83 return; 84 } 85 86 TestValueCountConsumer<List<SplitInfo>> splitInfoConsumer = new TestValueCountConsumer<>(); 87 embeddingComponent.setSplitInfoCallback(splitInfoConsumer); 88 SplitAttributes.SplitType splitType = new SplitAttributes.SplitType.RatioSplitType( 89 getIntent().getFloatExtra(EXTRA_SPLIT_RATIO, 0.5f)); 90 91 SplitPairRule splitPairRule = createSplitPairRuleBuilder( 92 activityActivityPair -> true /* activityActivityPredicate */, 93 activityIntentPair -> true /* activityIntentPredicate */, 94 parentWindowMetrics -> true /* parentWindowMetricsPredicate */) 95 .setDefaultSplitAttributes(new SplitAttributes.Builder() 96 .setSplitType(splitType).build()) 97 .build(); 98 embeddingComponent.setEmbeddingRules(Collections.singleton(splitPairRule)); 99 100 // Launch an activity from a different UID that recognizes this package's signature and 101 // verify that it is split with this activity. 102 startActivityCrossUidInSplit(this, 103 new ComponentName("android.server.wm.jetpack", 104 "android.server.wm.jetpack.utils.TestActivityKnownEmbeddingCerts"), 105 splitPairRule, splitInfoConsumer, EMBEDDED_ACTIVITY_ID, false /* verify */); 106 } 107 } 108