MainActivity.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.jmonkeyengine.tests;
  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.content.pm.ApplicationInfo;
  9. import android.os.Bundle;
  10. import android.text.Editable;
  11. import android.text.TextWatcher;
  12. import android.util.Log;
  13. import android.view.Menu;
  14. import android.view.MenuInflater;
  15. import android.view.MenuItem;
  16. import android.view.View;
  17. import android.widget.AdapterView;
  18. import android.widget.AdapterView.OnItemClickListener;
  19. import android.widget.Button;
  20. import android.widget.EditText;
  21. import android.widget.ListView;
  22. import com.jme3.app.Application;
  23. import dalvik.system.DexFile;
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.Enumeration;
  27. import java.util.List;
  28. //TODO: Create onscreen virtual keypad for triggering normal mapped keys used by test apps or modify test apps for touch with onscreen keypad
  29. /**
  30. * Main Activity started by the application. Users select different jME3 test
  31. * applications that are started via TestsHarness Activity.
  32. * @author iwgeric
  33. */
  34. public class MainActivity extends Activity implements OnItemClickListener, View.OnClickListener, TextWatcher {
  35. private static final String TAG = "MainActivity";
  36. /**
  37. * Static String to pass the key for the selected test app to the
  38. * TestsHarness class to start the application. Also used to store the
  39. * current selection to the savedInstanceState Bundle.
  40. */
  41. public static final String SELECTED_APP_CLASS = "Selected_App_Class";
  42. /**
  43. * Static String to pass the key for the selected list position to the
  44. * savedInstanceState Bundle so the list position can be restored after
  45. * exiting the test application.
  46. */
  47. public static final String SELECTED_LIST_POSITION = "Selected_List_Position";
  48. /**
  49. * Static String to pass the key for the setting for enabling mouse events to the
  50. * savedInstanceState Bundle.
  51. */
  52. public static final String ENABLE_MOUSE_EVENTS = "Enable_Mouse_Events";
  53. /**
  54. * Static String to pass the key for the setting for enabling joystick events to the
  55. * savedInstanceState Bundle.
  56. */
  57. public static final String ENABLE_JOYSTICK_EVENTS = "Enable_Joystick_Events";
  58. /* Fields to contain the current position and display contents of the spinner */
  59. private int currentPosition = 0;
  60. private String currentSelection = "";
  61. private List<String> classNames = new ArrayList<String>();
  62. private List<String> exclusions = new ArrayList<String>();
  63. private String rootPackage;
  64. /* ListView that displays the test application class names. */
  65. private ListView listClasses;
  66. /* ArrayAdapter connects the spinner widget to array-based data. */
  67. private CustomArrayAdapter arrayAdapter;
  68. /* Buttons to start application or stop the activity. */
  69. private Button btnOK;
  70. private Button btnCancel;
  71. /* Filter Edit Box */
  72. EditText editFilterText;
  73. /* Custom settings for the test app */
  74. private boolean enableMouseEvents = true;
  75. private boolean enableJoystickEvents = false;
  76. /**
  77. * Called when the activity is first created.
  78. */
  79. @Override
  80. public void onCreate(Bundle savedInstanceState) {
  81. super.onCreate(savedInstanceState);
  82. if (savedInstanceState != null) {
  83. Log.i(TAG, "Restoring selections in onCreate: "
  84. + "position: " + savedInstanceState.getInt(SELECTED_LIST_POSITION, 0)
  85. + "class: " + savedInstanceState.getString(SELECTED_APP_CLASS)
  86. );
  87. currentPosition = savedInstanceState.getInt(SELECTED_LIST_POSITION, 0);
  88. currentSelection = savedInstanceState.getString(SELECTED_APP_CLASS);
  89. enableMouseEvents = savedInstanceState.getBoolean(ENABLE_MOUSE_EVENTS, true);
  90. enableJoystickEvents = savedInstanceState.getBoolean(ENABLE_JOYSTICK_EVENTS, false);
  91. }
  92. /* Set content view and register views */
  93. setContentView(R.layout.test_chooser_layout);
  94. btnOK = (Button) findViewById(R.id.btnOK);
  95. btnCancel = (Button) findViewById(R.id.btnCancel);
  96. listClasses = (ListView) findViewById(R.id.listClasses);
  97. editFilterText = (EditText) findViewById(R.id.txtFilter);
  98. /* Define the root package to start with */
  99. rootPackage = "jme3test";
  100. /* Create an array of Strings to define which classes to exclude */
  101. exclusions.add("$"); // inner classes
  102. exclusions.add("TestChooser"); // Desktop test chooser class
  103. exclusions.add("awt"); // Desktop test chooser class
  104. // mExclusions.add("");
  105. /*
  106. * Read the class names from the dex file and filter based on
  107. * name and super class.
  108. */
  109. Log.i(TAG, "Composing Test list...");
  110. ApplicationInfo ai = this.getApplicationInfo();
  111. String classPath = ai.sourceDir;
  112. DexFile dex = null;
  113. Enumeration<String> apkClassNames = null;
  114. try {
  115. dex = new DexFile(classPath);
  116. apkClassNames = dex.entries();
  117. while (apkClassNames.hasMoreElements()) {
  118. String className = apkClassNames.nextElement();
  119. if (checkClassName(className) && checkClassType(className)) {
  120. classNames.add(className);
  121. }
  122. // classNames.add(className);
  123. }
  124. } catch (IOException e) {
  125. e.printStackTrace();
  126. } finally {
  127. try {
  128. dex.close();
  129. } catch (IOException e) {
  130. e.printStackTrace();
  131. }
  132. }
  133. /*
  134. * Create a backing Adapter for the List View from a list of the
  135. * classes. The list is defined by array of class names.
  136. */
  137. arrayAdapter = new CustomArrayAdapter(
  138. this,
  139. R.layout.test_chooser_row, // text view to display selection
  140. classNames // array of strings to display
  141. );
  142. /* Set the resource id for selected and non selected backgrounds */
  143. Log.i(TAG, "Setting Adapter Background Resource IDs");
  144. arrayAdapter.setSelectedBackgroundResource(R.drawable.selected);
  145. arrayAdapter.setNonSelectedBackgroundResource(R.drawable.nonselected);
  146. /* Attach the Adapter to the spinner */
  147. Log.i(TAG, "Setting ListView Adapter");
  148. listClasses.setAdapter(arrayAdapter);
  149. /* Set initial selection for the list */
  150. setSelection(currentPosition);
  151. /* Set Click and Text Changed listeners */
  152. listClasses.setOnItemClickListener(this);
  153. btnOK.setOnClickListener(this);
  154. btnCancel.setOnClickListener(this);
  155. editFilterText.addTextChangedListener(this);
  156. }
  157. /**
  158. * User selected an application. Sets the current selection and redraws
  159. * the list view to highlight the selected item.
  160. * @param parent AdapterView tied to the list
  161. * @param view The ListView
  162. * @param position Selection position in the list of class names
  163. * @param id
  164. */
  165. @Override
  166. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  167. setSelection(position);
  168. }
  169. /**
  170. * User clicked a view on the screen. Check for the OK and Cancel buttons
  171. * and either start the applicaiton or exit.
  172. * @param view
  173. */
  174. public void onClick(View view) {
  175. if (view.equals(btnOK)) {
  176. /* Get selected class, pack it in the intent and start the test app */
  177. Log.i(TAG, "User selected OK for class: " + currentSelection);
  178. Intent intent = new Intent(this, TestsHarness.class);
  179. intent.putExtra(SELECTED_APP_CLASS, currentSelection);
  180. intent.putExtra(ENABLE_MOUSE_EVENTS, enableMouseEvents);
  181. intent.putExtra(ENABLE_JOYSTICK_EVENTS, enableJoystickEvents);
  182. startActivity(intent);
  183. } else if (view.equals(btnCancel)) {
  184. /* Exit */
  185. Log.i(TAG, "User selected Cancel");
  186. finish();
  187. }
  188. }
  189. /**
  190. * Check class name to see if the class is in the root package and if it
  191. * contains any of the exclusion strings
  192. * @param className Class name to check
  193. * @return true if the check passes, false otherwise
  194. */
  195. private boolean checkClassName(String className) {
  196. boolean include = true;
  197. /* check to see if the class in inside the rootPackage package */
  198. if (className.startsWith(rootPackage)) {
  199. /* check to see if the class contains any of the exlusion strings */
  200. for (int i = 0; i < exclusions.size(); i++) {
  201. if (className.contains(exclusions.get(i))) {
  202. Log.i(TAG, "Skipping Class " + className + ". Includes exclusion string: " + exclusions.get(i) + ".");
  203. include = false;
  204. break;
  205. }
  206. }
  207. } else {
  208. include = false;
  209. Log.i(TAG, "Skipping Class " + className + ". Not in the root package: " + rootPackage + ".");
  210. }
  211. return include;
  212. }
  213. /**
  214. * Check to see if the class extends Application or SimpleApplication
  215. * @param className Class name to check
  216. * @return true if the check passes, false otherwise
  217. */
  218. private boolean checkClassType(String className) {
  219. boolean include = true;
  220. try {
  221. Class<?> clazz = (Class<?>)Class.forName(className);
  222. if (Application.class.isAssignableFrom(clazz)) {
  223. Log.i(TAG, "Class " + className + " is a jME Application");
  224. } else {
  225. include = false;
  226. Log.i(TAG, "Skipping Class " + className + ". Not a jME Application");
  227. }
  228. } catch (ClassNotFoundException cnfe) {
  229. include = false;
  230. Log.i(TAG, "Skipping Class " + className + ". Class not found.");
  231. }
  232. return include;
  233. }
  234. private void setSelection(int position) {
  235. if (position == -1) {
  236. arrayAdapter.setSelectedPosition(-1);
  237. currentPosition = -1;
  238. currentSelection = "";
  239. btnOK.setEnabled(false);
  240. listClasses.invalidateViews();
  241. } else {
  242. arrayAdapter.setSelectedPosition(position);
  243. currentPosition = position;
  244. currentSelection = arrayAdapter.getItem(position);
  245. btnOK.setEnabled(true);
  246. listClasses.invalidateViews();
  247. }
  248. }
  249. @Override
  250. public void onSaveInstanceState(Bundle savedInstanceState) {
  251. super.onSaveInstanceState(savedInstanceState);
  252. Log.i(TAG, "Saving selections in onSaveInstanceState: "
  253. + "position: " + currentPosition + ", "
  254. + "class: " + currentSelection + ", "
  255. + "mouseEvents: " + enableMouseEvents + ", "
  256. + "joystickEvents: " + enableJoystickEvents + ", "
  257. );
  258. // Save current selections to the savedInstanceState.
  259. // This bundle will be passed to onCreate if the process is
  260. // killed and restarted.
  261. savedInstanceState.putString(SELECTED_APP_CLASS, currentSelection);
  262. savedInstanceState.putInt(SELECTED_LIST_POSITION, currentPosition);
  263. savedInstanceState.putBoolean(ENABLE_MOUSE_EVENTS, enableMouseEvents);
  264. savedInstanceState.putBoolean(ENABLE_JOYSTICK_EVENTS, enableJoystickEvents);
  265. }
  266. @Override
  267. public void onRestoreInstanceState(Bundle savedInstanceState) {
  268. super.onRestoreInstanceState(savedInstanceState);
  269. // Log.i(TAG, "Restoring selections in onRestoreInstanceState: "
  270. // + "position: " + savedInstanceState.getInt(SELECTED_LIST_POSITION, 0)
  271. // + "class: " + savedInstanceState.getString(SELECTED_APP_CLASS)
  272. // );
  273. // //Restore selections from the savedInstanceState.
  274. // // This bundle has also been passed to onCreate.
  275. // currentPosition = savedInstanceState.getInt(SELECTED_LIST_POSITION, 0);
  276. // currentSelection = savedInstanceState.getString(SELECTED_APP_CLASS);
  277. }
  278. public void beforeTextChanged(CharSequence cs, int i, int i1, int i2) {
  279. }
  280. public void onTextChanged(CharSequence cs, int startPos, int beforePos, int count) {
  281. Log.i(TAG, "onTextChanged with cs: " + cs + ", startPos: " + startPos + ", beforePos: " + beforePos + ", count: " + count);
  282. arrayAdapter.getFilter().filter(cs.toString());
  283. setSelection(-1);
  284. }
  285. public void afterTextChanged(Editable edtbl) {
  286. }
  287. @Override
  288. protected void onDestroy() {
  289. super.onDestroy();
  290. editFilterText.removeTextChangedListener(this);
  291. }
  292. @Override
  293. public boolean onCreateOptionsMenu(Menu menu) {
  294. MenuInflater inflater = getMenuInflater();
  295. inflater.inflate(R.menu.optionsmenu, menu);
  296. return true;
  297. }
  298. @Override
  299. public boolean onPrepareOptionsMenu (Menu menu) {
  300. MenuItem item;
  301. item = menu.findItem(R.id.optionEnableMouseEvents);
  302. if (item != null) {
  303. Log.i(TAG, "Found EnableMouseEvents menu item");
  304. if (enableMouseEvents) {
  305. item.setTitle(R.string.strOptionDisableMouseEventsTitle);
  306. } else {
  307. item.setTitle(R.string.strOptionEnableMouseEventsTitle);
  308. }
  309. }
  310. item = menu.findItem(R.id.optionEnableJoystickEvents);
  311. if (item != null) {
  312. Log.i(TAG, "Found EnableJoystickEvents menu item");
  313. if (enableJoystickEvents) {
  314. item.setTitle(R.string.strOptionDisableJoystickEventsTitle);
  315. } else {
  316. item.setTitle(R.string.strOptionEnableJoystickEventsTitle);
  317. }
  318. }
  319. return true;
  320. }
  321. @Override
  322. public boolean onOptionsItemSelected(MenuItem item) {
  323. switch (item.getItemId()) {
  324. case R.id.optionEnableMouseEvents:
  325. enableMouseEvents = !enableMouseEvents;
  326. Log.i(TAG, "enableMouseEvents set to: " + enableMouseEvents);
  327. break;
  328. case R.id.optionEnableJoystickEvents:
  329. enableJoystickEvents = !enableJoystickEvents;
  330. Log.i(TAG, "enableJoystickEvents set to: " + enableJoystickEvents);
  331. break;
  332. default:
  333. return super.onOptionsItemSelected(item);
  334. }
  335. return true;
  336. }
  337. }