Skip to content

Commit 8895f9e

Browse files
authored
Merge pull request #262 from ajayyy/improvements
Long awaited necessary features
2 parents 9d3bcf4 + 26410e6 commit 8895f9e

File tree

9 files changed

+483
-196
lines changed

9 files changed

+483
-196
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
applicationId "ca.lakeeffect.scoutingapp"
99
minSdkVersion 14
1010
targetSdkVersion 27
11-
versionCode 34
12-
versionName "Deep Space - 2.3.1"
11+
versionCode 35
12+
versionName "Deep Space - 2.4.0"
1313
}
1414
buildTypes {
1515
release {

app/src/main/java/ca/lakeeffect/scoutingapp/ConnectionThread.java

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import android.content.Context;
77
import android.content.SharedPreferences;
88
import android.util.Base64;
9-
import android.widget.RelativeLayout;
109
import android.widget.TextView;
1110
import android.widget.Toast;
1211

@@ -16,6 +15,7 @@
1615
import java.nio.charset.Charset;
1716
import java.util.ArrayList;
1817
import java.util.Arrays;
18+
import java.util.Calendar;
1919

2020
/**
2121
* Created by Ajay on 02/10/2017.
@@ -25,7 +25,7 @@
2525

2626
public class ConnectionThread implements Runnable {
2727

28-
MainActivity mainActivity;
28+
ListeningActitivty listeningActitivty;
2929

3030
BluetoothSocket bluetoothSocket;
3131
BluetoothServerSocket bss;
@@ -37,8 +37,8 @@ public class ConnectionThread implements Runnable {
3737

3838
final String endSplitter = "{e}";
3939

40-
public ConnectionThread(MainActivity mainActivity, BluetoothSocket bluetoothSocket, OutputStream out, InputStream in, BluetoothServerSocket bss) {
41-
this.mainActivity = mainActivity;
40+
public ConnectionThread(ListeningActitivty listeningActitivty, BluetoothSocket bluetoothSocket, OutputStream out, InputStream in, BluetoothServerSocket bss) {
41+
this.listeningActitivty = listeningActitivty;
4242
this.bluetoothSocket = bluetoothSocket;
4343
this.out = out;
4444
this.in = in;
@@ -76,32 +76,32 @@ public void run() {
7676
message = new String(Base64.decode(message, Base64.DEFAULT), Charset.forName("UTF-8"));
7777

7878
if (message.contains("SEND SCHEDULE")) { //received data about the schedule
79-
mainActivity.runOnUiThread(new Runnable() {
79+
listeningActitivty.runOnUiThread(new Runnable() {
8080
@Override
8181
public void run() {
82-
Toast.makeText(mainActivity, "Received schedule",
82+
Toast.makeText(listeningActitivty, "Received schedule",
8383
Toast.LENGTH_LONG).show();
8484
}
8585
});
8686

8787
loadSchedule(message);
8888

89-
//send that this message was recieved, conver to base 64 and add the end splitter first
89+
//send that this message was received, convert to base 64 and add the end splitter first
9090
this.out.write((toBase64("RECEIVED") + endSplitter).getBytes(Charset.forName("UTF-8")));
9191
} else if (message.contains("REQUEST DATA")) { //received a request
92-
mainActivity.runOnUiThread(new Runnable() {
92+
listeningActitivty.runOnUiThread(new Runnable() {
9393
@Override
9494
public void run() {
95-
Toast.makeText(mainActivity, "Sending Data",
95+
Toast.makeText(listeningActitivty, "Sending Data",
9696
Toast.LENGTH_LONG).show();
9797
}
9898
});
9999
sendData();
100100
} else if (message.contains("REQUEST LABELS")) { //received a request
101-
mainActivity.runOnUiThread(new Runnable() {
101+
listeningActitivty.runOnUiThread(new Runnable() {
102102
@Override
103103
public void run() {
104-
Toast.makeText(mainActivity, "Sending Labels",
104+
Toast.makeText(listeningActitivty, "Sending Labels",
105105
Toast.LENGTH_LONG).show();
106106
}
107107
});
@@ -117,8 +117,7 @@ public void run() {
117117
bluetoothSocket.close();
118118
bss.close();
119119
deleteData();
120-
mainActivity.listenerThread.run();
121-
new Thread(mainActivity.listenerThread).start();
120+
new Thread(listeningActitivty.listenerThread).start();
122121
break;
123122
}
124123

@@ -127,8 +126,8 @@ public void run() {
127126

128127
} catch (IOException e) {
129128
e.printStackTrace();
130-
mainActivity.listenerThread = new ListenerThread(mainActivity);
131-
new Thread(mainActivity.listenerThread).start();
129+
listeningActitivty.listenerThread = new ListenerThread(listeningActitivty);
130+
new Thread(listeningActitivty.listenerThread).start();
132131
break;
133132
}
134133

@@ -148,7 +147,7 @@ public void loadSchedule(String schedule) {
148147
String[] matches = matchSchedule.split("::");
149148

150149
//reset schedules
151-
mainActivity.schedules = new ArrayList<>();
150+
listeningActitivty.schedules = new ArrayList<>();
152151

153152
//go through the user schedule and assign robots based on the match schedule
154153
for (int userID = 0; userID < userSchedules.length; userID++) {
@@ -157,7 +156,7 @@ public void loadSchedule(String schedule) {
157156

158157
UserData currentUserData = new UserData(userID, name);
159158

160-
mainActivity.schedules.add(currentUserData);
159+
listeningActitivty.schedules.add(currentUserData);
161160

162161
for (int matchNum = 0; matchNum < userSchedule.length; matchNum++) {
163162
String[] robotNumbers = matches[matchNum].split(",");
@@ -172,28 +171,30 @@ public void loadSchedule(String schedule) {
172171
}
173172
}
174173

175-
if (mainActivity.userIDSpinner != null) {
176-
mainActivity.runOnUiThread(new Runnable() {
174+
if (listeningActitivty.userIDSpinner != null) {
175+
listeningActitivty.runOnUiThread(new Runnable() {
177176
@Override
178177
public void run() {
179178
//update the userIDSpinner if the alert is open
180-
mainActivity.updateUserIDSpinner();
179+
listeningActitivty.updateUserIDSpinner();
181180

182181
//update the UI with the time remaining
183-
mainActivity.updateMatchesLeft();
182+
if (listeningActitivty instanceof MainActivity) {
183+
((MainActivity) listeningActitivty).updateMatchesLeft();
184+
}
184185
}
185186
});
186187
}
187188

188189
//update the shared preferences
189-
SharedPreferences prefs = mainActivity.getSharedPreferences("userSchedule", Context.MODE_PRIVATE);
190+
SharedPreferences prefs = listeningActitivty.getSharedPreferences("userSchedule", Context.MODE_PRIVATE);
190191
SharedPreferences.Editor editor = prefs.edit();
191192
//set size
192-
editor.putInt("userAmount", mainActivity.schedules.size());
193+
editor.putInt("userAmount", listeningActitivty.schedules.size());
193194

194195
//go through each user and add the data
195-
for (int i = 0; i < mainActivity.schedules.size(); i++) {
196-
UserData user = mainActivity.schedules.get(i);
196+
for (int i = 0; i < listeningActitivty.schedules.size(); i++) {
197+
UserData user = listeningActitivty.schedules.get(i);
197198

198199
String robots = "";
199200
for (int s = 0; s < user.robots.size(); s++) {
@@ -219,11 +220,29 @@ public void run() {
219220
}
220221

221222
editor.apply();
223+
224+
//get current dates to store the last date updated
225+
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
226+
int currentMonth = Calendar.getInstance().get(Calendar.MONTH);
227+
int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
228+
int currentHour = Calendar.getInstance().get(Calendar.HOUR);
229+
int currentMinute = Calendar.getInstance().get(Calendar.MINUTE);
230+
SharedPreferences lastUpdatedPrefs = listeningActitivty.getSharedPreferences("lastScheduleUpdate", Activity.MODE_PRIVATE);
231+
SharedPreferences.Editor lastUpdatedEditor = lastUpdatedPrefs.edit();
232+
233+
//store it in the shared preferences
234+
lastUpdatedEditor.putInt("year", currentYear);
235+
lastUpdatedEditor.putInt("month", currentMonth);
236+
lastUpdatedEditor.putInt("day", currentDay);
237+
lastUpdatedEditor.putInt("hour", currentHour);
238+
lastUpdatedEditor.putInt("minute", currentMinute);
239+
240+
lastUpdatedEditor.apply();
222241
}
223242

224243
public void sendLabels() {
225244
try {
226-
String outString = mainActivity.versionCode + ":::" + mainActivity.savedLabels;
245+
String outString = listeningActitivty.versionCode + ":::" + listeningActitivty.savedLabels;
227246
//convert to base 64 bytes
228247
String outBase64 = Base64.encodeToString(outString.getBytes(Charset.forName("UTF-8")), Base64.DEFAULT) + endSplitter;
229248

@@ -235,17 +254,17 @@ public void sendLabels() {
235254

236255
public void sendData() {
237256
try {
238-
String fullMessage = mainActivity.versionCode + ":::";
239-
for (String message : mainActivity.pendingMessages) {
240-
if (!fullMessage.equals(mainActivity.versionCode + ":::")) {
257+
String fullMessage = listeningActitivty.versionCode + ":::";
258+
for (String message : listeningActitivty.pendingMessages) {
259+
if (!fullMessage.equals(listeningActitivty.versionCode + ":::")) {
241260
fullMessage += "::";
242261
}
243262
fullMessage += message;
244263

245264
sentPendingMessages.add(message);
246265
}
247266

248-
if (mainActivity.pendingMessages.isEmpty()) {
267+
if (listeningActitivty.pendingMessages.isEmpty()) {
249268
fullMessage += "nodata";
250269
}
251270

@@ -257,7 +276,7 @@ public void sendData() {
257276

258277
public void deleteData() { //deleted items that are in sent pending messages (because they now have been sent)
259278

260-
SharedPreferences prefs2 = mainActivity.getSharedPreferences("pendingMessages", Activity.MODE_PRIVATE);
279+
SharedPreferences prefs2 = listeningActitivty.getSharedPreferences("pendingMessages", Activity.MODE_PRIVATE);
261280
SharedPreferences.Editor editor2 = prefs2.edit();
262281
if (prefs2.getInt("messageAmount", 0) - sentPendingMessages.size() >= 0) {
263282
editor2.putInt("messageAmount", prefs2.getInt("messageAmount", 0) - sentPendingMessages.size());
@@ -267,24 +286,24 @@ public void deleteData() { //deleted items that are in sent pending messages (be
267286
editor2.apply();
268287

269288
for (String message : new ArrayList<>(sentPendingMessages)) {
270-
mainActivity.pendingMessages.remove(message);
289+
listeningActitivty.pendingMessages.remove(message);
271290
sentPendingMessages.remove(message);
272291

273-
int loc = mainActivity.getLocationInSharedMessages(message);
292+
int loc = listeningActitivty.getLocationInSharedMessages(message);
274293

275294
if (loc != -1) {
276-
SharedPreferences prefs = mainActivity.getSharedPreferences("pendingMessages", Activity.MODE_PRIVATE);
295+
SharedPreferences prefs = listeningActitivty.getSharedPreferences("pendingMessages", Activity.MODE_PRIVATE);
277296
SharedPreferences.Editor editor = prefs.edit();
278297
editor.putString("message" + loc, null);
279298
editor.apply();
280299
}
281300
}
282301

283302
//set pending messages number on ui
284-
mainActivity.runOnUiThread(new Runnable() {
303+
listeningActitivty.runOnUiThread(new Runnable() {
285304
@Override
286305
public void run() {
287-
((TextView) (mainActivity.findViewById(R.id.numberOfPendingMessagesLayout)).findViewById(R.id.numberOfPendingMessages)).setText(mainActivity.pendingMessages.size() + "");
306+
((TextView) (listeningActitivty.findViewById(R.id.numberOfPendingMessagesLayout)).findViewById(R.id.numberOfPendingMessages)).setText(listeningActitivty.pendingMessages.size() + "");
288307
}
289308
});
290309
}

app/src/main/java/ca/lakeeffect/scoutingapp/FieldUIPage.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,15 @@ public String[] getData() {
336336
}
337337

338338
for (int i = 0; i < labelActions.length; i++) {
339-
labels.append(fieldPeriod + "Cargo Ship " + labelActions[i] + ",");
339+
//most important data
340+
labels.append(fieldPeriod + "Full Cargo Ship " + labelActions[i] + ",");
340341
data.append(cargoShip[i] + ",");
342+
labels.append(fieldPeriod + "Full Rocket " + labelActions[i] + ",");
343+
data.append(fullRocket[i] + ",");
344+
345+
//extra more detailed information
341346
labels.append(fieldPeriod + "Side Cargo Ship " + labelActions[i] + ",");
342347
data.append(sideCargoShip[i] + ",");
343-
labels.append(fieldPeriod + "Level 1 Rocket " + labelActions[i] + ",");
344-
data.append(levelOneRocket[i] + ",");
345-
labels.append(fieldPeriod + "Level 2 Rocket " + labelActions[i] + ",");
346-
data.append(levelTwoRocket[i] + ",");
347-
labels.append(fieldPeriod + "Level 3 Rocket " + labelActions[i] + ",");
348-
data.append(levelThreeRocket[i] + ",");
349348

350349
if (i == 0 || i == 1) {
351350
//only for hatch, not for cargo
@@ -354,9 +353,13 @@ public String[] getData() {
354353
labels.append(fieldPeriod + "Close Rocket " + labelActions[i] + ",");
355354
data.append(closeRocket[i] + ",");
356355
}
357-
358-
labels.append(fieldPeriod + "Full Rocket " + labelActions[i] + ",");
359-
data.append(fullRocket[i] + ",");
356+
357+
labels.append(fieldPeriod + "Level 1 Rocket " + labelActions[i] + ",");
358+
data.append(levelOneRocket[i] + ",");
359+
labels.append(fieldPeriod + "Level 2 Rocket " + labelActions[i] + ",");
360+
data.append(levelTwoRocket[i] + ",");
361+
labels.append(fieldPeriod + "Level 3 Rocket " + labelActions[i] + ",");
362+
data.append(levelThreeRocket[i] + ",");
360363
}
361364

362365
return (new String[]{labels.toString(), data.toString()});

app/src/main/java/ca/lakeeffect/scoutingapp/ListenerThread.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
import android.bluetooth.BluetoothAdapter;
44
import android.bluetooth.BluetoothServerSocket;
55
import android.bluetooth.BluetoothSocket;
6-
import android.content.SharedPreferences;
76

87
import java.io.IOException;
98
import java.io.InputStream;
109
import java.io.OutputStream;
11-
import java.nio.charset.Charset;
1210
import java.util.UUID;
1311

1412
/**
@@ -20,7 +18,7 @@
2018

2119
public class ListenerThread implements Runnable{
2220

23-
MainActivity mainActivity;
21+
ListeningActitivty listeningActitivty;
2422

2523
BluetoothSocket bluetoothSocket;
2624
BluetoothAdapter ba;
@@ -32,12 +30,17 @@ public class ListenerThread implements Runnable{
3230

3331
Thread connectionThreadThreadClass;
3432

35-
public ListenerThread(MainActivity mainActivity){
36-
this.mainActivity = mainActivity;
33+
//if currently connected to a device
34+
boolean connected = false;
35+
36+
public ListenerThread(ListeningActitivty listeningActitivty){
37+
this.listeningActitivty = listeningActitivty;
3738
}
3839

3940
@Override
4041
public void run() {
42+
//not connected to a device
43+
connected = false;
4144

4245
ba = BluetoothAdapter.getDefaultAdapter();
4346

@@ -55,9 +58,11 @@ public void run() {
5558
}
5659

5760
if (bluetoothSocket.isConnected()) {
58-
//call connection thread and break;
61+
//now connected to a device
62+
connected = true;
5963

60-
connectionThread = new ConnectionThread(mainActivity, bluetoothSocket, out, in, bss);
64+
//call connection thread and break;
65+
connectionThread = new ConnectionThread(listeningActitivty, bluetoothSocket, out, in, bss);
6166
connectionThreadThreadClass = new Thread(connectionThread);
6267
connectionThreadThreadClass.start();
6368
break;

0 commit comments

Comments
 (0)