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

3.4.1 Android: Java flush game thread tasks #4193

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
108 changes: 63 additions & 45 deletions cocos/platform/android/java/src/com/cocos/lib/CocosHelper.java
@@ -1,27 +1,27 @@
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.

http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.

http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package com.cocos.lib;

Expand Down Expand Up @@ -77,7 +77,7 @@ public class CocosHelper {
private static BatteryReceiver sBatteryReceiver = new BatteryReceiver();

public static final int NETWORK_TYPE_NONE = 0;
public static final int NETWORK_TYPE_LAN = 1;
public static final int NETWORK_TYPE_LAN = 1;
public static final int NETWORK_TYPE_WWAN = 2;

// The absolute path to the OBB if it exists.
Expand All @@ -86,6 +86,7 @@ public class CocosHelper {
// The OBB file
private static ZipResourceFile sOBBFile = null;

private static Object sTaskMtx = new Object();
private static List<Runnable> sTaskOnGameThread = Collections.synchronizedList(new ArrayList<>());

/**
Expand Down Expand Up @@ -120,13 +121,19 @@ static void unregisterBatteryLevelReceiver(Context context) {
}

public static void runOnGameThread(final Runnable runnable) {
sTaskOnGameThread.add(runnable);
synchronized (sTaskMtx) {
sTaskOnGameThread.add(runnable);
}
}

static void flushTasksOnGameThread() {
while(sTaskOnGameThread.size() > 0) {
Runnable r = sTaskOnGameThread.remove(0);
if(r != null) {
List<Runnable> tmp = sTaskOnGameThread;
synchronized (sTaskMtx) {
sTaskOnGameThread = Collections.synchronizedList(new ArrayList<>());
}
while (tmp.size() > 0) {
Runnable r = tmp.remove(0);
if (r != null) {
Comment on lines +130 to +136
Copy link
Contributor Author

Choose a reason for hiding this comment

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

同上

r.run();
}
}
Expand Down Expand Up @@ -159,31 +166,42 @@ public static int getNetworkType() {
// ===========================================================

private static boolean sInited = false;

public static void init(final Activity activity) {
sActivity = activity;
if (!sInited) {
CocosHelper.sVibrateService = (Vibrator)activity.getSystemService(Context.VIBRATOR_SERVICE);
CocosHelper.sVibrateService = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
CocosHelper.initObbFilePath();
CocosHelper.initializeOBBFile();

sInited = true;
}
}

public static float getBatteryLevel() { return sBatteryReceiver.sBatteryLevel; }
public static String getObbFilePath() { return CocosHelper.sObbFilePath; }
public static float getBatteryLevel() {
return sBatteryReceiver.sBatteryLevel;
}

public static String getObbFilePath() {
return CocosHelper.sObbFilePath;
}

public static String getWritablePath() {
return sActivity.getFilesDir().getAbsolutePath();
}

public static String getCurrentLanguage() {
return Locale.getDefault().getLanguage();
}

public static String getCurrentLanguageCode() {
return Locale.getDefault().toString();
}
public static String getDeviceModel(){

public static String getDeviceModel() {
return Build.MODEL;
}

public static String getSystemVersion() {
return Build.VERSION.RELEASE;
}
Expand All @@ -193,18 +211,18 @@ public static void vibrate(float duration) {
if (sVibrateService != null && sVibrateService.hasVibrator()) {
if (android.os.Build.VERSION.SDK_INT >= 26) {
Class<?> vibrationEffectClass = Class.forName("android.os.VibrationEffect");
if(vibrationEffectClass != null) {
if (vibrationEffectClass != null) {
final int DEFAULT_AMPLITUDE = CocosReflectionHelper.<Integer>getConstantValue(vibrationEffectClass,
"DEFAULT_AMPLITUDE");
//VibrationEffect.createOneShot(long milliseconds, int amplitude)
final Method method = vibrationEffectClass.getMethod("createOneShot",
new Class[]{Long.TYPE, Integer.TYPE});
Class<?> type = method.getReturnType();

Object effect = method.invoke(vibrationEffectClass,
Object effect = method.invoke(vibrationEffectClass,
new Object[]{(long) (duration * 1000), DEFAULT_AMPLITUDE});
//sVibrateService.vibrate(VibrationEffect effect);
CocosReflectionHelper.invokeInstanceMethod(sVibrateService,"vibrate",
CocosReflectionHelper.invokeInstanceMethod(sVibrateService, "vibrate",
new Class[]{type}, new Object[]{(effect)});
}
} else {
Expand All @@ -228,11 +246,11 @@ public static boolean openURL(String url) {
return ret;
}

public static void copyTextToClipboard(final String text){
public static void copyTextToClipboard(final String text) {
sActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
ClipboardManager myClipboard = (ClipboardManager)sActivity.getSystemService(Context.CLIPBOARD_SERVICE);
ClipboardManager myClipboard = (ClipboardManager) sActivity.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);
}
Expand All @@ -246,8 +264,8 @@ public static long[] getObbAssetFileDescriptor(final String path) {
if (descriptor != null) {
try {
ParcelFileDescriptor parcel = descriptor.getParcelFileDescriptor();
Method method = parcel.getClass().getMethod("getFd", new Class[] {});
array[0] = (Integer)method.invoke(parcel);
Method method = parcel.getClass().getMethod("getFd", new Class[]{});
array[0] = (Integer) method.invoke(parcel);
array[1] = descriptor.getStartOffset();
array[2] = descriptor.getLength();
} catch (NoSuchMethodException e) {
Expand Down Expand Up @@ -313,14 +331,14 @@ public static float[] getSafeArea() {
do {
Object windowInsectObj = GlobalObject.getActivity().getWindow().getDecorView().getRootWindowInsets();

if(windowInsectObj == null) break;
if (windowInsectObj == null) break;

Class<?> windowInsets = WindowInsets.class;
try {
Method wiGetDisplayCutout = windowInsets.getMethod("getDisplayCutout");
Object cutout = wiGetDisplayCutout.invoke(windowInsectObj);

if(cutout == null) break;
if (cutout == null) break;

Class<?> displayCutout = cutout.getClass();
Method dcGetLeft = displayCutout.getMethod("getSafeInsetLeft");
Expand All @@ -342,15 +360,15 @@ public static float[] getSafeArea() {
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}while(false);
} while (false);
}
return new float[]{0,0,0,0};
return new float[]{0, 0, 0, 0};
}

public static void setKeepScreenOn(boolean keepScreenOn) {
if(keepScreenOn) {
if (keepScreenOn) {
sActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}else {
} else {
sActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
Expand Down
Expand Up @@ -80,6 +80,7 @@ public class CocosHelper {
private static String sObbFilePath = "";


private static Object sTaskMtx = new Object();
private static List<Runnable> sTaskOnGameThread = Collections.synchronizedList(new ArrayList<>());

/**
Expand Down Expand Up @@ -131,13 +132,19 @@ static void unregisterBatteryLevelReceiver(Context context) {
}

public static void runOnGameThread(final Runnable runnable) {
sTaskOnGameThread.add(runnable);
synchronized (sTaskMtx) {
sTaskOnGameThread.add(runnable);
}
}

@SuppressWarnings("unused")
static void flushTasksOnGameThread() {
while (sTaskOnGameThread.size() > 0) {
Runnable r = sTaskOnGameThread.remove(0);
List<Runnable> tmp = sTaskOnGameThread;
synchronized (sTaskMtx) {
sTaskOnGameThread = Collections.synchronizedList(new ArrayList<>());
}
while (tmp.size() > 0) {
Runnable r = tmp.remove(0);
Comment on lines +142 to +147
Copy link
Contributor Author

Choose a reason for hiding this comment

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

避免循环中修改队列, 修复潜在无法退出的问题

runOnGameThread(()-> {
  ...
  runOnGameThread(()->{..});
...
});

if (r != null) {
r.run();
}
Expand Down