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 com.android.sdksandbox.app;
18 
19 import android.app.Activity;
20 import android.app.sdksandbox.SdkSandboxManager;
21 import android.app.sdksandbox.testutils.FakeLoadSdkCallback;
22 import android.app.sdksandbox.testutils.FakeSdkSandboxProcessDeathCallback;
23 import android.os.Bundle;
24 import android.util.Log;
25 
26 public class SdkSandboxTestActivity extends Activity {
27 
28     private static final String TAG = "SdkSandboxLifecycleTestActivity";
29     private static final String SDK_NAME = "com.android.testcode";
30     private static final String SDK_NAME_2 = "com.android.testcode2";
31 
32     @Override
onCreate(Bundle icicle)33     public void onCreate(Bundle icicle) {
34         super.onCreate(icicle);
35         Log.d(TAG, "Loading SDKs");
36 
37         SdkSandboxManager sdkSandboxManager =
38                 getApplicationContext().getSystemService(SdkSandboxManager.class);
39         assert sdkSandboxManager != null;
40 
41         // Add a callback so that this app does not die when the sandbox dies.
42         sdkSandboxManager.addSdkSandboxProcessDeathCallback(
43                 Runnable::run, new FakeSdkSandboxProcessDeathCallback());
44 
45         if (!sdkSandboxManager.getSandboxedSdks().isEmpty()) {
46             // Only load SDKs if they haven't already been loaded (e.g. if the activity is being
47             // recreated).
48             Log.d(TAG, "Skipping loading SDKs since already loaded");
49             return;
50         }
51 
52         Bundle params = new Bundle();
53         FakeLoadSdkCallback callback = new FakeLoadSdkCallback();
54         FakeLoadSdkCallback callback2 = new FakeLoadSdkCallback();
55         sdkSandboxManager.loadSdk(SDK_NAME, params, Runnable::run, callback);
56         sdkSandboxManager.loadSdk(SDK_NAME_2, params, Runnable::run, callback2);
57         callback.assertLoadSdkIsSuccessful();
58         callback2.assertLoadSdkIsSuccessful();
59     }
60 }
61