Menu

[r284]: / branches / hammer / player / StrobeMediaPlayback / html-template / lib / devicedetection.js  Maximize  Restore  History

Download this file

210 lines (189 with data), 6.9 kB

  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
/***********************************************************
* Copyright 2011 Adobe Systems Incorporated. All Rights Reserved.
*
* *********************************************************
* The contents of this file are subject to the Berkeley Software Distribution (BSD) Licence
* (the "License"); you may not use this file except in
* compliance with the License.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
*
* The Initial Developer of the Original Code is Adobe Systems Incorporated.
* Portions created by Adobe Systems Incorporated are Copyright (C) 2011 Adobe Systems
* Incorporated. All Rights Reserved.
*
**********************************************************/
/**
* Constructor
* @param userAgent Override the default detection of the user agent
*/
function DeviceDetection(userAgent){
/** Storage for callbacks */
this.callbacks = new Array();
/** Selected user agent */
this.userAgent = userAgent && typeof(userAgent) == "string" ? userAgent : navigator.userAgent;
/** Profiles list */
this.profiles = [];
/** Default profile */
this.selectedProfile = this.profiles[0];
};
/**
* Loads and parses JSON. If a 'successCallback' is not provided, the
* Expected format: <profile name="{string}" mobile="{boolean}" flash="{boolean}">{regex}</profile>
* @param xml XML to process
*/
DeviceDetection.prototype.addProfiles = function(list){
var len = list.settings.length;
var value;
var settings = {};
for(var i = 0; i < len; i++){
value = list.settings[i];
settings[value.type] = { quality: value.quality,
flashenabled: value.flashenabled == "true",
flashfirst: value.flashfirst == "true"};
}
len = list.profiles.length;
for(var i = 0; i < len; i++){
value = list.profiles[i];
//generate a profile object
profile = { name: value.name,
type: value.type,
regex: value.regex};
var quality = value.quality;
switch(quality){
case "hd":
case "sd":
case "mobile":
profile.quality = quality;
break;
default:
profile.quality = settings[profile.type] == null ? "sd" : settings[profile.type].quality;
break;
}
profile.flashenabled = value.flashenabled == "undefined" ? settings[profile.type].flashenabled == "true" : value.flashenabled == "true";
profile.flashfirst = value.flashfirst == "undefined" ? settings[profile.type].flashfirst == "true" : value.flashfirst == "true";
this.addProfile(profile);
}
return this;
}
/**
* Loads and parses XML. If a 'successCallback' is not provided, the
* Expected format: <profile name="{string}" mobile="{boolean}" flash="{boolean}">{regex}</profile>
* @param xml XML to process
*/
DeviceDetection.prototype.loadProfilesXML = function(url, successCallback){
if(!jQuery){
if(console) console.error("Could not find jQuery.");
return false;
}
//create a proxy so it can be accessed inside the jquery loop (each)
var thisProxy = this;
//use jquery to load the profiles xml
$.get(url,
function(data){ //result function
var profile;
var settings = {};
$(data).find("setting").each(function(index){
settings[$(this).attr("type")] = { quality: $(this).attr("quality"),
flashenabled: $(this).attr("flashenabled") == "true",
flashfirst: $(this).attr("flashfirst") == "true"};
});
$(data).find("profile").each(function(index){ //loop over each profile
//generate a profile object
profile = { name: $(this).attr("name"),
type: $(this).attr("type"),
regex: $(this).text()};
var quality = $(this).attr("quality");
switch(quality){
case "hd":
case "sd":
case "mobile":
profile.quality = quality;
break;
default:
profile.quality = settings[profile.type] == null ? "sd" : settings[profile.type].quality;
break;
}
profile.flashenabled = $(this).attr("flashenabled") == "undefined" ? settings[profile.type].flashenabled == "true" : $(this).attr("flashenabled") == "true";
profile.flashfirst = $(this).attr("flashfirst") == "undefined" ? settings[profile.type].flashfirst == "true" : $(this).attr("flashfirst") == "true";
thisProxy.addProfile(profile);
});
if(typeof(successCallback) == "function") successCallback(thisProxy);
}, "xml");
}
/**
* Adds a callback to handle the execute result
*/
DeviceDetection.prototype.addCallback = function(callback){
if(typeof(callback) == "function"){
this.callbacks.push(callback);
}
return this; //allow method chaining
}
/**
* Executes processing of the userAgent
*/
DeviceDetection.prototype.detect = function(){
var thisProxy = this;
var len = this.profiles.length;
var item;
for(var i = 0; i < len; i++){
item = this.profiles[i];
if(thisProxy.userAgent.search(item.regex) != -1){
this.selectedProfile = item;
break;
}
}
if(this.selectedProfile == null){
//TODO handle this case
}
//Protect from user generated errors
if(this.callbacks && typeof(this.callbacks) == "object" && this.callbacks.length > 0){
var len = this.callbacks.length;
for(var i = 0; i < len; i++){
try{
this.callbacks[i](this);
}catch(error){
if(console) console.log("[Error] " + error.message);
}
}
}
return this; //allow method chaining
};
/**
* Set a specific profile to override the defaults
* @param name Profile name
* @param profile Object containing valid profile values
*/
DeviceDetection.prototype.addProfile = function(profile){
if(typeof(profile) != "object"){
if(console) console.error("Setting a profile requires a valid object.");
return this; //object must be an Object
}
//TODO require all fields here?
if(typeof(profile.regex) == "string"){
profile.regex = new RegExp(profile.regex, "i"); //should we make the regex flags dynamic?
}
this.profiles.push(profile);
return this;
}
//ACCESSORS
DeviceDetection.prototype.profileDetected = function(){
return this.selectedProfile != null;
}
/**
* Simple call to determine whether to use Flash or not
*/
DeviceDetection.prototype.useFlash = function(){
if(this.flashFirst() && this.flashEnabled()) return true;
else return this.flashEnabled();
}
DeviceDetection.prototype.getProfile = function(){ return this.selectedProfile; }
DeviceDetection.prototype.type = function(){ return this.profileDetected() ? this.selectedProfile.type : null; }
DeviceDetection.prototype.flashEnabled = function(){ return this.profileDetected() ? this.selectedProfile.flashenabled : null; }
DeviceDetection.prototype.flashFirst = function(){ return this.profileDetected() ? this.selectedProfile.flashfirst : null; }
DeviceDetection.prototype.isMobile = function(){ return this.profileDetected() ? this.selectedProfile.type == "mobile" : false; }
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.