AccessoryChat.java revision 580b64111788fd312697d4fe60017127877dadf4
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.app.PendingIntent; 21import android.content.BroadcastReceiver; 22import android.content.Context; 23import android.content.Intent; 24import android.content.IntentFilter; 25import android.os.Bundle; 26import android.os.Handler; 27import android.os.Message; 28import android.os.ParcelFileDescriptor; 29import android.view.KeyEvent; 30import android.view.View; 31import android.view.inputmethod.EditorInfo; 32import android.util.Log; 33import android.widget.EditText; 34import android.widget.TextView; 35 36import com.android.future.usb.UsbAccessory; 37import com.android.future.usb.UsbManager; 38 39import java.io.FileDescriptor; 40import java.io.FileInputStream; 41import java.io.FileOutputStream; 42import java.io.IOException; 43 44public class AccessoryChat extends Activity implements Runnable, TextView.OnEditorActionListener { 45 46 private static final String TAG = "AccessoryChat"; 47 48 private static final String ACTION_USB_PERMISSION = 49 "com.android.accessorychat.action.USB_PERMISSION"; 50 51 private TextView mLog; 52 private EditText mEditText; 53 private ParcelFileDescriptor mFileDescriptor; 54 private FileInputStream mInputStream; 55 private FileOutputStream mOutputStream; 56 private UsbManager mUsbManager; 57 private PendingIntent mPermissionIntent; 58 private boolean mPermissionRequestPending; 59 60 private static final int MESSAGE_LOG = 1; 61 62 private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { 63 @Override 64 public void onReceive(Context context, Intent intent) { 65 if (ACTION_USB_PERMISSION.equals(intent.getAction())) { 66 synchronized (this) { 67 UsbAccessory accessory = UsbManager.getAccessory(intent); 68 if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { 69 openAccessory(accessory); 70 } else { 71 Log.d(TAG, "permission denied for accessory " + accessory); 72 } 73 mPermissionRequestPending = false; 74 } 75 } 76 } 77 }; 78 79 @Override 80 public void onCreate(Bundle savedInstanceState) { 81 super.onCreate(savedInstanceState); 82 83 mUsbManager = UsbManager.getInstance(this); 84 mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); 85 IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); 86 registerReceiver(mUsbReceiver, filter); 87 88 setContentView(R.layout.accessory_chat); 89 mLog = (TextView)findViewById(R.id.log); 90 mEditText = (EditText)findViewById(R.id.message); 91 mEditText.setOnEditorActionListener(this); 92 } 93 94 @Override 95 public void onResume() { 96 super.onResume(); 97 98 Intent intent = getIntent(); 99 Log.d(TAG, "intent: " + intent); 100 UsbAccessory[] accessories = mUsbManager.getAccessoryList(); 101 UsbAccessory accessory = (accessories == null ? null : accessories[0]); 102 if (accessory != null) { 103 if (mUsbManager.hasPermission(accessory)) { 104 openAccessory(accessory); 105 } else { 106 synchronized (mUsbReceiver) { 107 if (!mPermissionRequestPending) { 108 mUsbManager.requestPermission(accessory, mPermissionIntent); 109 mPermissionRequestPending = true; 110 } 111 } 112 } 113 } else { 114 Log.d(TAG, "mAccessory is null"); 115 } 116 } 117 118 @Override 119 public void onPause() { 120 super.onPause(); 121 if (mFileDescriptor != null) { 122 try { 123 mFileDescriptor.close(); 124 } catch (IOException e) { 125 } finally { 126 mFileDescriptor = null; 127 } 128 } 129 } 130 131 @Override 132 public void onDestroy() { 133 unregisterReceiver(mUsbReceiver); 134 super.onDestroy(); 135 } 136 137 private void openAccessory(UsbAccessory accessory) { 138 mFileDescriptor = mUsbManager.openAccessory(accessory); 139 if (mFileDescriptor != null) { 140 FileDescriptor fd = mFileDescriptor.getFileDescriptor(); 141 mInputStream = new FileInputStream(fd); 142 mOutputStream = new FileOutputStream(fd); 143 Thread thread = new Thread(null, this, "AccessoryChat"); 144 thread.start(); 145 Log.d(TAG, "openAccessory succeeded"); 146 } else { 147 Log.d(TAG, "openAccessory fail"); 148 } 149 } 150 151 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 152 if (actionId == EditorInfo.IME_ACTION_DONE && mOutputStream != null) { 153 try { 154 mOutputStream.write(v.getText().toString().getBytes()); 155 } catch (IOException e) { 156 Log.e(TAG, "write failed", e); 157 } 158 v.setText(""); 159 return true; 160 } 161 Log.d(TAG, "onEditorAction " + actionId + " event: " + event); 162 return false; 163 } 164 165 public void run() { 166 int ret = 0; 167 byte[] buffer = new byte[16384]; 168 while (ret >= 0) { 169 try { 170 ret = mInputStream.read(buffer); 171 } catch (IOException e) { 172 break; 173 } 174 175 if (ret > 0) { 176 Message m = Message.obtain(mHandler, MESSAGE_LOG); 177 String text = new String(buffer, 0, ret); 178 Log.d(TAG, "chat: " + text); 179 m.obj = text; 180 mHandler.sendMessage(m); 181 } 182 } 183 Log.d(TAG, "thread out"); 184 } 185 186 Handler mHandler = new Handler() { 187 @Override 188 public void handleMessage(Message msg) { 189 switch (msg.what) { 190 case MESSAGE_LOG: 191 mLog.setText(mLog.getText() + "\n" + (String)msg.obj); 192 break; 193 } 194 } 195 }; 196} 197 198 199