<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-556059086866018353</atom:id><lastBuildDate>Fri, 22 May 2026 20:59:22 +0000</lastBuildDate><category>android</category><category>release</category><category>wallswitch</category><category>programming</category><category>image processing</category><title>Playing with Androids</title><description>My current experiments learning the Android platform.  I&#39;m currently working on an App called WallSwitch that will automagically switch your wallpaper for you, at a defined interval.  I figured I might as well do something the iPhone can&#39;t do (for now).</description><link>http://kfb-android.blogspot.com/</link><managingEditor>noreply@blogger.com (Keith)</managingEditor><generator>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-556059086866018353.post-1006303047187733290</guid><pubDate>Sat, 08 May 2010 23:44:00 +0000</pubDate><atom:updated>2010-05-08T16:44:52.350-07:00</atom:updated><title>Making Random Suck Less</title><description>One of the biggest complaints I get about WallSwitch is that the random switching sucks.  It repeats images too frequently and just doesn&#39;t &#39;feel right&#39;.  I couldn&#39;t agree more, so when I ran across &lt;a href=&quot;http://www.rexswain.com/benford.html&quot;&gt;this article,&lt;/a&gt; I figured I could do something about it.
&lt;br/&gt;&lt;br/&gt;

The problem is that random numbers don&#39;t work how the human brain expects.  The article above explains it in detail, but suffice it to say that pure/pseudo random numbers suck for most things a user interacts with.  What I wanted was a way to provide selections that are still nondeterministic, but closer to what users expect.
&lt;/br&gt;&lt;/br&gt;

The method itself is nothing more than random selection using weighted items.  I wanted to keep a feeling of randomness, but make sure more recent selections were less likely to appear.  This being Java, the first thing I did was make an interface to hold the object and it&#39;s priority:
&lt;br/&gt;&lt;br/&gt;

&lt;pre class=&quot;brush: java&quot;&gt;
public interface BetterRandomItem {
 int getPriority();
 void setPriority(int newPriority);
}
&lt;/pre&gt;
&lt;br/&gt;&lt;br/&gt;

Then, I made a choice() function (name taken from Python&#39;s &lt;a href=&quot;http://docs.python.org/library/random.html#random.choice&quot;&gt;random.choice&lt;/a&gt;) that uses this priority.
&lt;br/&gt;&lt;br/&gt;

&lt;pre class=&quot;brush: java&quot;&gt;
public static BetterRandomItem choice(List&lt;BetterRandomItem&gt; items) {
  if(items == null || items.size() == 0)
   throw new IllegalArgumentException(&quot;items is null or empty&quot;);
  
  // Sum all the priority values
  int itemSpace = 0;
  for(int i = 0; i &lt; items.size(); i++)
   itemSpace += items.get(i).getPriority();  
  
  // Pull an int from that space
  int target = new Random().nextInt(itemSpace + 1);
  
  // Match the int to the corresponding item
  int tmpVal = 0;
  for(int i = 0; i &lt; items.size(); i++){
   tmpVal += items.get(i).getPriority();
   if(tmpVal &gt;= target)
    return items.get(i);
  }
  
  // If that failed, return the last in the list, I guess
  return items.get(items.size() - 1);
 }
&lt;/pre&gt;
&lt;br/&gt;&lt;br/&gt;

Not the most complex piece of code ever written, but it&#39;ll go a long way to making users a bit happier with &#39;random&#39; selections.  Since putting this in, the emails and comments about my random function sucking have completely stopped.
&lt;br/&gt;&lt;br/&gt;

&lt;b&gt;Note&lt;/b&gt;: For my implementation, I kept track of the last 50 images displayed and gave them weights 1-50. Any unseen images were weighted as (num seen images * 10), giving them significantly more weight than those that had been seen.
&lt;br/&gt;&lt;br/&gt;

&lt;b&gt;Another Note&lt;/b&gt;: Watch out for int overflows in itemSpace. It would be nice if Random.nextLong took an argument.</description><link>http://kfb-android.blogspot.com/2010/05/making-random-suck-less.html</link><author>noreply@blogger.com (Keith)</author><thr:total>9</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-556059086866018353.post-7913366831230987603</guid><pubDate>Fri, 26 Feb 2010 23:52:00 +0000</pubDate><atom:updated>2010-02-26T16:13:20.244-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">android</category><category domain="http://www.blogger.com/atom/ns#">release</category><category domain="http://www.blogger.com/atom/ns#">wallswitch</category><title>WallSwitch 2.0 (and 2.0.1) Released!</title><description>WallSwitch 2.0 was released a couple days ago.  It was quickly followed by 2.0.1 with some bug fixes for Droid users.

There are more blog posts and updates to come.  I&#39;ll be working on a couple more bug fixes and a new, smaller, widget for the next version.</description><link>http://kfb-android.blogspot.com/2010/02/wallswitch-20-and-201-release.html</link><author>noreply@blogger.com (Keith)</author><thr:total>8</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-556059086866018353.post-5761462858668954826</guid><pubDate>Tue, 07 Jul 2009 05:12:00 +0000</pubDate><atom:updated>2009-07-06T22:20:39.525-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">android</category><category domain="http://www.blogger.com/atom/ns#">release</category><category domain="http://www.blogger.com/atom/ns#">wallswitch</category><title>WallSwitch v1.5.0 Released!</title><description>I&#39;m incredibly happy to finally announce that WallSwitch 1.5 has been published to the Android Market!  It&#39;s been a significant amount of work to add some of the new features and fix the outstanding bugs.  Here&#39;s what you can expect to find in the new version:
&lt;br/&gt;

&lt;ul&gt;
&lt;li&gt;You can now select multiple wallpaper folders&lt;/li&gt;
&lt;li&gt;New picker for auto-switch interval&lt;/li&gt;
&lt;li&gt;Low Performance Mode, in case your phone is sluggish&lt;/li&gt;
&lt;li&gt;Android 1.5 officially, and exclusively, supported&lt;/li&gt;
&lt;li&gt;Won&#39;t auto-switch right at boot. It waits a minute so the boot can finish.&lt;/li&gt;
&lt;/ul&gt;

There were also a few bugs fixed in the image processing.  Notably, cropping should be faster and more accurate. As always, if you find any bugs, please don&#39;t hesitate to email me.</description><link>http://kfb-android.blogspot.com/2009/07/wallswitch-v150-released.html</link><author>noreply@blogger.com (Keith)</author><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-556059086866018353.post-3530620184863319765</guid><pubDate>Mon, 04 May 2009 00:34:00 +0000</pubDate><atom:updated>2009-07-06T22:20:24.288-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">android</category><category domain="http://www.blogger.com/atom/ns#">release</category><category domain="http://www.blogger.com/atom/ns#">wallswitch</category><title>WallSwitch v1.0.4 Released!</title><description>Released tonight with a couple more bug fixes.  
&lt;br/&gt;&lt;br/&gt;

I&#39;m going to start work on v1.1 soon, which will add some of my more frequently requested features.</description><link>http://kfb-android.blogspot.com/2009/05/wallswitch-v104-released.html</link><author>noreply@blogger.com (Keith)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-556059086866018353.post-6780065081313624884</guid><pubDate>Thu, 30 Apr 2009 03:40:00 +0000</pubDate><atom:updated>2009-07-06T22:21:08.788-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">android</category><category domain="http://www.blogger.com/atom/ns#">release</category><category domain="http://www.blogger.com/atom/ns#">wallswitch</category><title>WallSwitch v1.0.3 Released!</title><description>Just a quick note on a release that happened a couple days ago.  Some bugs were found very quickly and I had to do a quick release so it would work on more phones.
&lt;br/&gt;&lt;br/&gt;

Other than that, working on a couple more features.  Right now, I&#39;m thinking checkboxes instead of a radio button for choosing the folder for your wallpapers will be the first thing I do.  It seems to be the more requested feature at the moment and is a really good idea.</description><link>http://kfb-android.blogspot.com/2009/04/wallswitch-v103-released.html</link><author>noreply@blogger.com (Keith)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-556059086866018353.post-6743760763318255263</guid><pubDate>Mon, 27 Apr 2009 03:25:00 +0000</pubDate><atom:updated>2009-07-06T22:21:14.988-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">android</category><category domain="http://www.blogger.com/atom/ns#">release</category><category domain="http://www.blogger.com/atom/ns#">wallswitch</category><title>WallSwitch 1.0 Released!</title><description>&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5LQMKlJ_2_fBSTOBZ5BIUsI1KKUvKuegfKGIYr0-ktAkDDIRYu0BjTnjrcXaYLAowjDbOzq5bb9Pvq0ANAtp9VmJIT1d6fRWFq6oppiH9dqEfuYISmaVz5sUNaqXWMAaAtibPs167spc/s1600-h/icona.png&quot;&gt;&lt;img style=&quot;float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 96px; height: 96px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5LQMKlJ_2_fBSTOBZ5BIUsI1KKUvKuegfKGIYr0-ktAkDDIRYu0BjTnjrcXaYLAowjDbOzq5bb9Pvq0ANAtp9VmJIT1d6fRWFq6oppiH9dqEfuYISmaVz5sUNaqXWMAaAtibPs167spc/s400/icona.png&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5329159778365555650&quot; /&gt;&lt;/a&gt;


I&#39;m happy to announce that WallSwitch 1.0 has been released!
&lt;br/&gt;&lt;br/&gt;

WallSwitch is a program designed to randomly switch the wallpaper on your phone at a given interval.  It handles all the image processing, like cropping, automatically so that you never have to manually set your wallpaper again!
&lt;br/&gt;&lt;br/&gt;

It is also designed to intelligently determine if an image should be cropped or letterboxed so that you always have the optimal wallpaper size for you.
&lt;br/&gt;&lt;br/&gt;

You can download it to your G1 (or any future Android devices) through the Android Marketplace.  Search for &#39;WallSwitch&#39;.</description><link>http://kfb-android.blogspot.com/2009/04/wallswitch-10-released.html</link><author>noreply@blogger.com (Keith)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5LQMKlJ_2_fBSTOBZ5BIUsI1KKUvKuegfKGIYr0-ktAkDDIRYu0BjTnjrcXaYLAowjDbOzq5bb9Pvq0ANAtp9VmJIT1d6fRWFq6oppiH9dqEfuYISmaVz5sUNaqXWMAaAtibPs167spc/s72-c/icona.png" height="72" width="72"/><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-556059086866018353.post-4186869992369781460</guid><pubDate>Sat, 25 Apr 2009 19:18:00 +0000</pubDate><atom:updated>2009-04-25T14:26:04.222-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">android</category><category domain="http://www.blogger.com/atom/ns#">programming</category><title>Registering for TIME_TICK After a Reboot in Android</title><description>Creating a service that will receive &lt;code&gt;TIME_TICK&lt;/code&gt;, but also start on boot isn&#39;t immediately obvious.  It took me a bit to figure out the best way to do it, so I figure it&#39;s worth sharing.
&lt;br/&gt;&lt;br/&gt;

To start, you can&#39;t register your receiver to get &lt;code&gt;TIME_TICK&lt;/code&gt; in the android manifest.  The registration for &lt;code&gt;TIME_TICK&lt;/code&gt; has to happen in code using &lt;code&gt;Intent.registerReceiver&lt;/code&gt;. You can, however, define that your receiver has &lt;code&gt;android.permission.RECEIVE_BOOT_COMPLETED&lt;/code&gt; and set your receiver to handle it, which will give you some control on boot.  Here&#39;s the relevant portion of &lt;code&gt;AndroidManifest.xml&lt;/code&gt;:
&lt;br/&gt;&lt;br/&gt;

&lt;pre class=&quot;brush: xml&quot;&gt;
&amp;lt;application android:icon=&quot;@drawable/icon&quot; android:label=&quot;@string/app_name&quot;
 android:enabled=&quot;true&quot; android:debuggable=&quot;false&quot;&amp;gt;
 &amp;lt;activity android:name=&quot;.AndroidMain&quot; android:label=&quot;@string/app_name&quot;&amp;gt;
  &amp;lt;intent-filter&amp;gt;
   &amp;lt;action android:name=&quot;android.intent.action.MAIN&quot; /&amp;gt;
   &amp;lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&amp;gt;
  &amp;lt;/intent-filter&amp;gt;
 &amp;lt;/activity&amp;gt;
 &amp;lt;service android:name=&quot;DemoService&quot;&amp;gt;&amp;lt;/service&amp;gt;
 &amp;lt;receiver android:name=&quot;DemoReceiver&quot;
  android:permission=&quot;android.permission.RECEIVE_BOOT_COMPLETED&quot;&amp;gt;
  &amp;lt;intent-filter&amp;gt;
   &amp;lt;action android:name=&quot;android.intent.action.TIME_TICK&quot;&amp;gt;&amp;lt;/action&amp;gt;
   &amp;lt;action android:name=&quot;android.intent.action.BOOT_COMPLETED&quot;&amp;gt;&amp;lt;/action&amp;gt;
  &amp;lt;/intent-filter&amp;gt;
 &amp;lt;/receiver&amp;gt;
&amp;lt;/application&amp;gt;
&amp;lt;uses-permission android:name=&quot;android.permission.RECEIVE_BOOT_COMPLETED&quot;&amp;gt;&amp;lt;/uses-permission&amp;gt;
&lt;/pre&gt;

&lt;br/&gt;
This isn&#39;t enough, however, since your receiver can&#39;t register itself to listen to &lt;code&gt;TIME_TICK&lt;/code&gt;.  &lt;code&gt;Intent.registerReceiver&lt;/code&gt; registers for the lifetime of the Intent it&#39;s called from.  If we register it using the intent created for &lt;code&gt;BOOT_COMPLETED&lt;/code&gt;, it&#39;s dead as soon as it exits the handler.  Therefore, we need to start a background service that will keep running.  This service then creates a new instance of the receiver and registers that for &lt;code&gt;TIME_TICK&lt;/code&gt;.
&lt;br/&gt;&lt;br/&gt;

DemoReceiver.java:
&lt;br/&gt;
&lt;pre class=&quot;brush: java&quot;&gt;
public class DemoReceiver extends BroadcastReceiver {
 static final String LOGGING_TAG = &quot;MyDemo&quot;;

 @Override
 public void onReceive(Context context, Intent intent) {
  if (intent.getAction().compareTo(Intent.ACTION_BOOT_COMPLETED) == 0){   
   Log.v(LOGGING_TAG, &quot;DemoReceiver.onReceive(ACTION_BOOT_COMPLETED)&quot;);   
   context.startService(new Intent(context, DemoService.class));   
  }else if(intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0)
   Log.v(LOGGING_TAG, &quot;DemoReceiver.onReceive(ACTION_TIME_TICK)&quot;);
  else
   Log.v(LOGGING_TAG, &quot;DemoReceiver.onReceive(&quot; + intent.getAction() + &quot;)&quot;);
 }
}
&lt;/pre&gt;
&lt;br/&gt;&lt;br/&gt;
DemoService.java:
&lt;br/&gt;
&lt;pre class=&quot;brush: java&quot;&gt;
public class DemoService extends Service {
 static final String LOGGING_TAG = &quot;MyDemo&quot;;
 
 @Override
 public IBinder onBind(Intent intent) {
  return null;
 }
 
 @Override
 public void onStart(Intent intent, int startId){
  super.onStart(intent, startId);
  Log.v(LOGGING_TAG, &quot;DemoService.onStart()&quot;);
 }
 
 @Override
 public void onCreate(){
  super.onCreate();
  Log.v(LOGGING_TAG, &quot;DemoService.onCreate()&quot;);
  
  registerReceiver(
    new DemoReceiver(), 
    new IntentFilter(Intent.ACTION_TIME_TICK));  
 }
}
&lt;/pre&gt;
&lt;br/&gt;&lt;br/&gt;

If this all works correctly, we&#39;ll see log messages that look something like:
&lt;br/&gt;

&lt;pre class=&quot;brush: plain&quot;&gt;
04-25 21:04:22.580: VERBOSE/MyDemo(182): DemoReceiver.onReceive(ACTION_BOOT_COMPLETED)
04-25 21:04:22.630: VERBOSE/MyDemo(182): DemoService.onCreate()
04-25 21:04:22.650: VERBOSE/MyDemo(182): DemoService.onStart()
04-25 21:05:00.140: VERBOSE/MyDemo(182): DemoReceiver.onReceive(ACTION_TIME_TICK)
&lt;/pre&gt;</description><link>http://kfb-android.blogspot.com/2009/04/registering-for-timetick-after-reboot.html</link><author>noreply@blogger.com (Keith)</author><thr:total>6</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-556059086866018353.post-3300054423614128985</guid><pubDate>Wed, 15 Apr 2009 02:11:00 +0000</pubDate><atom:updated>2009-07-06T22:20:56.007-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">android</category><category domain="http://www.blogger.com/atom/ns#">image processing</category><category domain="http://www.blogger.com/atom/ns#">programming</category><title>Image Processing in Android</title><description>While working on WallSwitch, I ran into a number of hairy problems with image processing.  The worst, by far, was this:

&lt;pre class=&quot;brush: plain&quot;&gt;
04-15 02:20:34.693: ERROR/dalvikvm-heap(165): 50331648-byte external allocation too large for this process.
04-15 02:20:34.693: ERROR/(165): VM won&#39;t let us allocate 50331648 bytes
04-15 02:20:38.233: ERROR/AndroidRuntime(165): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 
&lt;/pre&gt;

I got it when trying to do a simple &lt;code&gt;BitmapFactory.decodeFile()&lt;/code&gt; on a jpeg I had on the sd card.  The jpeg was huge (~4MB) but that should exceed the 16MB heap I get, right?  I found some decent help &lt;a href=&quot;http://www.mail-archive.com/android-developers@googlegroups.com/msg20988.html&quot;&gt;here&lt;/a&gt; and &lt;a href=&quot;http://groups.google.com/group/android-developers/browse_thread/thread/bb9375aecad6d47c/4f1592a89880b295&quot;&gt;here&lt;/a&gt;, but nothing that really explained what was going on.  I knew I was hitting the limit, but had no idea why.
&lt;br/&gt;&lt;br/&gt;


Then, I remembered something a friend had told me a few years ago about how jpegs are compressed (duh) but that it had to first decompress to a bitmap before painting to your screen.  It makes sense, after all.  If I&#39;ve got 1024x768 pixels on my monitor and I want to paint a picture over all of it, I&#39;m going to need 768,432 bytes (depending on bit-depth).
&lt;br/&gt;&lt;br/&gt;

I whipped out my calculator and had a facepalm moment.  Needless to say 50,331,648 is 4096x6144 * 2, which corresponds to the size of my jpeg, when fully decompressed.
&lt;br/&gt;&lt;br/&gt;

Fixing this was, happily, very easy.  You just need to set &lt;code&gt;inSampleSize&lt;/code&gt; on your &lt;code&gt;BitmapFactory.Options&lt;/code&gt; to something useful. It&#39;ll sample the image you get back, returning something 1/2, 1/4, ... the size.  The key here is to try to keep the sample size a power of two.  It isn&#39;t necessary, but it makes the processing faster and it&#39;ll make sure the image you get back keeps the same proportions.
&lt;br/&gt;&lt;br/&gt;

&lt;pre class=&quot;brush: java&quot;&gt;
// First, get the dimensions of the image
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);

// Only scale if we need to 
// (16384 buffer for img processing)
Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) &gt;= Math.abs(options.outWidth - targetWidth);
if(options.outHeight * options.outWidth * 2 &gt;= 16384){
    // Load, scaling to smallest power of 2 that&#39;ll get it &lt;= desired dimensions
    double sampleSize = scaleByHeight
        ? options.outHeight / targetHeight
        : options.outWidth / targetWidth;
    options.inSampleSize = 
        (int)Math.pow(2d, Math.floor(
        Math.log(sampleSize)/Math.log(2d)));
}

// Do the actual decoding
options.inJustDecodeBounds = false;
options.inTempStorage = new byte[IMG_BUFFER_LEN];  
Bitmap output = BitmapFactory.decodeFile(filePath, options);
&lt;/pre&gt;

It&#39;s still a pretty slow operation to perform, but this at least makes it possible!</description><link>http://kfb-android.blogspot.com/2009/04/image-processing-in-android.html</link><author>noreply@blogger.com (Keith)</author><thr:total>9</thr:total></item></channel></rss>