Merge pull request #3 from alunny/preferences

add support for <preference name="fullscreen"> to res/xml/phonegap.xml
This commit is contained in:
Joe Bowser 2012-01-10 13:09:04 -08:00
commit cb0b054079
6 changed files with 188 additions and 12 deletions

5
.gitignore vendored
View File

@ -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
*.tmp

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,13 +317,27 @@ public class DroidGap extends PhonegapActivity {
*/
@Override
public void onCreate(Bundle savedInstanceState) {
preferences = new PreferenceSet();
// Load PhoneGap configuration:
// white list of allowed URLs
// debug setting
this.loadConfiguration();
LOG.d(TAG, "DroidGap.onCreate()");
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// This builds the view. We could probably get away with NOT having a LinearLayout, but I like having a bucket!
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
if (preferences.prefMatches("fullscreen","true")) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
// This builds the view. We could probably get away with NOT having a LinearLayout, but I like having a bucket!
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
@ -327,11 +347,6 @@ public class DroidGap extends PhonegapActivity {
root.setBackgroundColor(this.backgroundColor);
root.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT, 0.0F));
// Load PhoneGap configuration:
// white list of allowed URLs
// debug setting
this.loadConfiguration();
// If url was passed in to intent, then init webview, which will load the url
Bundle bundle = this.getIntent().getExtras();
@ -1880,7 +1895,18 @@ 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 != null &&
readonlyString.equals("true"));
LOG.i("PhoneGapLog", "Found preference for %s", name);
preferences.add(new PreferenceNode(name, value, readonly));
}
}
try {

View File

@ -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;
}
}

View File

@ -0,0 +1,43 @@
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;
}
public boolean prefMatches(String prefName, String prefValue) {
String value = pref(prefName);
if (value == null) {
return false;
} else {
return value.equals(prefValue);
}
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,55 @@
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"));
}
@Test
public void testUnsetPreferenceChecking() {
PreferenceSet emptySet = new PreferenceSet();
boolean value = emptySet.prefMatches("fullscreen", "true");
assertEquals(false, value);
}
@Test
public void testSetPreferenceChecking() {
preferences.add(screen);
boolean value = preferences.prefMatches("fullscreen", "true");
assertEquals(true, value);
}
}