From 15260b58f3ff4a67d3269b3b110f75d4cc5d5b41 Mon Sep 17 00:00:00 2001 From: RicomenPL Date: Thu, 31 Aug 2017 17:29:10 +0200 Subject: [PATCH] Add cookies database and remake grade dialog (#18) * Add cookies database * Remake dialog fragment * Add selectable to textview in dialog --- app/build.gradle | 1 + .../grades/GradesDialogFragment.java | 2 +- .../dashboard/grades/GradesFragment.java | 23 ++- .../wulkanowy/activity/main/LoginTask.java | 19 +- .../activity/started/LoadingTask.java | 2 +- .../wulkanowy/database/DatabaseAdapter.java | 11 +- .../wulkanowy/database/DatabaseHelper.java | 9 + .../database/accounts/AccountsDatabase.java | 19 -- .../database/cookies/CookiesDatabase.java | 58 ++++++ .../database/grades/GradesDatabase.java | 73 +------ app/src/main/res/layout/grades_dialog.xml | 194 +++++++++--------- 11 files changed, 208 insertions(+), 203 deletions(-) create mode 100644 app/src/main/java/io/github/wulkanowy/database/cookies/CookiesDatabase.java diff --git a/app/build.gradle b/app/build.gradle index 70905a9b6..70b2c1db3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -41,6 +41,7 @@ dependencies { compile 'org.apache.commons:commons-collections4:4.1' compile 'com.thoughtbot:expandablerecyclerview:1.3' compile 'com.android.support:cardview-v7:25.3.1' + compile 'com.google.code.gson:gson:2.8.1' testCompile 'junit:junit:4.12' testCompile 'org.mockito:mockito-core:2.9.0' diff --git a/app/src/main/java/io/github/wulkanowy/activity/dashboard/grades/GradesDialogFragment.java b/app/src/main/java/io/github/wulkanowy/activity/dashboard/grades/GradesDialogFragment.java index e03f9cb64..42f08fff4 100644 --- a/app/src/main/java/io/github/wulkanowy/activity/dashboard/grades/GradesDialogFragment.java +++ b/app/src/main/java/io/github/wulkanowy/activity/dashboard/grades/GradesDialogFragment.java @@ -36,9 +36,9 @@ public class GradesDialogFragment extends DialogFragment { TextView colorText = (TextView) view.findViewById(R.id.color_dialog_text_value); TextView okTextClick = (TextView) view.findViewById(R.id.OK_dialog); + subjectText.setText(grade.getSubject()); gradeText.setText(grade.getValue()); gradeText.setBackgroundResource(grade.getValueColor()); - subjectText.setText(grade.getSubject()); weightText.setText(grade.getWeight()); dateText.setText(grade.getDate()); colorText.setText(colorHexToColorName(grade.getColor())); diff --git a/app/src/main/java/io/github/wulkanowy/activity/dashboard/grades/GradesFragment.java b/app/src/main/java/io/github/wulkanowy/activity/dashboard/grades/GradesFragment.java index 103d9f51a..e7d356d4d 100644 --- a/app/src/main/java/io/github/wulkanowy/activity/dashboard/grades/GradesFragment.java +++ b/app/src/main/java/io/github/wulkanowy/activity/dashboard/grades/GradesFragment.java @@ -10,9 +10,11 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import java.io.FileInputStream; -import java.io.ObjectInputStream; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -24,6 +26,7 @@ import io.github.wulkanowy.api.grades.Subject; import io.github.wulkanowy.api.grades.SubjectsList; import io.github.wulkanowy.database.accounts.Account; import io.github.wulkanowy.database.accounts.AccountsDatabase; +import io.github.wulkanowy.database.cookies.CookiesDatabase; import io.github.wulkanowy.database.grades.GradesDatabase; import io.github.wulkanowy.database.subjects.SubjectsDatabase; @@ -61,7 +64,8 @@ public class GradesFragment extends Fragment { public class MarksTask extends AsyncTask { private Context mContext; - private Map loginCookies; + + private Map loginCookies = new HashMap<>(); MarksTask(Context context) { mContext = context; @@ -69,12 +73,15 @@ public class GradesFragment extends Fragment { @Override protected Void doInBackground(Void... params) { - String cookiesPath = mContext.getFilesDir().getPath() + "/cookies.txt"; - long userId = mContext.getSharedPreferences("LoginData", mContext.MODE_PRIVATE).getLong("isLogin", 0); + long userId = mContext.getSharedPreferences("LoginData", Context.MODE_PRIVATE).getLong("isLogin", 0); try { - ObjectInputStream ois = new ObjectInputStream(new FileInputStream(cookiesPath)); - loginCookies = (Map) ois.readObject(); + Gson gson = new GsonBuilder().enableComplexMapKeySerialization() + .setPrettyPrinting().create(); + CookiesDatabase cookiesDatabase = new CookiesDatabase(mContext); + cookiesDatabase.open(); + loginCookies = gson.fromJson(cookiesDatabase.getCookies(), loginCookies.getClass()); + cookiesDatabase.close(); } catch (Exception e) { e.printStackTrace(); } @@ -102,7 +109,7 @@ public class GradesFragment extends Fragment { gradesDatabase.open(); gradesDatabase.put(gradesList.getAll()); - for (Subject subject : subjectsList.getAll()) { + for (Subject subject : subjectsDatabase.getAllSubjectsNames()) { List gradeItems = gradesDatabase.getSubjectGrades(userId, SubjectsDatabase.getSubjectId(subject.getName())); if (gradeItems.size() > 0) { subjectWithGradesList.add(new SubjectWithGrades(subject.getName(), gradeItems)); diff --git a/app/src/main/java/io/github/wulkanowy/activity/main/LoginTask.java b/app/src/main/java/io/github/wulkanowy/activity/main/LoginTask.java index 83f16ce33..d27b1c84b 100644 --- a/app/src/main/java/io/github/wulkanowy/activity/main/LoginTask.java +++ b/app/src/main/java/io/github/wulkanowy/activity/main/LoginTask.java @@ -8,9 +8,10 @@ import android.database.SQLException; import android.os.AsyncTask; import android.widget.Toast; -import java.io.FileOutputStream; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + import java.io.IOException; -import java.io.ObjectOutputStream; import io.github.wulkanowy.R; import io.github.wulkanowy.activity.dashboard.DashboardActivity; @@ -24,6 +25,7 @@ import io.github.wulkanowy.api.user.BasicInformation; import io.github.wulkanowy.api.user.PersonalData; import io.github.wulkanowy.database.accounts.Account; import io.github.wulkanowy.database.accounts.AccountsDatabase; +import io.github.wulkanowy.database.cookies.CookiesDatabase; import io.github.wulkanowy.security.CryptoException; import io.github.wulkanowy.security.Safety; @@ -67,12 +69,13 @@ public class LoginTask extends AsyncTask { return R.string.login_denied_text; } try { - String cookiesPath = activity.getFilesDir().getPath() + "/cookies.txt"; - FileOutputStream out = new FileOutputStream(cookiesPath); - ObjectOutputStream outputStream = new ObjectOutputStream(out); - outputStream.writeObject(login.getCookies()); - outputStream.flush(); - } catch (IOException e) { + Gson gson = new GsonBuilder().enableComplexMapKeySerialization() + .setPrettyPrinting().create(); + CookiesDatabase cookiesDatabase = new CookiesDatabase(activity); + cookiesDatabase.open(); + cookiesDatabase.put(gson.toJson(login.getCookies())); + cookiesDatabase.close(); + } catch (SQLException e) { return R.string.login_cookies_save_failed_text; } diff --git a/app/src/main/java/io/github/wulkanowy/activity/started/LoadingTask.java b/app/src/main/java/io/github/wulkanowy/activity/started/LoadingTask.java index ac83a4b80..3b9e17d1c 100644 --- a/app/src/main/java/io/github/wulkanowy/activity/started/LoadingTask.java +++ b/app/src/main/java/io/github/wulkanowy/activity/started/LoadingTask.java @@ -76,7 +76,7 @@ public class LoadingTask extends AsyncTask { AccountsDatabase accountsDatabase = new AccountsDatabase(activity); accountsDatabase.open(); - if (accountsDatabase.checkExist("accounts", null, null)) { + if (accountsDatabase.checkExist("accounts")) { try { Account account = accountsDatabase.getAccount(activity.getSharedPreferences("LoginData", activity.MODE_PRIVATE).getLong("isLogin", 0)); accountsDatabase.close(); diff --git a/app/src/main/java/io/github/wulkanowy/database/DatabaseAdapter.java b/app/src/main/java/io/github/wulkanowy/database/DatabaseAdapter.java index 41e3b080d..88ee6298d 100644 --- a/app/src/main/java/io/github/wulkanowy/database/DatabaseAdapter.java +++ b/app/src/main/java/io/github/wulkanowy/database/DatabaseAdapter.java @@ -9,9 +9,13 @@ import android.util.Log; public class DatabaseAdapter { private final String DATABASE_NAME = "accountdatabase.db"; - private final int DATABASE_VERSION = 2; + + private final int DATABASE_VERSION = 3; + public static SQLiteDatabase database; + private DatabaseHelper databaseHelper; + public Context context; public DatabaseAdapter(Context context) { @@ -69,12 +73,17 @@ public class DatabaseAdapter { return false; } + public boolean checkExist(String tableName) { + return checkExist(tableName, null, null); + } + public void deleteAndCreate(String tableName) { database.execSQL(databaseHelper.DROP_TABLE + tableName); database.execSQL(databaseHelper.SUBJECT_TABLE); database.execSQL(databaseHelper.ACCOUNT_TABLE); database.execSQL(databaseHelper.GRADE_TABLE); + database.execSQL(databaseHelper.COOKIES_TABLE); Log.d(DatabaseHelper.DEBUG_TAG, "Recreate table " + tableName); diff --git a/app/src/main/java/io/github/wulkanowy/database/DatabaseHelper.java b/app/src/main/java/io/github/wulkanowy/database/DatabaseHelper.java index 494bad2d9..8c8f02548 100644 --- a/app/src/main/java/io/github/wulkanowy/database/DatabaseHelper.java +++ b/app/src/main/java/io/github/wulkanowy/database/DatabaseHelper.java @@ -9,12 +9,14 @@ import android.util.Log; public class DatabaseHelper extends SQLiteOpenHelper { public final static String DEBUG_TAG = "SQLiteWulkanowyDatabase"; + public final String ACCOUNT_TABLE = "CREATE TABLE IF NOT EXISTS accounts( " + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT, " + "email TEXT," + "password TEXT, " + "county TEXT );"; + public final String SUBJECT_TABLE = "CREATE TABLE IF NOT EXISTS subjects( " + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT, " + @@ -22,6 +24,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { "finalRating1 TEXT, " + "predictedRating2 TEXT, " + "finalRating2 TEXT );"; + public final String GRADE_TABLE = "CREATE TABLE IF NOT EXISTS grades( " + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "userID INTEGER, " + @@ -37,6 +40,10 @@ public class DatabaseHelper extends SQLiteOpenHelper { "semester INTEGER, " + "isNew INTEGER );"; + public final String COOKIES_TABLE = "CREATE TABLE IF NOT EXISTS cookies( " + + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + + "cookies TEXT );"; + public final String DROP_TABLE = "DROP TABLE IF EXISTS "; public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { @@ -48,6 +55,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { db.execSQL(ACCOUNT_TABLE); db.execSQL(SUBJECT_TABLE); db.execSQL(GRADE_TABLE); + db.execSQL(COOKIES_TABLE); Log.d(DEBUG_TAG, "Create database"); } @@ -56,6 +64,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { db.execSQL(DROP_TABLE + "accounts"); db.execSQL(DROP_TABLE + "subjects"); db.execSQL(DROP_TABLE + "grades"); + db.execSQL(DROP_TABLE + "cookies"); onCreate(db); Log.d(DEBUG_TAG, "Database upgrade from ver." + oldVersion + " to ver." + newVersion); } diff --git a/app/src/main/java/io/github/wulkanowy/database/accounts/AccountsDatabase.java b/app/src/main/java/io/github/wulkanowy/database/accounts/AccountsDatabase.java index adf2a5507..5cb8465c4 100644 --- a/app/src/main/java/io/github/wulkanowy/database/accounts/AccountsDatabase.java +++ b/app/src/main/java/io/github/wulkanowy/database/accounts/AccountsDatabase.java @@ -41,25 +41,6 @@ public class AccountsDatabase extends DatabaseAdapter { throw new SQLException("Attempt to write on read-only database"); } - public long update(Account account) throws SQLException { - - ContentValues updateAccount = new ContentValues(); - updateAccount.put(name, account.getName()); - updateAccount.put(email, account.getEmail()); - updateAccount.put(password, account.getPassword()); - updateAccount.put(county, account.getCounty()); - String args[] = {account.getId() + ""}; - - if (!database.isReadOnly()) { - long updateId = database.update(accounts, updateAccount, "id=?", args); - Log.d(DatabaseHelper.DEBUG_TAG, "Update account " + updateId + " into database"); - return updateId; - } - - Log.e(DatabaseHelper.DEBUG_TAG, "Attempt to write on read-only database"); - throw new SQLException("Attempt to write on read-only database"); - } - public Account getAccount(long id) throws SQLException { Account account = new Account(); diff --git a/app/src/main/java/io/github/wulkanowy/database/cookies/CookiesDatabase.java b/app/src/main/java/io/github/wulkanowy/database/cookies/CookiesDatabase.java new file mode 100644 index 000000000..6cd40b060 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/database/cookies/CookiesDatabase.java @@ -0,0 +1,58 @@ +package io.github.wulkanowy.database.cookies; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.SQLException; +import android.util.Log; + +import io.github.wulkanowy.database.DatabaseAdapter; +import io.github.wulkanowy.database.DatabaseHelper; + +public class CookiesDatabase extends DatabaseAdapter { + + private String cookies = "cookies"; + + public CookiesDatabase(Context context) { + super(context); + } + + public long put(String serializableCookiesMap) { + + ContentValues newCookie = new ContentValues(); + newCookie.put(cookies, serializableCookiesMap); + + if (!database.isReadOnly()) { + if (!checkExist(cookies)) { + long newId = database.insertOrThrow(cookies, null, newCookie); + Log.d(DatabaseHelper.DEBUG_TAG, "Put cookies into database"); + return newId; + } else { + deleteAndCreate(cookies); + long newId = database.insertOrThrow(cookies, null, newCookie); + Log.d(DatabaseHelper.DEBUG_TAG, "Put cookies into database"); + return newId; + } + } else { + Log.e(DatabaseHelper.DEBUG_TAG, "Attempt to write on read-only database"); + throw new SQLException("Attempt to write on read-only database"); + } + } + + public String getCookies() { + + String exec = "SELECT " + cookies + " FROM " + cookies; + + Cursor cursor = database.rawQuery(exec, null); + + cursor.moveToFirst(); + + String cookie = cursor.getString(0); + + cursor.close(); + + Log.d(DatabaseHelper.DEBUG_TAG, "Extract cookies from database"); + + return cookie; + } +} diff --git a/app/src/main/java/io/github/wulkanowy/database/grades/GradesDatabase.java b/app/src/main/java/io/github/wulkanowy/database/grades/GradesDatabase.java index 19f311b14..c5b9479d9 100644 --- a/app/src/main/java/io/github/wulkanowy/database/grades/GradesDatabase.java +++ b/app/src/main/java/io/github/wulkanowy/database/grades/GradesDatabase.java @@ -4,7 +4,6 @@ package io.github.wulkanowy.database.grades; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; -import android.database.CursorIndexOutOfBoundsException; import android.database.SQLException; import android.util.Log; @@ -20,7 +19,6 @@ import io.github.wulkanowy.database.subjects.SubjectsDatabase; public class GradesDatabase extends DatabaseAdapter { - private String idText = "id"; private String userIdText = "userID"; private String subjectIdText = "subjectID"; private String subject = "subject"; @@ -70,7 +68,7 @@ public class GradesDatabase extends DatabaseAdapter { List newIdList = new ArrayList<>(); List preparedList; - if (checkExist(grades, null, null)) { + if (checkExist(grades)) { preparedList = DatabaseComparer.compareGradesLists(gradeList, getAllUserGrades()); deleteAndCreate(grades); } else { @@ -84,75 +82,6 @@ public class GradesDatabase extends DatabaseAdapter { return newIdList; } - public long update(Grade grade) throws SQLException { - - ContentValues updateGrade = new ContentValues(); - updateGrade.put(userIdText, grade.getUserID()); - updateGrade.put(subjectIdText, grade.getSubjectID()); - updateGrade.put(subject, grade.getSubject()); - updateGrade.put(value, grade.getValue()); - updateGrade.put(color, grade.getColor()); - updateGrade.put(symbol, grade.getSymbol()); - updateGrade.put(description, grade.getDescription()); - updateGrade.put(weight, grade.getWeight()); - updateGrade.put(date, grade.getDate()); - updateGrade.put(teacher, grade.getTeacher()); - updateGrade.put(semester, grade.getSemester()); - updateGrade.put(isNew, grade.isNew() ? 1 : 0); - String args[] = {grade.getId() + ""}; - - if (!database.isReadOnly()) { - long updateId = database.update(grades, updateGrade, "id=?", args); - Log.d(DatabaseHelper.DEBUG_TAG, "Update grade " + updateId + " into database"); - return updateId; - } - - Log.e(DatabaseHelper.DEBUG_TAG, "Attempt to write on read-only database"); - throw new SQLException("Attempt to write on read-only database"); - - } - - public Grade getGrade(long id) throws SQLException { - - Grade grade = new Grade(); - - String[] columns = {idText, userIdText, subjectIdText, value, color, description, weight, date, teacher}; - String args[] = {id + ""}; - - try { - Cursor cursor = database.query(grades, columns, "id=?", args, null, null, null, null); - if (cursor != null) { - cursor.moveToFirst(); - grade.setId(cursor.getInt(0)); - grade.setUserID(cursor.getInt(1)); - grade.setSubjectID(cursor.getInt(2)); - grade.setSubject(cursor.getString(3)); - grade.setValue(cursor.getString(4)); - grade.setColor(cursor.getString(5)); - grade.setSymbol(cursor.getString(6)); - grade.setDescription(cursor.getString(7)); - grade.setWeight(cursor.getString(8)); - grade.setDate(cursor.getString(9)); - grade.setTeacher(cursor.getString(10)); - grade.setSemester(cursor.getString(11)); - grade.setIsNew(cursor.getInt(12) != 0); - cursor.close(); - } - } catch (SQLException e) { - - Log.e(DatabaseHelper.DEBUG_TAG, e.getMessage()); - throw e; - } catch (CursorIndexOutOfBoundsException e) { - - Log.e(DatabaseHelper.DEBUG_TAG, e.getMessage()); - throw new SQLException(e.getMessage()); - } - - Log.d(DatabaseHelper.DEBUG_TAG, "Extract grade " + id + " from database"); - - return grade; - } - public List getSubjectGrades(long userId, long subjectId) throws SQLException { String whereExec = "SELECT * FROM " + grades + " WHERE " + userIdText + "=? AND " + subjectIdText + "=?"; diff --git a/app/src/main/res/layout/grades_dialog.xml b/app/src/main/res/layout/grades_dialog.xml index 311c5131a..6b5d1e9ec 100644 --- a/app/src/main/res/layout/grades_dialog.xml +++ b/app/src/main/res/layout/grades_dialog.xml @@ -1,67 +1,67 @@ - + android:minHeight="450dp" + android:minWidth="325dp" + android:orientation="vertical"> + + android:layout_height="match_parent" + android:layout_marginTop="15dp" + android:minHeight="450dp" + android:padding="20dp" + tools:ignore="UselessParent"> + - - + android:textIsSelectable="true" + android:textSize="20sp" /> + android:layout_alignParentStart="true" + android:layout_below="@+id/subject_dialog_text_value" + android:text="@string/dialog_description_text" + android:textIsSelectable="true" + android:textSize="17sp" /> @@ -94,71 +90,82 @@ android:id="@+id/weight_dialog_text_value" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/weight_dialog_text" - android:layout_marginEnd="80dp" - android:layout_marginRight="80dp" android:layout_marginTop="3dp" android:text="@string/grades_text" + android:textIsSelectable="true" android:textSize="12sp" /> - + - + - + - + - + - - - - - + - + \ No newline at end of file