Skip to content

Commit 6178081

Browse files
committed
Add legacy importer
1 parent f5dc263 commit 6178081

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed

app/build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
applicationId "org.aykit.MyOwnNotes"
99
minSdkVersion 16
1010
targetSdkVersion 23
11-
versionCode 1
12-
versionName '0.1'
11+
versionCode 10
12+
versionName '2.0b'
1313
}
1414
buildTypes {
1515
release {
@@ -23,7 +23,7 @@ android {
2323
resValue "string", "app_name", "MyOwnNotes[Local]"
2424
}
2525
store {
26-
applicationId 'org.aykit.MyOwnNotes.store'
26+
applicationId 'org.aykit.MyOwnNotes'
2727
resValue "string", "app_name", "MyOwnNotes"
2828
}
2929
}

app/src/main/java/org/aykit/MyOwnNotes/activities/NoteListActivity.java

+22
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030
import org.aykit.MyOwnNotes.database.model.Note;
3131
import org.aykit.MyOwnNotes.fragments.NoteDetailFragment;
3232
import org.aykit.MyOwnNotes.fragments.NoteListFragment;
33+
import org.aykit.MyOwnNotes.helpers.LegacyImporter;
3334
import org.aykit.MyOwnNotes.helpers.Settings;
3435

36+
import java.util.List;
37+
3538
import butterknife.Bind;
3639
import butterknife.ButterKnife;
3740

@@ -91,6 +94,8 @@ protected void onCreate(Bundle savedInstanceState) {
9194
setContentView(R.layout.activity_note_app_bar);
9295
ButterKnife.bind(this);
9396

97+
checkForLegacyDatabase();
98+
9499
setSupportActionBar(toolbar);
95100
toolbar.setTitle(getTitle());
96101

@@ -256,4 +261,21 @@ public void run() {
256261
.negativeText(android.R.string.no)
257262
.show();
258263
}
264+
265+
private void checkForLegacyDatabase(){
266+
LegacyImporter importer = new LegacyImporter(this);
267+
if (importer.checkForMigration()){
268+
final List<Note> extractedNotes = importer.extractNotes();
269+
270+
new Thread(new Runnable() {
271+
@Override
272+
public void run() {
273+
for (Note note : extractedNotes) {
274+
getContentResolver().insert(NotesProvider.NOTES.CONTENT_URI, note.getContentValues());
275+
}
276+
SyncNotesAsyncTask.start(NoteListActivity.this);
277+
}
278+
}).start();
279+
}
280+
}
259281
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.aykit.MyOwnNotes.helpers;
2+
3+
import android.content.Context;
4+
import android.database.Cursor;
5+
import android.database.sqlite.SQLiteDatabase;
6+
7+
import org.aykit.MyOwnNotes.database.model.Note;
8+
9+
import java.io.File;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
/**
14+
* Created by mklepp on 21/02/16.
15+
*/
16+
public class LegacyImporter {
17+
18+
static final String LEGACY_DB_NAME = "notes.db";
19+
20+
private Context mContext;
21+
22+
public LegacyImporter(Context context) {
23+
this.mContext = context;
24+
}
25+
26+
private File getDatabasePath(){
27+
return mContext.getDatabasePath(LEGACY_DB_NAME);
28+
}
29+
30+
public boolean checkForMigration(){
31+
File oldDb = getDatabasePath();
32+
33+
if (oldDb.exists()){
34+
return true;
35+
}
36+
37+
return false;
38+
}
39+
40+
public List<Note> extractNotes(){
41+
42+
List<Note> extractedNotes = new ArrayList<>();
43+
44+
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(getDatabasePath(), null);
45+
46+
Cursor cursor = db.rawQuery("SELECT * FROM notes WHERE noteStatus IS NOT NULL", null);
47+
48+
if (cursor!=null)
49+
{
50+
while(cursor.moveToNext()){
51+
Note note = new Note();
52+
note.title = "Unsynchronized Note from previous version";
53+
note.content = cursor.getString(cursor.getColumnIndex("content"));
54+
55+
extractedNotes.add(note);
56+
}
57+
58+
// close and delete old database
59+
db.close();
60+
getDatabasePath().delete();
61+
} else {
62+
db.close();
63+
}
64+
65+
return extractedNotes;
66+
}
67+
}

0 commit comments

Comments
 (0)