SDLActivity.java 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. package org.libsdl.app;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.app.Dialog;
  5. import android.app.UiModeManager;
  6. import android.content.ClipboardManager;
  7. import android.content.ClipData;
  8. import android.content.Context;
  9. import android.content.DialogInterface;
  10. import android.content.Intent;
  11. import android.content.pm.ActivityInfo;
  12. import android.content.pm.ApplicationInfo;
  13. import android.content.pm.PackageManager;
  14. import android.content.res.Configuration;
  15. import android.graphics.Bitmap;
  16. import android.graphics.Color;
  17. import android.graphics.PorterDuff;
  18. import android.graphics.drawable.Drawable;
  19. import android.hardware.Sensor;
  20. import android.net.Uri;
  21. import android.os.Build;
  22. import android.os.Bundle;
  23. import android.os.Handler;
  24. import android.os.Message;
  25. import android.text.Editable;
  26. import android.text.InputType;
  27. import android.text.Selection;
  28. import android.util.DisplayMetrics;
  29. import android.util.Log;
  30. import android.util.SparseArray;
  31. import android.view.Display;
  32. import android.view.Gravity;
  33. import android.view.InputDevice;
  34. import android.view.KeyEvent;
  35. import android.view.PointerIcon;
  36. import android.view.Surface;
  37. import android.view.View;
  38. import android.view.ViewGroup;
  39. import android.view.Window;
  40. import android.view.WindowManager;
  41. import android.view.inputmethod.BaseInputConnection;
  42. import android.view.inputmethod.EditorInfo;
  43. import android.view.inputmethod.InputConnection;
  44. import android.view.inputmethod.InputMethodManager;
  45. import android.widget.Button;
  46. import android.widget.EditText;
  47. import android.widget.LinearLayout;
  48. import android.widget.RelativeLayout;
  49. import android.widget.TextView;
  50. import android.widget.Toast;
  51. import java.util.Hashtable;
  52. import java.util.Locale;
  53. /**
  54. SDL Activity
  55. */
  56. public class SDLActivity extends Activity implements View.OnSystemUiVisibilityChangeListener {
  57. private static final String TAG = "SDL";
  58. private static final int SDL_MAJOR_VERSION = 2;
  59. private static final int SDL_MINOR_VERSION = 26;
  60. private static final int SDL_MICRO_VERSION = 5;
  61. /*
  62. // Display InputType.SOURCE/CLASS of events and devices
  63. //
  64. // SDLActivity.debugSource(device.getSources(), "device[" + device.getName() + "]");
  65. // SDLActivity.debugSource(event.getSource(), "event");
  66. public static void debugSource(int sources, String prefix) {
  67. int s = sources;
  68. int s_copy = sources;
  69. String cls = "";
  70. String src = "";
  71. int tst = 0;
  72. int FLAG_TAINTED = 0x80000000;
  73. if ((s & InputDevice.SOURCE_CLASS_BUTTON) != 0) cls += " BUTTON";
  74. if ((s & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) cls += " JOYSTICK";
  75. if ((s & InputDevice.SOURCE_CLASS_POINTER) != 0) cls += " POINTER";
  76. if ((s & InputDevice.SOURCE_CLASS_POSITION) != 0) cls += " POSITION";
  77. if ((s & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) cls += " TRACKBALL";
  78. int s2 = s_copy & ~InputDevice.SOURCE_ANY; // keep class bits
  79. s2 &= ~( InputDevice.SOURCE_CLASS_BUTTON
  80. | InputDevice.SOURCE_CLASS_JOYSTICK
  81. | InputDevice.SOURCE_CLASS_POINTER
  82. | InputDevice.SOURCE_CLASS_POSITION
  83. | InputDevice.SOURCE_CLASS_TRACKBALL);
  84. if (s2 != 0) cls += "Some_Unkown";
  85. s2 = s_copy & InputDevice.SOURCE_ANY; // keep source only, no class;
  86. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  87. tst = InputDevice.SOURCE_BLUETOOTH_STYLUS;
  88. if ((s & tst) == tst) src += " BLUETOOTH_STYLUS";
  89. s2 &= ~tst;
  90. }
  91. tst = InputDevice.SOURCE_DPAD;
  92. if ((s & tst) == tst) src += " DPAD";
  93. s2 &= ~tst;
  94. tst = InputDevice.SOURCE_GAMEPAD;
  95. if ((s & tst) == tst) src += " GAMEPAD";
  96. s2 &= ~tst;
  97. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  98. tst = InputDevice.SOURCE_HDMI;
  99. if ((s & tst) == tst) src += " HDMI";
  100. s2 &= ~tst;
  101. }
  102. tst = InputDevice.SOURCE_JOYSTICK;
  103. if ((s & tst) == tst) src += " JOYSTICK";
  104. s2 &= ~tst;
  105. tst = InputDevice.SOURCE_KEYBOARD;
  106. if ((s & tst) == tst) src += " KEYBOARD";
  107. s2 &= ~tst;
  108. tst = InputDevice.SOURCE_MOUSE;
  109. if ((s & tst) == tst) src += " MOUSE";
  110. s2 &= ~tst;
  111. if (Build.VERSION.SDK_INT >= 26) {
  112. tst = InputDevice.SOURCE_MOUSE_RELATIVE;
  113. if ((s & tst) == tst) src += " MOUSE_RELATIVE";
  114. s2 &= ~tst;
  115. tst = InputDevice.SOURCE_ROTARY_ENCODER;
  116. if ((s & tst) == tst) src += " ROTARY_ENCODER";
  117. s2 &= ~tst;
  118. }
  119. tst = InputDevice.SOURCE_STYLUS;
  120. if ((s & tst) == tst) src += " STYLUS";
  121. s2 &= ~tst;
  122. tst = InputDevice.SOURCE_TOUCHPAD;
  123. if ((s & tst) == tst) src += " TOUCHPAD";
  124. s2 &= ~tst;
  125. tst = InputDevice.SOURCE_TOUCHSCREEN;
  126. if ((s & tst) == tst) src += " TOUCHSCREEN";
  127. s2 &= ~tst;
  128. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
  129. tst = InputDevice.SOURCE_TOUCH_NAVIGATION;
  130. if ((s & tst) == tst) src += " TOUCH_NAVIGATION";
  131. s2 &= ~tst;
  132. }
  133. tst = InputDevice.SOURCE_TRACKBALL;
  134. if ((s & tst) == tst) src += " TRACKBALL";
  135. s2 &= ~tst;
  136. tst = InputDevice.SOURCE_ANY;
  137. if ((s & tst) == tst) src += " ANY";
  138. s2 &= ~tst;
  139. if (s == FLAG_TAINTED) src += " FLAG_TAINTED";
  140. s2 &= ~FLAG_TAINTED;
  141. if (s2 != 0) src += " Some_Unkown";
  142. Log.v(TAG, prefix + "int=" + s_copy + " CLASS={" + cls + " } source(s):" + src);
  143. }
  144. */
  145. public static boolean mIsResumedCalled, mHasFocus;
  146. public static final boolean mHasMultiWindow = (Build.VERSION.SDK_INT >= 24);
  147. // Cursor types
  148. // private static final int SDL_SYSTEM_CURSOR_NONE = -1;
  149. private static final int SDL_SYSTEM_CURSOR_ARROW = 0;
  150. private static final int SDL_SYSTEM_CURSOR_IBEAM = 1;
  151. private static final int SDL_SYSTEM_CURSOR_WAIT = 2;
  152. private static final int SDL_SYSTEM_CURSOR_CROSSHAIR = 3;
  153. private static final int SDL_SYSTEM_CURSOR_WAITARROW = 4;
  154. private static final int SDL_SYSTEM_CURSOR_SIZENWSE = 5;
  155. private static final int SDL_SYSTEM_CURSOR_SIZENESW = 6;
  156. private static final int SDL_SYSTEM_CURSOR_SIZEWE = 7;
  157. private static final int SDL_SYSTEM_CURSOR_SIZENS = 8;
  158. private static final int SDL_SYSTEM_CURSOR_SIZEALL = 9;
  159. private static final int SDL_SYSTEM_CURSOR_NO = 10;
  160. private static final int SDL_SYSTEM_CURSOR_HAND = 11;
  161. protected static final int SDL_ORIENTATION_UNKNOWN = 0;
  162. protected static final int SDL_ORIENTATION_LANDSCAPE = 1;
  163. protected static final int SDL_ORIENTATION_LANDSCAPE_FLIPPED = 2;
  164. protected static final int SDL_ORIENTATION_PORTRAIT = 3;
  165. protected static final int SDL_ORIENTATION_PORTRAIT_FLIPPED = 4;
  166. protected static int mCurrentOrientation;
  167. protected static Locale mCurrentLocale;
  168. // Handle the state of the native layer
  169. public enum NativeState {
  170. INIT, RESUMED, PAUSED
  171. }
  172. public static NativeState mNextNativeState;
  173. public static NativeState mCurrentNativeState;
  174. /** If shared libraries (e.g. SDL or the native application) could not be loaded. */
  175. public static boolean mBrokenLibraries = true;
  176. // Main components
  177. protected static SDLActivity mSingleton;
  178. protected static SDLSurface mSurface;
  179. protected static DummyEdit mTextEdit;
  180. protected static boolean mScreenKeyboardShown;
  181. protected static ViewGroup mLayout;
  182. protected static SDLClipboardHandler mClipboardHandler;
  183. protected static Hashtable<Integer, PointerIcon> mCursors;
  184. protected static int mLastCursorID;
  185. protected static SDLGenericMotionListener_API12 mMotionListener;
  186. protected static HIDDeviceManager mHIDDeviceManager;
  187. // This is what SDL runs in. It invokes SDL_main(), eventually
  188. protected static Thread mSDLThread;
  189. protected static SDLGenericMotionListener_API12 getMotionListener() {
  190. if (mMotionListener == null) {
  191. if (Build.VERSION.SDK_INT >= 26) {
  192. mMotionListener = new SDLGenericMotionListener_API26();
  193. } else if (Build.VERSION.SDK_INT >= 24) {
  194. mMotionListener = new SDLGenericMotionListener_API24();
  195. } else {
  196. mMotionListener = new SDLGenericMotionListener_API12();
  197. }
  198. }
  199. return mMotionListener;
  200. }
  201. /**
  202. * This method returns the name of the shared object with the application entry point
  203. * It can be overridden by derived classes.
  204. */
  205. protected String getMainSharedObject() {
  206. String library;
  207. String[] libraries = SDLActivity.mSingleton.getLibraries();
  208. if (libraries.length > 0) {
  209. library = "lib" + libraries[libraries.length - 1] + ".so";
  210. } else {
  211. library = "libmain.so";
  212. }
  213. return getContext().getApplicationInfo().nativeLibraryDir + "/" + library;
  214. }
  215. /**
  216. * This method returns the name of the application entry point
  217. * It can be overridden by derived classes.
  218. */
  219. protected String getMainFunction() {
  220. return "SDL_main";
  221. }
  222. /**
  223. * This method is called by SDL before loading the native shared libraries.
  224. * It can be overridden to provide names of shared libraries to be loaded.
  225. * The default implementation returns the defaults. It never returns null.
  226. * An array returned by a new implementation must at least contain "SDL2".
  227. * Also keep in mind that the order the libraries are loaded may matter.
  228. * @return names of shared libraries to be loaded (e.g. "SDL2", "main").
  229. */
  230. protected String[] getLibraries() {
  231. return new String[] {
  232. "SDL2",
  233. // "SDL2_image",
  234. // "SDL2_mixer",
  235. // "SDL2_net",
  236. // "SDL2_ttf",
  237. "main"
  238. };
  239. }
  240. // Load the .so
  241. public void loadLibraries() {
  242. for (String lib : getLibraries()) {
  243. SDL.loadLibrary(lib);
  244. }
  245. }
  246. /**
  247. * This method is called by SDL before starting the native application thread.
  248. * It can be overridden to provide the arguments after the application name.
  249. * The default implementation returns an empty array. It never returns null.
  250. * @return arguments for the native application.
  251. */
  252. protected String[] getArguments() {
  253. return new String[0];
  254. }
  255. public static void initialize() {
  256. // The static nature of the singleton and Android quirkyness force us to initialize everything here
  257. // Otherwise, when exiting the app and returning to it, these variables *keep* their pre exit values
  258. mSingleton = null;
  259. mSurface = null;
  260. mTextEdit = null;
  261. mLayout = null;
  262. mClipboardHandler = null;
  263. mCursors = new Hashtable<Integer, PointerIcon>();
  264. mLastCursorID = 0;
  265. mSDLThread = null;
  266. mIsResumedCalled = false;
  267. mHasFocus = true;
  268. mNextNativeState = NativeState.INIT;
  269. mCurrentNativeState = NativeState.INIT;
  270. }
  271. protected SDLSurface createSDLSurface(Context context) {
  272. return new SDLSurface(context);
  273. }
  274. // Setup
  275. @Override
  276. protected void onCreate(Bundle savedInstanceState) {
  277. Log.v(TAG, "Device: " + Build.DEVICE);
  278. Log.v(TAG, "Model: " + Build.MODEL);
  279. Log.v(TAG, "onCreate()");
  280. super.onCreate(savedInstanceState);
  281. try {
  282. Thread.currentThread().setName("SDLActivity");
  283. } catch (Exception e) {
  284. Log.v(TAG, "modify thread properties failed " + e.toString());
  285. }
  286. // Load shared libraries
  287. String errorMsgBrokenLib = "";
  288. try {
  289. loadLibraries();
  290. mBrokenLibraries = false; /* success */
  291. } catch(UnsatisfiedLinkError e) {
  292. System.err.println(e.getMessage());
  293. mBrokenLibraries = true;
  294. errorMsgBrokenLib = e.getMessage();
  295. } catch(Exception e) {
  296. System.err.println(e.getMessage());
  297. mBrokenLibraries = true;
  298. errorMsgBrokenLib = e.getMessage();
  299. }
  300. if (!mBrokenLibraries) {
  301. String expected_version = String.valueOf(SDL_MAJOR_VERSION) + "." +
  302. String.valueOf(SDL_MINOR_VERSION) + "." +
  303. String.valueOf(SDL_MICRO_VERSION);
  304. String version = nativeGetVersion();
  305. if (!version.equals(expected_version)) {
  306. mBrokenLibraries = true;
  307. errorMsgBrokenLib = "SDL C/Java version mismatch (expected " + expected_version + ", got " + version + ")";
  308. }
  309. }
  310. if (mBrokenLibraries) {
  311. mSingleton = this;
  312. AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
  313. dlgAlert.setMessage("An error occurred while trying to start the application. Please try again and/or reinstall."
  314. + System.getProperty("line.separator")
  315. + System.getProperty("line.separator")
  316. + "Error: " + errorMsgBrokenLib);
  317. dlgAlert.setTitle("SDL Error");
  318. dlgAlert.setPositiveButton("Exit",
  319. new DialogInterface.OnClickListener() {
  320. @Override
  321. public void onClick(DialogInterface dialog,int id) {
  322. // if this button is clicked, close current activity
  323. SDLActivity.mSingleton.finish();
  324. }
  325. });
  326. dlgAlert.setCancelable(false);
  327. dlgAlert.create().show();
  328. return;
  329. }
  330. // Set up JNI
  331. SDL.setupJNI();
  332. // Initialize state
  333. SDL.initialize();
  334. // So we can call stuff from static callbacks
  335. mSingleton = this;
  336. SDL.setContext(this);
  337. mClipboardHandler = new SDLClipboardHandler();
  338. mHIDDeviceManager = HIDDeviceManager.acquire(this);
  339. // Set up the surface
  340. mSurface = createSDLSurface(getApplication());
  341. mLayout = new RelativeLayout(this);
  342. mLayout.addView(mSurface);
  343. // Get our current screen orientation and pass it down.
  344. mCurrentOrientation = SDLActivity.getCurrentOrientation();
  345. // Only record current orientation
  346. SDLActivity.onNativeOrientationChanged(mCurrentOrientation);
  347. try {
  348. if (Build.VERSION.SDK_INT < 24) {
  349. mCurrentLocale = getContext().getResources().getConfiguration().locale;
  350. } else {
  351. mCurrentLocale = getContext().getResources().getConfiguration().getLocales().get(0);
  352. }
  353. } catch(Exception ignored) {
  354. }
  355. setContentView(mLayout);
  356. setWindowStyle(false);
  357. getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(this);
  358. // Get filename from "Open with" of another application
  359. Intent intent = getIntent();
  360. if (intent != null && intent.getData() != null) {
  361. String filename = intent.getData().getPath();
  362. if (filename != null) {
  363. Log.v(TAG, "Got filename: " + filename);
  364. SDLActivity.onNativeDropFile(filename);
  365. }
  366. }
  367. }
  368. protected void pauseNativeThread() {
  369. mNextNativeState = NativeState.PAUSED;
  370. mIsResumedCalled = false;
  371. if (SDLActivity.mBrokenLibraries) {
  372. return;
  373. }
  374. SDLActivity.handleNativeState();
  375. }
  376. protected void resumeNativeThread() {
  377. mNextNativeState = NativeState.RESUMED;
  378. mIsResumedCalled = true;
  379. if (SDLActivity.mBrokenLibraries) {
  380. return;
  381. }
  382. SDLActivity.handleNativeState();
  383. }
  384. // Events
  385. @Override
  386. protected void onPause() {
  387. Log.v(TAG, "onPause()");
  388. super.onPause();
  389. if (mHIDDeviceManager != null) {
  390. mHIDDeviceManager.setFrozen(true);
  391. }
  392. if (!mHasMultiWindow) {
  393. pauseNativeThread();
  394. }
  395. }
  396. @Override
  397. protected void onResume() {
  398. Log.v(TAG, "onResume()");
  399. super.onResume();
  400. if (mHIDDeviceManager != null) {
  401. mHIDDeviceManager.setFrozen(false);
  402. }
  403. if (!mHasMultiWindow) {
  404. resumeNativeThread();
  405. }
  406. }
  407. @Override
  408. protected void onStop() {
  409. Log.v(TAG, "onStop()");
  410. super.onStop();
  411. if (mHasMultiWindow) {
  412. pauseNativeThread();
  413. }
  414. }
  415. @Override
  416. protected void onStart() {
  417. Log.v(TAG, "onStart()");
  418. super.onStart();
  419. if (mHasMultiWindow) {
  420. resumeNativeThread();
  421. }
  422. }
  423. public static int getCurrentOrientation() {
  424. int result = SDL_ORIENTATION_UNKNOWN;
  425. Activity activity = (Activity)getContext();
  426. if (activity == null) {
  427. return result;
  428. }
  429. Display display = activity.getWindowManager().getDefaultDisplay();
  430. switch (display.getRotation()) {
  431. case Surface.ROTATION_0:
  432. result = SDL_ORIENTATION_PORTRAIT;
  433. break;
  434. case Surface.ROTATION_90:
  435. result = SDL_ORIENTATION_LANDSCAPE;
  436. break;
  437. case Surface.ROTATION_180:
  438. result = SDL_ORIENTATION_PORTRAIT_FLIPPED;
  439. break;
  440. case Surface.ROTATION_270:
  441. result = SDL_ORIENTATION_LANDSCAPE_FLIPPED;
  442. break;
  443. }
  444. return result;
  445. }
  446. @Override
  447. public void onWindowFocusChanged(boolean hasFocus) {
  448. super.onWindowFocusChanged(hasFocus);
  449. Log.v(TAG, "onWindowFocusChanged(): " + hasFocus);
  450. if (SDLActivity.mBrokenLibraries) {
  451. return;
  452. }
  453. mHasFocus = hasFocus;
  454. if (hasFocus) {
  455. mNextNativeState = NativeState.RESUMED;
  456. SDLActivity.getMotionListener().reclaimRelativeMouseModeIfNeeded();
  457. SDLActivity.handleNativeState();
  458. nativeFocusChanged(true);
  459. } else {
  460. nativeFocusChanged(false);
  461. if (!mHasMultiWindow) {
  462. mNextNativeState = NativeState.PAUSED;
  463. SDLActivity.handleNativeState();
  464. }
  465. }
  466. }
  467. @Override
  468. public void onLowMemory() {
  469. Log.v(TAG, "onLowMemory()");
  470. super.onLowMemory();
  471. if (SDLActivity.mBrokenLibraries) {
  472. return;
  473. }
  474. SDLActivity.nativeLowMemory();
  475. }
  476. @Override
  477. public void onConfigurationChanged(Configuration newConfig) {
  478. Log.v(TAG, "onConfigurationChanged()");
  479. super.onConfigurationChanged(newConfig);
  480. if (SDLActivity.mBrokenLibraries) {
  481. return;
  482. }
  483. if (mCurrentLocale == null || !mCurrentLocale.equals(newConfig.locale)) {
  484. mCurrentLocale = newConfig.locale;
  485. SDLActivity.onNativeLocaleChanged();
  486. }
  487. }
  488. @Override
  489. protected void onDestroy() {
  490. Log.v(TAG, "onDestroy()");
  491. if (mHIDDeviceManager != null) {
  492. HIDDeviceManager.release(mHIDDeviceManager);
  493. mHIDDeviceManager = null;
  494. }
  495. if (SDLActivity.mBrokenLibraries) {
  496. super.onDestroy();
  497. return;
  498. }
  499. if (SDLActivity.mSDLThread != null) {
  500. // Send Quit event to "SDLThread" thread
  501. SDLActivity.nativeSendQuit();
  502. // Wait for "SDLThread" thread to end
  503. try {
  504. SDLActivity.mSDLThread.join();
  505. } catch(Exception e) {
  506. Log.v(TAG, "Problem stopping SDLThread: " + e);
  507. }
  508. }
  509. SDLActivity.nativeQuit();
  510. super.onDestroy();
  511. }
  512. @Override
  513. public void onBackPressed() {
  514. // Check if we want to block the back button in case of mouse right click.
  515. //
  516. // If we do, the normal hardware back button will no longer work and people have to use home,
  517. // but the mouse right click will work.
  518. //
  519. boolean trapBack = SDLActivity.nativeGetHintBoolean("SDL_ANDROID_TRAP_BACK_BUTTON", false);
  520. if (trapBack) {
  521. // Exit and let the mouse handler handle this button (if appropriate)
  522. return;
  523. }
  524. // Default system back button behavior.
  525. if (!isFinishing()) {
  526. super.onBackPressed();
  527. }
  528. }
  529. // Called by JNI from SDL.
  530. public static void manualBackButton() {
  531. mSingleton.pressBackButton();
  532. }
  533. // Used to get us onto the activity's main thread
  534. public void pressBackButton() {
  535. runOnUiThread(new Runnable() {
  536. @Override
  537. public void run() {
  538. if (!SDLActivity.this.isFinishing()) {
  539. SDLActivity.this.superOnBackPressed();
  540. }
  541. }
  542. });
  543. }
  544. // Used to access the system back behavior.
  545. public void superOnBackPressed() {
  546. super.onBackPressed();
  547. }
  548. @Override
  549. public boolean dispatchKeyEvent(KeyEvent event) {
  550. if (SDLActivity.mBrokenLibraries) {
  551. return false;
  552. }
  553. int keyCode = event.getKeyCode();
  554. // Ignore certain special keys so they're handled by Android
  555. if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
  556. keyCode == KeyEvent.KEYCODE_VOLUME_UP ||
  557. keyCode == KeyEvent.KEYCODE_CAMERA ||
  558. keyCode == KeyEvent.KEYCODE_ZOOM_IN || /* API 11 */
  559. keyCode == KeyEvent.KEYCODE_ZOOM_OUT /* API 11 */
  560. ) {
  561. return false;
  562. }
  563. return super.dispatchKeyEvent(event);
  564. }
  565. /* Transition to next state */
  566. public static void handleNativeState() {
  567. if (mNextNativeState == mCurrentNativeState) {
  568. // Already in same state, discard.
  569. return;
  570. }
  571. // Try a transition to init state
  572. if (mNextNativeState == NativeState.INIT) {
  573. mCurrentNativeState = mNextNativeState;
  574. return;
  575. }
  576. // Try a transition to paused state
  577. if (mNextNativeState == NativeState.PAUSED) {
  578. if (mSDLThread != null) {
  579. nativePause();
  580. }
  581. if (mSurface != null) {
  582. mSurface.handlePause();
  583. }
  584. mCurrentNativeState = mNextNativeState;
  585. return;
  586. }
  587. // Try a transition to resumed state
  588. if (mNextNativeState == NativeState.RESUMED) {
  589. if (mSurface.mIsSurfaceReady && mHasFocus && mIsResumedCalled) {
  590. if (mSDLThread == null) {
  591. // This is the entry point to the C app.
  592. // Start up the C app thread and enable sensor input for the first time
  593. // FIXME: Why aren't we enabling sensor input at start?
  594. mSDLThread = new Thread(new SDLMain(), "SDLThread");
  595. mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
  596. mSDLThread.start();
  597. // No nativeResume(), don't signal Android_ResumeSem
  598. } else {
  599. nativeResume();
  600. }
  601. mSurface.handleResume();
  602. mCurrentNativeState = mNextNativeState;
  603. }
  604. }
  605. }
  606. // Messages from the SDLMain thread
  607. static final int COMMAND_CHANGE_TITLE = 1;
  608. static final int COMMAND_CHANGE_WINDOW_STYLE = 2;
  609. static final int COMMAND_TEXTEDIT_HIDE = 3;
  610. static final int COMMAND_SET_KEEP_SCREEN_ON = 5;
  611. protected static final int COMMAND_USER = 0x8000;
  612. protected static boolean mFullscreenModeActive;
  613. /**
  614. * This method is called by SDL if SDL did not handle a message itself.
  615. * This happens if a received message contains an unsupported command.
  616. * Method can be overwritten to handle Messages in a different class.
  617. * @param command the command of the message.
  618. * @param param the parameter of the message. May be null.
  619. * @return if the message was handled in overridden method.
  620. */
  621. protected boolean onUnhandledMessage(int command, Object param) {
  622. return false;
  623. }
  624. /**
  625. * A Handler class for Messages from native SDL applications.
  626. * It uses current Activities as target (e.g. for the title).
  627. * static to prevent implicit references to enclosing object.
  628. */
  629. protected static class SDLCommandHandler extends Handler {
  630. @Override
  631. public void handleMessage(Message msg) {
  632. Context context = SDL.getContext();
  633. if (context == null) {
  634. Log.e(TAG, "error handling message, getContext() returned null");
  635. return;
  636. }
  637. switch (msg.arg1) {
  638. case COMMAND_CHANGE_TITLE:
  639. if (context instanceof Activity) {
  640. ((Activity) context).setTitle((String)msg.obj);
  641. } else {
  642. Log.e(TAG, "error handling message, getContext() returned no Activity");
  643. }
  644. break;
  645. case COMMAND_CHANGE_WINDOW_STYLE:
  646. if (Build.VERSION.SDK_INT >= 19) {
  647. if (context instanceof Activity) {
  648. Window window = ((Activity) context).getWindow();
  649. if (window != null) {
  650. if ((msg.obj instanceof Integer) && ((Integer) msg.obj != 0)) {
  651. int flags = View.SYSTEM_UI_FLAG_FULLSCREEN |
  652. View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
  653. View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
  654. View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
  655. View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
  656. View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.INVISIBLE;
  657. window.getDecorView().setSystemUiVisibility(flags);
  658. window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
  659. window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
  660. SDLActivity.mFullscreenModeActive = true;
  661. } else {
  662. int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_VISIBLE;
  663. window.getDecorView().setSystemUiVisibility(flags);
  664. window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
  665. window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
  666. SDLActivity.mFullscreenModeActive = false;
  667. }
  668. }
  669. } else {
  670. Log.e(TAG, "error handling message, getContext() returned no Activity");
  671. }
  672. }
  673. break;
  674. case COMMAND_TEXTEDIT_HIDE:
  675. if (mTextEdit != null) {
  676. // Note: On some devices setting view to GONE creates a flicker in landscape.
  677. // Setting the View's sizes to 0 is similar to GONE but without the flicker.
  678. // The sizes will be set to useful values when the keyboard is shown again.
  679. mTextEdit.setLayoutParams(new RelativeLayout.LayoutParams(0, 0));
  680. InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
  681. imm.hideSoftInputFromWindow(mTextEdit.getWindowToken(), 0);
  682. mScreenKeyboardShown = false;
  683. mSurface.requestFocus();
  684. }
  685. break;
  686. case COMMAND_SET_KEEP_SCREEN_ON:
  687. {
  688. if (context instanceof Activity) {
  689. Window window = ((Activity) context).getWindow();
  690. if (window != null) {
  691. if ((msg.obj instanceof Integer) && ((Integer) msg.obj != 0)) {
  692. window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  693. } else {
  694. window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  695. }
  696. }
  697. }
  698. break;
  699. }
  700. default:
  701. if ((context instanceof SDLActivity) && !((SDLActivity) context).onUnhandledMessage(msg.arg1, msg.obj)) {
  702. Log.e(TAG, "error handling message, command is " + msg.arg1);
  703. }
  704. }
  705. }
  706. }
  707. // Handler for the messages
  708. Handler commandHandler = new SDLCommandHandler();
  709. // Send a message from the SDLMain thread
  710. boolean sendCommand(int command, Object data) {
  711. Message msg = commandHandler.obtainMessage();
  712. msg.arg1 = command;
  713. msg.obj = data;
  714. boolean result = commandHandler.sendMessage(msg);
  715. if (Build.VERSION.SDK_INT >= 19) {
  716. if (command == COMMAND_CHANGE_WINDOW_STYLE) {
  717. // Ensure we don't return until the resize has actually happened,
  718. // or 500ms have passed.
  719. boolean bShouldWait = false;
  720. if (data instanceof Integer) {
  721. // Let's figure out if we're already laid out fullscreen or not.
  722. Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
  723. DisplayMetrics realMetrics = new DisplayMetrics();
  724. display.getRealMetrics(realMetrics);
  725. boolean bFullscreenLayout = ((realMetrics.widthPixels == mSurface.getWidth()) &&
  726. (realMetrics.heightPixels == mSurface.getHeight()));
  727. if ((Integer) data == 1) {
  728. // If we aren't laid out fullscreen or actively in fullscreen mode already, we're going
  729. // to change size and should wait for surfaceChanged() before we return, so the size
  730. // is right back in native code. If we're already laid out fullscreen, though, we're
  731. // not going to change size even if we change decor modes, so we shouldn't wait for
  732. // surfaceChanged() -- which may not even happen -- and should return immediately.
  733. bShouldWait = !bFullscreenLayout;
  734. } else {
  735. // If we're laid out fullscreen (even if the status bar and nav bar are present),
  736. // or are actively in fullscreen, we're going to change size and should wait for
  737. // surfaceChanged before we return, so the size is right back in native code.
  738. bShouldWait = bFullscreenLayout;
  739. }
  740. }
  741. if (bShouldWait && (SDLActivity.getContext() != null)) {
  742. // We'll wait for the surfaceChanged() method, which will notify us
  743. // when called. That way, we know our current size is really the
  744. // size we need, instead of grabbing a size that's still got
  745. // the navigation and/or status bars before they're hidden.
  746. //
  747. // We'll wait for up to half a second, because some devices
  748. // take a surprisingly long time for the surface resize, but
  749. // then we'll just give up and return.
  750. //
  751. synchronized (SDLActivity.getContext()) {
  752. try {
  753. SDLActivity.getContext().wait(500);
  754. } catch (InterruptedException ie) {
  755. ie.printStackTrace();
  756. }
  757. }
  758. }
  759. }
  760. }
  761. return result;
  762. }
  763. // C functions we call
  764. public static native String nativeGetVersion();
  765. public static native int nativeSetupJNI();
  766. public static native int nativeRunMain(String library, String function, Object arguments);
  767. public static native void nativeLowMemory();
  768. public static native void nativeSendQuit();
  769. public static native void nativeQuit();
  770. public static native void nativePause();
  771. public static native void nativeResume();
  772. public static native void nativeFocusChanged(boolean hasFocus);
  773. public static native void onNativeDropFile(String filename);
  774. public static native void nativeSetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float rate);
  775. public static native void onNativeResize();
  776. public static native void onNativeKeyDown(int keycode);
  777. public static native void onNativeKeyUp(int keycode);
  778. public static native boolean onNativeSoftReturnKey();
  779. public static native void onNativeKeyboardFocusLost();
  780. public static native void onNativeMouse(int button, int action, float x, float y, boolean relative);
  781. public static native void onNativeTouch(int touchDevId, int pointerFingerId,
  782. int action, float x,
  783. float y, float p);
  784. public static native void onNativeAccel(float x, float y, float z);
  785. public static native void onNativeClipboardChanged();
  786. public static native void onNativeSurfaceCreated();
  787. public static native void onNativeSurfaceChanged();
  788. public static native void onNativeSurfaceDestroyed();
  789. public static native String nativeGetHint(String name);
  790. public static native boolean nativeGetHintBoolean(String name, boolean default_value);
  791. public static native void nativeSetenv(String name, String value);
  792. public static native void onNativeOrientationChanged(int orientation);
  793. public static native void nativeAddTouch(int touchId, String name);
  794. public static native void nativePermissionResult(int requestCode, boolean result);
  795. public static native void onNativeLocaleChanged();
  796. /**
  797. * This method is called by SDL using JNI.
  798. */
  799. public static boolean setActivityTitle(String title) {
  800. // Called from SDLMain() thread and can't directly affect the view
  801. return mSingleton.sendCommand(COMMAND_CHANGE_TITLE, title);
  802. }
  803. /**
  804. * This method is called by SDL using JNI.
  805. */
  806. public static void setWindowStyle(boolean fullscreen) {
  807. // Called from SDLMain() thread and can't directly affect the view
  808. mSingleton.sendCommand(COMMAND_CHANGE_WINDOW_STYLE, fullscreen ? 1 : 0);
  809. }
  810. /**
  811. * This method is called by SDL using JNI.
  812. * This is a static method for JNI convenience, it calls a non-static method
  813. * so that is can be overridden
  814. */
  815. public static void setOrientation(int w, int h, boolean resizable, String hint)
  816. {
  817. if (mSingleton != null) {
  818. mSingleton.setOrientationBis(w, h, resizable, hint);
  819. }
  820. }
  821. /**
  822. * This can be overridden
  823. */
  824. public void setOrientationBis(int w, int h, boolean resizable, String hint)
  825. {
  826. int orientation_landscape = -1;
  827. int orientation_portrait = -1;
  828. /* If set, hint "explicitly controls which UI orientations are allowed". */
  829. if (hint.contains("LandscapeRight") && hint.contains("LandscapeLeft")) {
  830. orientation_landscape = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
  831. } else if (hint.contains("LandscapeRight")) {
  832. orientation_landscape = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
  833. } else if (hint.contains("LandscapeLeft")) {
  834. orientation_landscape = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
  835. }
  836. if (hint.contains("Portrait") && hint.contains("PortraitUpsideDown")) {
  837. orientation_portrait = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
  838. } else if (hint.contains("Portrait")) {
  839. orientation_portrait = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
  840. } else if (hint.contains("PortraitUpsideDown")) {
  841. orientation_portrait = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
  842. }
  843. boolean is_landscape_allowed = (orientation_landscape != -1);
  844. boolean is_portrait_allowed = (orientation_portrait != -1);
  845. int req; /* Requested orientation */
  846. /* No valid hint, nothing is explicitly allowed */
  847. if (!is_portrait_allowed && !is_landscape_allowed) {
  848. if (resizable) {
  849. /* All orientations are allowed */
  850. req = ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR;
  851. } else {
  852. /* Fixed window and nothing specified. Get orientation from w/h of created window */
  853. req = (w > h ? ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
  854. }
  855. } else {
  856. /* At least one orientation is allowed */
  857. if (resizable) {
  858. if (is_portrait_allowed && is_landscape_allowed) {
  859. /* hint allows both landscape and portrait, promote to full sensor */
  860. req = ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR;
  861. } else {
  862. /* Use the only one allowed "orientation" */
  863. req = (is_landscape_allowed ? orientation_landscape : orientation_portrait);
  864. }
  865. } else {
  866. /* Fixed window and both orientations are allowed. Choose one. */
  867. if (is_portrait_allowed && is_landscape_allowed) {
  868. req = (w > h ? orientation_landscape : orientation_portrait);
  869. } else {
  870. /* Use the only one allowed "orientation" */
  871. req = (is_landscape_allowed ? orientation_landscape : orientation_portrait);
  872. }
  873. }
  874. }
  875. Log.v(TAG, "setOrientation() requestedOrientation=" + req + " width=" + w +" height="+ h +" resizable=" + resizable + " hint=" + hint);
  876. mSingleton.setRequestedOrientation(req);
  877. }
  878. /**
  879. * This method is called by SDL using JNI.
  880. */
  881. public static void minimizeWindow() {
  882. if (mSingleton == null) {
  883. return;
  884. }
  885. Intent startMain = new Intent(Intent.ACTION_MAIN);
  886. startMain.addCategory(Intent.CATEGORY_HOME);
  887. startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  888. mSingleton.startActivity(startMain);
  889. }
  890. /**
  891. * This method is called by SDL using JNI.
  892. */
  893. public static boolean shouldMinimizeOnFocusLoss() {
  894. /*
  895. if (Build.VERSION.SDK_INT >= 24) {
  896. if (mSingleton == null) {
  897. return true;
  898. }
  899. if (mSingleton.isInMultiWindowMode()) {
  900. return false;
  901. }
  902. if (mSingleton.isInPictureInPictureMode()) {
  903. return false;
  904. }
  905. }
  906. return true;
  907. */
  908. return false;
  909. }
  910. /**
  911. * This method is called by SDL using JNI.
  912. */
  913. public static boolean isScreenKeyboardShown()
  914. {
  915. if (mTextEdit == null) {
  916. return false;
  917. }
  918. if (!mScreenKeyboardShown) {
  919. return false;
  920. }
  921. InputMethodManager imm = (InputMethodManager) SDL.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
  922. return imm.isAcceptingText();
  923. }
  924. /**
  925. * This method is called by SDL using JNI.
  926. */
  927. public static boolean supportsRelativeMouse()
  928. {
  929. // DeX mode in Samsung Experience 9.0 and earlier doesn't support relative mice properly under
  930. // Android 7 APIs, and simply returns no data under Android 8 APIs.
  931. //
  932. // This is fixed in Samsung Experience 9.5, which corresponds to Android 8.1.0, and
  933. // thus SDK version 27. If we are in DeX mode and not API 27 or higher, as a result,
  934. // we should stick to relative mode.
  935. //
  936. if ((Build.VERSION.SDK_INT < 27) && isDeXMode()) {
  937. return false;
  938. }
  939. return SDLActivity.getMotionListener().supportsRelativeMouse();
  940. }
  941. /**
  942. * This method is called by SDL using JNI.
  943. */
  944. public static boolean setRelativeMouseEnabled(boolean enabled)
  945. {
  946. if (enabled && !supportsRelativeMouse()) {
  947. return false;
  948. }
  949. return SDLActivity.getMotionListener().setRelativeMouseEnabled(enabled);
  950. }
  951. /**
  952. * This method is called by SDL using JNI.
  953. */
  954. public static boolean sendMessage(int command, int param) {
  955. if (mSingleton == null) {
  956. return false;
  957. }
  958. return mSingleton.sendCommand(command, param);
  959. }
  960. /**
  961. * This method is called by SDL using JNI.
  962. */
  963. public static Context getContext() {
  964. return SDL.getContext();
  965. }
  966. /**
  967. * This method is called by SDL using JNI.
  968. */
  969. public static boolean isAndroidTV() {
  970. UiModeManager uiModeManager = (UiModeManager) getContext().getSystemService(UI_MODE_SERVICE);
  971. if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
  972. return true;
  973. }
  974. if (Build.MANUFACTURER.equals("MINIX") && Build.MODEL.equals("NEO-U1")) {
  975. return true;
  976. }
  977. if (Build.MANUFACTURER.equals("Amlogic") && Build.MODEL.equals("X96-W")) {
  978. return true;
  979. }
  980. return Build.MANUFACTURER.equals("Amlogic") && Build.MODEL.startsWith("TV");
  981. }
  982. public static double getDiagonal()
  983. {
  984. DisplayMetrics metrics = new DisplayMetrics();
  985. Activity activity = (Activity)getContext();
  986. if (activity == null) {
  987. return 0.0;
  988. }
  989. activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
  990. double dWidthInches = metrics.widthPixels / (double)metrics.xdpi;
  991. double dHeightInches = metrics.heightPixels / (double)metrics.ydpi;
  992. return Math.sqrt((dWidthInches * dWidthInches) + (dHeightInches * dHeightInches));
  993. }
  994. /**
  995. * This method is called by SDL using JNI.
  996. */
  997. public static boolean isTablet() {
  998. // If our diagonal size is seven inches or greater, we consider ourselves a tablet.
  999. return (getDiagonal() >= 7.0);
  1000. }
  1001. /**
  1002. * This method is called by SDL using JNI.
  1003. */
  1004. public static boolean isChromebook() {
  1005. if (getContext() == null) {
  1006. return false;
  1007. }
  1008. return getContext().getPackageManager().hasSystemFeature("org.chromium.arc.device_management");
  1009. }
  1010. /**
  1011. * This method is called by SDL using JNI.
  1012. */
  1013. public static boolean isDeXMode() {
  1014. if (Build.VERSION.SDK_INT < 24) {
  1015. return false;
  1016. }
  1017. try {
  1018. final Configuration config = getContext().getResources().getConfiguration();
  1019. final Class<?> configClass = config.getClass();
  1020. return configClass.getField("SEM_DESKTOP_MODE_ENABLED").getInt(configClass)
  1021. == configClass.getField("semDesktopModeEnabled").getInt(config);
  1022. } catch(Exception ignored) {
  1023. return false;
  1024. }
  1025. }
  1026. /**
  1027. * This method is called by SDL using JNI.
  1028. */
  1029. public static DisplayMetrics getDisplayDPI() {
  1030. return getContext().getResources().getDisplayMetrics();
  1031. }
  1032. /**
  1033. * This method is called by SDL using JNI.
  1034. */
  1035. public static boolean getManifestEnvironmentVariables() {
  1036. try {
  1037. if (getContext() == null) {
  1038. return false;
  1039. }
  1040. ApplicationInfo applicationInfo = getContext().getPackageManager().getApplicationInfo(getContext().getPackageName(), PackageManager.GET_META_DATA);
  1041. Bundle bundle = applicationInfo.metaData;
  1042. if (bundle == null) {
  1043. return false;
  1044. }
  1045. String prefix = "SDL_ENV.";
  1046. final int trimLength = prefix.length();
  1047. for (String key : bundle.keySet()) {
  1048. if (key.startsWith(prefix)) {
  1049. String name = key.substring(trimLength);
  1050. String value = bundle.get(key).toString();
  1051. nativeSetenv(name, value);
  1052. }
  1053. }
  1054. /* environment variables set! */
  1055. return true;
  1056. } catch (Exception e) {
  1057. Log.v(TAG, "exception " + e.toString());
  1058. }
  1059. return false;
  1060. }
  1061. // This method is called by SDLControllerManager's API 26 Generic Motion Handler.
  1062. public static View getContentView() {
  1063. return mLayout;
  1064. }
  1065. static class ShowTextInputTask implements Runnable {
  1066. /*
  1067. * This is used to regulate the pan&scan method to have some offset from
  1068. * the bottom edge of the input region and the top edge of an input
  1069. * method (soft keyboard)
  1070. */
  1071. static final int HEIGHT_PADDING = 15;
  1072. public int x, y, w, h;
  1073. public ShowTextInputTask(int x, int y, int w, int h) {
  1074. this.x = x;
  1075. this.y = y;
  1076. this.w = w;
  1077. this.h = h;
  1078. /* Minimum size of 1 pixel, so it takes focus. */
  1079. if (this.w <= 0) {
  1080. this.w = 1;
  1081. }
  1082. if (this.h + HEIGHT_PADDING <= 0) {
  1083. this.h = 1 - HEIGHT_PADDING;
  1084. }
  1085. }
  1086. @Override
  1087. public void run() {
  1088. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(w, h + HEIGHT_PADDING);
  1089. params.leftMargin = x;
  1090. params.topMargin = y;
  1091. if (mTextEdit == null) {
  1092. mTextEdit = new DummyEdit(SDL.getContext());
  1093. mLayout.addView(mTextEdit, params);
  1094. } else {
  1095. mTextEdit.setLayoutParams(params);
  1096. }
  1097. mTextEdit.setVisibility(View.VISIBLE);
  1098. mTextEdit.requestFocus();
  1099. InputMethodManager imm = (InputMethodManager) SDL.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
  1100. imm.showSoftInput(mTextEdit, 0);
  1101. mScreenKeyboardShown = true;
  1102. }
  1103. }
  1104. /**
  1105. * This method is called by SDL using JNI.
  1106. */
  1107. public static boolean showTextInput(int x, int y, int w, int h) {
  1108. // Transfer the task to the main thread as a Runnable
  1109. return mSingleton.commandHandler.post(new ShowTextInputTask(x, y, w, h));
  1110. }
  1111. public static boolean isTextInputEvent(KeyEvent event) {
  1112. // Key pressed with Ctrl should be sent as SDL_KEYDOWN/SDL_KEYUP and not SDL_TEXTINPUT
  1113. if (event.isCtrlPressed()) {
  1114. return false;
  1115. }
  1116. return event.isPrintingKey() || event.getKeyCode() == KeyEvent.KEYCODE_SPACE;
  1117. }
  1118. public static boolean handleKeyEvent(View v, int keyCode, KeyEvent event, InputConnection ic) {
  1119. int deviceId = event.getDeviceId();
  1120. int source = event.getSource();
  1121. if (source == InputDevice.SOURCE_UNKNOWN) {
  1122. InputDevice device = InputDevice.getDevice(deviceId);
  1123. if (device != null) {
  1124. source = device.getSources();
  1125. }
  1126. }
  1127. // if (event.getAction() == KeyEvent.ACTION_DOWN) {
  1128. // Log.v("SDL", "key down: " + keyCode + ", deviceId = " + deviceId + ", source = " + source);
  1129. // } else if (event.getAction() == KeyEvent.ACTION_UP) {
  1130. // Log.v("SDL", "key up: " + keyCode + ", deviceId = " + deviceId + ", source = " + source);
  1131. // }
  1132. // Dispatch the different events depending on where they come from
  1133. // Some SOURCE_JOYSTICK, SOURCE_DPAD or SOURCE_GAMEPAD are also SOURCE_KEYBOARD
  1134. // So, we try to process them as JOYSTICK/DPAD/GAMEPAD events first, if that fails we try them as KEYBOARD
  1135. //
  1136. // Furthermore, it's possible a game controller has SOURCE_KEYBOARD and
  1137. // SOURCE_JOYSTICK, while its key events arrive from the keyboard source
  1138. // So, retrieve the device itself and check all of its sources
  1139. if (SDLControllerManager.isDeviceSDLJoystick(deviceId)) {
  1140. // Note that we process events with specific key codes here
  1141. if (event.getAction() == KeyEvent.ACTION_DOWN) {
  1142. if (SDLControllerManager.onNativePadDown(deviceId, keyCode) == 0) {
  1143. return true;
  1144. }
  1145. } else if (event.getAction() == KeyEvent.ACTION_UP) {
  1146. if (SDLControllerManager.onNativePadUp(deviceId, keyCode) == 0) {
  1147. return true;
  1148. }
  1149. }
  1150. }
  1151. if ((source & InputDevice.SOURCE_KEYBOARD) == InputDevice.SOURCE_KEYBOARD) {
  1152. if (event.getAction() == KeyEvent.ACTION_DOWN) {
  1153. if (isTextInputEvent(event)) {
  1154. if (ic != null) {
  1155. ic.commitText(String.valueOf((char) event.getUnicodeChar()), 1);
  1156. } else {
  1157. SDLInputConnection.nativeCommitText(String.valueOf((char) event.getUnicodeChar()), 1);
  1158. }
  1159. }
  1160. onNativeKeyDown(keyCode);
  1161. return true;
  1162. } else if (event.getAction() == KeyEvent.ACTION_UP) {
  1163. onNativeKeyUp(keyCode);
  1164. return true;
  1165. }
  1166. }
  1167. if ((source & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE) {
  1168. // on some devices key events are sent for mouse BUTTON_BACK/FORWARD presses
  1169. // they are ignored here because sending them as mouse input to SDL is messy
  1170. if ((keyCode == KeyEvent.KEYCODE_BACK) || (keyCode == KeyEvent.KEYCODE_FORWARD)) {
  1171. switch (event.getAction()) {
  1172. case KeyEvent.ACTION_DOWN:
  1173. case KeyEvent.ACTION_UP:
  1174. // mark the event as handled or it will be handled by system
  1175. // handling KEYCODE_BACK by system will call onBackPressed()
  1176. return true;
  1177. }
  1178. }
  1179. }
  1180. return false;
  1181. }
  1182. /**
  1183. * This method is called by SDL using JNI.
  1184. */
  1185. public static Surface getNativeSurface() {
  1186. if (SDLActivity.mSurface == null) {
  1187. return null;
  1188. }
  1189. return SDLActivity.mSurface.getNativeSurface();
  1190. }
  1191. // Input
  1192. /**
  1193. * This method is called by SDL using JNI.
  1194. */
  1195. public static void initTouch() {
  1196. int[] ids = InputDevice.getDeviceIds();
  1197. for (int id : ids) {
  1198. InputDevice device = InputDevice.getDevice(id);
  1199. /* Allow SOURCE_TOUCHSCREEN and also Virtual InputDevices because they can send TOUCHSCREEN events */
  1200. if (device != null && ((device.getSources() & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN
  1201. || device.isVirtual())) {
  1202. int touchDevId = device.getId();
  1203. /*
  1204. * Prevent id to be -1, since it's used in SDL internal for synthetic events
  1205. * Appears when using Android emulator, eg:
  1206. * adb shell input mouse tap 100 100
  1207. * adb shell input touchscreen tap 100 100
  1208. */
  1209. if (touchDevId < 0) {
  1210. touchDevId -= 1;
  1211. }
  1212. nativeAddTouch(touchDevId, device.getName());
  1213. }
  1214. }
  1215. }
  1216. // Messagebox
  1217. /** Result of current messagebox. Also used for blocking the calling thread. */
  1218. protected final int[] messageboxSelection = new int[1];
  1219. /**
  1220. * This method is called by SDL using JNI.
  1221. * Shows the messagebox from UI thread and block calling thread.
  1222. * buttonFlags, buttonIds and buttonTexts must have same length.
  1223. * @param buttonFlags array containing flags for every button.
  1224. * @param buttonIds array containing id for every button.
  1225. * @param buttonTexts array containing text for every button.
  1226. * @param colors null for default or array of length 5 containing colors.
  1227. * @return button id or -1.
  1228. */
  1229. public int messageboxShowMessageBox(
  1230. final int flags,
  1231. final String title,
  1232. final String message,
  1233. final int[] buttonFlags,
  1234. final int[] buttonIds,
  1235. final String[] buttonTexts,
  1236. final int[] colors) {
  1237. messageboxSelection[0] = -1;
  1238. // sanity checks
  1239. if ((buttonFlags.length != buttonIds.length) && (buttonIds.length != buttonTexts.length)) {
  1240. return -1; // implementation broken
  1241. }
  1242. // collect arguments for Dialog
  1243. final Bundle args = new Bundle();
  1244. args.putInt("flags", flags);
  1245. args.putString("title", title);
  1246. args.putString("message", message);
  1247. args.putIntArray("buttonFlags", buttonFlags);
  1248. args.putIntArray("buttonIds", buttonIds);
  1249. args.putStringArray("buttonTexts", buttonTexts);
  1250. args.putIntArray("colors", colors);
  1251. // trigger Dialog creation on UI thread
  1252. runOnUiThread(new Runnable() {
  1253. @Override
  1254. public void run() {
  1255. messageboxCreateAndShow(args);
  1256. }
  1257. });
  1258. // block the calling thread
  1259. synchronized (messageboxSelection) {
  1260. try {
  1261. messageboxSelection.wait();
  1262. } catch (InterruptedException ex) {
  1263. ex.printStackTrace();
  1264. return -1;
  1265. }
  1266. }
  1267. // return selected value
  1268. return messageboxSelection[0];
  1269. }
  1270. protected void messageboxCreateAndShow(Bundle args) {
  1271. // TODO set values from "flags" to messagebox dialog
  1272. // get colors
  1273. int[] colors = args.getIntArray("colors");
  1274. int backgroundColor;
  1275. int textColor;
  1276. int buttonBorderColor;
  1277. int buttonBackgroundColor;
  1278. int buttonSelectedColor;
  1279. if (colors != null) {
  1280. int i = -1;
  1281. backgroundColor = colors[++i];
  1282. textColor = colors[++i];
  1283. buttonBorderColor = colors[++i];
  1284. buttonBackgroundColor = colors[++i];
  1285. buttonSelectedColor = colors[++i];
  1286. } else {
  1287. backgroundColor = Color.TRANSPARENT;
  1288. textColor = Color.TRANSPARENT;
  1289. buttonBorderColor = Color.TRANSPARENT;
  1290. buttonBackgroundColor = Color.TRANSPARENT;
  1291. buttonSelectedColor = Color.TRANSPARENT;
  1292. }
  1293. // create dialog with title and a listener to wake up calling thread
  1294. final AlertDialog dialog = new AlertDialog.Builder(this).create();
  1295. dialog.setTitle(args.getString("title"));
  1296. dialog.setCancelable(false);
  1297. dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
  1298. @Override
  1299. public void onDismiss(DialogInterface unused) {
  1300. synchronized (messageboxSelection) {
  1301. messageboxSelection.notify();
  1302. }
  1303. }
  1304. });
  1305. // create text
  1306. TextView message = new TextView(this);
  1307. message.setGravity(Gravity.CENTER);
  1308. message.setText(args.getString("message"));
  1309. if (textColor != Color.TRANSPARENT) {
  1310. message.setTextColor(textColor);
  1311. }
  1312. // create buttons
  1313. int[] buttonFlags = args.getIntArray("buttonFlags");
  1314. int[] buttonIds = args.getIntArray("buttonIds");
  1315. String[] buttonTexts = args.getStringArray("buttonTexts");
  1316. final SparseArray<Button> mapping = new SparseArray<Button>();
  1317. LinearLayout buttons = new LinearLayout(this);
  1318. buttons.setOrientation(LinearLayout.HORIZONTAL);
  1319. buttons.setGravity(Gravity.CENTER);
  1320. for (int i = 0; i < buttonTexts.length; ++i) {
  1321. Button button = new Button(this);
  1322. final int id = buttonIds[i];
  1323. button.setOnClickListener(new View.OnClickListener() {
  1324. @Override
  1325. public void onClick(View v) {
  1326. messageboxSelection[0] = id;
  1327. dialog.dismiss();
  1328. }
  1329. });
  1330. if (buttonFlags[i] != 0) {
  1331. // see SDL_messagebox.h
  1332. if ((buttonFlags[i] & 0x00000001) != 0) {
  1333. mapping.put(KeyEvent.KEYCODE_ENTER, button);
  1334. }
  1335. if ((buttonFlags[i] & 0x00000002) != 0) {
  1336. mapping.put(KeyEvent.KEYCODE_ESCAPE, button); /* API 11 */
  1337. }
  1338. }
  1339. button.setText(buttonTexts[i]);
  1340. if (textColor != Color.TRANSPARENT) {
  1341. button.setTextColor(textColor);
  1342. }
  1343. if (buttonBorderColor != Color.TRANSPARENT) {
  1344. // TODO set color for border of messagebox button
  1345. }
  1346. if (buttonBackgroundColor != Color.TRANSPARENT) {
  1347. Drawable drawable = button.getBackground();
  1348. if (drawable == null) {
  1349. // setting the color this way removes the style
  1350. button.setBackgroundColor(buttonBackgroundColor);
  1351. } else {
  1352. // setting the color this way keeps the style (gradient, padding, etc.)
  1353. drawable.setColorFilter(buttonBackgroundColor, PorterDuff.Mode.MULTIPLY);
  1354. }
  1355. }
  1356. if (buttonSelectedColor != Color.TRANSPARENT) {
  1357. // TODO set color for selected messagebox button
  1358. }
  1359. buttons.addView(button);
  1360. }
  1361. // create content
  1362. LinearLayout content = new LinearLayout(this);
  1363. content.setOrientation(LinearLayout.VERTICAL);
  1364. content.addView(message);
  1365. content.addView(buttons);
  1366. if (backgroundColor != Color.TRANSPARENT) {
  1367. content.setBackgroundColor(backgroundColor);
  1368. }
  1369. // add content to dialog and return
  1370. dialog.setView(content);
  1371. dialog.setOnKeyListener(new Dialog.OnKeyListener() {
  1372. @Override
  1373. public boolean onKey(DialogInterface d, int keyCode, KeyEvent event) {
  1374. Button button = mapping.get(keyCode);
  1375. if (button != null) {
  1376. if (event.getAction() == KeyEvent.ACTION_UP) {
  1377. button.performClick();
  1378. }
  1379. return true; // also for ignored actions
  1380. }
  1381. return false;
  1382. }
  1383. });
  1384. dialog.show();
  1385. }
  1386. private final Runnable rehideSystemUi = new Runnable() {
  1387. @Override
  1388. public void run() {
  1389. if (Build.VERSION.SDK_INT >= 19) {
  1390. int flags = View.SYSTEM_UI_FLAG_FULLSCREEN |
  1391. View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
  1392. View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
  1393. View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
  1394. View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
  1395. View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.INVISIBLE;
  1396. SDLActivity.this.getWindow().getDecorView().setSystemUiVisibility(flags);
  1397. }
  1398. }
  1399. };
  1400. public void onSystemUiVisibilityChange(int visibility) {
  1401. if (SDLActivity.mFullscreenModeActive && ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0 || (visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0)) {
  1402. Handler handler = getWindow().getDecorView().getHandler();
  1403. if (handler != null) {
  1404. handler.removeCallbacks(rehideSystemUi); // Prevent a hide loop.
  1405. handler.postDelayed(rehideSystemUi, 2000);
  1406. }
  1407. }
  1408. }
  1409. /**
  1410. * This method is called by SDL using JNI.
  1411. */
  1412. public static boolean clipboardHasText() {
  1413. return mClipboardHandler.clipboardHasText();
  1414. }
  1415. /**
  1416. * This method is called by SDL using JNI.
  1417. */
  1418. public static String clipboardGetText() {
  1419. return mClipboardHandler.clipboardGetText();
  1420. }
  1421. /**
  1422. * This method is called by SDL using JNI.
  1423. */
  1424. public static void clipboardSetText(String string) {
  1425. mClipboardHandler.clipboardSetText(string);
  1426. }
  1427. /**
  1428. * This method is called by SDL using JNI.
  1429. */
  1430. public static int createCustomCursor(int[] colors, int width, int height, int hotSpotX, int hotSpotY) {
  1431. Bitmap bitmap = Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888);
  1432. ++mLastCursorID;
  1433. if (Build.VERSION.SDK_INT >= 24) {
  1434. try {
  1435. mCursors.put(mLastCursorID, PointerIcon.create(bitmap, hotSpotX, hotSpotY));
  1436. } catch (Exception e) {
  1437. return 0;
  1438. }
  1439. } else {
  1440. return 0;
  1441. }
  1442. return mLastCursorID;
  1443. }
  1444. /**
  1445. * This method is called by SDL using JNI.
  1446. */
  1447. public static void destroyCustomCursor(int cursorID) {
  1448. if (Build.VERSION.SDK_INT >= 24) {
  1449. try {
  1450. mCursors.remove(cursorID);
  1451. } catch (Exception e) {
  1452. }
  1453. }
  1454. return;
  1455. }
  1456. /**
  1457. * This method is called by SDL using JNI.
  1458. */
  1459. public static boolean setCustomCursor(int cursorID) {
  1460. if (Build.VERSION.SDK_INT >= 24) {
  1461. try {
  1462. mSurface.setPointerIcon(mCursors.get(cursorID));
  1463. } catch (Exception e) {
  1464. return false;
  1465. }
  1466. } else {
  1467. return false;
  1468. }
  1469. return true;
  1470. }
  1471. /**
  1472. * This method is called by SDL using JNI.
  1473. */
  1474. public static boolean setSystemCursor(int cursorID) {
  1475. int cursor_type = 0; //PointerIcon.TYPE_NULL;
  1476. switch (cursorID) {
  1477. case SDL_SYSTEM_CURSOR_ARROW:
  1478. cursor_type = 1000; //PointerIcon.TYPE_ARROW;
  1479. break;
  1480. case SDL_SYSTEM_CURSOR_IBEAM:
  1481. cursor_type = 1008; //PointerIcon.TYPE_TEXT;
  1482. break;
  1483. case SDL_SYSTEM_CURSOR_WAIT:
  1484. cursor_type = 1004; //PointerIcon.TYPE_WAIT;
  1485. break;
  1486. case SDL_SYSTEM_CURSOR_CROSSHAIR:
  1487. cursor_type = 1007; //PointerIcon.TYPE_CROSSHAIR;
  1488. break;
  1489. case SDL_SYSTEM_CURSOR_WAITARROW:
  1490. cursor_type = 1004; //PointerIcon.TYPE_WAIT;
  1491. break;
  1492. case SDL_SYSTEM_CURSOR_SIZENWSE:
  1493. cursor_type = 1017; //PointerIcon.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW;
  1494. break;
  1495. case SDL_SYSTEM_CURSOR_SIZENESW:
  1496. cursor_type = 1016; //PointerIcon.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW;
  1497. break;
  1498. case SDL_SYSTEM_CURSOR_SIZEWE:
  1499. cursor_type = 1014; //PointerIcon.TYPE_HORIZONTAL_DOUBLE_ARROW;
  1500. break;
  1501. case SDL_SYSTEM_CURSOR_SIZENS:
  1502. cursor_type = 1015; //PointerIcon.TYPE_VERTICAL_DOUBLE_ARROW;
  1503. break;
  1504. case SDL_SYSTEM_CURSOR_SIZEALL:
  1505. cursor_type = 1020; //PointerIcon.TYPE_GRAB;
  1506. break;
  1507. case SDL_SYSTEM_CURSOR_NO:
  1508. cursor_type = 1012; //PointerIcon.TYPE_NO_DROP;
  1509. break;
  1510. case SDL_SYSTEM_CURSOR_HAND:
  1511. cursor_type = 1002; //PointerIcon.TYPE_HAND;
  1512. break;
  1513. }
  1514. if (Build.VERSION.SDK_INT >= 24) {
  1515. try {
  1516. mSurface.setPointerIcon(PointerIcon.getSystemIcon(SDL.getContext(), cursor_type));
  1517. } catch (Exception e) {
  1518. return false;
  1519. }
  1520. }
  1521. return true;
  1522. }
  1523. /**
  1524. * This method is called by SDL using JNI.
  1525. */
  1526. public static void requestPermission(String permission, int requestCode) {
  1527. if (Build.VERSION.SDK_INT < 23) {
  1528. nativePermissionResult(requestCode, true);
  1529. return;
  1530. }
  1531. Activity activity = (Activity)getContext();
  1532. if (activity.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
  1533. activity.requestPermissions(new String[]{permission}, requestCode);
  1534. } else {
  1535. nativePermissionResult(requestCode, true);
  1536. }
  1537. }
  1538. @Override
  1539. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  1540. boolean result = (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED);
  1541. nativePermissionResult(requestCode, result);
  1542. }
  1543. /**
  1544. * This method is called by SDL using JNI.
  1545. */
  1546. public static int openURL(String url)
  1547. {
  1548. try {
  1549. Intent i = new Intent(Intent.ACTION_VIEW);
  1550. i.setData(Uri.parse(url));
  1551. int flags = Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
  1552. if (Build.VERSION.SDK_INT >= 21) {
  1553. flags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
  1554. } else {
  1555. flags |= Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET;
  1556. }
  1557. i.addFlags(flags);
  1558. mSingleton.startActivity(i);
  1559. } catch (Exception ex) {
  1560. return -1;
  1561. }
  1562. return 0;
  1563. }
  1564. /**
  1565. * This method is called by SDL using JNI.
  1566. */
  1567. public static int showToast(String message, int duration, int gravity, int xOffset, int yOffset)
  1568. {
  1569. if(null == mSingleton) {
  1570. return - 1;
  1571. }
  1572. try
  1573. {
  1574. class OneShotTask implements Runnable {
  1575. String mMessage;
  1576. int mDuration;
  1577. int mGravity;
  1578. int mXOffset;
  1579. int mYOffset;
  1580. OneShotTask(String message, int duration, int gravity, int xOffset, int yOffset) {
  1581. mMessage = message;
  1582. mDuration = duration;
  1583. mGravity = gravity;
  1584. mXOffset = xOffset;
  1585. mYOffset = yOffset;
  1586. }
  1587. public void run() {
  1588. try
  1589. {
  1590. Toast toast = Toast.makeText(mSingleton, mMessage, mDuration);
  1591. if (mGravity >= 0) {
  1592. toast.setGravity(mGravity, mXOffset, mYOffset);
  1593. }
  1594. toast.show();
  1595. } catch(Exception ex) {
  1596. Log.e(TAG, ex.getMessage());
  1597. }
  1598. }
  1599. }
  1600. mSingleton.runOnUiThread(new OneShotTask(message, duration, gravity, xOffset, yOffset));
  1601. } catch(Exception ex) {
  1602. return -1;
  1603. }
  1604. return 0;
  1605. }
  1606. }
  1607. /**
  1608. Simple runnable to start the SDL application
  1609. */
  1610. class SDLMain implements Runnable {
  1611. @Override
  1612. public void run() {
  1613. // Runs SDL_main()
  1614. String library = SDLActivity.mSingleton.getMainSharedObject();
  1615. String function = SDLActivity.mSingleton.getMainFunction();
  1616. String[] arguments = SDLActivity.mSingleton.getArguments();
  1617. try {
  1618. android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_DISPLAY);
  1619. } catch (Exception e) {
  1620. Log.v("SDL", "modify thread properties failed " + e.toString());
  1621. }
  1622. Log.v("SDL", "Running main function " + function + " from library " + library);
  1623. SDLActivity.nativeRunMain(library, function, arguments);
  1624. Log.v("SDL", "Finished main function");
  1625. if (SDLActivity.mSingleton != null && !SDLActivity.mSingleton.isFinishing()) {
  1626. // Let's finish the Activity
  1627. SDLActivity.mSDLThread = null;
  1628. SDLActivity.mSingleton.finish();
  1629. } // else: Activity is already being destroyed
  1630. }
  1631. }
  1632. /* This is a fake invisible editor view that receives the input and defines the
  1633. * pan&scan region
  1634. */
  1635. class DummyEdit extends View implements View.OnKeyListener {
  1636. InputConnection ic;
  1637. public DummyEdit(Context context) {
  1638. super(context);
  1639. setFocusableInTouchMode(true);
  1640. setFocusable(true);
  1641. setOnKeyListener(this);
  1642. }
  1643. @Override
  1644. public boolean onCheckIsTextEditor() {
  1645. return true;
  1646. }
  1647. @Override
  1648. public boolean onKey(View v, int keyCode, KeyEvent event) {
  1649. return SDLActivity.handleKeyEvent(v, keyCode, event, ic);
  1650. }
  1651. //
  1652. @Override
  1653. public boolean onKeyPreIme (int keyCode, KeyEvent event) {
  1654. // As seen on StackOverflow: http://stackoverflow.com/questions/7634346/keyboard-hide-event
  1655. // FIXME: Discussion at http://bugzilla.libsdl.org/show_bug.cgi?id=1639
  1656. // FIXME: This is not a 100% effective solution to the problem of detecting if the keyboard is showing or not
  1657. // FIXME: A more effective solution would be to assume our Layout to be RelativeLayout or LinearLayout
  1658. // FIXME: And determine the keyboard presence doing this: http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android
  1659. // FIXME: An even more effective way would be if Android provided this out of the box, but where would the fun be in that :)
  1660. if (event.getAction()==KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
  1661. if (SDLActivity.mTextEdit != null && SDLActivity.mTextEdit.getVisibility() == View.VISIBLE) {
  1662. SDLActivity.onNativeKeyboardFocusLost();
  1663. }
  1664. }
  1665. return super.onKeyPreIme(keyCode, event);
  1666. }
  1667. @Override
  1668. public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
  1669. ic = new SDLInputConnection(this, true);
  1670. outAttrs.inputType = InputType.TYPE_CLASS_TEXT |
  1671. InputType.TYPE_TEXT_FLAG_MULTI_LINE;
  1672. outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI |
  1673. EditorInfo.IME_FLAG_NO_FULLSCREEN /* API 11 */;
  1674. return ic;
  1675. }
  1676. }
  1677. class SDLInputConnection extends BaseInputConnection {
  1678. protected EditText mEditText;
  1679. protected String mCommittedText = "";
  1680. public SDLInputConnection(View targetView, boolean fullEditor) {
  1681. super(targetView, fullEditor);
  1682. mEditText = new EditText(SDL.getContext());
  1683. }
  1684. @Override
  1685. public Editable getEditable() {
  1686. return mEditText.getEditableText();
  1687. }
  1688. @Override
  1689. public boolean sendKeyEvent(KeyEvent event) {
  1690. /*
  1691. * This used to handle the keycodes from soft keyboard (and IME-translated input from hardkeyboard)
  1692. * However, as of Ice Cream Sandwich and later, almost all soft keyboard doesn't generate key presses
  1693. * and so we need to generate them ourselves in commitText. To avoid duplicates on the handful of keys
  1694. * that still do, we empty this out.
  1695. */
  1696. /*
  1697. * Return DOES still generate a key event, however. So rather than using it as the 'click a button' key
  1698. * as we do with physical keyboards, let's just use it to hide the keyboard.
  1699. */
  1700. if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
  1701. if (SDLActivity.onNativeSoftReturnKey()) {
  1702. return true;
  1703. }
  1704. }
  1705. return super.sendKeyEvent(event);
  1706. }
  1707. @Override
  1708. public boolean commitText(CharSequence text, int newCursorPosition) {
  1709. if (!super.commitText(text, newCursorPosition)) {
  1710. return false;
  1711. }
  1712. updateText();
  1713. return true;
  1714. }
  1715. @Override
  1716. public boolean setComposingText(CharSequence text, int newCursorPosition) {
  1717. if (!super.setComposingText(text, newCursorPosition)) {
  1718. return false;
  1719. }
  1720. updateText();
  1721. return true;
  1722. }
  1723. @Override
  1724. public boolean deleteSurroundingText(int beforeLength, int afterLength) {
  1725. if (!super.deleteSurroundingText(beforeLength, afterLength)) {
  1726. return false;
  1727. }
  1728. updateText();
  1729. return true;
  1730. }
  1731. protected void updateText() {
  1732. final Editable content = getEditable();
  1733. if (content == null) {
  1734. return;
  1735. }
  1736. String text = content.toString();
  1737. int compareLength = Math.min(text.length(), mCommittedText.length());
  1738. int matchLength, offset;
  1739. /* Backspace over characters that are no longer in the string */
  1740. for (matchLength = 0; matchLength < compareLength; ) {
  1741. int codePoint = mCommittedText.codePointAt(matchLength);
  1742. if (codePoint != text.codePointAt(matchLength)) {
  1743. break;
  1744. }
  1745. matchLength += Character.charCount(codePoint);
  1746. }
  1747. /* FIXME: This doesn't handle graphemes, like '🌬️' */
  1748. for (offset = matchLength; offset < mCommittedText.length(); ) {
  1749. int codePoint = mCommittedText.codePointAt(offset);
  1750. nativeGenerateScancodeForUnichar('\b');
  1751. offset += Character.charCount(codePoint);
  1752. }
  1753. if (matchLength < text.length()) {
  1754. String pendingText = text.subSequence(matchLength, text.length()).toString();
  1755. for (offset = 0; offset < pendingText.length(); ) {
  1756. int codePoint = pendingText.codePointAt(offset);
  1757. if (codePoint == '\n') {
  1758. if (SDLActivity.onNativeSoftReturnKey()) {
  1759. return;
  1760. }
  1761. }
  1762. /* Higher code points don't generate simulated scancodes */
  1763. if (codePoint < 128) {
  1764. nativeGenerateScancodeForUnichar((char)codePoint);
  1765. }
  1766. offset += Character.charCount(codePoint);
  1767. }
  1768. SDLInputConnection.nativeCommitText(pendingText, 0);
  1769. }
  1770. mCommittedText = text;
  1771. }
  1772. public static native void nativeCommitText(String text, int newCursorPosition);
  1773. public static native void nativeGenerateScancodeForUnichar(char c);
  1774. }
  1775. class SDLClipboardHandler implements
  1776. ClipboardManager.OnPrimaryClipChangedListener {
  1777. protected ClipboardManager mClipMgr;
  1778. SDLClipboardHandler() {
  1779. mClipMgr = (ClipboardManager) SDL.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
  1780. mClipMgr.addPrimaryClipChangedListener(this);
  1781. }
  1782. public boolean clipboardHasText() {
  1783. return mClipMgr.hasPrimaryClip();
  1784. }
  1785. public String clipboardGetText() {
  1786. ClipData clip = mClipMgr.getPrimaryClip();
  1787. if (clip != null) {
  1788. ClipData.Item item = clip.getItemAt(0);
  1789. if (item != null) {
  1790. CharSequence text = item.getText();
  1791. if (text != null) {
  1792. return text.toString();
  1793. }
  1794. }
  1795. }
  1796. return null;
  1797. }
  1798. public void clipboardSetText(String string) {
  1799. mClipMgr.removePrimaryClipChangedListener(this);
  1800. ClipData clip = ClipData.newPlainText(null, string);
  1801. mClipMgr.setPrimaryClip(clip);
  1802. mClipMgr.addPrimaryClipChangedListener(this);
  1803. }
  1804. @Override
  1805. public void onPrimaryClipChanged() {
  1806. SDLActivity.onNativeClipboardChanged();
  1807. }
  1808. }