Commit f9a90e91 authored by fpeterfalvi's avatar fpeterfalvi
Browse files

GameActivity created in server.

Open and read file containing guess questions and display them in a new activity.
parent a0033ca4
......@@ -26,4 +26,5 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.github.rustamg:file-dialogs:1.0'
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="onlab.kvizserver">
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_WIFI_STATE"/>
android:name="android.permission.ACCESS_WIFI_STATE"
android:required="true" />
<uses-permission
android:required="true"
android:name="android.permission.CHANGE_WIFI_STATE"/>
android:name="android.permission.CHANGE_WIFI_STATE"
android:required="true" />
<uses-permission
android:required="true"
android:name="android.permission.INTERNET"/>
android:name="android.permission.INTERNET"
android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
>
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
......@@ -30,10 +32,9 @@
<activity
android:name=".LobbyActivity"
android:label="@string/title_activity_lobby"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="landscape"></activity>
android:screenOrientation="landscape"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".GameActivity"></activity>
</application>
</manifest>
\ No newline at end of file
package onlab.kvizserver;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import onlab.kvizserver.model.GuessQuestion;
public class GameActivity extends AppCompatActivity {
List<GuessQuestion> questions = new ArrayList<>();
int questionIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Bundle extras = getIntent().getExtras();
String questionFileName = extras.getString("QUESTION_FILE_NAME");
BufferedReader buffreader;
try {
buffreader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(questionFileName))));
String line;
while ((line = buffreader.readLine()) != null) {
String[] parts = line.split("\t");
GuessQuestion guessQuestion = new GuessQuestion(parts[0], Integer.parseInt(parts[1]));
questions.add(guessQuestion);
}
} catch (IOException e) {
e.printStackTrace();
}
Button nextQuestionBtn = (Button) findViewById(R.id.NextQuestion);
final TextView questionText = (TextView) findViewById(R.id.QuestionText);
nextQuestionBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (questionIndex < questions.size()) {
questionText.setText(questions.get(questionIndex).getQuestionText());
questionIndex++;
} else {
questionText.setText("No more questions.");
}
}
});
}
}
......@@ -53,6 +53,8 @@ public class LobbyActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ClientListAdapter adapter;
private String questionFileName;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -71,6 +73,7 @@ public class LobbyActivity extends AppCompatActivity {
HOSTNAME = "";
} else {
HOSTNAME = extras.getString("STRING_HOSTNAME");
questionFileName = extras.getString("QUESTION_FILE_NAME");
}
} else {
HOSTNAME = (String) savedInstanceState.getSerializable("STRING_HOSTNAME");
......@@ -82,7 +85,16 @@ public class LobbyActivity extends AppCompatActivity {
this.serverThread.start();
updateConversationHandler = new Handler();
Button startGameBtn = (Button) findViewById(R.id.StartGame);
startGameBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
serverThread.interrupt();
Intent intent = new Intent(getApplicationContext(), GameActivity.class);
intent.putExtra("QUESTION_FILE_NAME", questionFileName);
startActivity(intent);
}
});
}
@Override
......
package onlab.kvizserver;
import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
import com.rustamg.filedialogs.FileDialog;
import com.rustamg.filedialogs.OpenFileDialog;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements FileDialog.OnFileSelectedListener {
private String fileCanonicalPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -17,17 +29,36 @@ public class MainActivity extends AppCompatActivity {
Button StartLobbyBtn = (Button) findViewById(R.id.StartLobby);
final EditText txtName = (EditText) findViewById(R.id.txtName);
Button ChooseQuestionFileBtn = (Button) findViewById(R.id.ChooseQuestionFile);
ChooseQuestionFileBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FileDialog dialog = new OpenFileDialog();
dialog.show(getSupportFragmentManager(), OpenFileDialog.class.getName());
}
});
StartLobbyBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), LobbyActivity.class);
String hname = txtName.getText().toString();
intent.putExtra("STRING_HOSTNAME", hname);
intent.putExtra("QUESTION_FILE_NAME", fileCanonicalPath);
startActivity(intent);
}
});
}
@Override
public void onFileSelected(FileDialog dialog, File file) {
try {
fileCanonicalPath = file.getCanonicalPath();
Log.d("MainActivity", fileCanonicalPath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package onlab.kvizserver.model;
public class GuessQuestion {
private String questionText;
private int correctAnswer;
public GuessQuestion(String questionText, int correctAnswer) {
this.questionText = questionText;
this.correctAnswer = correctAnswer;
}
public String getQuestionText() {
return questionText;
}
public int getCorrectAnswer() {
return correctAnswer;
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="onlab.kvizserver.GameActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The place of the question."
android:id="@+id/QuestionText"
android:textSize="30sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Question"
android:id="@+id/NextQuestion"
android:textSize="40sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
......@@ -11,16 +11,29 @@
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Waiting..."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.15"
android:textSize="56sp"/>
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Waiting..."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.15"
android:textSize="56sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start game"
android:id="@+id/StartGame"/>
</LinearLayout>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
......
......@@ -26,7 +26,18 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.301" />
app:layout_constraintVertical_bias="0.231" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose question file"
android:id="@+id/ChooseQuestionFile"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.433" />
<Button
android:layout_width="wrap_content"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment