Skip to content

Commit f164a5e

Browse files
committed
- building out first test for SqAN Test to execute to gauge SqAN performance
- gradle upgrade
1 parent 99d1c8c commit f164a5e

19 files changed

Lines changed: 1104 additions & 57 deletions

testing/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
<provider
3838
android:name="org.sofwerx.sqantest.providers.SqANFileProvider"
39-
android:authorities="${applicationId}.mission.provider"
39+
android:authorities="${applicationId}.report.provider"
4040
android:exported="false"
4141
android:grantUriPermissions="true">
4242
<meta-data

testing/app/src/main/java/org/sofwerx/sqantest/IpcBroadcastTransceiver.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
import org.sofwerx.sqan.ipc.BftBroadcast;
1111
import org.sofwerx.sqan.ipc.BftDevice;
12+
import org.sofwerx.sqantest.tests.support.PacketException;
13+
import org.sofwerx.sqantest.tests.support.TestException;
14+
import org.sofwerx.sqantest.tests.support.TestPacket;
1215

1316
import java.util.ArrayList;
1417

@@ -24,6 +27,7 @@ public class IpcBroadcastTransceiver extends BroadcastReceiver {
2427
private final static String PACKET_CHANNEL = "channel";
2528
private final static String RECEIVED = "rcv";
2629
private final static String CHAT_CHANNEL = "chat";
30+
private final static String TEST_CHANNEL = "test";
2731
private static IpcBroadcastTransceiver receiver = null;
2832
private static boolean isSqAn;
2933
private static BftBroadcast broadcast = new BftBroadcast();
@@ -35,6 +39,10 @@ public class IpcBroadcastTransceiver extends BroadcastReceiver {
3539
public interface IpcListener {
3640
void onChatPacketReceived(int origin, byte[] data);
3741
void onSaBroadcastReceived(BftBroadcast broadcast);
42+
void onTestPacketReceived(TestPacket packet);
43+
void onError(TestException error);
44+
void onOtherDataReceived(int origin, int size);
45+
void onTestCommand(byte command);
3846
}
3947

4048
public static void register(Context context) {
@@ -94,6 +102,17 @@ public static void broadcastChat(Context context, byte[] bytes) {
94102
}
95103
}
96104

105+
public static void broadcastTest(Context context, TestPacket packet) {
106+
if ((context != null) && (packet != null)) {
107+
Intent intent = new Intent(BROADCAST_PKT);
108+
intent.putExtra(PACKET_BYTES,packet.toByteArray());
109+
intent.putExtra(PACKET_CHANNEL,TEST_CHANNEL);
110+
if (isSqAn)
111+
intent.putExtra(RECEIVED,true);
112+
context.sendBroadcast(intent);
113+
}
114+
}
115+
97116
@Override
98117
public void onReceive(Context context, Intent intent) {
99118
if (intent != null) {
@@ -111,9 +130,30 @@ public void onReceive(Context context, Intent intent) {
111130
if (listener != null)
112131
listener.onSaBroadcastReceived(broadcast);
113132
} else if (CHAT_CHANNEL.equalsIgnoreCase(incomingChannel)) {
114-
int src = bundle.getInt(PACKET_ORIGIN,Integer.MIN_VALUE);
133+
int src = bundle.getInt(PACKET_ORIGIN,SqAnTestService.BROADCAST_ADDRESS);
115134
if (listener != null)
116135
listener.onChatPacketReceived(src,bytes);
136+
} else if (TEST_CHANNEL.equalsIgnoreCase(incomingChannel)) {
137+
int src = bundle.getInt(PACKET_ORIGIN,SqAnTestService.BROADCAST_ADDRESS);
138+
if (listener != null) {
139+
if (bytes.length > 0) {
140+
if (bytes.length == 1)
141+
listener.onTestCommand(bytes[0]);
142+
try {
143+
TestPacket packet = TestPacket.newTestPacket(bytes);
144+
if (packet.getOrigin() == SqAnTestService.BROADCAST_ADDRESS)
145+
packet.setOrigin(src);
146+
listener.onTestPacketReceived(TestPacket.newTestPacket(bytes));
147+
} catch (PacketException e) {
148+
listener.onError(e);
149+
}
150+
} else
151+
listener.onError(new PacketException("Empty test packet received over IPC"));
152+
}
153+
} else {
154+
int src = bundle.getInt(PACKET_ORIGIN,SqAnTestService.BROADCAST_ADDRESS);
155+
if (listener != null)
156+
listener.onOtherDataReceived(src,(bytes==null)?0:bytes.length);
117157
}
118158
}
119159
}

testing/app/src/main/java/org/sofwerx/sqantest/SqAnTestService.java

Lines changed: 143 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,31 @@
1111
import android.os.IBinder;
1212
import android.util.Log;
1313

14+
import androidx.core.content.FileProvider;
15+
1416
import org.sofwerx.sqan.ipc.BftBroadcast;
1517
import org.sofwerx.sqan.ipc.BftDevice;
1618
import org.sofwerx.sqantest.tests.AbstractTest;
17-
import org.sofwerx.sqantest.tests.TestTest;
19+
import org.sofwerx.sqantest.tests.SimpleTest;
20+
import org.sofwerx.sqantest.tests.support.TestException;
21+
import org.sofwerx.sqantest.tests.support.TestPacket;
1822

23+
import java.io.File;
1924
import java.util.ArrayList;
2025
import java.util.concurrent.atomic.AtomicBoolean;
2126

2227
public class SqAnTestService extends Service implements IpcBroadcastTransceiver.IpcListener {
2328
private final static String TAG = "SqAnTestSvc";
29+
public final static int BROADCAST_ADDRESS = Integer.MIN_VALUE;
30+
private final static int TEST_NOTIFICATION_ID = 2;
2431
private final static int NOTIFICATION_ID = 1;
2532
private final static String NOTIFICATION_CHANNEL = "sqan_test";
2633
public final static String ACTION_STOP = "STOP";
27-
private AbstractTest test = new TestTest(); //FIXME this is for testing the test mechanism only
28-
//private ArrayList<org.sofwerx.sqan.ipc.BftDevice> devices;
34+
private AbstractTest test = new SimpleTest(this); //FIXME this is for testing the test mechanism only
2935
private AtomicBoolean isRunning = new AtomicBoolean(false);
3036
private IpcBroadcastTransceiver.IpcListener uiIpcListener = null;
31-
private BftBroadcast bftBroadcast;
37+
private static BftBroadcast bftBroadcast;
38+
private int thisDevice = BROADCAST_ADDRESS;
3239

3340
private final IBinder mBinder = new SqAnTestBinder();
3441

@@ -39,27 +46,157 @@ public class SqAnTestService extends Service implements IpcBroadcastTransceiver.
3946
public AbstractTest getTest() { return test; }
4047
public void setTest(AbstractTest test) { this.test = test; }
4148

49+
public int getDeviceId() { return thisDevice; }
50+
4251
@Override
4352
public void onChatPacketReceived(int origin, byte[] data) {
4453
if (uiIpcListener != null)
4554
uiIpcListener.onChatPacketReceived(origin, data);
55+
if (test != null)
56+
test.onOtherDataReceived(origin,(data==null)?0:data.length);
4657
}
4758

4859
@Override
4960
public void onSaBroadcastReceived(BftBroadcast broadcast) {
5061
this.bftBroadcast = broadcast;
62+
if ((broadcast != null) && (thisDevice == BROADCAST_ADDRESS)) {
63+
ArrayList<BftDevice> devices = broadcast.getDevices();
64+
if ((devices != null) && !devices.isEmpty())
65+
thisDevice = devices.get(0).getUUID();
66+
}
5167
if (uiIpcListener != null)
5268
uiIpcListener.onSaBroadcastReceived(broadcast);
5369
}
5470

71+
@Override
72+
public void onTestPacketReceived(TestPacket packet) {
73+
if (test != null)
74+
test.onTestPacketReceived(packet);
75+
}
76+
77+
@Override
78+
public void onError(TestException error) {
79+
Log.e(TAG,"Testing error: "+error.getMessage());
80+
if (test != null)
81+
test.onException(error);
82+
}
83+
84+
@Override
85+
public void onOtherDataReceived(int origin, int size) {
86+
if (test != null)
87+
test.onOtherDataReceived(origin,size);
88+
}
89+
90+
@Override
91+
public void onTestCommand(byte command) {
92+
AbstractTest newTest = AbstractTest.newFromCommand(this,command);
93+
if (test != null) {
94+
if (newTest == null)
95+
test.stop();
96+
else {
97+
if (test.getCommandType() == newTest.getCommandType()) {
98+
if (!test.isRunning())
99+
test.start();
100+
return; //ignore as this test is already running
101+
} else
102+
test.stop();
103+
}
104+
}
105+
test = newTest;
106+
notifyOfTest(test);
107+
if (uiIpcListener != null)
108+
uiIpcListener.onTestCommand(command);
109+
}
110+
111+
public void notifyOfTest(AbstractTest test) {
112+
if (test != null)
113+
setForeground(test.getName()+" is "+(test.isRunning()?"running...":"paused"));
114+
else
115+
setForeground("SqAN Test is ready");
116+
}
117+
118+
/*public void notifyOfTest(AbstractTest test) {
119+
NotificationManager notificationManager = getSystemService(NotificationManager.class);
120+
if (test == null)
121+
notificationManager.cancel(TEST_NOTIFICATION_ID);
122+
else {
123+
PendingIntent pendingIntent = null;
124+
try {
125+
Intent notificationIntent = new Intent(this, Class.forName("org.sofwerx.sqantest.ui.MainActivity"));
126+
pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
127+
} catch (ClassNotFoundException ignore) {
128+
}
129+
130+
String message = "Test "+test.getName()+" in progress";
131+
132+
Notification.Builder builder;
133+
builder = new Notification.Builder(this);
134+
builder.setContentIntent(pendingIntent);
135+
builder.setSmallIcon(R.drawable.ic_notify);
136+
builder.setContentTitle("Testing...");
137+
builder.setTicker(message);
138+
builder.setContentText(message);
139+
140+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
141+
buildNotificationChannel();
142+
builder.setChannelId(NOTIFICATION_CHANNEL);
143+
}
144+
145+
notificationManager.notify(TEST_NOTIFICATION_ID, builder.build());
146+
}
147+
}*/
148+
149+
public void notifyOfReport(AbstractTest test, File file) {
150+
NotificationManager notificationManager = getSystemService(NotificationManager.class);
151+
if (test == null)
152+
notificationManager.cancel(TEST_NOTIFICATION_ID);
153+
else {
154+
PendingIntent pendingIntent = null;
155+
try {
156+
Intent notificationIntent = new Intent(this, Class.forName("org.sofwerx.sqantest.ui.MainActivity"));
157+
pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
158+
} catch (ClassNotFoundException ignore) {
159+
}
160+
161+
Notification.Builder builder;
162+
builder = new Notification.Builder(this);
163+
164+
String message = test.getName();
165+
if ((file != null) && file.exists()) {
166+
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
167+
intentShareFile.setType("application/octet-stream");
168+
intentShareFile.putExtra(Intent.EXTRA_STREAM,
169+
FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".report.provider", file));
170+
intentShareFile.putExtra(Intent.EXTRA_SUBJECT, file.getName());
171+
intentShareFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_ACTIVITY_NEW_TASK);
172+
message += " and saved in " + file.getPath();
173+
PendingIntent pIntentShare = PendingIntent.getService(this, 0, intentShareFile, PendingIntent.FLAG_UPDATE_CURRENT);
174+
builder.addAction(android.R.drawable.ic_menu_share, "Share", pIntentShare);
175+
}
176+
177+
builder.setContentIntent(pendingIntent);
178+
builder.setSmallIcon(R.drawable.ic_notify);
179+
builder.setContentTitle("Report ready");
180+
builder.setTicker(message);
181+
builder.setContentText(message);
182+
183+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
184+
buildNotificationChannel();
185+
builder.setChannelId(NOTIFICATION_CHANNEL);
186+
}
187+
188+
notificationManager.notify(TEST_NOTIFICATION_ID, builder.build());
189+
}
190+
}
191+
55192
public BftBroadcast getLastBftBroadcast() { return bftBroadcast; }
56193

57-
public BftDevice getDevice(int uuid) {
194+
public static BftDevice getDevice(int uuid) {
58195
if ((bftBroadcast != null) && (uuid > 0l)) {
59196
ArrayList<BftDevice> devices = bftBroadcast.getDevices();
60197
if ((devices != null) && !devices.isEmpty()) {
61198
for (BftDevice device:devices) {
62-
if ((device != null) && (device.getUUID() == uuid))
199+
if ((device != null) && (device.getUUID() != BROADCAST_ADDRESS) && (device.getUUID() == uuid))
63200
return device;
64201
}
65202
}

0 commit comments

Comments
 (0)