MainActivity.java 17 KB

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