From aeec2496262b111a13be280971a3d42bbf46cc88 Mon Sep 17 00:00:00 2001
From: Lioncash <mathew1800@gmail.com>
Date: Thu, 29 Aug 2013 17:20:46 -0400
Subject: [PATCH] [Android] Decouple the emulation processes from the Main
 activity. Moved them into their own activity called EmulationActivity.

---
 Source/Android/AndroidManifest.xml            |   4 +-
 Source/Android/res/layout/emulation_view.xml  |   4 +-
 .../dolphinemu/DolphinEmulator.java           | 141 +--------------
 .../dolphinemu/EmulationActivity.java         | 160 ++++++++++++++++++
 .../dolphinemu/gamelist/GameListFragment.java |   7 +-
 5 files changed, 173 insertions(+), 143 deletions(-)
 create mode 100644 Source/Android/src/org/dolphinemu/dolphinemu/EmulationActivity.java

diff --git a/Source/Android/AndroidManifest.xml b/Source/Android/AndroidManifest.xml
index a88597d29c..bbf17fb974 100644
--- a/Source/Android/AndroidManifest.xml
+++ b/Source/Android/AndroidManifest.xml
@@ -32,8 +32,8 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
-        <activity android:name="org.dolphinemu.dolphinemu.gamelist.GameListActivity" >
-        </activity>
+        <activity android:name="org.dolphinemu.dolphinemu.gamelist.GameListActivity" />
+        <activity android:name="org.dolphinemu.dolphinemu.EmulationActivity" />
         <activity
             android:name="org.dolphinemu.dolphinemu.settings.PrefsActivity"
             android:label="@string/settings" >
diff --git a/Source/Android/res/layout/emulation_view.xml b/Source/Android/res/layout/emulation_view.xml
index 017e98602e..9dcdf11166 100644
--- a/Source/Android/res/layout/emulation_view.xml
+++ b/Source/Android/res/layout/emulation_view.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<merge xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content"
     android:layout_height="match_parent">
 
@@ -11,4 +11,4 @@
         android:focusable="false"
         android:focusableInTouchMode="false"/>
 
-</RelativeLayout>
\ No newline at end of file
+</merge>
\ No newline at end of file
diff --git a/Source/Android/src/org/dolphinemu/dolphinemu/DolphinEmulator.java b/Source/Android/src/org/dolphinemu/dolphinemu/DolphinEmulator.java
index 00aabfaf66..5aa1075cde 100644
--- a/Source/Android/src/org/dolphinemu/dolphinemu/DolphinEmulator.java
+++ b/Source/Android/src/org/dolphinemu/dolphinemu/DolphinEmulator.java
@@ -7,31 +7,23 @@
 package org.dolphinemu.dolphinemu;
 
 import android.app.Activity;
-import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.os.Environment;
-import android.util.DisplayMetrics;
 import android.util.Log;
-import android.view.InputDevice;
-import android.view.KeyEvent;
-import android.view.MotionEvent;
-import android.view.WindowManager;
 
 import java.io.*;
-import java.util.List;
 
 import org.dolphinemu.dolphinemu.gamelist.GameListActivity;
-import org.dolphinemu.dolphinemu.settings.InputConfigFragment;
 import org.dolphinemu.dolphinemu.settings.UserPreferences;
 
+/**
+ * The main activity of this emulator.
+ *
+ * @param <MainActivity> Main activity.
+ */
 public final class DolphinEmulator<MainActivity> extends Activity 
 {
-	private static boolean Running = false;
-
-	private float screenWidth;
-	private float screenHeight;
-
 	private void CopyAsset(String asset, String output)
 	{
 		InputStream in = null;
@@ -62,30 +54,6 @@ public final class DolphinEmulator<MainActivity> extends Activity
 		}
 	}
 
-	@Override
-	public void onStop()
-	{
-		super.onStop();
-		if (Running)
-			NativeLibrary.StopEmulation();
-	}
-
-	@Override
-	public void onPause()
-	{
-		super.onPause();
-		if (Running)
-			NativeLibrary.PauseEmulation();
-	}
-
-	@Override
-	public void onResume()
-	{
-		super.onResume();
-		if (Running)
-			NativeLibrary.UnPauseEmulation();
-	}
-
 	/** Called when the activity is first created. */
 	@Override
 	public void onCreate(Bundle savedInstanceState)
@@ -138,103 +106,4 @@ public final class DolphinEmulator<MainActivity> extends Activity
 			UserPreferences.LoadDolphinConfigToPrefs(this);
 		}
 	}
-
-	@Override
-	public void onActivityResult(int requestCode, int resultCode, Intent data)
-	{
-		super.onActivityResult(requestCode, resultCode, data);
-		
-		if (resultCode == Activity.RESULT_OK)
-		{
-			DisplayMetrics displayMetrics = new DisplayMetrics();
-			WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
-			wm.getDefaultDisplay().getMetrics(displayMetrics);
-			screenWidth = displayMetrics.widthPixels;
-			screenHeight = displayMetrics.heightPixels;
-
-			String FileName = data.getStringExtra("Select");
-			this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
-			NativeLibrary.SetDimensions((int)screenWidth, (int)screenHeight);
-			NativeLibrary.SetFilename(FileName);
-			setContentView(R.layout.emulation_view);
-			Running = true;
-		}
-	}
-
-	@Override
-	public boolean onTouchEvent(MotionEvent event)
-	{
-		float X = event.getX();
-		float Y = event.getY();
-		int Action = event.getActionMasked();
-
-		// Converts button locations 0 - 1 to OGL screen coords -1.0 - 1.0
-		float ScreenX = ((X / screenWidth) * 2.0f) - 1.0f;
-		float ScreenY = ((Y / screenHeight) * -2.0f) + 1.0f;
-
-		NativeLibrary.onTouchEvent(Action, ScreenX, ScreenY);
-		
-		return false;
-	}
-
-	// Gets button presses
-	@Override
-	public boolean dispatchKeyEvent(KeyEvent event)
-	{
-		int action = 0;
-
-		// Special catch for the back key
-		// Currently disabled because stopping and starting emulation is broken.
-		/*
-		if (event.getSource() == InputDevice.SOURCE_KEYBOARD
-			&& event.getKeyCode() == KeyEvent.KEYCODE_BACK
-			&& event.getAction() == KeyEvent.ACTION_UP)
-		{
-			if (Running)
-				NativeLibrary.StopEmulation();
-			Running = false;
-			Intent ListIntent = new Intent(this, GameListActivity.class);
-			startActivityForResult(ListIntent, 1);
-			return true;
-		}
-		*/
-
-		if (Running)
-		{
-			switch (event.getAction())
-			{
-				case KeyEvent.ACTION_DOWN:
-					action = 0;
-					break;
-				case KeyEvent.ACTION_UP:
-					action = 1;
-					break;
-				default:
-					return false;
-			}
-			InputDevice input = event.getDevice();
-			NativeLibrary.onGamePadEvent(InputConfigFragment.getInputDesc(input), event.getKeyCode(), action);
-			return true;
-		}
-		return false;
-	}
-
-	@Override
-	public boolean dispatchGenericMotionEvent(MotionEvent event)
-	{
-		if (((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) == 0) || !Running)
-		{
-			return super.dispatchGenericMotionEvent(event);
-		}
-
-		InputDevice input = event.getDevice();
-		List<InputDevice.MotionRange> motions = input.getMotionRanges();
-
-		for (InputDevice.MotionRange range : motions)
-		{
-			NativeLibrary.onGamePadMoveEvent(InputConfigFragment.getInputDesc(input), range.getAxis(), event.getAxisValue(range.getAxis()));
-		}
-
-		return true;
-	}
 }
\ No newline at end of file
diff --git a/Source/Android/src/org/dolphinemu/dolphinemu/EmulationActivity.java b/Source/Android/src/org/dolphinemu/dolphinemu/EmulationActivity.java
new file mode 100644
index 0000000000..d388d20485
--- /dev/null
+++ b/Source/Android/src/org/dolphinemu/dolphinemu/EmulationActivity.java
@@ -0,0 +1,160 @@
+package org.dolphinemu.dolphinemu;
+
+import java.util.List;
+
+import org.dolphinemu.dolphinemu.settings.InputConfigFragment;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.DisplayMetrics;
+import android.view.InputDevice;
+import android.view.KeyEvent;
+import android.view.MotionEvent;
+import android.view.Window;
+import android.view.WindowManager;
+
+/**
+ * This is the activity where all of the emulation handling happens.
+ * This activity is responsible for displaying the SurfaceView that we render to.
+ */
+public final class EmulationActivity extends Activity
+{
+	private boolean Running;
+	private float screenWidth;
+	private float screenHeight;
+
+	@Override
+	public void onCreate(Bundle savedInstanceState)
+	{
+		super.onCreate(savedInstanceState);
+
+		// Retrieve screen dimensions.
+		DisplayMetrics displayMetrics = new DisplayMetrics();
+		WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
+		wm.getDefaultDisplay().getMetrics(displayMetrics);
+		this.screenHeight = displayMetrics.heightPixels;
+		this.screenWidth = displayMetrics.widthPixels;
+
+		// Request window features for the emulation view.
+		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
+		getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
+
+		// Set the native rendering screen width/height.
+		// Also get the intent passed from the GameList when the game
+		// was selected. This is so the path of the game can be retrieved
+		// and set on the native side of the code so the emulator can actually
+		// load the game.
+		Intent gameToEmulate = getIntent();
+		NativeLibrary.SetDimensions((int)screenWidth, (int)screenHeight);
+		NativeLibrary.SetFilename(gameToEmulate.getStringExtra("SelectedGame"));
+		Running = true;
+		
+		// Set the emulation window.
+		setContentView(R.layout.emulation_view);
+		
+	}
+
+	@Override
+	public void onStop()
+	{
+		super.onStop();
+		if (Running)
+			NativeLibrary.StopEmulation();
+	}
+
+	@Override
+	public void onPause()
+	{
+		super.onPause();
+		if (Running)
+			NativeLibrary.PauseEmulation();
+	}
+
+	@Override
+	public void onResume()
+	{
+		super.onResume();
+		if (Running)
+			NativeLibrary.UnPauseEmulation();
+	}
+	
+	@Override
+	public boolean onTouchEvent(MotionEvent event)
+	{
+		float X = event.getX();
+		float Y = event.getY();
+		int Action = event.getActionMasked();
+
+		// Converts button locations 0 - 1 to OGL screen coords -1.0 - 1.0
+		float ScreenX = ((X / screenWidth) * 2.0f) - 1.0f;
+		float ScreenY = ((Y / screenHeight) * -2.0f) + 1.0f;
+
+		NativeLibrary.onTouchEvent(Action, ScreenX, ScreenY);
+		
+		return false;
+	}
+
+	// Gets button presses
+	@Override
+	public boolean dispatchKeyEvent(KeyEvent event)
+	{
+		int action = 0;
+
+		// Special catch for the back key
+		// Currently disabled because stopping and starting emulation is broken.
+		/*
+		if (event.getSource() == InputDevice.SOURCE_KEYBOARD
+			&& event.getKeyCode() == KeyEvent.KEYCODE_BACK
+			&& event.getAction() == KeyEvent.ACTION_UP)
+		{
+			if (Running)
+				NativeLibrary.StopEmulation();
+			Running = false;
+			Intent ListIntent = new Intent(this, GameListActivity.class);
+			startActivityForResult(ListIntent, 1);
+			return true;
+		}
+		*/
+
+		if (Running)
+		{
+			switch (event.getAction())
+			{
+				case KeyEvent.ACTION_DOWN:
+					action = 0;
+					break;
+				case KeyEvent.ACTION_UP:
+					action = 1;
+					break;
+				default:
+					return false;
+			}
+			InputDevice input = event.getDevice();
+			NativeLibrary.onGamePadEvent(InputConfigFragment.getInputDesc(input), event.getKeyCode(), action);
+			return true;
+		}
+		return false;
+	}
+
+	@Override
+	public boolean dispatchGenericMotionEvent(MotionEvent event)
+	{
+		if (((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) == 0) || !Running)
+		{
+			return super.dispatchGenericMotionEvent(event);
+		}
+
+		InputDevice input = event.getDevice();
+		List<InputDevice.MotionRange> motions = input.getMotionRanges();
+
+		for (InputDevice.MotionRange range : motions)
+		{
+			NativeLibrary.onGamePadMoveEvent(InputConfigFragment.getInputDesc(input), range.getAxis(), event.getAxisValue(range.getAxis()));
+		}
+
+		return true;
+	}
+}
diff --git a/Source/Android/src/org/dolphinemu/dolphinemu/gamelist/GameListFragment.java b/Source/Android/src/org/dolphinemu/dolphinemu/gamelist/GameListFragment.java
index 9dc6fcba8d..d5739d68ed 100644
--- a/Source/Android/src/org/dolphinemu/dolphinemu/gamelist/GameListFragment.java
+++ b/Source/Android/src/org/dolphinemu/dolphinemu/gamelist/GameListFragment.java
@@ -25,6 +25,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
+import org.dolphinemu.dolphinemu.EmulationActivity;
 import org.dolphinemu.dolphinemu.NativeLibrary;
 import org.dolphinemu.dolphinemu.R;
 
@@ -132,9 +133,9 @@ public final class GameListFragment extends Fragment
 	{
 		Toast.makeText(mMe, getString(R.string.file_clicked) + o, Toast.LENGTH_SHORT).show();
 
-		Intent intent = new Intent();
-		intent.putExtra("Select", o);
-		mMe.setResult(Activity.RESULT_OK, intent);
+		Intent intent = new Intent(mMe, EmulationActivity.class);
+		intent.putExtra("SelectedGame", o);
+		mMe.startActivity(intent);
 		mMe.finish();
 	}