Merge "implement joystick button" into gingerbread
diff --git a/demokit/app/AndroidManifest.xml b/demokit/app/AndroidManifest.xml
index d873887..e9e63bd 100644
--- a/demokit/app/AndroidManifest.xml
+++ b/demokit/app/AndroidManifest.xml
@@ -25,6 +25,8 @@
             android:icon="@drawable/icon"
             android:hardwareAccelerated="true">
         <uses-library android:name="com.android.future.usb.accessory" />
+	<uses-sdk android:minSdkVersion="10" />
+
 
         <activity android:name="DemoKitActivity"
                   android:label="DemoKit">
diff --git a/demokit/app/res/layout/main.xml b/demokit/app/res/layout/main.xml
index 4becaa0..8fdf685 100644
--- a/demokit/app/res/layout/main.xml
+++ b/demokit/app/res/layout/main.xml
@@ -78,10 +78,16 @@
                 <TextView android:layout_width="wrap_content" android:id="@+id/textView12" android:layout_height="wrap_content" android:text="@string/y" android:layout_marginRight="8dip"></TextView>
                 <TextView android:layout_width="wrap_content" android:id="@+id/joyY" android:layout_height="wrap_content"></TextView>
             </LinearLayout>
+            <LinearLayout android:id="@+id/linearLayout5" android:layout_width="wrap_content" android:layout_height="wrap_content">
+	      <TextView android:layout_width="wrap_content" android:id="@+id/textViewJoyButton" android:layout_height="wrap_content" android:text="@string/button" android:layout_marginRight="8dip"></TextView>
+	      <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/droid_off" android:id="@+id/joyButtonImage"></ImageView>
+            </LinearLayout>
+
+
         </TableRow>
         <TableRow android:layout_width="match_parent" android:id="@+id/tableRow9" android:layout_height="wrap_content">
             <TextView android:id="@+id/textView11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="@string/cap"></TextView>
-            <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="left" android:src="@drawable/redball" android:id="@+id/cap"></ImageView>
+            <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="left" android:src="@drawable/droid_off" android:id="@+id/cap"></ImageView>
         </TableRow>
     </TableLayout>
 
diff --git a/demokit/app/res/values/strings.xml b/demokit/app/res/values/strings.xml
index 54be6eb..3080b76 100644
--- a/demokit/app/res/values/strings.xml
+++ b/demokit/app/res/values/strings.xml
@@ -2,6 +2,7 @@
 <resources>
     <string name="hello">Hello World, DemoKitActivity!</string>
     <string name="app_name">DemoKit</string>
+    <string name="button">button</string>
     <string name="button1">1</string>
     <string name="button2">2</string>
     <string name="button3">3</string>
diff --git a/demokit/app/src/com/google/DemoKit/DemoKitActivity.java b/demokit/app/src/com/google/DemoKit/DemoKitActivity.java
index 338276f..a43fea2 100644
--- a/demokit/app/src/com/google/DemoKit/DemoKitActivity.java
+++ b/demokit/app/src/com/google/DemoKit/DemoKitActivity.java
@@ -85,6 +85,8 @@
 
     TextView mJoyX;
     TextView mJoyY;
+    ImageView mJoyButtonImage;
+
 
     ImageView mCap;
 
@@ -190,6 +192,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);
@@ -234,6 +241,7 @@
 
         mJoyX = (TextView)findViewById(R.id.joyX);
         mJoyY = (TextView)findViewById(R.id.joyY);
+        mJoyButtonImage = (ImageView)findViewById(R.id.joyButtonImage);
 
         mCap = (ImageView)findViewById(R.id.cap);
 
@@ -244,11 +252,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) {
@@ -302,6 +323,7 @@
         mButton1Image.setImageDrawable(mSwitchOff);
         mButton2Image.setImageDrawable(mSwitchOff);
         mButton3Image.setImageDrawable(mSwitchOff);
+        mJoyButtonImage.setImageDrawable(mSwitchOff);
         mCap.setImageDrawable(mSwitchOff);
         mLed1Red.setProgress(0);
         mLed1Green.setProgress(0);
@@ -436,6 +458,8 @@
                         mButton3Image.setImageDrawable(o.getState() != 0 ? mSwitchOn : mSwitchOff);
                     else if (o.getSw() == 3)
                         mCap.setImageDrawable(o.getState() != 0 ? mSwitchOn : mSwitchOff);
+                    else if (o.getSw() == 4)
+                        mJoyButtonImage.setImageDrawable(o.getState() != 0 ? mSwitchOn : mSwitchOff);
                     break;
 
                 case MESSAGE_TEMPERATURE:
@@ -492,7 +516,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) {