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 17 package com.android.sdksandbox; 18 19 import static android.app.sdksandbox.SdkSandboxManager.EXTRA_SANDBOXED_ACTIVITY_HANDLER; 20 21 import android.annotation.NonNull; 22 import android.app.Activity; 23 import android.app.sdksandbox.SdkSandboxManager; 24 import android.app.sdksandbox.sdkprovider.SdkSandboxActivityRegistry; 25 import android.os.Build; 26 import android.os.Bundle; 27 import android.os.IBinder; 28 import android.util.Log; 29 import android.view.Window; 30 31 import androidx.annotation.RequiresApi; 32 33 import com.android.internal.annotations.VisibleForTesting; 34 35 /** 36 * Activity to start for SDKs running in the SDK Runtime. 37 * 38 * <p>It should be created when an {@link android.content.Intent} with action name ({@link 39 * SdkSandboxManager#ACTION_START_SANDBOXED_ACTIVITY}) is started. 40 * 41 * @hide 42 */ 43 public class SandboxedActivity extends Activity { 44 private static final String TAG = "SandboxedActivity"; 45 46 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) 47 @Override onCreate(@onNull Bundle savedInstanceState)48 public void onCreate(@NonNull Bundle savedInstanceState) { 49 super.onCreate(savedInstanceState); 50 requestWindowFeature(Window.FEATURE_NO_TITLE); 51 52 notifySdkOnActivityCreation(); 53 } 54 55 /** 56 * Notify the SDK by calling {@link 57 * android.app.sdksandbox.sdkprovider.SdkSandboxActivityHandler#onActivityCreated(Activity)}. 58 */ 59 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) notifySdkOnActivityCreation()60 public void notifySdkOnActivityCreation() { 61 if (this.getIntent() == null 62 || this.getIntent().getExtras() == null 63 || this.getIntent() 64 .getExtras() 65 .getBinder(SdkSandboxManager.EXTRA_SANDBOXED_ACTIVITY_HANDLER) 66 == null) { 67 Log.e( 68 TAG, 69 "Extra params of the intent are missing the IBinder value for the key" 70 + SdkSandboxManager.EXTRA_SANDBOXED_ACTIVITY_HANDLER); 71 finish(); 72 return; 73 } 74 IBinder token = 75 this.getIntent() 76 .getExtras() 77 .getBinder(SdkSandboxManager.EXTRA_SANDBOXED_ACTIVITY_HANDLER); 78 SdkSandboxActivityRegistry registry = SdkSandboxActivityRegistry.getInstance(); 79 try { 80 registry.notifyOnActivityCreation(token, this); 81 } catch (Exception e) { 82 Log.e(TAG, "Failed to start the SandboxedActivity and going to finish it: ", e); 83 finish(); 84 } 85 } 86 87 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE) 88 @NonNull getSandboxedActivityHandlerKey()89 String getSandboxedActivityHandlerKey() { 90 return EXTRA_SANDBOXED_ACTIVITY_HANDLER; 91 } 92 } 93