Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[TIMOB-7308] Android: Implement subtitleid and titleid properties for Ti.Map.Annotation #2345

Merged
merged 2 commits into from

2 participants

@joshthecoder

This implements subtitleid and titleid properties for map annotations.
See JIRA ticket for a test case and instructions. Please test both runtimes (V8 and Rhino).

@billdawson

Bill reviewing instead of Opie (so Opie can do one of mine)

@billdawson

CR/FR accepted. Tested 2.2 emulator and 3.1 device (T-Mobile G-Slate). Tested both rhino & v8. Also ran KS (just v8) for a sanity, and then drillbit "ui", "android.ui" and "locale" tests for kicks and laughs.

@billdawson billdawson merged commit 7d81569 into appcelerator:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 7, 2012
  1. Enable subtitleid and titleid for Map Annotations.

    Joshua Roesslein authored
  2. Move locale property handling to KrollProxy.

    Joshua Roesslein authored
This page is out of date. Refresh to see the latest.
View
13 android/modules/map/src/java/ti/modules/titanium/map/AnnotationProxy.java
@@ -8,6 +8,7 @@
import java.lang.ref.WeakReference;
+import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
@@ -26,7 +27,9 @@
TiC.PROPERTY_RIGHT_VIEW,
TiC.PROPERTY_RIGHT_BUTTON,
TiC.PROPERTY_SUBTITLE,
- TiC.PROPERTY_TITLE
+ TiC.PROPERTY_SUBTITLEID,
+ TiC.PROPERTY_TITLE,
+ TiC.PROPERTY_TITLEID
})
public class AnnotationProxy extends KrollProxy
{
@@ -55,6 +58,14 @@ public void setViewProxy(ViewProxy viewProxy)
}
@Override
+ protected KrollDict getLangConversionTable() {
+ KrollDict table = new KrollDict();
+ table.put(TiC.PROPERTY_SUBTITLE, TiC.PROPERTY_SUBTITLEID);
+ table.put(TiC.PROPERTY_TITLE, TiC.PROPERTY_TITLEID);
+ return table;
+ }
+
+ @Override
public void onPropertyChanged(String name, Object value)
{
super.onPropertyChanged(name, value);
View
153 android/titanium/src/java/org/appcelerator/kroll/KrollProxy.java
@@ -24,11 +24,13 @@
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.proxy.ActivityProxy;
+import org.appcelerator.titanium.util.TiRHelper;
import org.appcelerator.titanium.util.TiUrl;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
+import android.util.Pair;
/**
@@ -77,6 +79,8 @@
protected Handler mainHandler = null;
protected Handler runtimeHandler = null;
+ private KrollDict langConversionTable = null;
+
public static final String PROXY_ID_PREFIX = "proxy$";
@@ -104,6 +108,7 @@ public KrollProxy(String baseCreationUrl)
creationUrl = new TiUrl(baseCreationUrl);
this.listenerIdGenerator = new AtomicInteger(0);
this.eventListeners = Collections.synchronizedMap(new HashMap<String, HashMap<Integer, KrollEventCallback>>());
+ this.langConversionTable = getLangConversionTable();
}
private void setupProxy(KrollObject object, Object[] creationArguments, TiUrl creationUrl)
@@ -223,6 +228,135 @@ protected void handleDefaultValues()
}
/**
+ * @return the language conversion table used to load localized values for certain properties from the locale files.
+ * For each localizable property, such as "title," the proxy should define a second property, such as "titleid", used to specify a
+ * localization key for that property. If the user specifies a localization key in "titleid", the corresponding localized text from the locale file
+ * is used for "title."
+ *
+ * Subclasses should override this method to return a table mapping localizable properties to the corresponding localization key properties.
+ *
+ * For example, if the proxy has two properties, "title" and "text", and the corresponding localization key properties are "titleid" and "textid", this might look like:
+ * </br>
+ *
+ * <pre><code>protected KrollDict getLangConversionTable()
+ *{
+ * KrollDict table = new KrollDict();
+ * table.put("title", "titleid");
+ * table.put("text", "textid");
+ * return table;
+ *} </pre> </code>
+ *
+ * @module.api
+ *
+ */
+ protected KrollDict getLangConversionTable()
+ {
+ return null;
+ }
+
+ /**
+ * Handles initialization of the proxy's locale string properties.
+ *
+ * @see #getLangConversionTable()
+ */
+ private void handleLocaleProperties()
+ {
+ if (langConversionTable == null) {
+ return;
+ }
+
+ /*
+ * Iterate through the language conversion table.
+ * This table maps target properties to their locale lookup property.
+ * Example: title -> titleid
+ *
+ * The lookup identifier stored in the locale property (titleid) will be used
+ * to query the locale strings file to get the localized value.
+ * This localized value will be set to the targeted property (title).
+ */
+ for (Map.Entry<String, Object> entry : langConversionTable.entrySet()) {
+ // Get the lookup identifier stored in the locale property.
+ String localeProperty = entry.getValue().toString();
+ String lookupId = properties.getString(localeProperty);
+ if (lookupId == null) {
+ // If no locale lookup identifier is provided, skip this entry.
+ continue;
+ }
+
+ // Lookup the localized string from the locale file.
+ String localizedValue = getLocalizedText(lookupId);
+ if (localizedValue == null) {
+ // If there is no localized value for this identifier,
+ // log a warning and skip over the entry.
+ Log.w(TAG, "No localized string found for identifier: " + lookupId);
+ continue;
+ }
+
+ // Set the localized value to the targeted property.
+ String targetProperty = entry.getKey();
+ setProperty(targetProperty, localizedValue);
+ }
+ }
+
+ /**
+ * Updates the lookup identifier value of a locale property.
+ * This will also update the targeted value with the string found
+ * using the new lookup identifier.
+ *
+ * @param localeProperty name of the locale property (example: titleid)
+ * @param newLookupId the new lookup identifier
+ * @return a pair containing the name of the target property which was updated and the new value set on it.
+ */
+ private Pair<String, String> updateLocaleProperty(String localeProperty, String newLookupId)
+ {
+ if (langConversionTable == null) {
+ return null;
+ }
+
+ properties.put(localeProperty, newLookupId);
+
+ // Determine which localized property this locale property updates.
+ for (Map.Entry<String, Object> entry : langConversionTable.entrySet()) {
+ if (entry.getValue().toString().equals(localeProperty)) {
+ String targetProperty = entry.getKey();
+ String localizedValue = getLocalizedText(newLookupId);
+ setProperty(targetProperty, localizedValue);
+
+ return Pair.create(targetProperty, localizedValue);
+ }
+ }
+
+ // If we reach this point, the provided locale property is not valid.
+ return null;
+ }
+
+ /**
+ * Return true if the given property is a locale property.
+ * @param propertyName name of the property to check (ex: titleid)
+ * @return true if this property is a locale property
+ */
+ private boolean isLocaleProperty(String propertyName)
+ {
+ return propertyName.endsWith("id");
+ }
+
+ /**
+ * Looks up a localized string given an identifier.
+ *
+ * @param lookupId the identifier of the localized value to look up.
+ * @return the localized string if found, otherwise null.
+ */
+ private String getLocalizedText(String lookupId)
+ {
+ try {
+ int resid = TiRHelper.getResource("string." + lookupId);
+ return getActivity().getString(resid);
+ } catch (TiRHelper.ResourceNotFoundException e) {
+ return null;
+ }
+ }
+
+ /**
* Handles the creation {@link KrollDict} passed into the create method for this proxy.
* This is usually the first (and sometimes only) argument to the proxy's create method.
*
@@ -238,6 +372,7 @@ public void handleCreationDict(KrollDict dict)
properties.putAll(dict);
handleDefaultValues();
+ handleLocaleProperties();
if (modelListener != null) {
modelListener.processProperties(properties);
@@ -554,9 +689,21 @@ public void setPropertyAndFire(String name, Object value)
public void onPropertyChanged(String name, Object value)
{
- Object oldValue = properties.get(name);
- properties.put(name, value);
- firePropertyChanged(name, oldValue, value);
+ String propertyName = name;
+ Object newValue = value;
+
+ if (isLocaleProperty(name)) {
+ Log.i(TAG, "Updating locale: " + name);
+ Pair<String, String> update = updateLocaleProperty(name, value.toString());
+ if (update != null) {
+ propertyName = update.first;
+ newValue = update.second;
+ }
+ }
+
+ Object oldValue = properties.get(propertyName);
+ properties.put(propertyName, newValue);
+ firePropertyChanged(propertyName, oldValue, newValue);
}
public void onPropertiesChanged(Object[][] changes)
View
5 android/titanium/src/java/org/appcelerator/titanium/TiC.java
@@ -1539,6 +1539,11 @@
/**
* @module.api
*/
+ public static final String PROPERTY_SUBTITLEID = "subtitleid";
+
+ /**
+ * @module.api
+ */
public static final String PROPERTY_SUCCESS = "success";
/**
View
108 android/titanium/src/java/org/appcelerator/titanium/proxy/TiViewProxy.java
@@ -28,8 +28,6 @@
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiAnimationBuilder;
import org.appcelerator.titanium.util.TiConvert;
-import org.appcelerator.titanium.util.TiRHelper;
-import org.appcelerator.titanium.util.TiRHelper.ResourceNotFoundException;
import org.appcelerator.titanium.util.TiUrl;
import org.appcelerator.titanium.view.TiAnimation;
import org.appcelerator.titanium.view.TiUIView;
@@ -88,7 +86,6 @@
protected TiUIView view;
protected Object pendingAnimationLock;
protected TiAnimationBuilder pendingAnimation;
- private KrollDict langConversionTable;
private boolean isDecorView = false;
private AtomicBoolean layoutStarted = new AtomicBoolean();
@@ -98,92 +95,17 @@
*/
public TiViewProxy()
{
- langConversionTable = getLangConversionTable();
pendingAnimationLock = new Object();
defaultValues.put(TiC.PROPERTY_BACKGROUND_REPEAT, false);
}
- /**
- * Returns true if idPropertyName is an id field for a localized
- * text lookup (i.e., the right/value side of an entry in
- * langConversionTable).
- */
- public boolean isLocalizedTextId(String idPropertyName)
- {
- if (langConversionTable != null && langConversionTable.containsValue(idPropertyName)) {
- return true;
- }
- return false;
- }
-
- public void setLocalizedText(String idPropertyName, String idPropertyValue)
- {
- if (langConversionTable == null) {
- return;
- }
- for (String propertyName : langConversionTable.keySet()) {
- String thisIdPropertyName = langConversionTable.getString(propertyName);
- if (idPropertyName.equals(thisIdPropertyName)) {
- try {
- String localText = getLocalizedText(idPropertyValue);
- //If key exists, overwrite the text.
- if (localText != null) {
- setPropertyAndFire(propertyName, localText);
- }
- } catch (ResourceNotFoundException e) {
- Log.w(LCAT, "Localized text key '" + idPropertyValue + "' is invalid.");
- }
- break;
- }
- }
- }
-
- private String getLocalizedText(String lookupId)
- throws TiRHelper.ResourceNotFoundException
- {
- int resid = TiRHelper.getResource("string." + lookupId);
- if (resid != 0) {
- return getActivity().getString(resid);
- } else {
- // Actually won't get here because getResource will throw
- // if invalid key.
- Log.w(LCAT, "Localized text key '" + lookupId + "' is invalid.");
- return null;
- }
- }
-
@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);
}
@@ -251,34 +173,6 @@ protected KrollDict handleStyleOptions(KrollDict options)
return options;
}
- /**
- * @return the language conversion table used to load localized values for certain properties from the locale files.
- * For each localizable property, such as "title," the proxy should define a second property, such as "titleid", used to specify a
- * localization key for that property. If the user specifies a localization key in "titleid", the corresponding localized text from the locale file
- * is used for "title."
- *
- * Subclasses should override this method to return a table mapping localizable properties to the corresponding localization key properties.
- *
- * For example, if the proxy has two properties, "title" and "text", and the corresponding localization key properties are "titleid" and "textid", this might look like:
- * </br>
- *
- * <pre><code>protected KrollDict getLangConversionTable()
- *{
- * KrollDict table = new KrollDict();
- * table.put("title", "titleid");
- * table.put("text", "textid");
- * return table;
- *} </pre> </code>
- * @module.api
- *
- */
- protected KrollDict getLangConversionTable()
- {
- // subclasses override to return a table mapping of langid keys to actual keys
- // used for specifying things like titleid vs. title so that you can localize them
- return null;
- }
-
public TiAnimationBuilder getPendingAnimation()
{
synchronized(pendingAnimationLock) {
View
13 android/titanium/src/java/org/appcelerator/titanium/view/TiUIView.java
@@ -38,10 +38,10 @@
import android.os.Build;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
-import android.view.ScaleGestureDetector;
-import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
+import android.view.ScaleGestureDetector;
+import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
@@ -527,13 +527,8 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
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);
- }
+ if (DBG) {
+ Log.d(LCAT, "Unhandled property key: " + key);
}
}
}
View
2  apidoc/Titanium/Map/Annotation.yml
@@ -138,7 +138,6 @@ properties:
- name: subtitleid
summary: Key in the locale file to use for the subtitle property.
type: String
- platforms: [iphone, ipad, mobileweb]
- name: title
summary: Primary title of the annotation view.
@@ -147,7 +146,6 @@ properties:
- name: titleid
summary: Key in the locale file to use for the title property.
type: String
- platforms: [iphone, ipad, mobileweb]
events:
- name: click
Something went wrong with that request. Please try again.