package vip.ltpp.euv.oppowatch;
import android.app.Activity;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.view.Choreographer;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
public final class EuvOppoWatchActivity extends Activity
implements SurfaceHolder.Callback, Choreographer.FrameCallback {
static {
try {
System.loadLibrary("euv_android_host");
} catch (UnsatisfiedLinkError ignored) {
}
}
private static native long nativeCreate(int width, int height, float density, boolean isWatch);
private static native void nativeDestroy(long handle);
private static native void nativeAttachSurface(long handle, Surface surface, int width, int height);
private static native void nativeDetachSurface(long handle);
private static native int nativeStepFrame(long handle, float dtSecs);
private static native int nativeNeedsFrame(long handle);
private static native void nativeTouch(long handle, int action, int pointerId, float x, float y);
private static native void nativeCrown(long handle, float delta);
private static native void nativeLifecycle(long handle, int tag);
private long nativeHandle = 0L;
private SurfaceView surfaceView;
private boolean ambientMode = false;
private boolean framePumpActive = false;
private boolean choreographerArmed = false;
private long lastFrameNanos = 0L;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
float density = Math.max(1.0f, getResources().getDisplayMetrics().density);
int w = Math.max(120, Math.round(getResources().getDisplayMetrics().widthPixels / density));
int h = Math.max(120, Math.round(getResources().getDisplayMetrics().heightPixels / density));
try {
nativeHandle = nativeCreate(w, h, density, true);
} catch (UnsatisfiedLinkError ignored) {
nativeHandle = 0L;
}
surfaceView = new SurfaceView(this);
surfaceView.getHolder().addCallback(this);
surfaceView.setFocusable(true);
surfaceView.setFocusableInTouchMode(true);
setContentView(surfaceView);
}
@Override
protected void onResume() {
super.onResume();
ambientMode = false;
if (nativeHandle != 0L) nativeLifecycle(nativeHandle, 1);
startFramePump();
}
@Override
protected void onPause() {
super.onPause();
ambientMode = true;
stopFramePump();
if (nativeHandle != 0L) nativeLifecycle(nativeHandle, 2);
}
@Override
protected void onDestroy() {
stopFramePump();
if (nativeHandle != 0L) {
nativeDestroy(nativeHandle);
nativeHandle = 0L;
}
super.onDestroy();
}
private void wakeFramePump() {
if (!framePumpActive || ambientMode || choreographerArmed) return;
choreographerArmed = true;
lastFrameNanos = 0L;
Choreographer.getInstance().postFrameCallback(this);
}
private void startFramePump() {
if (framePumpActive) {
wakeFramePump();
return;
}
framePumpActive = true;
wakeFramePump();
}
private void stopFramePump() {
framePumpActive = false;
choreographerArmed = false;
lastFrameNanos = 0L;
Choreographer.getInstance().removeFrameCallback(this);
}
@Override
public void doFrame(long frameTimeNanos) {
choreographerArmed = false;
if (!framePumpActive || ambientMode) return;
float dt = lastFrameNanos > 0L
? Math.min(0.05f, Math.max(0.002f, (frameTimeNanos - lastFrameNanos) / 1_000_000_000.0f))
: (1.0f / 60.0f);
lastFrameNanos = frameTimeNanos;
int rendered = 0;
if (nativeHandle != 0L) {
rendered = nativeStepFrame(nativeHandle, dt);
}
boolean needsNext = (rendered != 0)
|| (nativeHandle != 0L && nativeNeedsFrame(nativeHandle) != 0);
if (framePumpActive && !ambientMode && needsNext) {
choreographerArmed = true;
Choreographer.getInstance().postFrameCallback(this);
} else {
lastFrameNanos = 0L;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (nativeHandle != 0L && holder != null) {
nativeAttachSurface(nativeHandle, holder.getSurface(), surfaceView.getWidth(), surfaceView.getHeight());
wakeFramePump();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (nativeHandle != 0L && holder != null) {
nativeAttachSurface(nativeHandle, holder.getSurface(), width, height);
wakeFramePump();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (nativeHandle != 0L) nativeDetachSurface(nativeHandle);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event != null && nativeHandle != 0L) {
float density = Math.max(1.0f, getResources().getDisplayMetrics().density);
int idx = event.getActionIndex();
nativeTouch(
nativeHandle,
event.getActionMasked(),
event.getPointerId(idx),
event.getX(idx) / density,
event.getY(idx) / density
);
wakeFramePump();
return true;
}
return super.onTouchEvent(event);
}
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (event != null
&& event.getAction() == MotionEvent.ACTION_SCROLL
&& event.isFromSource(InputDevice.SOURCE_ROTARY_ENCODER)) {
float delta = -event.getAxisValue(MotionEvent.AXIS_SCROLL);
if (delta != 0.0f) {
if (nativeHandle != 0L) {
nativeCrown(nativeHandle, delta);
wakeFramePump();
}
Vibrator vib = getSystemService(Vibrator.class);
if (vib != null) {
vib.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK));
}
return true;
}
}
return super.onGenericMotionEvent(event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && nativeHandle != 0L) {
nativeLifecycle(nativeHandle, 4);
wakeFramePump();
return true;
}
return super.onKeyDown(keyCode, event);
}
}