-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadControl.java
155 lines (132 loc) · 7.56 KB
/
ThreadControl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;
public class ThreadControl{
protected Thread currentSongCheckThread,expiredTokenRefresh, songProgressThread;
LyricsForSpotifyApplication parent;
protected boolean currentSongCheckExit = false;
protected boolean expiredTokenRefreshExit = false;
protected boolean songProgressThreadExit = false;
ThreadControl(LyricsForSpotifyApplication parent){
this.parent = parent;
}
public void startCurrentSongCheckThread() {
currentSongCheckThread = new Thread(new Runnable() {
@Override
public void run() {
//Will keep the thread alive until the current song is no longer playing
while(parent.isCurrentSongStillPlaying() && !currentSongCheckExit)
parent.isCurrentSongStillPlaying();
}
});
currentSongCheckThread.start();
}
public void expiredTokenRefreshStart() {
expiredTokenRefresh = new Thread(new Runnable() {
@Override
public void run() {
try {
while (!expiredTokenRefreshExit) {
TimeUnit.SECONDS.sleep(parent.userAuthorization.getExpires_in() - 10);
URL accessTokenRefreshURL = new URL("https://accounts.spotify.com/api/token?grant_type=refresh_token&refresh_token=" + parent.userAuthorization.getRefresh_token() + "&client_id=" + Configuration.getClientID() + "&client_secret="+ Configuration.getClientSecret());
HttpURLConnection connection = (HttpURLConnection) accessTokenRefreshURL.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("client_id", Configuration.getEncodedClientID());
connection.setRequestProperty("client_secret", Configuration.getEncodedClientSecret());
connection.setRequestProperty("Content-length", "0");
System.out.println(connection.getResponseCode());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String bufferedInput;
while ((bufferedInput = bufferedReader.readLine()) != null) {
String[] splitInput = bufferedInput.split("\"");
for (int i = 0; i < splitInput.length; i++) {
System.out.println(splitInput[i]);
switch (splitInput[i]) {
case ("access_token"):
parent.userAuthorization.setAccess_token(splitInput[i + 2]);
i += 2;
break;
case ("expires_in"):
String[] expiresSplit = splitInput[i + 1].split(":");
parent.userAuthorization.setExpires_in(Integer.parseInt(expiresSplit[1].split(",")[0]));
i++;
break;
}
}
}
System.out.println("Access Token: " + parent.userAuthorization.getAccess_token());
System.out.println("Expires in: " + parent.userAuthorization.getExpires_in());
}
} catch (InterruptedException | IOException e) {
e.printStackTrace();
parent.runStepBasedOnStatus("New Application");
}
}
});
expiredTokenRefresh.start();
}
protected void songProgressThreadStart() {
songProgressThread = new Thread(new Runnable() {
@Override
public void run(){
try {
while (songProgressThread != null || songProgressThread.isInterrupted() || !songProgressThread.isAlive() && !songProgressThreadExit){
TimeUnit.MILLISECONDS.sleep(100);
URL playerURL = new URL("https://api.spotify.com/v1/me/player/currently-playing");
HttpURLConnection connection = (HttpURLConnection) playerURL.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Authorization", "Bearer " + parent.userAuthorization.getAccess_token());
System.out.println("Progress response Code: " + connection.getResponseCode());
if (connection.getResponseCode() == 200){
String input;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((input = bufferedReader.readLine()) != null){
String[] inputSplit = input.split(":|,");
switch (inputSplit[0].replace(" ","")) {
case "\"progress_ms\"":
System.out.println("Current progress: "+inputSplit[1]);
parent.currentSongProgressMS = Double.parseDouble(inputSplit[1].replace("\"","").replace(" ",""));
break;
case "\"duration_ms\"":
System.out.println("Current duration: "+inputSplit[1]);
parent.currentSongDuration = Double.parseDouble(inputSplit[1].replace("\"","").replace(" ",""));
break;
}
}
parent.scrollPane.setVvalue((parent.currentSongProgressMS / parent.currentSongDuration)*100);
System.out.println(parent.scrollPane.getVvalue());
}else {
System.out.println("Error during song progress check: Error Code " + connection.getResponseCode());
}
}
} catch (Exception e){
songProgressThread.interrupt();
System.out.println(e.getMessage());
songProgressThreadStart();
}
}
});
songProgressThread.start();
}
public void resetThreads() {
currentSongCheckExit = true;
expiredTokenRefreshExit = true;
songProgressThreadExit = true;
if (currentSongCheckThread != null && !currentSongCheckThread.isInterrupted()) {
currentSongCheckThread.interrupt();
}
if (expiredTokenRefresh != null && !expiredTokenRefresh.isInterrupted()) {
expiredTokenRefresh.interrupt();
}
if (songProgressThread != null && !songProgressThread.isInterrupted()) {
songProgressThread.interrupt();
}
currentSongCheckExit = false;
expiredTokenRefreshExit = false;
songProgressThreadExit = false;
}
}