-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphonegap.js
306 lines (269 loc) · 8.08 KB
/
phonegap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* Taken from phonegap.
*/
try {
var _anomFunkMap = {};
var _anoFunkMapNextId = 0;
function anomToNameFunk(fun) {
var funkId = "f" + _anomFunkMapNextId++;
var funk = function() {
fun.apply(this,arguments);
_anomFunkMap[funkId] = null;
delete _anomFunkMap[funkId];
}
_anomFunkMap[funkId] = funk;
return "_anomFunkMap."+funkId;
}
function GetFunctionName(fn) {
if(fn) {
// var m = fn.toString().match(/^\s*function\s+([^\(]+)/);
return m ? m[1] : anomToNameFunk(fn);
} else {
return null;
}
}
if (typeof(DeviceInfo) != 'object')
DeviceInfo = {};
/**
* This represent the PhoneGap API itself, and provides a global namespace for accessing
* information about the state of PhoneGap.
* @class
*/
PhoneGap = {
queue: {
ready: true,
commands: [],
timer: null
},
_constructors: []
};
/**
* Boolean flag indicating if the PhoneGap API is available and initialized.
*/ // TODO: Remove this, it is unused here ... -jm
PhoneGap.available = DeviceInfo.uuid != undefined;
/**
* Add an initialization function to a queue that ensures it will run and initialize
* application constructors only once PhoneGap has been initialized.
* @param {Function} func The function callback you want run once PhoneGap is initialized
*/
PhoneGap.addConstructor = function(func) {
var state = document.readyState;
if ( ( state == 'loaded' || state == 'complete' ) && DeviceInfo.uuid != null )
{
func();
}
else
{
PhoneGap._constructors.push(func);
}
};
(function()
{
var timer = setInterval(function()
{
var state = document.readyState;
if ( ( state == 'loaded' || state == 'complete' ) && DeviceInfo.uuid != null )
{
clearInterval(timer); // stop looking
// run our constructors list
while (PhoneGap._constructors.length > 0)
{
var constructor = PhoneGap._constructors.shift();
try
{
constructor();
}
catch(e)
{
if (typeof(debug['log']) == 'function')
{
debug.log("Failed to run constructor: " + debug.processMessage(e));
}
else
{
alert("Failed to run constructor: " + e.message);
}
}
}
// all constructors run, now fire the deviceready event
var e = document.createEvent('Events');
e.initEvent('deviceready');
document.dispatchEvent(e);
}
}, 1);
})();
/**
* Execute a PhoneGap command in a queued fashion, to ensure commands do not
* execute with any race conditions, and only run when PhoneGap is ready to
* recieve them.
* @param {String} command Command to be run in PhoneGap, e.g. "ClassName.method"
* @param {String[]} [args] Zero or more arguments to pass to the method
* object paramters are passed as an array object [object1, object2] each object will be passed as JSON strings
*/
PhoneGap.exec = function() {
PhoneGap.queue.commands.push(arguments);
if (PhoneGap.queue.timer == null)
PhoneGap.queue.timer = setInterval(PhoneGap.run_command, 10);
};
/**
* Internal function used to dispatch the request to PhoneGap. It processes the
* command queue and executes the next command on the list. Simple parameters are passed
* as arguments on the url. JavaScript objects converted into a JSON string and passed as a
* query string argument of the url.
* @private
*/
PhoneGap.run_command = function() {
if (!PhoneGap.available || !PhoneGap.queue.ready)
return;
PhoneGap.queue.ready = false;
var args = PhoneGap.queue.commands.shift();
if (PhoneGap.queue.commands.length == 0) {
clearInterval(PhoneGap.queue.timer);
PhoneGap.queue.timer = null;
}
var uri = [];
var dict = null;
for (var i = 1; i < args.length; i++) {
var arg = args[i];
if (arg == undefined || arg == null)
arg = '';
if (typeof(arg) == 'object')
dict = arg;
else
uri.push(encodeURIComponent(arg));
}
var url = "gap://" + args[0] + "/" + uri.join("/");
if (dict != null) {
url += "?" + encodeURIComponent(JSON.stringify(dict));
}
document.location = url;
};
/**
* This class provides access to the debugging console.
* @constructor
*/
function DebugConsole(isDeprecated) {
this.logLevel = DebugConsole.INFO_LEVEL;
this.isDeprecated = isDeprecated ? true : false;
}
// from most verbose, to least verbose
DebugConsole.ALL_LEVEL = 1; // same as first level
DebugConsole.INFO_LEVEL = 1;
DebugConsole.WARN_LEVEL = 2;
DebugConsole.ERROR_LEVEL = 4;
DebugConsole.NONE_LEVEL = 8;
DebugConsole.prototype.setLevel = function(level) {
this.logLevel = level;
}
/**
* Utility function for rendering and indenting strings, or serializing
* objects to a string capable of being printed to the console.
* @param {Object|String} message The string or object to convert to an indented string
* @private
*/
DebugConsole.prototype.processMessage = function(message) {
if (typeof(message) != 'object') {
return (this.isDeprecated ? "WARNING: debug object is deprecated, please use console object \n" + message : message);
} else {
/**
* @function
* @ignore
*/
function indent(str) {
return str.replace(/^/mg, " ");
}
/**
* @function
* @ignore
*/
function makeStructured(obj) {
var str = "";
for (var i in obj) {
try {
if (typeof(obj[i]) == 'object') {
str += i + ":\n" + indent(makeStructured(obj[i])) + "\n";
} else {
str += i + " = " + indent(String(obj[i])).replace(/^ /, "") + "\n";
}
} catch(e) {
str += i + " = EXCEPTION: " + e.message + "\n";
}
}
return str;
}
return ((this.isDeprecated ? "WARNING: debug object is deprecated, please use console object\n" : "") + "Object:\n" + makeStructured(message));
}
};
/**
* Print a normal log message to the console
* @param {Object|String} message Message or object to print to the console
*/
DebugConsole.prototype.log = function(message) {
if (PhoneGap.available && this.logLevel <= DebugConsole.INFO_LEVEL)
PhoneGap.exec('DebugConsole.log',
this.processMessage(message),
{ logLevel: 'INFO' }
);
else
console.log(message);
};
/**
* Print a warning message to the console
* @param {Object|String} message Message or object to print to the console
*/
DebugConsole.prototype.warn = function(message) {
if (PhoneGap.available && this.logLevel <= DebugConsole.WARN_LEVEL)
PhoneGap.exec('DebugConsole.log',
this.processMessage(message),
{ logLevel: 'WARN' }
);
else
console.error(message);
};
/**
* Print an error message to the console
* @param {Object|String} message Message or object to print to the console
*/
DebugConsole.prototype.error = function(message) {
if (PhoneGap.available && this.logLevel <= DebugConsole.ERROR_LEVEL)
PhoneGap.exec('DebugConsole.log',
this.processMessage(message),
{ logLevel: 'ERROR' }
);
else
console.error(message);
};
PhoneGap.addConstructor(function() {
window.console = new DebugConsole();
window.debug = new DebugConsole(true);
});
/**
* this represents the mobile device, and provides properties for inspecting the model, version, UUID of the
* phone, etc.
* @constructor
*/
function Device()
{
this.platform = null;
this.version = null;
this.name = null;
this.phonegap = null;
this.uuid = null;
try
{
this.platform = DeviceInfo.platform;
this.version = DeviceInfo.version;
this.name = DeviceInfo.name;
this.phonegap = DeviceInfo.gap;
this.uuid = DeviceInfo.uuid;
}
catch(e)
{
// TODO:
}
this.available = PhoneGap.available = this.uuid != null;
}
PhoneGap.addConstructor(function() {
navigator.device = window.device = new Device();
});
} catch(err) { alert('error : ' + err.description); }