Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a98c5172 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "TouchLatency: add a menu option to select frame rate"

parents c8cc5b8f 97ef90f0
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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",
+6 −1
Original line number Diff line number Diff line
@@ -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'
    }
}
+75 −17
Original line number Diff line number Diff line
@@ -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;
+16 −0
Original line number Diff line number Diff line
<?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
+11 −7
Original line number Diff line number Diff line
@@ -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>
Loading