Skip to content

Commit

Permalink
Refactored various things and improved the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasCz authored and JonasCz committed Nov 21, 2015
1 parent 8690359 commit 13861d0
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 117 deletions.
48 changes: 48 additions & 0 deletions app/src/main/java/jonas/tool/saveForOffline/Database.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package jonas.tool.saveForOffline;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class Database extends SQLiteOpenHelper {

public static final String DATABASE_NAME="SavedPagesMeta.db";
public static final String TABLE_NAME="main";
public static final String TITLE="title";
public static final String FILE_LOCATION="file_location";
public static final String THUMBNAIL="thumbnail";
public static final String ORIGINAL_URL="origurl";
public static final String ID="_id";
public static final String TIMESTAMP="timestamp";
public static final String SAVED_PAGE_BASE_DIRECTORY="tags";

public Database(Context context) {
super(context, DATABASE_NAME, null, 4);

}

@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("
+ID+" INTEGER PRIMARY KEY, "
+TITLE+" TEXT, "
+FILE_LOCATION+" TEXT, "
+THUMBNAIL+" TEXT, "
+ORIGINAL_URL+" TEXT, "
+SAVED_PAGE_BASE_DIRECTORY+" TEXT, "
+TIMESTAMP+" TEXT DEFAULT CURRENT_TIMESTAMP)";

db.execSQL(CREATE_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);

}

}
48 changes: 0 additions & 48 deletions app/src/main/java/jonas/tool/saveForOffline/DbHelper.java

This file was deleted.

51 changes: 16 additions & 35 deletions app/src/main/java/jonas/tool/saveForOffline/DisplayAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public static boolean hasDate (Layout l) {
}

private Context mContext;
private DbHelper mHelper;
private SQLiteDatabase dataBase;
private FuzzyDateFormatter fuzzyFormatter;

Expand All @@ -88,7 +87,7 @@ public static boolean hasDate (Layout l) {

private boolean darkMode;

public ArrayList<Integer> selectedViewsPositions = new ArrayList<Integer>();
public List<Integer> selectedViewsPositions = new ArrayList<Integer>();

private Bitmap placeHolder;

Expand All @@ -97,13 +96,13 @@ public static boolean hasDate (Layout l) {
public void refreshData(String searchQuery, SortOrder order, boolean dataSetChanged) {
switch (order) {
case OLDEST_FIRST:
sqlStatement = "SELECT * FROM " + DbHelper.TABLE_NAME + " WHERE " + DbHelper.KEY_TITLE + " LIKE'%" + searchQuery + "%' ORDER BY " + DbHelper.KEY_ID + " ASC";
sqlStatement = "SELECT * FROM " + Database.TABLE_NAME + " WHERE " + Database.TITLE + " LIKE'%" + searchQuery + "%' ORDER BY " + Database.ID + " ASC";
break;
case ALPHABETICAL:
sqlStatement = "SELECT * FROM " + DbHelper.TABLE_NAME + " WHERE " + DbHelper.KEY_TITLE + " LIKE'%" + searchQuery + "%' ORDER BY " + DbHelper.KEY_TITLE + " ASC";
sqlStatement = "SELECT * FROM " + Database.TABLE_NAME + " WHERE " + Database.TITLE + " LIKE'%" + searchQuery + "%' ORDER BY " + Database.TITLE + " ASC";
break;
default: //newest first
sqlStatement = "SELECT * FROM " + DbHelper.TABLE_NAME + " WHERE " + DbHelper.KEY_TITLE + " LIKE'%" + searchQuery + "%' ORDER BY " + DbHelper.KEY_ID + " DESC";
sqlStatement = "SELECT * FROM " + Database.TABLE_NAME + " WHERE " + Database.TITLE + " LIKE'%" + searchQuery + "%' ORDER BY " + Database.ID + " DESC";
break;
}

Expand All @@ -115,10 +114,8 @@ public void refreshData(String searchQuery, SortOrder order, boolean dataSetChan
public DisplayAdapter(Context c) {
this.mContext = c;

mHelper = new DbHelper(c);
dataBase = mHelper.getReadableDatabase();
FuzzyDateMessages fdm = new FuzzyDateMessages();
fuzzyFormatter = new FuzzyDateFormatter(Calendar.getInstance(), fdm);
dataBase = new Database(c).getReadableDatabase();
fuzzyFormatter = new FuzzyDateFormatter(Calendar.getInstance(), new FuzzyDateMessages());

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(mContext);

Expand All @@ -128,7 +125,6 @@ public DisplayAdapter(Context c) {
refreshData(null, SortOrder.NEWEST_FIRST, false);

placeHolder = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.icon_website_large);

}

public String getSearchQuery() {
Expand All @@ -151,28 +147,13 @@ public Object getItem(int position) {
@Override
public long getItemId(int position) {
if (dbCursor.getCount() != 0) {
return Long.valueOf(dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_ID)));
return Long.valueOf(dbCursor.getString(dbCursor.getColumnIndex(Database.ID)));
} else return 0;
}

public String getPropertiesByPosition(int position, String type) {

dbCursor.moveToPosition(position);

//todo use switch statement, this is horrible..
if (type.equals("id")) {
return dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_ID));
} else if (type.equals("thumbnail_location")) {
return dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_THUMBNAIL));
} else if (type.equals("file_location")) {
return dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_FILE_LOCATION));
} else if (type.equals("title")) {
return dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_TITLE));
} else if (type.equals("orig_url")) {
return dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_ORIG_URL));
} else if (type.equals("date")) {
return dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_TIMESTAMP));
} else { return null; }
return dbCursor.getString(dbCursor.getColumnIndex(type));
}

private View inflateView (View convertView, Holder mHolder) {
Expand Down Expand Up @@ -223,11 +204,11 @@ private void setListImage (ImageView imageView) {
if (Layout.getCurrentLayout() == Layout.SMALL_TEXT_ONLY) return;
switch ((String) imageView.getTag()) {
case "show:icon":
File icon = new File(new File(dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_THUMBNAIL))).getParent(), "saveForOffline_icon.png");
File icon = new File(new File(dbCursor.getString(dbCursor.getColumnIndex(Database.THUMBNAIL))).getParent(), "saveForOffline_icon.png");
Picasso.with(mContext).load(icon).error(R.drawable.icon_website_large).into(imageView);
break;
case "show:thumbnail":
File image = new File(dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_THUMBNAIL)));
File image = new File(dbCursor.getString(dbCursor.getColumnIndex(Database.THUMBNAIL)));
Picasso.with(mContext).load(image).placeholder(R.drawable.placeholder).into(imageView);
break;
default:
Expand Down Expand Up @@ -261,16 +242,16 @@ public View getView(int position, View convertView, ViewGroup parent) {

if (Layout.hasDate(Layout.getCurrentLayout())) {
try {
mHolder.txt_date.setText("Saved " + fuzzyFormatter.getFuzzy(dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_TIMESTAMP))));
mHolder.txt_date.setText("Saved " + fuzzyFormatter.getFuzzy(dbCursor.getString(dbCursor.getColumnIndex(Database.TIMESTAMP))));
} catch (ParseException e) {
Log.e("displayAdapter", "attempted to parse date '" + dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_TIMESTAMP)) + "' for display, with format yyyy-MM-dd HH:mm:ss, which resulted in a ParseException");
Log.e("displayAdapter", "attempted to parse date '" + dbCursor.getString(dbCursor.getColumnIndex(Database.TIMESTAMP)) + "' for display, with format yyyy-MM-dd HH:mm:ss, which resulted in a ParseException");
mHolder.txt_date.setText("Date unavailable");
}
}
mHolder.txt_id.setText(dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_ID)));
mHolder.txt_filelocation.setText(dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_FILE_LOCATION)));
mHolder.txt_orig_url.setText(dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_ORIG_URL)));
mHolder.txt_title.setText(dbCursor.getString(dbCursor.getColumnIndex(DbHelper.KEY_TITLE)));
mHolder.txt_id.setText(dbCursor.getString(dbCursor.getColumnIndex(Database.ID)));
mHolder.txt_filelocation.setText(dbCursor.getString(dbCursor.getColumnIndex(Database.FILE_LOCATION)));
mHolder.txt_orig_url.setText(dbCursor.getString(dbCursor.getColumnIndex(Database.ORIGINAL_URL)));
mHolder.txt_title.setText(dbCursor.getString(dbCursor.getColumnIndex(Database.TITLE)));

setListImage(mHolder.listimage);

Expand Down
36 changes: 18 additions & 18 deletions app/src/main/java/jonas/tool/saveForOffline/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class MainActivity extends Activity implements SearchView.OnQueryTextList


private DisplayAdapter gridAdapter;
private DbHelper mHelper;
private Database mHelper;
private SQLiteDatabase dataBase;

private TextView noSavedPages;
Expand Down Expand Up @@ -196,12 +196,12 @@ public void onItemClick(AdapterView<?> arg0, View clickedView, int position,

Intent i = new Intent(getApplicationContext(),
ViewActivity.class);
i.putExtra("orig_url", gridAdapter.getPropertiesByPosition(position, "orig_url"));
i.putExtra("title", gridAdapter.getPropertiesByPosition(position, "title"));
i.putExtra("id", gridAdapter.getPropertiesByPosition(position, "id"));
i.putExtra("fileLocation", gridAdapter.getPropertiesByPosition(position, "file_location"));
i.putExtra("thumbnailLocation", gridAdapter.getPropertiesByPosition(position, "thumbnail_location"));
i.putExtra("date", gridAdapter.getPropertiesByPosition(position, "date"));
i.putExtra("orig_url", gridAdapter.getPropertiesByPosition(position, Database.ORIGINAL_URL));
i.putExtra("title", gridAdapter.getPropertiesByPosition(position, Database.TITLE));
i.putExtra("id", gridAdapter.getPropertiesByPosition(position, Database.ID));
i.putExtra("fileLocation", gridAdapter.getPropertiesByPosition(position, Database.FILE_LOCATION));
i.putExtra("thumbnailLocation", gridAdapter.getPropertiesByPosition(position, Database.THUMBNAIL));
i.putExtra("date", gridAdapter.getPropertiesByPosition(position, Database.TIMESTAMP));

startActivity(i);

Expand Down Expand Up @@ -266,7 +266,7 @@ public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
e = (EditText) layout.findViewById(R.id.rename_dialog_edit);
TextView t = (TextView) layout.findViewById(R.id.rename_dialog_text);
if (gridAdapter.selectedViewsPositions.size() == 1) {
e.setText(gridAdapter.getPropertiesByPosition(gridAdapter.selectedViewsPositions.get(0), "title"));
e.setText(gridAdapter.getPropertiesByPosition(gridAdapter.selectedViewsPositions.get(0), Database.TITLE));
e.selectAll();

} else {
Expand All @@ -280,15 +280,15 @@ public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {

public void onClick(DialogInterface dialog, int which) {

mHelper = new DbHelper(MainActivity.this);
mHelper = new Database(MainActivity.this);
dataBase = mHelper.getWritableDatabase();

for (Integer position: gridAdapter.selectedViewsPositions) {

ContentValues values=new ContentValues();

values.put(DbHelper.KEY_TITLE, e.getText().toString() );
dataBase.update(DbHelper.TABLE_NAME, values, DbHelper.KEY_ID + "=" + gridAdapter.getPropertiesByPosition(position, "id"), null);
values.put(Database.TITLE, e.getText().toString() );
dataBase.update(Database.TABLE_NAME, values, Database.ID + "=" + gridAdapter.getPropertiesByPosition(position, Database.ID), null);


}
Expand Down Expand Up @@ -330,7 +330,7 @@ public void onClick(DialogInterface dialog, int which) {
if (gridAdapter.selectedViewsPositions.size() == 1) {
//build.setTitle("Do you want to delete ?");

build.setMessage("Do you want to delete ?\r\n" + gridAdapter.getPropertiesByPosition(gridAdapter.selectedViewsPositions.get(0), "title"));
build.setMessage("Do you want to delete ?\r\n" + gridAdapter.getPropertiesByPosition(gridAdapter.selectedViewsPositions.get(0), Database.TITLE));
} else {
//build.setTitle("Delete ?");
build.setMessage("Delete these " + gridAdapter.selectedViewsPositions.size() + " saved pages ?");
Expand All @@ -349,29 +349,29 @@ public void onClick(DialogInterface dialog, int which) {

pd.show();

mHelper = new DbHelper(MainActivity.this);
mHelper = new Database(MainActivity.this);
dataBase = mHelper.getWritableDatabase();

for (Integer position: gridAdapter.selectedViewsPositions) {


String fileLocation = gridAdapter.getPropertiesByPosition(position, "thumbnail_location");
String fileLocation = gridAdapter.getPropertiesByPosition(position, Database.THUMBNAIL);
File file = new File(fileLocation);
file.delete();

//for compatibility with old versions
if (gridAdapter.getPropertiesByPosition(position, "file_location").endsWith("mht")) {
fileLocation = gridAdapter.getPropertiesByPosition(position, "file_location");
if (gridAdapter.getPropertiesByPosition(position, Database.FILE_LOCATION).endsWith("mht")) {
fileLocation = gridAdapter.getPropertiesByPosition(position, Database.FILE_LOCATION);
file = new File(fileLocation);
file.delete();
} else {
fileLocation = gridAdapter.getPropertiesByPosition(position, "file_location");
fileLocation = gridAdapter.getPropertiesByPosition(position, Database.FILE_LOCATION);
file = new File(fileLocation);
file = file.getParentFile();
DirectoryHelper.deleteDirectory(file);
}

dataBase.delete(DbHelper.TABLE_NAME, DbHelper.KEY_ID + "=" + gridAdapter.getPropertiesByPosition(position, "id"), null);
dataBase.delete(Database.TABLE_NAME, Database.ID + "=" + gridAdapter.getPropertiesByPosition(position, Database.ID), null);
pd.setProgress(gridAdapter.selectedViewsPositions.indexOf(position));

}
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/jonas/tool/saveForOffline/SaveActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,17 @@ private void handleSave () {
private void addToDb () {
String title = webview.getTitle();

DbHelper mHelper = new DbHelper(this);
Database mHelper = new Database(this);
SQLiteDatabase dataBase = mHelper.getWritableDatabase();
ContentValues values=new ContentValues();

values.put(DbHelper.KEY_FILE_LOCATION,filelocation );
values.put(DbHelper.KEY_TITLE,title );
values.put(DbHelper.KEY_THUMBNAIL,thumbnail );
values.put(DbHelper.KEY_ORIG_URL,origurl );
values.put(Database.FILE_LOCATION,filelocation );
values.put(Database.TITLE,title );
values.put(Database.THUMBNAIL,thumbnail );
values.put(Database.ORIGINAL_URL,origurl );

//insert data into database
dataBase.insert(DbHelper.TABLE_NAME, null, values);
dataBase.insert(Database.TABLE_NAME, null, values);

//close database
dataBase.close();
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/jonas/tool/saveForOffline/SaveService.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ public class SaveService extends Service {

private void addToDb(String destinationDirectory, String title, String originalUrl) {

DbHelper mHelper = new DbHelper(SaveService.this);
Database mHelper = new Database(SaveService.this);
SQLiteDatabase dataBase = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();

values.put(DbHelper.KEY_FILE_LOCATION, destinationDirectory + "index.html");
values.put(DbHelper.KEY_SAVED_PAGE_BASE_DIRECTORY, destinationDirectory);
values.put(DbHelper.KEY_TITLE, title);
values.put(DbHelper.KEY_THUMBNAIL, destinationDirectory + "saveForOffline_thumbnail.png");
values.put(DbHelper.KEY_ORIG_URL, originalUrl);
values.put(Database.FILE_LOCATION, destinationDirectory + "index.html");
values.put(Database.SAVED_PAGE_BASE_DIRECTORY, destinationDirectory);
values.put(Database.TITLE, title);
values.put(Database.THUMBNAIL, destinationDirectory + "saveForOffline_thumbnail.png");
values.put(Database.ORIGINAL_URL, originalUrl);

dataBase.insert(DbHelper.TABLE_NAME, null, values);
dataBase.insert(Database.TABLE_NAME, null, values);

dataBase.close();
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/jonas/tool/saveForOffline/ViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@ public void onClick(DialogInterface dialog,
int which) {


DbHelper mHelper = new DbHelper(ViewActivity.this);
Database mHelper = new Database(ViewActivity.this);
SQLiteDatabase dataBase = mHelper.getWritableDatabase();
Intent incomingIntent2 = getIntent();

dataBase.delete(
DbHelper.TABLE_NAME,
DbHelper.KEY_ID + "=" + incomingIntent2.getStringExtra("id"), null);
Database.TABLE_NAME,
Database.ID + "=" + incomingIntent2.getStringExtra("id"), null);


File file = new File(incomingIntent2.getStringExtra("thumbnailLocation"));
Expand Down

0 comments on commit 13861d0

Please sign in to comment.