reading preferences from phonegap.xml

adds PreferenceNode and PreferenceSet classes as wrappers for the W3C
config.xml <preference> nodes

populates a PreferenceSet @preferences member
This commit is contained in:
alunny
2012-01-09 17:29:50 -08:00
parent d91beb5ad9
commit 752b1b0e30
6 changed files with 147 additions and 3 deletions
+19 -1
View File
@@ -75,6 +75,9 @@ import com.phonegap.api.LOG;
import com.phonegap.api.PhonegapActivity;
import com.phonegap.api.PluginManager;
import com.phonegap.PreferenceNode;
import com.phonegap.PreferenceSet;
/**
* This class is the main Android activity that represents the PhoneGap
* application. It should be extended by the user to load the specific
@@ -220,6 +223,9 @@ public class DroidGap extends PhonegapActivity {
// when another application (activity) is started.
protected boolean keepRunning = true;
// preferences read from phonegap.xml
protected PreferenceSet preferences;
private boolean classicRender;
/**
@@ -311,6 +317,8 @@ public class DroidGap extends PhonegapActivity {
*/
@Override
public void onCreate(Bundle savedInstanceState) {
preferences = new PreferenceSet();
LOG.d(TAG, "DroidGap.onCreate()");
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
@@ -1880,7 +1888,17 @@ public class DroidGap extends PhonegapActivity {
{
this.classicRender = enabled.equals("true");
}
}
else if (strNode.equals("preference")) {
String name = xml.getAttributeValue(null, "name");
String value = xml.getAttributeValue(null, "value");
String readonlyString = xml.getAttributeValue(null, "readonly");
boolean readonly = (readonlyString.equals("true"));
LOG.i("PhoneGapLog", "Found preference for %s", name);
preferences.add(new PreferenceNode(name, value, readonly));
}
}
try {
@@ -0,0 +1,16 @@
package com.phonegap;
// represents the <preference> element from the W3C config.xml spec
// see http://www.w3.org/TR/widgets/#the-preference-element-and-its-attributes
public class PreferenceNode {
public String name;
public String value;
public boolean readonly;
// constructor
public PreferenceNode(String name, String value, boolean readonly) {
this.name = name;
this.value = value;
this.readonly = readonly;
}
}
@@ -0,0 +1,33 @@
package com.phonegap;
import java.util.HashSet;
import com.phonegap.PreferenceNode;
public class PreferenceSet {
private HashSet<PreferenceNode> innerSet;
public PreferenceSet() {
this.innerSet = new HashSet<PreferenceNode>();
}
public void add(PreferenceNode node) {
this.innerSet.add(node);
}
public int size() {
return this.innerSet.size();
}
public void clear() {
this.innerSet.clear();
}
public String pref(String prefName) {
for (PreferenceNode n : innerSet)
if (prefName.equals(n.name))
return n.value;
return null;
}
}