Skip to content

Commit

Permalink
Merge pull request #271 from ajayyy/improvements
Browse files Browse the repository at this point in the history
Fixed issues with new features
  • Loading branch information
ajayyy authored Mar 22, 2019
2 parents 24291ee + b3399f0 commit 88c0578
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public void sendLabels() {
public void sendData() {
try {
String fullMessage = listeningActitivty.versionCode + ":::";
for (String message : listeningActitivty.pendingMessages) {
for (String message : listeningActitivty.unsentData) {
if (!fullMessage.equals(listeningActitivty.versionCode + ":::")) {
fullMessage += "::";
}
Expand All @@ -264,7 +264,7 @@ public void sendData() {
sentPendingMessages.add(message);
}

if (listeningActitivty.pendingMessages.isEmpty()) {
if (listeningActitivty.unsentData.isEmpty()) {
fullMessage += "nodata";
}

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

for (String message : new ArrayList<>(sentPendingMessages)) {
listeningActitivty.pendingMessages.remove(message);
listeningActitivty.unsentData.remove(message);
sentPendingMessages.remove(message);

int loc = listeningActitivty.getLocationInSharedMessages(message);
Expand All @@ -303,7 +303,12 @@ public void deleteData() { //deleted items that are in sent pending messages (be
listeningActitivty.runOnUiThread(new Runnable() {
@Override
public void run() {
((TextView) (listeningActitivty.findViewById(R.id.numberOfPendingMessagesLayout)).findViewById(R.id.numberOfPendingMessages)).setText(listeningActitivty.pendingMessages.size() + "");
if (listeningActitivty instanceof MainActivity) {
((TextView) (listeningActitivty.findViewById(R.id.numberOfPendingMessagesLayout)).findViewById(R.id.numberOfPendingMessages)).setText(listeningActitivty.unsentData.size() + "");
} else if (listeningActitivty instanceof StartActivity) {
//for the start screen layout
((TextView) listeningActitivty.findViewById(R.id.numberOfPendingMessages)).setText("Unsent Data: " + listeningActitivty.unsentData.size());
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public class ListenerThread implements Runnable{
//if currently connected to a device
boolean connected = false;

boolean running = true;

BluetoothServerSocket bss = null;

public ListenerThread(ListeningActitivty listeningActitivty){
this.listeningActitivty = listeningActitivty;
}
Expand All @@ -44,8 +48,7 @@ public void run() {

ba = BluetoothAdapter.getDefaultAdapter();

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

if (bluetoothSocket.isConnected()) {
if (bluetoothSocket != null && bluetoothSocket.isConnected()) {
//now connected to a device
connected = true;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ca.lakeeffect.scoutingapp;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
Expand Down Expand Up @@ -27,16 +29,64 @@ public class ListeningActitivty extends AppCompatActivity {

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

ArrayList<String> pendingMessages = new ArrayList<>();
ArrayList<String> unsentData = new ArrayList<>();

String savedLabels = null; //generated at the beginning

public void loadUnsentData() {
//go through all saved pending messages and add them to the variable
//called pendingMessages for legacy reasons
SharedPreferences prefs = getSharedPreferences("pendingMessages", MODE_PRIVATE);
int messageAmount = prefs.getInt("messageAmount", 0);
for (int i = 0; i < messageAmount; i++) {
if (prefs.getString("message" + i, null) == null) {
messageAmount ++;
i++;
if(i > 150){
break;
}
} else {
unsentData.add(prefs.getString("message" + i, ""));
}
}

//reset the amount of pending messages
SharedPreferences prefs2 = getSharedPreferences("pendingMessages", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor2 = prefs2.edit();
editor2.putInt("messageAmount", unsentData.size());
editor2.apply();
}

public void loadSchedule() {
//load the saved schedule
SharedPreferences schedulePrefs = getSharedPreferences("userSchedule", Context.MODE_PRIVATE);
int userAmount = schedulePrefs.getInt("userAmount", 0);

for (int i = 0; i < userAmount; i++) {
String[] robotNumbers = schedulePrefs.getString("robots" + i, "").split(",");
String[] alliances = schedulePrefs.getString("alliances" + i, "").split(",");
String userName = schedulePrefs.getString("userName" + i, "");

UserData user = new UserData(i, userName);

for (String robotNum : robotNumbers) {
user.robots.add(Integer.parseInt(robotNum));
}
for (String alliance : alliances) {
user.alliances.add(Boolean.parseBoolean(alliance));
}

//add the user data to the list of schedules
schedules.add(user);
}
}

public void updateUserIDSpinner() {
String oldSelection = ((String) userIDSpinner.getSelectedItem());

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

if (userNames.size() > 0) {
if (schedules.size() > 0) {
userNames.add("Please choose a name");
for (UserData userData : schedules){
userNames.add(userData.userName);
Expand Down
61 changes: 10 additions & 51 deletions app/src/main/java/ca/lakeeffect/scoutingapp/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.text.Editable;
import android.text.TextWatcher;
Expand Down Expand Up @@ -132,35 +131,14 @@ protected void onCreate(Bundle savedInstanceState) {
//call alert (asking scout name and robot number)
alert(false);

//add all buttons and counters etc.

//go through all saved pending messages and add them to the variable
SharedPreferences prefs = getSharedPreferences("pendingMessages", MODE_PRIVATE);
int messageAmount = prefs.getInt("messageAmount", 0);
for (int i = 0; i < messageAmount; i++) {
if (prefs.getString("message" + i, null) == null) {
messageAmount ++;
i++;
if(i > 150){
break;
}
} else {
pendingMessages.add(prefs.getString("message" + i, ""));
}
}

//reset the amount of pending messages
SharedPreferences prefs2 = getSharedPreferences("pendingMessages", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor2 = prefs2.edit();
editor2.putInt("messageAmount", pendingMessages.size());
editor2.apply();
loadUnsentData();

//set device name
BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
((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

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


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

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

//load the saved schedule
SharedPreferences schedulePrefs = getSharedPreferences("userSchedule", Context.MODE_PRIVATE);
int userAmount = schedulePrefs.getInt("userAmount", 0);
loadSchedule();

for (int i = 0; i < userAmount; i++) {
String[] robotNumbers = schedulePrefs.getString("robots" + i, "").split(",");
String[] alliances = schedulePrefs.getString("alliances" + i, "").split(",");
String userName = schedulePrefs.getString("userName" + i, "");

UserData user = new UserData(i, userName);

for (String robotNum : robotNumbers) {
user.robots.add(Integer.parseInt(robotNum));
}
for (String alliance : alliances) {
user.alliances.add(Boolean.parseBoolean(alliance));
}

//add the user data to the list of schedules
schedules.add(user);
}
//update the UI if necessary
if (userIDSpinner != null) {
updateUserIDSpinner();
Expand Down Expand Up @@ -594,7 +553,7 @@ public boolean saveData() {
fulldata = Base64.encodeToString(fulldata.getBytes(Charset.forName("UTF-8")), Base64.DEFAULT);

//add to pending messages
pendingMessages.add(fulldata);
unsentData.add(fulldata);
//add to sharedprefs
SharedPreferences prefs = getSharedPreferences("pendingMessages", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
Expand All @@ -603,7 +562,7 @@ public boolean saveData() {
editor.apply();

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

out.close();

Expand All @@ -622,7 +581,7 @@ public void run() {
byte[] bytes = new byte[1000];
try {
if (!connected) {
pendingMessages.add(robotNum + ":" + labels.toString() + ":" + data.toString());
unsentData.add(robotNum + ":" + labels.toString() + ":" + data.toString());
SharedPreferences prefs = getSharedPreferences("pendingMessages", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("message" + prefs.getInt("messageAmount", 0), robotNum + ":" + labels.toString() + ":" + data.toString());
Expand All @@ -635,7 +594,7 @@ public void run() {
return;
}
if (!connected) {
pendingMessages.add(robotNum + ":" + labels.toString() + ":" + data.toString());
unsentData.add(robotNum + ":" + labels.toString() + ":" + data.toString());
SharedPreferences prefs = getSharedPreferences("pendingMessages", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("message" + prefs.getInt("messageAmount", 0), robotNum + ":" + labels.toString() + ":" + data.toString());
Expand Down Expand Up @@ -692,8 +651,8 @@ public void onClick(DialogInterface dialog, int which) {
alert(false);
}
if (item.getItemId() == R.id.resetPendingMessages) {
for(int i = 0; i< pendingMessages.size(); i++){
pendingMessages.remove(i);
for(int i = 0; i< unsentData.size(); i++){
unsentData.remove(i);
}

SharedPreferences prefs = getSharedPreferences("pendingMessages", Activity.MODE_PRIVATE);
Expand All @@ -705,7 +664,7 @@ public void onClick(DialogInterface dialog, int which) {
runOnUiThread(new Runnable() {
@Override
public void run() {
((TextView) findViewById(R.id.numberOfPendingMessagesLayout).findViewById(R.id.numberOfPendingMessages)).setText(pendingMessages.size() + "");
((TextView) findViewById(R.id.numberOfPendingMessagesLayout).findViewById(R.id.numberOfPendingMessages)).setText(unsentData.size() + "");
}
});
}
Expand Down
Loading

0 comments on commit 88c0578

Please sign in to comment.