View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // GenomeBrowser Project
18  //
19  // BrowserInfo.java
20  // Since: Jun 18, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.util;
26  
27  import java.util.HashMap;
28  
29  /**
30   * URL query string reader. For example, .../UTGB.html?species=human&revision=hg18
31   * 
32   * @author leo
33   * 
34   */
35  public class BrowserInfo {
36  	public static HashMap<String, String> getURLQueryRequestParameters() {
37  		HashMap<String, String> properties = new HashMap<String, String>();
38  		String query = getQueryString();
39  		if (query == null || query.length() < 1)
40  			return properties;
41  
42  		String queryPart = query.substring(1);
43  		String[] keyAndValue = queryPart.split("&");
44  		for (int i = 0; i < keyAndValue.length; i++) {
45  			String[] kv = keyAndValue[i].split("=");
46  			if (kv.length > 1)
47  				properties.put(kv[0], unescape(kv[1]));
48  			else
49  				properties.put(kv[0], "");
50  		}
51  
52  		return properties;
53  	}
54  
55  	public native static String unescape(String s) /*-{
56  	      return unescape(s);
57  	   }-*/;
58  
59  	public static native void setQueryString(String qs) /*-{
60  	   	$wnd.location.search = qs;
61  	   }-*/;
62  
63  	public static native String getQueryString() /*-{
64  	    return $wnd.location.search;
65  	    }-*/;
66  
67  	public static native String getProtocol() /*-{
68  	    return $wnd.location.protocol;
69  	    }-*/;
70  
71  	public static native String getPort() /*-{
72  	    return $wnd.location.port;
73  	    }-*/;
74  
75  	public static native String getPath() /*-{
76  	    return $wnd.location.pathname;
77  	    }-*/;
78  
79  	public static native String getHref() /*-{
80  	    return $wnd.location.href;
81  	    }-*/;
82  
83  	public static native String getHostName() /*-{
84  	    return $wnd.location.hostname;
85  	    }-*/;
86  
87  	public static native String getHost() /*-{
88  	    return $wnd.location.host;
89  	    }-*/;
90  
91  	public static native String getHash() /*-{
92  	    return $wnd.location.hash;
93  	    }-*/;
94  
95  	public static native boolean isGoogleGearsInstalled() /*-{
96  	       return ($wnd.google && google.gears);
97  	   }-*/;
98  
99  	public static native String getUserAgent() /*-{
100 			return navigator.userAgent;
101 	}-*/;
102 
103 	private static Boolean hasCanvasSupport = null;
104 
105 	public static boolean isCanvasSupported() {
106 		if (hasCanvasSupport == null)
107 			hasCanvasSupport = isCanvasSupportedInternal();
108 		return hasCanvasSupport.booleanValue();
109 	}
110 
111 	private static native boolean isCanvasSupportedInternal() /*-{
112 		return !!document.createElement('canvas').getContext;
113 	}-*/;
114 
115 	public static enum Agent {
116 		Unknown, Firefox, Safari, Opera, Chrome, IE, MobileSafari
117 	}
118 	
119 	public static native boolean isMobileSafari() /*-{
120 	      var ua = navigator.userAgent.toLowerCase();
121 	      if (ua.indexOf("webkit") != -1 && ua.indexOf("mobile") != -1) {
122 	        return true;
123 	      }
124 	      return false;
125 	}-*/;
126 
127 	private static Agent agent = null;
128 	
129 	/**
130 	 * Retrieve a short name, suitable for use in a tab or filename, for a given user agent.
131 	 * 
132 	 * @param userAgent
133 	 * @return short name of user agent
134 	 */
135 	public static Agent getBrowserType() {
136 		if(agent != null)
137 			return agent;
138 
139 		
140 		String userAgent = getUserAgent();
141 		String lcAgent = userAgent.toLowerCase();
142 		if(isMobileSafari()) {
143 			agent = Agent.MobileSafari; 
144 		}
145 		else if (lcAgent.contains("msie")) {
146 			agent = Agent.IE;
147 		}
148 		else if (lcAgent.contains("chrome")) {
149 			agent = Agent.Chrome;
150 		}
151 		else if (lcAgent.contains("opera")) {
152 			agent = Agent.Opera;
153 		}
154 		else if (lcAgent.contains("webkit") || lcAgent.contains("safari")) {
155 			agent = Agent.Safari;
156 		}
157 		else if (lcAgent.contains("firefox")) {
158 			agent = Agent.Firefox;
159 		}
160 		else 
161 			agent = Agent.Unknown;
162 		
163 		return agent;
164 	}
165 
166 	public static boolean isIE() {
167 		return getBrowserType() == Agent.IE;
168 	}
169 
170 }