<commit_before>package com.example.windowviewdebug;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;

import com.jmedeisis.windowview.WindowView;

/**
 * WindowView that exposes many internal properties through overlay debug text.
 */
public class DebugWindowView extends WindowView {
    private static final String LOG_TAG = DebugWindowView.class.getSimpleName();

    private boolean debugTilt = false;
    private boolean debugImage = false;
    private static final boolean DEBUG_LIFECYCLE = false;
    private final static int DEBUG_TEXT_SIZE = 32;
    private Paint debugTextPaint;

    private float latestYaw;
    private float latestPitch;
    private float latestRoll;

    public DebugWindowView(Context context) {
        super(context);
    }

    public DebugWindowView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DebugWindowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public DebugWindowView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void init(Context context, AttributeSet attrs){
        super.init(context, attrs);

        debugTextPaint = new Paint();
        debugTextPaint.setColor(Color.MAGENTA);
        debugTextPaint.setTextSize(DEBUG_TEXT_SIZE);
        debugTextPaint.setTypeface(Typeface.MONOSPACE);
    }

    @Override
    public void onTiltUpdate(float yaw, float pitch, float roll){
        super.onTiltUpdate(yaw, pitch, roll);
        this.latestYaw = yaw;
        this.latestPitch = pitch;
        this.latestRoll = roll;
    }

    /**
     * Enables/disables on-screen debug information.
     * @param debugTilt if true, displays on-screen information about the current tilt values and limits.
     * @param debugImage if true, displays on-screen information about the source image and dimensions.
     */
    public void setDebugEnabled(boolean debugTilt, boolean debugImage){
        this.debugTilt = debugTilt;
        this.debugImage = debugImage;
        invalidate();
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus){
        super.onWindowFocusChanged(hasWindowFocus);
        if(DEBUG_LIFECYCLE) Log.d(LOG_TAG, "onWindowFocusChanged(), hasWindowFocus: " + hasWindowFocus);
    }

    @Override
    protected void onAttachedToWindow(){
        super.onAttachedToWindow();
        if(DEBUG_LIFECYCLE) Log.d(LOG_TAG, "onAttachedToWindow()");
    }

    @Override
    protected void onDetachedFromWindow(){
        super.onDetachedFromWindow();
        if(DEBUG_LIFECYCLE) Log.d(LOG_TAG, "onDetachedFromWindow()");
    }

    @SuppressWarnings("UnusedAssignment")
    @Override
    protected void onDraw(@NonNull Canvas canvas){
        super.onDraw(canvas);

        int i = 0;
        if(debugImage){
            debugText(canvas, i++, "width      " + getWidth());
            debugText(canvas, i++, "height     " + getHeight());
            debugText(canvas, i++, "img width  " + (getWidth() + widthDifference));
            debugText(canvas, i++, "img height " + (getHeight() + heightDifference));

            debugText(canvas, i++, getTranslateMode() + " translateMode");

            float translateX = 0;
            float translateY = 0;
            if(heightMatches){
                translateX = (-getHorizontalOrigin() +
                        clampAbsoluteFloating(getHorizontalOrigin(), latestRoll, getMaxRoll())) / getMaxRoll();
            } else {
                translateY = (getVerticalOrigin() -
                        clampAbsoluteFloating(getVerticalOrigin(), latestPitch, getMaxPitch())) / getMaxPitch();
            }
            debugText(canvas, i++, "tx " + translateX);
            debugText(canvas, i++, "ty " + translateY);
            debugText(canvas, i++, "tx abs " + Math.round((widthDifference / 2) * translateX));
            debugText(canvas, i++, "ty abs " + Math.round((heightDifference / 2) * translateY));
            debugText(canvas, i++, "height matches " + heightMatches);
        }

        if(debugTilt){
            if(null == sensor) {
                debugText(canvas, i++, "NO PRIVATE SENSOR");
            } else {
                switch (sensor.getChosenSensorType()) {
                    case 0:
                        debugText(canvas, i++, "NO AVAILABLE SENSOR");
                        break;
                    case Sensor.TYPE_ROTATION_VECTOR:
                        debugText(canvas, i++, "ROTATION_VECTOR");
                        break;
                    case Sensor.TYPE_GRAVITY:
                        debugText(canvas, i++, "MAG + GRAVITY");
                        break;
                    case Sensor.TYPE_ACCELEROMETER:
                        debugText(canvas, i++, "MAG + ACCELEROMETER");
                        break;
                }
            }
            switch (getSensorSamplingPeriod()){
                case SensorManager.SENSOR_DELAY_FASTEST:
                    debugText(canvas, i++, "SENSOR_DELAY_FASTEST");
                    break;
                case SensorManager.SENSOR_DELAY_GAME:
                    debugText(canvas, i++, "SENSOR_DELAY_GAME");
                    break;
                case SensorManager.SENSOR_DELAY_UI:
                    debugText(canvas, i++, "SENSOR_DELAY_UI");
                    break;
                case SensorManager.SENSOR_DELAY_NORMAL:
                    debugText(canvas, i++, "SENSOR_DELAY_NORMAL");
                    break;
                default:
                    debugText(canvas, i++, "Sensor delay " + getSensorSamplingPeriod() + "us");
                    break;
            }
            debugText(canvas, i++, getTiltSensorMode() + " tiltSensorMode");
            debugText(canvas, i++, getOrientationMode() + " orientationMode");

            /*if(haveOrigin){
                SensorManager.getOrientation(rotationMatrixOrigin, orientationOrigin);
                debugText(canvas, i++, "org yaw   " + orientationOrigin[0]*DEGREES_PER_RADIAN);
                debugText(canvas, i++, "org pitch " + orientationOrigin[1]*DEGREES_PER_RADIAN);
                debugText(canvas, i++, "org roll  " + orientationOrigin[2]*DEGREES_PER_RADIAN);
            }*/

            debugText(canvas, i++, "yaw   " + latestYaw);
            debugText(canvas, i++, "pitch " + latestPitch);
            debugText(canvas, i++, "roll  " + latestRoll);

            debugText(canvas, i++, "MAX_PITCH " + getMaxPitch());
            debugText(canvas, i++, "MAX_ROLL  " + getMaxRoll());

            debugText(canvas, i++, "HOR ORIGIN " + getHorizontalOrigin());
            debugText(canvas, i++, "VER ORIGIN " + getVerticalOrigin());

            if(null != sensor) {
                switch (sensor.getScreenRotation()) {
                    case Surface.ROTATION_0:
                        debugText(canvas, i++, "ROTATION_0");
                        break;
                    case Surface.ROTATION_90:
                        debugText(canvas, i++, "ROTATION_90");
                        break;
                    case Surface.ROTATION_180:
                        debugText(canvas, i++, "ROTATION_180");
                        break;
                    case Surface.ROTATION_270:<commit_msg>Only show valid debug values for WindowViews with externally managed TiltSensors in sample-debug.<commit_after>package com.example.windowviewdebug;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;

import com.jmedeisis.windowview.WindowView;

/**
 * WindowView that exposes many internal properties through overlay debug text.
 */
public class DebugWindowView extends WindowView {
    private static final String LOG_TAG = DebugWindowView.class.getSimpleName();

    private boolean debugTilt = false;
    private boolean debugImage = false;
    private static final boolean DEBUG_LIFECYCLE = false;
    private final static int DEBUG_TEXT_SIZE = 32;
    private Paint debugTextPaint;

    private float latestYaw;
    private float latestPitch;
    private float latestRoll;

    public DebugWindowView(Context context) {
        super(context);
    }

    public DebugWindowView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DebugWindowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public DebugWindowView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void init(Context context, AttributeSet attrs){
        super.init(context, attrs);

        debugTextPaint = new Paint();
        debugTextPaint.setColor(Color.MAGENTA);
        debugTextPaint.setTextSize(DEBUG_TEXT_SIZE);
        debugTextPaint.setTypeface(Typeface.MONOSPACE);
    }

    @Override
    public void onTiltUpdate(float yaw, float pitch, float roll){
        super.onTiltUpdate(yaw, pitch, roll);
        this.latestYaw = yaw;
        this.latestPitch = pitch;
        this.latestRoll = roll;
    }

    /**
     * Enables/disables on-screen debug information.
     * @param debugTilt if true, displays on-screen information about the current tilt values and limits.
     * @param debugImage if true, displays on-screen information about the source image and dimensions.
     */
    public void setDebugEnabled(boolean debugTilt, boolean debugImage){
        this.debugTilt = debugTilt;
        this.debugImage = debugImage;
        invalidate();
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus){
        super.onWindowFocusChanged(hasWindowFocus);
        if(DEBUG_LIFECYCLE) Log.d(LOG_TAG, "onWindowFocusChanged(), hasWindowFocus: " + hasWindowFocus);
    }

    @Override
    protected void onAttachedToWindow(){
        super.onAttachedToWindow();
        if(DEBUG_LIFECYCLE) Log.d(LOG_TAG, "onAttachedToWindow()");
    }

    @Override
    protected void onDetachedFromWindow(){
        super.onDetachedFromWindow();
        if(DEBUG_LIFECYCLE) Log.d(LOG_TAG, "onDetachedFromWindow()");
    }

    @SuppressWarnings("UnusedAssignment")
    @Override
    protected void onDraw(@NonNull Canvas canvas){
        super.onDraw(canvas);

        int i = 0;
        if(debugImage){
            debugText(canvas, i++, "width      " + getWidth());
            debugText(canvas, i++, "height     " + getHeight());
            debugText(canvas, i++, "img width  " + (getWidth() + widthDifference));
            debugText(canvas, i++, "img height " + (getHeight() + heightDifference));

            debugText(canvas, i++, getTranslateMode() + " translateMode");

            float translateX = 0;
            float translateY = 0;
            if(heightMatches){
                translateX = (-getHorizontalOrigin() +
                        clampAbsoluteFloating(getHorizontalOrigin(), latestRoll, getMaxRoll())) / getMaxRoll();
            } else {
                translateY = (getVerticalOrigin() -
                        clampAbsoluteFloating(getVerticalOrigin(), latestPitch, getMaxPitch())) / getMaxPitch();
            }
            debugText(canvas, i++, "tx " + translateX);
            debugText(canvas, i++, "ty " + translateY);
            debugText(canvas, i++, "tx abs " + Math.round((widthDifference / 2) * translateX));
            debugText(canvas, i++, "ty abs " + Math.round((heightDifference / 2) * translateY));
            debugText(canvas, i++, "height matches " + heightMatches);
        }

        if(debugTilt){
            if(null == sensor) {
                debugText(canvas, i++, "EXTERNAL TILT SENSOR");
            } else {
                switch (sensor.getChosenSensorType()) {
                    case 0:
                        debugText(canvas, i++, "NO AVAILABLE SENSOR");
                        break;
                    case Sensor.TYPE_ROTATION_VECTOR:
                        debugText(canvas, i++, "ROTATION_VECTOR");
                        break;
                    case Sensor.TYPE_GRAVITY:
                        debugText(canvas, i++, "MAG + GRAVITY");
                        break;
                    case Sensor.TYPE_ACCELEROMETER:
                        debugText(canvas, i++, "MAG + ACCELEROMETER");
                        break;
                }
                switch (getSensorSamplingPeriod()) {
                    case SensorManager.SENSOR_DELAY_FASTEST:
                        debugText(canvas, i++, "SENSOR_DELAY_FASTEST");
                        break;
                    case SensorManager.SENSOR_DELAY_GAME:
                        debugText(canvas, i++, "SENSOR_DELAY_GAME");
                        break;
                    case SensorManager.SENSOR_DELAY_UI:
                        debugText(canvas, i++, "SENSOR_DELAY_UI");
                        break;
                    case SensorManager.SENSOR_DELAY_NORMAL:
                        debugText(canvas, i++, "SENSOR_DELAY_NORMAL");
                        break;
                    default:
                        debugText(canvas, i++, "Sensor delay " + getSensorSamplingPeriod() + "us");
                        break;
                }
                debugText(canvas, i++, getOrientationMode() + " orientationMode");
            }
            debugText(canvas, i++, getTiltSensorMode() + " tiltSensorMode");

            /*if(haveOrigin){
                SensorManager.getOrientation(rotationMatrixOrigin, orientationOrigin);
                debugText(canvas, i++, "org yaw   " + orientationOrigin[0]*DEGREES_PER_RADIAN);
                debugText(canvas, i++, "org pitch " + orientationOrigin[1]*DEGREES_PER_RADIAN);
                debugText(canvas, i++, "org roll  " + orientationOrigin[2]*DEGREES_PER_RADIAN);
            }*/

            debugText(canvas, i++, "yaw   " + latestYaw);
            debugText(canvas, i++, "pitch " + latestPitch);
            debugText(canvas, i++, "roll  " + latestRoll);

            debugText(canvas, i++, "MAX_PITCH " + getMaxPitch());
            debugText(canvas, i++, "MAX_ROLL  " + getMaxRoll());

            debugText(canvas, i++, "HOR ORIGIN " + getHorizontalOrigin());
            debugText(canvas, i++, "VER ORIGIN " + getVerticalOrigin());

            if(null != sensor) {
                switch (sensor.getScreenRotation()) {
                    case Surface.ROTATION_0:
                        debugText(canvas, i++, "ROTATION_0");
                        break;
                    case Surface.ROTATION_90:
                        debugText(canvas, i++, "ROTATION_90");
                        break;
                    case Surface.ROTATION_180:
                        debugText(canvas, i++, "ROTATION_180");
                        break;
                    case Surface.ROTATION_270:
{'fraction_non_alphanumeric': 0.10385756676557864, 'fraction_numerical': 0.0033544058831118566, 'mean_word_length': 2.10556891025641, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7662568', 'n_tokens_mistral': 4344, 'n_tokens_neox': 4103, 'n_words': 913}