added polling thread at join queue and disabling of login button

This commit is contained in:
aj 2020-02-02 09:31:31 +00:00
parent 6385aac6b2
commit 3625bd22ca
2 changed files with 61 additions and 14 deletions

View File

@ -17,6 +17,8 @@ import java.util.concurrent.ExecutionException;
public class JoinQueueActivity extends AppCompatActivity {
Thread pollingThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -27,6 +29,10 @@ public class JoinQueueActivity extends AppCompatActivity {
@Override
public void onClick(View v) {
if(pollingThread.isAlive()){
pollingThread.interrupt();
}
SharedPreferences preferences = getApplicationContext().getSharedPreferences("scheduler_preferences", Context.MODE_PRIVATE);
QueueTask task = new QueueTask("location1", preferences.getString("api_key", null));
@ -49,21 +55,59 @@ public class JoinQueueActivity extends AppCompatActivity {
}
});
SharedPreferences preferences = getApplicationContext().getSharedPreferences("scheduler_preferences", Context.MODE_PRIVATE);
GetUserTask task = new GetUserTask(preferences.getString("api_key", null));
final SharedPreferences preferences = getApplicationContext().getSharedPreferences("scheduler_preferences", Context.MODE_PRIVATE);
final long timeInterval = 2000;
Runnable runnable = new Runnable() {
task.execute();
try {
User user = task.get();
public void run() {
boolean changed = false;
while (!changed) {
if(user.getState() != User.State.inactive){
joinButton.setEnabled(false);
GetUserTask task = new GetUserTask(preferences.getString("api_key", null));
task.execute();
try {
User user = task.get();
Intent intent;
switch(user.getState()){
case in_queue:
intent = new Intent(JoinQueueActivity.this, WaitingActivity.class);
startActivity(intent);
changed = true;
break;
case assigned:
intent = new Intent(JoinQueueActivity.this, PluginWait_Activity.class);
startActivity(intent);
changed = true;
break;
case connected_charging:
intent = new Intent(JoinQueueActivity.this, ChargingActivity.class);
startActivity(intent);
changed = true;
break;
case connected_full:
intent = new Intent(JoinQueueActivity.this, FullActivity.class);
startActivity(intent);
changed = true;
break;
}
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
};
pollingThread = new Thread(runnable);
pollingThread.start();
}
}

View File

@ -32,6 +32,7 @@ import java.util.concurrent.ExecutionException;
public class Login2Activity extends AppCompatActivity {
ProgressBar loading;
Button signInButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -55,9 +56,9 @@ public class Login2Activity extends AppCompatActivity {
}
final Button signInButton = findViewById(R.id.login);
final EditText usernameEditText = findViewById(R.id.username);
final EditText passwordEditText = findViewById(R.id.password);
signInButton = findViewById(R.id.login);
loading = findViewById(R.id.loading);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
@ -92,8 +93,9 @@ public class Login2Activity extends AppCompatActivity {
private void tryLogin(String username, String password) {
NativeLoginTask task = new NativeLoginTask(username, password);
signInButton.setEnabled(false);
NativeLoginTask task = new NativeLoginTask(username, password);
task.execute();
try {
@ -101,6 +103,7 @@ public class Login2Activity extends AppCompatActivity {
if (result == null) {
signInButton.setEnabled(true);
Toast.makeText(Login2Activity.this, "error", Toast.LENGTH_SHORT).show();
}else{