Category Archives: Code

How to check for all versions of IE (including IE 11)

Just found out this lovely tidbit today that IE 11 has decided to drop MSIE from its user-agent. That means that if you need to detect any version of IE, you’ll need to use a different regular expression.

This is what I came up with for my use case. The MSIE part captures <= 10 and the Trident part should capture 11+. navigator.userAgent.match(/(MSIE|Trident\/.*rv:[0-9]*)/i)

How to install PHP Memcache on Windows WAMP, Linux for any operating system (without really installing)

I have a site that runs on Linux in production that uses Memcache. Installing Memcache on Linux is a cinch. I was setting this site up in my development environment however (Windows 7 64-bit, with WAMP, specifically Apache 2 and PHP 5.3) and I quickly discovered it’s not so easy. For the life of me, I could not find the correct php_memcache.dll file.

After spending a ton of time unsuccessfully install Memcache on my local machine, I realized that I don’t even need the memory caching functionality of Memcache, I just need the functions to work. Continue reading How to install PHP Memcache on Windows WAMP, Linux for any operating system (without really installing)

Android ANR keyDispatchingTimedOut

[ad]

I recently had some issues with my Mancala app where some users were experiencing frequent force closes and I could not figure out the reason for this behavior. Fortunately, with the help of Android's relatively new force close reporting features, I was able to get some insight into the issue. The big issue seemed to be an ANR keyDispatchingTimedOut error, which was something I had no idea about, and Google wasn't super helpful.

Here's an example of the "Dalvik threads" that were reported to me in the Android Developer Console. My understanding is that this is a list of threads and their stack traces at the time of the force close.

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
DALVIK THREADS:
&quot;main&quot; prio=5 tid=1 NATIVE
  | group=&quot;main&quot; sCount=1 dsCount=0 s=N obj=0x4001d8c8 self=0xccc8
  | sysTid=9498 nice=0 sched=0/0 cgrp=default handle=-1345013672
  | schedstat=( 103942869 246185301 151 )
  at java.net.InetAddress.getaddrinfo(Native Method)
  at java.net.InetAddress.lookupHostByName(InetAddress.java:508)
  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:280)
  at java.net.InetAddress.getByName(InetAddress.java:310)
  at java.net.InetSocketAddress.<init>(InetSocketAddress.java:110)
  at java.net.InetSocketAddress.<init>(InetSocketAddress.java:89)
  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1152)
  at com.xoise.mancala.Mancala.checkUpdate(Mancala.java:203)
  at com.xoise.mancala.Mancala.onCreate(Mancala.java:45)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:123)
  at android.app.ActivityThread.main(ActivityThread.java:4627)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:521)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
  at dalvik.system.NativeStart.main(Native Method)
 
&quot;SoundPoolThread&quot; prio=5 tid=8 NATIVE
  | group=&quot;main&quot; sCount=1 dsCount=0 s=N obj=0x43ee6dc8 self=0x22b020
  | sysTid=9506 nice=0 sched=0/0 cgrp=default handle=1204184
  | schedstat=( 854492 25299072 9 )
  at dalvik.system.NativeStart.run(Native Method)
 
&quot;SoundPool&quot; prio=5 tid=9 NATIVE
  | group=&quot;main&quot; sCount=1 dsCount=0 s=N obj=0x43ee6e88 self=0x22b430
  | sysTid=9505 nice=0 sched=0/0 cgrp=default handle=1203920
  | schedstat=( 122071 25207520 2 )
  at dalvik.system.NativeStart.run(Native Method)
 
&quot;Binder Thread #2&quot; prio=5 tid=7 NATIVE
  | group=&quot;main&quot; sCount=1 dsCount=0 s=N obj=0x43ecfa48 self=0x13f1f8
  | sysTid=9504 nice=0 sched=0/0 cgrp=default handle=1189136
  | schedstat=( 3265379 22705078 15 )
  at dalvik.system.NativeStart.run(Native Method)
 
&quot;Binder Thread #1&quot; prio=5 tid=6 NATIVE
  | group=&quot;main&quot; sCount=1 dsCount=0 s=N obj=0x43ecf988 self=0x125a90
  | sysTid=9503 nice=0 sched=0/0 cgrp=default handle=1202768
  | schedstat=( 4425047 132843018 15 )
  at dalvik.system.NativeStart.run(Native Method)
 
&quot;Compiler&quot; daemon prio=5 tid=5 VMWAIT
  | group=&quot;system&quot; sCount=1 dsCount=0 s=N obj=0x43ecd348 self=0x121228
  | sysTid=9502 nice=0 sched=0/0 cgrp=default handle=1184232
  | schedstat=( 1708986 15808104 6 )
  at dalvik.system.NativeStart.run(Native Method)
 
&quot;JDWP&quot; daemon prio=5 tid=4 VMWAIT
  | group=&quot;system&quot; sCount=1 dsCount=0 s=N obj=0x43ecd2a0 self=0x120b08
  | sysTid=9501 nice=0 sched=0/0 cgrp=default handle=1184152
  | schedstat=( 2685545 5004884 22 )
  at dalvik.system.NativeStart.run(Native Method)
 
&quot;Signal Catcher&quot; daemon prio=5 tid=3 RUNNABLE
  | group=&quot;system&quot; sCount=0 dsCount=0 s=N obj=0x43ecd1e8 self=0x120fa8
  | sysTid=9500 nice=0 sched=0/0 cgrp=default handle=1196024
  | schedstat=( 3875733 5920410 15 )
  at dalvik.system.NativeStart.run(Native Method)
 
&quot;HeapWorker&quot; daemon prio=5 tid=2 VMWAIT
  | group=&quot;system&quot; sCount=1 dsCount=0 s=N obj=0x43111d58 self=0x129518
  | sysTid=9499 nice=0 sched=0/0 cgrp=default handle=1185056
  | schedstat=( 27954104 52368162 15 )
  at dalvik.system.NativeStart.run(Native Method)</init></init></init>

This ANR, or Application Not Responding, error occurs when a process on the main thread takes too long (something like 5 seconds). Android kills that process and any related by design to spare the device's resources.

The solution is to run resource-expensive tasks on another thread, and then post, or update the main thread accordingly. A common use for this is network calls (and this was what my problem was). The following is an example of how to implement this logic, in my case to check if an update is needed:

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
private void checkUpdate() {
	SharedPreferences prefs = getPreferences(0);
	SharedPreferences.Editor editor = prefs.edit();
 
	long lastUpdateTime = prefs.getLong(&quot;lastUpdateTime&quot;, 0);
	long time = System.currentTimeMillis() / 1000;
 
	if (lastUpdateTime + 3600 * 24 &lt; time) {
		checkNetworkUpdate();
		editor.putLong(&quot;lastUpdateTime&quot;, time);
		editor.commit();
	}
}
 
protected void checkNetworkUpdate() {
	Thread t = new Thread() {
		public void run() {
			try {
				URL updateURL = new URL(&quot;http://www.updateurl.com/&quot;);       
				URLConnection conn = updateURL.openConnection();
				conn.setConnectTimeout(3000);
				Scanner scanner = new Scanner(conn.getInputStream()); 
				scanner.useDelimiter(&quot;\\n&quot;);
 
				int curVersion = getPackageManager().getPackageInfo(&quot;your.package.name&quot;, 0).versionCode;
				int newVersion = 0;
				if (scanner.hasNext())
					newVersion = Integer.valueOf(scanner.next());
 
				final boolean displayUpdate = newVersion &gt; curVersion;
 
				runOnUiThread(new Runnable() {
					public void run() {
						handleNetworkReply(displayUpdate);
					}
				});
			} catch (Exception e) {}
		}
	};
	t.start();
}
 
private void handleNetworkReply(boolean displayUpdate) {
	if (displayUpdate) {
		(new AlertDialog.Builder(this)).setMessage(&quot;An update is available!\n\nOpen Android Market to see the details?&quot;)
			.setTitle(&quot;Update Available&quot;)
			.setPositiveButton(&quot;Yes&quot;, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(&quot;market://details?id=your.package.name&quot;));
					startActivity(intent);
				}
			})
			.setNegativeButton(&quot;No&quot;, null)
			.create()
			.show();
	}
}

Then just call checkUpdate() to perform the task. Note the major points of importance here being the Thread object with a run() method and the runOnUiThread() method of the Activity, which allows you to post data to the main UI thread.

How to Hide Android WebView Highlight Border (or change it’s color)

Here's one that had me at a loss for a long time that I just figured out. You can easily remove the highlight border (the border that comes up when an element is focused) or change it's color in a WebView with CSS! The WebKit-specific property "-webkit-tap-highlight-color" is what you're looking for.

The following line will disable it on a page completely:

1
2
3
* {
	-webkit-tap-highlight-color: rgba(0, 0, 0, 0);	
}

rgba() is just like rgb(), but it takes a 4th parameter for opacity. It's my belief that this would probably work for iPhone WebView's as well, since both Chrome and Safari are based off of WebKit.

[ad]

PHP RSS Parser – RSS Reader Class for PHP

Just had to create a quick RSS parser for PHP and thought I'd post my solution. The implementation and class follows.

<?php
$rss = new RSSReader('http://news.google.com/?output=rss');
while ($rss -> hasNext())
	print_r($rss -> next());
?>
[ad]
<?php
class RSSReader {
	var $xml = null;
	var $pos = 0;
	var $count = 0;
 
	function __construct($feed_url) {
		$this -> load_url($feed_url);
	}
 
	function load_url($feed_url) {
		$this -> load_string(file_get_contents($feed_url));
	}
 
	function load_string($feed_string) {
		$this -> xml = simplexml_load_string(str_replace('content:encoded', 'content_encoded', $feed_string));
		$this -> pos = 0;
		$this -> count = count($this -> xml -> channel -> item);
	}
 
	function get_title() {
		return $this -> xml -> channel -> title;
	}
 
	function get_link() {
		return $this -> xml -> channel -> link;
	}
 
	function get_pubdate() {
		return $this -> xml -> channel -> pubdate;
	}
 
	function hasNext() {
		return $this -> count > $this -> pos;
	}
 
	function next() {
		$obj = $this -> xml -> channel -> item[$this -> pos++];
		return array(
			'title' => (string) $obj -> title,
			'link' => (string) $obj -> link,
			'description' => (string) $obj -> description,
			'content' => (string) $obj -> content_encoded,
			'pubDate' => strtotime($obj -> pubDate),
		);
	}
}
?>