· 7 years ago · Oct 18, 2018, 02:14 AM
1public class NotesAdapter {
2 public static final String KEY_TITLE = "title";
3 public static final String KEY_BODY = "body";
4 public static final String KEY_COLOR = "color";
5 public static final String KEY_ROWID = "_id";
6
7 private static final String TAG = "NotesDbAdapter";
8 private DatabaseHelper mDbHelper;
9 SQLiteDatabase mDb;
10
11 /**
12 * Database creation sql statement
13 */
14 private static final String DATABASE_CREATE =
15 "CREATE TABLE IF NOT EXISTS notes (_id integer primary key autoincrement, " +
16 "title text not null, body text not null, color text not null);";
17
18 private static final String DATABASE_NAME = "data";
19 private static final String DATABASE_TABLE = "notes";
20 private static final int DATABASE_VERSION = 3;
21 public static String DB_FILEPATH = "/data/data/com.dreamhawk.ea/databases/data";
22
23 private final Context mCtx;
24
25 private static class DatabaseHelper extends SQLiteOpenHelper {
26
27 DatabaseHelper(Context context) {
28 super(context, DATABASE_NAME, null, DATABASE_VERSION);
29 }
30
31 @Override
32 public void onCreate(SQLiteDatabase db) {
33
34 db.execSQL(DATABASE_CREATE);
35 }
36
37 @Override
38 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
39 if(oldVersion==3 && newVersion==4) {
40 db.execSQL("UPDATE "+DATABASE_NAME+" SET 0 WHERE "+KEY_COLOR+"='RED_URI'");
41 db.execSQL("UPDATE "+DATABASE_NAME+" SET 1 WHERE "+KEY_COLOR+"='YELLOW_URI'");
42 db.execSQL("UPDATE "+DATABASE_NAME+" SET 2 WHERE "+KEY_COLOR+"='BLUE_URI'");
43
44 }
45 else {
46 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
47 + newVersion + ", which will destroy all old data");
48 db.execSQL("DROP TABLE IF EXISTS notes");
49 onCreate(db);
50 }
51 }
52
53
54 public boolean importDatabase() throws IOException {
55
56 // Close the SQLiteOpenHelper so it will commit the created empty
57 // database to internal storage.
58 close();
59 File newDb = new File(Environment.getExternalStorageDirectory(), "/Android/data/SimpleNotes/files/data");
60 File oldDb = new File(DB_FILEPATH);
61 if (newDb.exists()) {
62 Prefs.copyFile(newDb, oldDb);
63 // Access the copied database so SQLiteHelper will cache it and mark
64 // it as created.
65 getWritableDatabase().close();
66 return true;
67 }
68 return false;
69 }
70
71 }
72
73 /**
74 * Constructor - takes the context to allow the database to be
75 * opened/created
76 *
77 * @param ctx the Context within which to work
78 */
79 public NotesAdapter(Context ctx) {
80 this.mCtx = ctx;
81 }
82
83 /**
84 * Open the notes database. If it cannot be opened, try to create a new
85 * instance of the database. If it cannot be created, throw an exception to
86 * signal the failure
87 *
88 * @return this (self reference, allowing this to be chained in an
89 * initialization call)
90 * @throws SQLException if the database could be neither opened or created
91 */
92 public NotesAdapter open() throws SQLException {
93 mDbHelper = new DatabaseHelper(mCtx);
94 mDb = mDbHelper.getWritableDatabase();
95 return this;
96 }
97
98 public boolean test() throws IOException{
99 return mDbHelper.importDatabase();
100 }
101
102 /**
103 * Create a new note using the title and body provided. If the note is
104 * successfully created return the new rowId for that note, otherwise return
105 * a -1 to indicate failure.
106 *
107 * @param title the title of the note
108 * @param body the body of the note
109 * @return rowId or -1 if failed
110 */
111 public long createNote(String title, String body, String color) {
112 ContentValues initialValues = new ContentValues();
113 initialValues.put(KEY_TITLE, title);
114 initialValues.put(KEY_BODY, body);
115 initialValues.put(KEY_COLOR, color);
116
117 return mDb.insert(DATABASE_TABLE, null, initialValues);
118 }
119
120 /**
121 * Delete the note with the given rowId
122 *
123 * @param rowId id of note to delete
124 * @return true if deleted, false otherwise
125 */
126 public boolean deleteNote(long rowId) {
127
128 return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
129 }
130
131 /**
132 * Return a Cursor over the list of all notes in the database
133 *
134 * @return Cursor over all notes
135 */
136 public Cursor fetchAllNotes() {
137
138 return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
139 KEY_BODY, KEY_COLOR}, null, null, null, null, null);
140 }
141
142 /**
143 * Return a Cursor positioned at the note that matches the given rowId
144 *
145 * @param rowId id of note to retrieve
146 * @return Cursor positioned to matching note, if found
147 * @throws SQLException if note could not be found/retrieved
148 */
149 public Cursor fetchNote(long rowId) throws SQLException {
150
151 Cursor mCursor =
152
153 mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
154 KEY_TITLE, KEY_BODY, KEY_COLOR}, KEY_ROWID + "=" + rowId, null,
155 null, null, null, null);
156 if (mCursor != null) {
157 mCursor.moveToFirst();
158 }
159 return mCursor;
160
161 }
162
163
164 /**
165 * Update the note using the details provided. The note to be updated is
166 * specified using the rowId, and it is altered to use the title and body
167 * values passed in
168 *
169 * @param rowId id of note to update
170 * @param title value to set note title to
171 * @param body value to set note body to
172 * @return true if the note was successfully updated, false otherwise
173 */
174 public boolean updateNote(long rowId, String title, String body, String color) {
175 ContentValues args = new ContentValues();
176 args.put(KEY_TITLE, title);
177 args.put(KEY_BODY, body);
178 args.put(KEY_COLOR, color);
179
180 return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
181 }