Permalink
Browse files

Added Network command to BlackBerry branch, which implements a reacha…

…bility API. Allows users to check if a network connection (either via WiFi or carrier-based networks) is available.
  • Loading branch information...
1 parent 93d4fd6 commit 2a333dd06829cc89204e8fd9fe0119f7c2ab1fdd @filmaj filmaj committed Sep 14, 2009
View
4 blackberry/src/com/nitobi/phonegap/api/CommandManager.java
@@ -28,6 +28,7 @@
import com.nitobi.phonegap.api.impl.DeviceCommand;
import com.nitobi.phonegap.api.impl.GeoLocationCommand;
import com.nitobi.phonegap.api.impl.MediaCommand;
+import com.nitobi.phonegap.api.impl.NetworkCommand;
import com.nitobi.phonegap.api.impl.NotificationCommand;
import com.nitobi.phonegap.api.impl.TelephonyCommand;
@@ -40,7 +41,7 @@
public final class CommandManager {
// List of installed Commands
- private Command[] commands = new Command[7];
+ private Command[] commands = new Command[8];
public CommandManager(PhoneGap phoneGap) {
commands[0] = new CameraCommand(phoneGap);
@@ -50,6 +51,7 @@ public CommandManager(PhoneGap phoneGap) {
commands[4] = new GeoLocationCommand();
commands[5] = new DeviceCommand();
commands[6] = new MediaCommand();
+ commands[7] = new NetworkCommand();
}
/**
View
79 blackberry/src/com/nitobi/phonegap/api/impl/NetworkCommand.java
@@ -0,0 +1,79 @@
+/**
+ * The MIT License
+ * -------------------------------------------------------------
+ * Copyright (c) 2008, Rob Ellis, Brock Whitten, Brian Leroux, Joe Bowser, Dave Johnson, Nitobi
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.nitobi.phonegap.api.impl;
+
+import net.rim.device.api.system.RadioInfo;
+
+import com.nitobi.phonegap.api.Command;
+
+/**
+ * Vibrates the phone if able.
+ *
+ * @author Jose Noheda
+ *
+ */
+public class NetworkCommand implements Command {
+
+ private static final int REACHABLE_COMMAND = 0;
+ private static final String CODE = "PhoneGap=network";
+ private static final int NOT_REACHABLE = 0;
+ private static final int REACHABLE_VIA_CARRIER_DATA_NETWORK = 1;
+ private static final int REACHABLE_VIA_WIFI_NETWORK = 2;
+
+ /**
+ * Determines whether the specified instruction is accepted by the command.
+ * @param instruction The string instruction passed from JavaScript via cookie.
+ * @return true if the Command accepts the instruction, false otherwise.
+ */
+ public boolean accept(String instruction) {
+ return instruction != null && instruction.startsWith(CODE);
+ }
+
+ public String execute(String instruction) {
+ switch (getCommand(instruction)) {
+ case REACHABLE_COMMAND:
+ if (RadioInfo.isDataServiceOperational()) {
+ // Data services available - determine what service to use.
+ int service = RadioInfo.getNetworkType();
+ int reachability = NOT_REACHABLE;
+ if ((service & RadioInfo.NETWORK_802_11) != 0) {
+ reachability = REACHABLE_VIA_WIFI_NETWORK;
+ }
+ if ((service & RadioInfo.NETWORK_GPRS) != 0 || (service & RadioInfo.NETWORK_UMTS) != 0 ) {
+ reachability = REACHABLE_VIA_CARRIER_DATA_NETWORK;
+ }
+ return ";navigator.network.lastReachability = "+reachability+";if (navigator.network.isReachable_success) navigator.network.isReachable_success("+reachability+");";
+ } else {
+ // No data services - unreachable.
+ return ";navigator.network.lastReachability = NetworkStatus.NOT_REACHABLE;if (navigator.network.isReachable_success) navigator.network.isReachable_success(NetworkStatus.NOT_REACHABLE);";
+ }
+ }
+ return null;
+ }
+ private int getCommand(String instruction) {
+ String command = instruction.substring(CODE.length()+1);
+ if (command.startsWith("reach")) return REACHABLE_COMMAND;
+ return -1;
+ }
+
+}

0 comments on commit 2a333dd

Please sign in to comment.