package com.pr0gramm.app.ui.views.viewer.video;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;

/**
 * {@link ViewBackend} that uses a {@link TextureView}
 */
class TextureViewBackend implements ViewBackend, TextureView.SurfaceTextureListener {
    private final TextureView textureView;
    private final Callbacks callbacks;

    private SurfaceTexture surfaceTexture;

    public TextureViewBackend(Context context, Callbacks callbacks) {
        this.callbacks = callbacks;

        this.textureView = new TextureView(context);
        this.textureView.setSurfaceTextureListener(this);
    }

    @Override
    public void setSize(int width, int height) {
        // do nothing
    }

    @Override
    public void setSurface(MediaPlayer mp) {
        mp.setSurface(new Surface(surfaceTexture));
    }

    @Override
    public View getView() {
        return textureView;
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        surfaceTexture = surface;
        callbacks.onAvailable(this);
        callbacks.onSizeChanged(this, width, height);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        surfaceTexture = surface;
        callbacks.onSizeChanged(this, width, height);
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        callbacks.onDestroy(this);

        if (surfaceTexture != null) {
            surfaceTexture.release();
            surfaceTexture = null;
        }

        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    }

    @Override
    public boolean hasSurface() {
        return surfaceTexture != null;
    }
}

@@ -492,29 +492,15 @@
 face
-Texture surfaceTextur
+ surfac
 e;%0A%0A
@@ -901,35 +901,15 @@
 ace(
-new Surface(surfaceTexture)
+surface
 );%0A 
@@ -1108,32 +1108,43 @@
     
+this.
 surface
-Texture = 
+ = new Surface(
 surface
+)
 ;%0A  
@@ -1350,42 +1350,8 @@
 ) %7B%0A
-        surfaceTexture = surface;%0A
     
@@ -1540,30 +1540,28 @@
     if (
+this.
 surface
-Texture
  != null
@@ -1576,30 +1576,28 @@
         
+this.
 surface
-Texture
 .release
@@ -1612,30 +1612,28 @@
         
+this.
 surface
-Texture
  = null;
@@ -1803,24 +1803,24 @@
 Surface() %7B%0A
+
         retu
@@ -1829,23 +1829,16 @@
  surface
-Texture
  != null

Fix a leak with the TextureViewBackend