Commit fa47579f authored by fallenstardust's avatar fallenstardust

游戏结束杀死自己进程

修复打开多个文件,MainActivity多个实例
parent 9531b13e
...@@ -132,7 +132,7 @@ void DeckBuilder::Terminate() { ...@@ -132,7 +132,7 @@ void DeckBuilder::Terminate() {
//os::Printer::log("setLastDeck", linebuf); //os::Printer::log("setLastDeck", linebuf);
mainGame->SaveConfig(); mainGame->SaveConfig();
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
} }
bool DeckBuilder::OnEvent(const irr::SEvent& event) { bool DeckBuilder::OnEvent(const irr::SEvent& event) {
#ifdef _IRR_ANDROID_PLATFORM_ #ifdef _IRR_ANDROID_PLATFORM_
......
...@@ -679,7 +679,7 @@ void DuelClient::HandleSTOCPacketLan(char* data, unsigned int len) { ...@@ -679,7 +679,7 @@ void DuelClient::HandleSTOCPacketLan(char* data, unsigned int len) {
mainGame->SaveConfig(); mainGame->SaveConfig();
event_base_loopbreak(client_base); event_base_loopbreak(client_base);
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
break; break;
} }
case STOC_REPLAY: { case STOC_REPLAY: {
...@@ -980,7 +980,7 @@ int DuelClient::ClientAnalyze(char * msg, unsigned int len) { ...@@ -980,7 +980,7 @@ int DuelClient::ClientAnalyze(char * msg, unsigned int len) {
mainGame->gMutex.unlock(); mainGame->gMutex.unlock();
event_base_loopbreak(client_base); event_base_loopbreak(client_base);
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
} }
return false; return false;
} }
......
...@@ -145,7 +145,7 @@ bool ClientField::OnEvent(const irr::SEvent& event) { ...@@ -145,7 +145,7 @@ bool ClientField::OnEvent(const irr::SEvent& event) {
else else
mainGame->ShowElement(mainGame->wLanWindow); mainGame->ShowElement(mainGame->wLanWindow);
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
} else { } else {
mainGame->PopupElement(mainGame->wSurrender); mainGame->PopupElement(mainGame->wSurrender);
} }
......
...@@ -2017,4 +2017,8 @@ void Game::ChangeToIGUIImageButton(irr::gui::IGUIButton* button, irr::video::ITe ...@@ -2017,4 +2017,8 @@ void Game::ChangeToIGUIImageButton(irr::gui::IGUIButton* button, irr::video::ITe
button->setOverrideFont(font); button->setOverrideFont(font);
} }
void Game::OnGameClose() {
android::onGameExit(appMain);
this->device->closeDevice();
} }
}
\ No newline at end of file
...@@ -167,11 +167,12 @@ public: ...@@ -167,11 +167,12 @@ public:
void AddDebugMsg(const char* msgbuf); void AddDebugMsg(const char* msgbuf);
void ErrorLog(const char* msgbuf); void ErrorLog(const char* msgbuf);
void addMessageBox(const wchar_t* caption, const wchar_t* text); void addMessageBox(const wchar_t* caption, const wchar_t* text);
void initUtils(); void initUtils(){}
void ClearTextures(); void ClearTextures();
void CloseGameButtons(); void CloseGameButtons();
void CloseGameWindow(); void CloseGameWindow();
void CloseDuelWindow(); void CloseDuelWindow();
void OnGameClose();
void ChangeToIGUIImageWindow(irr::gui::IGUIWindow* window, irr::gui::IGUIImage* bgwindow, irr::video::ITexture* image); void ChangeToIGUIImageWindow(irr::gui::IGUIWindow* window, irr::gui::IGUIImage* bgwindow, irr::video::ITexture* image);
void ChangeToIGUIImageButton(irr::gui::IGUIButton* button, irr::video::ITexture* image, irr::video::ITexture* pressedImage, irr::gui::CGUITTFont* font=0); void ChangeToIGUIImageButton(irr::gui::IGUIButton* button, irr::video::ITexture* image, irr::video::ITexture* pressedImage, irr::gui::CGUITTFont* font=0);
......
...@@ -65,10 +65,10 @@ int main(int argc, char* argv[]) { ...@@ -65,10 +65,10 @@ int main(int argc, char* argv[]) {
bool keep_on_return = false; bool keep_on_return = false;
bool open_file = false; bool open_file = false;
#ifdef _IRR_ANDROID_PLATFORM_ #ifdef _IRR_ANDROID_PLATFORM_
__android_log_print(ANDROID_LOG_WARN, "ygo", "handle args %d", argc);
//android //android
for(int i = 0; i < argc; ++i) { for(int i = 0; i < argc; ++i) {
const char* arg = argv[i].c_str(); const char* arg = argv[i].c_str();
os::Printer::log("kkargv ", arg);
#else #else
//pc的第一个是exe的路径 //pc的第一个是exe的路径
for(int i = 1; i < argc; ++i) { for(int i = 1; i < argc; ++i) {
...@@ -78,7 +78,6 @@ int main(int argc, char* argv[]) { ...@@ -78,7 +78,6 @@ int main(int argc, char* argv[]) {
wchar_t fname[1024]; wchar_t fname[1024];
char* tmp = sub_string(arg, 2); char* tmp = sub_string(arg, 2);
BufferIO::DecodeUTF8(tmp, fname); BufferIO::DecodeUTF8(tmp, fname);
__android_log_print(ANDROID_LOG_DEBUG, "ygo", "load cdb=%s", tmp);
ygo::dataManager.LoadDB(fname); ygo::dataManager.LoadDB(fname);
delete tmp; delete tmp;
} else if(!strcmp(arg, "-k")) { // Keep on return } else if(!strcmp(arg, "-k")) { // Keep on return
...@@ -94,7 +93,7 @@ int main(int argc, char* argv[]) { ...@@ -94,7 +93,7 @@ int main(int argc, char* argv[]) {
ygo::mainGame->HideElement(ygo::mainGame->wMainMenu); ygo::mainGame->HideElement(ygo::mainGame->wMainMenu);
ClickButton(ygo::mainGame->btnJoinHost); ClickButton(ygo::mainGame->btnJoinHost);
break; break;
} else if (arg[0] == '-' && arg[1] == 'r') { // Replay } else if(!strcmp(arg, "-r")) { // Replay
exit_on_return = !keep_on_return; exit_on_return = !keep_on_return;
int index = 0; int index = 0;
if((i+1) < argc){//下一个参数是录像名 if((i+1) < argc){//下一个参数是录像名
......
...@@ -63,7 +63,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) { ...@@ -63,7 +63,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) {
case BUTTON_MODE_EXIT: { case BUTTON_MODE_EXIT: {
mainGame->soundManager->StopBGM(); mainGame->soundManager->StopBGM();
mainGame->SaveConfig(); mainGame->SaveConfig();
mainGame->device->closeDevice(); mainGame->OnGameClose();
break; break;
} }
case BUTTON_LAN_MODE: { case BUTTON_LAN_MODE: {
...@@ -121,7 +121,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) { ...@@ -121,7 +121,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) {
mainGame->HideElement(mainGame->wLanWindow); mainGame->HideElement(mainGame->wLanWindow);
mainGame->ShowElement(mainGame->wMainMenu); mainGame->ShowElement(mainGame->wMainMenu);
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
break; break;
} }
case BUTTON_LAN_REFRESH: { case BUTTON_LAN_REFRESH: {
...@@ -214,7 +214,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) { ...@@ -214,7 +214,7 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) {
mainGame->wChat->setVisible(false); mainGame->wChat->setVisible(false);
mainGame->SaveConfig(); mainGame->SaveConfig();
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
break; break;
} }
case BUTTON_REPLAY_MODE: { case BUTTON_REPLAY_MODE: {
......
...@@ -249,7 +249,7 @@ void ReplayMode::EndDuel() { ...@@ -249,7 +249,7 @@ void ReplayMode::EndDuel() {
mainGame->device->setEventReceiver(&mainGame->menuHandler); mainGame->device->setEventReceiver(&mainGame->menuHandler);
mainGame->gMutex.unlock(); mainGame->gMutex.unlock();
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
} }
} }
void ReplayMode::Restart(bool refresh) { void ReplayMode::Restart(bool refresh) {
......
...@@ -154,7 +154,7 @@ int SingleMode::SinglePlayThread() { ...@@ -154,7 +154,7 @@ int SingleMode::SinglePlayThread() {
mainGame->gMutex.unlock(); mainGame->gMutex.unlock();
mainGame->SaveConfig(); mainGame->SaveConfig();
if(exit_on_return) if(exit_on_return)
mainGame->device->closeDevice(); mainGame->OnGameClose();
} }
return 0; return 0;
} }
......
...@@ -887,6 +887,20 @@ void process_input(ANDROID_APP app, ...@@ -887,6 +887,20 @@ void process_input(ANDROID_APP app,
} }
} }
void onGameExit(ANDROID_APP app){
if (!app || !app->activity || !app->activity->vm)
return;
JNIEnv* jni = nullptr;
app->activity->vm->AttachCurrentThread(&jni, NULL);
jobject lNativeActivity = app->activity->clazz;
jclass ClassNativeActivity = jni->GetObjectClass(lNativeActivity);
jmethodID methodId = jni->GetMethodID(ClassNativeActivity,
"onGameExit", "()V");
jni->CallVoidMethod(lNativeActivity, methodId);
jni->DeleteLocalRef(ClassNativeActivity);
app->activity->vm->DetachCurrentThread();
}
s32 handleInput(ANDROID_APP app, AInputEvent* androidEvent) { s32 handleInput(ANDROID_APP app, AInputEvent* androidEvent) {
IrrlichtDevice* device = (IrrlichtDevice*) app->userData; IrrlichtDevice* device = (IrrlichtDevice*) app->userData;
s32 Status = 0; s32 Status = 0;
......
...@@ -193,6 +193,8 @@ extern s32 handleInput(ANDROID_APP app, AInputEvent* androidEvent); ...@@ -193,6 +193,8 @@ extern s32 handleInput(ANDROID_APP app, AInputEvent* androidEvent);
extern bool android_deck_delete(const char* deck_name); extern bool android_deck_delete(const char* deck_name);
extern void onGameExit(ANDROID_APP app);
extern void runWindbot(ANDROID_APP app, const char* args); extern void runWindbot(ANDROID_APP app, const char* args);
} }
......
APP_ABI := arm64-v8a armeabi-v7a x86 APP_ABI := arm64-v8a #armeabi-v7a x86
APP_PLATFORM := android-21 APP_PLATFORM := android-21
#APP_MODULES := YGOMobile #APP_MODULES := YGOMobile
#NDK_TOOLCHAIN_VERSION=4.8 #NDK_TOOLCHAIN_VERSION=4.8
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
android:value="YGOMobile"/> android:value="YGOMobile"/>
</activity> </activity>
<receiver android:name="cn.garymb.ygomobile.GameReceiver" <receiver android:name="cn.garymb.ygomobile.GameReceiver"
android:process=":game" > android:process=":gamehelper" >
<intent-filter> <intent-filter>
<action android:name="cn.garymb.ygomobile.game.start"/> <action android:name="cn.garymb.ygomobile.game.start"/>
<action android:name="cn.garymb.ygomobile.game.stop"/> <action android:name="cn.garymb.ygomobile.game.stop"/>
......
...@@ -17,6 +17,7 @@ import android.os.Build; ...@@ -17,6 +17,7 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.Process;
import android.util.Log; import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.HapticFeedbackConstants; import android.view.HapticFeedbackConstants;
...@@ -60,9 +61,6 @@ public class YGOMobileActivity extends NativeActivity implements ...@@ -60,9 +61,6 @@ public class YGOMobileActivity extends NativeActivity implements
private static final int CHAIN_CONTROL_PANEL_X_POSITION_LEFT_EDGE = 205; private static final int CHAIN_CONTROL_PANEL_X_POSITION_LEFT_EDGE = 205;
private static final int CHAIN_CONTROL_PANEL_Y_REVERT_POSITION = 100; private static final int CHAIN_CONTROL_PANEL_Y_REVERT_POSITION = 100;
private static final int MAX_REFRESH = 30 * 1000; private static final int MAX_REFRESH = 30 * 1000;
private static int sChainControlXPostion = -1;
private static int sChainControlYPostion = -1;
private static boolean USE_SURFACE = true;
protected final int windowsFlags = protected final int windowsFlags =
Build.VERSION.SDK_INT >= 19 ? ( Build.VERSION.SDK_INT >= 19 ? (
View.SYSTEM_UI_FLAG_LAYOUT_STABLE View.SYSTEM_UI_FLAG_LAYOUT_STABLE
...@@ -71,6 +69,7 @@ public class YGOMobileActivity extends NativeActivity implements ...@@ -71,6 +69,7 @@ public class YGOMobileActivity extends NativeActivity implements
| View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) : | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) :
View.SYSTEM_UI_FLAG_LOW_PROFILE; View.SYSTEM_UI_FLAG_LOW_PROFILE;
protected View mContentView; protected View mContentView;
protected ComboBoxCompat mGlobalComboBox; protected ComboBoxCompat mGlobalComboBox;
protected EditWindowCompat mGlobalEditText; protected EditWindowCompat mGlobalEditText;
...@@ -80,6 +79,8 @@ public class YGOMobileActivity extends NativeActivity implements ...@@ -80,6 +79,8 @@ public class YGOMobileActivity extends NativeActivity implements
private NetworkController mNetController; private NetworkController mNetController;
private volatile boolean mOverlayShowRequest = false; private volatile boolean mOverlayShowRequest = false;
private volatile int mCompatGUIMode; private volatile int mCompatGUIMode;
private static int sChainControlXPostion = -1;
private static int sChainControlYPostion = -1;
private GameApplication mApp; private GameApplication mApp;
private Handler handler = new Handler(); private Handler handler = new Handler();
private FullScreenUtils mFullScreenUtils; private FullScreenUtils mFullScreenUtils;
...@@ -87,12 +88,10 @@ public class YGOMobileActivity extends NativeActivity implements ...@@ -87,12 +88,10 @@ public class YGOMobileActivity extends NativeActivity implements
private FrameLayout mLayout; private FrameLayout mLayout;
private SurfaceView mSurfaceView; private SurfaceView mSurfaceView;
private boolean replaced = false; private boolean replaced = false;
private static boolean USE_SURFACE = true;
private String[] mArgV; private String[] mArgV;
// public static int notchHeight; // public static int notchHeight;
//电池管理
private PowerManager mPM;
private PowerManager.WakeLock mLock;
private GameApplication app() { private GameApplication app() {
if (mApp == null) { if (mApp == null) {
...@@ -138,6 +137,10 @@ public class YGOMobileActivity extends NativeActivity implements ...@@ -138,6 +137,10 @@ public class YGOMobileActivity extends NativeActivity implements
.setPackage(getPackageName())); .setPackage(getPackageName()));
} }
//电池管理
private PowerManager mPM;
private PowerManager.WakeLock mLock;
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
...@@ -201,7 +204,6 @@ public class YGOMobileActivity extends NativeActivity implements ...@@ -201,7 +204,6 @@ public class YGOMobileActivity extends NativeActivity implements
} }
private void handleExternalCommand(Intent intent) { private void handleExternalCommand(Intent intent) {
//
YGOGameOptions options = intent YGOGameOptions options = intent
.getParcelableExtra(YGOGameOptions.YGO_GAME_OPTIONS_BUNDLE_KEY); .getParcelableExtra(YGOGameOptions.YGO_GAME_OPTIONS_BUNDLE_KEY);
long time = intent.getLongExtra(YGOGameOptions.YGO_GAME_OPTIONS_BUNDLE_TIME, 0); long time = intent.getLongExtra(YGOGameOptions.YGO_GAME_OPTIONS_BUNDLE_TIME, 0);
...@@ -543,4 +545,10 @@ public class YGOMobileActivity extends NativeActivity implements ...@@ -543,4 +545,10 @@ public class YGOMobileActivity extends NativeActivity implements
} }
}); });
} }
@Override
public void onGameExit() {
Log.e("ygomobile", "game exit");
Process.killProcess(Process.myPid());
}
} }
...@@ -195,10 +195,10 @@ public final class IrrlichtBridge { ...@@ -195,10 +195,10 @@ public final class IrrlichtBridge {
void toggleIME(String hint, boolean isShow); void toggleIME(String hint, boolean isShow);
void shareFile(String title, String path);
void showComboBoxCompat(String[] items, boolean isShow, int mode); void showComboBoxCompat(String[] items, boolean isShow, int mode);
void shareFile(String title, String path);
void performHapticFeedback(); void performHapticFeedback();
/** /**
...@@ -213,5 +213,7 @@ public final class IrrlichtBridge { ...@@ -213,5 +213,7 @@ public final class IrrlichtBridge {
int getPositionX(); int getPositionX();
int getPositionY(); int getPositionY();
void onGameExit();
} }
} }
...@@ -34,6 +34,9 @@ android { ...@@ -34,6 +34,9 @@ android {
} }
} }
buildTypes { buildTypes {
debug {
debuggable false
}
release { release {
shrinkResources false shrinkResources false
minifyEnabled false minifyEnabled false
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
android:excludeFromRecents="false" android:excludeFromRecents="false"
android:exported="true" android:exported="true"
android:screenOrientation="portrait" android:screenOrientation="portrait"
android:launchMode="singleTop"
android:stateNotNeeded="true" android:stateNotNeeded="true"
android:theme="@style/AppTheme" android:theme="@style/AppTheme"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize"> android:windowSoftInputMode="stateAlwaysHidden|adjustResize">
......
...@@ -31,13 +31,13 @@ import static cn.garymb.ygomobile.Constants.QUERY_NAME; ...@@ -31,13 +31,13 @@ import static cn.garymb.ygomobile.Constants.QUERY_NAME;
public class GameUriManager { public class GameUriManager {
private Activity activity; private Activity activity;
private String[] fname;
public GameUriManager(Activity activity) { public GameUriManager(Activity activity) {
this.activity = activity; this.activity = activity;
} }
public boolean doIntent(Intent intent) { public boolean doIntent(Intent intent) {
Log.i("ygo", "doIntent");
if (ACTION_OPEN_DECK.equals(intent.getAction())) { if (ACTION_OPEN_DECK.equals(intent.getAction())) {
if (intent.getData() != null) { if (intent.getData() != null) {
doUri(intent.getData()); doUri(intent.getData());
...@@ -58,7 +58,7 @@ public class GameUriManager { ...@@ -58,7 +58,7 @@ public class GameUriManager {
options.mUserName = intent.getStringExtra(Constants.QUERY_USER); options.mUserName = intent.getStringExtra(Constants.QUERY_USER);
options.mPort = intent.getIntExtra(Constants.QUERY_PORT, 0); options.mPort = intent.getIntExtra(Constants.QUERY_PORT, 0);
options.mRoomName = intent.getStringExtra(Constants.QUERY_ROOM); options.mRoomName = intent.getStringExtra(Constants.QUERY_ROOM);
YGOStarter.startGame(getActivity(), options, null); YGOStarter.startGame(getActivity(), options);
} catch (Exception e) { } catch (Exception e) {
Toast.makeText(getActivity(), R.string.start_game_error, Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), R.string.start_game_error, Toast.LENGTH_SHORT).show();
activity.finish(); activity.finish();
...@@ -120,6 +120,7 @@ public class GameUriManager { ...@@ -120,6 +120,7 @@ public class GameUriManager {
private void doUri(Uri uri) { private void doUri(Uri uri) {
Intent startSeting = new Intent(activity, SettingsActivity.class); Intent startSeting = new Intent(activity, SettingsActivity.class);
Log.i("ygo", "doUri:"+uri);
if ("file".equals(uri.getScheme())) { if ("file".equals(uri.getScheme())) {
File file = new File(uri.getPath()); File file = new File(uri.getPath());
if (file.getName().toLowerCase(Locale.US).endsWith(".ydk")) { if (file.getName().toLowerCase(Locale.US).endsWith(".ydk")) {
...@@ -155,8 +156,7 @@ public class GameUriManager { ...@@ -155,8 +156,7 @@ public class GameUriManager {
} else if (file.getName().toLowerCase(Locale.US).endsWith(".yrp")) { } else if (file.getName().toLowerCase(Locale.US).endsWith(".yrp")) {
File yrp = new File(AppsSettings.get().getResourcePath() + "/" + CORE_REPLAY_PATH + "/" + file.getName()); File yrp = new File(AppsSettings.get().getResourcePath() + "/" + CORE_REPLAY_PATH + "/" + file.getName());
if (yrp.exists()) { if (yrp.exists()) {
fname = new String[]{"-r", yrp.getName()}; YGOStarter.startGame(getActivity(), null, "-r ", yrp.getName());
YGOStarter.startGame(getActivity(), null, fname);
Toast.makeText(activity, activity.getString(R.string.file_exist), Toast.LENGTH_LONG).show(); Toast.makeText(activity, activity.getString(R.string.file_exist), Toast.LENGTH_LONG).show();
} else { } else {
try { try {
...@@ -164,17 +164,15 @@ public class GameUriManager { ...@@ -164,17 +164,15 @@ public class GameUriManager {
} catch (Throwable e) { } catch (Throwable e) {
Toast.makeText(activity, activity.getString(R.string.install_failed_bcos) + e, Toast.LENGTH_LONG).show(); Toast.makeText(activity, activity.getString(R.string.install_failed_bcos) + e, Toast.LENGTH_LONG).show();
} }
if (!ComponentUtils.isActivityRunning(getActivity(), new ComponentName(getActivity(), YGOMobileActivity.class))) { if (!YGOStarter.isGameRunning(getActivity())) {
fname = new String[]{"-r", yrp.getName()}; YGOStarter.startGame(getActivity(), null, "-r ", yrp.getName());
YGOStarter.startGame(getActivity(), null, fname);
Toast.makeText(activity, activity.getString(R.string.file_installed), Toast.LENGTH_LONG).show(); Toast.makeText(activity, activity.getString(R.string.file_installed), Toast.LENGTH_LONG).show();
} }
} }
} else if (file.getName().toLowerCase(Locale.US).endsWith(".lua")) { } else if (file.getName().toLowerCase(Locale.US).endsWith(".lua")) {
File single = new File(AppsSettings.get().getResourcePath() + "/" + CORE_SINGLE_PATH + "/" + file.getName()); File single = new File(AppsSettings.get().getResourcePath() + "/" + CORE_SINGLE_PATH + "/" + file.getName());
if (single.exists()) { if (single.exists()) {
fname = new String[]{"-s", single.getName()}; YGOStarter.startGame(activity, null, "-s", single.getName());
YGOStarter.startGame(activity, null, fname);
Toast.makeText(activity, activity.getString(R.string.file_exist), Toast.LENGTH_LONG).show(); Toast.makeText(activity, activity.getString(R.string.file_exist), Toast.LENGTH_LONG).show();
} else { } else {
try { try {
...@@ -182,9 +180,8 @@ public class GameUriManager { ...@@ -182,9 +180,8 @@ public class GameUriManager {
} catch (Throwable e) { } catch (Throwable e) {
Toast.makeText(activity, activity.getString(R.string.install_failed_bcos) + e, Toast.LENGTH_LONG).show(); Toast.makeText(activity, activity.getString(R.string.install_failed_bcos) + e, Toast.LENGTH_LONG).show();
} }
if (!ComponentUtils.isActivityRunning(getActivity(), new ComponentName(getActivity(), YGOMobileActivity.class))) { if (!YGOStarter.isGameRunning(getActivity())) {
fname = new String[]{"-s", single.getName()}; YGOStarter.startGame(activity, null, "-s", single.getName());
YGOStarter.startGame(activity, null, fname);
Toast.makeText(activity, activity.getString(R.string.file_installed), Toast.LENGTH_LONG).show(); Toast.makeText(activity, activity.getString(R.string.file_installed), Toast.LENGTH_LONG).show();
} }
} }
...@@ -247,8 +244,7 @@ public class GameUriManager { ...@@ -247,8 +244,7 @@ public class GameUriManager {
try { try {
ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "r"); ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "r");
if (yrp.exists()) { if (yrp.exists()) {
fname = new String[]{"-r", yrp.getName()}; YGOStarter.startGame(getActivity(), null, "-r ", yrp.getName());
YGOStarter.startGame(getActivity(), null, fname);
Toast.makeText(activity, activity.getString(R.string.file_exist), Toast.LENGTH_SHORT).show(); Toast.makeText(activity, activity.getString(R.string.file_exist), Toast.LENGTH_SHORT).show();
} else { } else {
if (pfd == null) { if (pfd == null) {
...@@ -266,9 +262,8 @@ public class GameUriManager { ...@@ -266,9 +262,8 @@ public class GameUriManager {
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }
if (!ComponentUtils.isActivityRunning(activity, new ComponentName(activity, YGOMobileActivity.class))) { if (!YGOStarter.isGameRunning(getActivity())) {
fname = new String[]{"-r", yrp.getName()}; YGOStarter.startGame(activity, null, "-r ", yrp.getName());
YGOStarter.startGame(activity, null, fname);
Toast.makeText(activity, activity.getString(R.string.file_installed), Toast.LENGTH_LONG).show(); Toast.makeText(activity, activity.getString(R.string.file_installed), Toast.LENGTH_LONG).show();
} }
} else if (urifile.getName().toLowerCase(Locale.US).endsWith(".lua")) { } else if (urifile.getName().toLowerCase(Locale.US).endsWith(".lua")) {
...@@ -276,8 +271,7 @@ public class GameUriManager { ...@@ -276,8 +271,7 @@ public class GameUriManager {
try { try {
ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "r"); ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "r");
if (single.exists()) { if (single.exists()) {
fname = new String[]{"-s", single.getName()}; YGOStarter.startGame(getActivity(), null, "-s", single.getName());
YGOStarter.startGame(getActivity(), null, fname);
Toast.makeText(activity, activity.getString(R.string.file_exist), Toast.LENGTH_SHORT).show(); Toast.makeText(activity, activity.getString(R.string.file_exist), Toast.LENGTH_SHORT).show();
} else { } else {
if (pfd == null) { if (pfd == null) {
...@@ -295,9 +289,8 @@ public class GameUriManager { ...@@ -295,9 +289,8 @@ public class GameUriManager {
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }
if (!ComponentUtils.isActivityRunning(activity, new ComponentName(activity, YGOMobileActivity.class))) { if (!YGOStarter.isGameRunning(getActivity())) {
fname = new String[]{"-s", single.getName()}; YGOStarter.startGame(activity, null, "-s", single.getName());
YGOStarter.startGame(activity, null, fname);
Toast.makeText(activity, activity.getString(R.string.file_installed), Toast.LENGTH_LONG).show(); Toast.makeText(activity, activity.getString(R.string.file_installed), Toast.LENGTH_LONG).show();
} }
} }
......
...@@ -3,6 +3,8 @@ package cn.garymb.ygomobile; ...@@ -3,6 +3,8 @@ package cn.garymb.ygomobile;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.graphics.Bitmap; import android.graphics.Bitmap;
...@@ -19,6 +21,7 @@ import com.bumptech.glide.Glide; ...@@ -19,6 +21,7 @@ import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.ViewTarget; import com.bumptech.glide.request.target.ViewTarget;
import com.bumptech.glide.signature.StringSignature; import com.bumptech.glide.signature.StringSignature;
import com.tencent.bugly.proguard.C;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
...@@ -27,6 +30,7 @@ import cn.garymb.ygodata.YGOGameOptions; ...@@ -27,6 +30,7 @@ import cn.garymb.ygodata.YGOGameOptions;
import cn.garymb.ygomobile.core.IrrlichtBridge; import cn.garymb.ygomobile.core.IrrlichtBridge;
import cn.garymb.ygomobile.lite.R; import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.ui.plus.ViewTargetPlus; import cn.garymb.ygomobile.ui.plus.ViewTargetPlus;
import cn.garymb.ygomobile.utils.ComponentUtils;
public class YGOStarter { public class YGOStarter {
...@@ -152,12 +156,13 @@ public class YGOStarter { ...@@ -152,12 +156,13 @@ public class YGOStarter {
private static long lasttime = 0; private static long lasttime = 0;
/** /**
*
* @param activity * @param activity
* @param options * @param options
* @param args 例如(播放完退出游戏):-r 1111.yrp * @param args 例如(播放完退出游戏):-r 1111.yrp
* 或者(播放完不退出游戏):-k -r 1111.yrp * 或者(播放完不退出游戏):-k -r 1111.yrp
*/ */
public static void startGame(Activity activity, YGOGameOptions options, String[] args) { public static void startGame(Activity activity, YGOGameOptions options, String... args) {
//如果距离上次加入游戏的时间大于1秒才处理 //如果距离上次加入游戏的时间大于1秒才处理
if (System.currentTimeMillis() - lasttime >= 1000) { if (System.currentTimeMillis() - lasttime >= 1000) {
lasttime = System.currentTimeMillis(); lasttime = System.currentTimeMillis();
...@@ -171,9 +176,8 @@ public class YGOStarter { ...@@ -171,9 +176,8 @@ public class YGOStarter {
intent.putExtra(YGOGameOptions.YGO_GAME_OPTIONS_BUNDLE_KEY, options); intent.putExtra(YGOGameOptions.YGO_GAME_OPTIONS_BUNDLE_KEY, options);
intent.putExtra(YGOGameOptions.YGO_GAME_OPTIONS_BUNDLE_TIME, System.currentTimeMillis()); intent.putExtra(YGOGameOptions.YGO_GAME_OPTIONS_BUNDLE_TIME, System.currentTimeMillis());
} }
if (args != null) { if(args != null) {
Log.i("kk arg", args[0]+""+args[1]); intent.putExtra(IrrlichtBridge.EXTRA_ARGV,args);
intent.putExtra(IrrlichtBridge.EXTRA_ARGV, args);
} }
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.e("YGOStarter", "跳转前" + System.currentTimeMillis()); Log.e("YGOStarter", "跳转前" + System.currentTimeMillis());
...@@ -207,4 +211,9 @@ public class YGOStarter { ...@@ -207,4 +211,9 @@ public class YGOStarter {
int oldRequestedOrientation; int oldRequestedOrientation;
boolean isRunning = false; boolean isRunning = false;
} }
public static boolean isGameRunning(Context context) {
return ComponentUtils.isProcessRunning(context, context.getPackageName() + ":game")
&& ComponentUtils.isActivityRunning(context, new ComponentName(context, YGOMobileActivity.class));
}
} }
...@@ -435,7 +435,7 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie ...@@ -435,7 +435,7 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie
builder.setLeftButtonText(R.string.join_game); builder.setLeftButtonText(R.string.join_game);
builder.setLeftButtonListener((dlg, i) -> { builder.setLeftButtonListener((dlg, i) -> {
dlg.dismiss(); dlg.dismiss();
if (Build.VERSION.SDK_INT >= 23 && ComponentUtils.isActivityRunning(this, new ComponentName(this, YGOMobileActivity.class))) { if (Build.VERSION.SDK_INT >= 23 && YGOStarter.isGameRunning(getActivity())) {
Toast toast = Toast.makeText(getApplicationContext(), R.string.tip_return_to_duel, Toast.LENGTH_SHORT); Toast toast = Toast.makeText(getApplicationContext(), R.string.tip_return_to_duel, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0); toast.setGravity(Gravity.CENTER, 0, 0);
toast.show(); toast.show();
...@@ -473,7 +473,7 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie ...@@ -473,7 +473,7 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie
options.mUserName = serverInfo.getPlayerName(); options.mUserName = serverInfo.getPlayerName();
options.mPort = serverInfo.getPort(); options.mPort = serverInfo.getPort();
options.mRoomName = name; options.mRoomName = name;
YGOStarter.startGame(this, options, null); YGOStarter.startGame(this, options);
} }
protected abstract void checkResourceDownload(ResCheckTask.ResCheckListener listener); protected abstract void checkResourceDownload(ResCheckTask.ResCheckListener listener);
...@@ -558,7 +558,7 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie ...@@ -558,7 +558,7 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie
tv.setOnClickListener((v) -> { tv.setOnClickListener((v) -> {
openGame(); openGame();
}); });
if (ComponentUtils.isActivityRunning(this, new ComponentName(this, YGOMobileActivity.class))) { if (YGOStarter.isGameRunning(getActivity())) {
tv.setVisibility(View.VISIBLE); tv.setVisibility(View.VISIBLE);
} else { } else {
tv.setVisibility(View.GONE); tv.setVisibility(View.GONE);
...@@ -664,7 +664,7 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie ...@@ -664,7 +664,7 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie
} }
public void showTipsToast() { public void showTipsToast() {
if (!ComponentUtils.isActivityRunning(this, new ComponentName(this, YGOMobileActivity.class))) { if (!YGOStarter.isGameRunning(getActivity())) {
String[] tipsList = this.getResources().getStringArray(R.array.tips); String[] tipsList = this.getResources().getStringArray(R.array.tips);
int x = (int) (Math.random() * tipsList.length); int x = (int) (Math.random() * tipsList.length);
String tips = tipsList[x]; String tips = tipsList[x];
......
...@@ -196,7 +196,7 @@ public class MainActivity extends HomeActivity { ...@@ -196,7 +196,7 @@ public class MainActivity extends HomeActivity {
super.onResume(); super.onResume();
YGOStarter.onResumed(this); YGOStarter.onResumed(this);
//如果游戏Activity已经不存在了,则 //如果游戏Activity已经不存在了,则
if (!ComponentUtils.isActivityRunning(this, new ComponentName(this, YGOMobileActivity.class))) { if (!YGOStarter.isGameRunning(getActivity())) {
sendBroadcast(new Intent(IrrlichtBridge.ACTION_STOP).setPackage(getPackageName())); sendBroadcast(new Intent(IrrlichtBridge.ACTION_STOP).setPackage(getPackageName()));
} }
} }
...@@ -246,7 +246,7 @@ public class MainActivity extends HomeActivity { ...@@ -246,7 +246,7 @@ public class MainActivity extends HomeActivity {
@Override @Override
protected void openGame() { protected void openGame() {
if (enableStart) { if (enableStart) {
YGOStarter.startGame(this, null, null); YGOStarter.startGame(this, null);
} else { } else {
VUiKit.show(this, R.string.dont_start_game); VUiKit.show(this, R.string.dont_start_game);
} }
......
...@@ -4,12 +4,25 @@ import android.app.ActivityManager; ...@@ -4,12 +4,25 @@ import android.app.ActivityManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.text.TextUtils;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import java.util.List; import java.util.List;
public class ComponentUtils { public class ComponentUtils {
public static boolean isProcessRunning(Context context, String processName) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
if (tasks != null) {
for (ActivityManager.RunningAppProcessInfo taskInfo : tasks) {
if (context.getApplicationInfo().uid == taskInfo.uid && TextUtils.equals(processName, taskInfo.processName)) {
return true;
}
}
}
return false;
}
public static boolean isActivityRunning(Context context, ComponentName componentName) { public static boolean isActivityRunning(Context context, ComponentName componentName) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
return isActivityRunningV21(context, componentName); return isActivityRunningV21(context, componentName);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment