Add account database

This commit is contained in:
RicomenPL 2017-07-23 17:02:16 +02:00
parent 5c0ee06302
commit 170b8a194b
10 changed files with 318 additions and 36 deletions

View File

@ -4,9 +4,9 @@ package io.github.wulkanowy.activity.dashboard;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.support.v4.app.FragmentTransaction;
import io.github.wulkanowy.R;
import io.github.wulkanowy.activity.dashboard.attendance.AttendanceFragment;

View File

@ -3,6 +3,7 @@ package io.github.wulkanowy.activity.main;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.SQLException;
import android.os.AsyncTask;
import android.widget.Toast;
@ -13,7 +14,12 @@ import java.io.ObjectOutputStream;
import io.github.wulkanowy.R;
import io.github.wulkanowy.activity.dashboard.DashboardActivity;
import io.github.wulkanowy.api.Cookies;
import io.github.wulkanowy.api.login.*;
import io.github.wulkanowy.api.login.AccountPermissionException;
import io.github.wulkanowy.api.login.BadCredentialsException;
import io.github.wulkanowy.api.login.Login;
import io.github.wulkanowy.api.login.LoginErrorException;
import io.github.wulkanowy.database.accounts.AccountData;
import io.github.wulkanowy.database.accounts.DatabaseAccount;
public class LoginTask extends AsyncTask<String, Integer, Integer> {
@ -21,9 +27,12 @@ public class LoginTask extends AsyncTask<String, Integer, Integer> {
private ProgressDialog progress;
public LoginTask(Activity context) {
private boolean save;
public LoginTask(Activity context, boolean save) {
activity = context;
progress = new ProgressDialog(activity);
this.save = save;
}
@Override
@ -61,6 +70,25 @@ public class LoginTask extends AsyncTask<String, Integer, Integer> {
return R.string.login_cookies_save_failed;
}
if (save) {
AccountData accountData = new AccountData()
.setName("")
.setEmail(credentials[0])
.setPassword(credentials[1])
.setCounty(credentials[2]);
DatabaseAccount databaseAccount = new DatabaseAccount(activity);
try{
databaseAccount.open();
databaseAccount.put(accountData);
databaseAccount.close();
}
catch (SQLException e){
return R.string.SQLite_ioError_text;
}
}
//Map<String, String> cookiesList = login.getJar();
return R.string.login_accepted;

View File

@ -22,8 +22,6 @@ public class MainActivity extends AppCompatActivity {
private float mTouchPosition;
private float mReleasePosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -75,7 +73,7 @@ public class MainActivity extends AppCompatActivity {
}
if (!email.isEmpty() && !password.isEmpty() && !county.isEmpty()) {
new LoginTask(this).execute(email, password, county);
new LoginTask(this,true).execute(email, password, county);
} else {
Toast.makeText(this, R.string.data_text, Toast.LENGTH_SHORT).show();
}

View File

@ -2,9 +2,8 @@ package io.github.wulkanowy.activity.started;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.SQLException;
import android.os.AsyncTask;
import android.widget.Toast;
@ -16,6 +15,8 @@ import java.net.SocketAddress;
import io.github.wulkanowy.R;
import io.github.wulkanowy.activity.main.LoginTask;
import io.github.wulkanowy.activity.main.MainActivity;
import io.github.wulkanowy.database.accounts.AccountData;
import io.github.wulkanowy.database.accounts.DatabaseAccount;
public class LoadingTask extends AsyncTask<Void, Void, Void> {
@ -23,7 +24,7 @@ import io.github.wulkanowy.activity.main.MainActivity;
private boolean isOnline;
private final boolean SAVE_DATA = false;
private final boolean SAVE_DATA = true;
LoadingTask(Activity main) {
activity = main;
@ -31,6 +32,7 @@ import io.github.wulkanowy.activity.main.MainActivity;
@Override
protected Void doInBackground(Void... voids) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
@ -45,33 +47,8 @@ import io.github.wulkanowy.activity.main.MainActivity;
protected void onPostExecute(Void result) {
if (isOnline) {
SharedPreferences sharedPreferences = activity.getSharedPreferences("io.github.wulkanowy", Context.MODE_PRIVATE);
if (SAVE_DATA) {
if (sharedPreferences.contains("wulkanowy")) {
String idAccount = sharedPreferences.getString("wulkanowy", "");
String email = sharedPreferences.getString(idAccount, "");
String password = sharedPreferences.getString("sandi" + email, "");
String county = sharedPreferences.getString("county" + email, "");
if (!email.isEmpty() || !password.isEmpty() || !county.isEmpty()) {
new LoginTask(activity).execute(email, password, county);
} else if (password.isEmpty() || email.isEmpty() || county.isEmpty()) {
Toast.makeText(activity, R.string.data_text, Toast.LENGTH_SHORT).show();
}
} else {
Intent intent = new Intent(activity, MainActivity.class);
activity.startActivity(intent);
}
}
else{
Intent intent = new Intent(activity, MainActivity.class);
activity.startActivity(intent);
}
signIn();
}
else{
Intent intent = new Intent(activity, MainActivity.class);
@ -95,4 +72,32 @@ import io.github.wulkanowy.activity.main.MainActivity;
return false;
}
}
private boolean signIn(){
if (SAVE_DATA) {
DatabaseAccount databaseAccount = new DatabaseAccount(activity);
if (databaseAccount.checkExist()) {
try {
AccountData accountData = databaseAccount.getAccount(1);
databaseAccount.close();
if (accountData != null) {
new LoginTask(activity, false).execute(accountData.getEmail(), accountData.getPassword(), accountData.getCounty());
return true;
}
}
catch (SQLException e){
Toast.makeText(activity,R.string.SQLite_ioError_text,Toast.LENGTH_LONG ).show();
}
}
}
Intent intent = new Intent(activity, MainActivity.class);
activity.startActivity(intent);
return false;
}
}

View File

@ -0,0 +1,68 @@
package io.github.wulkanowy.database.accounts;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class AccountAdapter {
private DatabaseHelper databaseHelper;
private Context context;
public SQLiteDatabase database;
private final String DATABASE_NAME = "accountdatabase.db";
private final int DATABASE_VERSION = 1;
AccountAdapter(Context context){
this.context = context;
}
public AccountAdapter open(){
databaseHelper = new DatabaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
try{
database = databaseHelper.getWritableDatabase();
} catch (SQLException e){
database = databaseHelper.getReadableDatabase();
Log.w(DatabaseHelper.DEBUG_TAG,"Database in read-only");
}
Log.d(DatabaseHelper.DEBUG_TAG,"Open database");
return this;
}
public void close(){
databaseHelper.close();
Log.d(DatabaseHelper.DEBUG_TAG,"Close database");
}
public boolean checkExist(){
open();
Log.d(DatabaseHelper.DEBUG_TAG,"Check exist table");
Cursor cursor = database.rawQuery("SELECT COUNT(*) FROM accounts", null);
if(cursor != null){
cursor.moveToFirst();
int count = cursor.getInt(0);
if(count > 0){
return true;
}
cursor.close();
close();
}
return false;
}
}

View File

@ -0,0 +1,60 @@
package io.github.wulkanowy.database.accounts;
public class AccountData {
private int id;
private String name;
private String email;
private String password;
private String county;
public AccountData setId(int id){
this.id = id;
return this;
}
public AccountData setName(String name){
this.name = name;
return this;
}
public AccountData setEmail(String email){
this.email = email;
return this;
}
public AccountData setPassword(String password){
this.password = password;
return this;
}
public AccountData setCounty(String county){
this.county = county;
return this;
}
public int getId(){
return id;
}
public String getName(){
return name;
}
public String getEmail(){
return email;
}
public String getPassword(){
return password;
}
public String getCounty(){
return county;
}
}

View File

@ -0,0 +1,82 @@
package io.github.wulkanowy.database.accounts;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;
public class DatabaseAccount extends AccountAdapter {
private String name = "name";
private String email = "email";
private String password = "password";
private String county = "county";
private String idText = "id";
private String accounts = "accounts";
public DatabaseAccount(Context context){
super(context);
}
public void put(AccountData accountData) throws SQLException{
ContentValues newAccount = new ContentValues();
newAccount.put(name,accountData.getName());
newAccount.put(email,accountData.getEmail());
newAccount.put(password,accountData.getPassword());
newAccount.put(county,accountData.getCounty());
Log.d(DatabaseHelper.DEBUG_TAG,"Put account into database");
if(!database.isReadOnly()) {
database.insertOrThrow(accounts, null, newAccount);
}
}
public long update(AccountData accountData){
ContentValues updateAccount = new ContentValues();
updateAccount.put(name,accountData.getName());
updateAccount.put(email,accountData.getEmail());
updateAccount.put(password,accountData.getPassword());
updateAccount.put(county,accountData.getCounty());
String args[] = {accountData.getId() + ""};
Log.d(DatabaseHelper.DEBUG_TAG,"Update account into database");
return database.update(accounts, updateAccount, "id=?", args);
}
public AccountData getAccount(int id) throws SQLException{
AccountData accountData = new AccountData();
String[] columns = {idText, name, email, password, county};
String args[] = {id + ""};
try {
Cursor cursor = database.query(accounts, columns, "id=?", args, null, null, null, null);
if(cursor != null) {
cursor.moveToFirst();
accountData.setId(cursor.getInt(0));
accountData.setName(cursor.getString(1));
accountData.setEmail(cursor.getString(2));
accountData.setPassword(cursor.getString(3));
accountData.setCounty(cursor.getString(4));
cursor.close();
}
}catch (SQLException e){
Log.e(DatabaseHelper.DEBUG_TAG,e.getMessage());
throw e;
}
Log.d(DatabaseHelper.DEBUG_TAG,"Extract account from base");
return accountData;
}
}

View File

@ -0,0 +1,39 @@
package io.github.wulkanowy.database.accounts;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.*;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
private final String ACCOUN_TABLE = "CREATE TABLE accounts( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, " +
"email TEXT," +
"password TEXT, " +
"county TEXT );";
private final String DROP_ACCOUNT_TABLE = "DROP TABLE IF EXISTS accounts";
public final static String DEBUG_TAG = "SQLiteAccountsDatabse";
public DatabaseHelper(Context context, String name, CursorFactory factory, int version){
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(ACCOUN_TABLE);
Log.d(DEBUG_TAG,"Create database");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL(DROP_ACCOUNT_TABLE);
onCreate(db);
Log.d(DEBUG_TAG,"Upgrade database");
}
}

View File

@ -22,4 +22,5 @@
<string name="title_settings">Ustawienia</string>
<string name="noInternet_text">Brak połączenia z internetem</string>
<string name="login_cookies_save_failed">Nie udało się zapisać sesji</string>
<string name="SQLite_ioError_text">W bazie danych wystąpił błąd. Zrestartuj aplikacje a także sprawdź iość wolnego miejsca w pamięci wewnętrznej</string>
</resources>

View File

@ -22,4 +22,5 @@
<string name="title_settings">Settings</string>
<string name="noInternet_text">No internet connection</string>
<string name="login_cookies_save_failed">Failed to save session</string>
<string name="SQLite_ioError_text">An error occurred in the database. Restart the applications and check the free space in the internal memory</string>
</resources>