Add cookies database and remake grade dialog (#18)

* Add cookies database
* Remake dialog fragment
* Add selectable to textview in dialog
This commit is contained in:
RicomenPL 2017-08-31 17:29:10 +02:00 committed by Mikołaj Pich
parent add96d37dc
commit 15260b58f3
11 changed files with 208 additions and 203 deletions

View File

@ -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'

View File

@ -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()));

View File

@ -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<Void, Void, Void> {
private Context mContext;
private Map<String, String> loginCookies;
private Map<String, String> 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<String, String>) 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<GradeItem> gradeItems = gradesDatabase.getSubjectGrades(userId, SubjectsDatabase.getSubjectId(subject.getName()));
if (gradeItems.size() > 0) {
subjectWithGradesList.add(new SubjectWithGrades(subject.getName(), gradeItems));

View File

@ -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<String, Integer, Integer> {
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;
}

View File

@ -76,7 +76,7 @@ public class LoadingTask extends AsyncTask<Void, Void, Void> {
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();

View File

@ -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);

View File

@ -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);
}

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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<Long> newIdList = new ArrayList<>();
List<Grade> 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<GradeItem> getSubjectGrades(long userId, long subjectId) throws SQLException {
String whereExec = "SELECT * FROM " + grades + " WHERE " + userIdText + "=? AND " + subjectIdText + "=?";

View File

@ -1,67 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:minHeight="350dp"
android:minWidth="300dp"
android:orientation="vertical"
android:padding="20dp">
android:minHeight="450dp"
android:minWidth="325dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:minHeight="450dp"
android:padding="20dp"
tools:ignore="UselessParent">
<TextView
android:id="@+id/dialog_grade_text"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="end"
android:gravity="center"
android:text="@string/app_name"
android:textSize="30sp" />
<TextView
android:id="@+id/subject_dialog_text"
android:layout_width="wrap_content"
android:id="@+id/subject_dialog_text_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:text="@string/dialog_subject_text"
android:textSize="17sp" />
<TextView
android:id="@+id/subject_dialog_text_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/subject_dialog_text"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="3dp"
android:layout_gravity="start"
android:layout_marginBottom="10dp"
android:layout_marginEnd="90dp"
android:layout_marginRight="90dp"
android:gravity="center_vertical"
android:maxLines="5"
android:minHeight="80dp"
android:minLines="2"
android:text="@string/grades_text"
android:textSize="12sp" />
android:textIsSelectable="true"
android:textSize="20sp" />
<TextView
android:id="@+id/description_dialog_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dialog_description_text"
android:textSize="17sp"
android:layout_marginTop="10dp"
android:layout_below="@+id/subject_dialog_text_value"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
android:layout_alignParentStart="true"
android:layout_below="@+id/subject_dialog_text_value"
android:text="@string/dialog_description_text"
android:textIsSelectable="true"
android:textSize="17sp" />
<TextView
android:id="@+id/description_dialog_text_value"
@ -70,11 +70,9 @@
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/description_dialog_text"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="3dp"
android:maxLines="3"
android:text="@string/noDescription_text"
android:textIsSelectable="true"
android:textSize="12sp" />
<TextView
@ -84,8 +82,6 @@
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/description_dialog_text_value"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="10dp"
android:text="@string/dialog_weight_text"
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" />
</RelativeLayout>
<TextView
android:id="@+id/teacher_dialog_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/weight_dialog_text_value"
android:layout_marginTop="10dp"
android:text="@string/dialog_teacher_text"
android:textSize="17sp" />
<TextView
android:id="@+id/teacher_dialog_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/dialog_teacher_text"
android:textSize="17sp" />
<TextView
android:id="@+id/teacher_dialog_text_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/teacher_dialog_text"
android:layout_marginTop="3dp"
android:text="@string/dialog_teacher_text"
android:textIsSelectable="true"
android:textSize="12sp" />
<TextView
android:id="@+id/teacher_dialog_text_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="@string/grades_text"
android:textSize="12sp" />
<TextView
android:id="@+id/color_dialog_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/teacher_dialog_text_value"
android:layout_marginTop="10dp"
android:text="@string/dialog_color_text"
android:textSize="17sp" />
<TextView
android:id="@+id/color_dialog_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/dialog_color_text"
android:textSize="17sp" />
<TextView
android:id="@+id/color_dialog_text_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/color_dialog_text"
android:layout_marginTop="3dp"
android:text="@string/dialog_color_text"
android:textIsSelectable="true"
android:textSize="12sp" />
<TextView
android:id="@+id/color_dialog_text_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="@string/grades_text"
android:textSize="12sp" />
<TextView
android:id="@+id/date_dialog_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/color_dialog_text_value"
android:layout_marginTop="10dp"
android:text="@string/dialog_date_text"
android:textSize="17sp" />
<TextView
android:id="@+id/date_dialog_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/dialog_date_text"
android:textSize="17sp" />
<TextView
android:id="@+id/date_dialog_text_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="@string/grades_text"
android:textSize="12sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginTop="19dp">
<TextView
android:id="@+id/date_dialog_text_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/date_dialog_text"
android:layout_marginTop="3dp"
android:text="@string/dialog_date_text"
android:textIsSelectable="true"
android:textSize="12sp" />
<TextView
android:id="@+id/OK_dialog"
@ -167,8 +174,9 @@
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:gravity="center"
@ -177,4 +185,4 @@
</RelativeLayout>
</LinearLayout>
</ScrollView>
</ScrollView>