Fixed repository

This commit is contained in:
Caesar2011
2017-04-06 10:03:31 +02:00
parent 4d2a472686
commit cff541d58e
5 changed files with 270 additions and 4 deletions

View File

@@ -63,10 +63,6 @@ public class QuestionEndFragment extends Fragment {
@Override @Override
public void onGameEndEvent(GameScores scores) { public void onGameEndEvent(GameScores scores) {
Log.d(NAME, "onGameEndEvent" + scores); Log.d(NAME, "onGameEndEvent" + scores);
//scores = new GameScores(100, 43*1000);
//scores = new GameScores(100, 7*60*1000 + 43*1000);
//scores = new GameScores(100, 3*60*60*1000 + 7*60*1000 + 43*1000);
//scores = new GameScores(100, 15*24*60*60*1000 + 3*60*60*1000 + 7*60*1000 + 43*1000);
if (scores != null) { if (scores != null) {
TextView textView = (TextView) view.findViewById(R.id.textViewScore); TextView textView = (TextView) view.findViewById(R.id.textViewScore);
textView.setText(String.format(Locale.getDefault(), "%1$d", scores.getScore())); textView.setText(String.format(Locale.getDefault(), "%1$d", scores.getScore()));

View File

@@ -0,0 +1,25 @@
package de.hwr_berlin.it14.postgrachelor.Types;
/**
* Created by Sebastian on 06.04.2017.
* Timing results on each tick, when game is running
*/
public class Timings {
private final long timeDiff;
private final int score;
public Timings(long timeDiff, int score) {
this.timeDiff = timeDiff;
this.score = score;
}
public long getTimeDiff() {
return timeDiff;
}
public int getScore() {
return score;
}
}

View File

@@ -0,0 +1,36 @@
package de.hwr_berlin.it14.postgrachelor.Utils;
import java.util.Locale;
/**
* Created by Sebastian on 06.04.2017.
* Convert between data types
*/
public class Conversion {
public static String millisToTime(long millis) {
int seconds = (int) (millis/1000) % 60;
int minutes = (int) (millis/1000/60) % 60;
int hours = (int) (millis/1000/60/60) % 24;
int days = (int) millis/1000/60/60/24;
String result = intToStr(minutes, 2, '0')+":"+intToStr(seconds, 2, '0');
if (hours > 0)
result = intToStr(hours)+":"+result;
if (days > 0)
result = intToStr(days)+" days "+result;
return result;
}
public static String intToStr(int i) {
return String.format(Locale.getDefault(), "%1d", i);
}
private static String intToStr(int i, int padding) {
return String.format(Locale.getDefault(), "%"+Conversion.intToStr(padding)+"s", i);
}
private static String intToStr(int i, int padding, char padChar) {
return Conversion.intToStr(i, padding).replace(' ', padChar);
}
}

View File

@@ -0,0 +1,141 @@
package de.hwr_berlin.it14.postgrachelor.Utils;
import android.app.Activity;
import android.app.ProgressDialog;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Scanner;
import java.util.StringTokenizer;
import static android.content.ContentValues.TAG;
abstract class JsonRequest extends AsyncTask<Void, Void, JSONObject> {
private final String connectionURL;
protected final String path;
private final HashMap<String, String> params;
final Activity activity;
private ProgressDialog pDialog;
JsonRequest(String path, HashMap<String, String> params, Activity activity) {
this.connectionURL = getConnectionURL();
this.path = path;
this.params = params;
this.activity = activity;
Log.d("fragment, JSONRequest", connectionURL+path);
}
protected abstract String getConnectionURL();
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d("fragment, JSONRequest", "onPre1");
// Showing progress dialog
pDialog = new ProgressDialog(this.activity);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected JSONObject doInBackground(Void... arg0) {
URL url = null;
String jsonText = "";
JSONObject jsonObj = null;
try {
url = new URL(parseURL(this.connectionURL+this.path, this.params));
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection urlConnection = null;
if (url == null)
return new JSONObject();
try {
urlConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
if (urlConnection == null)
return new JSONObject();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
Scanner s = new Scanner(in).useDelimiter("\\A");
jsonText = s.hasNext() ? s.next() : "";
} catch (IOException e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
if (!jsonText.equals("")) {
jsonText = jsonText.substring(jsonText.indexOf("{"), jsonText.lastIndexOf("}")+1);
try {
jsonObj = new JSONObject(jsonText);
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity.getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity.getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return jsonObj;
}
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
}
private String parseURL(String url, Map<String, String> params)
{
Uri.Builder builder = Uri.parse(url).buildUpon();
if (params != null) {
for (String key : params.keySet()) {
builder.appendQueryParameter(key, params.get(key));
}
}
return builder.build().toString();
}
}

View File

@@ -0,0 +1,68 @@
package de.hwr_berlin.it14.postgrachelor.Utils;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import org.json.JSONObject;
import java.util.HashMap;
/**
* Created by sebastian on 19.03.17.
* Implementation of JsonRequest
*/
public class JsonRequestPG extends JsonRequest {
private final AsyncResponse delegate;
public interface AsyncResponse {
void processFinish(JSONObject data);
void processError(int status, String message);
}
public JsonRequestPG(String path, HashMap<String, String> params, Activity activity, AsyncResponse delegate) {
super(path, params, activity);
this.delegate = delegate;
}
@Override
protected String getConnectionURL() {
return "http://leander.sebse.de/";
}
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
if (result == null) {
delegate.processError(-1, "Internal error occurred!");
return;
}
JSONObject meta = result.optJSONObject("meta");
if (meta==null) {
delegate.processError(-2, "Invalid JSON: Meta tag not found!");
return;
}
int status = meta.optInt("status", -3);
String message = meta.optString("message", "");
if (status != 0) {
new AlertDialog.Builder(this.activity)
.setTitle("Error "+status)
.setMessage(message)
.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
delegate.processError(status, message);
return;
}
JSONObject data = result.optJSONObject("data");
delegate.processFinish(data);
}
}