LeOS-Genesis/app/src/main/java/com/darkweb/genesissearchengine/dataManager/tabDataModel.java

233 lines
8.3 KiB
Java
Raw Normal View History

2020-11-11 13:11:13 +01:00
package com.darkweb.genesissearchengine.dataManager;
import android.annotation.SuppressLint;
2020-12-11 08:05:08 +01:00
import android.content.ContentValues;
2020-11-27 12:40:46 +01:00
import android.graphics.Bitmap;
import android.os.Handler;
2020-12-11 08:05:08 +01:00
import com.darkweb.genesissearchengine.appManager.databaseManager.databaseController;
2020-11-11 13:11:13 +01:00
import com.darkweb.genesissearchengine.appManager.homeManager.geckoSession;
import com.darkweb.genesissearchengine.appManager.tabManager.tabRowModel;
2020-11-27 12:40:46 +01:00
import org.mozilla.geckoview.GeckoResult;
2020-12-11 08:05:08 +01:00
import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
2020-11-11 13:11:13 +01:00
import java.util.ArrayList;
2020-12-11 08:05:08 +01:00
import java.util.Calendar;
2020-11-11 13:11:13 +01:00
import java.util.List;
2020-12-11 08:05:08 +01:00
import java.util.Locale;
2020-11-11 13:11:13 +01:00
@SuppressLint("CommitPrefEdits")
class tabDataModel
{
private ArrayList<tabRowModel> mTabs = new ArrayList<>();
2020-12-11 08:05:08 +01:00
ArrayList<tabRowModel> getTab(){
return mTabs;
}
2020-11-11 13:11:13 +01:00
/*List Tabs*/
2020-12-11 08:05:08 +01:00
void initializeTab(ArrayList<tabRowModel> pTabMdel){
mTabs.addAll(pTabMdel);
}
geckoSession getHomePage(){
2021-01-18 11:07:12 +01:00
if(mTabs.size()>0){
return mTabs.get(0).getSession();
}else {
return null;
2020-11-11 13:11:13 +01:00
}
}
2020-12-11 08:05:08 +01:00
void addTabs(geckoSession mSession,boolean pIsDataSavable){
tabRowModel mTabModel = new tabRowModel(mSession);
mTabs.add(0,mTabModel);
if(mTabs.size()>20){
closeTab(mTabs.get(mTabs.size()-1).getSession());
}
if(pIsDataSavable){
2021-01-18 11:07:12 +01:00
String[] params = new String[3];
2020-12-11 08:05:08 +01:00
params[0] = mTabModel.getSession().getTitle();
params[1] = mTabModel.getSession().getCurrentURL();
2021-01-18 11:07:12 +01:00
params[2] = mTabModel.getSession().getTheme();
2020-12-11 08:05:08 +01:00
String m_date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH).format(Calendar.getInstance().getTime());
2021-01-18 11:07:12 +01:00
databaseController.getInstance().execSQL("INSERT INTO tab(mid,date,title,url,theme) VALUES('"+ mTabModel.getmId() +"','" + m_date + "',?,?,?);",params);
2020-12-11 08:05:08 +01:00
}
2020-11-11 13:11:13 +01:00
}
2020-12-11 08:05:08 +01:00
2020-11-11 13:11:13 +01:00
void clearTab() {
int size = mTabs.size();
for(int counter = 0; counter< size; counter++){
mTabs.get(0).getSession().stop();
2020-12-24 09:55:18 +01:00
mTabs.get(0).getSession().closeSession();
2020-11-11 13:11:13 +01:00
mTabs.remove(0);
}
if(mTabs.size()>0){
mTabs.get(0).getSession().closeSession();
}
2020-12-11 08:05:08 +01:00
databaseController.getInstance().execSQL("DELETE FROM tab WHERE 1",null);
2020-12-24 09:55:18 +01:00
2020-11-11 13:11:13 +01:00
}
2020-12-11 08:05:08 +01:00
2020-11-11 13:11:13 +01:00
void closeTab(geckoSession mSession) {
for(int counter = 0; counter< mTabs.size(); counter++){
2020-12-11 08:05:08 +01:00
if(mTabs.get(counter).getSession().getSessionID().equals(mSession.getSessionID()))
2020-11-11 13:11:13 +01:00
{
2020-12-11 08:05:08 +01:00
databaseController.getInstance().execSQL("DELETE FROM tab WHERE mid='" + mTabs.get(counter).getmId() + "'",null);
2020-11-11 13:11:13 +01:00
mTabs.remove(counter);
break;
}
}
}
2020-12-11 08:05:08 +01:00
2020-11-11 13:11:13 +01:00
void moveTabToTop(geckoSession mSession) {
for(int counter = 0; counter< mTabs.size(); counter++){
2020-12-11 08:05:08 +01:00
if(mTabs.get(counter).getSession().getSessionID().equals(mSession.getSessionID()))
2020-11-11 13:11:13 +01:00
{
2020-12-11 08:05:08 +01:00
String m_date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH).format(Calendar.getInstance().getTime());
databaseController.getInstance().execSQL("UPDATE tab SET date = '" + m_date + "' WHERE mid='"+mTabs.get(counter).getmId() + "'",null);
mTabs.add(0,mTabs.remove(counter));
2020-11-11 13:11:13 +01:00
break;
}
}
}
2020-12-11 08:05:08 +01:00
2021-01-18 11:07:12 +01:00
boolean updateTab(String mSessionID, geckoSession pSession) {
2020-12-11 08:05:08 +01:00
2021-01-18 11:07:12 +01:00
boolean mSessionUpdated = false;
2020-12-11 08:05:08 +01:00
for(int counter = 0; counter< mTabs.size(); counter++){
if(mTabs.get(counter).getSession().getSessionID().equals(mSessionID))
{
2021-01-18 11:07:12 +01:00
String[] params = new String[3];
2020-12-11 08:05:08 +01:00
params[0] = mTabs.get(counter).getSession().getTitle();
params[1] = mTabs.get(counter).getSession().getCurrentURL();
2021-01-18 11:07:12 +01:00
params[2] = mTabs.get(counter).getSession().getTheme();
mSessionUpdated = true;
2020-12-11 08:05:08 +01:00
String m_date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH).format(Calendar.getInstance().getTime());
2021-01-18 11:07:12 +01:00
databaseController.getInstance().execSQL("UPDATE tab SET date = '" + m_date + "' , url = ? , title = ?, theme = ? WHERE mid='"+mTabs.get(counter).getmId() + "'",params);
2020-12-11 08:05:08 +01:00
return true;
}
}
2021-01-18 11:07:12 +01:00
if(!mSessionUpdated){
addTabs(pSession, true);
}
2020-12-11 08:05:08 +01:00
return false;
}
2020-11-11 13:11:13 +01:00
tabRowModel getCurrentTab(){
if(mTabs.size()>0){
return mTabs.get(0);
}
else {
return null;
}
}
2020-11-27 12:40:46 +01:00
2020-12-11 08:05:08 +01:00
tabRowModel getLastTab(){
if(mTabs.size()>0){
return mTabs.get(mTabs.size()-1);
}
else {
return null;
}
}
public void updatePixels(String pSessionID, GeckoResult<Bitmap> pBitmapManager){
2020-11-27 12:40:46 +01:00
for(int counter = 0; counter< mTabs.size(); counter++){
2020-12-11 08:05:08 +01:00
if(mTabs.get(counter).getSession().getSessionID().equals(pSessionID))
2020-11-27 12:40:46 +01:00
{
final Handler handler = new Handler();
int finalCounter = counter;
2020-12-11 08:05:08 +01:00
int finalCounter1 = counter;
2020-11-27 12:40:46 +01:00
handler.postDelayed(() ->
{
try {
2021-01-18 11:07:12 +01:00
Bitmap mBitmap = pBitmapManager.poll(500);
2020-11-27 12:40:46 +01:00
mTabs.get(finalCounter).setmBitmap(mBitmap);
2020-12-11 08:05:08 +01:00
ByteArrayOutputStream bos = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] mThumbnail = bos.toByteArray();
ContentValues mContentValues = new ContentValues();
mContentValues.put("mThumbnail", mThumbnail);
databaseController.getInstance().execTab("tab",mContentValues, mTabs.get(finalCounter1).getmId());
2020-11-27 12:40:46 +01:00
} catch (Throwable throwable) {
throwable.printStackTrace();
}
2020-12-24 09:55:18 +01:00
}, 500);
2020-11-27 12:40:46 +01:00
}
}
}
2021-01-18 11:07:12 +01:00
public ArrayList<ArrayList<String>> getSuggestions(String pQuery){
ArrayList<ArrayList<String>> mModel = new ArrayList<>();
for(int count = 0; count<= mTabs.size()-1 && mTabs.size()<500; count++){
if(mTabs.get(count).getSession().getTitle().toLowerCase().contains(pQuery) || mTabs.get(count).getSession().getCurrentURL().toLowerCase().contains(pQuery)){
ArrayList<String> mTempModel = new ArrayList<>();
mTempModel.add(mTabs.get(count).getSession().getTitle().toLowerCase());
mTempModel.add(mTabs.get(count).getSession().getCurrentURL());
mModel.add(mTempModel);
}
}
return mModel;
}
2020-11-11 13:11:13 +01:00
int getTotalTabs(){
return mTabs.size();
}
/*List Suggestion*/
2021-01-18 11:07:12 +01:00
public Object onTrigger(dataEnums.eTabCommands pCommands, List<Object> pData){
if(pCommands == dataEnums.eTabCommands.GET_TOTAL_TAB){
2020-11-11 13:11:13 +01:00
return getTotalTabs();
}
2021-01-18 11:07:12 +01:00
else if(pCommands == dataEnums.eTabCommands.GET_CURRENT_TAB){
2020-11-11 13:11:13 +01:00
return getCurrentTab();
}
2021-01-18 11:07:12 +01:00
else if(pCommands == dataEnums.eTabCommands.GET_LAST_TAB){
2020-12-11 08:05:08 +01:00
return getLastTab();
}
2021-01-18 11:07:12 +01:00
else if(pCommands == dataEnums.eTabCommands.MOVE_TAB_TO_TOP){
moveTabToTop((geckoSession)pData.get(0));
2020-11-11 13:11:13 +01:00
}
2021-01-18 11:07:12 +01:00
else if(pCommands == dataEnums.eTabCommands.CLOSE_TAB){
closeTab((geckoSession)pData.get(0));
2020-11-11 13:11:13 +01:00
}
2021-01-18 11:07:12 +01:00
else if(pCommands == dataEnums.eTabCommands.M_CLEAR_TAB){
2020-11-11 13:11:13 +01:00
clearTab();
}
2021-01-18 11:07:12 +01:00
else if(pCommands == dataEnums.eTabCommands.M_ADD_TAB){
addTabs((geckoSession)pData.get(0), (boolean)pData.get(1));
2020-11-11 13:11:13 +01:00
}
2021-01-18 11:07:12 +01:00
else if(pCommands == dataEnums.eTabCommands.M_UPDATE_TAB){
updateTab((String) pData.get(1), (geckoSession) pData.get(5));
2020-12-11 08:05:08 +01:00
}
2021-01-18 11:07:12 +01:00
else if(pCommands == dataEnums.eTabCommands.GET_TAB){
2020-11-11 13:11:13 +01:00
return getTab();
}
2021-01-18 11:07:12 +01:00
else if(pCommands == dataEnums.eTabCommands.M_GET_SUGGESTIONS){
return getSuggestions((String) pData.get(0));
}
else if(pCommands == dataEnums.eTabCommands.M_UPDATE_PIXEL){
updatePixels((String)pData.get(0), (GeckoResult<Bitmap>)pData.get(1));
2020-12-11 08:05:08 +01:00
}
2021-01-18 11:07:12 +01:00
else if(pCommands == dataEnums.eTabCommands.M_HOME_PAGE){
2020-12-11 08:05:08 +01:00
return getHomePage();
2020-11-27 12:40:46 +01:00
}
2020-11-11 13:11:13 +01:00
return null;
}
}