AssessmentActivity.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. package com.cygnusa.hyperm;
  2. import android.annotation.SuppressLint;
  3. import android.content.ContentValues;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.database.Cursor;
  7. import android.graphics.Color;
  8. import android.graphics.PorterDuff;
  9. import android.graphics.drawable.Drawable;
  10. import android.os.AsyncTask;
  11. import android.os.Bundle;
  12. import android.os.Handler;
  13. import android.util.Log;
  14. import android.view.LayoutInflater;
  15. import android.view.MenuItem;
  16. import android.view.View;
  17. import android.view.animation.AlphaAnimation;
  18. import android.view.animation.Animation;
  19. import android.widget.Button;
  20. import android.widget.ImageButton;
  21. import android.widget.TextView;
  22. import android.widget.Toast;
  23. import androidx.appcompat.app.AlertDialog;
  24. import androidx.appcompat.app.AppCompatActivity;
  25. import androidx.appcompat.widget.Toolbar;
  26. import androidx.fragment.app.Fragment;
  27. import androidx.fragment.app.FragmentManager;
  28. import androidx.fragment.app.FragmentStatePagerAdapter;
  29. import androidx.viewpager.widget.ViewPager;
  30. import com.google.firebase.analytics.FirebaseAnalytics;
  31. import com.google.gson.Gson;
  32. import com.google.gson.JsonElement;
  33. import com.google.gson.reflect.TypeToken;
  34. import com.ogaclejapan.smarttablayout.SmartTabLayout;
  35. import com.cygnusa.hyperm.fancyshowcase.FancyShowCaseView;
  36. import com.cygnusa.hyperm.fancyshowcase.FocusShape;
  37. import com.cygnusa.hyperm.fragments.QuestionFragment;
  38. import com.cygnusa.hyperm.roundcornerprogressbar.RoundCornerProgressBar;
  39. import com.cygnusa.hyperm.util.ApiService;
  40. import com.cygnusa.hyperm.util.CustomViewPager;
  41. import com.cygnusa.hyperm.util.DepthPageTransformerNew;
  42. import com.cygnusa.hyperm.util.GlobalData;
  43. import com.cygnusa.hyperm.util.MainApplication;
  44. import com.cygnusa.hyperm.util.TransparentProgressDialog;
  45. import com.cygnusa.hyperm.util.UtilService;
  46. import com.cygnusa.hyperm.wrappers.AnswerResponse;
  47. import com.cygnusa.hyperm.wrappers.AssessmentAnswer;
  48. import com.cygnusa.hyperm.wrappers.AssessmentList;
  49. import com.cygnusa.hyperm.wrappers.AssessmentQuestion;
  50. import com.cygnusa.hyperm.wrappers.AssessmentQuestionResponse;
  51. import com.cygnusa.hyperm.wrappers.AssessmentScore;
  52. import com.cygnusa.hyperm.wrappers.CommonResponse;
  53. import com.cygnusa.hyperm.wrappers.ErrorResponse;
  54. import com.cygnusa.hyperm.wrappers.HitCountModel;
  55. import java.text.SimpleDateFormat;
  56. import java.util.ArrayList;
  57. import java.util.Calendar;
  58. import java.util.Date;
  59. import java.util.HashMap;
  60. import java.util.concurrent.TimeUnit;
  61. import retrofit2.Call;
  62. import retrofit2.Callback;
  63. import retrofit2.Response;
  64. public class AssessmentActivity extends AppCompatActivity {
  65. public static AssessmentList ASSESSMENT = new AssessmentList();
  66. public static String TRAINING_ID = "";
  67. public static String CATEGORY_ID = "";
  68. public static boolean isOffline = false;
  69. ApiService restService;
  70. TransparentProgressDialog progressDialog;
  71. TextView title;
  72. ArrayList<AssessmentQuestion> ASSESSMENT_QUESTIONS = new ArrayList<>();
  73. CustomViewPager viewPager;
  74. SmartTabLayout indicatorTab;
  75. ImageButton previous, next;
  76. RoundCornerProgressBar linearTimerView;
  77. TextView time;
  78. Button submitButton;
  79. String START_TIME = "";
  80. String END_TIME = "";
  81. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  82. AlertDialog.Builder alertDialogBuilder;
  83. AlertDialog alertDialog;
  84. Bundle bundle;
  85. Handler handler = new Handler();
  86. private boolean isBlinking = false;
  87. String fromTime, toTime, finalTime;
  88. Calendar c;
  89. SimpleDateFormat df;
  90. Runnable runnable = new Runnable() {
  91. @Override
  92. public void run() {
  93. if (linearTimerView.getProgress() == 0) {
  94. onSubmitAnswer(true);
  95. time.setText("TIME LEFT");
  96. } else {
  97. linearTimerView.setProgress(linearTimerView.getProgress() - 1);
  98. long millis = (long) (linearTimerView.getProgress() * 1000);
  99. String formattedTime = String.format("%02d:%02d",
  100. TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1),
  101. TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1));
  102. time.setText(formattedTime);
  103. updateProgressColor();
  104. handler.postDelayed(runnable, 1000);
  105. }
  106. }
  107. };
  108. private FirebaseAnalytics firebaseAnalytics;
  109. @Override
  110. protected void onCreate(Bundle savedInstanceState) {
  111. super.onCreate(savedInstanceState);
  112. setContentView(R.layout.activity_assessment);
  113. setupWindowAnimations();
  114. restService = ((MainApplication) getApplication()).getClient();
  115. viewPager = findViewById(R.id.viewPager);
  116. title = findViewById(R.id.title);
  117. indicatorTab = findViewById(R.id.indicatorTab);
  118. submitButton = findViewById(R.id.submitButton);
  119. submitButton.setVisibility(View.GONE);
  120. linearTimerView = findViewById(R.id.linearTimerView);
  121. previous = findViewById(R.id.previous);
  122. next = findViewById(R.id.next);
  123. time = findViewById(R.id.time);
  124. time.setVisibility(View.VISIBLE);
  125. linearTimerView.setVisibility(View.GONE);
  126. firebaseAnalytics = FirebaseAnalytics.getInstance(this);
  127. FirebaseAnalytics.getInstance(this).setAnalyticsCollectionEnabled(true);
  128. bundle = new Bundle();
  129. bundle.putString(FirebaseAnalytics.Param.ITEM_ID, "1");
  130. bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, "Assessment Activity");
  131. firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
  132. Toolbar toolbar = findViewById(R.id.toolbar);
  133. setSupportActionBar(toolbar);
  134. toolbar.setContentInsetStartWithNavigation(0);
  135. toolbar.setTitle("");
  136. getSupportActionBar().setTitle("");
  137. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  138. getSupportActionBar().setDisplayShowHomeEnabled(true);
  139. @SuppressLint("PrivateResource")
  140. Drawable backArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_material);
  141. backArrow.setColorFilter(getResources().getColor(R.color.btn_bg_color), PorterDuff.Mode.SRC_ATOP);
  142. getSupportActionBar().setHomeAsUpIndicator(backArrow);
  143. title.setText(ASSESSMENT.getAssessmentname() + "");
  144. title.setSelected(true);
  145. if (ASSESSMENT.getAssessmenttimetype().trim().equalsIgnoreCase("0")) {
  146. time.setVisibility(View.INVISIBLE);
  147. linearTimerView.setVisibility(View.INVISIBLE);
  148. }
  149. viewPager.post(new Runnable() {
  150. @Override
  151. public void run() {
  152. getAssesmentQuistion();
  153. }
  154. });
  155. previous.setEnabled(false);
  156. next.setEnabled(false);
  157. viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  158. @Override
  159. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  160. }
  161. @Override
  162. public void onPageSelected(int position) {
  163. if (position == 0) {
  164. previous.setEnabled(false);
  165. next.setEnabled(true);
  166. if (ASSESSMENT_QUESTIONS.size() == 1) {
  167. submitButton.setVisibility(View.VISIBLE);
  168. }
  169. } else if (position == ASSESSMENT_QUESTIONS.size() - 1) {
  170. previous.setEnabled(true);
  171. next.setEnabled(false);
  172. submitButton.setVisibility(View.VISIBLE);
  173. } else {
  174. previous.setEnabled(true);
  175. next.setEnabled(true);
  176. }
  177. }
  178. @Override
  179. public void onPageScrollStateChanged(int state) {
  180. }
  181. });
  182. previous.setOnClickListener(new View.OnClickListener() {
  183. @Override
  184. public void onClick(View v) {
  185. if (viewPager.getCurrentItem() > 0) {
  186. viewPager.setCurrentItem(viewPager.getCurrentItem() - 1, true);
  187. }
  188. }
  189. });
  190. next.setOnClickListener(new View.OnClickListener() {
  191. @Override
  192. public void onClick(View v) {
  193. if (viewPager.getCurrentItem() < ASSESSMENT_QUESTIONS.size() - 1) {
  194. viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
  195. }
  196. }
  197. });
  198. submitButton.setOnClickListener(new View.OnClickListener() {
  199. @Override
  200. public void onClick(View v) {
  201. getToTime();
  202. onSubmitAnswer(false);
  203. }
  204. });
  205. findViewById(R.id.helpIcon).setOnClickListener(new View.OnClickListener() {
  206. @Override
  207. public void onClick(View v) {
  208. showHelpScreen();
  209. }
  210. });
  211. getCurrentTime();
  212. }
  213. private void getCurrentTime() {
  214. c = Calendar.getInstance();
  215. System.out.println("fromTime " + c.getTime());
  216. df = new SimpleDateFormat("HH:mm:ss");
  217. fromTime = df.format(c.getTime());
  218. Log.e("fromTime", fromTime);
  219. }
  220. private void sendHitCountToServer() {
  221. restService.sendAllHitCount(buildConfigData()).enqueue(new Callback<CommonResponse>() {
  222. @Override
  223. public void onResponse(Call<CommonResponse> call, Response<CommonResponse> response) {
  224. if (response.body() != null) {
  225. } else {
  226. }
  227. }
  228. @Override
  229. public void onFailure(Call<CommonResponse> call, Throwable t) {
  230. if (t.getMessage() != null) {
  231. Log.e("responeHit", t.getMessage());
  232. }
  233. }
  234. });
  235. }
  236. private HitCountModel buildConfigData() {
  237. HitCountModel hitCountModel = new HitCountModel();
  238. hitCountModel.setPage("Assessment");
  239. hitCountModel.setViewtime("0");
  240. return hitCountModel;
  241. }
  242. private void onSubmitAnswer(boolean isAutoSubmit) {
  243. if (ASSESSMENT_QUESTIONS != null && ASSESSMENT_QUESTIONS.size() > 0) {
  244. ArrayList<AssessmentAnswer> answers = new ArrayList<>();
  245. END_TIME = format.format(new Date());
  246. for (AssessmentQuestion question : ASSESSMENT_QUESTIONS) {
  247. AssessmentAnswer answer = new AssessmentAnswer();
  248. answer.setAqid(question.getAqid());
  249. answer.setQid(question.getQid());
  250. if (isAutoSubmit) {
  251. answer.setOptions(question.getAnswer());
  252. } else {
  253. if (question.getIs_mandatory().trim().equalsIgnoreCase("1")) {
  254. if (!question.isAnswered()) {
  255. viewPager.setCurrentItem(ASSESSMENT_QUESTIONS.indexOf(question));
  256. GlobalData.showToast(AssessmentActivity.this, "Please answer all mandatory questions", Toast.LENGTH_SHORT).show();
  257. return;
  258. } else {
  259. answer.setOptions(question.getAnswer());
  260. }
  261. } else {
  262. answer.setOptions(question.getAnswer());
  263. }
  264. }
  265. answers.add(answer);
  266. }
  267. Gson gson = new Gson();
  268. JsonElement jsonElement = gson.toJsonTree(
  269. answers, new TypeToken<ArrayList<AssessmentAnswer>>() {
  270. }.getType());
  271. HashMap<String, String> map = new HashMap<>();
  272. map.put("ans_arr", jsonElement.toString());
  273. map.put("startdatetime", START_TIME);
  274. map.put("enddatetime", END_TIME);
  275. if (new UtilService().isNetworkAvailable(AssessmentActivity.this)) {
  276. progressDialog = new TransparentProgressDialog(AssessmentActivity.this);
  277. progressDialog.show();
  278. restService.postAssessment(ASSESSMENT.getAssessmentid(), map).enqueue(new Callback<AnswerResponse>() {
  279. @Override
  280. public void onResponse(Call<AnswerResponse> call, Response<AnswerResponse> response) {
  281. if (progressDialog != null && progressDialog.isShowing()) {
  282. progressDialog.dismiss();
  283. }
  284. if (response.body() != null && response.body().getStatus() == 200) {
  285. try {
  286. int d = (int) Math.round(Double.parseDouble(response.body().getData().getPercentage_score()));
  287. showPopup(response.body().getData(), String.valueOf(d), response.body().getAssessment_msg());
  288. GlobalData.showToast(getBaseContext(), response.body().getAssessment_msg(), Toast.LENGTH_LONG).show();
  289. } catch (Exception e) {
  290. e.printStackTrace();
  291. }
  292. } else if (response.body() != null && response.body().getStatus() == 401) {
  293. GlobalData.canLogout = true;
  294. try {
  295. handler.removeCallbacksAndMessages(null);
  296. } catch (Exception e) {
  297. e.printStackTrace();
  298. }
  299. finish();
  300. } else if (response.body() != null) {
  301. GlobalData.showToast(getBaseContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
  302. } else if (response.errorBody() != null) {
  303. Gson gson = new Gson();
  304. ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
  305. GlobalData.showToast(getBaseContext(), message.getMessage(), Toast.LENGTH_SHORT).show();
  306. if (message.getStatus() == 401) {
  307. GlobalData.canLogout = true;
  308. try {
  309. handler.removeCallbacksAndMessages(null);
  310. } catch (Exception e) {
  311. e.printStackTrace();
  312. }
  313. finish();
  314. }
  315. }
  316. }
  317. @Override
  318. public void onFailure(Call<AnswerResponse> call, Throwable t) {
  319. if (progressDialog != null && progressDialog.isShowing()) {
  320. progressDialog.dismiss();
  321. }
  322. GlobalData.showToast(getBaseContext(), "Connection Failure, try again!", Toast.LENGTH_SHORT).show();
  323. }
  324. });
  325. } else {
  326. Cursor cursor = GlobalData.sdb.rawQuery("select * from assessment_question where category_id='"
  327. + CATEGORY_ID + "' and training_id='" + TRAINING_ID + "'", null);
  328. if (isOffline || cursor.getCount() > 0) {
  329. GlobalData.sdb.execSQL("delete from assessment_answer where assessment_id='"
  330. + ASSESSMENT.getAssessmentid()
  331. + "' and category_id='" + CATEGORY_ID + "' and training_id='" + TRAINING_ID + "'");
  332. JsonElement jsonElement1 = gson.toJsonTree(
  333. map, new TypeToken<HashMap<String, String>>() {
  334. }.getType());
  335. ContentValues values = new ContentValues();
  336. values.put("assessment_answer", jsonElement1.toString());
  337. values.put("training_id", TRAINING_ID);
  338. values.put("category_id", CATEGORY_ID);
  339. values.put("assessment_id", ASSESSMENT.getAssessmentid());
  340. GlobalData.sdb.insert("assessment_answer", null, values);
  341. GlobalData.showToast(getBaseContext(), "Assessment saved on local, will sync to server when online", Toast.LENGTH_LONG).show();
  342. try {
  343. handler.removeCallbacksAndMessages(null);
  344. } catch (Exception e) {
  345. e.printStackTrace();
  346. }
  347. finish();
  348. AssessmentActivity.this.overridePendingTransition(R.anim.animation_f_enter,
  349. R.anim.animation_f_leave);
  350. } else {
  351. GlobalData.showToast(getBaseContext(), "Please check your internet connection", Toast.LENGTH_SHORT).show();
  352. }
  353. }
  354. }
  355. }
  356. private void showPopup(final AssessmentScore assessmentScore, String s, String msg) {
  357. View view = LayoutInflater.from(AssessmentActivity.this).inflate(
  358. R.layout.assessment_popup, null);
  359. view.findViewById(R.id.continueButton).setOnClickListener(
  360. new View.OnClickListener() {
  361. @Override
  362. public void onClick(View v) {
  363. alertDialog.dismiss();
  364. try {
  365. handler.removeCallbacksAndMessages(null);
  366. } catch (Exception e) {
  367. e.printStackTrace();
  368. }
  369. finish();
  370. AssessmentActivity.this.overridePendingTransition(R.anim.animation_f_enter,
  371. R.anim.animation_f_leave);
  372. }
  373. });
  374. view.findViewById(R.id.reviewButton).setOnClickListener(
  375. new View.OnClickListener() {
  376. @Override
  377. public void onClick(View v) {
  378. alertDialog.dismiss();
  379. ReviewActivity.ASSESSMENT = ASSESSMENT;
  380. ReviewActivity.ASSESSMENT_SCORE = assessmentScore;
  381. startActivity(new Intent(AssessmentActivity.this, ReviewActivity.class));
  382. try {
  383. handler.removeCallbacksAndMessages(null);
  384. } catch (Exception e) {
  385. e.printStackTrace();
  386. }
  387. finish();
  388. }
  389. });
  390. TextView linkView = view.findViewById(R.id.score);
  391. TextView assesmentName = view.findViewById(R.id.assesmentName);
  392. if (Integer.parseInt(s) < 80) {
  393. linkView.setTextColor(getColor(R.color.red));
  394. } else {
  395. linkView.setTextColor(getColor(R.color.green));
  396. }
  397. linkView.setText(s);
  398. TextView assesmentMessage = view.findViewById(R.id.assesmentMessage);
  399. assesmentMessage.setText(msg);
  400. assesmentName.setText("For " + ASSESSMENT.getAssessmentname());
  401. alertDialogBuilder = new AlertDialog.Builder(AssessmentActivity.this);
  402. alertDialogBuilder.setCancelable(false);
  403. alertDialogBuilder.setView(view);
  404. alertDialog = alertDialogBuilder.create();
  405. alertDialog.show();
  406. }
  407. private void setupWindowAnimations() {
  408. this.overridePendingTransition(R.anim.animation_enter,
  409. R.anim.animation_leave);
  410. }
  411. @Override
  412. public boolean onOptionsItemSelected(MenuItem item) {
  413. if (item.getItemId() == android.R.id.home) {
  414. confirmClose();
  415. }
  416. return super.onOptionsItemSelected(item);
  417. }
  418. private void confirmClose() {
  419. new AlertDialog.Builder(AssessmentActivity.this)
  420. .setMessage("Are you sure you want to exit the assessment?")
  421. .setPositiveButton("Yes",
  422. new DialogInterface.OnClickListener() {
  423. @Override
  424. public void onClick(
  425. DialogInterface dialog,
  426. int which) {
  427. try {
  428. handler.removeCallbacksAndMessages(null);
  429. } catch (Exception e) {
  430. e.printStackTrace();
  431. }
  432. getToTime();
  433. finish();
  434. AssessmentActivity.this.overridePendingTransition(R.anim.animation_f_enter,
  435. R.anim.animation_f_leave);
  436. }
  437. }).setNegativeButton("No", null).show();
  438. }
  439. private void getToTime() {
  440. c = Calendar.getInstance();
  441. System.out.println("toTime " + c.getTime());
  442. df = new SimpleDateFormat("HH:mm:ss");
  443. toTime = df.format(c.getTime());
  444. Log.e("toTime", toTime);
  445. finalTime = UtilService.getTimeInterval(this, df, fromTime, toTime);
  446. if (GlobalData.user.getData().getRole().equalsIgnoreCase("9"))
  447. sendHitCountToServer();
  448. }
  449. @Override
  450. public void onBackPressed() {
  451. confirmClose();
  452. }
  453. @SuppressLint("StaticFieldLeak")
  454. private void getAssesmentQuistion() {
  455. progressDialog = new TransparentProgressDialog(AssessmentActivity.this);
  456. progressDialog.show();
  457. if (new UtilService().isNetworkAvailable(AssessmentActivity.this)) {
  458. restService.getAssesmentQuistion(ASSESSMENT.getAssessmentid()).enqueue(new Callback<AssessmentQuestionResponse>() {
  459. @Override
  460. public void onResponse(Call<AssessmentQuestionResponse> call, Response<AssessmentQuestionResponse> response) {
  461. if (progressDialog != null && progressDialog.isShowing()) {
  462. progressDialog.dismiss();
  463. }
  464. ASSESSMENT_QUESTIONS = new ArrayList<>();
  465. if (response.body() != null && response.body().getStatus() == 200) {
  466. ASSESSMENT_QUESTIONS = response.body().getData();
  467. bundle = new Bundle();
  468. bundle.putString("Assessment_questions", "Get All Assessment Questions");
  469. firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
  470. Cursor cursor = GlobalData.sdb.rawQuery("select * from assessment_question where assessment_id='"
  471. + ASSESSMENT.getAssessmentid()
  472. + "' and category_id='" + CATEGORY_ID + "' and training_id='" + TRAINING_ID + "'", null);
  473. if (cursor.getCount() > 0) {
  474. GlobalData.sdb.execSQL("delete from assessment_question where assessment_id='"
  475. + ASSESSMENT.getAssessmentid()
  476. + "' and category_id='" + CATEGORY_ID + "' and training_id='" + TRAINING_ID + "'");
  477. Gson gson = new Gson();
  478. JsonElement jsonElement = gson.toJsonTree(
  479. ASSESSMENT_QUESTIONS, new TypeToken<ArrayList<AssessmentQuestion>>() {
  480. }.getType());
  481. ContentValues values = new ContentValues();
  482. values.put("assessment_question", jsonElement.toString());
  483. values.put("training_id", TRAINING_ID);
  484. values.put("category_id", CATEGORY_ID);
  485. values.put("assessment_id", ASSESSMENT.getAssessmentid());
  486. GlobalData.sdb.insert("assessment_question", null, values);
  487. }
  488. setAssesmentQuistion();
  489. } else if (response.body() != null && response.body().getStatus() == 401) {
  490. GlobalData.canLogout = true;
  491. try {
  492. handler.removeCallbacksAndMessages(null);
  493. } catch (Exception e) {
  494. e.printStackTrace();
  495. }
  496. finish();
  497. } else if (response.body() != null) {
  498. GlobalData.showToast(getBaseContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
  499. } else if (response.errorBody() != null) {
  500. Gson gson = new Gson();
  501. ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
  502. GlobalData.showToast(getBaseContext(), message.getMessage(), Toast.LENGTH_SHORT).show();
  503. if (message.getStatus() == 401) {
  504. GlobalData.canLogout = true;
  505. try {
  506. handler.removeCallbacksAndMessages(null);
  507. } catch (Exception e) {
  508. e.printStackTrace();
  509. }
  510. finish();
  511. }
  512. }
  513. }
  514. @Override
  515. public void onFailure(Call<AssessmentQuestionResponse> call, Throwable t) {
  516. if (progressDialog != null && progressDialog.isShowing()) {
  517. progressDialog.dismiss();
  518. }
  519. GlobalData.showToast(getBaseContext(), "Connection Failure, try again!", Toast.LENGTH_SHORT).show();
  520. }
  521. });
  522. } else {
  523. new AsyncTask<Void, Void, String>() {
  524. @Override
  525. protected String doInBackground(Void... params) {
  526. try {
  527. Cursor cursor = GlobalData.sdb.rawQuery("select * from assessment_question where category_id='"
  528. + CATEGORY_ID + "' and training_id='" + TRAINING_ID + "' and assessment_id='"
  529. + ASSESSMENT.getAssessmentid() + "'", null);
  530. ASSESSMENT_QUESTIONS = new ArrayList<>();
  531. if (cursor.getCount() != 0) {
  532. cursor.moveToFirst();
  533. Gson gson = new Gson();
  534. ASSESSMENT_QUESTIONS = gson.fromJson(
  535. cursor.getString(cursor.getColumnIndex("assessment_question")),
  536. new TypeToken<ArrayList<AssessmentQuestion>>() {
  537. }.getType());
  538. }
  539. } catch (Exception e) {
  540. e.printStackTrace();
  541. }
  542. return null;
  543. }
  544. @Override
  545. protected void onPostExecute(String s) {
  546. super.onPostExecute(s);
  547. if (progressDialog != null && progressDialog.isShowing()) {
  548. progressDialog.dismiss();
  549. }
  550. if (ASSESSMENT_QUESTIONS.size() == 0) {
  551. GlobalData.showToast(getBaseContext(), "Please check your internet connection", Toast.LENGTH_SHORT).show();
  552. }
  553. setAssesmentQuistion();
  554. }
  555. }.execute();
  556. }
  557. }
  558. private void setAssesmentQuistion() {
  559. viewPager.setPageTransformer(true, new DepthPageTransformerNew());
  560. if (ASSESSMENT_QUESTIONS != null && ASSESSMENT_QUESTIONS.size() > 0) {
  561. viewPager.setAdapter(new AssesmentQuistionAdapter(getSupportFragmentManager()));
  562. indicatorTab.setViewPager(viewPager);
  563. viewPager.setPagingEnabled(true);
  564. } else {
  565. GlobalData.showToast(AssessmentActivity.this, "No Assessment Found", Toast.LENGTH_LONG).show();
  566. // onBackPressed();
  567. finish();
  568. }
  569. if (ASSESSMENT_QUESTIONS != null && ASSESSMENT_QUESTIONS.size() > 0) {
  570. previous.setEnabled(false);
  571. next.setEnabled(true);
  572. if (ASSESSMENT_QUESTIONS.size() == 1) {
  573. submitButton.setVisibility(View.VISIBLE);
  574. }
  575. }
  576. if (ASSESSMENT_QUESTIONS != null && ASSESSMENT_QUESTIONS.size() > 0) {
  577. viewPager.setOffscreenPageLimit(ASSESSMENT_QUESTIONS.size());
  578. }
  579. if (ASSESSMENT.getAssessmenttimetype().trim().equalsIgnoreCase("1")) {
  580. if (!ASSESSMENT.getAssessmenttime().trim().isEmpty()) {
  581. int seconds = Integer.parseInt(ASSESSMENT.getAssessmenttime().trim()) * 60;
  582. // seconds = 60;
  583. linearTimerView.setMax(seconds);
  584. linearTimerView.setProgress(seconds);
  585. handler.post(runnable);
  586. }
  587. }
  588. START_TIME = format.format(new Date());
  589. new Handler().postDelayed(new Runnable() {
  590. @Override
  591. public void run() {
  592. if (getPreferences(MODE_PRIVATE).getBoolean("AssessmentActivity", true)) {
  593. showHelpScreen();
  594. getPreferences(MODE_PRIVATE).edit().putBoolean("AssessmentActivity", false).apply();
  595. }
  596. }
  597. }, 1000);
  598. }
  599. private void manageBlinkEffect() {
  600. if (isBlinking) {
  601. return;
  602. }
  603. isBlinking = true;
  604. Animation anim = new AlphaAnimation(0.0f, 1.0f);
  605. anim.setDuration(1000);
  606. anim.setStartOffset(20);
  607. anim.setRepeatMode(Animation.REVERSE);
  608. anim.setRepeatCount(Animation.INFINITE);
  609. time.startAnimation(anim);
  610. }
  611. private void updateProgressColor() {
  612. float progress = linearTimerView.getProgress() / linearTimerView.getMax() * 100;
  613. if (progress < 60) {
  614. time.setTextColor(Color.parseColor("#F44336"));
  615. linearTimerView.setProgressColor(Color.parseColor("#F44336"));
  616. manageBlinkEffect();
  617. } else {
  618. linearTimerView.setProgressColor(Color.parseColor("#00344c"));
  619. time.setTextColor(Color.parseColor("#00344c"));
  620. }
  621. }
  622. @Override
  623. protected void onResume() {
  624. super.onResume();
  625. if (GlobalData.canLogout) {
  626. try {
  627. handler.removeCallbacksAndMessages(null);
  628. } catch (Exception e) {
  629. e.printStackTrace();
  630. }
  631. finish();
  632. }
  633. }
  634. private void showHelpScreen() {
  635. bundle = new Bundle();
  636. bundle.putString("Assessment_help_screen", "Assessment Help Screen");
  637. firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
  638. try {
  639. new FancyShowCaseView.Builder(this)
  640. .focusOn((View) indicatorTab.getParent())
  641. .title("Tap on the arrows to navigate next or previous questions")
  642. .titleSize(22, 1)
  643. .closeOnTouch(true)
  644. .fitSystemWindows(false)
  645. .focusShape(FocusShape.CIRCLE)
  646. .backgroundColor(Color.parseColor("#99000000"))
  647. .build()
  648. .show();
  649. } catch (Exception e) {
  650. e.printStackTrace();
  651. }
  652. }
  653. public class AssesmentQuistionAdapter extends FragmentStatePagerAdapter {
  654. private AssesmentQuistionAdapter(FragmentManager fm) {
  655. super(fm);
  656. }
  657. @Override
  658. public Fragment getItem(int position) {
  659. QuestionFragment fragment = new QuestionFragment();
  660. fragment.setTrainingId(TRAINING_ID);
  661. fragment.setAssessmentQuestion(ASSESSMENT_QUESTIONS.get(position));
  662. fragment.setQuestionNo(String.valueOf((position + 1)));
  663. return fragment;
  664. }
  665. @Override
  666. public int getCount() {
  667. return ASSESSMENT_QUESTIONS.size();
  668. }
  669. @Override
  670. public CharSequence getPageTitle(int position) {
  671. return String.valueOf((position + 1));
  672. }
  673. }
  674. }