1 /* 2 * Copyright (C) 2023 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 package android.app.sdksandbox.sandboxactivity; 17 18 import android.annotation.NonNull; 19 import android.annotation.Nullable; 20 import android.annotation.SystemApi; 21 import android.app.sdksandbox.SdkSandboxManager; 22 import android.app.sdksandbox.sdkprovider.SdkSandboxActivityRegistry; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.os.Build; 26 27 import androidx.annotation.RequiresApi; 28 29 /** 30 * Provides information required for building the sandbox activities. 31 * 32 * @hide 33 */ 34 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) 35 public abstract class SdkSandboxActivityAuthority { 36 private static class InstanceHolder { 37 private static final SdkSandboxActivityAuthority INSTANCE = 38 new SdkSandboxActivityAuthorityImpl(); 39 } 40 41 /** Returns a Single instance of this class, instantiated lazily. */ 42 @NonNull getInstance()43 public static SdkSandboxActivityAuthority getInstance() { 44 return InstanceHolder.INSTANCE; 45 } 46 SdkSandboxActivityAuthority()47 private SdkSandboxActivityAuthority() {} 48 49 /** 50 * Returns true if the intent is an SdkSandbox Activity intent. In other words, if the intent 51 * targets either the Sdk Sandbox package or the action is {@link 52 * ACTION_START_SANDBOXED_ACTIVITY}. 53 * 54 * @param context the context. 55 * @param intent the intent. 56 * @hide 57 */ 58 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) isSdkSandboxActivityIntent( @onNull Context context, @Nullable Intent intent)59 public static boolean isSdkSandboxActivityIntent( 60 @NonNull Context context, @Nullable Intent intent) { 61 if (intent == null) { 62 return false; 63 } 64 if (intent.getAction() != null 65 && intent.getAction().equals(SdkSandboxManager.ACTION_START_SANDBOXED_ACTIVITY)) { 66 return true; 67 } 68 final String sandboxPackageName = context.getPackageManager().getSdkSandboxPackageName(); 69 if (intent.getPackage() != null && intent.getPackage().equals(sandboxPackageName)) { 70 return true; 71 } 72 if (intent.getComponent() != null 73 && intent.getComponent().getPackageName().equals(sandboxPackageName)) { 74 return true; 75 } 76 return false; 77 } 78 79 /** 80 * Returns {@link ActivityContextInfo} instance containing the information which is needed to 81 * build the sandbox activity {@link android.content.Context} for the passed {@link Intent}. 82 * 83 * @param intent an {@link Intent} for a sandbox {@link android.app.Activity} containing 84 * information to identify the SDK which requested the activity. 85 * @return {@link ActivityContextInfo} instance. 86 * @throws IllegalArgumentException if the intent doesn't refer to a registered {@link 87 * android.app.sdksandbox.sdkprovider.SdkSandboxActivityHandler} 88 * @throws IllegalStateException if Customized SDK Context flag is not enabled 89 */ 90 @NonNull 91 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) getActivityContextInfo(@onNull Intent intent)92 public ActivityContextInfo getActivityContextInfo(@NonNull Intent intent) { 93 SdkSandboxActivityRegistry registry = SdkSandboxActivityRegistry.getInstance(); 94 ActivityContextInfo contextInfo = registry.getContextInfo(intent); 95 if (contextInfo == null) { 96 throw new IllegalArgumentException( 97 "There is no registered SdkSandboxActivityHandler " 98 + "for the passed intent, " 99 + intent); 100 } 101 return contextInfo; 102 } 103 104 private static class SdkSandboxActivityAuthorityImpl extends SdkSandboxActivityAuthority {} 105 } 106