diff --git a/app/src/main/java/jonas/tool/saveForOffline/Database.java b/app/src/main/java/jonas/tool/saveForOffline/Database.java new file mode 100644 index 0000000..a4c7d09 --- /dev/null +++ b/app/src/main/java/jonas/tool/saveForOffline/Database.java @@ -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); + + } + +} diff --git a/app/src/main/java/jonas/tool/saveForOffline/DbHelper.java b/app/src/main/java/jonas/tool/saveForOffline/DbHelper.java deleted file mode 100644 index 6ba01dc..0000000 --- a/app/src/main/java/jonas/tool/saveForOffline/DbHelper.java +++ /dev/null @@ -1,48 +0,0 @@ -package jonas.tool.saveForOffline; - - -import android.content.Context; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; - - -public class DbHelper extends SQLiteOpenHelper { - - static final String DATABASE_NAME="SavedPagesMeta.db"; - public static final String TABLE_NAME="main"; - public static final String KEY_TITLE="title"; - public static final String KEY_FILE_LOCATION="file_location"; - public static final String KEY_THUMBNAIL="thumbnail"; - public static final String KEY_ORIG_URL="origurl"; - public static final String KEY_ID="_id"; - public static final String KEY_TIMESTAMP="timestamp"; - public static final String KEY_SAVED_PAGE_BASE_DIRECTORY="tags"; - - public DbHelper(Context context) { - super(context, DATABASE_NAME, null, 4); - - } - - @Override - public void onCreate(SQLiteDatabase db) { - String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" (" - +KEY_ID+" INTEGER PRIMARY KEY, " - +KEY_TITLE+" TEXT, " - +KEY_FILE_LOCATION+" TEXT, " - +KEY_THUMBNAIL+" TEXT, " - +KEY_ORIG_URL+" TEXT, " - +KEY_SAVED_PAGE_BASE_DIRECTORY+" TEXT, " - +KEY_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); - - } - -} diff --git a/app/src/main/java/jonas/tool/saveForOffline/DisplayAdapter.java b/app/src/main/java/jonas/tool/saveForOffline/DisplayAdapter.java index f7aa648..08c5430 100644 --- a/app/src/main/java/jonas/tool/saveForOffline/DisplayAdapter.java +++ b/app/src/main/java/jonas/tool/saveForOffline/DisplayAdapter.java @@ -79,7 +79,6 @@ public static boolean hasDate (Layout l) { } private Context mContext; - private DbHelper mHelper; private SQLiteDatabase dataBase; private FuzzyDateFormatter fuzzyFormatter; @@ -88,7 +87,7 @@ public static boolean hasDate (Layout l) { private boolean darkMode; - public ArrayList selectedViewsPositions = new ArrayList(); + public List selectedViewsPositions = new ArrayList(); private Bitmap placeHolder; @@ -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; } @@ -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); @@ -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() { @@ -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) { @@ -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: @@ -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); diff --git a/app/src/main/java/jonas/tool/saveForOffline/MainActivity.java b/app/src/main/java/jonas/tool/saveForOffline/MainActivity.java index 81b5bcf..65ffb54 100644 --- a/app/src/main/java/jonas/tool/saveForOffline/MainActivity.java +++ b/app/src/main/java/jonas/tool/saveForOffline/MainActivity.java @@ -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; @@ -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); @@ -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 { @@ -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); } @@ -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 ?"); @@ -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)); } diff --git a/app/src/main/java/jonas/tool/saveForOffline/SaveActivity.java b/app/src/main/java/jonas/tool/saveForOffline/SaveActivity.java index 237665b..e22512c 100644 --- a/app/src/main/java/jonas/tool/saveForOffline/SaveActivity.java +++ b/app/src/main/java/jonas/tool/saveForOffline/SaveActivity.java @@ -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(); diff --git a/app/src/main/java/jonas/tool/saveForOffline/SaveService.java b/app/src/main/java/jonas/tool/saveForOffline/SaveService.java index 5b90135..2329246 100644 --- a/app/src/main/java/jonas/tool/saveForOffline/SaveService.java +++ b/app/src/main/java/jonas/tool/saveForOffline/SaveService.java @@ -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(); } diff --git a/app/src/main/java/jonas/tool/saveForOffline/ViewActivity.java b/app/src/main/java/jonas/tool/saveForOffline/ViewActivity.java index 7c2a867..5f84df1 100644 --- a/app/src/main/java/jonas/tool/saveForOffline/ViewActivity.java +++ b/app/src/main/java/jonas/tool/saveForOffline/ViewActivity.java @@ -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"));