Permalink
Browse files

phonegap 1.0.0rc1-SNAPSHOT

  • Loading branch information...
1 parent b9d0643 commit a6b8f28ee06fb476ce6b43e1dc1a7a5780a8a49d @don committed Jul 16, 2011
View
@@ -3,7 +3,7 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
- <classpathentry kind="lib" path="/usr/local/phonegap/Android/phonegap.0.9.6.jar"/>
+ <classpathentry kind="lib" path="lib/phonegap-1.0.0rc1-SNAPSHOT.jar"/>
<classpathentry kind="lib" path="lib/phonegap-nfc.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
View
@@ -32,7 +32,7 @@
<application android:debuggable="true" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:configChanges="orientation|keyboardHidden" android:name=".RockPaperScissors"
- android:label="@string/app_name">
+ android:label="@string/app_name" android:screenOrientation="nosensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
View
@@ -8,7 +8,7 @@ Demo web app that allows you to play Rock Paper Scissors with another user via N
Requires
* Google Nexus S phone
-* PhoneGap 0.9.6+
+* PhoneGap 1.0
* phonegap-nfc plugin
See [phonegap-nfc](https://github.com/chariotsolutions/phonegap-nfc) for more info
View
@@ -4,8 +4,8 @@
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Rock Paper Scissors</title>
- <link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title" charset="utf-8">
- <script type="text/javascript" charset="utf-8" src="phonegap.0.9.6.js"></script>
+ <link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title" />
+ <script type="text/javascript" charset="utf-8" src="phonegap-1.0.0rc1-SNAPSHOT.js"></script>
<script type="text/javascript" charset="utf-8" src="phonegap-nfc.js"></script>
<script type="text/javascript" charset="utf-8" src="main.js"></script>
</head>
View
@@ -1,16 +1,23 @@
/*global NdefPlugin, Ndef */
-var choice = null;
+var choice = null,
+ listening = false,
+ mimeType = "game/rockpaperscissors";
-function onNfc(nfcEvent) {
- console.log(JSON.stringify(nfcEvent.tagData));
+function stop() {
+ navigator.nfc.unshareTag();
+ listening = false;
+ // TODO deselect button. blur doesn't work
+}
+function onNfc(nfcEvent) {
+ if (!listening) {
+ return;
+ }
+
var records = nfcEvent.tagData,
opponentsChoice = Ndef.bytesToString(records[0].payload),
result;
- // If choice is null, prompt the user or wait for a choice, then determine the winner
-
- // TODO clean up logic, doesn't handle edge cases
if (choice === opponentsChoice) {
result = "tie";
} else if (choice === "Rock" && opponentsChoice === "Scissors") {
@@ -24,34 +31,42 @@ function onNfc(nfcEvent) {
}
if (result === "tie") {
- alert("Tie! " + choice);
+ navigator.notification.alert(choice + " === " + opponentsChoice, stop, "Tie", "Meh");
} else if (result === "win") {
- alert("You WIN! " + choice + " beats " + opponentsChoice); // TODO grammar for Scissors
+ navigator.notification.alert(message(choice, opponentsChoice), stop, "You Win!", "OK");
} else {
- alert("You LOOSE! " + opponentsChoice + " beats " + choice);
+ navigator.notification.alert(message(opponentsChoice, choice), stop, "You Lose", "Bummer");
}
navigator.notification.vibrate(100);
}
+function message(win, lose) {
+ if (/s$/.test(win)) {
+ return win + " beat " + lose;
+ } else {
+ return win + " beats " + lose;
+ }
+}
+
function choose(text) {
choice = text;
var ndefMessage = [
- Ndef.mimeMediaRecord("game/rockpaperscissors", Ndef.stringToBytes(choice))
+ Ndef.mimeMediaRecord(mimeType, Ndef.stringToBytes(choice))
];
- window.plugins.NdefPlugin.p2p(
+ navigator.nfc.shareTag(
ndefMessage,
function () {
navigator.notification.vibrate(100);
}, function () {
alert("Failed to share tag.");
}
);
+ listening = true;
}
var ready = function () {
- var mimeType = "game/rockpaperscissors",
- buttons = document.getElementsByTagName('button');
+ var buttons = document.getElementsByTagName('button');
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", function () { choose(this.innerHTML); }, false);
@@ -65,12 +80,8 @@ var ready = function () {
alert('Failed to register mime type ' + mimeType + ' with NFC');
}
- window.plugins.NdefPlugin.addMimeTypeListener(mimeType, onNfc, win, fail);
- // don't want this but need as a hack
- window.plugins.NdefPlugin.addNdefFormattableListener(function () { alert("This tag is formattable"); });
+ navigator.nfc.addMimeTypeListener(mimeType, onNfc, win, fail);
};
-// deviceready is being called before the plugins finish initializing
-// add setTimeout as a kludge until the real problem is fixed
-document.addEventListener('deviceready', function () { window.setTimeout(ready, 500); }, false);
+document.addEventListener('deviceready', ready, false);
Oops, something went wrong.

0 comments on commit a6b8f28

Please sign in to comment.