X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2FBookmarksDatabaseHandler.java;fp=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2FBookmarksDatabaseHandler.java;h=45344d3bd93c70edb7027dd90b97af1481f5919b;hp=0000000000000000000000000000000000000000;hb=9c582d802e641b2b6d27271310fc16898020b470;hpb=a28488d4f4d041e09a547683df74586f02146b0d diff --git a/app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseHandler.java b/app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseHandler.java new file mode 100644 index 00000000..45344d3b --- /dev/null +++ b/app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseHandler.java @@ -0,0 +1,120 @@ +/** + * Copyright 2016 Soren Stoutner . + * + * This file is part of Privacy Browser . + * + * Privacy Browser is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Privacy Browser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Privacy Browser. If not, see . + */ + +package com.stoutner.privacybrowser; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +public class BookmarksDatabaseHandler extends SQLiteOpenHelper { + private static final int SCHEMA_VERSION = 1; + private static final String BOOKMARKS_DATABASE = "bookmarks.db"; + private static final String BOOKMARKS_TABLE = "bookmarks"; + + public static final String ID = "_id"; + public static final String DISPLAYORDER = "displayorder"; + public static final String BOOKMARK_NAME = "bookmarkname"; + public static final String BOOKMARK_URL = "bookmarkurl"; + public static final String PARENTFOLDER = "parentfolder"; + public static final String ISFOLDER = "isfolder"; + public static final String FAVORITEICON = "favoriteicon"; + + public BookmarksDatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { + super(context, BOOKMARKS_DATABASE, factory, SCHEMA_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase bookmarksDatabase) { + // Create the database if it doesn't exist. + String CREATE_BOOKMARKS_TABLE = "CREATE TABLE " + BOOKMARKS_TABLE + " (" + + ID + " integer primary key, " + + DISPLAYORDER + " integer, " + + BOOKMARK_NAME + " text, " + + BOOKMARK_URL + " text, " + + PARENTFOLDER + " text, " + + ISFOLDER + " boolean, " + + FAVORITEICON + " blob);"; + + bookmarksDatabase.execSQL(CREATE_BOOKMARKS_TABLE); + } + + @Override + public void onUpgrade(SQLiteDatabase bookmarksDatabase, int oldVersion, int newVersion) { + // Code for upgrading the database will be added here when the schema version > 1. + } + + public void createBookmark(String bookmarkName, String bookmarkURL, byte[] favoriteIcon) { + ContentValues bookmarkContentValues = new ContentValues(); + + // ID is created automatically. + bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName); + bookmarkContentValues.put(BOOKMARK_URL, bookmarkURL); + bookmarkContentValues.put(PARENTFOLDER, ""); + bookmarkContentValues.put(ISFOLDER, false); + bookmarkContentValues.put(FAVORITEICON, favoriteIcon); + + // Get a writable database handle. + SQLiteDatabase bookmarksDatabase = this.getWritableDatabase(); + + // The second argument is "null", which makes it so that completely null rows cannot be created. Not a problem in our case. + bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues); + + // Close the database handle. + bookmarksDatabase.close(); + } + + public Cursor getBookmarksCursor() { + // Get a readable database handle. + SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); + + // Get everything in the BOOKMARKS_TABLE. + String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE; + + // Return the results as a Cursor. The second argument is "null" because there are no selectionArgs. + // We can't close the Cursor because we need to use it in the parent activity. + return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null); + } + + public String getBookmarkURL(int databaseID) { + // Get a readable database handle. + SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); + + // Get the row for the selected databaseID. + String GET_BOOKMARK_URL = "Select * FROM " + BOOKMARKS_TABLE + + " WHERE " + ID + " = " + databaseID; + + // Save the results as Cursor and move it to the first (only) row. The second argument is "null" because there are no selectionArgs. + Cursor bookmarksCursor = bookmarksDatabase.rawQuery(GET_BOOKMARK_URL, null); + bookmarksCursor.moveToFirst(); + + // Get the int that identifies the "BOOKMARK_URL" column and save the string as bookmarkURL. + int urlColumnInt = bookmarksCursor.getColumnIndex(BOOKMARK_URL); + String bookmarkURLString = bookmarksCursor.getString(urlColumnInt); + + // Close the Cursor and the database handle. + bookmarksCursor.close(); + bookmarksDatabase.close(); + + // Return the bookmarkURLString. + return bookmarkURLString; + } +}