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

Commit fb6aecb7 authored by Patrick Scott's avatar Patrick Scott
Browse files

Added hooks from jni to create and manage SurfaceViews for plugins.

A subclass of SurfaceView is created in order to appear like a normal
SurfaceView to jni but can be managed in Java easily. This class implements the
SurfaceHolder.Callback interface in order to notify the native PluginSurface
when the surface changes.
parent 4a06b68c
Loading
Loading
Loading
Loading
+72 −0
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@ import android.os.Process;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.ArrayList;

@@ -1826,6 +1828,76 @@ final class WebViewCore {
        }
    }

    // This class looks like a SurfaceView to native code. In java, we can
    // assume the passed in SurfaceView is this class so we can talk to the
    // ViewManager through the ChildView.
    private class SurfaceViewProxy extends SurfaceView
            implements SurfaceHolder.Callback {
        private final ViewManager.ChildView mChildView;
        private int mPointer;
        SurfaceViewProxy(Context context, ViewManager.ChildView childView,
                int pointer) {
            super(context);
            setWillNotDraw(false); // this prevents the black box artifact
            getHolder().addCallback(this);
            mChildView = childView;
            mChildView.mView = this;
            mPointer = pointer;
        }
        void destroy() {
            mPointer = 0;
            mChildView.removeView();
        }
        void attach(int x, int y, int width, int height) {
            mChildView.attachView(x, y, width, height);
        }

        // SurfaceHolder.Callback methods
        public void surfaceCreated(SurfaceHolder holder) {
            if (mPointer != 0) {
                nativeSurfaceChanged(mPointer, 0, 0, 0, 0);
            }
        }
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            if (mPointer != 0) {
                nativeSurfaceChanged(mPointer, 1, format, width, height);
            }
        }
        public void surfaceDestroyed(SurfaceHolder holder) {
            if (mPointer != 0) {
                nativeSurfaceChanged(mPointer, 2, 0, 0, 0);
            }
        }
    }

    // PluginWidget functions for mainting SurfaceViews for the Surface drawing
    // model.
    private SurfaceView createSurface(int nativePointer) {
        if (mWebView == null) {
            return null;
        }
        return new SurfaceViewProxy(mContext,
                mWebView.mViewManager.createView(), nativePointer);
    }

    private void destroySurface(SurfaceView surface) {
        SurfaceViewProxy proxy = (SurfaceViewProxy) surface;
        proxy.destroy();
    }

    private void attachSurface(SurfaceView surface, int x, int y,
            int width, int height) {
        SurfaceViewProxy proxy = (SurfaceViewProxy) surface;
        proxy.attach(x, y, width, height);
    }

    // Callback for the SurfaceHolder.Callback. Called for all the surface
    // callbacks. The state parameter is one of Created(0), Changed(1),
    // Destroyed(2).
    private native void nativeSurfaceChanged(int pointer, int state, int format,
            int width, int height);

    private native void nativePause();
    private native void nativeResume();
    private native void nativeFreeMemory();