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 1 commit
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 @@ -53,7 +53,7 @@ protected void onCreate(Bundle savedInstanceState) {
getIntent().putExtra(GameActivity.META_DATA_LIB_NAME, libName);
super.onCreate(savedInstanceState);
initView();
mCocosEngine.init();
mCocosEngine.initWithView(findViewById(contentViewId));

setImmersiveMode();
Utils.hideVirtualButton();
Expand Down Expand Up @@ -87,7 +87,7 @@ private void setImmersiveMode() {

//Deprecated, for compatibility, keep this interface for now
protected void initView() {
mCocosEngine.initView(findViewById(contentViewId));
//
}

public SurfaceView getSurfaceView() {
Expand Down Expand Up @@ -128,7 +128,7 @@ protected void onStart() {
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mCocosEngine.setAudioFocus(hasFocus);
mCocosEngine.getAudio().setFocus(hasFocus);
}

private String getLibraryName() {
Expand Down
103 changes: 61 additions & 42 deletions native/cocos/platform/android/java/src/com/cocos/lib/CocosEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import java.util.List;


public class CocosEngine {
class CocosEngine {
private static WeakReference<CocosEngine> mRefCocosEngine;

private Audio mAudio;
private Context mContext;

private CocosSensorHandler mSensorHandler;
Expand All @@ -30,15 +32,30 @@ public class CocosEngine {

private native void initEnvNative(Context context);

public CocosEngine(Context context, String libName) {
static class Audio {
private WeakReference<Context> mRefContext;
Audio(Context context) {
mRefContext = new WeakReference<>(context);
}

void setFocus(boolean hasFocus) {
Context context = mRefContext.get();
if (context != null && hasFocus && CocosAudioFocusManager.isAudioFocusLoss()) {
CocosAudioFocusManager.registerAudioFocusListener(context);
}
}
}
minggo marked this conversation as resolved.
Show resolved Hide resolved

CocosEngine(Context context, String libName) {
mRefCocosEngine = new WeakReference<>(this);
mContext = context;
mAudio = new Audio(context);
mHandler = new Handler(context.getMainLooper());
System.loadLibrary(libName);
initEnvNative(context);
}

public void destroy() {
void destroy() {
mRefCocosEngine.clear();
CocosHelper.unregisterBatteryLevelReceiver(mContext);
CocosAudioFocusManager.unregisterAudioFocusListener(mContext);
Expand All @@ -47,27 +64,11 @@ public void destroy() {
mContext = null;
}

public void init() {
Activity activity = null;
if (mContext instanceof Activity) {
activity = (Activity) mContext;
}

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

CocosHelper.registerBatteryLevelReceiver(mContext);
CocosHelper.init();
CocosAudioFocusManager.registerAudioFocusListener(mContext);
CanvasRenderingContext2DImpl.init(mContext);
if (activity != null) {
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
}

mSensorHandler = new CocosSensorHandler(mContext);
void init() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will init be invoked?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be init at construct, but Actvity should be called after super.oncreate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and call initWithView instead

initWithView(null);
}

public void start() {
void start() {
mSurfaceView.setVisibility(View.VISIBLE);
if (null != mSurfaceViewArray) {
for (CocosSurfaceView surfaceView : mSurfaceViewArray) {
Expand All @@ -76,7 +77,7 @@ public void start() {
}
}

public void stop() {
void stop() {
mSurfaceView.setVisibility(View.INVISIBLE);
if (null != mSurfaceViewArray) {
for (CocosSurfaceView surfaceView : mSurfaceViewArray) {
Expand All @@ -85,44 +86,62 @@ public void stop() {
}
}

public void pause() {
void pause() {
mSensorHandler.onPause();
}

public void resume() {
void resume() {
mSensorHandler.onResume();
Utils.hideVirtualButton();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utils.hideVirtualButton(); is invoked in CocosActivity.onResume, why do you have to invoke it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to remove, it should be app's behavior

if (CocosAudioFocusManager.isAudioFocusLoss()) {
CocosAudioFocusManager.registerAudioFocusListener(mContext);
}
}

public SurfaceView getRenderView() {
SurfaceView getRenderView() {
return mSurfaceView;
}

public void setAudioFocus(boolean hasFocus) {
if (hasFocus && CocosAudioFocusManager.isAudioFocusLoss()) {
CocosAudioFocusManager.registerAudioFocusListener(mContext);
}
Audio getAudio() {
return mAudio;
}

void initView(FrameLayout parentView) {
mRootLayout = parentView;
CocosActivity cocosActivity = null;
if (mContext instanceof CocosActivity) {
cocosActivity = (CocosActivity) mContext;
mSurfaceView = cocosActivity.getSurfaceView();
} else {
// todo: create surfaceView
void initWithView(FrameLayout parentView) {
Activity activity = null;
if (mContext instanceof Activity) {
activity = (Activity) mContext;
}

if (mWebViewHelper == null) {
mWebViewHelper = new CocosWebViewHelper(mRootLayout);
// GlobalObject.init should be initialized at first.
GlobalObject.init(mContext, activity);

CocosHelper.registerBatteryLevelReceiver(mContext);
CocosHelper.init();
CocosAudioFocusManager.registerAudioFocusListener(mContext);
CanvasRenderingContext2DImpl.init(mContext);
if (activity != null) {
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
}

if (mVideoHelper == null) {
mVideoHelper = new CocosVideoHelper(cocosActivity, mRootLayout);
mSensorHandler = new CocosSensorHandler(mContext);

if (parentView != null) {
mRootLayout = parentView;
CocosActivity cocosActivity = null;
if (mContext instanceof CocosActivity) {
cocosActivity = (CocosActivity) mContext;
mSurfaceView = cocosActivity.getSurfaceView();
} else {
// todo: create surfaceView
}

if (mWebViewHelper == null) {
mWebViewHelper = new CocosWebViewHelper(mRootLayout);
}

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

Expand Down