diff options
author | Erik Gilling <konkers@android.com> | 2011-03-15 22:06:58 -0700 |
---|---|---|
committer | Erik Gilling <konkers@android.com> | 2011-03-15 22:06:58 -0700 |
commit | 519f672d94dbf0a4343966c180e92b7641d40430 (patch) | |
tree | 95b9e20b98ec3dea698c016786d056bc0fc29f3f | |
parent | c91ab30679144106eb3aff2496fe7fd641f1f26c (diff) | |
download | demokit-519f672d94dbf0a4343966c180e92b7641d40430.tar.gz |
Add demokit app
Change-Id: I1d0b59014300f0e049fbb78cb43d1d47730b07fc
-rw-r--r-- | demokit/app/Android.mk | 28 | ||||
-rw-r--r-- | demokit/app/AndroidManifest.xml | 41 | ||||
-rw-r--r-- | demokit/app/res/drawable-hdpi/greenball.png | bin | 0 -> 4423 bytes | |||
-rw-r--r-- | demokit/app/res/drawable-hdpi/icon.png | bin | 0 -> 4147 bytes | |||
-rw-r--r-- | demokit/app/res/drawable-hdpi/redball.png | bin | 0 -> 4357 bytes | |||
-rw-r--r-- | demokit/app/res/drawable-ldpi/greenball.png | bin | 0 -> 2521 bytes | |||
-rw-r--r-- | demokit/app/res/drawable-ldpi/icon.png | bin | 0 -> 1723 bytes | |||
-rw-r--r-- | demokit/app/res/drawable-ldpi/redball.png | bin | 0 -> 2487 bytes | |||
-rw-r--r-- | demokit/app/res/drawable-mdpi/greenball.png | bin | 0 -> 4423 bytes | |||
-rw-r--r-- | demokit/app/res/drawable-mdpi/icon.png | bin | 0 -> 4147 bytes | |||
-rw-r--r-- | demokit/app/res/drawable-mdpi/redball.png | bin | 0 -> 4357 bytes | |||
-rw-r--r-- | demokit/app/res/layout/main.xml | 88 | ||||
-rw-r--r-- | demokit/app/res/values/strings.xml | 34 | ||||
-rw-r--r-- | demokit/app/res/xml/accessory_filter.xml | 18 | ||||
-rw-r--r-- | demokit/app/src/com/google/DemoKit/DemoKitActivity.java | 457 |
15 files changed, 666 insertions, 0 deletions
diff --git a/demokit/app/Android.mk b/demokit/app/Android.mk new file mode 100644 index 0000000..293c212 --- /dev/null +++ b/demokit/app/Android.mk @@ -0,0 +1,28 @@ +# +# Copyright (C) 2011 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := tests + +LOCAL_SRC_FILES := $(call all-subdir-java-files) + +LOCAL_PACKAGE_NAME := DemoKit + +LOCAL_JAVA_LIBRARIES := com.android.future.usb.accessory + +include $(BUILD_PACKAGE) diff --git a/demokit/app/AndroidManifest.xml b/demokit/app/AndroidManifest.xml new file mode 100644 index 0000000..9523587 --- /dev/null +++ b/demokit/app/AndroidManifest.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2011 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.google.DemoKit"> + + <application android:label="DemoKit" + android:hardwareAccelerated="true"> + <uses-library android:name="com.android.future.usb.accessory" /> + + <activity android:name="DemoKitActivity" + android:label="DemoKit"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + + <intent-filter> + <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" /> + </intent-filter> + + <meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" + android:resource="@xml/accessory_filter" /> + + + </activity> + </application> +</manifest>
\ No newline at end of file diff --git a/demokit/app/res/drawable-hdpi/greenball.png b/demokit/app/res/drawable-hdpi/greenball.png Binary files differnew file mode 100644 index 0000000..7851232 --- /dev/null +++ b/demokit/app/res/drawable-hdpi/greenball.png diff --git a/demokit/app/res/drawable-hdpi/icon.png b/demokit/app/res/drawable-hdpi/icon.png Binary files differnew file mode 100644 index 0000000..8074c4c --- /dev/null +++ b/demokit/app/res/drawable-hdpi/icon.png diff --git a/demokit/app/res/drawable-hdpi/redball.png b/demokit/app/res/drawable-hdpi/redball.png Binary files differnew file mode 100644 index 0000000..fba2f5d --- /dev/null +++ b/demokit/app/res/drawable-hdpi/redball.png diff --git a/demokit/app/res/drawable-ldpi/greenball.png b/demokit/app/res/drawable-ldpi/greenball.png Binary files differnew file mode 100644 index 0000000..3a814fc --- /dev/null +++ b/demokit/app/res/drawable-ldpi/greenball.png diff --git a/demokit/app/res/drawable-ldpi/icon.png b/demokit/app/res/drawable-ldpi/icon.png Binary files differnew file mode 100644 index 0000000..1095584 --- /dev/null +++ b/demokit/app/res/drawable-ldpi/icon.png diff --git a/demokit/app/res/drawable-ldpi/redball.png b/demokit/app/res/drawable-ldpi/redball.png Binary files differnew file mode 100644 index 0000000..6e0add3 --- /dev/null +++ b/demokit/app/res/drawable-ldpi/redball.png diff --git a/demokit/app/res/drawable-mdpi/greenball.png b/demokit/app/res/drawable-mdpi/greenball.png Binary files differnew file mode 100644 index 0000000..7851232 --- /dev/null +++ b/demokit/app/res/drawable-mdpi/greenball.png diff --git a/demokit/app/res/drawable-mdpi/icon.png b/demokit/app/res/drawable-mdpi/icon.png Binary files differnew file mode 100644 index 0000000..8074c4c --- /dev/null +++ b/demokit/app/res/drawable-mdpi/icon.png diff --git a/demokit/app/res/drawable-mdpi/redball.png b/demokit/app/res/drawable-mdpi/redball.png Binary files differnew file mode 100644 index 0000000..fba2f5d --- /dev/null +++ b/demokit/app/res/drawable-mdpi/redball.png diff --git a/demokit/app/res/layout/main.xml b/demokit/app/res/layout/main.xml new file mode 100644 index 0000000..ec6f61f --- /dev/null +++ b/demokit/app/res/layout/main.xml @@ -0,0 +1,88 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent"> + <TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:addStatesFromChildren="true"> + <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_gravity="center_vertical" android:orientation="vertical" android:showDividers="beginning|middle|end" android:layout_weight="1" android:layout_height="wrap_content" android:baselineAligned="false" android:gravity="center_vertical" android:layout_marginBottom="8dip"> + <TextView android:id="@+id/textView17" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/buttons" android:layout_marginRight="8dip"></TextView> + <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:verticalScrollbarPosition="right" android:layout_width="match_parent" android:layout_gravity="fill_horizontal"> + <TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@string/button1" android:layout_gravity="center_vertical" android:layout_marginRight="8dip"></TextView> + <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/redball" android:id="@+id/button1Image"></ImageView> + </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/textView2" android:layout_height="wrap_content" android:text="@string/button2" android:layout_gravity="center_vertical" android:layout_marginRight="8dip"></TextView> + <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/redball" android:id="@+id/button2Image"></ImageView> + </LinearLayout> + <LinearLayout android:id="@+id/linearLayout6" android:layout_width="wrap_content" android:layout_height="wrap_content"> + <TextView android:layout_width="wrap_content" android:id="@+id/textView3" android:layout_height="wrap_content" android:text="@string/button3" android:layout_gravity="center_vertical" android:layout_marginRight="8dip"></TextView> + <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/redball" android:id="@+id/button3Image"></ImageView> + </LinearLayout> + </TableRow> + <TableRow android:id="@+id/tableRow14" android:layout_width="wrap_content" android:layout_height="wrap_content"> + <TextView android:id="@+id/textView19" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView> + <TextView android:id="@+id/textView20" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/led1"></TextView> + <TextView android:layout_gravity="center_horizontal" android:id="@+id/textView21" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/led2"></TextView> + <TextView android:layout_gravity="center_horizontal" android:id="@+id/textView22" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/led3"></TextView> + </TableRow> + <TableRow android:layout_height="wrap_content" android:id="@+id/tableRow4" android:layout_width="match_parent" android:layout_weight="1" android:gravity="center_vertical" android:layout_marginBottom="8dip"> + <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dip" android:text="@string/red"></TextView> + <SeekBar android:layout_height="wrap_content" android:max="255" android:id="@+id/led1Red" android:layout_marginRight="16dip" android:layout_width="250dip"></SeekBar> + <SeekBar android:id="@+id/led2Red" android:layout_height="wrap_content" android:max="255" android:layout_marginRight="16dip" android:layout_width="250dip"></SeekBar> + <SeekBar android:id="@+id/led3Red" android:layout_height="wrap_content" android:max="255" android:layout_marginRight="16dip" android:layout_width="250dip"></SeekBar> + </TableRow> + <TableRow android:layout_height="wrap_content" android:id="@+id/tableRow5" android:layout_width="match_parent" android:layout_weight="1" android:gravity="center_vertical" android:layout_marginBottom="8dip"> + <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dip" android:text="@string/green"></TextView> + <SeekBar android:layout_height="wrap_content" android:max="255" android:id="@+id/led1Green" android:layout_marginRight="16dip" android:layout_width="250dip"></SeekBar> + <SeekBar android:id="@+id/led2Green" android:layout_height="wrap_content" android:max="255" android:layout_marginRight="16dip" android:layout_width="250dip"></SeekBar> + <SeekBar android:id="@+id/led3Green" android:layout_height="wrap_content" android:max="255" android:layout_marginRight="16dip" android:layout_width="250dip"></SeekBar> + </TableRow> + <TableRow android:layout_height="wrap_content" android:id="@+id/tableRow6" android:layout_width="match_parent" android:layout_weight="1" android:gravity="center_vertical" android:layout_marginBottom="8dip"> + <TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dip" android:text="@string/blue"></TextView> + <SeekBar android:layout_height="wrap_content" android:max="255" android:id="@+id/led1Blue" android:layout_marginRight="16dip" android:layout_width="250dip"></SeekBar> + <SeekBar android:id="@+id/led2Blue" android:layout_height="wrap_content" android:max="255" android:layout_marginRight="16dip" android:layout_width="250dip"></SeekBar> + <SeekBar android:id="@+id/led3Blue" android:layout_height="wrap_content" android:max="255" android:layout_marginRight="16dip" android:layout_width="250dip"></SeekBar> + </TableRow> + <TableRow android:layout_height="wrap_content" android:id="@+id/tableRow2" android:layout_width="match_parent" android:layout_weight="1" android:gravity="center_vertical" android:layout_marginBottom="8dip"> + <TextView android:id="@+id/textView18" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/relays" android:layout_marginRight="8dip"></TextView> + <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content"> + <TextView android:text="@string/relay1" android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dip"></TextView> + <ToggleButton android:text="ToggleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/relay1Button"></ToggleButton> + </LinearLayout> + <LinearLayout android:id="@+id/linearLayout7" android:layout_width="wrap_content" android:layout_height="wrap_content"> + <TextView android:layout_width="wrap_content" android:id="@+id/textView14" android:layout_height="wrap_content" android:text="@string/relay2" android:layout_marginRight="8dip"></TextView> + <ToggleButton android:layout_width="wrap_content" android:text="ToggleButton" android:layout_height="wrap_content" android:id="@+id/relay2Button"></ToggleButton> + </LinearLayout> + </TableRow> + <TableRow android:layout_height="wrap_content" android:id="@+id/tableRow3" android:layout_width="match_parent" android:layout_weight="1" android:layout_marginBottom="8dip"> + <TextView android:text="@string/temperature" android:id="@+id/textView15" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dip"></TextView> + <TextView android:id="@+id/temperature" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> + </TableRow> + <TableRow android:layout_height="wrap_content" android:id="@+id/tableRow13" android:layout_width="match_parent" android:layout_weight="1" android:layout_marginBottom="8dip"> + <TextView android:text="@string/light" android:id="@+id/textView16" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dip"></TextView> + <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/light"></TextView> + </TableRow> + <TableRow android:id="@+id/tableRow7" android:layout_width="wrap_content" android:layout_height="wrap_content"> + <TextView android:layout_marginRight="8dip" android:id="@+id/textView8" android:layout_width="wrap_content" android:text="@string/servos" android:layout_height="wrap_content"></TextView> + <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="8dip" android:id="@+id/servo1" android:max="255" android:progress="127"></SeekBar> + <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="8dip" android:id="@+id/servo2" android:max="255" android:progress="127"></SeekBar> + <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="8dip" android:id="@+id/servo3" android:max="255" android:progress="127"></SeekBar> + </TableRow> + <TableRow android:layout_width="match_parent" android:id="@+id/tableRow8" android:layout_height="wrap_content"> + <TextView android:id="@+id/textView9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/joystick"></TextView> + <LinearLayout android:id="@+id/linearLayout3" android:layout_width="wrap_content" android:layout_height="wrap_content"> + <TextView android:layout_width="wrap_content" android:id="@+id/textView10" android:layout_height="wrap_content" android:text="@string/x" android:layout_marginRight="8dip"></TextView> + <TextView android:layout_width="wrap_content" android:id="@+id/joyX" android:layout_height="wrap_content"></TextView> + </LinearLayout> + <LinearLayout android:id="@+id/linearLayout4" android:layout_width="wrap_content" android:layout_height="wrap_content"> + <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> + </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> + </TableRow> + </TableLayout> + +</LinearLayout> diff --git a/demokit/app/res/values/strings.xml b/demokit/app/res/values/strings.xml new file mode 100644 index 0000000..54be6eb --- /dev/null +++ b/demokit/app/res/values/strings.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="hello">Hello World, DemoKitActivity!</string> + <string name="app_name">DemoKit</string> + <string name="button1">1</string> + <string name="button2">2</string> + <string name="button3">3</string> + <string name="led1Red">Led 1 Red</string> + <string name="led1Green">Led 1 Green</string> + <string name="led1Blue">Led 1 Blue</string> + <string name="led2Red">Led 2 Red</string> + <string name="led2Green">Led 2 Green</string> + <string name="led2Blue">Led 2 Blue</string> + <string name="led3Red">Led 3 Red</string> + <string name="led3Green">Led 3 Green</string> + <string name="led3Blue">Led 3 Blue</string> + <string name="relay1">1</string> + <string name="relay2">2</string> + <string name="temperature">Temperature</string> + <string name="light">Light</string> + <string name="buttons">Buttons</string> + <string name="relays">Relays</string> + <string name="led1">Led 1</string> + <string name="led2">Led 2</string> + <string name="led3">Led 3</string> + <string name="red">Red</string> + <string name="green">Green</string> + <string name="blue">Blue</string> + <string name="servos">Servos</string> + <string name="x">X</string> + <string name="y">Y</string> + <string name="joystick">Joystick</string> + <string name="cap">Cap Button</string> +</resources> diff --git a/demokit/app/res/xml/accessory_filter.xml b/demokit/app/res/xml/accessory_filter.xml new file mode 100644 index 0000000..5631b03 --- /dev/null +++ b/demokit/app/res/xml/accessory_filter.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2011 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<resources> + <usb-accessory manufacturer="Google, Inc." model="DemoKit" version="1.0" /> +</resources> diff --git a/demokit/app/src/com/google/DemoKit/DemoKitActivity.java b/demokit/app/src/com/google/DemoKit/DemoKitActivity.java new file mode 100644 index 0000000..f141cc9 --- /dev/null +++ b/demokit/app/src/com/google/DemoKit/DemoKitActivity.java @@ -0,0 +1,457 @@ +package com.google.DemoKit; + +import android.app.Activity; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.os.ParcelFileDescriptor; +import android.util.Log; + +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.SeekBar; +import android.widget.ToggleButton; +import android.widget.CompoundButton; +import android.graphics.drawable.Drawable; + +import com.android.future.usb.UsbAccessory; +import com.android.future.usb.UsbManager; + +import java.io.FileDescriptor; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + + +public class DemoKitActivity extends Activity implements Runnable, SeekBar.OnSeekBarChangeListener, CompoundButton.OnCheckedChangeListener { + private static final String TAG = "DemoKit"; + + private static final String ACTION_USB_PERMISSION = + "com.google.DemoKit.action.USB_PERMISSION"; + + private UsbManager mUsbManager; + private PendingIntent mPermissionIntent; + private boolean mPermissionRequestPending; + + ParcelFileDescriptor mFileDescriptor; + FileInputStream mInputStream; + FileOutputStream mOutputStream; + + ImageView mButton1Image; + ImageView mButton2Image; + ImageView mButton3Image; + + SeekBar mLed1Red; + SeekBar mLed1Green; + SeekBar mLed1Blue; + SeekBar mLed2Red; + SeekBar mLed2Green; + SeekBar mLed2Blue; + SeekBar mLed3Red; + SeekBar mLed3Green; + SeekBar mLed3Blue; + + ToggleButton mRelay1Button; + ToggleButton mRelay2Button; + + TextView mTemperature; + TextView mLight; + + SeekBar mServo1; + SeekBar mServo2; + SeekBar mServo3; + + TextView mJoyX; + TextView mJoyY; + + ImageView mCap; + + + Drawable redball; + Drawable greenball; + + private static final int MESSAGE_SWITCH = 1; + private static final int MESSAGE_TEMPERATURE = 2; + private static final int MESSAGE_LIGHT = 3; + private static final int MESSAGE_JOY = 4; + + private class SwitchMsg { + private byte sw; + private byte state; + public SwitchMsg(byte sw, byte state) { + this.sw = sw; + this.state = state; + } + + public byte getSw() { + return sw; + } + + public byte getState() { + return state; + } + } + + private class TemperatureMsg { + private int temperature; + + public TemperatureMsg(int temperature) { + this.temperature = temperature; + } + + public int getTemperature() { + return temperature; + } + } + + private class LightMsg { + private int light; + + public LightMsg(int light) { + this.light = light; + } + + public int getLight() { + return light; + } + } + + private class JoyMsg { + private int x; + private int y; + + public JoyMsg(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + } + + private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (ACTION_USB_PERMISSION.equals(intent.getAction())) { + synchronized (this) { + UsbAccessory accessory = UsbManager.getAccessory(intent); + if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { + openAccessory(accessory); + } else { + Log.d(TAG, "permission denied for accessory " + accessory); + } + mPermissionRequestPending = false; + } + } + } + }; + + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mUsbManager = UsbManager.getInstance(this); + mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); + IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); + registerReceiver(mUsbReceiver, filter); + + setContentView(R.layout.main); + + mButton1Image = (ImageView)findViewById(R.id.button1Image); + mButton2Image = (ImageView)findViewById(R.id.button2Image); + mButton3Image = (ImageView)findViewById(R.id.button3Image); + + mLed1Red = (SeekBar)findViewById(R.id.led1Red); + mLed1Red.setOnSeekBarChangeListener(this); + mLed1Green = (SeekBar)findViewById(R.id.led1Green); + mLed1Green.setOnSeekBarChangeListener(this); + mLed1Blue = (SeekBar)findViewById(R.id.led1Blue); + mLed1Blue.setOnSeekBarChangeListener(this); + + mLed2Red = (SeekBar)findViewById(R.id.led2Red); + mLed2Red.setOnSeekBarChangeListener(this); + mLed2Green = (SeekBar)findViewById(R.id.led2Green); + mLed2Green.setOnSeekBarChangeListener(this); + mLed2Blue = (SeekBar)findViewById(R.id.led2Blue); + mLed2Blue.setOnSeekBarChangeListener(this); + + mLed3Red = (SeekBar)findViewById(R.id.led3Red); + mLed3Red.setOnSeekBarChangeListener(this); + mLed3Green = (SeekBar)findViewById(R.id.led3Green); + mLed3Green.setOnSeekBarChangeListener(this); + mLed3Blue = (SeekBar)findViewById(R.id.led3Blue); + mLed3Blue.setOnSeekBarChangeListener(this); + + mRelay1Button = (ToggleButton)findViewById(R.id.relay1Button); + mRelay1Button.setOnCheckedChangeListener(this); + mRelay2Button = (ToggleButton)findViewById(R.id.relay2Button); + mRelay2Button.setOnCheckedChangeListener(this); + + mTemperature = (TextView)findViewById(R.id.temperature); + mLight = (TextView)findViewById(R.id.light); + + mServo1 = (SeekBar)findViewById(R.id.servo1); + mServo1.setOnSeekBarChangeListener(this); + mServo2 = (SeekBar)findViewById(R.id.servo2); + mServo2.setOnSeekBarChangeListener(this); + mServo3 = (SeekBar)findViewById(R.id.servo3); + mServo3.setOnSeekBarChangeListener(this); + + mJoyX = (TextView)findViewById(R.id.joyX); + mJoyY = (TextView)findViewById(R.id.joyY); + + mCap = (ImageView)findViewById(R.id.cap); + + redball = getResources().getDrawable(R.drawable.redball); + greenball = getResources().getDrawable(R.drawable.greenball); + + } + + @Override + public void onResume() { + super.onResume(); + + Intent intent = getIntent(); + Log.d(TAG, "intent: " + intent); + UsbAccessory[] accessories = mUsbManager.getAccessoryList(); + UsbAccessory accessory = (accessories == null ? null : accessories[0]); + if (accessory != null) { + if (mUsbManager.hasPermission(accessory)) { + openAccessory(accessory); + } else { + synchronized (mUsbReceiver) { + if (!mPermissionRequestPending) { + mUsbManager.requestPermission(accessory, mPermissionIntent); + mPermissionRequestPending = true; + } + } + } + } else { + Log.d(TAG, "mAccessory is null"); + } + } + + @Override + public void onPause() { + super.onPause(); + if (mFileDescriptor != null) { + try { + mFileDescriptor.close(); + } catch (IOException e) { + } finally { + mFileDescriptor = null; + } + } + } + + @Override + public void onDestroy() { + unregisterReceiver(mUsbReceiver); + super.onDestroy(); + } + + private void openAccessory(UsbAccessory accessory) { + Log.d(TAG, "openAccessory: " + accessory); + mFileDescriptor = mUsbManager.openAccessory(accessory); + if (mFileDescriptor != null) { + FileDescriptor fd = mFileDescriptor.getFileDescriptor(); + mInputStream = new FileInputStream(fd); + mOutputStream = new FileOutputStream(fd); + Thread thread = new Thread(null, this, "AccessoryChat"); + thread.start(); + Log.d(TAG, "openAccessory succeeded"); + } else { + Log.d(TAG, "openAccessory fail"); + } + } + + private int composeInt(byte hi, byte lo) { + int val = (int)hi & 0xff; + val *= 256; + val += (int)lo & 0xff; + return val; + } + + public void run() { + int ret = 0; + byte[] buffer = new byte[16384]; + int i; + + while (ret >= 0) { + try { + ret = mInputStream.read(buffer); + } catch (IOException e) { + break; + } + + Log.d(TAG, "got bytes " + ret); + i = 0; + while (i < ret) { + int len = ret - i; + + switch (buffer[i]) { + case 0x1: + if (len >= 3) { + Message m = Message.obtain(mHandler, MESSAGE_SWITCH); + m.obj = new SwitchMsg(buffer[i+1], buffer[i+2]); + mHandler.sendMessage(m); + } + i += 3; + break; + + case 0x4: + if (len >= 3) { + Message m = Message.obtain(mHandler, MESSAGE_TEMPERATURE); + m.obj = new TemperatureMsg(composeInt(buffer[i+1], buffer[i+2])); + mHandler.sendMessage(m); + } + i += 3; + break; + + case 0x5: + if (len >= 3) { + Message m = Message.obtain(mHandler, MESSAGE_LIGHT); + m.obj = new LightMsg(composeInt(buffer[i+1], buffer[i+2])); + mHandler.sendMessage(m); + } + i += 3; + break; + + case 0x6: + if (len >= 3) { + Message m = Message.obtain(mHandler, MESSAGE_JOY); + m.obj = new JoyMsg(buffer[i+1], buffer[i+2]); + mHandler.sendMessage(m); + } + i += 3; + break; + + default: + Log.d(TAG, "unknown msg: " + buffer[i]); + i = len; + break; + } + } + + } + Log.d(TAG, "thread out"); + } + + Handler mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case MESSAGE_SWITCH: + SwitchMsg o = (SwitchMsg)msg.obj; + if (o.getSw() == 0) + mButton1Image.setImageDrawable(o.getState() != 0 ? greenball : redball); + else if (o.getSw() == 1) + mButton2Image.setImageDrawable(o.getState() != 0 ? greenball : redball); + else if (o.getSw() == 2) + mButton3Image.setImageDrawable(o.getState() != 0 ? greenball : redball); + else if (o.getSw() == 3) + mCap.setImageDrawable(o.getState() != 0 ? greenball : redball); + break; + + case MESSAGE_TEMPERATURE: + TemperatureMsg t = (TemperatureMsg)msg.obj; + mTemperature.setText(String.format("%04x", t.getTemperature())); + break; + + case MESSAGE_LIGHT: + LightMsg l = (LightMsg)msg.obj; + mLight.setText(String.format("%04x", l.getLight())); + break; + + case MESSAGE_JOY: + JoyMsg j = (JoyMsg)msg.obj; + mJoyX.setText(String.format("%d", j.getX())); + mJoyY.setText(String.format("%d", j.getY())); + break; + + } + } + }; + + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + byte[] buffer = new byte[3]; + if (progress > 255) + progress = 255; + + buffer[0] = 0x2; + buffer[1] = -1; + buffer[2] = (byte) progress; + + if (seekBar == mLed1Red) + buffer[1] = 0x0; + else if (seekBar == mLed1Green) + buffer[1] = 0x1; + else if (seekBar == mLed1Blue) + buffer[1] = 0x2; + else if (seekBar == mLed2Red) + buffer[1] = 0x3; + else if (seekBar == mLed2Green) + buffer[1] = 0x4; + else if (seekBar == mLed2Blue) + buffer[1] = 0x5; + else if (seekBar == mLed3Red) + buffer[1] = 0x6; + else if (seekBar == mLed3Green) + buffer[1] = 0x7; + else if (seekBar == mLed3Blue) + buffer[1] = 0x8; + else if (seekBar == mServo1) + buffer[1] = 0x10; + else if (seekBar == mServo2) + buffer[1] = 0x11; + else if (seekBar == mServo3) + buffer[1] = 0x12; + + if (buffer[1] != -1) { + try { + mOutputStream.write(buffer); + } catch (IOException e) { + Log.e(TAG, "write failed", e); + } + } + + } + + public void onStartTrackingTouch(SeekBar seekBar) { + } + + public void onStopTrackingTouch(SeekBar seekBar) { + } + + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + byte[] buffer = new byte[3]; + buffer[0] = 0x3; + buffer[1] = -1; + buffer[2] = isChecked ? (byte)0x1 : (byte)0x0; + + if (buttonView == mRelay1Button) + buffer[1] = 0; + else if (buttonView == mRelay2Button) + buffer[1] = 1; + + if (buffer[1] != -1) { + try { + mOutputStream.write(buffer); + } catch (IOException e) { + Log.e(TAG, "write failed", e); + } + } + } +} + |