1111import android .os .IBinder ;
1212import android .util .Log ;
1313
14+ import androidx .core .content .FileProvider ;
15+
1416import org .sofwerx .sqan .ipc .BftBroadcast ;
1517import org .sofwerx .sqan .ipc .BftDevice ;
1618import 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 ;
1924import java .util .ArrayList ;
2025import java .util .concurrent .atomic .AtomicBoolean ;
2126
2227public 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