From b38b982abc4cc611615a41d14fdeae6a7d1a8bd2 Mon Sep 17 00:00:00 2001 From: Sujit Kumar Date: Wed, 9 Dec 2020 21:41:14 +0530 Subject: [PATCH] Several performance improvements --- .../therealsuji/vtopchennai/HomeActivity.java | 936 ++++++----- .../java/tk/therealsuji/vtopchennai/VTOP.java | 1482 +++++++++-------- 2 files changed, 1294 insertions(+), 1124 deletions(-) diff --git a/app/src/main/java/tk/therealsuji/vtopchennai/HomeActivity.java b/app/src/main/java/tk/therealsuji/vtopchennai/HomeActivity.java index 11f0f004..af9ee7a2 100644 --- a/app/src/main/java/tk/therealsuji/vtopchennai/HomeActivity.java +++ b/app/src/main/java/tk/therealsuji/vtopchennai/HomeActivity.java @@ -183,464 +183,508 @@ protected void onCreate(Bundle savedInstanceState) { creditsView.setText(credits); final float pixelDensity = this.getResources().getDisplayMetrics().density; - - Calendar cal = Calendar.getInstance(); - Calendar calFuture = Calendar.getInstance(); - calFuture.add(Calendar.MINUTE, 30); - int dayCode = cal.get(Calendar.DAY_OF_WEEK); - SimpleDateFormat df = new SimpleDateFormat("HH:mm", Locale.ENGLISH); - - String day; - - if (dayCode == 1) { - day = "sun"; - } else if (dayCode == 2) { - day = "mon"; - } else if (dayCode == 3) { - day = "tue"; - } else if (dayCode == 4) { - day = "wed"; - } else if (dayCode == 5) { - day = "thu"; - } else if (dayCode == 6) { - day = "fri"; - } else { - day = "sat"; - } - - SQLiteDatabase myDatabase = this.openOrCreateDatabase("vtop", Context.MODE_PRIVATE, null); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS timetable_theory (id INT(3) PRIMARY KEY, start_time VARCHAR, end_time VARCHAR, mon VARCHAR, tue VARCHAR, wed VARCHAR, thu VARCHAR, fri VARCHAR, sat VARCHAR, sun VARCHAR)"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS timetable_lab (id INT(3) PRIMARY KEY, start_time VARCHAR, end_time VARCHAR, mon VARCHAR, tue VARCHAR, wed VARCHAR, thu VARCHAR, fri VARCHAR, sat VARCHAR, sun VARCHAR)"); - - Cursor theory = myDatabase.rawQuery("SELECT start_time, end_time, " + day + " FROM timetable_theory", null); - Cursor lab = myDatabase.rawQuery("SELECT start_time, end_time, " + day + " FROM timetable_lab", null); - - int startTheory = theory.getColumnIndex("start_time"); - int endTheory = theory.getColumnIndex("end_time"); - int dayTheory = theory.getColumnIndex(day); - - int startLab = lab.getColumnIndex("start_time"); - int endLab = lab.getColumnIndex("end_time"); - int dayLab = lab.getColumnIndex(day); - - theory.moveToFirst(); - lab.moveToFirst(); - - LinearLayout upcoming = findViewById(R.id.upcoming); - - boolean flag = false; - - SimpleDateFormat hour24 = new SimpleDateFormat("HH:mm", Locale.ENGLISH); - SimpleDateFormat hour12 = new SimpleDateFormat("h:mm a", Locale.ENGLISH); - - for (int i = 0; i < theory.getCount() && i < lab.getCount(); ++i, theory.moveToNext(), lab.moveToNext()) { - String startTimeTheory = theory.getString(startTheory); - String endTimeTheory = theory.getString(endTheory); - String startTimeLab = lab.getString(startLab); - String endTimeLab = lab.getString(endLab); - - try { - Date currentTime = df.parse(df.format(cal.getTime())); - Date futureTime = df.parse(df.format(calFuture.getTime())); - - assert currentTime != null; - assert futureTime != null; - - if ((futureTime.after(df.parse(startTimeTheory)) || futureTime.equals(df.parse(startTimeTheory))) && currentTime.before(df.parse(startTimeTheory)) && !theory.getString(dayTheory).equals("null")) { - if (!flag) { - upcoming.removeAllViews(); - } - - /* - The upcoming text - */ - TextView heading = new TextView(this); - TableRow.LayoutParams headingParams = new TableRow.LayoutParams( - TableRow.LayoutParams.WRAP_CONTENT, - TableRow.LayoutParams.WRAP_CONTENT - ); - headingParams.setMarginStart((int) (20 * pixelDensity)); - headingParams.setMarginEnd((int) (20 * pixelDensity)); - headingParams.setMargins(0, 0, 0, (int) (5 * pixelDensity)); - heading.setLayoutParams(headingParams); - heading.setText(getString(R.string.upcoming)); - heading.setTextColor(getColor(R.color.colorPrimary)); - heading.setTextSize(20); - heading.setTypeface(ResourcesCompat.getFont(this, R.font.rubik)); - - upcoming.addView(heading); //Adding the upcoming text to the block - - /* - The inner LinearLayout - */ - LinearLayout innerBlock = new LinearLayout(this); - LinearLayout.LayoutParams innerBlockParams = new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.WRAP_CONTENT - ); - innerBlock.setLayoutParams(innerBlockParams); - innerBlock.setOrientation(LinearLayout.HORIZONTAL); - - /* - The upcoming class course code - */ - String course = theory.getString(dayTheory).split("-")[1].trim() + " - Theory"; - - TextView period = new TextView(this); - TableRow.LayoutParams periodParams = new TableRow.LayoutParams( - TableRow.LayoutParams.WRAP_CONTENT, - TableRow.LayoutParams.WRAP_CONTENT - ); - periodParams.setMarginStart((int) (20 * pixelDensity)); - periodParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); - period.setLayoutParams(periodParams); - period.setText(course); - period.setTextColor(getColor(R.color.colorPrimary)); - period.setTextSize(16); - period.setTypeface(ResourcesCompat.getFont(this, R.font.rubik), Typeface.BOLD); - - innerBlock.addView(period); //Adding the upcoming class to innerBlock - - /* - Making a proper string of the timings - */ - String timings = startTimeTheory + " - " + endTimeTheory; - if (!DateFormat.is24HourFormat(this)) { - try { - Date startTime = hour24.parse(startTimeTheory); - Date endTime = hour24.parse(endTimeTheory); - assert startTime != null; - assert endTime != null; - timings = hour12.format(startTime) + " - " + hour12.format(endTime); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /* - The timing TextView - */ - TextView timing = new TextView(this); - TableRow.LayoutParams timingParams = new TableRow.LayoutParams( - TableRow.LayoutParams.MATCH_PARENT, - TableRow.LayoutParams.WRAP_CONTENT - ); - timingParams.setMarginEnd((int) (20 * pixelDensity)); - timingParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); - timing.setLayoutParams(timingParams); - timing.setText(timings); - timing.setTextColor(getColor(R.color.colorPrimary)); - timing.setTextSize(16); - timing.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END); - timing.setTypeface(ResourcesCompat.getFont(this, R.font.rubik), Typeface.BOLD); - - innerBlock.addView(timing); //Adding timing to innerBlock - - /* - Finally adding the innerBlock to the main block - */ - upcoming.addView(innerBlock); - flag = true; //Flag is set so that the next code doesn't erase everything + final SimpleDateFormat hour24 = new SimpleDateFormat("HH:mm", Locale.ENGLISH); + final SimpleDateFormat hour12 = new SimpleDateFormat("h:mm a", Locale.ENGLISH); + + final Context context = this; + final LinearLayout upcoming = findViewById(R.id.upcoming); + + new Thread(new Runnable() { + @Override + public void run() { + Calendar cal = Calendar.getInstance(); + Calendar calFuture = Calendar.getInstance(); + calFuture.add(Calendar.MINUTE, 30); + int dayCode = cal.get(Calendar.DAY_OF_WEEK); + + String day; + + if (dayCode == 1) { + day = "sun"; + } else if (dayCode == 2) { + day = "mon"; + } else if (dayCode == 3) { + day = "tue"; + } else if (dayCode == 4) { + day = "wed"; + } else if (dayCode == 5) { + day = "thu"; + } else if (dayCode == 6) { + day = "fri"; + } else { + day = "sat"; } - if ((futureTime.after(df.parse(startTimeLab)) || futureTime.equals(df.parse(startTimeLab))) && currentTime.before(df.parse(startTimeLab)) && !lab.getString(dayLab).equals("null")) { - if (!flag) { - upcoming.removeAllViews(); - } - - /* - The upcoming text - */ - TextView heading = new TextView(this); - TableRow.LayoutParams headingParams = new TableRow.LayoutParams( - TableRow.LayoutParams.WRAP_CONTENT, - TableRow.LayoutParams.WRAP_CONTENT - ); - headingParams.setMarginStart((int) (20 * pixelDensity)); - headingParams.setMarginEnd((int) (20 * pixelDensity)); - headingParams.setMargins(0, 0, 0, (int) (5 * pixelDensity)); - heading.setLayoutParams(headingParams); - heading.setText(getString(R.string.upcoming)); - heading.setTextColor(getColor(R.color.colorPrimary)); - heading.setTextSize(20); - heading.setTypeface(ResourcesCompat.getFont(this, R.font.rubik)); - - upcoming.addView(heading); //Adding the upcoming text to the block - - /* - The inner LinearLayout - */ - LinearLayout innerBlock = new LinearLayout(this); - LinearLayout.LayoutParams innerBlockParams = new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.WRAP_CONTENT - ); - innerBlock.setLayoutParams(innerBlockParams); - innerBlock.setOrientation(LinearLayout.HORIZONTAL); - - /* - The upcoming class course code - */ - String course = lab.getString(dayLab).split("-")[1].trim() + " - Lab"; - - TextView period = new TextView(this); - TableRow.LayoutParams periodParams = new TableRow.LayoutParams( - TableRow.LayoutParams.WRAP_CONTENT, - TableRow.LayoutParams.WRAP_CONTENT - ); - periodParams.setMarginStart((int) (20 * pixelDensity)); - periodParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); - period.setLayoutParams(periodParams); - period.setText(course); - period.setTextColor(getColor(R.color.colorPrimary)); - period.setTextSize(16); - period.setTypeface(ResourcesCompat.getFont(this, R.font.rubik), Typeface.BOLD); - - innerBlock.addView(period); //Adding the upcoming class to innerBlock - - /* - Making a proper string of the timings - */ - String timings = startTimeLab + " - " + endTimeLab; - if (!DateFormat.is24HourFormat(this)) { - try { - Date startTime = hour24.parse(startTimeLab); - Date endTime = hour24.parse(endTimeLab); - assert startTime != null; - assert endTime != null; - timings = hour12.format(startTime) + " - " + hour12.format(endTime); - } catch (Exception e) { - e.printStackTrace(); + SQLiteDatabase myDatabase = context.openOrCreateDatabase("vtop", Context.MODE_PRIVATE, null); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS timetable_theory (id INT(3) PRIMARY KEY, start_time VARCHAR, end_time VARCHAR, mon VARCHAR, tue VARCHAR, wed VARCHAR, thu VARCHAR, fri VARCHAR, sat VARCHAR, sun VARCHAR)"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS timetable_lab (id INT(3) PRIMARY KEY, start_time VARCHAR, end_time VARCHAR, mon VARCHAR, tue VARCHAR, wed VARCHAR, thu VARCHAR, fri VARCHAR, sat VARCHAR, sun VARCHAR)"); + + Cursor theory = myDatabase.rawQuery("SELECT start_time, end_time, " + day + " FROM timetable_theory", null); + Cursor lab = myDatabase.rawQuery("SELECT start_time, end_time, " + day + " FROM timetable_lab", null); + + int startTheory = theory.getColumnIndex("start_time"); + int endTheory = theory.getColumnIndex("end_time"); + int dayTheory = theory.getColumnIndex(day); + + int startLab = lab.getColumnIndex("start_time"); + int endLab = lab.getColumnIndex("end_time"); + int dayLab = lab.getColumnIndex(day); + + theory.moveToFirst(); + lab.moveToFirst(); + + boolean flag = false; + + for (int i = 0; i < theory.getCount() && i < lab.getCount(); ++i, theory.moveToNext(), lab.moveToNext()) { + String startTimeTheory = theory.getString(startTheory); + String endTimeTheory = theory.getString(endTheory); + String startTimeLab = lab.getString(startLab); + String endTimeLab = lab.getString(endLab); + + try { + Date currentTime = hour24.parse(hour24.format(cal.getTime())); + Date futureTime = hour24.parse(hour24.format(calFuture.getTime())); + + assert currentTime != null; + assert futureTime != null; + + if ((futureTime.after(hour24.parse(startTimeTheory)) || futureTime.equals(hour24.parse(startTimeTheory))) && currentTime.before(hour24.parse(startTimeTheory)) && !theory.getString(dayTheory).equals("null")) { + if (!flag) { + upcoming.removeAllViews(); + } + + /* + The upcoming text + */ + final TextView heading = new TextView(context); + TableRow.LayoutParams headingParams = new TableRow.LayoutParams( + TableRow.LayoutParams.WRAP_CONTENT, + TableRow.LayoutParams.WRAP_CONTENT + ); + headingParams.setMarginStart((int) (20 * pixelDensity)); + headingParams.setMarginEnd((int) (20 * pixelDensity)); + headingParams.setMargins(0, 0, 0, (int) (5 * pixelDensity)); + heading.setLayoutParams(headingParams); + heading.setText(getString(R.string.upcoming)); + heading.setTextColor(getColor(R.color.colorPrimary)); + heading.setTextSize(20); + heading.setTypeface(ResourcesCompat.getFont(context, R.font.rubik)); + + runOnUiThread(new Runnable() { + @Override + public void run() { + upcoming.addView(heading); //Adding the upcoming text to the block + } + }); + + /* + The inner LinearLayout + */ + final LinearLayout innerBlock = new LinearLayout(context); + LinearLayout.LayoutParams innerBlockParams = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.WRAP_CONTENT + ); + innerBlock.setLayoutParams(innerBlockParams); + innerBlock.setOrientation(LinearLayout.HORIZONTAL); + + /* + The upcoming class course code + */ + String course = theory.getString(dayTheory).split("-")[1].trim() + " - Theory"; + + TextView period = new TextView(context); + TableRow.LayoutParams periodParams = new TableRow.LayoutParams( + TableRow.LayoutParams.WRAP_CONTENT, + TableRow.LayoutParams.WRAP_CONTENT + ); + periodParams.setMarginStart((int) (20 * pixelDensity)); + periodParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); + period.setLayoutParams(periodParams); + period.setText(course); + period.setTextColor(getColor(R.color.colorPrimary)); + period.setTextSize(16); + period.setTypeface(ResourcesCompat.getFont(context, R.font.rubik), Typeface.BOLD); + + innerBlock.addView(period); //Adding the upcoming class to innerBlock + + /* + Making a proper string of the timings + */ + String timings = startTimeTheory + " - " + endTimeTheory; + if (!DateFormat.is24HourFormat(context)) { + try { + Date startTime = hour24.parse(startTimeTheory); + Date endTime = hour24.parse(endTimeTheory); + assert startTime != null; + assert endTime != null; + timings = hour12.format(startTime) + " - " + hour12.format(endTime); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /* + The timing TextView + */ + TextView timing = new TextView(context); + TableRow.LayoutParams timingParams = new TableRow.LayoutParams( + TableRow.LayoutParams.MATCH_PARENT, + TableRow.LayoutParams.WRAP_CONTENT + ); + timingParams.setMarginEnd((int) (20 * pixelDensity)); + timingParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); + timing.setLayoutParams(timingParams); + timing.setText(timings); + timing.setTextColor(getColor(R.color.colorPrimary)); + timing.setTextSize(16); + timing.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END); + timing.setTypeface(ResourcesCompat.getFont(context, R.font.rubik), Typeface.BOLD); + + innerBlock.addView(timing); //Adding timing to innerBlock + + /* + Finally adding the innerBlock to the main block + */ + runOnUiThread(new Runnable() { + @Override + public void run() { + upcoming.addView(innerBlock); + } + }); + flag = true; //Flag is set so that the next code doesn't erase everything } - } - - /* - The timing TextView - */ - TextView timing = new TextView(this); - TableRow.LayoutParams timingParams = new TableRow.LayoutParams( - TableRow.LayoutParams.MATCH_PARENT, - TableRow.LayoutParams.WRAP_CONTENT - ); - timingParams.setMarginEnd((int) (20 * pixelDensity)); - timingParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); - timing.setLayoutParams(timingParams); - timing.setText(timings); - timing.setTextColor(getColor(R.color.colorPrimary)); - timing.setTextSize(16); - timing.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END); - timing.setTypeface(ResourcesCompat.getFont(this, R.font.rubik), Typeface.BOLD); - - innerBlock.addView(timing); //Adding timing to innerBlock - - /* - Finally adding the innerBlock to the main block - */ - upcoming.addView(innerBlock); - flag = true; //Flag is set so that the next code doesn't erase everything - } - - if (flag) { - break; // If either Upcoming or Ongoing and Upcoming has been added, the loop can stop - } - if ((currentTime.after(df.parse(startTimeTheory)) || currentTime.equals(df.parse(startTimeTheory))) && (currentTime.before(df.parse(endTimeTheory)) || currentTime.equals(df.parse(endTimeTheory))) && !theory.getString(dayTheory).equals("null")) { - upcoming.removeAllViews(); - - /* - The ongoing text - */ - TextView heading = new TextView(this); - TableRow.LayoutParams headingParams = new TableRow.LayoutParams( - TableRow.LayoutParams.WRAP_CONTENT, - TableRow.LayoutParams.WRAP_CONTENT - ); - headingParams.setMarginStart((int) (20 * pixelDensity)); - headingParams.setMarginEnd((int) (20 * pixelDensity)); - headingParams.setMargins(0, 0, 0, (int) (5 * pixelDensity)); - heading.setLayoutParams(headingParams); - heading.setText(getString(R.string.ongoing)); - heading.setTextColor(getColor(R.color.colorPrimary)); - heading.setTextSize(20); - heading.setTypeface(ResourcesCompat.getFont(this, R.font.rubik)); - - upcoming.addView(heading); //Adding the ongoing text to the block - - /* - The inner LinearLayout - */ - LinearLayout innerBlock = new LinearLayout(this); - LinearLayout.LayoutParams innerBlockParams = new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.WRAP_CONTENT - ); - innerBlock.setLayoutParams(innerBlockParams); - innerBlock.setOrientation(LinearLayout.HORIZONTAL); - - /* - The ongoing class course code - */ - String course = theory.getString(dayTheory).split("-")[1].trim() + " - Theory"; - TextView period = new TextView(this); - TableRow.LayoutParams periodParams = new TableRow.LayoutParams( - TableRow.LayoutParams.WRAP_CONTENT, - TableRow.LayoutParams.WRAP_CONTENT - ); - periodParams.setMarginStart((int) (20 * pixelDensity)); - periodParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); - period.setLayoutParams(periodParams); - period.setText(course); - period.setTextColor(getColor(R.color.colorPrimary)); - period.setTextSize(16); - period.setTypeface(ResourcesCompat.getFont(this, R.font.rubik), Typeface.BOLD); - - innerBlock.addView(period); //Adding the ongoing class to innerBlock - - /* - Making a proper string of the timings - */ - String timings = startTimeTheory + " - " + endTimeTheory; - if (!DateFormat.is24HourFormat(this)) { - try { - Date startTime = hour24.parse(startTimeTheory); - Date endTime = hour24.parse(endTimeTheory); - assert startTime != null; - assert endTime != null; - timings = hour12.format(startTime) + " - " + hour12.format(endTime); - } catch (Exception e) { - e.printStackTrace(); + if ((futureTime.after(hour24.parse(startTimeLab)) || futureTime.equals(hour24.parse(startTimeLab))) && currentTime.before(hour24.parse(startTimeLab)) && !lab.getString(dayLab).equals("null")) { + if (!flag) { + upcoming.removeAllViews(); + } + + /* + The upcoming text + */ + final TextView heading = new TextView(context); + TableRow.LayoutParams headingParams = new TableRow.LayoutParams( + TableRow.LayoutParams.WRAP_CONTENT, + TableRow.LayoutParams.WRAP_CONTENT + ); + headingParams.setMarginStart((int) (20 * pixelDensity)); + headingParams.setMarginEnd((int) (20 * pixelDensity)); + headingParams.setMargins(0, 0, 0, (int) (5 * pixelDensity)); + heading.setLayoutParams(headingParams); + heading.setText(getString(R.string.upcoming)); + heading.setTextColor(getColor(R.color.colorPrimary)); + heading.setTextSize(20); + heading.setTypeface(ResourcesCompat.getFont(context, R.font.rubik)); + + runOnUiThread(new Runnable() { + @Override + public void run() { + upcoming.addView(heading); //Adding the upcoming text to the block + } + }); + + /* + The inner LinearLayout + */ + final LinearLayout innerBlock = new LinearLayout(context); + LinearLayout.LayoutParams innerBlockParams = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.WRAP_CONTENT + ); + innerBlock.setLayoutParams(innerBlockParams); + innerBlock.setOrientation(LinearLayout.HORIZONTAL); + + /* + The upcoming class course code + */ + String course = lab.getString(dayLab).split("-")[1].trim() + " - Lab"; + + TextView period = new TextView(context); + TableRow.LayoutParams periodParams = new TableRow.LayoutParams( + TableRow.LayoutParams.WRAP_CONTENT, + TableRow.LayoutParams.WRAP_CONTENT + ); + periodParams.setMarginStart((int) (20 * pixelDensity)); + periodParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); + period.setLayoutParams(periodParams); + period.setText(course); + period.setTextColor(getColor(R.color.colorPrimary)); + period.setTextSize(16); + period.setTypeface(ResourcesCompat.getFont(context, R.font.rubik), Typeface.BOLD); + + innerBlock.addView(period); //Adding the upcoming class to innerBlock + + /* + Making a proper string of the timings + */ + String timings = startTimeLab + " - " + endTimeLab; + if (!DateFormat.is24HourFormat(context)) { + try { + Date startTime = hour24.parse(startTimeLab); + Date endTime = hour24.parse(endTimeLab); + assert startTime != null; + assert endTime != null; + timings = hour12.format(startTime) + " - " + hour12.format(endTime); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /* + The timing TextView + */ + TextView timing = new TextView(context); + TableRow.LayoutParams timingParams = new TableRow.LayoutParams( + TableRow.LayoutParams.MATCH_PARENT, + TableRow.LayoutParams.WRAP_CONTENT + ); + timingParams.setMarginEnd((int) (20 * pixelDensity)); + timingParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); + timing.setLayoutParams(timingParams); + timing.setText(timings); + timing.setTextColor(getColor(R.color.colorPrimary)); + timing.setTextSize(16); + timing.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END); + timing.setTypeface(ResourcesCompat.getFont(context, R.font.rubik), Typeface.BOLD); + + innerBlock.addView(timing); //Adding timing to innerBlock + + /* + Finally adding the innerBlock to the main block + */ + runOnUiThread(new Runnable() { + @Override + public void run() { + upcoming.addView(innerBlock); + } + }); + flag = true; //Flag is set so that the next code doesn't erase everything } - } - /* - The timing TextView - */ - TextView timing = new TextView(this); - TableRow.LayoutParams timingParams = new TableRow.LayoutParams( - TableRow.LayoutParams.MATCH_PARENT, - TableRow.LayoutParams.WRAP_CONTENT - ); - timingParams.setMarginEnd((int) (20 * pixelDensity)); - timingParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); - timing.setLayoutParams(timingParams); - timing.setText(timings); - timing.setTextColor(getColor(R.color.colorPrimary)); - timing.setTextSize(16); - timing.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END); - timing.setTypeface(ResourcesCompat.getFont(this, R.font.rubik), Typeface.BOLD); - - /* - Finally adding the innerBlock to the main block - */ - innerBlock.addView(timing); - - upcoming.addView(innerBlock); - flag = true; //Flag is set so that the next code doesn't erase everything - } + if (flag) { + break; // If either Upcoming or Ongoing and Upcoming has been added, the loop can stop + } - if ((currentTime.after(df.parse(startTimeLab)) || currentTime.equals(df.parse(startTimeLab))) && (currentTime.before(df.parse(endTimeLab)) || currentTime.equals(df.parse(endTimeLab))) && !lab.getString(dayLab).equals("null")) { - if (!flag) { - upcoming.removeAllViews(); - } + if ((currentTime.after(hour24.parse(startTimeTheory)) || currentTime.equals(hour24.parse(startTimeTheory))) && (currentTime.before(hour24.parse(endTimeTheory)) || currentTime.equals(hour24.parse(endTimeTheory))) && !theory.getString(dayTheory).equals("null")) { + upcoming.removeAllViews(); + + /* + The ongoing text + */ + final TextView heading = new TextView(context); + TableRow.LayoutParams headingParams = new TableRow.LayoutParams( + TableRow.LayoutParams.WRAP_CONTENT, + TableRow.LayoutParams.WRAP_CONTENT + ); + headingParams.setMarginStart((int) (20 * pixelDensity)); + headingParams.setMarginEnd((int) (20 * pixelDensity)); + headingParams.setMargins(0, 0, 0, (int) (5 * pixelDensity)); + heading.setLayoutParams(headingParams); + heading.setText(getString(R.string.ongoing)); + heading.setTextColor(getColor(R.color.colorPrimary)); + heading.setTextSize(20); + heading.setTypeface(ResourcesCompat.getFont(context, R.font.rubik)); + + runOnUiThread(new Runnable() { + @Override + public void run() { + upcoming.addView(heading); //Adding the ongoing text to the block + } + }); + + /* + The inner LinearLayout + */ + final LinearLayout innerBlock = new LinearLayout(context); + LinearLayout.LayoutParams innerBlockParams = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.WRAP_CONTENT + ); + innerBlock.setLayoutParams(innerBlockParams); + innerBlock.setOrientation(LinearLayout.HORIZONTAL); + + /* + The ongoing class course code + */ + String course = theory.getString(dayTheory).split("-")[1].trim() + " - Theory"; + TextView period = new TextView(context); + TableRow.LayoutParams periodParams = new TableRow.LayoutParams( + TableRow.LayoutParams.WRAP_CONTENT, + TableRow.LayoutParams.WRAP_CONTENT + ); + periodParams.setMarginStart((int) (20 * pixelDensity)); + periodParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); + period.setLayoutParams(periodParams); + period.setText(course); + period.setTextColor(getColor(R.color.colorPrimary)); + period.setTextSize(16); + period.setTypeface(ResourcesCompat.getFont(context, R.font.rubik), Typeface.BOLD); + + innerBlock.addView(period); //Adding the ongoing class to innerBlock + + /* + Making a proper string of the timings + */ + String timings = startTimeTheory + " - " + endTimeTheory; + if (!DateFormat.is24HourFormat(context)) { + try { + Date startTime = hour24.parse(startTimeTheory); + Date endTime = hour24.parse(endTimeTheory); + assert startTime != null; + assert endTime != null; + timings = hour12.format(startTime) + " - " + hour12.format(endTime); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /* + The timing TextView + */ + TextView timing = new TextView(context); + TableRow.LayoutParams timingParams = new TableRow.LayoutParams( + TableRow.LayoutParams.MATCH_PARENT, + TableRow.LayoutParams.WRAP_CONTENT + ); + timingParams.setMarginEnd((int) (20 * pixelDensity)); + timingParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); + timing.setLayoutParams(timingParams); + timing.setText(timings); + timing.setTextColor(getColor(R.color.colorPrimary)); + timing.setTextSize(16); + timing.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END); + timing.setTypeface(ResourcesCompat.getFont(context, R.font.rubik), Typeface.BOLD); + + /* + Finally adding the innerBlock to the main block + */ + innerBlock.addView(timing); + + runOnUiThread(new Runnable() { + @Override + public void run() { + upcoming.addView(innerBlock); + } + }); + flag = true; //Flag is set so that the next code doesn't erase everything + } - /* - The ongoing text - */ - TextView heading = new TextView(this); - TableRow.LayoutParams headingParams = new TableRow.LayoutParams( - TableRow.LayoutParams.WRAP_CONTENT, - TableRow.LayoutParams.WRAP_CONTENT - ); - headingParams.setMarginStart((int) (20 * pixelDensity)); - headingParams.setMarginEnd((int) (20 * pixelDensity)); - headingParams.setMargins(0, 0, 0, (int) (5 * pixelDensity)); - heading.setLayoutParams(headingParams); - heading.setText(getString(R.string.ongoing)); - heading.setTextColor(getColor(R.color.colorPrimary)); - heading.setTextSize(20); - heading.setTypeface(ResourcesCompat.getFont(this, R.font.rubik)); - - upcoming.addView(heading); //Adding the ongoing text to the block - - /* - The inner LinearLayout - */ - LinearLayout innerBlock = new LinearLayout(this); - LinearLayout.LayoutParams innerBlockParams = new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.WRAP_CONTENT - ); - innerBlock.setLayoutParams(innerBlockParams); - innerBlock.setOrientation(LinearLayout.HORIZONTAL); - - /* - The ongoing class course code - */ - String course = lab.getString(dayLab).split("-")[1].trim() + " - Lab"; - TextView period = new TextView(this); - TableRow.LayoutParams periodParams = new TableRow.LayoutParams( - TableRow.LayoutParams.WRAP_CONTENT, - TableRow.LayoutParams.WRAP_CONTENT - ); - periodParams.setMarginStart((int) (20 * pixelDensity)); - periodParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); - period.setLayoutParams(periodParams); - period.setText(course); - period.setTextColor(getColor(R.color.colorPrimary)); - period.setTextSize(16); - period.setTypeface(ResourcesCompat.getFont(this, R.font.rubik), Typeface.BOLD); - - innerBlock.addView(period); //Adding the ongoing class to innerBlock - - /* - Making a proper string of the timings - */ - String timings = startTimeLab + " - " + endTimeLab; - if (!DateFormat.is24HourFormat(this)) { - try { - Date startTime = hour24.parse(startTimeLab); - Date endTime = hour24.parse(endTimeLab); - assert startTime != null; - assert endTime != null; - timings = hour12.format(startTime) + " - " + hour12.format(endTime); - } catch (Exception e) { - e.printStackTrace(); + if ((currentTime.after(hour24.parse(startTimeLab)) || currentTime.equals(hour24.parse(startTimeLab))) && (currentTime.before(hour24.parse(endTimeLab)) || currentTime.equals(hour24.parse(endTimeLab))) && !lab.getString(dayLab).equals("null")) { + if (!flag) { + upcoming.removeAllViews(); + } + + /* + The ongoing text + */ + final TextView heading = new TextView(context); + TableRow.LayoutParams headingParams = new TableRow.LayoutParams( + TableRow.LayoutParams.WRAP_CONTENT, + TableRow.LayoutParams.WRAP_CONTENT + ); + headingParams.setMarginStart((int) (20 * pixelDensity)); + headingParams.setMarginEnd((int) (20 * pixelDensity)); + headingParams.setMargins(0, 0, 0, (int) (5 * pixelDensity)); + heading.setLayoutParams(headingParams); + heading.setText(getString(R.string.ongoing)); + heading.setTextColor(getColor(R.color.colorPrimary)); + heading.setTextSize(20); + heading.setTypeface(ResourcesCompat.getFont(context, R.font.rubik)); + + runOnUiThread(new Runnable() { + @Override + public void run() { + upcoming.addView(heading); //Adding the ongoing text to the block + } + }); + + /* + The inner LinearLayout + */ + final LinearLayout innerBlock = new LinearLayout(context); + LinearLayout.LayoutParams innerBlockParams = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.WRAP_CONTENT + ); + innerBlock.setLayoutParams(innerBlockParams); + innerBlock.setOrientation(LinearLayout.HORIZONTAL); + + /* + The ongoing class course code + */ + String course = lab.getString(dayLab).split("-")[1].trim() + " - Lab"; + TextView period = new TextView(context); + TableRow.LayoutParams periodParams = new TableRow.LayoutParams( + TableRow.LayoutParams.WRAP_CONTENT, + TableRow.LayoutParams.WRAP_CONTENT + ); + periodParams.setMarginStart((int) (20 * pixelDensity)); + periodParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); + period.setLayoutParams(periodParams); + period.setText(course); + period.setTextColor(getColor(R.color.colorPrimary)); + period.setTextSize(16); + period.setTypeface(ResourcesCompat.getFont(context, R.font.rubik), Typeface.BOLD); + + innerBlock.addView(period); //Adding the ongoing class to innerBlock + + /* + Making a proper string of the timings + */ + String timings = startTimeLab + " - " + endTimeLab; + if (!DateFormat.is24HourFormat(context)) { + try { + Date startTime = hour24.parse(startTimeLab); + Date endTime = hour24.parse(endTimeLab); + assert startTime != null; + assert endTime != null; + timings = hour12.format(startTime) + " - " + hour12.format(endTime); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /* + The timing TextView + */ + TextView timing = new TextView(context); + TableRow.LayoutParams timingParams = new TableRow.LayoutParams( + TableRow.LayoutParams.MATCH_PARENT, + TableRow.LayoutParams.WRAP_CONTENT + ); + timingParams.setMarginEnd((int) (20 * pixelDensity)); + timingParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); + timing.setLayoutParams(timingParams); + timing.setText(timings); + timing.setTextColor(getColor(R.color.colorPrimary)); + timing.setTextSize(16); + timing.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END); + timing.setTypeface(ResourcesCompat.getFont(context, R.font.rubik), Typeface.BOLD); + + /* + Finally adding the innerBlock to the main block + */ + innerBlock.addView(timing); + + runOnUiThread(new Runnable() { + @Override + public void run() { + upcoming.addView(innerBlock); + } + }); + flag = true; //Flag is set so that the next code doesn't erase everything } + } catch (Exception e) { + e.printStackTrace(); } - - /* - The timing TextView - */ - TextView timing = new TextView(this); - TableRow.LayoutParams timingParams = new TableRow.LayoutParams( - TableRow.LayoutParams.MATCH_PARENT, - TableRow.LayoutParams.WRAP_CONTENT - ); - timingParams.setMarginEnd((int) (20 * pixelDensity)); - timingParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity)); - timing.setLayoutParams(timingParams); - timing.setText(timings); - timing.setTextColor(getColor(R.color.colorPrimary)); - timing.setTextSize(16); - timing.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END); - timing.setTypeface(ResourcesCompat.getFont(this, R.font.rubik), Typeface.BOLD); - - /* - Finally adding the innerBlock to the main block - */ - innerBlock.addView(timing); - - upcoming.addView(innerBlock); - flag = true; //Flag is set so that the next code doesn't erase everything } - } catch (Exception e) { - e.printStackTrace(); - } - } - theory.close(); - lab.close(); - myDatabase.close(); + theory.close(); + lab.close(); + myDatabase.close(); + } + }).start(); String refreshedDate = sharedPreferences.getString("refreshedDate", getString(R.string.refreshed_unavailable)); String refreshedTime = sharedPreferences.getString("refreshedTime", getString(R.string.refreshed_unavailable)); diff --git a/app/src/main/java/tk/therealsuji/vtopchennai/VTOP.java b/app/src/main/java/tk/therealsuji/vtopchennai/VTOP.java index ed620b17..18524f15 100644 --- a/app/src/main/java/tk/therealsuji/vtopchennai/VTOP.java +++ b/app/src/main/java/tk/therealsuji/vtopchennai/VTOP.java @@ -195,9 +195,7 @@ public void onReceiveValue(String src) { captchaLayout.setVisibility(View.VISIBLE); } catch (Exception e) { e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } } }); @@ -224,7 +222,7 @@ public void signIn(String username, String password, String captcha) { " successFlag = 'Invalid Captcha';" + " } else if(response.toLowerCase().includes('invalid user id / password')) {" + " successFlag = 'Invalid User Id / Password';" + - " } else if(response.toLowerCase().includes('user id not available')) {" + + " } else if(response.toLowerCase().includes('user id not available')) {" + " successFlag = 'User Id Not available';" + " }" + " }" + @@ -301,9 +299,7 @@ public void onReceiveValue(String obj) { */ String temp = obj.substring(1, obj.length() - 1); if (obj.equals("false") || obj.equals("null")) { - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } else if (temp.equals("")) { Toast.makeText(context, "Sorry, we had some trouble downloading your profile.", Toast.LENGTH_LONG).show(); ((Activity) context).finish(); @@ -315,9 +311,7 @@ public void onReceiveValue(String obj) { openTimetable(); } catch (Exception e) { e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again later.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } } } @@ -361,9 +355,7 @@ public void onReceiveValue(String obj) { obj is in the form of a JSON string like {"0": "Option 1", "1": "Option 2", "2": "Option 3",...} */ if (obj.equals("false") || obj.equals("null")) { - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } else { try { JSONObject myObj = new JSONObject(obj); @@ -378,9 +370,7 @@ public void onReceiveValue(String obj) { semesterLayout.setVisibility(View.VISIBLE); } catch (Exception e) { e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } } } @@ -423,9 +413,7 @@ public void onReceiveValue(String value) { if (value.equals("true")) { downloadTimetable(); } else { - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } } }); @@ -523,373 +511,389 @@ public void downloadTimetable() { "return obj;" + "})();", new ValueCallback() { @Override - public void onReceiveValue(String obj) { + public void onReceiveValue(final String obj) { String temp = obj.substring(1, obj.length() - 1); if (obj.equals("null")) { - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } else if (temp.equals("unreleased") || temp.equals("")) { - myDatabase.execSQL("DROP TABLE IF EXISTS timetable_lab"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS timetable_lab (id INT(3) PRIMARY KEY, start_time VARCHAR, end_time VARCHAR, mon VARCHAR, tue VARCHAR, wed VARCHAR, thu VARCHAR, fri VARCHAR, sat VARCHAR, sun VARCHAR)"); - - myDatabase.execSQL("DROP TABLE IF EXISTS timetable_theory"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS timetable_theory (id INT(3) PRIMARY KEY, start_time VARCHAR, end_time VARCHAR, mon VARCHAR, tue VARCHAR, wed VARCHAR, thu VARCHAR, fri VARCHAR, sat VARCHAR, sun VARCHAR)"); - - myDatabase.execSQL("DROP TABLE IF EXISTS faculty"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS faculty (id INT(3) PRIMARY KEY, course VARCHAR, faculty VARCHAR)"); - - AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE); - Intent notificationIntent = new Intent(context, NotificationReceiver.class); - for (int j = 0; j < Integer.parseInt(sharedPreferences.getString("alarms", "0")); ++j) { - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.cancel(pendingIntent); - } - - downloadProctor(); - } else { - try { - JSONObject myObj = new JSONObject(obj); - String credits = "Credits: " + myObj.getString("credits"); - sharedPreferences.edit().putString("credits", credits).apply(); - - myDatabase.execSQL("DROP TABLE IF EXISTS timetable_lab"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS timetable_lab (id INT(3) PRIMARY KEY, start_time VARCHAR, end_time VARCHAR, mon VARCHAR, tue VARCHAR, wed VARCHAR, thu VARCHAR, fri VARCHAR, sat VARCHAR, sun VARCHAR)"); - - myDatabase.execSQL("DROP TABLE IF EXISTS timetable_theory"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS timetable_theory (id INT(3) PRIMARY KEY, start_time VARCHAR, end_time VARCHAR, mon VARCHAR, tue VARCHAR, wed VARCHAR, thu VARCHAR, fri VARCHAR, sat VARCHAR, sun VARCHAR)"); - - JSONObject lab = new JSONObject(myObj.getString("lab")); - JSONObject theory = new JSONObject(myObj.getString("theory")); - JSONObject mon = new JSONObject(myObj.getString("mon")); - JSONObject tue = new JSONObject(myObj.getString("tue")); - JSONObject wed = new JSONObject(myObj.getString("wed")); - JSONObject thu = new JSONObject(myObj.getString("thu")); - JSONObject fri = new JSONObject(myObj.getString("fri")); - JSONObject sat = new JSONObject(myObj.getString("sat")); - JSONObject sun = new JSONObject(myObj.getString("sun")); - - Calendar c = Calendar.getInstance(); - AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE); - Intent notificationIntent = new Intent(context, NotificationReceiver.class); - SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.ENGLISH); - - for (int j = 0; j < Integer.parseInt(sharedPreferences.getString("alarms", "0")); ++j) { - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.cancel(pendingIntent); - } - - int j = 0; - - for (int i = 0; i < lab.length() / 2 && i < theory.length() / 2; ++i) { - String start_time = lab.getString(i + "start"); - if (start_time.toLowerCase().equals("lunch")) { - continue; - } - myDatabase.execSQL("INSERT INTO timetable_lab (id, start_time) VALUES ('" + i + "', '" + start_time + "')"); - - String end_time = lab.getString(i + "end"); - if (end_time.toLowerCase().equals("lunch")) { - continue; - } - myDatabase.execSQL("UPDATE timetable_lab SET end_time = '" + end_time + "' WHERE id = " + i); - - if (mon.has(i + "lab")) { - String period = mon.getString(i + "lab"); - myDatabase.execSQL("UPDATE timetable_lab SET mon = '" + period + "' WHERE id = " + i); - - Date date = df.parse("06-01-2020 " + start_time); - - assert date != null; - c.setTime(date); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - - c.add(Calendar.MINUTE, -30); - pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - } else { - myDatabase.execSQL("UPDATE timetable_lab SET mon = 'null' WHERE id = " + i); - } - - if (tue.has(i + "lab")) { - String period = tue.getString(i + "lab"); - myDatabase.execSQL("UPDATE timetable_lab SET tue = '" + period + "' WHERE id = " + i); - - Date date = df.parse("07-01-2020 " + start_time); - - assert date != null; - c.setTime(date); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - - c.add(Calendar.MINUTE, -30); - pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - } else { - myDatabase.execSQL("UPDATE timetable_lab SET tue = 'null' WHERE id = " + i); - } - - if (wed.has(i + "lab")) { - String period = wed.getString(i + "lab"); - myDatabase.execSQL("UPDATE timetable_lab SET wed = '" + period + "' WHERE id = " + i); - - Date date = df.parse("01-01-2020 " + start_time); - - assert date != null; - c.setTime(date); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - - c.add(Calendar.MINUTE, -30); - pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - } else { - myDatabase.execSQL("UPDATE timetable_lab SET wed = 'null' WHERE id = " + i); - } - - if (thu.has(i + "lab")) { - String period = thu.getString(i + "lab"); - myDatabase.execSQL("UPDATE timetable_lab SET thu = '" + period + "' WHERE id = " + i); - - Date date = df.parse("02-01-2020 " + start_time); - - assert date != null; - c.setTime(date); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - - c.add(Calendar.MINUTE, -30); - pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - } else { - myDatabase.execSQL("UPDATE timetable_lab SET thu = 'null' WHERE id = " + i); - } - - if (fri.has(i + "lab")) { - String period = fri.getString(i + "lab"); - myDatabase.execSQL("UPDATE timetable_lab SET fri = '" + period + "' WHERE id = " + i); - - Date date = df.parse("03-01-2020 " + start_time); - - assert date != null; - c.setTime(date); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - - c.add(Calendar.MINUTE, -30); - pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - } else { - myDatabase.execSQL("UPDATE timetable_lab SET fri = 'null' WHERE id = " + i); - } - - if (sat.has(i + "lab")) { - String period = sat.getString(i + "lab"); - myDatabase.execSQL("UPDATE timetable_lab SET sat = '" + period + "' WHERE id = " + i); - - Date date = df.parse("04-01-2020 " + start_time); - - assert date != null; - c.setTime(date); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - - c.add(Calendar.MINUTE, -30); - pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - } else { - myDatabase.execSQL("UPDATE timetable_lab SET sat = 'null' WHERE id = " + i); - } - - if (sun.has(i + "lab")) { - String period = sun.getString(i + "lab"); - myDatabase.execSQL("UPDATE timetable_lab SET sun = '" + period + "' WHERE id = " + i); - - Date date = df.parse("05-01-2020 " + start_time); - - assert date != null; - c.setTime(date); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - - c.add(Calendar.MINUTE, -30); - pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - } else { - myDatabase.execSQL("UPDATE timetable_lab SET sun = 'null' WHERE id = " + i); - - } - - start_time = theory.getString(i + "start"); - myDatabase.execSQL("INSERT INTO timetable_theory (id, start_time) VALUES ('" + i + "', '" + start_time + "')"); - - end_time = theory.getString(i + "end"); - myDatabase.execSQL("UPDATE timetable_theory SET end_time = '" + end_time + "' WHERE id = " + i); - - if (mon.has(i + "theory")) { - String period = mon.getString(i + "theory"); - myDatabase.execSQL("UPDATE timetable_theory SET mon = '" + period + "' WHERE id = " + i); - - Date date = df.parse("06-01-2020 " + start_time); - - assert date != null; - c.setTime(date); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - - c.add(Calendar.MINUTE, -30); - pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - } else { - myDatabase.execSQL("UPDATE timetable_theory SET mon = 'null' WHERE id = " + i); - } - - if (tue.has(i + "theory")) { - String period = tue.getString(i + "theory"); - myDatabase.execSQL("UPDATE timetable_theory SET tue = '" + period + "' WHERE id = " + i); - - Date date = df.parse("07-01-2020 " + start_time); - - assert date != null; - c.setTime(date); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - - c.add(Calendar.MINUTE, -30); - pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - } else { - myDatabase.execSQL("UPDATE timetable_theory SET tue = 'null' WHERE id = " + i); - } - - if (wed.has(i + "theory")) { - String period = wed.getString(i + "theory"); - myDatabase.execSQL("UPDATE timetable_theory SET wed = '" + period + "' WHERE id = " + i); - - Date date = df.parse("01-01-2020 " + start_time); - - assert date != null; - c.setTime(date); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - - c.add(Calendar.MINUTE, -30); - pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - } else { - myDatabase.execSQL("UPDATE timetable_theory SET wed = 'null' WHERE id = " + i); - } + new Thread(new Runnable() { + @Override + public void run() { + myDatabase.execSQL("DROP TABLE IF EXISTS timetable_lab"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS timetable_lab (id INT(3) PRIMARY KEY, start_time VARCHAR, end_time VARCHAR, mon VARCHAR, tue VARCHAR, wed VARCHAR, thu VARCHAR, fri VARCHAR, sat VARCHAR, sun VARCHAR)"); - if (thu.has(i + "theory")) { - String period = thu.getString(i + "theory"); - myDatabase.execSQL("UPDATE timetable_theory SET thu = '" + period + "' WHERE id = " + i); + myDatabase.execSQL("DROP TABLE IF EXISTS timetable_theory"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS timetable_theory (id INT(3) PRIMARY KEY, start_time VARCHAR, end_time VARCHAR, mon VARCHAR, tue VARCHAR, wed VARCHAR, thu VARCHAR, fri VARCHAR, sat VARCHAR, sun VARCHAR)"); - Date date = df.parse("02-01-2020 " + start_time); + myDatabase.execSQL("DROP TABLE IF EXISTS faculty"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS faculty (id INT(3) PRIMARY KEY, course VARCHAR, faculty VARCHAR)"); - assert date != null; - c.setTime(date); + AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE); + Intent notificationIntent = new Intent(context, NotificationReceiver.class); + for (int j = 0; j < sharedPreferences.getInt("alarmCount", 0); ++j) { PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - - c.add(Calendar.MINUTE, -30); - pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - } else { - myDatabase.execSQL("UPDATE timetable_theory SET thu = 'null' WHERE id = " + i); + alarmManager.cancel(pendingIntent); } - if (fri.has(i + "theory")) { - String period = fri.getString(i + "theory"); - myDatabase.execSQL("UPDATE timetable_theory SET fri = '" + period + "' WHERE id = " + i); - - Date date = df.parse("03-01-2020 " + start_time); - - assert date != null; - c.setTime(date); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - - c.add(Calendar.MINUTE, -30); - pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - } else { - myDatabase.execSQL("UPDATE timetable_theory SET fri = 'null' WHERE id = " + i); - } - - if (sat.has(i + "theory")) { - String period = sat.getString(i + "theory"); - myDatabase.execSQL("UPDATE timetable_theory SET sat = '" + period + "' WHERE id = " + i); - - Date date = df.parse("04-01-2020 " + start_time); - - assert date != null; - c.setTime(date); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - - c.add(Calendar.MINUTE, -30); - pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - } else { - myDatabase.execSQL("UPDATE timetable_theory SET sat = 'null' WHERE id = " + i); - } - - if (sun.has(i + "theory")) { - String period = sun.getString(i + "theory"); - myDatabase.execSQL("UPDATE timetable_theory SET sun = '" + period + "' WHERE id = " + i); - - Date date = df.parse("05-01-2020 " + start_time); - - assert date != null; - c.setTime(date); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - - c.add(Calendar.MINUTE, -30); - pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); - alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); - ++j; - } else { - myDatabase.execSQL("UPDATE timetable_theory SET sun = 'null' WHERE id = " + i); + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + downloadProctor(); + } + }); + } + }).start(); + } else { + new Thread(new Runnable() { + @Override + public void run() { + try { + JSONObject myObj = new JSONObject(obj); + String credits = "Credits: " + myObj.getString("credits"); + sharedPreferences.edit().putString("credits", credits).apply(); + + myDatabase.execSQL("DROP TABLE IF EXISTS timetable_lab"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS timetable_lab (id INT(3) PRIMARY KEY, start_time VARCHAR, end_time VARCHAR, mon VARCHAR, tue VARCHAR, wed VARCHAR, thu VARCHAR, fri VARCHAR, sat VARCHAR, sun VARCHAR)"); + + myDatabase.execSQL("DROP TABLE IF EXISTS timetable_theory"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS timetable_theory (id INT(3) PRIMARY KEY, start_time VARCHAR, end_time VARCHAR, mon VARCHAR, tue VARCHAR, wed VARCHAR, thu VARCHAR, fri VARCHAR, sat VARCHAR, sun VARCHAR)"); + + JSONObject lab = new JSONObject(myObj.getString("lab")); + JSONObject theory = new JSONObject(myObj.getString("theory")); + JSONObject mon = new JSONObject(myObj.getString("mon")); + JSONObject tue = new JSONObject(myObj.getString("tue")); + JSONObject wed = new JSONObject(myObj.getString("wed")); + JSONObject thu = new JSONObject(myObj.getString("thu")); + JSONObject fri = new JSONObject(myObj.getString("fri")); + JSONObject sat = new JSONObject(myObj.getString("sat")); + JSONObject sun = new JSONObject(myObj.getString("sun")); + + Calendar c = Calendar.getInstance(); + AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE); + Intent notificationIntent = new Intent(context, NotificationReceiver.class); + SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.ENGLISH); + + for (int j = 0; j < sharedPreferences.getInt("alarmCount", 0); ++j) { + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.cancel(pendingIntent); + } + + int j = 0; + + for (int i = 0; i < lab.length() / 2 && i < theory.length() / 2; ++i) { + String start_time = lab.getString(i + "start"); + if (start_time.toLowerCase().equals("lunch")) { + continue; + } + myDatabase.execSQL("INSERT INTO timetable_lab (id, start_time) VALUES ('" + i + "', '" + start_time + "')"); + + String end_time = lab.getString(i + "end"); + if (end_time.toLowerCase().equals("lunch")) { + continue; + } + myDatabase.execSQL("UPDATE timetable_lab SET end_time = '" + end_time + "' WHERE id = " + i); + + if (mon.has(i + "lab")) { + String period = mon.getString(i + "lab"); + myDatabase.execSQL("UPDATE timetable_lab SET mon = '" + period + "' WHERE id = " + i); + + Date date = df.parse("06-01-2020 " + start_time); + + assert date != null; + c.setTime(date); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + + c.add(Calendar.MINUTE, -30); + pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + } else { + myDatabase.execSQL("UPDATE timetable_lab SET mon = 'null' WHERE id = " + i); + } + + if (tue.has(i + "lab")) { + String period = tue.getString(i + "lab"); + myDatabase.execSQL("UPDATE timetable_lab SET tue = '" + period + "' WHERE id = " + i); + + Date date = df.parse("07-01-2020 " + start_time); + + assert date != null; + c.setTime(date); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + + c.add(Calendar.MINUTE, -30); + pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + } else { + myDatabase.execSQL("UPDATE timetable_lab SET tue = 'null' WHERE id = " + i); + } + + if (wed.has(i + "lab")) { + String period = wed.getString(i + "lab"); + myDatabase.execSQL("UPDATE timetable_lab SET wed = '" + period + "' WHERE id = " + i); + + Date date = df.parse("01-01-2020 " + start_time); + + assert date != null; + c.setTime(date); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + + c.add(Calendar.MINUTE, -30); + pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + } else { + myDatabase.execSQL("UPDATE timetable_lab SET wed = 'null' WHERE id = " + i); + } + + if (thu.has(i + "lab")) { + String period = thu.getString(i + "lab"); + myDatabase.execSQL("UPDATE timetable_lab SET thu = '" + period + "' WHERE id = " + i); + + Date date = df.parse("02-01-2020 " + start_time); + + assert date != null; + c.setTime(date); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + + c.add(Calendar.MINUTE, -30); + pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + } else { + myDatabase.execSQL("UPDATE timetable_lab SET thu = 'null' WHERE id = " + i); + } + + if (fri.has(i + "lab")) { + String period = fri.getString(i + "lab"); + myDatabase.execSQL("UPDATE timetable_lab SET fri = '" + period + "' WHERE id = " + i); + + Date date = df.parse("03-01-2020 " + start_time); + + assert date != null; + c.setTime(date); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + + c.add(Calendar.MINUTE, -30); + pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + } else { + myDatabase.execSQL("UPDATE timetable_lab SET fri = 'null' WHERE id = " + i); + } + + if (sat.has(i + "lab")) { + String period = sat.getString(i + "lab"); + myDatabase.execSQL("UPDATE timetable_lab SET sat = '" + period + "' WHERE id = " + i); + + Date date = df.parse("04-01-2020 " + start_time); + + assert date != null; + c.setTime(date); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + + c.add(Calendar.MINUTE, -30); + pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + } else { + myDatabase.execSQL("UPDATE timetable_lab SET sat = 'null' WHERE id = " + i); + } + + if (sun.has(i + "lab")) { + String period = sun.getString(i + "lab"); + myDatabase.execSQL("UPDATE timetable_lab SET sun = '" + period + "' WHERE id = " + i); + + Date date = df.parse("05-01-2020 " + start_time); + + assert date != null; + c.setTime(date); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + + c.add(Calendar.MINUTE, -30); + pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + } else { + myDatabase.execSQL("UPDATE timetable_lab SET sun = 'null' WHERE id = " + i); + + } + + start_time = theory.getString(i + "start"); + myDatabase.execSQL("INSERT INTO timetable_theory (id, start_time) VALUES ('" + i + "', '" + start_time + "')"); + + end_time = theory.getString(i + "end"); + myDatabase.execSQL("UPDATE timetable_theory SET end_time = '" + end_time + "' WHERE id = " + i); + + if (mon.has(i + "theory")) { + String period = mon.getString(i + "theory"); + myDatabase.execSQL("UPDATE timetable_theory SET mon = '" + period + "' WHERE id = " + i); + + Date date = df.parse("06-01-2020 " + start_time); + + assert date != null; + c.setTime(date); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + + c.add(Calendar.MINUTE, -30); + pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + } else { + myDatabase.execSQL("UPDATE timetable_theory SET mon = 'null' WHERE id = " + i); + } + + if (tue.has(i + "theory")) { + String period = tue.getString(i + "theory"); + myDatabase.execSQL("UPDATE timetable_theory SET tue = '" + period + "' WHERE id = " + i); + + Date date = df.parse("07-01-2020 " + start_time); + + assert date != null; + c.setTime(date); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + + c.add(Calendar.MINUTE, -30); + pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + } else { + myDatabase.execSQL("UPDATE timetable_theory SET tue = 'null' WHERE id = " + i); + } + + if (wed.has(i + "theory")) { + String period = wed.getString(i + "theory"); + myDatabase.execSQL("UPDATE timetable_theory SET wed = '" + period + "' WHERE id = " + i); + + Date date = df.parse("01-01-2020 " + start_time); + + assert date != null; + c.setTime(date); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + + c.add(Calendar.MINUTE, -30); + pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + } else { + myDatabase.execSQL("UPDATE timetable_theory SET wed = 'null' WHERE id = " + i); + } + + if (thu.has(i + "theory")) { + String period = thu.getString(i + "theory"); + myDatabase.execSQL("UPDATE timetable_theory SET thu = '" + period + "' WHERE id = " + i); + + Date date = df.parse("02-01-2020 " + start_time); + + assert date != null; + c.setTime(date); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + + c.add(Calendar.MINUTE, -30); + pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + } else { + myDatabase.execSQL("UPDATE timetable_theory SET thu = 'null' WHERE id = " + i); + } + + if (fri.has(i + "theory")) { + String period = fri.getString(i + "theory"); + myDatabase.execSQL("UPDATE timetable_theory SET fri = '" + period + "' WHERE id = " + i); + + Date date = df.parse("03-01-2020 " + start_time); + + assert date != null; + c.setTime(date); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + + c.add(Calendar.MINUTE, -30); + pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + } else { + myDatabase.execSQL("UPDATE timetable_theory SET fri = 'null' WHERE id = " + i); + } + + if (sat.has(i + "theory")) { + String period = sat.getString(i + "theory"); + myDatabase.execSQL("UPDATE timetable_theory SET sat = '" + period + "' WHERE id = " + i); + + Date date = df.parse("04-01-2020 " + start_time); + + assert date != null; + c.setTime(date); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + + c.add(Calendar.MINUTE, -30); + pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + } else { + myDatabase.execSQL("UPDATE timetable_theory SET sat = 'null' WHERE id = " + i); + } + + if (sun.has(i + "theory")) { + String period = sun.getString(i + "theory"); + myDatabase.execSQL("UPDATE timetable_theory SET sun = '" + period + "' WHERE id = " + i); + + Date date = df.parse("05-01-2020 " + start_time); + + assert date != null; + c.setTime(date); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + + c.add(Calendar.MINUTE, -30); + pendingIntent = PendingIntent.getBroadcast(context, j, notificationIntent, 0); + alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent); + ++j; + } else { + myDatabase.execSQL("UPDATE timetable_theory SET sun = 'null' WHERE id = " + i); + } + } + + sharedPreferences.edit().putInt("alarmCount", j).apply(); + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + downloadFaculty(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + error(); } } - - //sharedPreferences.edit().putString("alarms", Integer.toString(j)).apply(); - downloadFaculty(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + }).start(); } } }); @@ -933,46 +937,60 @@ public void downloadFaculty() { "return obj;" + "})();", new ValueCallback() { @Override - public void onReceiveValue(String obj) { + public void onReceiveValue(final String obj) { String temp = obj.substring(1, obj.length() - 1); if (obj.equals("null")) { - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } else if (temp.equals("")) { - try { - myDatabase.execSQL("DROP TABLE IF EXISTS faculty"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS faculty (id INT(3) PRIMARY KEY, course VARCHAR, faculty VARCHAR)"); - - downloadProctor(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + new Thread(new Runnable() { + @Override + public void run() { + try { + myDatabase.execSQL("DROP TABLE IF EXISTS faculty"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS faculty (id INT(3) PRIMARY KEY, course VARCHAR, faculty VARCHAR)"); + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + downloadProctor(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + error(); + } + } + }).start(); } else { - try { - JSONObject myObj = new JSONObject(obj); - - myDatabase.execSQL("DROP TABLE IF EXISTS faculty"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS faculty (id INT(3) PRIMARY KEY, course VARCHAR, faculty VARCHAR)"); - - for (int i = 0; i < myObj.length(); ++i) { - JSONObject tempObj = new JSONObject(myObj.getString(Integer.toString(i))); - String course = tempObj.getString("course"); - String faculty = tempObj.getString("faculty"); - - myDatabase.execSQL("INSERT INTO faculty (course, faculty) VALUES('" + course + "', '" + faculty + "')"); + new Thread(new Runnable() { + @Override + public void run() { + try { + JSONObject myObj = new JSONObject(obj); + + myDatabase.execSQL("DROP TABLE IF EXISTS faculty"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS faculty (id INT(3) PRIMARY KEY, course VARCHAR, faculty VARCHAR)"); + + for (int i = 0; i < myObj.length(); ++i) { + JSONObject tempObj = new JSONObject(myObj.getString(Integer.toString(i))); + String course = tempObj.getString("course"); + String faculty = tempObj.getString("faculty"); + + myDatabase.execSQL("INSERT INTO faculty (course, faculty) VALUES('" + course + "', '" + faculty + "')"); + } + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + downloadProctor(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + error(); + } } - - downloadProctor(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + }).start(); } } }); @@ -1012,47 +1030,61 @@ public void downloadProctor() { "return obj;" + "})();", new ValueCallback() { @Override - public void onReceiveValue(String obj) { + public void onReceiveValue(final String obj) { String temp = obj.substring(1, obj.length() - 1); if (obj.equals("null")) { - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } else if (temp.equals("unavailable") || temp.equals("")) { - try { - myDatabase.execSQL("DROP TABLE IF EXISTS proctor"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS proctor (id INT(3) PRIMARY KEY, column1 VARCHAR, column2 VARCHAR)"); - - downloadDeanHOD(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + new Thread(new Runnable() { + @Override + public void run() { + try { + myDatabase.execSQL("DROP TABLE IF EXISTS proctor"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS proctor (id INT(3) PRIMARY KEY, column1 VARCHAR, column2 VARCHAR)"); + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + downloadDeanHOD(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + error(); + } + } + }).start(); } else { - try { - JSONObject myObj = new JSONObject(obj); - - myDatabase.execSQL("DROP TABLE IF EXISTS proctor"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS proctor (id INT(3) PRIMARY KEY, column1 VARCHAR, column2 VARCHAR)"); - - Iterator keys = myObj.keys(); - - while (keys.hasNext()) { - String key = (String) keys.next(); - String value = myObj.getString(key); - - myDatabase.execSQL("INSERT INTO proctor (column1, column2) VALUES('" + key + "', '" + value + "')"); + new Thread(new Runnable() { + @Override + public void run() { + try { + JSONObject myObj = new JSONObject(obj); + + myDatabase.execSQL("DROP TABLE IF EXISTS proctor"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS proctor (id INT(3) PRIMARY KEY, column1 VARCHAR, column2 VARCHAR)"); + + Iterator keys = myObj.keys(); + + while (keys.hasNext()) { + String key = (String) keys.next(); + String value = myObj.getString(key); + + myDatabase.execSQL("INSERT INTO proctor (column1, column2) VALUES('" + key + "', '" + value + "')"); + } + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + downloadDeanHOD(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + error(); + } } - - downloadDeanHOD(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + }).start(); } } }); @@ -1125,64 +1157,78 @@ public void downloadDeanHOD() { "return obj;" + "})();", new ValueCallback() { @Override - public void onReceiveValue(String obj) { + public void onReceiveValue(final String obj) { String temp = obj.substring(1, obj.length() - 1); if (obj.equals("null")) { - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } else if (temp.equals("unavailable") || temp.equals("")) { - try { - myDatabase.execSQL("DROP TABLE IF EXISTS dean"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS dean (id INT(3) PRIMARY KEY, column1 VARCHAR, column2 VARCHAR)"); - - myDatabase.execSQL("DROP TABLE IF EXISTS hod"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS hod (id INT(3) PRIMARY KEY, column1 VARCHAR, column2 VARCHAR)"); - - openAttendance(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } - } else { - try { - JSONObject myObj = new JSONObject(obj); - JSONObject dean = new JSONObject(myObj.getString("dean")); - JSONObject hod = new JSONObject(myObj.getString("hod")); - - myDatabase.execSQL("DROP TABLE IF EXISTS dean"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS dean (id INT(3) PRIMARY KEY, column1 VARCHAR, column2 VARCHAR)"); - - myDatabase.execSQL("DROP TABLE IF EXISTS hod"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS hod (id INT(3) PRIMARY KEY, column1 VARCHAR, column2 VARCHAR)"); - - Iterator keys = dean.keys(); - - while (keys.hasNext()) { - String key = (String) keys.next(); - String value = dean.getString(key); - - myDatabase.execSQL("INSERT INTO dean (column1, column2) VALUES('" + key + "', '" + value + "')"); + new Thread(new Runnable() { + @Override + public void run() { + try { + myDatabase.execSQL("DROP TABLE IF EXISTS dean"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS dean (id INT(3) PRIMARY KEY, column1 VARCHAR, column2 VARCHAR)"); + + myDatabase.execSQL("DROP TABLE IF EXISTS hod"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS hod (id INT(3) PRIMARY KEY, column1 VARCHAR, column2 VARCHAR)"); + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + openAttendance(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + error(); + } } - - keys = hod.keys(); - - while (keys.hasNext()) { - String key = (String) keys.next(); - String value = hod.getString(key); - - myDatabase.execSQL("INSERT INTO hod (column1, column2) VALUES('" + key + "', '" + value + "')"); + }).start(); + } else { + new Thread(new Runnable() { + @Override + public void run() { + try { + JSONObject myObj = new JSONObject(obj); + JSONObject dean = new JSONObject(myObj.getString("dean")); + JSONObject hod = new JSONObject(myObj.getString("hod")); + + myDatabase.execSQL("DROP TABLE IF EXISTS dean"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS dean (id INT(3) PRIMARY KEY, column1 VARCHAR, column2 VARCHAR)"); + + myDatabase.execSQL("DROP TABLE IF EXISTS hod"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS hod (id INT(3) PRIMARY KEY, column1 VARCHAR, column2 VARCHAR)"); + + Iterator keys = dean.keys(); + + while (keys.hasNext()) { + String key = (String) keys.next(); + String value = dean.getString(key); + + myDatabase.execSQL("INSERT INTO dean (column1, column2) VALUES('" + key + "', '" + value + "')"); + } + + keys = hod.keys(); + + while (keys.hasNext()) { + String key = (String) keys.next(); + String value = hod.getString(key); + + myDatabase.execSQL("INSERT INTO hod (column1, column2) VALUES('" + key + "', '" + value + "')"); + } + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + openAttendance(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + error(); + } } - - openAttendance(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + }).start(); } } }); @@ -1216,9 +1262,7 @@ public void onReceiveValue(String value) { if (value.equals("true")) { downloadAttendance(); } else { - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } } }); @@ -1303,49 +1347,62 @@ public void downloadAttendance() { "return obj;" + "})();", new ValueCallback() { @Override - public void onReceiveValue(String obj) { + public void onReceiveValue(final String obj) { String temp = obj.substring(1, obj.length() - 1); if (obj.equals("null")) { - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } else if (temp.equals("unavailable") || temp.equals("")) { - try { - myDatabase.execSQL("DROP TABLE IF EXISTS attendance"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS attendance (id INT(3) PRIMARY KEY, course VARCHAR, type VARCHAR, attended VARCHAR, total VARCHAR, percent VARCHAR)"); - - downloadExams(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + new Thread(new Runnable() { + @Override + public void run() { + try { + myDatabase.execSQL("DROP TABLE IF EXISTS attendance"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS attendance (id INT(3) PRIMARY KEY, course VARCHAR, type VARCHAR, attended VARCHAR, total VARCHAR, percent VARCHAR)"); + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + downloadExams(); + } + }); + } catch (Exception e) { + error(); + } + } + }).start(); } else { - try { - JSONObject myObj = new JSONObject(obj); - - myDatabase.execSQL("DROP TABLE IF EXISTS attendance"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS attendance (id INT(3) PRIMARY KEY, course VARCHAR, type VARCHAR, attended VARCHAR, total VARCHAR, percent VARCHAR)"); - - for (int i = 0; i < myObj.length(); ++i) { - JSONObject tempObj = new JSONObject(myObj.getString(Integer.toString(i))); - String course = tempObj.getString("course"); - String type = tempObj.getString("type"); - String attended = tempObj.getString("attended"); - String total = tempObj.getString("total"); - String percent = tempObj.getString("percent"); - - myDatabase.execSQL("INSERT INTO attendance (course, type, attended, total, percent) VALUES('" + course + "', '" + type + "', '" + attended + "', '" + total + "', '" + percent + "')"); + new Thread(new Runnable() { + @Override + public void run() { + try { + JSONObject myObj = new JSONObject(obj); + + myDatabase.execSQL("DROP TABLE IF EXISTS attendance"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS attendance (id INT(3) PRIMARY KEY, course VARCHAR, type VARCHAR, attended VARCHAR, total VARCHAR, percent VARCHAR)"); + + for (int i = 0; i < myObj.length(); ++i) { + JSONObject tempObj = new JSONObject(myObj.getString(Integer.toString(i))); + String course = tempObj.getString("course"); + String type = tempObj.getString("type"); + String attended = tempObj.getString("attended"); + String total = tempObj.getString("total"); + String percent = tempObj.getString("percent"); + + myDatabase.execSQL("INSERT INTO attendance (course, type, attended, total, percent) VALUES('" + course + "', '" + type + "', '" + attended + "', '" + total + "', '" + percent + "')"); + } + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + downloadExams(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + error(); + } } - - downloadExams(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + }).start(); } } }); @@ -1417,49 +1474,63 @@ public void downloadExams() { "return obj;" + "})();", new ValueCallback() { @Override - public void onReceiveValue(String obj) { + public void onReceiveValue(final String obj) { String temp = obj.substring(1, obj.length() - 1); if (obj.equals("null")) { - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } else if (temp.equals("nothing") || temp.equals("")) { - try { - myDatabase.execSQL("DROP TABLE IF EXISTS exams"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS exams (id INT(3) PRIMARY KEY, course VARCHAR, date VARCHAR, time VARCHAR)"); - - downloadReceipts(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + new Thread(new Runnable() { + @Override + public void run() { + try { + myDatabase.execSQL("DROP TABLE IF EXISTS exams"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS exams (id INT(3) PRIMARY KEY, course VARCHAR, date VARCHAR, time VARCHAR)"); + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + downloadReceipts(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + error(); + } + } + }).start(); } else { - try { - JSONObject myObj = new JSONObject(obj); - - myDatabase.execSQL("DROP TABLE IF EXISTS exams"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS exams (id INT(3) PRIMARY KEY, course VARCHAR, date VARCHAR, start_time VARCHAR, end_time VARCHAR)"); - - for (int i = 0; i < myObj.length(); ++i) { - JSONObject tempObj = new JSONObject(myObj.getString(Integer.toString(i))); - String course = tempObj.getString("course"); - String date = tempObj.getString("date").toUpperCase(); - String[] time = tempObj.getString("time").split("-"); - String startTime = time[0].trim(); - String endTime = time[1].trim(); - - myDatabase.execSQL("INSERT INTO exams (course, date, start_time, end_time) VALUES('" + course + "', '" + date + "', '" + startTime + "', '" + endTime + "')"); + new Thread(new Runnable() { + @Override + public void run() { + try { + JSONObject myObj = new JSONObject(obj); + + myDatabase.execSQL("DROP TABLE IF EXISTS exams"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS exams (id INT(3) PRIMARY KEY, course VARCHAR, date VARCHAR, start_time VARCHAR, end_time VARCHAR)"); + + for (int i = 0; i < myObj.length(); ++i) { + JSONObject tempObj = new JSONObject(myObj.getString(Integer.toString(i))); + String course = tempObj.getString("course"); + String date = tempObj.getString("date").toUpperCase(); + String[] time = tempObj.getString("time").split("-"); + String startTime = time[0].trim(); + String endTime = time[1].trim(); + + myDatabase.execSQL("INSERT INTO exams (course, date, start_time, end_time) VALUES('" + course + "', '" + date + "', '" + startTime + "', '" + endTime + "')"); + } + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + downloadReceipts(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + error(); + } } - - downloadReceipts(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + }).start(); } } }); @@ -1517,47 +1588,61 @@ public void downloadReceipts() { "return obj;" + "})();", new ValueCallback() { @Override - public void onReceiveValue(String obj) { + public void onReceiveValue(final String obj) { String temp = obj.substring(1, obj.length() - 1); if (obj.equals("null")) { - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } else if (temp.equals("")) { - try { - myDatabase.execSQL("DROP TABLE IF EXISTS receipts"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS receipts (id INT(3) PRIMARY KEY, receipt VARCHAR, date VARCHAR, amount VARCHAR)"); - - downloadMessages(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + new Thread(new Runnable() { + @Override + public void run() { + try { + myDatabase.execSQL("DROP TABLE IF EXISTS receipts"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS receipts (id INT(3) PRIMARY KEY, receipt VARCHAR, date VARCHAR, amount VARCHAR)"); + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + downloadMessages(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + error(); + } + } + }).start(); } else { - try { - JSONObject myObj = new JSONObject(obj); - - myDatabase.execSQL("DROP TABLE IF EXISTS receipts"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS receipts (id INT(3) PRIMARY KEY, receipt VARCHAR, date VARCHAR, amount VARCHAR)"); - - for (int i = 0; i < myObj.length(); ++i) { - JSONObject tempObj = new JSONObject(myObj.getString(Integer.toString(i))); - String receipt = tempObj.getString("receipt"); - String date = tempObj.getString("date").toUpperCase(); - String amount = tempObj.getString("amount"); - - myDatabase.execSQL("INSERT INTO receipts (receipt, date, amount) VALUES('" + receipt + "', '" + date + "', '" + amount + "')"); + new Thread(new Runnable() { + @Override + public void run() { + try { + JSONObject myObj = new JSONObject(obj); + + myDatabase.execSQL("DROP TABLE IF EXISTS receipts"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS receipts (id INT(3) PRIMARY KEY, receipt VARCHAR, date VARCHAR, amount VARCHAR)"); + + for (int i = 0; i < myObj.length(); ++i) { + JSONObject tempObj = new JSONObject(myObj.getString(Integer.toString(i))); + String receipt = tempObj.getString("receipt"); + String date = tempObj.getString("date").toUpperCase(); + String amount = tempObj.getString("amount"); + + myDatabase.execSQL("INSERT INTO receipts (receipt, date, amount) VALUES('" + receipt + "', '" + date + "', '" + amount + "')"); + } + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + downloadMessages(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + error(); + } } - - downloadMessages(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + }).start(); } } }); @@ -1594,38 +1679,54 @@ public void onReceiveValue(String value) { /* Dropping and recreating an empty table */ - try { - myDatabase.execSQL("DROP TABLE IF EXISTS messages"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS messages (id INT(3) PRIMARY KEY, faculty VARCHAR, time VARCHAR, message VARCHAR)"); - - downloadSpotlight(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + new Thread(new Runnable() { + @Override + public void run() { + try { + myDatabase.execSQL("DROP TABLE IF EXISTS messages"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS messages (id INT(3) PRIMARY KEY, faculty VARCHAR, time VARCHAR, message VARCHAR)"); + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + downloadSpotlight(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); + isOpened = false; + reloadPage(); + } + } + }).start(); } else if (temp.equals("new")) { /* Dropping, recreating and adding announcements */ - try { - myDatabase.execSQL("DROP TABLE IF EXISTS messages"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS messages (id INT(3) PRIMARY KEY, faculty VARCHAR, time VARCHAR, message VARCHAR)"); - - myDatabase.execSQL("INSERT INTO messages (faculty, time, message) VALUES('null', 'null', 'null')"); //To be changed with the actual announcements - - downloadSpotlight(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + new Thread(new Runnable() { + @Override + public void run() { + try { + myDatabase.execSQL("DROP TABLE IF EXISTS messages"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS messages (id INT(3) PRIMARY KEY, faculty VARCHAR, time VARCHAR, message VARCHAR)"); + + myDatabase.execSQL("INSERT INTO messages (faculty, time, message) VALUES('null', 'null', 'null')"); //To be changed with the actual announcements + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + downloadSpotlight(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + error(); + } + } + }).start(); } else { - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } } }); @@ -1666,55 +1767,69 @@ public void downloadSpotlight() { "return obj;" + "})();", new ValueCallback() { @Override - public void onReceiveValue(String obj) { + public void onReceiveValue(final String obj) { String temp = obj.substring(1, obj.length() - 1); if (obj.equals("null")) { - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); + error(); } else if (temp.equals("nothing") || temp.equals("")) { /* Dropping and recreating an empty table */ - try { - myDatabase.execSQL("DROP TABLE IF EXISTS spotlight"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS spotlight (id INT(3) PRIMARY KEY, category VARCHAR, announcement VARCHAR)"); - - finishUp(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + new Thread(new Runnable() { + @Override + public void run() { + try { + myDatabase.execSQL("DROP TABLE IF EXISTS spotlight"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS spotlight (id INT(3) PRIMARY KEY, category VARCHAR, announcement VARCHAR)"); + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + finishUp(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + error(); + } + } + }).start(); } else { /* Dropping, recreating and adding announcements */ - try { - JSONObject myObj = new JSONObject(obj); - - myDatabase.execSQL("DROP TABLE IF EXISTS spotlight"); - myDatabase.execSQL("CREATE TABLE IF NOT EXISTS spotlight (id INT(3) PRIMARY KEY, category VARCHAR, announcement VARCHAR)"); - - Iterator keys = myObj.keys(); - - while (keys.hasNext()) { - String key = (String) keys.next(); - JSONObject tempObj = new JSONObject(myObj.getString(key)); - - for (int i = 0; i < tempObj.length(); ++i) { - myDatabase.execSQL("INSERT INTO spotlight (category, announcement) VALUES('" + key + "', '" + tempObj.getString(Integer.toString(i)) + "')"); + new Thread(new Runnable() { + @Override + public void run() { + try { + JSONObject myObj = new JSONObject(obj); + + myDatabase.execSQL("DROP TABLE IF EXISTS spotlight"); + myDatabase.execSQL("CREATE TABLE IF NOT EXISTS spotlight (id INT(3) PRIMARY KEY, category VARCHAR, announcement VARCHAR)"); + + Iterator keys = myObj.keys(); + + while (keys.hasNext()) { + String key = (String) keys.next(); + JSONObject tempObj = new JSONObject(myObj.getString(key)); + + for (int i = 0; i < tempObj.length(); ++i) { + myDatabase.execSQL("INSERT INTO spotlight (category, announcement) VALUES('" + key + "', '" + tempObj.getString(Integer.toString(i)) + "')"); + } + } + + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + finishUp(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + error(); } } - - finishUp(); - } catch (Exception e) { - e.printStackTrace(); - Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); - isOpened = false; - reloadPage(); - } + }).start(); } } }); @@ -1752,4 +1867,15 @@ public void hideLayouts() { captchaLayout.setVisibility(View.INVISIBLE); semesterLayout.setVisibility(View.INVISIBLE); } + + public void error() { + ((Activity) context).runOnUiThread(new Runnable() { + @Override + public void run() { + Toast.makeText(context, "Sorry, something went wrong. Please try again.", Toast.LENGTH_LONG).show(); + isOpened = false; + reloadPage(); + } + }); + } }