USBUART
A library for reading/wring data via USB-UART adapters
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules
UsbBusActivity.java
Go to the documentation of this file.
1 
7 /*
8  * This file is part of USBUART Library. http://usbuart.info/
9  *
10  * Copyright © 2016 Eugene Hutorny <eugene@hutorny.in.ua>
11  *
12  * The USBUART Library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License v2
14  * as published by the Free Software Foundation;
15  *
16  * The USBUART Library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with the USBUART Library; if not, see
23  * <http://www.gnu.org/licenses/gpl-2.0.html>.
24  */
25 
26 package info.usbuart.service;
27 
28 import android.app.Activity;
29 import android.content.ComponentName;
30 import android.content.Intent;
31 import android.content.ServiceConnection;
32 import android.hardware.usb.UsbDevice;
33 import android.hardware.usb.UsbManager;
34 import android.os.Bundle;
35 import android.os.IBinder;
36 import android.util.Log;
37 
38 public class UsbBusActivity extends Activity implements ServiceConnection {
39 
40  @Override
41  public void onCreate(Bundle savedInstanceState) {
42  super.onCreate(savedInstanceState);
43  Log.d(TAG,"onCreate");
44  if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(getIntent().getAction())) {
45  device = getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);
46  }
47  if ( device == null ) finish();
48  else startService();
49  }
50 
51  @Override
52  protected void onDestroy() {
53  Log.d(TAG,"onDestroy");
54  if( binder != null ) unbindService(this);
55  super.onDestroy();
56  }
57 
58  private void startService() {
59  Log.d(TAG,"startService");
60  Intent intent = new Intent(getApplicationContext(), UsbUartService.class);
61  startService(intent);
62  bindService(intent, this, BIND_ABOVE_CLIENT);
63  }
64 
65  @Override
66  public void onServiceConnected(ComponentName name, IBinder binder) {
67  Log.d(TAG,"onServiceConnected");
68  this.binder = binder;
69  if( device != null ) {
70  IService service = (IService) binder.queryLocalInterface(IService.class.getCanonicalName());
71  service.attached(device);
72  }
73  finish();
74  }
75 
76  @Override
77  public void onServiceDisconnected(ComponentName name) {
78  Log.d(TAG,"onServiceDisconnected");
79  binder = null;
80  }
81 
82  private IBinder binder ;
83  private UsbDevice device;
84  private static String TAG = "UsbBusActivity";
85 }