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 Log.d(TAG, "openAccessory: " + accessory); 139 mFileDescriptor = mUsbManager.openAccessory(accessory); 140 if (mFileDescriptor != null) { 141 FileDescriptor fd = mFileDescriptor.getFileDescriptor(); 142 mInputStream = new FileInputStream(fd); 143 mOutputStream = new FileOutputStream(fd); 144 Thread thread = new Thread(null, this, "AccessoryChat"); 145 thread.start(); 146 Log.d(TAG, "openAccessory succeeded"); 147 } else { 148 Log.d(TAG, "openAccessory fail"); 149 } 150 } 151 152 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 153 if (actionId == EditorInfo.IME_ACTION_DONE && mOutputStream != null) { 154 try { 155 mOutputStream.write(v.getText().toString().getBytes()); 156 } catch (IOException e) { 157 Log.e(TAG, "write failed", e); 158 } 159 v.setText(""); 160 return true; 161 } 162 Log.d(TAG, "onEditorAction " + actionId + " event: " + event); 163 return false; 164 } 165 166 public void run() { 167 int ret = 0; 168 byte[] buffer = new byte[16384]; 169 while (ret >= 0) { 170 try { 171 ret = mInputStream.read(buffer); 172 } catch (IOException e) { 173 break; 174 } 175 176 if (ret > 0) { 177 Message m = Message.obtain(mHandler, MESSAGE_LOG); 178 String text = new String(buffer, 0, ret); 179 Log.d(TAG, "chat: " + text); 180 m.obj = text; 181 mHandler.sendMessage(m); 182 } 183 } 184 Log.d(TAG, "thread out"); 185 } 186 187 Handler mHandler = new Handler() { 188 @Override 189 public void handleMessage(Message msg) { 190 switch (msg.what) { 191 case MESSAGE_LOG: 192 mLog.setText(mLog.getText() + "\n" + (String)msg.obj); 193 break; 194 } 195 } 196 }; 197} 198 199 200