FileCache: use the cache even when the system clock is wrong
[xcsoar.git] / android / src / MultiPort.java
blob716475f8dd6c5ce671a88ca1736108b06836849d
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 package org.xcsoar;
26 import java.util.Collection;
27 import java.util.LinkedList;
28 import java.util.Iterator;
29 import android.util.Log;
31 /**
32 * An #AndroidPort implementation that combines multiple #AndroidPort
33 * objects.
35 class MultiPort implements AndroidPort, InputListener {
36 private InputListener listener;
38 private static final String TAG = "XCSoar";
40 private Collection<AndroidPort> ports = new LinkedList<AndroidPort>();
41 private boolean error = false;
43 private synchronized int checkValid() {
44 boolean ready = false, limbo = false;
46 for (Iterator<AndroidPort> i = ports.iterator(); i.hasNext();) {
47 AndroidPort port = i.next();
49 switch (port.getState()) {
50 case STATE_READY:
51 ready = true;
52 break;
54 case STATE_FAILED:
55 Log.i(TAG, "Bluetooth disconnect from " + port);
57 i.remove();
58 port.close();
59 error = true;
60 break;
62 case STATE_LIMBO:
63 limbo = true;
64 break;
68 if (ready) {
69 error = false;
70 return STATE_READY;
71 } else if (limbo || !error) {
72 error = false;
73 return STATE_LIMBO;
74 } else
75 return STATE_FAILED;
78 public synchronized void add(AndroidPort port) {
79 error = false;
80 checkValid();
82 ports.add(port);
83 port.setListener(this);
86 @Override public void setListener(InputListener _listener) {
87 listener = _listener;
90 @Override public synchronized void close() {
91 error = true;
93 for (AndroidPort port : ports)
94 port.close();
96 ports.clear();
99 @Override public int getState() {
100 return checkValid();
103 @Override public boolean drain() {
104 // XXX not implemented
105 return true;
108 @Override public synchronized int getBaudRate() {
109 // XXX not implemented
110 return 19200;
113 @Override public boolean setBaudRate(int baud) {
114 // XXX not implemented
115 return true;
118 @Override public synchronized int write(byte[] data, int length) {
119 int result = -1;
121 for (Iterator<AndroidPort> i = ports.iterator(); i.hasNext();) {
122 AndroidPort port = i.next();
123 int nbytes = port.write(data, length);
124 if (nbytes < 0 && port.getState() == STATE_FAILED) {
125 error = true;
126 i.remove();
127 port.close();
128 } else if (nbytes > result)
129 result = nbytes;
132 return result;
135 @Override public void dataReceived(byte[] data, int length) {
136 InputListener l = listener;
137 if (l != null)
138 l.dataReceived(data, length);