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.sdksandboxcode; 18 19 import android.app.sdksandbox.SandboxedSdkContext; 20 import android.app.sdksandbox.SandboxedSdkProvider; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.graphics.Canvas; 24 import android.graphics.Color; 25 import android.graphics.Paint; 26 import android.net.Uri; 27 import android.os.Bundle; 28 import android.view.View; 29 import android.widget.Toast; 30 31 import java.util.Random; 32 import java.util.concurrent.Executor; 33 34 public class SampleSandboxedSdkProvider extends SandboxedSdkProvider { 35 36 private SandboxedSdkContext mContext; 37 38 @Override initSdk(SandboxedSdkContext context, Bundle params, Executor executor, InitSdkCallback callback)39 public void initSdk(SandboxedSdkContext context, Bundle params, 40 Executor executor, InitSdkCallback callback) { 41 mContext = context; 42 callback.onInitSdkFinished(null); 43 } 44 45 @Override getView(Context windowContext, Bundle params)46 public View getView(Context windowContext, Bundle params) { 47 return new TestView(windowContext, mContext); 48 } 49 50 @Override onExtraDataReceived(Bundle extraData)51 public void onExtraDataReceived(Bundle extraData) { 52 } 53 54 private static class TestView extends View { 55 56 private SandboxedSdkContext mSandboxedSdkContext; 57 TestView(Context context, SandboxedSdkContext sandboxedSdkContext)58 TestView(Context context, SandboxedSdkContext sandboxedSdkContext) { 59 super(context); 60 mSandboxedSdkContext = sandboxedSdkContext; 61 } 62 63 @Override onDraw(Canvas canvas)64 protected void onDraw(Canvas canvas) { 65 super.onDraw(canvas); 66 67 Paint paint = new Paint(); 68 paint.setStyle(Paint.Style.FILL); 69 paint.setColor(Color.WHITE); 70 paint.setTextSize(50); 71 Random random = new Random(); 72 String message = mSandboxedSdkContext.getResources().getString(R.string.view_message); 73 int c = Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)); 74 canvas.drawColor(c); 75 canvas.drawText(message, 75, 75, paint); 76 77 setOnClickListener(this::onClickListener); 78 } 79 onClickListener(View view)80 private void onClickListener(View view) { 81 Context context = view.getContext(); 82 Toast.makeText(context, "Opening url", Toast.LENGTH_LONG).show(); 83 84 String url = "http://www.google.com"; 85 Intent visitUrl = new Intent(Intent.ACTION_VIEW); 86 visitUrl.setData(Uri.parse(url)); 87 visitUrl.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 88 89 mContext.startActivity(visitUrl); 90 } 91 92 } 93 } 94