1 /*
2  * Copyright (C) 2024 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.socketapp;
18 
19 import android.app.sdksandbox.SandboxedSdk;
20 import android.app.sdksandbox.SandboxedSdkProvider;
21 import android.content.Context;
22 import android.net.LocalServerSocket;
23 import android.net.LocalSocket;
24 import android.os.Binder;
25 import android.os.Bundle;
26 import android.util.Log;
27 import android.view.View;
28 
29 import java.io.IOException;
30 
31 /* This Provider is used to test sockets to and from SDK sandbox processes. */
32 public class SocketSdkProvider extends SandboxedSdkProvider {
33 
34     private static final String SOCKET_NAME = "SocketSdkProvider";
35     private static final String TAG = "SocketSdkProvider";
36 
37     @Override
onLoadSdk(Bundle params)38     public final SandboxedSdk onLoadSdk(Bundle params) {
39         new Thread(this::connect).start();
40 
41         return new SandboxedSdk(new Binder());
42     }
43 
44     @Override
getView(Context windowContext, Bundle params, int width, int height)45     public final View getView(Context windowContext, Bundle params, int width, int height) {
46         return null;
47     }
48 
connect()49     private void connect() {
50         Log.i(TAG, "waiting connections from sandbox");
51         try (LocalServerSocket serverSocket = new LocalServerSocket(SOCKET_NAME);
52                 LocalSocket localSocket = serverSocket.accept()) {
53             throw new IOException("This should not happen");
54         } catch (IOException e) {
55             throw new RuntimeException("Crashing test sandbox", e);
56         }
57     }
58 }
59