Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor CocosActivity #16203

Merged
merged 11 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,61 +26,35 @@

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.google.androidgamesdk.GameActivity;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class CocosActivity extends GameActivity {
private static final String TAG = "CocosActivity";
private CocosWebViewHelper mWebViewHelper = null;
private CocosVideoHelper mVideoHelper = null;

private CocosSensorHandler mSensorHandler;
private List<CocosSurfaceView> mSurfaceViewArray;
private FrameLayout mRootLayout;



private native void onCreateNative();
private CocosEngine mCocosEngine;

@Override
protected void onCreate(Bundle savedInstanceState) {
onLoadNativeLibraries();
onCreateNative();
String libName = getLibraryName();
mCocosEngine = new CocosEngine(this, libName);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getIntent().putExtra(GameActivity.META_DATA_LIB_NAME, libName);
super.onCreate(savedInstanceState);

// GlobalObject.init should be initialized at first.
GlobalObject.init(this, this);

CocosHelper.registerBatteryLevelReceiver(this);
CocosHelper.init();
CocosAudioFocusManager.registerAudioFocusListener(this);
CanvasRenderingContext2DImpl.init(this);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
initView();


mSensorHandler = new CocosSensorHandler(this);
mCocosEngine.initWithView(findViewById(contentViewId));

setImmersiveMode();

Utils.hideVirtualButton();

mSurfaceView.setOnTouchListener((v, event) -> processMotionEvent(event));
Expand Down Expand Up @@ -110,100 +84,53 @@ private void setImmersiveMode() {
}
}

//Deprecated, for compatibility, keep this interface for now
protected void initView() {
mRootLayout = findViewById(contentViewId);
if (mWebViewHelper == null) {
mWebViewHelper = new CocosWebViewHelper(mRootLayout);
}

if (mVideoHelper == null) {
mVideoHelper = new CocosVideoHelper(this, mRootLayout);
}
//
}



public SurfaceView getSurfaceView() {
return this.mSurfaceView;
return mSurfaceView;
}

@Override
protected void onDestroy() {
super.onDestroy();
CocosHelper.unregisterBatteryLevelReceiver(this);
CocosAudioFocusManager.unregisterAudioFocusListener(this);
CanvasRenderingContext2DImpl.destroy();
GlobalObject.destroy();
mCocosEngine.destroy();
}

@Override
protected void onPause() {
super.onPause();
mSensorHandler.onPause();
mCocosEngine.pause();
}

@Override
protected void onResume() {
super.onResume();
mSensorHandler.onResume();
Utils.hideVirtualButton();
if (CocosAudioFocusManager.isAudioFocusLoss()) {
CocosAudioFocusManager.registerAudioFocusListener(this);
}
mCocosEngine.resume();
}

@Override
protected void onStop() {
super.onStop();
mSurfaceView.setVisibility(View.INVISIBLE);
if (null != mSurfaceViewArray) {
for (CocosSurfaceView surfaceView : mSurfaceViewArray) {
surfaceView.setVisibility(View.INVISIBLE);
}
}
mCocosEngine.stop();
}

@Override
protected void onStart() {
super.onStart();
mSurfaceView.setVisibility(View.VISIBLE);
if (null != mSurfaceViewArray) {
for (CocosSurfaceView surfaceView : mSurfaceViewArray) {
surfaceView.setVisibility(View.VISIBLE);
}
}
mCocosEngine.start();
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && CocosAudioFocusManager.isAudioFocusLoss()) {
CocosAudioFocusManager.registerAudioFocusListener(this);
}
}

// invoke from native code
@SuppressWarnings({"UnusedDeclaration"})
private void createSurface(int x, int y, int width, int height, int windowId) {
runOnUiThread(new Runnable() {
@Override
public void run() {
CocosSurfaceView view = new CocosSurfaceView(CocosActivity.this, windowId);
view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height);
params.leftMargin = x;
params.topMargin = y;
//mSubsurfaceView.setBackgroundColor(Color.BLUE);
mRootLayout.addView(view, params);
if (null == mSurfaceViewArray) {
mSurfaceViewArray = new ArrayList<>();
}
mSurfaceViewArray.add(view);
}
});
mCocosEngine.getAudio().setFocus(hasFocus);
}

private void onLoadNativeLibraries() {
private String getLibraryName() {
try {
ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);

Expand All @@ -212,11 +139,10 @@ private void onLoadNativeLibraries() {
if (TextUtils.isEmpty(libName)) {
Log.e(TAG, "can not find library, please config android.app.lib_name at AndroidManifest.xml");
}
assert libName != null;
System.loadLibrary(libName);
getIntent().putExtra(GameActivity.META_DATA_LIB_NAME, libName);
return libName;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ of this software and associated engine source code (the "Software"), a limited,
import android.os.Build;
import android.util.Log;

class CocosAudioFocusManager {
public class CocosAudio {
dumganhar marked this conversation as resolved.
Show resolved Hide resolved

private static final String _TAG = "CocosAudioFocusManager";
private static final String _TAG = "CocosAudio";
private static boolean isAudioFocusLost = true;
private Context mApplicationContext;

private final static AudioManager.OnAudioFocusChangeListener sAfChangeListener = focusChange -> {
Log.d(_TAG, "onAudioFocusChange: " + focusChange + ", thread: " + Thread.currentThread().getName());
Expand Down Expand Up @@ -65,7 +66,17 @@ class CocosAudioFocusManager {
}
};

static void registerAudioFocusListener(Context context) {
CocosAudio(Context context) {
mApplicationContext = context.getApplicationContext();
}

public void setFocus(boolean hasFocus) {
if (hasFocus && isAudioFocusLoss()) {
registerAudioFocusListener(mApplicationContext);
}
}

void registerAudioFocusListener(Context context) {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
assert am != null;
int result;
Expand Down Expand Up @@ -102,7 +113,7 @@ static void registerAudioFocusListener(Context context) {
Log.e(_TAG, "requestAudioFocus failed!");
}

static void unregisterAudioFocusListener(Context context) {
void unregisterAudioFocusListener(Context context) {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
assert am != null;
int result = am.abandonAudioFocus(sAfChangeListener);
Expand All @@ -113,7 +124,7 @@ static void unregisterAudioFocusListener(Context context) {
}
}

static boolean isAudioFocusLoss() {
boolean isAudioFocusLoss() {
minggo marked this conversation as resolved.
Show resolved Hide resolved
return isAudioFocusLost;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,14 @@ private static void showNative(String defaultValue, int maxLength, boolean isMul
GlobalObject.runOnUiThread(new Runnable() {
@Override
public void run() {
Intent i = new Intent(GlobalObject.getActivity(), CocosEditBoxActivity.class);
Intent i = new Intent(GlobalObject.getContext(), CocosEditBoxActivity.class);
i.putExtra("defaultValue", defaultValue);
i.putExtra("maxLength", maxLength);
i.putExtra("isMultiline", isMultiline);
i.putExtra("confirmHold", confirmHold);
i.putExtra("confirmType", confirmType);
i.putExtra("inputType", inputType);
GlobalObject.getActivity().startActivity(i);
GlobalObject.getContext().startActivity(i);
}
});
}
Expand Down
Loading