<commit_before>#include "chronotext/android/cinder/CinderDelegate.h"
#include "chronotext/InputSource.h"
#include "chronotext/utils/accel/AccelEvent.h"

#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>

using namespace ci;
using namespace ci::app;
using namespace std;

#define GRAVITY_EARTH 9.80665f

/*
 * CALLED ON THE RENDERER'S THREAD FROM chronotext.android.gl.GLRenderer.onSurfaceCreated()
 */
void CinderDelegate::launch(JavaVM *javaVM, jobject javaContext, jobject javaListener)
{
    mJavaVM = javaVM;
    mJavaListener = javaListener;

    JNIEnv *env;
    javaVM->GetEnv((void**)&env, JNI_VERSION_1_4);

    // ---

    jmethodID getAssetsMethod = env->GetMethodID(env->GetObjectClass(javaContext), "getAssets", "()Landroid/content/res/AssetManager;");
    AAssetManager *assetManager = AAssetManager_fromJava(env, env->CallObjectMethod(javaContext, getAssetsMethod));

    InputSource::setAndroidAssetManager(assetManager);

    // ---

    jmethodID getFilesDirMethod = env->GetMethodID(env->GetObjectClass(javaContext), "getFilesDir", "()Ljava/io/File;");
    jobject filesDirObject = env->CallObjectMethod(javaContext, getFilesDirMethod);
    jmethodID getAbsolutePathMethod = env->GetMethodID(env->GetObjectClass(filesDirObject), "getAbsolutePath", "()Ljava/lang/String;");
    jstring absolutePathString = (jstring)env->CallObjectMethod(filesDirObject, getAbsolutePathMethod);

    const char *internalDataPath = env->GetStringUTFChars(absolutePathString, NULL);
    InputSource::setAndroidInternalDataPath(internalDataPath);
    env->ReleaseStringUTFChars(absolutePathString, internalDataPath);

    // ---

    jmethodID getPackageCodePathMethod = env->GetMethodID(env->GetObjectClass(javaContext), "getPackageCodePath", "()Ljava/lang/String;");
    jstring packageCodePathString = (jstring)env->CallObjectMethod(javaContext, getPackageCodePathMethod);

    const char *apkPath = env->GetStringUTFChars(packageCodePathString, NULL);
    InputSource::setAndroidApkPath(apkPath);
    env->ReleaseStringUTFChars(packageCodePathString, apkPath);

    // ---

    ALooper *looper = ALooper_forThread();

    if (!looper)
    {
        looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
    }

    mSensorManager = ASensorManager_getInstance();
    mAccelerometerSensor = ASensorManager_getDefaultSensor(mSensorManager, ASENSOR_TYPE_ACCELEROMETER);
    mSensorEventQueue = ASensorManager_createEventQueue(mSensorManager, looper, 3, NULL, NULL/*sensorEventCallback, this*/); // WOULD BE BETTER TO USE A CALL-BACK, BUT IT'S NOT WORKING
}

void CinderDelegate::processSensorEvents()
{
    ASensorEvent event;

    while (ASensorEventQueue_getEvents(mSensorEventQueue, &event, 1) > 0)
    {
      if (event.type == ASENSOR_TYPE_ACCELEROMETER)
      {
          float x = event.acceleration.x;
          float y = event.acceleration.y;
          float z = event.acceleration.z;

          /*
           * APPLYING THE EVENTUAL ORIENTATION FIX
           */
          if (mAccelerometerRotation == ACCELEROMETER_ROTATION_LANDSCAPE)
          {
              float tmp = x;
              x = -y;
              y = +tmp;
          }
          else if (mAccelerometerRotation == ACCELEROMETER_ROTATION_PORTRAIT)
          {
              float tmp = x;
              x = +y;
              y = -tmp;
          }

          /*
           * FOR CONSISTENCY WITH iOS
           */
          x /= -GRAVITY_EARTH;
          y /= +GRAVITY_EARTH;
          z /= +GRAVITY_EARTH;

          accelerated(x, y, z);
      }
    }
}

void CinderDelegate::accelerated(float x, float y, float z)
{
    Vec3f acceleration(x, y, z);
    Vec3f filtered = mLastAccel * (1 - mAccelFilterFactor) + acceleration * mAccelFilterFactor;

    AccelEvent event(filtered, acceleration, mLastAccel, mLastRawAccel);
    sketch->accelerated(event);

    mLastAccel = filtered;
    mLastRawAccel = acceleration;
}

void CinderDelegate::setup(int width, int height, int accelerometerRotation)
{
    mWidth = width;
    mHeight = height;
    mAccelerometerRotation = accelerometerRotation;

    sketch->setup(false);
    sketch->resize();
}

void CinderDelegate::shutdown()
{
    ASensorManager_destroyEventQueue(mSensorManager, mSensorEventQueue);

    sketch->shutdown();
    delete sketch;
}

void CinderDelegate::draw()
{
    /*
     * WOULD BE BETTER TO USE A CALL-BACK, BUT IT'S NOT WORKING
     */
    processSensorEvents();

    sketch->run(); // NECESSARY FOR THE "MESSAGE-PUMP"
    sketch->update();
    sketch->draw();
    mFrameCount++;
}

void CinderDelegate::event(int id)
{
    switch (id)
    {
        case EVENT_ATTACHED:
        case EVENT_SHOWN:
            mFrameCount = 0;
            mTimer.start();
            sketch->start(CinderSketch::FLAG_FOCUS_GAINED);
            break;
            
        case EVENT_RESUMED:
            mFrameCount = 0;
            mTimer.start();
            
            /*
             * ASSERTION: THE GL CONTEXT HAS JUST BEEN RE-CREATED
             */
            sketch->setup(true);
            sketch->resize();
            
            sketch->start(CinderSketch::FLAG_APP_RESUME);
            break;
            
        case EVENT_DETACHED:
        case EVENT_HIDDEN:
            mTimer.stop();
            sketch->stop(CinderSketch::FLAG_FOCUS_LOST);
            break;
            
        case EVENT_PAUSED:
            mTimer.stop();
            sketch->stop(CinderSketch::FLAG_APP_PAUSE);
            break;
<commit_msg>ANDROID: CinderDelegate: NOT CALLING Sketch::resize UPON RESUME (CONSISTENT WITH iOS VERSION)<commit_after>#include "chronotext/android/cinder/CinderDelegate.h"
#include "chronotext/InputSource.h"
#include "chronotext/utils/accel/AccelEvent.h"

#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>

using namespace ci;
using namespace ci::app;
using namespace std;

#define GRAVITY_EARTH 9.80665f

/*
 * CALLED ON THE RENDERER'S THREAD FROM chronotext.android.gl.GLRenderer.onSurfaceCreated()
 */
void CinderDelegate::launch(JavaVM *javaVM, jobject javaContext, jobject javaListener)
{
    mJavaVM = javaVM;
    mJavaListener = javaListener;

    JNIEnv *env;
    javaVM->GetEnv((void**)&env, JNI_VERSION_1_4);

    // ---

    jmethodID getAssetsMethod = env->GetMethodID(env->GetObjectClass(javaContext), "getAssets", "()Landroid/content/res/AssetManager;");
    AAssetManager *assetManager = AAssetManager_fromJava(env, env->CallObjectMethod(javaContext, getAssetsMethod));

    InputSource::setAndroidAssetManager(assetManager);

    // ---

    jmethodID getFilesDirMethod = env->GetMethodID(env->GetObjectClass(javaContext), "getFilesDir", "()Ljava/io/File;");
    jobject filesDirObject = env->CallObjectMethod(javaContext, getFilesDirMethod);
    jmethodID getAbsolutePathMethod = env->GetMethodID(env->GetObjectClass(filesDirObject), "getAbsolutePath", "()Ljava/lang/String;");
    jstring absolutePathString = (jstring)env->CallObjectMethod(filesDirObject, getAbsolutePathMethod);

    const char *internalDataPath = env->GetStringUTFChars(absolutePathString, NULL);
    InputSource::setAndroidInternalDataPath(internalDataPath);
    env->ReleaseStringUTFChars(absolutePathString, internalDataPath);

    // ---

    jmethodID getPackageCodePathMethod = env->GetMethodID(env->GetObjectClass(javaContext), "getPackageCodePath", "()Ljava/lang/String;");
    jstring packageCodePathString = (jstring)env->CallObjectMethod(javaContext, getPackageCodePathMethod);

    const char *apkPath = env->GetStringUTFChars(packageCodePathString, NULL);
    InputSource::setAndroidApkPath(apkPath);
    env->ReleaseStringUTFChars(packageCodePathString, apkPath);

    // ---

    ALooper *looper = ALooper_forThread();

    if (!looper)
    {
        looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
    }

    mSensorManager = ASensorManager_getInstance();
    mAccelerometerSensor = ASensorManager_getDefaultSensor(mSensorManager, ASENSOR_TYPE_ACCELEROMETER);
    mSensorEventQueue = ASensorManager_createEventQueue(mSensorManager, looper, 3, NULL, NULL/*sensorEventCallback, this*/); // WOULD BE BETTER TO USE A CALL-BACK, BUT IT'S NOT WORKING
}

void CinderDelegate::processSensorEvents()
{
    ASensorEvent event;

    while (ASensorEventQueue_getEvents(mSensorEventQueue, &event, 1) > 0)
    {
      if (event.type == ASENSOR_TYPE_ACCELEROMETER)
      {
          float x = event.acceleration.x;
          float y = event.acceleration.y;
          float z = event.acceleration.z;

          /*
           * APPLYING THE EVENTUAL ORIENTATION FIX
           */
          if (mAccelerometerRotation == ACCELEROMETER_ROTATION_LANDSCAPE)
          {
              float tmp = x;
              x = -y;
              y = +tmp;
          }
          else if (mAccelerometerRotation == ACCELEROMETER_ROTATION_PORTRAIT)
          {
              float tmp = x;
              x = +y;
              y = -tmp;
          }

          /*
           * FOR CONSISTENCY WITH iOS
           */
          x /= -GRAVITY_EARTH;
          y /= +GRAVITY_EARTH;
          z /= +GRAVITY_EARTH;

          accelerated(x, y, z);
      }
    }
}

void CinderDelegate::accelerated(float x, float y, float z)
{
    Vec3f acceleration(x, y, z);
    Vec3f filtered = mLastAccel * (1 - mAccelFilterFactor) + acceleration * mAccelFilterFactor;

    AccelEvent event(filtered, acceleration, mLastAccel, mLastRawAccel);
    sketch->accelerated(event);

    mLastAccel = filtered;
    mLastRawAccel = acceleration;
}

void CinderDelegate::setup(int width, int height, int accelerometerRotation)
{
    mWidth = width;
    mHeight = height;
    mAccelerometerRotation = accelerometerRotation;

    sketch->setup(false);
    sketch->resize();
}

void CinderDelegate::shutdown()
{
    ASensorManager_destroyEventQueue(mSensorManager, mSensorEventQueue);

    sketch->shutdown();
    delete sketch;
}

void CinderDelegate::draw()
{
    /*
     * WOULD BE BETTER TO USE A CALL-BACK, BUT IT'S NOT WORKING
     */
    processSensorEvents();

    sketch->run(); // NECESSARY FOR THE "MESSAGE-PUMP"
    sketch->update();
    sketch->draw();
    mFrameCount++;
}

void CinderDelegate::event(int id)
{
    switch (id)
    {
        case EVENT_ATTACHED:
        case EVENT_SHOWN:
            mFrameCount = 0;
            mTimer.start();
            sketch->start(CinderSketch::FLAG_FOCUS_GAINED);
            break;
            
        case EVENT_RESUMED:
            mFrameCount = 0;
            mTimer.start();
            
            /*
             * ASSERTIONS: THE GL CONTEXT HAS JUST BEEN RE-CREATED, WITH THE SAME DIMENSIONS AS BEFORE
             */
            sketch->setup(true);
            
            sketch->start(CinderSketch::FLAG_APP_RESUME);
            break;
            
        case EVENT_DETACHED:
        case EVENT_HIDDEN:
            mTimer.stop();
            sketch->stop(CinderSketch::FLAG_FOCUS_LOST);
            break;
            
        case EVENT_PAUSED:
            mTimer.stop();
            sketch->stop(CinderSketch::FLAG_APP_PAUSE);
            break;

{'fraction_non_alphanumeric': 0.10704937268706562, 'fraction_numerical': 0.002888347323765683, 'mean_word_length': 2.8727717581265293, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 68, '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': '5893472', 'n_tokens_mistral': 3539, 'n_tokens_neox': 3459, 'n_words': 621}