1 /* 2 * Copyright (C) 2023 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 android.adpf.hintsession.app; 18 19 import static android.adpf.common.ADPFHintSessionConstants.TESTS_ENABLED; 20 import static android.adpf.common.ADPFHintSessionConstants.TEST_NAME_KEY; 21 import static android.adpf.common.ADPFHintSessionConstants.IS_HINT_SESSION_SUPPORTED_KEY; 22 23 import android.app.KeyguardManager; 24 import android.app.NativeActivity; 25 import android.content.Intent; 26 import android.os.Bundle; 27 import android.util.Log; 28 import android.view.WindowManager; 29 30 import java.util.HashMap; 31 import java.util.Map; 32 33 /** 34 * A simple activity to create and use hint session APIs. 35 */ 36 public class ADPFHintSessionDeviceActivity 37 extends NativeActivity { 38 39 static { 40 System.loadLibrary( 41 "adpfhintsession_test_helper_jni"); 42 } 43 onResume()44 protected void onResume() { 45 super.onResume(); 46 setFullscreen(); 47 } 48 setFailure(String message)49 protected void setFailure(String message) { 50 synchronized (mMetrics) { 51 mMetrics.put("failure", message); 52 } 53 } 54 55 private static final String TAG = android.adpf.hintsession.app 56 .ADPFHintSessionDeviceActivity.class.getSimpleName(); 57 58 private final Map<String, String> mMetrics = new HashMap<>(); 59 60 /** 61 * Flag used to indicate tests are finished, used by 62 * waitForTestFinished to allow the instrumentation to block 63 * on test completion correctly. 64 */ 65 private Boolean mFinished = false; 66 private final Object mFinishedLock = new Object(); 67 68 @Override onCreate(Bundle icicle)69 public void onCreate(Bundle icicle) { 70 super.onCreate(icicle); 71 keepScreenOn(); 72 Intent intent = getIntent(); 73 String testName = intent.getStringExtra( 74 TEST_NAME_KEY); 75 if (testName == null) { 76 setFailure("Test starts without name"); 77 return; 78 } 79 Log.i(TAG, "Device activity created"); 80 sendConfigToNative(TESTS_ENABLED); 81 } 82 keepScreenOn()83 private void keepScreenOn() { 84 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 85 setTurnScreenOn(true); 86 KeyguardManager km = getSystemService(KeyguardManager.class); 87 if (km != null) { 88 km.requestDismissKeyguard(this, null); 89 } 90 } 91 setFullscreen()92 private void setFullscreen() { 93 getWindow().setFlags( 94 WindowManager.LayoutParams.FLAG_FULLSCREEN, 95 WindowManager.LayoutParams.FLAG_FULLSCREEN); 96 } 97 98 @Override onStart()99 public void onStart() { 100 super.onStart(); 101 } 102 103 /** 104 * Sends test information to the native code and signals 105 * it to start running performance tests. 106 */ sendConfigToNative(String[] data)107 public native void sendConfigToNative(String[] data); 108 109 /** 110 * Sends test information from the native code back up 111 * to the Java code once testing is complete 112 */ sendResultsToJava(String[] names, String[] values)113 public void sendResultsToJava(String[] names, 114 String[] values) { 115 synchronized (mMetrics) { 116 for (int i = 0; i < names.length; ++i) { 117 mMetrics.put(names[i], values[i]); 118 } 119 String key = mMetrics.get(IS_HINT_SESSION_SUPPORTED_KEY); 120 if (key != null && key.equals("false")) { 121 Log.i(TAG, "Skipping the test as the hint session is not supported"); 122 } 123 } 124 125 setFinished(); 126 } 127 128 /** 129 * Signals to the app that everything is finished, 130 */ setFinished()131 public void setFinished() { 132 synchronized (mFinishedLock) { 133 mFinished = true; 134 mFinishedLock.notifyAll(); 135 } 136 } 137 138 /** 139 * Blocks until the test has completed, to allow instrumentation 140 * to wait for the test to completely finish 141 */ waitForTestFinished()142 public void waitForTestFinished() { 143 while (true) { 144 synchronized (mFinishedLock) { 145 if (mFinished) { 146 break; 147 } 148 try { 149 mFinishedLock.wait(); 150 } catch (InterruptedException e) { 151 System.err.println("Interrupted!"); 152 break; 153 } 154 } 155 } 156 } 157 158 /** 159 * Gets the hint session test metrics. 160 */ getMetrics()161 public Map<String, String> getMetrics() { 162 synchronized (mMetrics) { 163 return new HashMap<>(mMetrics); 164 } 165 } 166 } 167