import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
-import org.joda.time.DateTime;
-
import rx.Observable;
import adullact.publicrowdfunding.PublicrowdFundingApplication;
import adullact.publicrowdfunding.model.exception.NoAccountExistsInLocal;
import adullact.publicrowdfunding.model.local.cache.Cache;
+import adullact.publicrowdfunding.model.local.cache.Local;
+import adullact.publicrowdfunding.model.local.callback.HoldToDo;
import adullact.publicrowdfunding.model.local.callback.WhatToDo;
-import adullact.publicrowdfunding.model.local.utilities.Utility;
import adullact.publicrowdfunding.model.server.entities.RowAffected;
import adullact.publicrowdfunding.model.server.entities.ServerAccount;
import adullact.publicrowdfunding.model.server.entities.Service;
import adullact.publicrowdfunding.model.server.entities.SimpleServerResponse;
-import android.content.Context;
import android.content.SharedPreferences;
/**
- * Created by Ferrand on 16/07/2014.
+ * @author Ferrand and Nelaupe
*/
public class Account extends Resource<Account, ServerAccount, ServerAccount> {
/* ---- Singleton ---- */
private static Account m_own = null;
private void initialize() throws NoAccountExistsInLocal {
- SharedPreferences sharedPreferences = m_context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
- if(!sharedPreferences.contains(KEY_USERNAME) || !sharedPreferences.contains(KEY_USERNAME) || !sharedPreferences.contains(KEY_USERNAME)) {
- {
- m_own = null;
- throw new NoAccountExistsInLocal();
- }
+ SharedPreferences sharedPreferences = PublicrowdFundingApplication.sharedPreferences();
+ if(!sharedPreferences.contains(KEY_USERNAME) || !sharedPreferences.contains(KEY_PASSWORD) || !sharedPreferences.contains(KEY_PSEUDO) || !sharedPreferences.contains(KEY_ADMIN)) {
+ m_own = null;
+ throw new NoAccountExistsInLocal();
}
m_username = sharedPreferences.getString(KEY_USERNAME, "");
- m_password = sharedPreferences.getString(KEY_PASSWORD, "");
- m_lastSync = Utility.stringToDateTime(sharedPreferences.getString(KEY_LAST_SYNC, ""));
+ m_password = decrypt("mystery", sharedPreferences.getString(KEY_PASSWORD, ""));
+ m_administrator = sharedPreferences.getBoolean(KEY_ADMIN, false);
+
+ User user = new User();
+ user.setResourceId(sharedPreferences.getString(KEY_PSEUDO, ""));
+ m_user = new Local<User>(user).useIt();
+
+ new Local<Account>(this).useIt();
}
public static Account getOwn() throws NoAccountExistsInLocal {
if(m_own == null) {
try {
- m_own = new Account(PublicrowdFundingApplication.context());
+ m_own = new Account();
m_own.initialize();
}
catch(NoAccountExistsInLocal exception) {
return m_own;
}
- private Account(Context context) {
- this.m_context = context;
- }
/* ------------------- */
/* --- Static const to store --- */
- private static final String FILE_NAME = "logs";
private static final String KEY_USERNAME = "name";
private static final String KEY_PASSWORD = "password";
- private static final String KEY_LAST_SYNC = "last sync";
+ private static final String KEY_ADMIN = "admin";
+ private static final String KEY_PSEUDO = "pseudo";
/* ----------------------------- */
/* ----- Resource ----- */
ServerAccount res = new ServerAccount();
res.username = m_username;
res.password = m_password;
- res.administrator = m_administrator;
+ res.administrator = m_administrator ? 1 : 0;
res.pseudo = m_user.getResourceId();
return res;
}
@Override
public Account makeCopyFromServer(ServerAccount serverAccount) {
- return null;
+ Account res = new Account();
+ res.m_username = serverAccount.username;
+ res.m_administrator = (serverAccount.administrator == 1);
+ res.m_anonymous = false;
+ res.m_user = new User().getCache(serverAccount.pseudo);
+
+ return res;
}
@Override
public Account syncFromServer(ServerAccount serverAccount) {
- this.m_username = serverAccount.username;
- this.m_lastSync = DateTime.now();
- this.m_administrator = serverAccount.administrator;
- this.m_anonymous = false;
- this.m_context = PublicrowdFundingApplication.context();
- this.m_user = new User().getCache(serverAccount.pseudo);
+ this.m_username = serverAccount.username;
+ this.m_administrator = (serverAccount.administrator == 1);
+ this.m_anonymous = false;
+ this.m_user = new User().getCache(serverAccount.pseudo);
- return this;
+ return this;
}
@Override
/* ---- Own data ---- */
private String m_username;
private String m_password;
- private DateTime m_lastSync;
private boolean m_administrator;
private boolean m_anonymous;
- private Context m_context;
/* ------------------ */
/* --- References --- */
public Account() {
this.m_username = null;
this.m_password = null;
- this.m_lastSync = null;
this.m_administrator = false;
this.m_anonymous = true;
- this.m_context = null;
}
public Account(String username, String password, String pseudo) {
this.m_username = username;
this.m_password = password;
this.m_user = new User().getCache(pseudo);
- this.m_lastSync = null;
this.m_administrator = false;
this.m_anonymous = false;
- this.m_context = null;
}
public void setOwn() {
m_own = this;
- }
-
- public void setLastSync(DateTime lastSync) {
- this.m_lastSync = lastSync;
+ save();
}
public boolean isAdmin() {
return m_administrator;
}
- public DateTime getLastSync() {
- return m_lastSync;
- }
-
public String getUsername() {
return m_username;
}
public Cache<User> getUser() {
return m_user;
}
-
+
public void setAdmin(){
- m_administrator = true;
+ m_administrator = true;
}
-
+
+ public static boolean isConnect(){
+ return (m_own != null);
+ }
+
+ public static boolean autoConnect() {
+ try {
+ Account.getOwn();
+ return true;
+ } catch (NoAccountExistsInLocal noAccountExistsInLocal) {
+ return false;
+ }
+ }
+
public static void disconnect(){
- m_own = null;
+ Account own = m_own;
+ m_own = null;
+
+ SharedPreferences.Editor editor = PublicrowdFundingApplication.sharedPreferences().edit();
+
+ editor.remove(KEY_USERNAME);
+ editor.remove(KEY_PASSWORD);
+ editor.remove(KEY_PSEUDO);
+ editor.remove(KEY_ADMIN);
+ editor.apply();
+
+ own.getUser(new HoldToDo<User>() {
+ @Override
+ public void hold(User user) {
+ new Cache<User>(user).declareUpToDate().useIt();
+ }
+ });
+ new Cache<Account>(own).declareUpToDate().useIt();
}
+
+
+
- public void save() {
- SharedPreferences sharedPreferences = m_context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
+ private void save() {
+ SharedPreferences.Editor editor = PublicrowdFundingApplication.sharedPreferences().edit();
- sharedPreferences.edit().putString(KEY_USERNAME, m_username);
- sharedPreferences.edit().putString(KEY_PASSWORD, m_password);
- sharedPreferences.edit().putString(KEY_LAST_SYNC, Utility.DateTimeToString(m_lastSync));
- sharedPreferences.edit().commit();
+ editor.putString(KEY_USERNAME, m_username);
+ editor.putString(KEY_PASSWORD, encrypt("mystery", m_password));
+ editor.putString(KEY_PSEUDO, m_user.getResourceId());
+ editor.putBoolean(KEY_ADMIN, m_administrator);
+
+ editor.apply();
}
/* --------- Cryptography part ------------ */
private final static String HEX = "0123456789ABCDEF";
- private static String encrypt(String seed, String password) throws Exception {
- byte[] rawKey = getRawKey(seed.getBytes());
- byte[] result = encrypt(rawKey, password.getBytes());
+ private static String encrypt(String seed, String password) {
+ byte[] result;
+ try {
+ byte[] rawKey = getRawKey(seed.getBytes());
+ result = encrypt(rawKey, password.getBytes());
+ }
+ catch(Exception exception) {
+ return password;
+ }
return toHex(result);
}
- private static String decrypt(String seed, String encrypted) throws Exception {
- byte[] rawKey = getRawKey(seed.getBytes());
- byte[] enc = toByte(encrypted);
- byte[] result = decrypt(rawKey, enc);
+ private static String decrypt(String seed, String encrypted) {
+ byte[] result;
+ try {
+ byte[] rawKey = getRawKey(seed.getBytes());
+ byte[] enc = toByte(encrypted);
+ result = decrypt(rawKey, enc);
+ }
+ catch(Exception exception) {
+ return encrypted;
+ }
return new String(result);
}
return decrypted;
}
- private static String toHex(String txt) {
- return toHex(txt.getBytes());
- }
- private static String fromHex(String hex) {
- return new String(toByte(hex));
- }
-
private static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];