incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bows...@apache.org
Subject [14/17] git commit: reading preferences from phonegap.xml
Date Mon, 23 Jan 2012 23:05:55 GMT
reading preferences from phonegap.xml

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

populates a PreferenceSet @preferences member


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/commit/752b1b0e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/752b1b0e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/752b1b0e

Branch: refs/heads/master
Commit: 752b1b0e3038a3a632a4e1f98d805a2b8399e5ff
Parents: d91beb5
Author: alunny <alunny@gmail.com>
Authored: Mon Jan 9 17:29:50 2012 -0800
Committer: alunny <alunny@gmail.com>
Committed: Mon Jan 9 17:29:50 2012 -0800

----------------------------------------------------------------------
 .gitignore                                         |    5 +-
 framework/src/com/phonegap/DroidGap.java           |   20 +++++++-
 framework/src/com/phonegap/PreferenceNode.java     |   16 ++++++
 framework/src/com/phonegap/PreferenceSet.java      |   33 ++++++++++++
 .../test/com/phonegap/PreferenceNodeTest.java      |   35 ++++++++++++
 framework/test/com/phonegap/PreferenceSetTest.java |   41 +++++++++++++++
 6 files changed, 147 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/752b1b0e/.gitignore
----------------------------------------------------------------------
diff --git a/.gitignore b/.gitignore
index 0d47668..b83f1e9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,9 +7,10 @@ framework/proguard.cfg
 framework/phonegap.jar
 framework/phonegap-*.jar
 framework/bin
+framework/test/com/phonegap/*.class
 framework/assets/www/.DS_Store
 framework/assets/www/phonegap-*.js
 example
-test
+./test
 tmp
-*.tmp
\ No newline at end of file
+*.tmp

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/752b1b0e/framework/src/com/phonegap/DroidGap.java
----------------------------------------------------------------------
diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java
index 2eee859..2b92ef6 100755
--- a/framework/src/com/phonegap/DroidGap.java
+++ b/framework/src/com/phonegap/DroidGap.java
@@ -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 {

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/752b1b0e/framework/src/com/phonegap/PreferenceNode.java
----------------------------------------------------------------------
diff --git a/framework/src/com/phonegap/PreferenceNode.java b/framework/src/com/phonegap/PreferenceNode.java
new file mode 100644
index 0000000..2851c24
--- /dev/null
+++ b/framework/src/com/phonegap/PreferenceNode.java
@@ -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;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/752b1b0e/framework/src/com/phonegap/PreferenceSet.java
----------------------------------------------------------------------
diff --git a/framework/src/com/phonegap/PreferenceSet.java b/framework/src/com/phonegap/PreferenceSet.java
new file mode 100644
index 0000000..4e6f79e
--- /dev/null
+++ b/framework/src/com/phonegap/PreferenceSet.java
@@ -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;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/752b1b0e/framework/test/com/phonegap/PreferenceNodeTest.java
----------------------------------------------------------------------
diff --git a/framework/test/com/phonegap/PreferenceNodeTest.java b/framework/test/com/phonegap/PreferenceNodeTest.java
new file mode 100644
index 0000000..91cf4bf
--- /dev/null
+++ b/framework/test/com/phonegap/PreferenceNodeTest.java
@@ -0,0 +1,35 @@
+import org.junit.*;
+import static org.junit.Assert.*;
+
+import com.phonegap.PreferenceNode;
+
+public class PreferenceNodeTest {
+    @Test
+        public void testConstructor() {
+            PreferenceNode foo = new com.phonegap.PreferenceNode("fullscreen", "false", false);
+            assertEquals("fullscreen", foo.name);
+            assertEquals("false", foo.value);
+            assertEquals(false, foo.readonly);
+        }
+
+    @Test
+        public void testNameAssignment() {
+            PreferenceNode foo = new com.phonegap.PreferenceNode("fullscreen", "false", false);
+            foo.name = "widescreen";
+            assertEquals("widescreen", foo.name);
+        }
+
+    @Test
+        public void testValueAssignment() {
+            PreferenceNode foo = new com.phonegap.PreferenceNode("fullscreen", "false", false);
+            foo.value = "maybe";
+            assertEquals("maybe", foo.value);
+        }
+
+    @Test
+        public void testReadonlyAssignment() {
+            PreferenceNode foo = new com.phonegap.PreferenceNode("fullscreen", "false", false);
+            foo.readonly = true;
+            assertEquals(true, foo.readonly);
+        }
+}

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/752b1b0e/framework/test/com/phonegap/PreferenceSetTest.java
----------------------------------------------------------------------
diff --git a/framework/test/com/phonegap/PreferenceSetTest.java b/framework/test/com/phonegap/PreferenceSetTest.java
new file mode 100644
index 0000000..fa56560
--- /dev/null
+++ b/framework/test/com/phonegap/PreferenceSetTest.java
@@ -0,0 +1,41 @@
+import org.junit.*;
+import static org.junit.Assert.*;
+
+import com.phonegap.PreferenceNode;
+import com.phonegap.PreferenceSet;
+
+public class PreferenceSetTest {
+    private PreferenceSet preferences;
+    private PreferenceNode screen;
+
+    @Before
+        public void setUp() {
+            preferences = new PreferenceSet();
+            screen = new PreferenceNode("fullscreen", "true", false);
+        }
+
+    @Test
+        public void testAddition() {
+            preferences.add(screen);
+            assertEquals(1, preferences.size());
+        }
+
+    @Test
+        public void testClear() {
+            preferences.add(screen);
+            preferences.clear();
+            assertEquals(0, preferences.size());
+        }
+
+    @Test
+        public void testPreferenceRetrieval() {
+            preferences.add(screen);
+            assertEquals("true", preferences.pref("fullscreen"));
+        }
+
+    @Test
+        public void testNoPreferenceRetrieval() {
+            // return null if the preference is not defined
+            assertEquals(null, preferences.pref("antigravity"));
+        }
+}


Mime
View raw message