<commit_before>package org.libsdl.app;


import android.content.*;
import android.view.*;
import android.os.*;
import android.util.Log;
import android.graphics.*;
import android.hardware.*;
import android.content.pm.ActivityInfo;

/**
    SDLSurfaceReplacement
*/
public class SDLSurfaceReplacement extends SDLSurfaceAlternative
    implements SurfaceHolder.Callback,
    TextureView.SurfaceTextureListener, // !!! augmentation [2017.10.28 c4augustus]
    View.OnKeyListener, View.OnTouchListener, SensorEventListener {

    // Sensors
    protected static SensorManager mSensorManager;
    protected static Display mDisplay;

    // Keep track of the surface size to normalize touch events
    protected static float mWidth, mHeight;

    // !!! augmentation [2017.10.28 c4augustus]
    private Surface mSurface;

    // !!! augmentation [2017.10.28 c4augustus]
    // BEGIN copied from Android API 22 SurfaceView
    /* Set SurfaceView's format to 565 by default to maintain backward
     * compatibility with applications assuming this format.
     */
    int mFormat = PixelFormat.RGB_565;
    boolean mHaveFrame = false;
    private final ViewTreeObserver.OnPreDrawListener mDrawListener =
            new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    // reposition ourselves where the surface is
                    mHaveFrame = getWidth() > 0 && getHeight() > 0;
                    // !!! TODO: ORIGINAL CODE FROM SurfaceView
                    //updateWindow(false, false);
                    if (mHaveFrame) {
                        surfaceChanged(null, mFormat, getWidth(), getHeight());
                    }
                    return true;
                }
            };
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    /*
        mParent.requestTransparentRegion(this);
        mSession = getWindowSession();
        mLayout.token = getWindowToken();
        mLayout.setTitle("SurfaceView");
        mViewVisibility = getVisibility() == VISIBLE;

        if (!mGlobalListenersAdded) {
            ViewTreeObserver observer = getViewTreeObserver();
            observer.addOnScrollChangedListener(mScrollChangedListener);
            observer.addOnPreDrawListener(mDrawListener);
            mGlobalListenersAdded = true;
        }
    */
    }
    // END copied from Android API 22 SurfaceView

    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        surfaceChanged(null, mFormat, width, height);
    }

    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        surfaceChanged(null, mFormat, width, height);
    }

    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        Log.v("SDL", "surfaceDestroyed()");
        // Call this *before* setting mIsSurfaceReady to 'false'
        SDLActivity.handlePause();
        SDLActivity.mIsSurfaceReady = false;
        SDLActivity.onNativeSurfaceDestroyed();
        return true;
    }

    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    }

    // Startup
    public SDLSurfaceReplacement(Context context, Surface surface) {
        super(context);
        /* !!! ORIGINAL CODE FROM SDLSurface
        getHolder().addCallback(this);
        */
        setSurfaceTextureListener(this); // !!! augmentation [2017.10.28 c4augustus]
        //mSurface = surface; // !!! augmentation [2017.10.28 c4augustus]
        setFocusable(true);
        setFocusableInTouchMode(true);
        requestFocus();
        setOnKeyListener(this);
        setOnTouchListener(this);

        mDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);

        if(Build.VERSION.SDK_INT >= 12) {
            setOnGenericMotionListener(new SDLGenericMotionListener_API12());
        }

        // Some arbitrary defaults to avoid a potential division by zero
        mWidth = 1.0f;
        mHeight = 1.0f;
    }

    public void handlePause() {
        enableSensor(Sensor.TYPE_ACCELEROMETER, false);
    }

    public void handleResume() {
        setFocusable(true);
        setFocusableInTouchMode(true);
        requestFocus();
        setOnKeyListener(this);
        setOnTouchListener(this);
        enableSensor(Sensor.TYPE_ACCELEROMETER, true);
    }

    public Surface getNativeSurface() {
        /* !!! ORIGINAL CODE FROM SDLSurface
        return getHolder().getSurface();
        */
        // !!! augmentation [2017.10.28 c4augustus]
        if (mSurface == null) {
            mSurface = new Surface(this.getSurfaceTexture());
        }
        return mSurface;
    }

    // Called when we have a valid drawing surface
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.v("SDL", "surfaceCreated()");
        holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
    }

    // Called when we lose the surface
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.v("SDL", "surfaceDestroyed()");
        // Call this *before* setting mIsSurfaceReady to 'false'
        SDLActivity.handlePause();
        SDLActivity.mIsSurfaceReady = false;
        SDLActivity.onNativeSurfaceDestroyed();
    }

    // Called when the surface is resized
    @Override
    public void surfaceChanged(SurfaceHolder holder,
                               int format, int width, int height) {
        Log.v("SDL", "surfaceChanged()");<commit_msg>enhance Android SDLSurfaceReplacement with an option to set a fixed content width and height that is scaled to fit within the TextureView<commit_after>package org.libsdl.app;


import android.content.*;
import android.view.*;
import android.os.*;
import android.util.Log;
import android.graphics.*;
import android.hardware.*;
import android.content.pm.ActivityInfo;

/**
    SDLSurfaceReplacement
*/
public class SDLSurfaceReplacement extends SDLSurfaceAlternative
    implements SurfaceHolder.Callback,
    TextureView.SurfaceTextureListener, // !!! augmentation [2017.10.28 c4augustus]
    View.OnKeyListener, View.OnTouchListener, SensorEventListener {

    // Sensors
    protected static SensorManager mSensorManager;
    protected static Display mDisplay;

    // Keep track of the surface size to normalize touch events
    protected static float mWidth, mHeight;

    // !!! augmentation [2017.10.28 c4augustus]
    private int mFixedContentWidth;
    private int mFixedContentHeight;
    private Surface mSurface;

    // !!! augmentation [2017.10.28 c4augustus]
    // BEGIN copied from Android API 22 SurfaceView
    /* Set SurfaceView's format to 565 by default to maintain backward
     * compatibility with applications assuming this format.
     */
    int mFormat = PixelFormat.RGB_565;
    boolean mHaveFrame = false;
    private final ViewTreeObserver.OnPreDrawListener mDrawListener =
            new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    // reposition ourselves where the surface is
                    mHaveFrame = getWidth() > 0 && getHeight() > 0;
                    // !!! TODO: ORIGINAL CODE FROM SurfaceView
                    //updateWindow(false, false);
                    if (mHaveFrame) {
                        surfaceChanged(null, mFormat, getWidth(), getHeight());
                    }
                    return true;
                }
            };
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    /*
        mParent.requestTransparentRegion(this);
        mSession = getWindowSession();
        mLayout.token = getWindowToken();
        mLayout.setTitle("SurfaceView");
        mViewVisibility = getVisibility() == VISIBLE;

        if (!mGlobalListenersAdded) {
            ViewTreeObserver observer = getViewTreeObserver();
            observer.addOnScrollChangedListener(mScrollChangedListener);
            observer.addOnPreDrawListener(mDrawListener);
            mGlobalListenersAdded = true;
        }
    */
    }
    // END copied from Android API 22 SurfaceView

    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        surfaceChanged(null, mFormat, width, height);
    }

    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        surfaceChanged(null, mFormat, width, height);
    }

    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        Log.v("SDL", "surfaceDestroyed()");
        // Call this *before* setting mIsSurfaceReady to 'false'
        SDLActivity.handlePause();
        SDLActivity.mIsSurfaceReady = false;
        SDLActivity.onNativeSurfaceDestroyed();
        return true;
    }

    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    }

    // Startup
    public SDLSurfaceReplacement(
            Context context,
            int fixedContentWidth,
            int fixedContentHeight) {
        super(context);
        mFixedContentWidth = fixedContentWidth;
        mFixedContentHeight = fixedContentHeight;
        /* !!! ORIGINAL CODE FROM SDLSurface
        getHolder().addCallback(this);
        */
        setSurfaceTextureListener(this); // !!! augmentation [2017.10.28 c4augustus]
        setFocusable(true);
        setFocusableInTouchMode(true);
        requestFocus();
        setOnKeyListener(this);
        setOnTouchListener(this);

        mDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);

        if(Build.VERSION.SDK_INT >= 12) {
            setOnGenericMotionListener(new SDLGenericMotionListener_API12());
        }

        // Some arbitrary defaults to avoid a potential division by zero
        mWidth = 1.0f;
        mHeight = 1.0f;
    }

    public void handlePause() {
        enableSensor(Sensor.TYPE_ACCELEROMETER, false);
    }

    public void handleResume() {
        setFocusable(true);
        setFocusableInTouchMode(true);
        requestFocus();
        setOnKeyListener(this);
        setOnTouchListener(this);
        enableSensor(Sensor.TYPE_ACCELEROMETER, true);
    }

    public Surface getNativeSurface() {
        /* !!! ORIGINAL CODE FROM SDLSurface
        return getHolder().getSurface();
        */
        // !!! augmentation [2017.10.28 c4augustus]
        if (mSurface == null) {
            mSurface = new Surface(this.getSurfaceTexture());
        }
        return mSurface;
    }

    // Called when we have a valid drawing surface
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.v("SDL", "surfaceCreated()");
        holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
    }

    // Called when we lose the surface
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.v("SDL", "surfaceDestroyed()");
        // Call this *before* setting mIsSurfaceReady to 'false'
        SDLActivity.handlePause();
        SDLActivity.mIsSurfaceReady = false;
        SDLActivity.onNativeSurfaceDestroyed();
    }

    // Called when the surface is resized
    @Override
    public void surfaceChanged(SurfaceHolder holder,
                               int format, int width, int height) {
        if (mFixedContentWidth > 0 && mFixedContentHeight > 0
                && (width != mFixedContentWidth
                        || height != mFixedContentHeight)) {
            Matrix matrix = new Matrix();
            matrix.setTranslate(0, mFixedContentHeight - height);
            matrix.postScale(
                    (float)width / (float)mFixedContentWidth,
                    (float)height / (float)mFixedContentHeight);
            setTransform(matrix);
            width = mFixedContentWidth;
            height = mFixedContentHeight;
        }
        Log.v("SDL", "surfaceChanged()");
{'fraction_non_alphanumeric': 0.09053293611386957, 'fraction_numerical': 0.011751075802714332, 'mean_word_length': 2.485722526680127, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6266885', 'n_tokens_mistral': 3305, 'n_tokens_neox': 3092, 'n_words': 750}