Thursday, December 01, 2011

FIXED! Mac and Excel 2011 -- Can't open XSLX, PPTX, or DOCX files

Have spent HOURS trying to figure out why I get the above error, "Excel cannot open this file.  The file format or file extension is not valid."  every time I try to view an XLSX/PPTX/DOCX file in an email attachment.

I've completely uninstalled Outlook 2011, done system traces to see what I could find, view all sorts of logs, re-installed multiple times, etc.  Even upgraded to Lion.

Turns out, it was the NAME of my hard drive causing the issue.  Go figure.

To check this, go into Disk Utility and see what the name of your HD is.  If it doesn't have ABC123 characters, change the name.  In the example below, it just shows ":" as the name:

To change the name of the drive, just RIGHT CLICK and select SHOW IN FINDER, then rename it as you would any normal file.

Wednesday, March 09, 2011

Android Background Processing

Background processing on mobile devices can be tough -- services, threads, etc, can be killed on a whim when memory/CPU resources start getting low, the device goes to sleep, network connectivity is less reliable, etc.

There are multiple ways to handle background processing in Android. Here's a QUICK overview of each (mainly so I can get it clear in my mind.)

Standard Java threading. Just create a new thread and start it. The issue here is that it is difficult to communicate things back to the UI. You end up having to use "handlers" to pass messages to the UI.

import android.os.Handler;
private void testThread() {
final int PRE_EXECUTE = 1;
final int PROGRESS_UPDATE = 2;
final int POST_EXECUTE = 3;
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == PRE_EXECUTE)
Log.d("JAVA THREAD", "PRE EXECUTE: " + msg.arg1);

else if (msg.what == PROGRESS_UPDATE)
Log.d("JAVA THREAD", "PROGRESS: " + msg.arg1);

else if (msg.what == POST_EXECUTE)
Log.d("JAVA THREAD", "POST EXECUTE: " + msg.arg1);

new Thread() {
public void run() {
// send a PRE-EXECUTE message just for example
Message msg = Message.obtain();
msg.what = PRE_EXECUTE;
msg.arg1 = 9999;

for (int i = 0; i < 10; i++) {
msg = Message.obtain();
msg.arg1 = i;
Log.d("JAVA THREAD", "ITERATION: " + i);

// send message to UI with result so it can update widgets, etc
msg = Message.obtain();
msg.what = POST_EXECUTE;
msg.arg1 = 12345;


Android utility class to do background processing. Can override methods such as onPreExecute(), doInBackground(), onProgressUpdate(), and onPostExecute() to easily handle background processing and communicating things back to the UI thread for screen updates, etc. This is VERY handy and does all the home-grown handler stuff you'd have to do if you just used Java threads. Almost all of the methods you override run on the UI thread EXCEPT for doInBackground(). Android handles everything for you. The issue with this approach is that the task is tied to whatever started it -- if Android kills the process that started it, then the thread will die, too.

import android.os.AsyncTask;
private void testAsyncTask() {
// Create an AsyncTask that counts to 10 seconds and publishes updates
new AsyncTask() {
protected void onPreExecute() {

protected Integer doInBackground(Void... params) {
for (int i = 0; i < 10; i++) {
return 12345;

protected void onProgressUpdate(Integer[] values) {
Log.d("TASK", "PROGRESS: " + values[0]);

protected void onPostExecute(Integer result) {
Log.d("TASK", "POST EXECUTE ON UI THREAD: " + result);

The recommended way to do anything that is long-running (more than a few seconds, perhaps, such as a background refresh with a web server), is to use a Service. A Service should be well-behaved -- it should not consume battery or memory resources, should not keep the device awake, etc. Services can still be killed be the OS, but the chances are less than other elements as Android gives a higher priority to services. There are still considerations you need to consider -- what if multiple requests to start a service are received? What if the device goes to sleep?

There are two variations of a Service in Android that I know about -- Service and IntentService. Service is mostly used for parallel processing as well as a permanent background process. I've always used the Service class incorrectly and ended up doing a lot of synchronization logic to ensure only a single thread was executing at one time.

And then I found the IntentService. The IntentService ensures that only a single thread is executing at a time. It also ensures that the service is stopped when work has completed (need to verify this!)

CommonsWare ( has released a really nice implementation of WakefulIntentService which makes it a snap to do work while retaining a WakeLock on the device so the CPU is available. Get it here: (Thanks, Mark -- I make it a point to read all of your posts as I'm bound to learn something new!)

Of course, the biggest building block/foundation of Android is the use of Intents and BroadcastReceivers. The thing to consider when processing an intent in onReceive(), is that the CPU might not be available upon exiting from the onReceive() method. Therefore, if you are staring a background Thread or AsyncTask, you may be hosed! You will need to acquire a wake lock yourself, or, better yet, start a background service that does the processing and then shuts down. Check out the WakefulIntentService as it makes things really easy:

import com.commonsware.cwac.wakeful.WakefulIntentService;

public class MyTestService extends WakefulIntentService {

public MyTestService(String name) {

protected void doWakefulWork(Intent intent) {
try {
// do something
} catch (Exception e) {
Log.e(C.TAG, "Evil.", e);

and, to start the service from anywhere in your code:

WakefulIntentService.sendWakefulWork(context, MyTestService.class);

Wednesday, December 16, 2009

Creating a ListView with Alternating Colors in Android

I've seen a lot of posts, and have done a lot of googling to figure out how to create a listview in Android with alternating colors. For example, row 1 light gray, row 2 dark gray, etc.

Here's a very easy way to accomplish this. It just involves creating a super-simple custom adapter.

Here's the source code/Eclipse project.

This example walks you through creating the project from scratch using eclipse and is valid for at least Android 1.5.

STEP 1) Create a new android project
- Give it a name and location
- Select Build Target of 1.5
- Give it a unique application name
- For this example, package name should be set to com.db.alternatinglistview
- Create activity is checked and name is MainActivity

STEP 2) Open up main.xml and modify it to have a listview as follows

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:visibility="visible"/>


STEP 3) Open up MainActivity and have it extend ListActivity

public class MainActivity extends ListActivity {

STEP 4) Let's create a list of cars, so create a Car class:

public class Car {
public String make;
public String model;

public Car(String make, String model) {
this.make = make;
this.model = model;

STEP 5) Have your model class extend HashMap and override get(). This is required because the SimpleAdapter will map fields from your list of Cars to widget ids in the layout (this should make more sense in the steps following).

public class Car extends HashMap {
public static String KEY_MODEL = "model";
public static String KEY_MAKE = "make";
public String get(Object k) {
String key = (String) k;
if (KEY_MAKE.equals(key))
return make;
else if (KEY_MODEL.equals(key))
return model;
return null;

STEP 6) Create a new SimpleAdapter which extends SimpleAdapter and extends the getView() method. This will allow us to call super.getView() and then modify the view before it is returned:

public class CarListAdapter extends SimpleAdapter {

private List<Car> cars;
* Alternating color list -- you could initialize this from anywhere.
* Note that the colors make use of the alpha here, otherwise they would be
* opaque and wouldn't give good results!
private int[] colors = new int[] { 0x30ffffff, 0x30808080 };

public CarListAdapter(Context context,
List<? extends Map<String, String>> cars,
int resource,
String[] from,
int[] to) {
super(context, cars, resource, from, to); = (List<Car>) cars;

public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);

int colorPos = position % colors.length;
return view;


STEP 7) Lastly, in your MainActivity, instantiate the new custom list adapter and set it on your activity:

public class MainActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {

// Create our own version of the list adapter
List<Car> cars = getData();
ListAdapter adapter = new CarListAdapter(this, cars,
android.R.layout.simple_list_item_2, new String[] {
Car.KEY_MODEL, Car.KEY_MAKE }, new int[] {, });

private List<Car> getData() {
List<Car> cars = new ArrayList<Car>();
cars.add(new Car("Dodge", "Viper"));
cars.add(new Car("Chevrolet", "Corvette"));
cars.add(new Car("Aston Martin", "Vanquish"));
cars.add(new Car("Lamborghini", "Diablo"));
cars.add(new Car("Ford", "Pinto"));
return cars;

Here's an example using the above code:

STEP 8)) Run the app and see the results. Play with the colors & alpha to control the look-n-feel. For example, modifying the
int[] colors>
in to the following:

private int[] colors = new int[] { 0x30ffffff, 0x30ff2020, 0x30808080 };

Yields the following results (goofy, but it makes the point!):

Sunday, April 19, 2009

Android: Controlling Airplane Mode

I've spent a lot of time looking for how to programatically enable and disable Airplane Mode. It doesn't appear that the 1.1 Android SDK exposes it yet.

Here're some code snippets to help with that.

Check to see if it is enabled or not:

boolean isEnabled = Settings.System.getInt(
Settings.System.AIRPLANE_MODE_ON, 0) == 1;

To toggle it:

// toggle airplane mode
Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

// Post an intent to reload
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);

To get notifications on state change:

IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");

BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.d("AirplaneMode", "Service state changed");

context.registerReceiver(receiver, intentFilter);

Please share if there's an easier way!

Thursday, September 25, 2008

Auto formatting code in MonoDevelop 1.0

I struggled with keeping my code formatted correctly, especially for C# formatting standards (I'm a long-time Java developer). Artistic Style is an opensource code-formatter.

Here's how I was able to get MonoDevelop to auto-format my C# code. For MonoDevelop 1.0, it requires patching the source due to a Mono bug.

This patch will work easily on the MonoDevelop 1.0 sources.

Getting and Patching MonoDevelop Source
This is really just a workaround -- the real issue appears to be a problem with the Mono compiler, not with MonoDevelop. However, by adding a simple cast to an anonymous delegate, the compiler works fine.

Get the MonoDevelop 1.0 Source Tar Ball
$ wget
$ tar xfvj monodevelop-1.0.tar.bz2
Compile and run MonoDevelop
For me on Ubuntu, I had to install some dependencies. Most all of them were lib*-dev packages. Just run "./configure" and fix the dependencies until configure reports no errors. Once dependencies were sorted out, here's how I built and run it before making changes:
$ cd monodevelop-1.0
$ ./configure
$ make
$ make run
Apply the Patch and Recompile
The following patch simply adds a cast to the anonymous delegate as a work-around to the mono bug.

You can easily manually apply the patch by editing line 79 of src/core/MonoDevelop.Core/MonoDevelop.Core/StringParserService.cs and just casting the anonymous delegate to a GenerateString type:
stringGenerators [providedTag.ToUpper ()] = (GenerateString)delegate (string tag) {
Here's a patch file if you want to apply it automatically:

--- src/core/MonoDevelop.Core/MonoDevelop.Core/StringParserService.cs 2008-03-10 20:21:08.000000000 -0600
+++ src/core/MonoDevelop.Core/MonoDevelop.Core/StringParserService.cs 2008-09-25 10:42:48.000000000 -0600
@@ -76,7 +76,7 @@
public static void RegisterStringTagProvider (IStringTagProvider tagProvider)
foreach (string providedTag in tagProvider.Tags) {
- stringGenerators [providedTag.ToUpper ()] = delegate (string tag) {
+ stringGenerators [providedTag.ToUpper ()] = (GenerateString)delegate (string tag) {
return tagProvider.Convert (tag);

Apply the patch with the following commands (assuming patchfile.txt contains the above text!)
$ cd monodevelop-1.0
$ patch -p0 < patchfile.txt
Rebuild and Run MonoDevelop
Once you have MonoDevelop building, we can run it and hook astyle up to it.
$ make run
Artistic Style -- Astyle
The astyle executable must be installed, of course. On Ubuntu, I just installed the "astyle" package via "sudo apt-get install astyle". From the Artistic Style web site you can get whatever distribution you want, or build it from source.

Once it is installed on your system, make sure it is in your command path.

Configure MonoDevelop External Tool
  1. In MonoDevelop, go to Edit->Preferences...
  2. Drill down to Tools->External Tools (a minor bug in MonoDevelop forces you to select another node before you select "External Tools")
  3. Click the "Add" button and fill in the following information:
    Title: _Format with AStyle
    Command: astyle
    Arguments: -b -n -N ${ItemPath}
    Working Directory: ${ItemDir}
    Click "Save Current File"
  4. Click OK

You may want to add additional cmd line options. Please let me know if there are any others that should be used for C#!

Now, when editing a file, you can use Tools->Format with AStyle (or Alt-T, F). You will be prompted to re-load the changed file at the top of the editor window.

Tuesday, September 16, 2008


I mentioned stunnel in my Netcat post before. Stunnel is short for "universal SSL tunnel". It is a great utility for securing TCP/IP and HTTP(s) connections when an application doesn't have the ability (or doesn't want to deal with) secure transport layer security.

I use stunnel frequently when I want to trap an HTTPS request then replay it to another server. I can't just trap the HTTPS data as it is encrypted. Therefore, I modify my client to use HTTP, trap the plain-text HTTP request, then use stunnel to do the HTTPS for me.

Stunnel is available for immediate download for *nix, Cygwin and a native Windows port.

Configuring Stunnel
Stunnel 4.x is configured via a conf file which is specified as the main parameter on the command line (stunnel 3.x uses cmd-line options to configure it.)
$ stunnel my.conf
The configuration file stunnel uses is broken into two main parts -- Global Options and Service-Level Options.

Global Options dictate how stunnel behaves such as forked or not, logging location, logging levels, etc. Common Global options are:
  • debug = 0-7, where 0=[emergency], 7=[debug] . The default is 5, [notice]
  • foreground = yes|no. This dictates whether or not stunnel will fork the process into the background or stay in the foreground.
  • output = somefile. This is where output goes. /dev/stdout indicates to just output to STDOUT.
  • pid = somefile. If present, the name of the file to write the background process' pid.
  • taskbar=yes|no. Win32 only -- shows a taskbar icon you can use to control the running instance.
Service-Level options are for individual protocols such as https, imap, etc, and control the way the forwarding/proxying behave. Common service-level options are:
  • accept=port #. This is what port incoming connections will be accepted on. Only a single value can be given, but you are free to create multiple services as the following incomplete example shows:

  • cert=somecert.pem. This specifies where your certificate resides. In client mode (client=yes), this cert will be used for 2-way SSL.
  • connect=[host:]port. Where the backend resides.
  • key=keyfile.pem. This is the private key to be used for serving up SSL connections.
These are the only options we'll use in the examples below. RTFM for the other options. :)

1) Proxying Plain Text HTTP client Traffic to HTTPS Server
I use this feature a lot to debug client-side HTTP issues and to see the exact HTTP message on-the-wire. Basically, this is handy to do HTTP from your client, but convert to HTTPS before hitting the server.

All this entails is doing "pseudo-https" with the following stunnel configuration:
$ cat https.conf
foreground = yes
output = /dev/stdout
debug = 7

accept = 9443
connect = localhost:443
client = yes
Then, your client can hit http://localhost:9443 which will be proxied to localhost:443 over SSL.

2) Creating an HTTPS Listener which Proxies to non-HTTP server
Requirement -- PEM-encoded file private key with signed certificate. The private key should not have a password on it. Both the private key AND the cert should be in the PEM-encoded file.

The stunnel conf looks like this to proxy incoming HTTPS requests to your local JBoss/Jetty/Tomcat service:
$ cat accept-https.conf

When you start stunnel with "stunnel accept-https.conf", you can test it with:
$curl --insecure https://localhost
Note that the "--insecure" option may be needed if the stunnel.pem file contains a cert signed by a non-trusted certificate authority. Likewise, in IE or Firefox, you'll need to add a security exception in order to test.

3) Load Balancing Incoming Connections
If multiple "connect" options are given for a service, then a round-robin algorithm is used to load-balance the back-end requests.

The following configuration will load balance incoming HTTP connects on port 80 to HTTPS ports 9080 and 9081.
$ cat loadbalance.conf
output = /dev/stdout
debug = 7

accept = 80
connect = localhost:9080
connect = localhost:9081

Wednesday, September 10, 2008

Debugging with Netcat

Wanted to spotlight one of my favorite utlities -- NETCAT. It's probably my most favorite of utilities. I've used it for years in debugging network issues, especially web issues. It's been described as the "The TCP/IP Swiss Army Knife." It's very powerful.

I use it frequently to grab and send http requests. It allows you to see the exact bytes sent/received on the wire from your browser.

Using netcat to grab an http request
1) Start netcat in listen mode on a port and save the request.
$ nc -l -p 9999 | tee somerequest.http

2) Perform a sample HTTP request using browser: http://localhost:9999/index.html
3) View the request -- you'll see the entire http request payload (headers & content)
GET /index.html HTTP/1.1
Host: localhost:9999
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko/2008072820 Firefox/3.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

Using netcat to play back an http request
Easy as capturing a request -- just redirect the saved HTTP request using:

$ nc 80 < somerequest.http > someresponse.http

You may notice that the response looks garbled -- this is probably due to the fact that it is GZipped-encoded. Look for a header such as "Content-Encoding: gzip". You could re-submit the request after removing the "Accept-Encoding:" header and it will no longer be in gzip format.

Cool Things to Do to Impress Your Friends
1) Copy a file from 1 server to another
Netcat just reads & dumps byts to and from ports. Very simple. To copy a file from one server to another without using SSH/FTP/RCP/etc, just do this:
On the source server, just redirect a file to a port:
$ nc -l -p 9999 < somefile.txt

On the destination server, just connect to that port and redirect the bytes to a local file:
$ nc 9999 > somefile.txt

You may want to do a checksum on the file to ensure contents were not modified or somehow broken.

2) Copy segments of a file (i.e., restarting a transfer)
If you are doing the above transfer and something occurred which caused network to fail, you can simply send just parts of the file and concatenate the new segments to the old file. You just need to know how may bytes the destination file already has, then use "dd" to strip them off. In the following example, the destination already had the first 12,000,150 bytes, so we will skip those.

$ dd bs=1 skip=12,000,150 if=somefile.txt | nc -l -p 9999

Then, just simply append the new contents to what you already have on the destination:
$ nc 9999 >> somefile.txt

3) Give shell access
Netcat can be used to pipe STDIN/STDOUT to a process, too. This can be dangerous, but also powerful. :)

This example creates a network pipe to bash, so anyone connecting to the listener port will have the users bash command access:

$ nc -l -p 9999 -e /bin/bash

Probably, a better way to utilize this feature is to perform a quick backup of a directory. On the source server, type (the -q 5 options tells netcat to close the connection 5 seconds after reaching the EOF)

$ tar zcfv - somedir | nc -q 5 -l -p 9999

Then, on the destination server, type:

$ nc 9999 > somedir.tar.gz

3b) If you have the "pv" utility installed, you can get progress information displayed to your terminal. Pv just displays information about the bytes traveling through a network pipe.

tar zcf - somedir | pv | nc -l -p 9999
61.3MB 0:00:30 [ 2MB/s] [ <=> ]

4) Port scanning
Netcat can act as a port-scanner, too.

$ nc -v -z localhost 1-100
localhost [] 80 (www) open

What about HTTPS?
Stunnel is another one of my favorite utilities. It allows you to tunnel TCP/IP connections over SSL. It also can act as an HTTPS proxy so that you can stick with HTTP traffic locally, but switch to HTTPS when you put it on the wire.

This is very handy when you don't have control over the server and it only requests https, but you want to take a look at packets/http messages b/w your client and the server.

I'll do another post soon on how to use stunnel to handle https.

WireShark/Tcpdump -- Packet analyzer. Very nice and powerful (wireshark used to be called Ethereal)
TcpMon -- was bundled with earlier version of Axis 1.x, but not sure where it went now? -- just sat in the middle b/w TCP/IP connections and listened, logged, and fwded in real time.
Firebug Firefox plugin -- Nice for HTTP debugging.