FocusedAttentionGame2.java 20.9 KB
Newer Older
Anuththara18's avatar
Anuththara18 committed
1 2
package com.anuththara18.attentionassessment.focused;

3
import androidx.appcompat.app.AlertDialog;
Anuththara18's avatar
Anuththara18 committed
4 5
import androidx.appcompat.app.AppCompatActivity;

6 7 8 9 10 11
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.PorterDuff;
import android.media.MediaPlayer;
import android.os.AsyncTask;
Anuththara18's avatar
Anuththara18 committed
12 13
import android.os.Build;
import android.os.Bundle;
14 15
import android.util.Log;
import android.view.MotionEvent;
Anuththara18's avatar
Anuththara18 committed
16 17
import android.view.View;
import android.view.WindowManager;
18 19 20 21
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
Anuththara18's avatar
Anuththara18 committed
22 23

import com.anuththara18.attentionassessment.R;
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
import com.anuththara18.attentionassessment.age.AgeActivity;
import com.anuththara18.attentionassessment.db.Api;
import com.anuththara18.attentionassessment.db.RequestHandler;
import com.anuththara18.attentionassessment.gender.GenderActivity;
import com.anuththara18.attentionassessment.home.NavigationDrawerActivity;
import com.anuththara18.attentionassessment.language.LanguageSetter;
import com.anuththara18.attentionassessment.map.Map1Activity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
Anuththara18's avatar
Anuththara18 committed
39 40 41

public class FocusedAttentionGame2 extends AppCompatActivity {

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
    private static final int CODE_GET_REQUEST = 1024;
    private static final int CODE_POST_REQUEST = 1025;

    public static final String DATABASE_NAME = "focusedAttention";
    SQLiteDatabase mDatabase;

    Random random = new Random();
    int radomTimer = 0;
    int rand = 0;

    int i = 1;
    private long startTime, clickedTime = 0;
    long reactionTime;
    int count;

    int totalCorrectResponses = 0;
    int noOfCorrectResponses = 0;
    int noOfCommissionErrors = 0;
    int noOfOmmissionErrors = 0;
    long totalReactionTime = 0;
    int meanReactionTime = 0;
    int duration = 0;

    ImageView bear1, bear2, bear3, bear4, cross_btn;
    TextView textView;

    String clicked = "null";

    Integer[] isi = { 3000, 3500, 4000, 4500, 5000, 5500, 6000 };

    MediaPlayer mp, mp2;


Anuththara18's avatar
Anuththara18 committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        else {
            View decorView = getWindow().getDecorView();
            // Hide the status bar.
            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
        setContentView(R.layout.activity_focused_attention_game2);
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

        bear1 = (ImageView) findViewById(R.id.bear1);
        bear2 = (ImageView) findViewById(R.id.bear2);
        bear3 = (ImageView) findViewById(R.id.bear3);
        bear4 = (ImageView) findViewById(R.id.bear4);
        cross_btn = (ImageView) findViewById(R.id.cross_btn);
        textView = (TextView) findViewById(R.id.textView);

        textView.setText(LanguageSetter.getresources().getString(R.string.focg2));

        mp = MediaPlayer.create(getApplicationContext(), R.raw.focused);
        mp.start();

        long gameStart = System.currentTimeMillis();

        //creating a database
        mDatabase = openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null);

        getCount();

        new Runnable() {
            int updateInterval;

            @Override
            public void run() {

                if ( i <= count ) { // 41 times

                    if ( i % 2 != 0 ) {
                        bear1.setVisibility(View.INVISIBLE);
                        bear2.setVisibility(View.INVISIBLE);
                        bear3.setVisibility(View.INVISIBLE);
                        bear4.setVisibility(View.INVISIBLE);
                        clicked = "null";

                        bear1.setEnabled(true);
                        bear2.setEnabled(true);
                        bear3.setEnabled(true);
                        bear4.setEnabled(true);

                        radomTimer = random.nextInt(5);
                        updateInterval = isi[radomTimer];
                        bear1.postDelayed(this, updateInterval);
                        Log.d("***************radomTimer******************", String.valueOf(i + " " + updateInterval));
                        duration = duration + updateInterval;
                        i++;
                    }
                    // appearance - 1s * 20 times
                    else {
                        if ( Map1Activity.level == 1 ) {

                            if ( i == 2 || i == 14 || i == 26 || i == 38 ) {
                                bear1.setVisibility(View.VISIBLE);
                                bear2.setVisibility(View.INVISIBLE);
                                bear3.setVisibility(View.INVISIBLE);
                                bear4.setVisibility(View.INVISIBLE);
                            }
                            else if ( i == 4 || i == 16 || i == 28 || i == 40 ) {
147
                                bear3.setVisibility(View.VISIBLE);
148
                                bear1.setVisibility(View.INVISIBLE);
149
                                bear2.setVisibility(View.INVISIBLE);
150 151 152
                                bear4.setVisibility(View.INVISIBLE);
                            }
                            else if ( i == 6 || i == 18 || i == 30 ) {
153 154
                                bear2.setVisibility(View.VISIBLE);
                                bear3.setVisibility(View.INVISIBLE);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
                                bear1.setVisibility(View.INVISIBLE);
                                bear4.setVisibility(View.INVISIBLE);
                            }
                            else if ( i == 8 || i == 20 || i == 32 ) {
                                bear4.setVisibility(View.VISIBLE);
                                bear2.setVisibility(View.INVISIBLE);
                                bear3.setVisibility(View.INVISIBLE);
                                bear1.setVisibility(View.INVISIBLE);
                            }
                            else if ( i == 10 || i == 22 || i == 34 ) {
                                bear1.setVisibility(View.VISIBLE);
                                bear2.setVisibility(View.INVISIBLE);
                                bear3.setVisibility(View.INVISIBLE);
                                bear4.setVisibility(View.INVISIBLE);
                            }
                            else if ( i == 12 || i == 24 || i == 36 ) {
171
                                bear4.setVisibility(View.VISIBLE);
172 173
                                bear1.setVisibility(View.INVISIBLE);
                                bear2.setVisibility(View.INVISIBLE);
174
                                bear1.setVisibility(View.INVISIBLE);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
                            }

                        }

                        bear1.setEnabled(true);
                        bear2.setEnabled(true);
                        bear3.setEnabled(true);
                        bear4.setEnabled(true);
                        clicked = "monkey";
                        startTime = System.currentTimeMillis();
                        bear1.postDelayed(this, 3000);
                        totalCorrectResponses++;
                        duration = duration + 3000;
                        i++;
                    }

                }
                else {
                    long gameEnd = System.currentTimeMillis();
                    long seconds = (gameEnd - gameStart) / 1000;
                    if ( noOfCorrectResponses == 0 ) {
                        meanReactionTime = 0;
                    }
                    else {
                        meanReactionTime = (int) Math.ceil(totalReactionTime / noOfCorrectResponses); // ms
                    }
                    Log.d("****************************************************", "****************************************************");
                    Log.d("Game Time", String.valueOf(seconds));
                    Log.d("total", String.valueOf(totalCorrectResponses));
                    Log.d("correctResponses", String.valueOf(noOfCorrectResponses));
                    noOfOmmissionErrors = totalCorrectResponses - noOfCorrectResponses;
                    Log.d("omissionErrors", String.valueOf(totalCorrectResponses - noOfCorrectResponses));
                    Log.d("commissionErrors", String.valueOf(noOfCommissionErrors));
                    Log.d("meanReactionTime", String.valueOf(meanReactionTime));
                    Log.d("duration", String.valueOf(duration));
                    saveDataToOnlineDB();
                    createTable();
                    saveDataToLocalDB();
                    Intent intent = new Intent(getApplicationContext(), FACompleteScreen.class);
                    finish();
                    mp.pause();
                    startActivity(intent);
                }

            }
        }.run();

        bear1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mp2 = MediaPlayer.create(getApplicationContext(), R.raw.button_click);
                mp2.start();
                // Your action here on button click
                clickedTime = System.currentTimeMillis();
                reactionTime = (clickedTime - startTime);
                //if (clicked.equals("monkey")) {
                if ( i - 1 == 2 || i - 1 == 14 || i - 1 == 26 || i - 1 == 38 || i - 1 == 10 || i - 1 == 22 || i - 1 == 34) {
                    totalReactionTime = totalReactionTime + reactionTime;
                    Log.d("correct ", startTime + " " + clickedTime + " " + reactionTime);
                    noOfCorrectResponses++;
                    bear1.setEnabled(false);
                } else {
                    Log.d("wrong", startTime + " " + clickedTime + " " + reactionTime);
                    noOfCommissionErrors++;
                }
            }
        });

        bear2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mp2 = MediaPlayer.create(getApplicationContext(), R.raw.button_click);
                mp2.start();
                // Your action here on button click
                clickedTime = System.currentTimeMillis();
                reactionTime = (clickedTime - startTime);
                //if (clicked.equals("monkey")) {
252
                if ( i - 1 == 6 || i - 1 == 18 || i - 1 == 30 || i - 1 == 42 ) {
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
                    totalReactionTime = totalReactionTime + reactionTime;
                    Log.d("correct ", startTime + " " + clickedTime + " " + reactionTime);
                    noOfCorrectResponses++;
                    bear2.setEnabled(false);
                } else {
                    Log.d("wrong", startTime + " " + clickedTime + " " + reactionTime);
                    noOfCommissionErrors++;
                }
            }
        });

        bear3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mp2 = MediaPlayer.create(getApplicationContext(), R.raw.button_click);
                mp2.start();
                // Your action here on button click
                clickedTime = System.currentTimeMillis();
                reactionTime = (clickedTime - startTime);
273
                if ( i - 1 == 4 || i - 1 == 16 || i - 1 == 28 || i - 1 == 40 ) {
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
                    totalReactionTime = totalReactionTime + reactionTime;
                    Log.d("correct ", startTime + " " + clickedTime + " " + reactionTime);
                    noOfCorrectResponses++;
                    bear3.setEnabled(false);
                } else {
                    Log.d("wrong", startTime + " " + clickedTime + " " + reactionTime);
                    noOfCommissionErrors++;
                }

            }
        });

        bear4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mp2 = MediaPlayer.create(getApplicationContext(), R.raw.button_click);
                mp2.start();
                // Your action here on button click
                clickedTime = System.currentTimeMillis();
                reactionTime = (clickedTime - startTime);
295
                if ( i - 1 == 8 || i - 1 == 20 || i - 1 == 32 || i - 1 == 12 || i - 1 == 24 || i - 1 == 36 )  {
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
                    totalReactionTime = totalReactionTime + reactionTime;
                    Log.d("correct ", startTime + " " + clickedTime + " " + reactionTime);
                    noOfCorrectResponses++;
                    bear4.setEnabled(false);
                } else {
                    Log.d("wrong", startTime + " " + clickedTime + " " + reactionTime);
                    noOfCommissionErrors++;
                }

            }
        });

        cross_btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        cross_btn.getBackground().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
                        view.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                        alert();
                        mp.pause();
                    case MotionEvent.ACTION_CANCEL: {
                        cross_btn.getBackground().clearColorFilter();
                        view.invalidate();
                        break;
                    }
                }
                return true;
            }
        });

    }

    private void getCount() {

        if (AgeActivity.age == 4 ) {
            count = 17;
        }
        else if (AgeActivity.age == 5 ) {
            count = 21;
        }
        else if (AgeActivity.age == 6 ) {
            count = 25;
        }
        else {
            count= 29;
        }

    }

    /*************************************************************************************************/

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mp.pause();
    }

    /*************************************************************************************************/

    private void alert() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("Do you really want to quit the game?");
        alertDialogBuilder.setPositiveButton("yes",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        Intent intent = new Intent(getApplicationContext(), NavigationDrawerActivity.class);
                        finish();
                        startActivity(intent);
                        //Toast.makeText(FocusedAttentionGame1.this,"You clicked yes button",Toast.LENGTH_LONG).show();
                    }
                });

        alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //finish();
            }
        });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

    /*************************************************************************************************/

    private void saveDataToOnlineDB() {

        String child = String.valueOf(GenderActivity.gender) + String.valueOf(AgeActivity.age);
        int child_ID = Integer.parseInt(child);
        int total_correct_responses = totalCorrectResponses;
        int no_of_correct_responses = noOfCorrectResponses;
        int no_of_ommission_errors = noOfOmmissionErrors;
        int no_of_commission_errors = noOfCommissionErrors;
        int mean_reaction_time = (int) meanReactionTime;
        int total_duration = duration;

        HashMap<String, Integer> params = new HashMap<>();
        params.put("childID", child_ID);
        params.put("totalCorrectResponses", total_correct_responses);
        params.put("noOfCorrectResponses", no_of_correct_responses);
        params.put("noOfCommissionErrors", no_of_commission_errors);
        params.put("noOfOmmissionErrors", no_of_ommission_errors);
        params.put("meanReactionTime", mean_reaction_time);
        params.put("totalDuration", total_duration);

        //Calling the create hero API
        PerformNetworkRequest request = new PerformNetworkRequest(Api.URL_FOCUSED_ATTENTION, params, CODE_POST_REQUEST);
        request.execute();
Anuththara18's avatar
Anuththara18 committed
409
    }
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515

    /*************************************************************************************************/

    //inner class to perform network request extending an AsyncTask
    private class PerformNetworkRequest extends AsyncTask<Void, Void, String> {

        //the url where we need to send the request
        String url;

        //the parameters
        HashMap<String, Integer> params;

        //the request code to define whether it is a GET or POST
        int requestCode;

        //constructor to initialize values
        PerformNetworkRequest(String url, HashMap<String, Integer> params, int requestCode) {
            this.url = url;
            this.params = params;
            this.requestCode = requestCode;
        }

        //when the task started displaying a progressbar
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }


        //this method will give the response from the request
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                JSONObject object = new JSONObject(s);
                if (!object.getBoolean("error")) {
                    Toast.makeText(getApplicationContext(), object.getString("message"), Toast.LENGTH_SHORT).show();
                    //refreshing the herolist after every operation
                    //so we get an updated list
                    //we will create this method right now it is commented
                    //because we haven't created it yet
                    //refreshHeroList(object.getJSONArray("heroes"));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        //the network operation will be performed in background
        @Override
        protected String doInBackground(Void... voids) {
            RequestHandler requestHandler = new RequestHandler();

            if (requestCode == CODE_POST_REQUEST)
                return requestHandler.sendPostRequest(url, params);


            if (requestCode == CODE_GET_REQUEST)
                return requestHandler.sendGetRequest(url);

            return null;
        }
    }

    /*************************************************************************************************/

    private void createTable() {
        mDatabase.execSQL(
                "CREATE TABLE IF NOT EXISTS focusedAttention (\n" +
                        "    id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
                        "    childID int NOT NULL,\n" +
                        "    totalCorrectResponses int NOT NULL,\n" +
                        "    noOfCorrectResponses int NOT NULL,\n" +
                        "    noOfCommissionErrors int NOT NULL,\n" +
                        "    noOfOmmissionErrors int NOT NULL,\n" +
                        "    meanReactionTime int NOT NULL,\n" +
                        "    totalDuration int NOT NULL\n" +
                        ");"
        );
    }

    /*************************************************************************************************/

    private void saveDataToLocalDB() {

        String child = String.valueOf(GenderActivity.gender) + String.valueOf(AgeActivity.age);
        int child_ID = Integer.parseInt(child);
        int total_correct_responses = totalCorrectResponses;
        int no_of_correct_responses = noOfCorrectResponses;
        int no_of_ommission_errors = noOfOmmissionErrors;
        int no_of_commission_errors = noOfCommissionErrors;
        int mean_reaction_time = (int) meanReactionTime;
        int total_duration = duration;

        String insertSQL = "INSERT INTO focusedAttention \n" +
                "(childID, totalCorrectResponses, noOfCorrectResponses, noOfCommissionErrors, noOfOmmissionErrors, meanReactionTime, totalDuration)\n" +
                "VALUES \n" +
                "(?, ?, ?, ?, ?, ?, ?);";

        mDatabase.execSQL(insertSQL, new Integer[]{child_ID, total_correct_responses, no_of_correct_responses, no_of_commission_errors, no_of_ommission_errors, mean_reaction_time, total_duration});

        //Toast.makeText(this, "Data Added Successfully", Toast.LENGTH_SHORT).show();
    }

    /*************************************************************************************************/

Anuththara18's avatar
Anuththara18 committed
516
}