Skip to content

Commit 88c0578

Browse files
authored
Merge pull request #271 from ajayyy/improvements
Fixed issues with new features
2 parents 24291ee + b3399f0 commit 88c0578

File tree

6 files changed

+117
-68
lines changed

6 files changed

+117
-68
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public void sendLabels() {
255255
public void sendData() {
256256
try {
257257
String fullMessage = listeningActitivty.versionCode + ":::";
258-
for (String message : listeningActitivty.pendingMessages) {
258+
for (String message : listeningActitivty.unsentData) {
259259
if (!fullMessage.equals(listeningActitivty.versionCode + ":::")) {
260260
fullMessage += "::";
261261
}
@@ -264,7 +264,7 @@ public void sendData() {
264264
sentPendingMessages.add(message);
265265
}
266266

267-
if (listeningActitivty.pendingMessages.isEmpty()) {
267+
if (listeningActitivty.unsentData.isEmpty()) {
268268
fullMessage += "nodata";
269269
}
270270

@@ -286,7 +286,7 @@ public void deleteData() { //deleted items that are in sent pending messages (be
286286
editor2.apply();
287287

288288
for (String message : new ArrayList<>(sentPendingMessages)) {
289-
listeningActitivty.pendingMessages.remove(message);
289+
listeningActitivty.unsentData.remove(message);
290290
sentPendingMessages.remove(message);
291291

292292
int loc = listeningActitivty.getLocationInSharedMessages(message);
@@ -303,7 +303,12 @@ public void deleteData() { //deleted items that are in sent pending messages (be
303303
listeningActitivty.runOnUiThread(new Runnable() {
304304
@Override
305305
public void run() {
306-
((TextView) (listeningActitivty.findViewById(R.id.numberOfPendingMessagesLayout)).findViewById(R.id.numberOfPendingMessages)).setText(listeningActitivty.pendingMessages.size() + "");
306+
if (listeningActitivty instanceof MainActivity) {
307+
((TextView) (listeningActitivty.findViewById(R.id.numberOfPendingMessagesLayout)).findViewById(R.id.numberOfPendingMessages)).setText(listeningActitivty.unsentData.size() + "");
308+
} else if (listeningActitivty instanceof StartActivity) {
309+
//for the start screen layout
310+
((TextView) listeningActitivty.findViewById(R.id.numberOfPendingMessages)).setText("Unsent Data: " + listeningActitivty.unsentData.size());
311+
}
307312
}
308313
});
309314
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public class ListenerThread implements Runnable{
3333
//if currently connected to a device
3434
boolean connected = false;
3535

36+
boolean running = true;
37+
38+
BluetoothServerSocket bss = null;
39+
3640
public ListenerThread(ListeningActitivty listeningActitivty){
3741
this.listeningActitivty = listeningActitivty;
3842
}
@@ -44,8 +48,7 @@ public void run() {
4448

4549
ba = BluetoothAdapter.getDefaultAdapter();
4650

47-
while (true) {
48-
BluetoothServerSocket bss = null;
51+
while (running) {
4952
try {
5053
System.out.println("started search");
5154
bss = ba.listenUsingRfcommWithServiceRecord("LakeEffectScoutingApp", UUID.fromString("6ba6afdc-6a0a-4b1d-a2bf-f71ac108b636"));
@@ -57,7 +60,7 @@ public void run() {
5760
e.printStackTrace();
5861
}
5962

60-
if (bluetoothSocket.isConnected()) {
63+
if (bluetoothSocket != null && bluetoothSocket.isConnected()) {
6164
//now connected to a device
6265
connected = true;
6366

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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ca.lakeeffect.scoutingapp;
22

3+
import android.app.Activity;
4+
import android.content.Context;
35
import android.content.SharedPreferences;
46
import android.support.v7.app.AppCompatActivity;
57
import android.widget.ArrayAdapter;
@@ -27,16 +29,64 @@ public class ListeningActitivty extends AppCompatActivity {
2729

2830
TextView matchesLeftText; //text that shows the matches left until off
2931

30-
ArrayList<String> pendingMessages = new ArrayList<>();
32+
ArrayList<String> unsentData = new ArrayList<>();
3133

3234
String savedLabels = null; //generated at the beginning
3335

36+
public void loadUnsentData() {
37+
//go through all saved pending messages and add them to the variable
38+
//called pendingMessages for legacy reasons
39+
SharedPreferences prefs = getSharedPreferences("pendingMessages", MODE_PRIVATE);
40+
int messageAmount = prefs.getInt("messageAmount", 0);
41+
for (int i = 0; i < messageAmount; i++) {
42+
if (prefs.getString("message" + i, null) == null) {
43+
messageAmount ++;
44+
i++;
45+
if(i > 150){
46+
break;
47+
}
48+
} else {
49+
unsentData.add(prefs.getString("message" + i, ""));
50+
}
51+
}
52+
53+
//reset the amount of pending messages
54+
SharedPreferences prefs2 = getSharedPreferences("pendingMessages", Activity.MODE_PRIVATE);
55+
SharedPreferences.Editor editor2 = prefs2.edit();
56+
editor2.putInt("messageAmount", unsentData.size());
57+
editor2.apply();
58+
}
59+
60+
public void loadSchedule() {
61+
//load the saved schedule
62+
SharedPreferences schedulePrefs = getSharedPreferences("userSchedule", Context.MODE_PRIVATE);
63+
int userAmount = schedulePrefs.getInt("userAmount", 0);
64+
65+
for (int i = 0; i < userAmount; i++) {
66+
String[] robotNumbers = schedulePrefs.getString("robots" + i, "").split(",");
67+
String[] alliances = schedulePrefs.getString("alliances" + i, "").split(",");
68+
String userName = schedulePrefs.getString("userName" + i, "");
69+
70+
UserData user = new UserData(i, userName);
71+
72+
for (String robotNum : robotNumbers) {
73+
user.robots.add(Integer.parseInt(robotNum));
74+
}
75+
for (String alliance : alliances) {
76+
user.alliances.add(Boolean.parseBoolean(alliance));
77+
}
78+
79+
//add the user data to the list of schedules
80+
schedules.add(user);
81+
}
82+
}
83+
3484
public void updateUserIDSpinner() {
3585
String oldSelection = ((String) userIDSpinner.getSelectedItem());
3686

3787
ArrayList<String> userNames = new ArrayList<>();
3888

39-
if (userNames.size() > 0) {
89+
if (schedules.size() > 0) {
4090
userNames.add("Please choose a name");
4191
for (UserData userData : schedules){
4292
userNames.add(userData.userName);

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

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import android.support.v4.app.ActivityCompat;
1818
import android.support.v4.view.ViewPager;
1919
import android.support.v7.app.AlertDialog;
20-
import android.support.v7.app.AppCompatActivity;
2120
import android.support.v7.widget.PopupMenu;
2221
import android.text.Editable;
2322
import android.text.TextWatcher;
@@ -132,35 +131,14 @@ protected void onCreate(Bundle savedInstanceState) {
132131
//call alert (asking scout name and robot number)
133132
alert(false);
134133

135-
//add all buttons and counters etc.
136-
137-
//go through all saved pending messages and add them to the variable
138-
SharedPreferences prefs = getSharedPreferences("pendingMessages", MODE_PRIVATE);
139-
int messageAmount = prefs.getInt("messageAmount", 0);
140-
for (int i = 0; i < messageAmount; i++) {
141-
if (prefs.getString("message" + i, null) == null) {
142-
messageAmount ++;
143-
i++;
144-
if(i > 150){
145-
break;
146-
}
147-
} else {
148-
pendingMessages.add(prefs.getString("message" + i, ""));
149-
}
150-
}
151-
152-
//reset the amount of pending messages
153-
SharedPreferences prefs2 = getSharedPreferences("pendingMessages", Activity.MODE_PRIVATE);
154-
SharedPreferences.Editor editor2 = prefs2.edit();
155-
editor2.putInt("messageAmount", pendingMessages.size());
156-
editor2.apply();
134+
loadUnsentData();
157135

158136
//set device name
159137
BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
160138
((TextView) findViewById(R.id.deviceNameLayout).findViewById(R.id.deviceName)).setText(ba.getName()); //if this method ends up not working refer to https://stackoverflow.com/a/6662271/1985387
161139

162140
//set pending messages number on ui
163-
((TextView) findViewById(R.id.numberOfPendingMessagesLayout).findViewById(R.id.numberOfPendingMessages)).setText(pendingMessages.size() + "");
141+
((TextView) findViewById(R.id.numberOfPendingMessagesLayout).findViewById(R.id.numberOfPendingMessages)).setText(unsentData.size() + "");
164142

165143

166144
//setup scrolling viewpager
@@ -173,27 +151,8 @@ protected void onCreate(Bundle savedInstanceState) {
173151

174152
robotNumText.setText("Round: " + matchNumber + " Robot: " + robotNum);
175153

176-
//load the saved schedule
177-
SharedPreferences schedulePrefs = getSharedPreferences("userSchedule", Context.MODE_PRIVATE);
178-
int userAmount = schedulePrefs.getInt("userAmount", 0);
154+
loadSchedule();
179155

180-
for (int i = 0; i < userAmount; i++) {
181-
String[] robotNumbers = schedulePrefs.getString("robots" + i, "").split(",");
182-
String[] alliances = schedulePrefs.getString("alliances" + i, "").split(",");
183-
String userName = schedulePrefs.getString("userName" + i, "");
184-
185-
UserData user = new UserData(i, userName);
186-
187-
for (String robotNum : robotNumbers) {
188-
user.robots.add(Integer.parseInt(robotNum));
189-
}
190-
for (String alliance : alliances) {
191-
user.alliances.add(Boolean.parseBoolean(alliance));
192-
}
193-
194-
//add the user data to the list of schedules
195-
schedules.add(user);
196-
}
197156
//update the UI if necessary
198157
if (userIDSpinner != null) {
199158
updateUserIDSpinner();
@@ -594,7 +553,7 @@ public boolean saveData() {
594553
fulldata = Base64.encodeToString(fulldata.getBytes(Charset.forName("UTF-8")), Base64.DEFAULT);
595554

596555
//add to pending messages
597-
pendingMessages.add(fulldata);
556+
unsentData.add(fulldata);
598557
//add to sharedprefs
599558
SharedPreferences prefs = getSharedPreferences("pendingMessages", MODE_PRIVATE);
600559
SharedPreferences.Editor editor = prefs.edit();
@@ -603,7 +562,7 @@ public boolean saveData() {
603562
editor.apply();
604563

605564
//set pending messages number on ui
606-
((TextView) findViewById(R.id.numberOfPendingMessagesLayout).findViewById(R.id.numberOfPendingMessages)).setText(pendingMessages.size() + "");
565+
((TextView) findViewById(R.id.numberOfPendingMessagesLayout).findViewById(R.id.numberOfPendingMessages)).setText(unsentData.size() + "");
607566

608567
out.close();
609568

@@ -622,7 +581,7 @@ public void run() {
622581
byte[] bytes = new byte[1000];
623582
try {
624583
if (!connected) {
625-
pendingMessages.add(robotNum + ":" + labels.toString() + ":" + data.toString());
584+
unsentData.add(robotNum + ":" + labels.toString() + ":" + data.toString());
626585
SharedPreferences prefs = getSharedPreferences("pendingMessages", MODE_PRIVATE);
627586
SharedPreferences.Editor editor = prefs.edit();
628587
editor.putString("message" + prefs.getInt("messageAmount", 0), robotNum + ":" + labels.toString() + ":" + data.toString());
@@ -635,7 +594,7 @@ public void run() {
635594
return;
636595
}
637596
if (!connected) {
638-
pendingMessages.add(robotNum + ":" + labels.toString() + ":" + data.toString());
597+
unsentData.add(robotNum + ":" + labels.toString() + ":" + data.toString());
639598
SharedPreferences prefs = getSharedPreferences("pendingMessages", MODE_PRIVATE);
640599
SharedPreferences.Editor editor = prefs.edit();
641600
editor.putString("message" + prefs.getInt("messageAmount", 0), robotNum + ":" + labels.toString() + ":" + data.toString());
@@ -692,8 +651,8 @@ public void onClick(DialogInterface dialog, int which) {
692651
alert(false);
693652
}
694653
if (item.getItemId() == R.id.resetPendingMessages) {
695-
for(int i = 0; i< pendingMessages.size(); i++){
696-
pendingMessages.remove(i);
654+
for(int i = 0; i< unsentData.size(); i++){
655+
unsentData.remove(i);
697656
}
698657

699658
SharedPreferences prefs = getSharedPreferences("pendingMessages", Activity.MODE_PRIVATE);
@@ -705,7 +664,7 @@ public void onClick(DialogInterface dialog, int which) {
705664
runOnUiThread(new Runnable() {
706665
@Override
707666
public void run() {
708-
((TextView) findViewById(R.id.numberOfPendingMessagesLayout).findViewById(R.id.numberOfPendingMessages)).setText(pendingMessages.size() + "");
667+
((TextView) findViewById(R.id.numberOfPendingMessagesLayout).findViewById(R.id.numberOfPendingMessages)).setText(unsentData.size() + "");
709668
}
710669
});
711670
}

0 commit comments

Comments
 (0)