diff options
| author | 2022-11-12 11:13:01 +0000 | |
|---|---|---|
| committer | 2022-11-12 11:13:01 +0000 | |
| commit | a98c517280752d324d7252d5df14c1c20bb5909f (patch) | |
| tree | 6f235072d9dfdf162a87de685cf16fcccf17ac6f | |
| parent | c8cc5b8fb3258b19e30c5ed71474c6cb2235b4e3 (diff) | |
| parent | 97ef90f07f83160b5b64119272bdcf76d0619152 (diff) | |
Merge "TouchLatency: add a menu option to select frame rate"
8 files changed, 113 insertions, 27 deletions
diff --git a/tests/TouchLatency/Android.bp b/tests/TouchLatency/Android.bp index 3a9e240d9746..4ef1ead7d9c9 100644 --- a/tests/TouchLatency/Android.bp +++ b/tests/TouchLatency/Android.bp @@ -12,6 +12,7 @@ android_test { manifest: "app/src/main/AndroidManifest.xml", // omit gradle 'build' dir srcs: ["app/src/main/java/**/*.java"], + static_libs: ["com.google.android.material_material"], resource_dirs: ["app/src/main/res"], aaptflags: ["--auto-add-overlay"], sdk_version: "current", diff --git a/tests/TouchLatency/app/build.gradle b/tests/TouchLatency/app/build.gradle index f5ae6f4b4ffc..129baab5529d 100644 --- a/tests/TouchLatency/app/build.gradle +++ b/tests/TouchLatency/app/build.gradle @@ -6,7 +6,7 @@ android { defaultConfig { applicationId "com.prefabulated.touchlatency" - minSdkVersion 28 + minSdkVersion 30 targetSdkVersion 33 versionCode 1 versionName "1.0" @@ -17,4 +17,9 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + + dependencies { + implementation 'androidx.appcompat:appcompat:1.5.1' + implementation 'com.google.android.material:material:1.6.0' + } } diff --git a/tests/TouchLatency/app/src/main/java/com/prefabulated/touchlatency/TouchLatencyActivity.java b/tests/TouchLatency/app/src/main/java/com/prefabulated/touchlatency/TouchLatencyActivity.java index 6ab3b3e6c037..2e93c878ceac 100644 --- a/tests/TouchLatency/app/src/main/java/com/prefabulated/touchlatency/TouchLatencyActivity.java +++ b/tests/TouchLatency/app/src/main/java/com/prefabulated/touchlatency/TouchLatencyActivity.java @@ -16,7 +16,6 @@ package com.prefabulated.touchlatency; -import android.app.Activity; import android.app.ActivityOptions; import android.content.Intent; import android.hardware.display.DisplayManager; @@ -30,25 +29,49 @@ import android.view.MenuItem; import android.view.Window; import android.view.WindowManager; -public class TouchLatencyActivity extends Activity { - private Mode mDisplayModes[]; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; + +import com.google.android.material.slider.RangeSlider; +import com.google.android.material.slider.RangeSlider.OnChangeListener; + +public class TouchLatencyActivity extends AppCompatActivity { + private static final int REFRESH_RATE_SLIDER_MIN = 20; + private static final int REFRESH_RATE_SLIDER_STEP = 5; + + private Menu mMenu; + private Mode[] mDisplayModes; private int mCurrentModeIndex; + private float mSliderPreferredRefreshRate; private DisplayManager mDisplayManager; + private final DisplayManager.DisplayListener mDisplayListener = new DisplayManager.DisplayListener() { @Override public void onDisplayAdded(int i) { - invalidateOptionsMenu(); + updateOptionsMenu(); } @Override public void onDisplayRemoved(int i) { - invalidateOptionsMenu(); + updateOptionsMenu(); } @Override public void onDisplayChanged(int i) { - invalidateOptionsMenu(); + updateOptionsMenu(); + } + }; + + private final RangeSlider.OnChangeListener mRefreshRateSliderListener = new OnChangeListener() { + @Override + public void onValueChange(@NonNull RangeSlider slider, float value, boolean fromUser) { + if (value == mSliderPreferredRefreshRate) return; + + mSliderPreferredRefreshRate = value; + WindowManager.LayoutParams w = getWindow().getAttributes(); + w.preferredRefreshRate = mSliderPreferredRefreshRate; + getWindow().setAttributes(w); } }; @@ -75,17 +98,23 @@ public class TouchLatencyActivity extends Activity { Trace.endSection(); } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - Trace.beginSection("TouchLatencyActivity onCreateOptionsMenu"); - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.menu_touch_latency, menu); + public void updateOptionsMenu() { if (mDisplayModes.length > 1) { - MenuItem menuItem = menu.findItem(R.id.display_mode); + MenuItem menuItem = mMenu.findItem(R.id.display_mode); Mode currentMode = getWindowManager().getDefaultDisplay().getMode(); updateDisplayMode(menuItem, currentMode); } - updateMultiDisplayMenu(menu.findItem(R.id.multi_display)); + updateRefreshRateMenu(mMenu.findItem(R.id.frame_rate)); + updateMultiDisplayMenu(mMenu.findItem(R.id.multi_display)); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + Trace.beginSection("TouchLatencyActivity onCreateOptionsMenu"); + mMenu = menu; + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_touch_latency, mMenu); + updateOptionsMenu(); Trace.endSection(); return true; } @@ -96,6 +125,32 @@ public class TouchLatencyActivity extends Activity { menuItem.setVisible(true); } + private float getHighestRefreshRate() { + float maxRefreshRate = 0; + for (Display.Mode mode : getDisplay().getSupportedModes()) { + if (sameSizeMode(mode) && mode.getRefreshRate() > maxRefreshRate) { + maxRefreshRate = mode.getRefreshRate(); + } + } + return maxRefreshRate; + } + + private void updateRefreshRateMenu(MenuItem item) { + item.setActionView(R.layout.refresh_rate_layout); + RangeSlider slider = item.getActionView().findViewById(R.id.slider_from_layout); + slider.addOnChangeListener(mRefreshRateSliderListener); + + float highestRefreshRate = getHighestRefreshRate(); + slider.setValueFrom(REFRESH_RATE_SLIDER_MIN); + slider.setValueTo(highestRefreshRate); + slider.setStepSize(REFRESH_RATE_SLIDER_STEP); + if (mSliderPreferredRefreshRate < REFRESH_RATE_SLIDER_MIN + || mSliderPreferredRefreshRate > highestRefreshRate) { + mSliderPreferredRefreshRate = highestRefreshRate; + } + slider.setValues(mSliderPreferredRefreshRate); + } + private void updateMultiDisplayMenu(MenuItem item) { item.setVisible(mDisplayManager.getDisplays().length > 1); } @@ -105,6 +160,12 @@ public class TouchLatencyActivity extends Activity { mDisplayManager.registerDisplayListener(mDisplayListener, new Handler()); } + private boolean sameSizeMode(Display.Mode mode) { + Mode currentMode = mDisplayModes[mCurrentModeIndex]; + return currentMode.getPhysicalHeight() == mode.getPhysicalHeight() + && currentMode.getPhysicalWidth() == mode.getPhysicalWidth(); + } + public void changeDisplayMode(MenuItem item) { Window w = getWindow(); WindowManager.LayoutParams params = w.getAttributes(); @@ -112,10 +173,7 @@ public class TouchLatencyActivity extends Activity { int modeIndex = (mCurrentModeIndex + 1) % mDisplayModes.length; while (modeIndex != mCurrentModeIndex) { // skip modes with different resolutions - Mode currentMode = mDisplayModes[mCurrentModeIndex]; - Mode nextMode = mDisplayModes[modeIndex]; - if (currentMode.getPhysicalHeight() == nextMode.getPhysicalHeight() - && currentMode.getPhysicalWidth() == nextMode.getPhysicalWidth()) { + if (sameSizeMode(mDisplayModes[modeIndex])) { break; } modeIndex = (modeIndex + 1) % mDisplayModes.length; diff --git a/tests/TouchLatency/app/src/main/res/layout/refresh_rate_layout.xml b/tests/TouchLatency/app/src/main/res/layout/refresh_rate_layout.xml new file mode 100644 index 000000000000..bb9ce609c56f --- /dev/null +++ b/tests/TouchLatency/app/src/main/res/layout/refresh_rate_layout.xml @@ -0,0 +1,16 @@ +<?xml version="1.0" encoding="utf-8"?> +<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" + xmlns:app="http://schemas.android.com/apk/res-auto"> + + <com.google.android.material.slider.RangeSlider + android:id="@+id/slider_from_layout" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + app:tickColor="@color/cardview_light_background" + app:trackColor="@color/cardview_light_background" + app:thumbColor="@color/cardview_dark_background" + android:visibility="visible"/> + +</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file diff --git a/tests/TouchLatency/app/src/main/res/menu/menu_touch_latency.xml b/tests/TouchLatency/app/src/main/res/menu/menu_touch_latency.xml index abc7fd5d6bb2..7169021b6653 100644 --- a/tests/TouchLatency/app/src/main/res/menu/menu_touch_latency.xml +++ b/tests/TouchLatency/app/src/main/res/menu/menu_touch_latency.xml @@ -14,21 +14,25 @@ limitations under the License. --> <menu xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".TouchLatencyActivity"> <item android:id="@+id/action_settings" android:orderInCategory="101" - android:showAsAction="always" - android:title="@string/mode"/> + android:title="@string/mode" + app:showAsAction="always" /> + <item + android:id="@+id/frame_rate" + android:title="@string/frame_rate" + app:showAsAction="collapseActionView" /> <item android:id="@+id/display_mode" - android:showAsAction="ifRoom" android:title="@string/display_mode" - android:visible="false"/> - + android:visible="false" + app:showAsAction="always" /> <item android:id="@+id/multi_display" - android:showAsAction="ifRoom" android:title="@string/multi_display" - android:visible="false"/> + android:visible="false" + app:showAsAction="ifRoom" /> </menu> diff --git a/tests/TouchLatency/app/src/main/res/values/strings.xml b/tests/TouchLatency/app/src/main/res/values/strings.xml index 5ee86d8bd8bf..cad2df78ffcd 100644 --- a/tests/TouchLatency/app/src/main/res/values/strings.xml +++ b/tests/TouchLatency/app/src/main/res/values/strings.xml @@ -18,5 +18,6 @@ <string name="mode">Touch</string> <string name="display_mode">Mode</string> + <string name="frame_rate">Frame Rate</string> <string name="multi_display">multi-display</string> </resources> diff --git a/tests/TouchLatency/app/src/main/res/values/styles.xml b/tests/TouchLatency/app/src/main/res/values/styles.xml index 22da7c1d050b..b23a87e57754 100644 --- a/tests/TouchLatency/app/src/main/res/values/styles.xml +++ b/tests/TouchLatency/app/src/main/res/values/styles.xml @@ -16,7 +16,7 @@ <resources> <!-- Base application theme. --> - <style name="AppTheme" parent="@android:style/Theme.Material.Light.DarkActionBar"> + <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> <!-- Customize your theme here. --> </style> diff --git a/tests/TouchLatency/gradle.properties b/tests/TouchLatency/gradle.properties index 1d3591c8a4c9..ccd5dda1d6fa 100644 --- a/tests/TouchLatency/gradle.properties +++ b/tests/TouchLatency/gradle.properties @@ -15,4 +15,5 @@ # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects -# org.gradle.parallel=true
\ No newline at end of file +# org.gradle.parallel=true +android.useAndroidX=true
\ No newline at end of file |