Skip to content

Commit 850d675

Browse files
committed
IOIOLib: Make Android IOIOService reliable
Previously, a IOIOService would not correctly recover from a disconnect/ reconnect in the case of AOA. This change addresses this problem by forwarding all connection events through a hidden activity.
1 parent 4769a87 commit 850d675

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

software/IOIOLib/target/android/src/ioio/lib/util/android/IOIOService.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/*
22
* Copyright 2011 Ytai Ben-Tsvi. All rights reserved.
3-
*
4-
*
3+
*
4+
*
55
* Redistribution and use in source and binary forms, with or without modification, are
66
* permitted provided that the following conditions are met:
7-
*
7+
*
88
* 1. Redistributions of source code must retain the above copyright notice, this list of
99
* conditions and the following disclaimer.
10-
*
10+
*
1111
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
1212
* of conditions and the following disclaimer in the documentation and/or other materials
1313
* provided with the distribution.
14-
*
14+
*
1515
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
1616
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
1717
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR
@@ -21,7 +21,7 @@
2121
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2222
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2323
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24-
*
24+
*
2525
* The views and conclusions contained in the software and documentation are those of the
2626
* authors and should not be interpreted as representing official policies, either expressed
2727
* or implied.
@@ -36,7 +36,7 @@
3636

3737
/**
3838
* A convenience class for easy creation of IOIO-based services.
39-
*
39+
*
4040
* It is used by creating a concrete {@link Service} in your application, which
4141
* extends this class. This class then takes care of proper creation and
4242
* abortion of the IOIO connection and of a dedicated thread for IOIO
@@ -99,9 +99,7 @@ private void start(Intent intent) {
9999
helper_.start();
100100
started_ = true;
101101
} else {
102-
if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
103-
helper_.restart();
104-
}
102+
helper_.restart();
105103
}
106104
}
107105

@@ -147,7 +145,7 @@ protected void stop() {
147145
* IOIO. In multi-IOIO scenarios, where you want to identify which IOIO the
148146
* thread is for, consider overriding
149147
* {@link #createIOIOLooper(String, Object)} instead.
150-
*
148+
*
151149
* @return An implementation of {@link IOIOLooper}, or <code>null</code> to
152150
* skip.
153151
*/

software/applications/HelloIOIOService/AndroidManifest.xml

+7-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
android:label="@string/app_name">
88
<uses-library android:name="com.android.future.usb.accessory"
99
android:required="false" />
10-
<service android:name="HelloIOIOService">
10+
<service android:name="HelloIOIOService" />
11+
<activity android:label="@string/app_name" android:name="MainActivity"
12+
android:launchMode="singleTask">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
<category android:name="android.intent.category.LAUNCHER" />
16+
</intent-filter>
1117
<intent-filter>
1218
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
1319
</intent-filter>
@@ -18,14 +24,6 @@
1824
android:resource="@xml/accessory_filter" />
1925
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
2026
android:resource="@xml/device_filter" />
21-
22-
</service>
23-
<activity android:label="@string/app_name" android:name="MainActivity"
24-
android:launchMode="singleTask">
25-
<intent-filter>
26-
<action android:name="android.intent.action.MAIN" />
27-
<category android:name="android.intent.category.LAUNCHER" />
28-
</intent-filter>
2927
</activity>
3028
</application>
3129
</manifest>

software/applications/HelloIOIOService/src/ioio/examples/hello_service/HelloIOIOService.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package ioio.examples.hello_service;
22

3-
import android.app.Notification;
4-
import android.app.NotificationManager;
5-
import android.app.PendingIntent;
6-
import android.content.Intent;
7-
import android.os.IBinder;
83
import ioio.lib.api.DigitalOutput;
94
import ioio.lib.api.IOIO;
105
import ioio.lib.api.exception.ConnectionLostException;
116
import ioio.lib.util.BaseIOIOLooper;
127
import ioio.lib.util.IOIOLooper;
138
import ioio.lib.util.android.IOIOService;
9+
import android.app.Notification;
10+
import android.app.NotificationManager;
11+
import android.app.PendingIntent;
12+
import android.content.Intent;
13+
import android.os.IBinder;
1414

1515
/**
1616
* An example IOIO service. While this service is alive, it will attempt to
@@ -41,8 +41,8 @@ public void loop() throws ConnectionLostException,
4141
}
4242

4343
@Override
44-
public void onStart(Intent intent, int startId) {
45-
super.onStart(intent, startId);
44+
public int onStartCommand(Intent intent, int flags, int startId) {
45+
int result = super.onStartCommand(intent, flags, startId);
4646
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
4747
if (intent != null && intent.getAction() != null
4848
&& intent.getAction().equals("stop")) {
@@ -61,6 +61,7 @@ public void onStart(Intent intent, int startId) {
6161
notification.flags |= Notification.FLAG_ONGOING_EVENT;
6262
nm.notify(0, notification);
6363
}
64+
return result;
6465
}
6566

6667
@Override

0 commit comments

Comments
 (0)