Skip to content

Commit

Permalink
Migrate notes stored with older versions
Browse files Browse the repository at this point in the history
  • Loading branch information
khuttun committed Dec 9, 2018
1 parent e824586 commit 382f1ac
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 7,8 @@ android {
applicationId "com.khuttun.notificationnotes"
minSdkVersion 15
targetSdkVersion 26
versionCode 7
versionName "1.6"
versionCode 8
versionName "1.7"
}
buildTypes {
release {
Expand Down
36 changes: 36 additions & 0 deletions app/src/main/java/com/khuttun/notificationnotes/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 13,8 @@
import android.view.MenuItem;
import android.view.View;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity
{
/**
Expand Down Expand Up @@ -117,6 119,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data)
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (Globals.LOG) Log.d(Globals.TAG, "Creating MainActivity");
setContentView(R.layout.activity_main);

this.notesListAdapter = new NotesListAdapter(this, getSupportFragmentManager());
Expand All @@ -127,6 130,8 @@ protected void onCreate(Bundle savedInstanceState)

this.addNoteResult = null;
this.noteListObserver = new EmptyNoteListObserver(noteListView, findViewById(R.id.empty_text_view));

migratePreferences();
}

@Override
Expand Down Expand Up @@ -189,4 194,35 @@ protected void onResume()
this.addNoteResult = null;
}
}

private void migratePreferences()
{
// The storage of the notes has changed in version 1.6. Migrate old notes if found.
String oldPref = getPreferences(Context.MODE_PRIVATE).getString(Globals.NOTES_PREF_NAME, null);
if (oldPref != null)
{
if (Globals.LOG) Log.d(Globals.TAG, "Migrating old notes pref");

// Take into account also possibility that there's already notes stored to the new storage
String newPref = PreferenceManager.getDefaultSharedPreferences(this).getString(
Globals.NOTES_PREF_NAME,
"[]");

// Combine old and new versions of the stored notes
ArrayList<NotificationNote> notes = Globals.jsonToNoteList(oldPref);
if (Globals.LOG) Log.d(Globals.TAG, notes.size() " notes stored to old pref");
notes.addAll(Globals.jsonToNoteList(newPref));
if (Globals.LOG) Log.d(Globals.TAG, notes.size() " notes stored total");

// Store the combined notes
SharedPreferences.Editor newPrefsEditor = PreferenceManager.getDefaultSharedPreferences(this).edit();
newPrefsEditor.putString(Globals.NOTES_PREF_NAME, Globals.noteListToJson(notes));
newPrefsEditor.commit();

// Remove the old version
SharedPreferences.Editor oldPrefsEditor = getPreferences(Context.MODE_PRIVATE).edit();
oldPrefsEditor.remove(Globals.NOTES_PREF_NAME);
oldPrefsEditor.commit();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 16,7 @@ public class SettingsActivity extends AppCompatActivity implements SharedPrefere
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (Globals.LOG) Log.d(Globals.TAG, "Creating SettingsActivity");
setContentView(R.layout.activity_settings);
this.notificationMgr = new NotificationMgr(this);
}
Expand Down

0 comments on commit 382f1ac

Please sign in to comment.