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