1 /* 2 * Copyright (C) 2021 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.bedstead.testapp; 18 19 import android.content.Context; 20 21 import com.android.bedstead.nene.TestApis; 22 23 import java.io.BufferedReader; 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.io.InputStreamReader; 27 import java.util.HashSet; 28 import java.util.Set; 29 30 /** Entry point to Test App. Used for querying for {@link TestApp} instances. */ 31 public final class TestAppProvider { 32 33 private static final TestApis sTestApis = new TestApis(); 34 // Must be instrumentation context to access resources 35 private static final Context sContext = sTestApis.context().instrumentationContext(); 36 37 private boolean mTestAppsInitialised = false; 38 private final Set<TestAppDetails> mTestApps = new HashSet<>(); 39 40 /** Begin a query for a {@link TestApp}. */ query()41 public TestAppQueryBuilder query() { 42 return new TestAppQueryBuilder(this); 43 } 44 45 /** Get any {@link TestApp}. */ any()46 public TestApp any() { 47 return query().get(); 48 } 49 testApps()50 Set<TestAppDetails> testApps() { 51 initTestApps(); 52 return mTestApps; 53 } 54 initTestApps()55 private void initTestApps() { 56 if (mTestAppsInitialised) { 57 return; 58 } 59 mTestAppsInitialised = true; 60 61 int indexId = sContext.getResources().getIdentifier( 62 "raw/index", /* defType= */ null, sContext.getPackageName()); 63 64 try (InputStream inputStream = sContext.getResources().openRawResource(indexId); 65 BufferedReader bufferedReader = 66 new BufferedReader(new InputStreamReader(inputStream))) { 67 String apkName; 68 while ((apkName = bufferedReader.readLine()) != null) { 69 loadApk(apkName); 70 } 71 } catch (IOException e) { 72 throw new RuntimeException("TODO"); 73 } 74 } 75 loadApk(String apkName)76 private void loadApk(String apkName) { 77 TestAppDetails details = new TestAppDetails(); 78 details.mPackageName = "android." + apkName; // TODO: Actually index the package name 79 details.mResourceIdentifier = sContext.getResources().getIdentifier( 80 "raw/" + apkName, /* defType= */ null, sContext.getPackageName()); 81 82 mTestApps.add(details); 83 } 84 markTestAppUsed(TestAppDetails testApp)85 void markTestAppUsed(TestAppDetails testApp) { 86 mTestApps.remove(testApp); 87 } 88 } 89