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.codeproviderresources_1; 18 19 import android.app.sdksandbox.LoadSdkException; 20 import android.app.sdksandbox.SandboxedSdk; 21 import android.app.sdksandbox.SandboxedSdkProvider; 22 import android.content.Context; 23 import android.content.res.AssetManager; 24 import android.content.res.Resources; 25 import android.os.Binder; 26 import android.os.Bundle; 27 import android.view.View; 28 29 import java.io.BufferedReader; 30 import java.io.IOException; 31 import java.io.InputStreamReader; 32 33 public class ResourceSandboxedSdkProvider extends SandboxedSdkProvider { 34 35 private static final String STRING_RESOURCE = "Test String"; 36 private static final int INTEGER_RESOURCE = 1234; 37 private static final String STRING_ASSET = "This is a test asset"; 38 private static final String ASSET_FILE = "test-asset.txt"; 39 40 @Override onLoadSdk(Bundle params)41 public SandboxedSdk onLoadSdk(Bundle params) throws LoadSdkException { 42 Resources resources = getContext().getResources(); 43 String stringRes = resources.getString(R.string.test_string); 44 int integerRes = resources.getInteger(R.integer.test_integer); 45 if (!stringRes.equals(STRING_RESOURCE)) { 46 throw new LoadSdkException( 47 new Throwable(createErrorMessage(STRING_RESOURCE, stringRes)), new Bundle()); 48 } 49 if (integerRes != INTEGER_RESOURCE) { 50 throw new LoadSdkException( 51 new Throwable( 52 createErrorMessage( 53 String.valueOf(INTEGER_RESOURCE), String.valueOf(integerRes))), 54 new Bundle()); 55 } 56 57 AssetManager assets = getContext().getAssets(); 58 try (BufferedReader reader = new BufferedReader( 59 new InputStreamReader(assets.open(ASSET_FILE)))) { 60 String readAsset = reader.readLine(); 61 if (!readAsset.equals(STRING_ASSET)) { 62 throw new LoadSdkException( 63 new Throwable(createErrorMessage(STRING_ASSET, readAsset)), new Bundle()); 64 } 65 } catch (IOException e) { 66 throw new LoadSdkException( 67 new Throwable("File not found: " + ASSET_FILE), new Bundle()); 68 } 69 return new SandboxedSdk(new Binder()); 70 } 71 72 @Override getView(Context windowContext, Bundle params, int width, int height)73 public View getView(Context windowContext, Bundle params, int width, int height) { 74 return null; 75 } 76 77 /* Sends an error if the expected resource/asset does not match the read value. */ createErrorMessage(String expected, String actual)78 private String createErrorMessage(String expected, String actual) { 79 return new String("Expected " + expected + ", actual " + actual); 80 } 81 } 82