@Override protected void onPause() { Log.i("MoSync", "onPause"); super.onPause(); if (theMoSyncThreadIsDead()) { return; } mMoSyncThread.onPause(); // Moved the release hardware to the correct position seems to solve the problem. mMoSyncThread.releaseHardware(); if (nfcForegroundHandler != null) { nfcForegroundHandler.disableForeground(); } // Notify the local notifications manager that the application // has lost focus. LocalNotificationsManager.focusLost(); SYSLOG("Posting EVENT_TYPE_FOCUS_LOST to MoSync"); int[] event = new int[1]; event[0] = EVENT_TYPE_FOCUS_LOST; mMoSyncThread.postEvent(event); }
@Override protected void onResume() { Log.i("MoSync", "onResume"); super.onResume(); if (theMoSyncThreadIsDead()) { return; } mMoSyncThread.acquireHardware(); mMoSyncThread.onResume(); if (nfcForegroundHandler != null) { nfcForegroundHandler.enableForeground(); } // Notify the local notifications manager that the application // has gained focus. LocalNotificationsManager.focusGained(); SYSLOG("Posting EVENT_TYPE_FOCUS_GAINED to MoSync"); int[] event = new int[1]; event[0] = EVENT_TYPE_FOCUS_GAINED; mMoSyncThread.postEvent(event); }
/** * Sets screen and window properties. Creates and initializes the MoSync thread Calls the * createMoSyncView method and then shows the created view. */ @Override public void onCreate(Bundle savedInstanceState) { Log.i("MoSync", "MoSync onCreate"); super.onCreate(savedInstanceState); try { // If triggered by an NFC event, must handle it this way. MoSyncNFCService.handleNFCIntent(this, getIntent()); nfcForegroundHandler = MoSyncNFCForegroundUtil.create(this); } catch (Throwable t) { SYSLOG("No NFC"); } // Initialize. mMoSyncView = null; // MoSync Android apps do not have a system title bar. requestWindowFeature(Window.FEATURE_NO_TITLE); // Default screen orientation is portrait mode. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); try { // Create the thread. mMoSyncThread = new MoSyncThread(this, new Handler()); } catch (Exception ex) { MoSyncThread.logError("MoSync - Unable to create thread! " + "Application is closed!", ex); finish(); return; } try { // If triggered by a GCM/C2DM message, handle it here. // Call this after the MoSyncThread is created. if (getIntent() .getBooleanExtra(PushNotificationsUtil.MOSYNC_INTENT_EXTRA_NOTIFICATION, false)) { Log.e("@@MoSync", "MoSync activity started after a push notification was received"); PushNotificationsManager.handlePushNotificationIntent(getIntent()); } } catch (Throwable t) { SYSLOG("No GCM/C2DM message"); } // Create the view. mMoSyncView = createMoSyncView(); if (null != mMoSyncView) { mMoSyncThread.setMoSyncView(mMoSyncView); setContentView(mMoSyncView); } else { finish(); return; } registerShutdownListener(); }
@Override /** * Initialize the content of the Options menu each time Menu is pressed, based on the current * screen. */ public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); // Remove all the items from the menu. menu.clear(); // Get the focused screen widget. ScreenWidget currentScreen = mMoSyncThread.getCurrentScreen(); if (currentScreen != null) { // Get the menu items for that screen. List<OptionsMenuItem> items = currentScreen.getMenuItems(); // Add each menu item to the options menu. for (int i = 0; i < items.size(); i++) { MenuItem item = menu.add(0, items.get(i).getId(), 0, items.get(i).getTitle()); if (items.get(i).hasIconFromResources()) { item.setIcon(items.get(i).getIconResId()); } else { item.setIcon(items.get(i).getIcon()); } } return true; } return false; }
/** * Internal function for the maWidgetGetProperty system call. Gets a property on the given widget, * by accessing it from the widget table and calling its getProperty method. * * <p>Note: Should only be called on the UI thread. */ public int maWidgetGetProperty(int widgetHandle, String key, int memBuffer, int memBufferSize) { Widget widget = m_widgetTable.get(widgetHandle); if (widget == null) { Log.e("MoSync", "maWidgetGetProperty: Invalid child widget handle: " + widgetHandle); return IX_WIDGET.MAW_RES_INVALID_HANDLE; } String result; try { result = widget.getProperty(key); } catch (FeatureNotAvailableException fnae) { Log.e("MoSync", "Feature not available exception: " + fnae.getMessage()); return IX_WIDGET.MAW_RES_FEATURE_NOT_AVAILABLE; } if (result.equals(Widget.INVALID_PROPERTY_NAME)) { Log.e( "MoSync", "maWidgetGetProperty: Invalid or empty property '" + key + "' on widget: " + widgetHandle); return IX_WIDGET.MAW_RES_INVALID_PROPERTY_NAME; } if (result.length() + 1 > memBufferSize) { Log.e( "MoSync", "maWidgetGetProperty: Buffer size " + memBufferSize + " too short to hold buffer of size: " + result.length() + 1); return IX_WIDGET.MAW_RES_INVALID_STRING_BUFFER_SIZE; } byte[] ba = result.getBytes(); // Write string to MoSync memory. MoSyncThread mosyncThread = ((MoSync) m_activity).getMoSyncThread(); ByteBuffer buffer = mosyncThread.getMemorySlice(memBuffer, ba.length + 1); buffer.put(ba); buffer.put((byte) 0); return result.length(); }
/** * In case of maPanic the MoSync thread exits and is dead. In this case we should finish the * application. */ private boolean theMoSyncThreadIsDead() { if (null != mMoSyncThread && mMoSyncThread.isDead()) { MoSyncService.stopService(); finish(); return true; } else { return false; } }
/** Posts an EVENT_TYPE_CLOSE to the MoSync events queue. */ private void sendCloseEvent() { if (!mEventTypeCloseHasBeenSent) { // Send EVENT_TYPE_CLOSE int[] event = new int[1]; event[0] = EVENT_TYPE_CLOSE; mMoSyncThread.postEvent(event); mEventTypeCloseHasBeenSent = true; } }
@Override /** Event received when the Options menu is closed. */ public void onOptionsMenuClosed(Menu menu) { super.onOptionsMenuClosed(menu); // Get the focused screen widget. ScreenWidget currentScreen = mMoSyncThread.getCurrentScreen(); if (currentScreen != null) { EventQueue.getDefault().postOptionsMenuClosed(currentScreen.getHandle()); } }
@Override protected void onStop() { Log.i("MoSync", "onStop"); super.onStop(); if (theMoSyncThreadIsDead()) { return; } mMoSyncThread.releaseHardware(); }
/** * Creates the MoSyncView. If it fails the Activity is destroyed. * * @return */ private MoSyncView createMoSyncView() { // Log.i("MoSync", "createMoSyncView"); try { return new MoSyncView(this, mMoSyncThread); } catch (Exception ex) { MoSyncThread.logError( "MoSync - The MoSyncView could not be created, " + "the application could not start!", ex); ex.printStackTrace(); return null; } }
/** @see Widget.setProperty. */ @Override public boolean setProperty(String property, String value) throws PropertyConversionException, InvalidPropertyValueException { if (super.setProperty(property, value)) { return true; } TextView textView = (TextView) getView(); if (property.equals(IX_WIDGET.MAW_LABEL_TEXT)) { textView.setText(value); } else if (property.equals(IX_WIDGET.MAW_EDIT_BOX_PLACEHOLDER)) { textView.setHint(value); } else if (property.equals(IX_WIDGET.MAW_LABEL_FONT_COLOR)) { textView.setTextColor(ColorConverter.convert(value)); } else if (property.equals(IX_WIDGET.MAW_LABEL_FONT_SIZE)) { textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, FloatConverter.convert(value)); } else if (property.equals(Types.WIDGET_PROPERTY_TEXT_HORIZONTAL_ALIGNMENT)) { mHorizontalGravity = HorizontalAlignment.convert(value); textView.setGravity(mHorizontalGravity | mVerticalGravity); } else if (property.equals(Types.WIDGET_PROPERTY_TEXT_VERTICAL_ALIGNMENT)) { mVerticalGravity = VerticalAlignment.convert(value); textView.setGravity(mHorizontalGravity | mVerticalGravity); } else if (property.equals(IX_WIDGET.MAW_LABEL_MAX_NUMBER_OF_LINES)) { if (IntConverter.convert(value) < 0) { throw new InvalidPropertyValueException(property, value); } // This overrides any height setting. // Makes the TextView be at most this many lines tall. m_maxNrLines = IntConverter.convert(value); if (m_maxNrLines == 0) { textView.setMaxLines(Integer.MAX_VALUE); } else { textView.setMaxLines(m_maxNrLines); } } else if (property.equals(IX_WIDGET.MAW_LABEL_FONT_HANDLE)) { MoSyncFontHandle currentFont = null; // Search the handle in the list of fonts. currentFont = MoSyncThread.getMoSyncFont(IntConverter.convert(value)); if (currentFont != null) { setFontTypeface(currentFont.getTypeface(), currentFont.getFontSize()); } else throw new InvalidPropertyValueException(property, value); } else { return false; } return true; }
@Override public boolean onKeyUp(int keyCode, KeyEvent keyEvent) { SYSLOG("onKeyUp: " + keyEvent.toString()); int[] event = new int[3]; event[0] = EVENT_TYPE_KEY_RELEASED; event[1] = convertToMoSyncKeyCode(keyCode, keyEvent); event[2] = keyCode; mMoSyncThread.postEvent(event); // We need to intercept the back key otherwise the // activity will be terminated. if (keyCode == KeyEvent.KEYCODE_BACK) { // Pass on the back event in case we want to handle it. mMoSyncThread.handleBack(); return true; } else { return super.onKeyUp(keyCode, keyEvent); } }
/** * Send a pointer event. * * @param type The type of event to send, e.g. EVENT_TYPE_POINTER_MOVE. * @param eventData An array containing, in order: the x position, the y position, the id of the * pointer. * @return */ public boolean sendPointerEvent(int type, int[] eventData) { // TO-DO : Proper error handling if (eventData == null) { return false; } int[] touchEvent = new int[4]; touchEvent[0] = type; touchEvent[1] = eventData[0]; touchEvent[2] = eventData[1]; touchEvent[3] = eventData[2]; mMoSyncThread.postEvent(touchEvent); return true; }
@Override /** Event received when an Options menu item is selected. */ public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); // Get the focused screen widget. ScreenWidget currentScreen = mMoSyncThread.getCurrentScreen(); if (currentScreen != null) { EventQueue.getDefault() .postOptionsMenuItemSelected(currentScreen.getHandle(), item.getItemId()); return true; } return false; }
@Override protected void onDestroy() { Log.i("MoSync", "onDestroy"); super.onDestroy(); // Tell the MoSync thread to do cleanup. mMoSyncThread.onDestroy(); // Unregister the shutdown listener. unregisterShutdownListener(); // Send EVENT_TYPE_CLOSE. // TODO: Since we have made some cleanup, everything may not // be available to the app. Be aware of this if doing more // cleanup here. Applications should save data etc on // EVENT_TYPE_FOCUS_LOST. sendCloseEvent(); }
@Override public boolean onKeyDown(int keyCode, KeyEvent keyEvent) { SYSLOG("onKeyDown: " + keyEvent.toString()); // If the key is being held down we shouldn't send any more events. if (keyEvent.getRepeatCount() != 0) return true; int[] event = new int[3]; event[0] = EVENT_TYPE_KEY_PRESSED; event[1] = convertToMoSyncKeyCode(keyCode, keyEvent); event[2] = keyCode; mMoSyncThread.postEvent(event); // We need to intercept the back key otherwise the // activity will be terminated. if (keyCode == KeyEvent.KEYCODE_BACK) { return true; } else { return super.onKeyDown(keyCode, keyEvent); } }