AccessoryChat.java revision b547fc297f24ce2d74fc86ef2a79a4424b6b4c59
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.accessorychat;
18
19import android.app.Activity;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.os.Bundle;
24import android.os.Handler;
25import android.os.Message;
26import android.os.ParcelFileDescriptor;
27import android.view.KeyEvent;
28import android.view.View;
29import android.view.inputmethod.EditorInfo;
30import android.util.Log;
31import android.widget.EditText;
32import android.widget.TextView;
33
34import com.android.future.usb.UsbAccessory;
35import com.android.future.usb.UsbManager;
36
37import java.io.FileDescriptor;
38import java.io.FileInputStream;
39import java.io.FileOutputStream;
40import java.io.IOException;
41
42public class AccessoryChat extends Activity implements Runnable, TextView.OnEditorActionListener {
43
44    private static final String TAG = "AccessoryChat";
45    TextView mLog;
46    EditText mEditText;
47    ParcelFileDescriptor mFileDescriptor;
48    FileInputStream mInputStream;
49    FileOutputStream mOutputStream;
50
51    private static final int MESSAGE_LOG = 1;
52
53    @Override
54    public void onCreate(Bundle savedInstanceState) {
55        super.onCreate(savedInstanceState);
56
57        setContentView(R.layout.accessory_chat);
58        mLog = (TextView)findViewById(R.id.log);
59        mEditText = (EditText)findViewById(R.id.message);
60        mEditText.setOnEditorActionListener(this);
61    }
62
63    @Override
64    public void onResume() {
65        super.onResume();
66
67        Intent intent = getIntent();
68        Log.d(TAG, "intent: " + intent);
69        UsbManager manager = UsbManager.getInstance();
70        UsbAccessory[] accessories = manager.getAccessoryList();
71        UsbAccessory accessory = (accessories == null ? null : accessories[0]);
72        if (accessory != null) {
73            mFileDescriptor = manager.openAccessory(accessory);
74            if (mFileDescriptor != null) {
75                FileDescriptor fd = mFileDescriptor.getFileDescriptor();
76                mInputStream = new FileInputStream(fd);
77                mOutputStream = new FileOutputStream(fd);
78                Thread thread = new Thread(null, this, "AccessoryChat");
79                thread.start();
80            } else {
81                Log.d(TAG, "openAccessory fail");
82            }
83        } else {
84            Log.d(TAG, "mAccessory is null");
85        }
86    }
87
88    @Override
89    public void onPause() {
90        super.onPause();
91        if (mFileDescriptor != null) {
92            try {
93                mFileDescriptor.close();
94            } catch (IOException e) {
95            } finally {
96                mFileDescriptor = null;
97            }
98        }
99    }
100
101    @Override
102    public void onDestroy() {
103       super.onDestroy();
104    }
105
106    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
107        if (actionId == EditorInfo.IME_ACTION_DONE && mOutputStream != null) {
108            try {
109                mOutputStream.write(v.getText().toString().getBytes());
110            } catch (IOException e) {
111                Log.e(TAG, "write failed", e);
112            }
113            v.setText("");
114            return true;
115        }
116        Log.d(TAG, "onEditorAction " + actionId + " event: " + event);
117        return false;
118    }
119
120    public void run() {
121        int ret = 0;
122        byte[] buffer = new byte[16384];
123        while (ret >= 0) {
124            try {
125                ret = mInputStream.read(buffer);
126            } catch (IOException e) {
127                break;
128            }
129
130            if (ret > 0) {
131                Message m = Message.obtain(mHandler, MESSAGE_LOG);
132                String text = new String(buffer, 0, ret);
133                Log.d(TAG, "chat: " + text);
134                m.obj = text;
135                mHandler.sendMessage(m);
136            }
137        }
138        Log.d(TAG, "thread out");
139    }
140
141   Handler mHandler = new Handler() {
142        @Override
143        public void handleMessage(Message msg) {
144            switch (msg.what) {
145                case MESSAGE_LOG:
146                    mLog.setText(mLog.getText() + "\n" + (String)msg.obj);
147                    break;
148             }
149        }
150    };
151}
152
153
154