public boolean fireAppEvent(KrollInvocation invocation, String eventName, KrollDict data) { boolean handled = false; for (KrollProxy appEventProxy : appEventProxies) { boolean proxyHandled = appEventProxy.fireEvent(eventName, data); handled = handled || proxyHandled; } return handled; }
@Override public void eventListenerRemoved(String eventName, int count, KrollProxy proxy) { super.eventListenerRemoved(eventName, count, proxy); if (eventName.equals(TiC.EVENT_CLICK) && count == 0 && proxy.equals(this) && !(proxy instanceof TiWindowProxy)) { if (proxy.hasProperty(TiC.PROPERTY_TOUCH_ENABLED) && !TiConvert.toBoolean(proxy.getProperty(TiC.PROPERTY_TOUCH_ENABLED))) { setClickable(false); } } }
public void setBackgroundFromProxy(KrollProxy proxy) { Drawable background = null; Object bkgdImage = proxy.getProperty(TiC.PROPERTY_BACKGROUND_IMAGE); Object bkgdColor = proxy.getProperty(TiC.PROPERTY_BACKGROUND_COLOR); if (bkgdImage != null) { background = getBackgroundImageDrawable(proxy, bkgdImage.toString()); } else if (bkgdColor != null) { Integer bgColor = TiConvert.toColor(bkgdColor.toString()); background = new ColorDrawable(bgColor); } setBackgroundDrawable(proxy.getProperties(), background); }
protected void initialize() throws IOException { try { mp = new MediaPlayer(); String url = TiConvert.toString(proxy.getProperty(TiC.PROPERTY_URL)); if (URLUtil.isAssetUrl(url)) { Context context = proxy.getTiContext().getTiApp(); String path = url.substring(TiConvert.ASSET_URL.length()); AssetFileDescriptor afd = null; try { afd = context.getAssets().openFd(path); // Why mp.setDataSource(afd) doesn't work is a problem for another day. // http://groups.google.com/group/android-developers/browse_thread/thread/225c4c150be92416 mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); } catch (IOException e) { Log.e(LCAT, "Error setting file descriptor: ", e); } finally { if (afd != null) { afd.close(); } } } else { Uri uri = Uri.parse(url); if (uri.getScheme().equals(TiC.PROPERTY_FILE)) { mp.setDataSource(uri.getPath()); } else { remote = true; mp.setDataSource(url); } } mp.setLooping(looping); mp.setOnCompletionListener(this); mp.setOnErrorListener(this); mp.setOnInfoListener(this); mp.setOnBufferingUpdateListener(this); mp.prepare(); // Probably need to allow for Async setState(STATE_INITIALIZED); setVolume(volume); if (proxy.hasProperty(TiC.PROPERTY_TIME)) { setTime(TiConvert.toInt(proxy.getProperty(TiC.PROPERTY_TIME))); } } catch (Throwable t) { Log.w(LCAT, "Issue while initializing : ", t); release(); setState(STATE_STOPPED); } }
@Override public void handleCreationDict(KrollDict options) { options = handleStyleOptions(options); if (langConversionTable != null) { KrollDict foundStrings = new KrollDict(); for (String key : langConversionTable.keySet()) { // if we have it already, ignore if (!options.containsKey(key)) { String convertKey = (String) langConversionTable.get(key); String langKey = (String) options.get(convertKey); if (langKey != null) { try { String localText = getLocalizedText(langKey); foundStrings.put(key, localText); } catch (TiRHelper.ResourceNotFoundException e) { Log.w(LCAT, "Localized text key '" + langKey + "' is invalid."); } } } } if (!(foundStrings.isEmpty())) { extend(foundStrings); options.putAll(foundStrings); } } options = handleStyleOptions(options); super.handleCreationDict(options); // TODO eventManager.addOnEventChangeListener(this); }
@Override public boolean onInfo(MediaPlayer mp, int what, int extra) { String msg = "Unknown media issue."; switch (what) { case MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING: msg = "Stream not interleaved or interleaved improperly."; break; case MediaPlayer.MEDIA_INFO_NOT_SEEKABLE: msg = "Stream does not support seeking"; break; case MediaPlayer.MEDIA_INFO_UNKNOWN: msg = "Unknown media issue"; break; case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING: msg = "Video is too complex for decoder, video lagging."; // shouldn't occur, but covering // bases. break; } KrollDict data = new KrollDict(); data.put("code", 0); data.put("message", msg); proxy.fireEvent(EVENT_ERROR, data); return true; }
@Override public void handleCreationDict(KrollDict options) { options = handleStyleOptions(options); super.handleCreationDict(options); // TODO eventManager.addOnEventChangeListener(this); }
@Override public void setActivity(Activity activity) { super.setActivity(activity); if (children != null) { for (TiViewProxy child : children) { child.setActivity(activity); } } }
private void setState(int state) { proxy.setProperty("state", state); String stateDescription = ""; switch (state) { case STATE_BUFFERING: stateDescription = STATE_BUFFERING_DESC; break; case STATE_INITIALIZED: stateDescription = STATE_INITIALIZED_DESC; break; case STATE_PAUSED: stateDescription = STATE_PAUSED_DESC; break; case STATE_PLAYING: stateDescription = STATE_PLAYING_DESC; break; case STATE_STARTING: stateDescription = STATE_STARTING_DESC; break; case STATE_STOPPED: stateDescription = STATE_STOPPED_DESC; break; case STATE_STOPPING: stateDescription = STATE_STOPPING_DESC; break; case STATE_WAITING_FOR_DATA: stateDescription = STATE_WAITING_FOR_DATA_DESC; break; case STATE_WAITING_FOR_QUEUE: stateDescription = STATE_WAITING_FOR_QUEUE_DESC; break; } proxy.setProperty("stateDescription", stateDescription); if (DBG) { Log.d(LCAT, "Audio state changed: " + stateDescription); } KrollDict data = new KrollDict(); data.put("state", state); data.put("description", stateDescription); proxy.fireEvent(EVENT_CHANGE, data); }
@Override public void propertyChanged(String key, Object oldValue, Object newValue, KrollProxy proxy) { if (key.equals(TiC.PROPERTY_FONT)) { Spinner spinner = (Spinner) nativeView; TiSpinnerAdapter<TiViewProxy> adapter = (TiSpinnerAdapter<TiViewProxy>) spinner.getAdapter(); adapter.setFontProperties(proxy.getProperties()); adapter.notifyDataSetChanged(); } else { super.propertyChanged(key, oldValue, newValue, proxy); } }
@Override public void onPropertyChanged(String name, Object value) { if (Build.VERSION.SDK_INT >= TiC.API_LEVEL_ICE_CREAM_SANDWICH && TiC.PROPERTY_ON_HOME_ICON_ITEM_SELECTED.equals(name)) { // If we have a listener on the home icon item, then enable the home button (we need to do // this for ICS and // above) if (TiApplication.isUIThread()) { actionBar.setHomeButtonEnabled(true); } else { getMainHandler().obtainMessage(MSG_SET_HOME_BUTTON_ENABLED).sendToTarget(); } } super.onPropertyChanged(name, value); }
@Override public boolean onError(MediaPlayer mp, int what, int extra) { int code = 0; String msg = "Unknown media error."; if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) { msg = "Media server died"; } release(); KrollDict data = new KrollDict(); data.put("code", code); data.put("message", msg); proxy.fireEvent(EVENT_ERROR, data); return true; }
public void setTime(int position) { if (position < 0) { position = 0; } if (mp != null) { int duration = mp.getDuration(); if (position > duration) { position = duration; } mp.seekTo(position); } proxy.setProperty("time", position); }
public void setVolume(float volume) { try { if (volume < 0.0f) { this.volume = 0.0f; Log.w(LCAT, "Attempt to set volume less than 0.0. Volume set to 0.0"); } else if (volume > 1.0) { this.volume = 1.0f; proxy.setProperty("volume", volume); Log.w(LCAT, "Attempt to set volume greater than 1.0. Volume set to 1.0"); } else { this.volume = volume; // Store in 0.0 to 1.0, scale when setting hw } if (mp != null) { float scaledVolume = this.volume; mp.setVolume(scaledVolume, scaledVolume); } } catch (Throwable t) { Log.w(LCAT, "Issue while setting volume : ", t); } }
public AbsListItemProxy generateCellProxy(KrollProxy proxy, final String rootType) { AbsListItemProxy result = (AbsListItemProxy) proxy.createTypeViewFromDict(properties, rootType); return result; }
public Drawable getBackgroundImageDrawable(KrollProxy proxy, String path) { String url = proxy.resolveUrl(null, path); return TiFileHelper.loadDrawable(url); }
public void onCompletion(MediaPlayer mp) { proxy.fireEvent(EVENT_COMPLETE, null); stop(); }
public void propertyChanged(String key, Object oldValue, Object newValue, KrollProxy proxy) { if (key.equals(TiC.PROPERTY_LEFT)) { if (newValue != null) { layoutParams.optionLeft = TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_LEFT); } else { layoutParams.optionLeft = null; } layoutNativeView(); } else if (key.equals(TiC.PROPERTY_TOP)) { if (newValue != null) { layoutParams.optionTop = TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_TOP); } else { layoutParams.optionTop = null; } layoutNativeView(); } else if (key.equals(TiC.PROPERTY_CENTER)) { TiConvert.updateLayoutCenter(newValue, layoutParams); layoutNativeView(); } else if (key.equals(TiC.PROPERTY_RIGHT)) { if (newValue != null) { layoutParams.optionRight = TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_RIGHT); } else { layoutParams.optionRight = null; } layoutNativeView(); } else if (key.equals(TiC.PROPERTY_BOTTOM)) { if (newValue != null) { layoutParams.optionBottom = TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_BOTTOM); } else { layoutParams.optionBottom = null; } layoutNativeView(); } else if (key.equals(TiC.PROPERTY_SIZE)) { if (newValue instanceof HashMap) { HashMap<String, Object> d = (HashMap) newValue; propertyChanged(TiC.PROPERTY_WIDTH, oldValue, d.get(TiC.PROPERTY_WIDTH), proxy); propertyChanged(TiC.PROPERTY_HEIGHT, oldValue, d.get(TiC.PROPERTY_HEIGHT), proxy); } else if (newValue != null) { Log.w( LCAT, "Unsupported property type (" + (newValue.getClass().getSimpleName()) + ") for key: " + key + ". Must be an object/dictionary"); } } else if (key.equals(TiC.PROPERTY_HEIGHT)) { if (newValue != null) { if (!newValue.equals(TiC.SIZE_AUTO)) { layoutParams.optionHeight = TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_HEIGHT); layoutParams.autoHeight = false; } else { layoutParams.optionHeight = null; layoutParams.autoHeight = true; } } else { layoutParams.optionHeight = null; } layoutNativeView(); } else if (key.equals(TiC.PROPERTY_WIDTH)) { if (newValue != null) { if (!newValue.equals(TiC.SIZE_AUTO)) { layoutParams.optionWidth = TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_WIDTH); layoutParams.autoWidth = false; } else { layoutParams.optionWidth = null; layoutParams.autoWidth = true; } } else { layoutParams.optionWidth = null; } layoutNativeView(); } else if (key.equals(TiC.PROPERTY_ZINDEX)) { if (newValue != null) { layoutParams.optionZIndex = TiConvert.toInt(newValue); } else { layoutParams.optionZIndex = 0; } layoutNativeView(true); } else if (key.equals(TiC.PROPERTY_FOCUSABLE)) { boolean focusable = TiConvert.toBoolean(proxy.getProperty(TiC.PROPERTY_FOCUSABLE)); nativeView.setFocusable(focusable); if (focusable) { registerForKeyClick(nativeView); } else { // nativeView.setOnClickListener(null); // ? mistake? I assume OnKeyListener was meant nativeView.setOnKeyListener(null); } } else if (key.equals(TiC.PROPERTY_TOUCH_ENABLED)) { doSetClickable(TiConvert.toBoolean(newValue)); } else if (key.equals(TiC.PROPERTY_VISIBLE)) { nativeView.setVisibility(TiConvert.toBoolean(newValue) ? View.VISIBLE : View.INVISIBLE); } else if (key.equals(TiC.PROPERTY_ENABLED)) { nativeView.setEnabled(TiConvert.toBoolean(newValue)); } else if (key.startsWith(TiC.PROPERTY_BACKGROUND_PADDING)) { Log.i(LCAT, key + " not yet implemented."); } else if (key.equals(TiC.PROPERTY_OPACITY) || key.startsWith(TiC.PROPERTY_BACKGROUND_PREFIX) || key.startsWith(TiC.PROPERTY_BORDER_PREFIX)) { // Update first before querying. proxy.setProperty(key, newValue); KrollDict d = proxy.getProperties(); boolean hasImage = hasImage(d); boolean hasColorState = hasColorState(d); boolean hasBorder = hasBorder(d); boolean requiresCustomBackground = hasImage || hasColorState || hasBorder; if (!requiresCustomBackground) { if (background != null) { background.releaseDelegate(); background.setCallback(null); background = null; } if (d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_COLOR)) { Integer bgColor = TiConvert.toColor(d, TiC.PROPERTY_BACKGROUND_COLOR); if (nativeView != null) { nativeView.setBackgroundColor(bgColor); nativeView.postInvalidate(); } } else { if (key.equals(TiC.PROPERTY_OPACITY)) { setOpacity(TiConvert.toFloat(newValue)); } if (nativeView != null) { nativeView.setBackgroundDrawable(null); nativeView.postInvalidate(); } } } else { boolean newBackground = background == null; if (newBackground) { background = new TiBackgroundDrawable(); } Integer bgColor = null; if (!hasColorState) { if (d.get(TiC.PROPERTY_BACKGROUND_COLOR) != null) { bgColor = TiConvert.toColor(d, TiC.PROPERTY_BACKGROUND_COLOR); if (newBackground || (key.equals(TiC.PROPERTY_OPACITY) || key.equals(TiC.PROPERTY_BACKGROUND_COLOR))) { background.setBackgroundColor(bgColor); } } } if (hasImage || hasColorState) { if (newBackground || key.startsWith(TiC.PROPERTY_BACKGROUND_PREFIX)) { handleBackgroundImage(d); } } if (hasBorder) { if (newBackground) { initializeBorder(d, bgColor); } else if (key.startsWith(TiC.PROPERTY_BORDER_PREFIX)) { handleBorderProperty(key, newValue); } } applyCustomBackground(); } if (nativeView != null) { nativeView.postInvalidate(); } } else if (key.equals(TiC.PROPERTY_SOFT_KEYBOARD_ON_FOCUS)) { Log.w( LCAT, "Focus state changed to " + TiConvert.toString(newValue) + " not honored until next focus event."); } else if (key.equals(TiC.PROPERTY_TRANSFORM)) { if (nativeView != null) { applyTransform((Ti2DMatrix) newValue); } } else if (key.equals(TiC.PROPERTY_KEEP_SCREEN_ON)) { if (nativeView != null) { nativeView.setKeepScreenOn(TiConvert.toBoolean(newValue)); } } else { TiViewProxy viewProxy = getProxy(); if (viewProxy != null && viewProxy.isLocalizedTextId(key)) { viewProxy.setLocalizedText(key, TiConvert.toString(newValue)); } else { if (DBG) { Log.d(LCAT, "Unhandled property key: " + key); } } } }
public void registerProxy(KrollProxy proxy) { String proxyId = proxy.getProxyId(); if (!proxyMap.containsKey(proxyId)) { proxyMap.put(proxyId, new SoftReference<KrollProxy>(proxy)); } }
protected KrollProxySupport createProxy(String creationUrl, Object[] args) { return KrollProxy.createProxy(ProgressBarProxy.class, getRhinoObject(), args, creationUrl); }
@Override public boolean dispatchKeyEvent(KeyEvent event) { boolean handled = false; TiViewProxy window; if (this.window != null) { window = this.window; } else { window = this.view; } if (window == null) { return super.dispatchKeyEvent(event); } switch (event.getKeyCode()) { case KeyEvent.KEYCODE_BACK: { if (event.getAction() == KeyEvent.ACTION_UP) { String backEvent = "android:back"; KrollProxy proxy = null; // android:back could be fired from a tabGroup window (activityProxy) // or hw window (window).This event is added specifically to the activity // proxy of a tab group in window.js if (activityProxy.hasListeners(backEvent)) { proxy = activityProxy; } else if (window.hasListeners(backEvent)) { proxy = window; } if (proxy != null) { proxy.fireEvent(backEvent, null); handled = true; } } break; } case KeyEvent.KEYCODE_CAMERA: { if (window.hasListeners(TiC.EVENT_ANDROID_CAMERA)) { if (event.getAction() == KeyEvent.ACTION_UP) { window.fireEvent(TiC.EVENT_ANDROID_CAMERA, null); } handled = true; } // TODO: Deprecate old event if (window.hasListeners("android:camera")) { if (event.getAction() == KeyEvent.ACTION_UP) { window.fireEvent("android:camera", null); } handled = true; } break; } case KeyEvent.KEYCODE_FOCUS: { if (window.hasListeners(TiC.EVENT_ANDROID_FOCUS)) { if (event.getAction() == KeyEvent.ACTION_UP) { window.fireEvent(TiC.EVENT_ANDROID_FOCUS, null); } handled = true; } // TODO: Deprecate old event if (window.hasListeners("android:focus")) { if (event.getAction() == KeyEvent.ACTION_UP) { window.fireEvent("android:focus", null); } handled = true; } break; } case KeyEvent.KEYCODE_SEARCH: { if (window.hasListeners(TiC.EVENT_ANDROID_SEARCH)) { if (event.getAction() == KeyEvent.ACTION_UP) { window.fireEvent(TiC.EVENT_ANDROID_SEARCH, null); } handled = true; } // TODO: Deprecate old event if (window.hasListeners("android:search")) { if (event.getAction() == KeyEvent.ACTION_UP) { window.fireEvent("android:search", null); } handled = true; } break; } case KeyEvent.KEYCODE_VOLUME_UP: { if (window.hasListeners(TiC.EVENT_ANDROID_VOLUP)) { if (event.getAction() == KeyEvent.ACTION_UP) { window.fireEvent(TiC.EVENT_ANDROID_VOLUP, null); } handled = true; } // TODO: Deprecate old event if (window.hasListeners("android:volup")) { if (event.getAction() == KeyEvent.ACTION_UP) { window.fireEvent("android:volup", null); } handled = true; } break; } case KeyEvent.KEYCODE_VOLUME_DOWN: { if (window.hasListeners(TiC.EVENT_ANDROID_VOLDOWN)) { if (event.getAction() == KeyEvent.ACTION_UP) { window.fireEvent(TiC.EVENT_ANDROID_VOLDOWN, null); } handled = true; } // TODO: Deprecate old event if (window.hasListeners("android:voldown")) { if (event.getAction() == KeyEvent.ACTION_UP) { window.fireEvent("android:voldown", null); } handled = true; } break; } } if (!handled) { handled = super.dispatchKeyEvent(event); } return handled; }
// Handle creation options @Override public void handleCreationDict(KrollDict options) { super.handleCreationDict(options); }
@Override public void propertyChanged(String key, Object oldValue, Object newValue, KrollProxy proxy) { if (Log.isDebugModeEnabled()) { Log.d(TAG, "Property: " + key + " old: " + oldValue + " new: " + newValue, Log.DEBUG_MODE); } if (key.equals(TiC.PROPERTY_ENABLED)) { tv.setEnabled(TiConvert.toBoolean(newValue)); } else if (key.equals(TiC.PROPERTY_VALUE)) { tv.setText(TiConvert.toString(newValue)); } else if (key.equals(TiC.PROPERTY_MAX_LENGTH)) { maxLength = TiConvert.toInt(newValue); // truncate if current text exceeds max length Editable currentText = tv.getText(); if (maxLength >= 0 && currentText.length() > maxLength) { CharSequence truncateText = currentText.subSequence(0, maxLength); int cursor = tv.getSelectionStart() - 1; if (cursor > maxLength) { cursor = maxLength; } tv.setText(truncateText); tv.setSelection(cursor); } } else if (key.equals(TiC.PROPERTY_COLOR)) { tv.setTextColor(TiConvert.toColor((String) newValue)); } else if (key.equals(TiC.PROPERTY_HINT_TEXT)) { tv.setHint((String) newValue); } else if (key.equals(TiC.PROPERTY_ELLIPSIZE)) { if (TiConvert.toBoolean(newValue)) { tv.setEllipsize(TruncateAt.END); } else { tv.setEllipsize(null); } } else if (key.equals(TiC.PROPERTY_TEXT_ALIGN) || key.equals(TiC.PROPERTY_VERTICAL_ALIGN)) { String textAlign = null; String verticalAlign = null; if (key.equals(TiC.PROPERTY_TEXT_ALIGN)) { textAlign = TiConvert.toString(newValue); } else if (proxy.hasProperty(TiC.PROPERTY_TEXT_ALIGN)) { textAlign = TiConvert.toString(proxy.getProperty(TiC.PROPERTY_TEXT_ALIGN)); } if (key.equals(TiC.PROPERTY_VERTICAL_ALIGN)) { verticalAlign = TiConvert.toString(newValue); } else if (proxy.hasProperty(TiC.PROPERTY_VERTICAL_ALIGN)) { verticalAlign = TiConvert.toString(proxy.getProperty(TiC.PROPERTY_VERTICAL_ALIGN)); } handleTextAlign(textAlign, verticalAlign); } else if (key.equals(TiC.PROPERTY_KEYBOARD_TYPE) || (key.equals(TiC.PROPERTY_AUTOCORRECT) || key.equals(TiC.PROPERTY_AUTOCAPITALIZATION) || key.equals(TiC.PROPERTY_PASSWORD_MASK) || key.equals(TiC.PROPERTY_EDITABLE))) { KrollDict d = proxy.getProperties(); handleKeyboard(d); } else if (key.equals(TiC.PROPERTY_RETURN_KEY_TYPE)) { handleReturnKeyType(TiConvert.toInt(newValue)); } else if (key.equals(TiC.PROPERTY_FONT)) { TiUIHelper.styleText(tv, (HashMap) newValue); } else if (key.equals(TiC.PROPERTY_AUTO_LINK)) { TiUIHelper.linkifyIfEnabled(tv, newValue); } else { super.propertyChanged(key, oldValue, newValue, proxy); } }