66import android .content .Context ;
77import android .content .SharedPreferences ;
88import android .util .Base64 ;
9- import android .widget .RelativeLayout ;
109import android .widget .TextView ;
1110import android .widget .Toast ;
1211
1615import java .nio .charset .Charset ;
1716import java .util .ArrayList ;
1817import java .util .Arrays ;
18+ import java .util .Calendar ;
1919
2020/**
2121 * Created by Ajay on 02/10/2017.
2525
2626public 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 }
0 commit comments