• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
18 
19 import android.app.sdksandbox.SandboxedSdkContext;
20 import android.app.sdksandbox.SandboxedSdkProvider;
21 import android.content.Context;
22 import android.content.res.AssetManager;
23 import android.content.res.Resources;
24 import android.os.Bundle;
25 import android.view.View;
26 
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.util.concurrent.Executor;
31 
32 public class ResourceSandboxedSdkProvider extends SandboxedSdkProvider {
33 
34     private static final String STRING_RESOURCE = "Test String";
35     private static final int INTEGER_RESOURCE = 1234;
36     private static final String STRING_ASSET = "This is a test asset";
37     private static final String ASSET_FILE = "test-asset.txt";
38 
39     @Override
initSdk(SandboxedSdkContext context, Bundle params, Executor executor, InitSdkCallback callback)40     public void initSdk(SandboxedSdkContext context, Bundle params,
41             Executor executor, InitSdkCallback callback) {
42         Resources resources = context.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             sendError(callback, STRING_RESOURCE, stringRes);
47         }
48         if (integerRes != INTEGER_RESOURCE) {
49             sendError(callback, String.valueOf(INTEGER_RESOURCE), String.valueOf(integerRes));
50         }
51 
52         AssetManager assets = context.getAssets();
53         try (BufferedReader reader = new BufferedReader(
54                 new InputStreamReader(assets.open(ASSET_FILE)))) {
55             String readAsset = reader.readLine();
56             if (!readAsset.equals(STRING_ASSET)) {
57                 sendError(callback, STRING_ASSET, readAsset);
58             }
59         } catch (IOException e) {
60             callback.onInitSdkError("File not found: " + ASSET_FILE);
61         }
62         callback.onInitSdkFinished(new Bundle());
63     }
64 
65     @Override
getView(Context windowContext, Bundle params)66     public View getView(Context windowContext, Bundle params) {
67         return null;
68     }
69 
70     @Override
onExtraDataReceived(Bundle extraData)71     public void onExtraDataReceived(Bundle extraData) {
72     }
73 
74     /* Sends an error if the expected resource/asset does not match the read value. */
sendError(InitSdkCallback callback, String expected, String actual)75     private void sendError(InitSdkCallback callback, String expected, String actual) {
76         callback.onInitSdkError("Expected " + expected + ", actual " + actual);
77     }
78 }
79