Fix a crash after an orientation change

A result of the progress bars trying to call their onProgressChanged callback.

On an orientation change, save the accessory being accessed and restore it
appropriately in onCreate, skip accessory initialization in onResume in this
case.

Change-Id: I7b301153a9cd403159d63424dc8a96ba6ae28882
diff --git a/demokit/app/src/com/google/DemoKit/DemoKitActivity.java b/demokit/app/src/com/google/DemoKit/DemoKitActivity.java
index 338276f..bb1ec3a 100644
--- a/demokit/app/src/com/google/DemoKit/DemoKitActivity.java
+++ b/demokit/app/src/com/google/DemoKit/DemoKitActivity.java
@@ -190,6 +190,11 @@
         filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
         registerReceiver(mUsbReceiver, filter);
 
+        if (getLastNonConfigurationInstance() != null) {
+            mAccessory = (UsbAccessory) getLastNonConfigurationInstance();
+            openAccessory(mAccessory);
+        }
+
         setContentView(R.layout.main);
 
         mButton1Image = (ImageView)findViewById(R.id.button1Image);
@@ -244,11 +249,24 @@
     }
 
     @Override
+	public Object onRetainNonConfigurationInstance() {
+		if (mAccessory != null) {
+			return mAccessory;
+		} else {
+			return super.onRetainNonConfigurationInstance();
+		}
+	}
+
+	@Override
     public void onResume() {
         super.onResume();
 
         Intent intent = getIntent();
         Log.d(TAG, "intent: " + intent);
+        if (mInputStream != null && mOutputStream != null) {
+            return;
+        }
+
         UsbAccessory[] accessories = mUsbManager.getAccessoryList();
         UsbAccessory accessory = (accessories == null ? null : accessories[0]);
         if (accessory != null) {
@@ -492,7 +510,7 @@
         else if (seekBar == mServo3)
             buffer[1] = 0x12;
 
-        if (buffer[1] != -1) {
+        if (mOutputStream != null && buffer[1] != -1) {
             try {
                 mOutputStream.write(buffer);
             } catch (IOException e) {