Created
September 26, 2017 15:23
-
-
Save anonymous/70c524c1e8eb5e7ed893131a9c685b5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mbc.analytics.sdk.room.database; | |
import android.content.Context; | |
import java.util.List; | |
import mbc.analytics.sdk.logs.Logger; | |
import mbc.analytics.sdk.room.dao.AllAppsDao; | |
import mbc.analytics.sdk.room.dao.AppDao; | |
import mbc.analytics.sdk.room.dao.DataDao; | |
import mbc.analytics.sdk.room.dao.TimeDao; | |
import mbc.analytics.sdk.room.entity.AllAppsEntity; | |
import mbc.analytics.sdk.room.entity.AppEntity; | |
import mbc.analytics.sdk.room.entity.AppRelation; | |
import mbc.analytics.sdk.room.entity.DataEntity; | |
import mbc.analytics.sdk.room.entity.TimeEntity; | |
public class DatabaseRepository implements DatabaseRepoInterface.AppEntityInterface, DatabaseRepoInterface.TimeEntityInterface, DatabaseRepoInterface.DataEntityInterface, DatabaseRepoInterface.AllAppsInterface { | |
private AppDatabase db; | |
private AppDao mAppDao; | |
private TimeDao mTimeDao; | |
private DataDao mDataDao; | |
private AllAppsDao mallAppsDao; | |
public DatabaseRepository(Context ctx) { | |
db = AppDatabase.getAppDatabase(ctx); | |
if (db != null) { | |
mAppDao = db.appDao(); | |
mTimeDao = db.timeDao(); | |
mDataDao = db.dataDao(); | |
mallAppsDao = db.allAppsDao(); | |
} | |
} | |
public void databaseClose() { | |
if (db.isOpen()) { | |
db.close(); | |
} | |
} | |
public boolean isDatabaseOpen(){ | |
return db.isOpen(); | |
} | |
@Override | |
public void createAppEntity(String packageName, int sessionId, String time) { | |
if (appKeyExists(packageName.hashCode()) == null) { | |
AppEntity appEntity = new AppEntity(); | |
appEntity.setAppkey(packageName.hashCode()); | |
appEntity.setAppPackage(packageName); | |
mAppDao.insertApplicationModel(appEntity); | |
createTimeEntity(packageName.hashCode(), sessionId, time); | |
} else { | |
createTimeEntity(packageName.hashCode(), sessionId, time); | |
} | |
} | |
@Override | |
public DataEntity getData() { | |
return mDataDao.getData(); | |
} | |
@Override | |
public void createDataEntity(int key, String uuid, long startedDay, int maxSecRetry) { | |
key = keyChecker(key); | |
DataEntity dataEntity = new DataEntity(); | |
dataEntity.setKey(key); | |
dataEntity.setUuid(uuid); | |
dataEntity.setStartedDay(startedDay); | |
dataEntity.setMaxSecRetries(maxSecRetry); | |
if (mDataDao.getData() == null) { | |
mDataDao.setData(dataEntity); | |
} else { | |
mDataDao.updateData(dataEntity); | |
} | |
} | |
@Override | |
public void setSendDataInterval(int key, long sendDataInterval) { | |
if (mDataDao.getData() != null) { | |
DataEntity dataEntity = mDataDao.getData(); | |
dataEntity.setSendDataInterval(sendDataInterval); | |
Logger.info("dataEntity"+dataEntity); | |
mDataDao.updateData(dataEntity); | |
} | |
} | |
@Override | |
public void setSuccessSendDay(int key, long successDay) { | |
if (mDataDao.getData() != null) { | |
DataEntity dataEntity = mDataDao.getData(); | |
dataEntity.setDataSuccessDay(successDay); | |
mDataDao.updateData(dataEntity); | |
} | |
} | |
@Override | |
public void setLastSendDay(int key, long lastSendDay) { | |
if (mDataDao.getData() != null) { | |
DataEntity dataEntity = mDataDao.getData(); | |
dataEntity.setDataLastDay(lastSendDay); | |
dataEntity.setSendDataTime(lastSendDay + dataEntity.getSendDataInterval()); | |
mDataDao.updateData(dataEntity); | |
} | |
} | |
@Override | |
public long getLastSendDay(int key) { | |
if (mDataDao.getData() != null) { | |
DataEntity dataEntity = mDataDao.getData(); | |
return dataEntity.getDataLastDay(); | |
} else { | |
return System.currentTimeMillis(); | |
} | |
} | |
@Override | |
public void setConnectionInfo(int key, boolean status) { | |
if (mDataDao.getData() != null) { | |
DataEntity dataEntity = mDataDao.getData(); | |
dataEntity.setConnectionInfo(status); | |
mDataDao.updateData(dataEntity); | |
} | |
} | |
@Override | |
public boolean getConnectionInfo() { | |
if (mDataDao.getData() != null) { | |
DataEntity dataEntity = mDataDao.getData(); | |
return dataEntity.isConnectionInfo(); | |
} else { | |
return false; | |
} | |
} | |
@Override | |
public void updateSendDataAfterError(int key, long delay) { | |
if (mDataDao.getData() != null) { | |
DataEntity dataEntity = mDataDao.getData(); | |
dataEntity.setSendDataTime(delay + dataEntity.getSendDataTime()); | |
mDataDao.updateData(dataEntity); | |
} | |
} | |
@Override | |
public void setStatusOfService(int key, boolean killSwitchFlag, boolean pauseSwitchFlag) { | |
if (mDataDao.getData() != null) { | |
DataEntity dataEntity = mDataDao.getData(); | |
dataEntity.setKillSwitch(killSwitchFlag); | |
dataEntity.setPauseSwitch(pauseSwitchFlag); | |
mDataDao.updateData(dataEntity); | |
} | |
} | |
@Override | |
public boolean getKillSwitch(int key) { | |
if (mDataDao.getData() != null) { | |
DataEntity dataEntity = mDataDao.getData(); | |
return dataEntity.isKillSwitch(); | |
} else { | |
return false; | |
} | |
} | |
@Override | |
public boolean dataKeyExists(int key) { | |
if (mDataDao.isKeyExist(key) != null) | |
return Boolean.TRUE; | |
else | |
return Boolean.FALSE; | |
} | |
@Override | |
public boolean getPauseSwitch(int key) { | |
if (mDataDao.getData() != null) { | |
DataEntity dataEntity = mDataDao.getData(); | |
return dataEntity.isPauseSwitch(); | |
} else { | |
return false; | |
} | |
} | |
// kai gamw tis methodous ... auth einai h Agia Methodos | |
// mono egw kai o theos jeroume giati to ekana auto.... | |
// Ara otan tha thn exw kanei apo thn etairia kai ertheis na mou peis "Re kwsta giati to ekanes autos?" | |
// Tha sou pw oti einai kai gamw tis arxhstes methodous..... | |
private int keyChecker(int key) { | |
if (key == 1) { | |
return key; | |
} else { | |
return 1; | |
} | |
} | |
@Override | |
public void createTimeEntity(int key, int sessionId, String time) { | |
if (getTheLatest(key) == null || getTheLatest(key).isFull()) { | |
TimeEntity timeEntity = new TimeEntity(); | |
timeEntity.setforeignAppkey(key); | |
timeEntity.addTime(sessionId, time); | |
mTimeDao.insertTimeModel(timeEntity); | |
} else { | |
if (appKeyExists(key) != null) { | |
TimeEntity timeEntity = mAppDao.getTheLastestTransationRow(key); | |
timeEntity.addTime(sessionId, time); | |
mTimeDao.updateTimeModel(timeEntity); | |
} | |
} | |
} | |
@Override | |
public void updateAppEntity(AppEntity appEntity) { | |
if (appKeyExists(appEntity.getAppkey()) != null) { | |
mAppDao.updateApplicationModel(appEntity); | |
} | |
} | |
@Override | |
public List<AppEntity> getListApplications() { | |
return mAppDao.getAllApplication(); | |
} | |
@Override | |
public List<AppRelation> getAllData() { | |
return mAppDao.getAllData(); | |
} | |
@Override | |
public void clearApplicationsDatabase() { | |
mAppDao.deleteAllApplicationModel(); | |
mTimeDao.deleteAllTimeModel(); | |
} | |
@Override | |
public AppEntity appKeyExists(int packageNameHash) { | |
return mAppDao.findById(packageNameHash); | |
} | |
@Override | |
public AppRelation appRelationKeyExists(int packageNameHash) { | |
return mAppDao.findAppById(packageNameHash); | |
} | |
@Override | |
public TimeEntity getTheLatest(int key) { | |
return mTimeDao.getTheLatest(key); | |
} | |
@Override | |
public List<AllAppsEntity> getListAllApps() { | |
return mallAppsDao.getAllApps(); | |
} | |
@Override | |
public List<AllAppsEntity> getListAllAppsWithFilter() { | |
return mallAppsDao.getFiltedAllApps(); | |
} | |
@Override | |
public void deleteListAllApps() { | |
mallAppsDao.deleteAllApps(); | |
} | |
@Override | |
public void insertListAllApps(String appName, String packgageName, String versionName, int versionCode, long firstInstallTime, long lastUpdateTime) { | |
AllAppsEntity allAppsEntity = new AllAppsEntity(); | |
allAppsEntity.setAppNameHashCode(packgageName.hashCode()); | |
allAppsEntity.setAppName(appName); | |
allAppsEntity.setPackageName(packgageName); | |
allAppsEntity.setVersionName(versionName); | |
allAppsEntity.setVersionCode(versionCode); | |
allAppsEntity.setFirstInstallTime(firstInstallTime); | |
allAppsEntity.setLastUpdateTime(lastUpdateTime); | |
mallAppsDao.insertAllApps(allAppsEntity); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment