Skip to content
Merged
Changes from all 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 @@ -25,6 +25,7 @@ of this software and associated documentation files (the "Software"), to deal
package org.cocos2dx.lib;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
Expand All @@ -35,6 +36,7 @@ of this software and associated documentation files (the "Software"), to deal
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.os.PowerManager;
import android.preference.PreferenceManager.OnActivityResultListener;
import android.util.Log;
import android.view.View;
Expand Down Expand Up @@ -202,7 +204,11 @@ public void onWindowFocusChanged(boolean hasFocus) {
}

private void resumeIfHasFocus() {
if(hasFocus) {
//It is possible for the app to receive the onWindowsFocusChanged(true) event
//even though it is locked or asleep
boolean readyToPlay = !isDeviceLocked() && !isDeviceAsleep();

if(hasFocus && readyToPlay) {
this.hideVirtualButton();
Cocos2dxHelper.onResume();
mGLSurfaceView.onResume();
Expand Down Expand Up @@ -358,6 +364,24 @@ private static boolean isAndroidEmulator() {
return isEmulator;
}

private static boolean isDeviceLocked() {
KeyguardManager keyguardManager = (KeyguardManager)getContext().getSystemService(Context.KEYGUARD_SERVICE);
boolean locked = keyguardManager.inKeyguardRestrictedInputMode();
return locked;
}

private static boolean isDeviceAsleep() {
PowerManager powerManager = (PowerManager)getContext().getSystemService(Context.POWER_SERVICE);
if(powerManager == null) {
return false;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
return !powerManager.isInteractive();
} else {
return !powerManager.isScreenOn();
}
}

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
Expand Down