<commit_before>/* Copyright 2016 Samsung Electronics Co., LTD
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gearvrf;

import android.content.pm.PackageManager;
import android.graphics.PixelFormat;
import android.opengl.EGL14;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.EGLConfigChooser;
import android.opengl.GLSurfaceView.EGLContextFactory;
import android.opengl.GLSurfaceView.EGLWindowSurfaceFactory;
import android.opengl.GLSurfaceView.Renderer;
import android.util.DisplayMetrics;
import android.view.Surface;
import android.view.SurfaceHolder;

import org.gearvrf.utility.Log;
import org.gearvrf.utility.VrAppSettings;

import java.lang.ref.WeakReference;
import java.util.concurrent.CountDownLatch;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import javax.microedition.khronos.opengles.GL10;

/**
 * Keep Oculus-specifics here
 */
final class OvrVrapiActivityHandler implements OvrActivityHandler {

    private final GVRActivity mActivity;
    private long mPtr;
    private GLSurfaceView mSurfaceView;
    private EGLSurface mPixelBuffer;

    // warning: writable static state; used to determine when vrapi can be safely uninitialized
    private static WeakReference<OvrVrapiActivityHandler> sVrapiOwner = new WeakReference<>(null);
    private OvrViewManager mViewManager;
    private int mCurrentSurfaceWidth, mCurrentSurfaceHeight;

    OvrVrapiActivityHandler(final GVRActivity activity, final OvrActivityNative activityNative) throws VrapiNotAvailableException {
        if (null == activity) {
            throw new IllegalArgumentException();
        }
        try {
            activity.getPackageManager().getPackageInfo("com.oculus.systemdriver", PackageManager.GET_SIGNATURES);
        } catch (final PackageManager.NameNotFoundException e) {
            try {
                activity.getPackageManager().getPackageInfo("com.oculus.systemactivities", PackageManager.GET_SIGNATURES);
            } catch (PackageManager.NameNotFoundException e1) {
                Log.e(TAG, "oculus packages missing, assuming vrapi will not work");
                throw new VrapiNotAvailableException();
            }
        }
        mActivity = activity;
        mPtr = activityNative.getNative();

        if (null != sVrapiOwner.get()) {
            nativeUninitializeVrApi();
        }
        if (VRAPI_INITIALIZE_UNKNOWN_ERROR == nativeInitializeVrApi(mPtr)) {
            throw new VrapiNotAvailableException();
        }
        sVrapiOwner = new WeakReference<>(this);
    }

    @Override
    public void onPause() {
        if (null != mSurfaceView) {
            final CountDownLatch cdl = new CountDownLatch(1);
            mSurfaceView.onPause();
            mSurfaceView.queueEvent(new Runnable() {
                @Override
                public void run() {
                    //these two must happen on the gl thread
                    nativeLeaveVrMode(mPtr);
                    destroySurfaceForTimeWarp();
                    cdl.countDown();
                }
            });
            try {
                cdl.await();
            } catch (final InterruptedException e) {
            }
        }
        mCurrentSurfaceWidth = mCurrentSurfaceHeight = 0;
    }

    @Override
    public void onResume() {
        final OvrVrapiActivityHandler currentOwner = sVrapiOwner.get();
        if (this != currentOwner) {
            nativeUninitializeVrApi();
            nativeInitializeVrApi(mPtr);
            sVrapiOwner = new WeakReference<>(this);
        }

        if (null != mSurfaceView) {
            mSurfaceView.onResume();
        }
    }

    @Override
    public boolean onBack() {
        if (null != mSurfaceView) {
            mSurfaceView.queueEvent(new Runnable() {
                @Override
                public void run() {
                    nativeShowConfirmQuit(mPtr);
                }
            });
        }
        return true;
    }

    @Override
    public void onDestroy() {
        if (this == sVrapiOwner.get()) {
            nativeUninitializeVrApi();
            sVrapiOwner.clear();
        }
    }

    @Override
    public void setViewManager(GVRViewManager viewManager) {
        mViewManager = (OvrViewManager)viewManager;
    }

    @Override
    public void onSetScript() {
        mSurfaceView = new GLSurfaceView(mActivity);

        final DisplayMetrics metrics = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        final VrAppSettings appSettings = mActivity.getAppSettings();
        int defaultWidthPixels = Math.max(metrics.widthPixels, metrics.heightPixels);
        int defaultHeightPixels = Math.min(metrics.widthPixels, metrics.heightPixels);
        final int frameBufferWidth = appSettings.getFramebufferPixelsWide();
        final int frameBufferHeight = appSettings.getFramebufferPixelsHigh();
        final SurfaceHolder holder = mSurfaceView.getHolder();
        holder.setFormat(PixelFormat.TRANSLUCENT);

        if ((-1 != frameBufferHeight) && (-1 != frameBufferWidth)) {
            if ((defaultWidthPixels != frameBufferWidth) && (defaultHeightPixels != frameBufferHeight)) {
                Log.v(TAG, "--- window configuration ---");
                Log.v(TAG, "--- width: %d", frameBufferWidth);
                Log.v(TAG, "--- height: %d", frameBufferHeight);
                //a different resolution of the native window requested
                defaultWidthPixels = frameBufferWidth;<commit_msg>widgetplugin: ensure wp's surfaceview doesn't obscure the main one<commit_after>/* Copyright 2016 Samsung Electronics Co., LTD
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gearvrf;

import android.content.pm.PackageManager;
import android.graphics.PixelFormat;
import android.opengl.EGL14;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.EGLConfigChooser;
import android.opengl.GLSurfaceView.EGLContextFactory;
import android.opengl.GLSurfaceView.EGLWindowSurfaceFactory;
import android.opengl.GLSurfaceView.Renderer;
import android.util.DisplayMetrics;
import android.view.Surface;
import android.view.SurfaceHolder;

import org.gearvrf.utility.Log;
import org.gearvrf.utility.VrAppSettings;

import java.lang.ref.WeakReference;
import java.util.concurrent.CountDownLatch;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import javax.microedition.khronos.opengles.GL10;

/**
 * Keep Oculus-specifics here
 */
final class OvrVrapiActivityHandler implements OvrActivityHandler {

    private final GVRActivity mActivity;
    private long mPtr;
    private GLSurfaceView mSurfaceView;
    private EGLSurface mPixelBuffer;

    // warning: writable static state; used to determine when vrapi can be safely uninitialized
    private static WeakReference<OvrVrapiActivityHandler> sVrapiOwner = new WeakReference<>(null);
    private OvrViewManager mViewManager;
    private int mCurrentSurfaceWidth, mCurrentSurfaceHeight;

    OvrVrapiActivityHandler(final GVRActivity activity, final OvrActivityNative activityNative) throws VrapiNotAvailableException {
        if (null == activity) {
            throw new IllegalArgumentException();
        }
        try {
            activity.getPackageManager().getPackageInfo("com.oculus.systemdriver", PackageManager.GET_SIGNATURES);
        } catch (final PackageManager.NameNotFoundException e) {
            try {
                activity.getPackageManager().getPackageInfo("com.oculus.systemactivities", PackageManager.GET_SIGNATURES);
            } catch (PackageManager.NameNotFoundException e1) {
                Log.e(TAG, "oculus packages missing, assuming vrapi will not work");
                throw new VrapiNotAvailableException();
            }
        }
        mActivity = activity;
        mPtr = activityNative.getNative();

        if (null != sVrapiOwner.get()) {
            nativeUninitializeVrApi();
        }
        if (VRAPI_INITIALIZE_UNKNOWN_ERROR == nativeInitializeVrApi(mPtr)) {
            throw new VrapiNotAvailableException();
        }
        sVrapiOwner = new WeakReference<>(this);
    }

    @Override
    public void onPause() {
        if (null != mSurfaceView) {
            final CountDownLatch cdl = new CountDownLatch(1);
            mSurfaceView.onPause();
            mSurfaceView.queueEvent(new Runnable() {
                @Override
                public void run() {
                    //these two must happen on the gl thread
                    nativeLeaveVrMode(mPtr);
                    destroySurfaceForTimeWarp();
                    cdl.countDown();
                }
            });
            try {
                cdl.await();
            } catch (final InterruptedException e) {
            }
        }
        mCurrentSurfaceWidth = mCurrentSurfaceHeight = 0;
    }

    @Override
    public void onResume() {
        final OvrVrapiActivityHandler currentOwner = sVrapiOwner.get();
        if (this != currentOwner) {
            nativeUninitializeVrApi();
            nativeInitializeVrApi(mPtr);
            sVrapiOwner = new WeakReference<>(this);
        }

        if (null != mSurfaceView) {
            mSurfaceView.onResume();
        }
    }

    @Override
    public boolean onBack() {
        if (null != mSurfaceView) {
            mSurfaceView.queueEvent(new Runnable() {
                @Override
                public void run() {
                    nativeShowConfirmQuit(mPtr);
                }
            });
        }
        return true;
    }

    @Override
    public void onDestroy() {
        if (this == sVrapiOwner.get()) {
            nativeUninitializeVrApi();
            sVrapiOwner.clear();
        }
    }

    @Override
    public void setViewManager(GVRViewManager viewManager) {
        mViewManager = (OvrViewManager)viewManager;
    }

    @Override
    public void onSetScript() {
        mSurfaceView = new GLSurfaceView(mActivity);
        mSurfaceView.setZOrderOnTop(true);

        final DisplayMetrics metrics = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        final VrAppSettings appSettings = mActivity.getAppSettings();
        int defaultWidthPixels = Math.max(metrics.widthPixels, metrics.heightPixels);
        int defaultHeightPixels = Math.min(metrics.widthPixels, metrics.heightPixels);
        final int frameBufferWidth = appSettings.getFramebufferPixelsWide();
        final int frameBufferHeight = appSettings.getFramebufferPixelsHigh();
        final SurfaceHolder holder = mSurfaceView.getHolder();
        holder.setFormat(PixelFormat.TRANSLUCENT);

        if ((-1 != frameBufferHeight) && (-1 != frameBufferWidth)) {
            if ((defaultWidthPixels != frameBufferWidth) && (defaultHeightPixels != frameBufferHeight)) {
                Log.v(TAG, "--- window configuration ---");
                Log.v(TAG, "--- width: %d", frameBufferWidth);
                Log.v(TAG, "--- height: %d", frameBufferHeight);
                //a different resolution of the native window requested
                defaultWidthPixels = frameBufferWidth;
{'fraction_non_alphanumeric': 0.08765353325889297, 'fraction_numerical': 0.0030307864093156805, 'mean_word_length': 2.669593210418496, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4267870', 'n_tokens_mistral': 3455, 'n_tokens_neox': 3346, 'n_words': 792}