Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4caacc5

Browse files
committed
Merge branch 'master' of git://github.com/filmaj/phonegap
2 parents 6a263ec + 2a333dd commit 4caacc5

17 files changed

Lines changed: 956 additions & 185 deletions

blackberry/src/com/nitobi/phonegap/PhoneGap.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
package com.nitobi.phonegap;
2424

2525
import java.io.IOException;
26+
import java.util.Timer;
27+
import java.util.TimerTask;
2628
import java.util.Vector;
2729

2830
import javax.microedition.io.HttpConnection;
@@ -62,11 +64,12 @@ public class PhoneGap extends UiApplication implements RenderingApplication {
6264
public static final String PHONEGAP_PROTOCOL = "PhoneGap=";
6365
private static final String DEFAULT_INITIAL_URL = "data:///www/test/index.html";
6466
private static final String REFERER = "referer";
65-
private Vector pendingResponses = new Vector();
66-
private CommandManager commandManager = new CommandManager();
67+
public Vector pendingResponses = new Vector();
68+
private CommandManager commandManager;
6769
private RenderingSession _renderingSession;
6870
public HttpConnection _currentConnection;
6971
private MainScreen _mainScreen;
72+
private Timer refreshTimer;
7073

7174
/**
7275
* Launches the application. Accepts up to one parameter, an URL to the index page.
@@ -93,6 +96,7 @@ public PhoneGap(final String url) {
9396
}
9497

9598
private void init(final String url) {
99+
commandManager = new CommandManager(this);
96100
_mainScreen = new MainScreen();
97101
pushScreen(_mainScreen);
98102
_renderingSession = RenderingSession.getNewInstance();
@@ -103,7 +107,9 @@ private void init(final String url) {
103107
// Enable nice-looking BB browser field.
104108
_renderingSession.getRenderingOptions().setProperty(RenderingOptions.CORE_OPTIONS_GUID, 17000, true);
105109
PrimaryResourceFetchThread thread = new PrimaryResourceFetchThread(url, null, null, null, this);
106-
thread.start();
110+
thread.start();
111+
refreshTimer = new Timer();
112+
refreshTimer.scheduleAtFixedRate(new TimerRefresh(), 500, 500);
107113
}
108114
public Object eventOccurred(final Event event)
109115
{
@@ -232,7 +238,7 @@ public int getHistoryPosition(BrowserContent browserContent) {
232238
public HttpConnection getResource(RequestedResource resource, BrowserContent referrer) {
233239
if ((resource != null) && (resource.getUrl() != null) && !resource.isCacheOnly()) {
234240
String url = resource.getUrl().trim();
235-
if ((referrer == null) || (ConnectionManager.isInternal(url)))
241+
if ((referrer == null) || (ConnectionManager.isInternal(url, resource)))
236242
return ConnectionManager.getUnmanagedConnection(url, resource.getRequestHeaders(), null);
237243
else
238244
SecondaryResourceFetchThread.enqueue(resource, referrer);
@@ -262,6 +268,7 @@ public void processConnection(HttpConnection connection, Event e)
262268
try
263269
{
264270
browserContent = _renderingSession.getBrowserContent(connection, this, e);
271+
265272
if (browserContent != null)
266273
{
267274
Field field = browserContent.getDisplayableContent();
@@ -326,4 +333,22 @@ public static final String[] splitString(final String data, final char splitChar
326333
v.copyInto(result);
327334
return result;
328335
}
336+
private class TimerRefresh extends TimerTask
337+
{
338+
public void run()
339+
{
340+
UiApplication.getUiApplication().invokeLater(new Runnable()
341+
{
342+
public void run()
343+
{
344+
int numFields = _mainScreen.getFieldCount();
345+
for (int i = 0; i < numFields; i++) {
346+
Field field = _mainScreen.getField(i);
347+
field.getManager().invalidate();
348+
}
349+
_mainScreen.doPaint();
350+
}
351+
});
352+
}
353+
}
329354
}

blackberry/src/com/nitobi/phonegap/api/CommandManager.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
*/
2323
package com.nitobi.phonegap.api;
2424

25+
import com.nitobi.phonegap.PhoneGap;
2526
import com.nitobi.phonegap.api.impl.CameraCommand;
2627
import com.nitobi.phonegap.api.impl.ContactsCommand;
2728
import com.nitobi.phonegap.api.impl.DeviceCommand;
2829
import com.nitobi.phonegap.api.impl.GeoLocationCommand;
2930
import com.nitobi.phonegap.api.impl.MediaCommand;
31+
import com.nitobi.phonegap.api.impl.NetworkCommand;
3032
import com.nitobi.phonegap.api.impl.NotificationCommand;
3133
import com.nitobi.phonegap.api.impl.TelephonyCommand;
3234

@@ -39,16 +41,17 @@
3941
public final class CommandManager {
4042

4143
// List of installed Commands
42-
private Command[] commands = new Command[7];
44+
private Command[] commands = new Command[8];
4345

44-
public CommandManager() {
45-
commands[0] = new CameraCommand();
46+
public CommandManager(PhoneGap phoneGap) {
47+
commands[0] = new CameraCommand(phoneGap);
4648
commands[1] = new ContactsCommand();
4749
commands[2] = new NotificationCommand();
4850
commands[3] = new TelephonyCommand();
4951
commands[4] = new GeoLocationCommand();
5052
commands[5] = new DeviceCommand();
5153
commands[6] = new MediaCommand();
54+
commands[7] = new NetworkCommand();
5255
}
5356

5457
/**

blackberry/src/com/nitobi/phonegap/api/impl/CameraCommand.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@
2222
*/
2323
package com.nitobi.phonegap.api.impl;
2424

25+
import java.io.ByteArrayOutputStream;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
29+
import javax.microedition.io.Connector;
30+
import javax.microedition.io.file.FileConnection;
31+
2532
import net.rim.blackberry.api.invoke.CameraArguments;
2633
import net.rim.blackberry.api.invoke.Invoke;
34+
import net.rim.device.api.io.Base64OutputStream;
2735
import net.rim.device.api.io.file.FileSystemJournal;
2836
import net.rim.device.api.io.file.FileSystemJournalEntry;
2937
import net.rim.device.api.io.file.FileSystemJournalListener;
@@ -32,6 +40,7 @@
3240
import net.rim.device.api.system.EventInjector;
3341
import net.rim.device.api.ui.UiApplication;
3442

43+
import com.nitobi.phonegap.PhoneGap;
3544
import com.nitobi.phonegap.api.Command;
3645

3746
/**
@@ -42,26 +51,51 @@
4251
*/
4352
public class CameraCommand implements Command {
4453

45-
private static final int INVOKE_COMMAND = 0;
46-
private static final int PICTURE_COMMAND = 1;
54+
private static final int PICTURE_COMMAND = 0;
4755
private static final String CODE = "PhoneGap=camera";
48-
//private static final String
56+
private static final String CAMERA_ERROR_CALLBACK = ";if (navigator.camera.onError) { navigator.camera.onError(); }";
4957

5058
private long lastUSN = 0;
5159
private String photoPath;
60+
private String returnVal;
5261
private FileSystemJournalListener listener;
62+
private PhoneGap berryGap;
5363

54-
public CameraCommand() {
64+
public CameraCommand(PhoneGap phoneGap) {
65+
berryGap = phoneGap;
5566
listener = new FileSystemJournalListener() {
5667
public void fileJournalChanged() {
5768
long USN = FileSystemJournal.getNextUSN();
5869
for (long i = USN - 1; i >= lastUSN; --i) {
5970
FileSystemJournalEntry entry = FileSystemJournal.getEntry(i);
6071
if (entry != null) {
61-
if (entry.getEvent() == FileSystemJournalEntry.FILE_ADDED || entry.getEvent() == FileSystemJournalEntry.FILE_CHANGED || entry.getEvent() == FileSystemJournalEntry.FILE_RENAMED) {
72+
if (entry.getEvent() == FileSystemJournalEntry.FILE_CHANGED) {
6273
if (entry.getPath().indexOf(".jpg") != -1) {
6374
lastUSN = USN;
6475
photoPath = entry.getPath();
76+
77+
InputStream theImage;
78+
byte[] imageBytes;
79+
Base64OutputStream base64OutputStream = null;
80+
try {
81+
FileConnection fconn = (FileConnection)Connector.open("file://" + photoPath);
82+
imageBytes = new byte[(int) fconn.fileSize()];
83+
theImage = fconn.openInputStream();
84+
theImage.read(imageBytes);
85+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream( imageBytes.length );
86+
base64OutputStream = new Base64OutputStream( byteArrayOutputStream );
87+
base64OutputStream.write(imageBytes);
88+
base64OutputStream.flush();
89+
base64OutputStream.close();
90+
byteArrayOutputStream.flush();
91+
byteArrayOutputStream.close();
92+
//int sizeofbase64 = byteArrayOutputStream.toString().length();
93+
returnVal = ";if (navigator.camera.onSuccess) { navigator.camera.onSuccess('"+byteArrayOutputStream.toString()+"'); }";
94+
} catch (IOException e) {
95+
e.printStackTrace();
96+
returnVal = CAMERA_ERROR_CALLBACK;
97+
}
98+
berryGap.pendingResponses.addElement(returnVal);
6599
closeCamera();
66100
}
67101
}
@@ -81,25 +115,24 @@ public boolean accept(String instruction) {
81115
public String execute(String instruction) {
82116
switch (getCommand(instruction)) {
83117
case PICTURE_COMMAND:
84-
UiApplication.getUiApplication().removeFileSystemJournalListener(listener);
85-
return ";navigator.camera.picture = '" + photoPath + "';";
86-
case INVOKE_COMMAND:
87118
photoPath = null;
119+
returnVal = null;
88120
UiApplication.getUiApplication().addFileSystemJournalListener(listener);
89121
Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA, new CameraArguments());
122+
return "";
90123
}
91124
return null;
92125
}
93126

94127
private int getCommand(String instruction) {
95128
String command = instruction.substring(instruction.lastIndexOf('/') + 1);
96-
if ("obtain".equals(command)) return INVOKE_COMMAND;
97129
if ("picture".equals(command)) return PICTURE_COMMAND;
98130
return -1;
99131
}
100132

101133
public void closeCamera() {
102134
try {
135+
UiApplication.getUiApplication().removeFileSystemJournalListener(listener);
103136
EventInjector.KeyEvent inject = new EventInjector.KeyEvent(EventInjector.KeyEvent.KEY_DOWN, Characters.ESCAPE, 0);
104137
inject.post();
105138
inject.post();

blackberry/src/com/nitobi/phonegap/api/impl/ContactsCommand.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,24 +243,46 @@ private String getAgenda(Hashtable options) {
243243
}
244244
private static void addContactToBuffer(StringBuffer buff, BlackBerryContact contact) {
245245
// TODO: Eventually extend this to return proper labels/values for differing phone/email types.
246-
buff.append("{email:[{'label':'mobile','value':'");
247-
buff.append(contact.getString(Contact.EMAIL, 0));
248-
buff.append("'}], phoneNumber:[{'label':'mobile','value':'");
249-
buff.append(contact.getString(Contact.TEL, 0));
250-
buff.append("'}], firstName:'");
246+
buff.append("{");
247+
if (contact.countValues(Contact.EMAIL) > 0) {
248+
buff.append("email:[{'label':'mobile','value':'");
249+
buff.append(contact.getString(Contact.EMAIL, 0));
250+
buff.append("'}]");
251+
}
252+
final int numValues = contact.countValues(Contact.TEL);
253+
if (numValues > 0) {
254+
boolean sentinel = false;
255+
String phoneMobile = "";
256+
for (int index = 0; index < numValues; index++)
257+
{
258+
final int curAttributes = contact.getAttributes(Contact.TEL, index);
259+
// TODO: For now, we are only looking for the mobile contact number.
260+
if ((curAttributes & Contact.ATTR_MOBILE) == Contact.ATTR_MOBILE)
261+
{
262+
phoneMobile = contact.getString(Contact.TEL, index);
263+
sentinel = true;
264+
break;
265+
}
266+
}
267+
if (sentinel) {
268+
if (buff.length() > 1) buff.append(",");
269+
buff.append("phoneNumber:[{'label':'mobile','value':'");
270+
buff.append(phoneMobile);
271+
buff.append("'}]");
272+
}
273+
}
251274
// See if there is a meaningful name set for the contact.
252275
if (contact.countValues(Contact.NAME) > 0) {
276+
if (buff.length() > 1) buff.append(",");
277+
buff.append("firstName:'");
253278
final String[] name = contact.getStringArray(Contact.NAME, 0);
254279
final String firstName = name[Contact.NAME_GIVEN];
255280
final String lastName = name[Contact.NAME_FAMILY];
256-
if (firstName != null) buff.append(firstName + "',lastName:'");
257-
else buff.append("',lastName:'");
258-
if (lastName != null) buff.append(lastName + "',");
259-
else buff.append("',");
260-
} else {
261-
buff.append("',lastName:''");
281+
buff.append((firstName != null ? firstName : "") + "',lastName:'");
282+
buff.append((lastName != null ? lastName : "") + "'");
262283
}
263-
buff.append(",address:'");
284+
if (buff.length() > 1) buff.append(",");
285+
buff.append("address:'");
264286
// Build up a meaningful address field.
265287
if (contact.countValues(Contact.ADDR) > 0) {
266288
String address = "";
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* The MIT License
3+
* -------------------------------------------------------------
4+
* Copyright (c) 2008, Rob Ellis, Brock Whitten, Brian Leroux, Joe Bowser, Dave Johnson, Nitobi
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.nitobi.phonegap.api.impl;
24+
25+
import net.rim.device.api.system.RadioInfo;
26+
27+
import com.nitobi.phonegap.api.Command;
28+
29+
/**
30+
* Vibrates the phone if able.
31+
*
32+
* @author Jose Noheda
33+
*
34+
*/
35+
public class NetworkCommand implements Command {
36+
37+
private static final int REACHABLE_COMMAND = 0;
38+
private static final String CODE = "PhoneGap=network";
39+
private static final int NOT_REACHABLE = 0;
40+
private static final int REACHABLE_VIA_CARRIER_DATA_NETWORK = 1;
41+
private static final int REACHABLE_VIA_WIFI_NETWORK = 2;
42+
43+
/**
44+
* Determines whether the specified instruction is accepted by the command.
45+
* @param instruction The string instruction passed from JavaScript via cookie.
46+
* @return true if the Command accepts the instruction, false otherwise.
47+
*/
48+
public boolean accept(String instruction) {
49+
return instruction != null && instruction.startsWith(CODE);
50+
}
51+
52+
public String execute(String instruction) {
53+
switch (getCommand(instruction)) {
54+
case REACHABLE_COMMAND:
55+
if (RadioInfo.isDataServiceOperational()) {
56+
// Data services available - determine what service to use.
57+
int service = RadioInfo.getNetworkType();
58+
int reachability = NOT_REACHABLE;
59+
if ((service & RadioInfo.NETWORK_802_11) != 0) {
60+
reachability = REACHABLE_VIA_WIFI_NETWORK;
61+
}
62+
if ((service & RadioInfo.NETWORK_GPRS) != 0 || (service & RadioInfo.NETWORK_UMTS) != 0 ) {
63+
reachability = REACHABLE_VIA_CARRIER_DATA_NETWORK;
64+
}
65+
return ";navigator.network.lastReachability = "+reachability+";if (navigator.network.isReachable_success) navigator.network.isReachable_success("+reachability+");";
66+
} else {
67+
// No data services - unreachable.
68+
return ";navigator.network.lastReachability = NetworkStatus.NOT_REACHABLE;if (navigator.network.isReachable_success) navigator.network.isReachable_success(NetworkStatus.NOT_REACHABLE);";
69+
}
70+
}
71+
return null;
72+
}
73+
private int getCommand(String instruction) {
74+
String command = instruction.substring(CODE.length()+1);
75+
if (command.startsWith("reach")) return REACHABLE_COMMAND;
76+
return -1;
77+
}
78+
79+
}

0 commit comments

Comments
 (0)