Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Migration_13_14.java « migration « persistence « notes « owncloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9a5d5fdfb44d94abad977cc3ae2640968a56e838 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package it.niedermann.owncloud.notes.persistence.migration;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;

import java.util.Map;

import it.niedermann.owncloud.notes.android.DarkModeSetting;

public class Migration_13_14 {

    private static final String TAG = Migration_13_14.class.getSimpleName();

    public Migration_13_14(SQLiteDatabase db, int oldVersion, @NonNull Context context, @NonNull Runnable notifyWidgets) {
        if (oldVersion < 14) {
            // #754 Move single note widget preferences to database
            db.execSQL("CREATE TABLE WIDGET_SINGLE_NOTES ( " +
                    "ID INTEGER PRIMARY KEY, " +
                    "ACCOUNT_ID INTEGER, " +
                    "NOTE_ID INTEGER, " +
                    "THEME_MODE INTEGER NOT NULL, " +
                    "FOREIGN KEY(ACCOUNT_ID) REFERENCES ACCOUNTS(ID), " +
                    "FOREIGN KEY(NOTE_ID) REFERENCES NOTES(ID))");

            final String SP_WIDGET_KEY = "single_note_widget";
            final String SP_ACCOUNT_ID_KEY = "SNW_accountId";
            final String SP_DARK_THEME_KEY = "SNW_darkTheme";
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            Map<String, ?> prefs = sharedPreferences.getAll();
            for (Map.Entry<String, ?> pref : prefs.entrySet()) {
                final String key = pref.getKey();
                Integer widgetId = null;
                Long noteId = null;
                Long accountId = null;
                Integer themeMode = null;
                if (key != null && key.startsWith(SP_WIDGET_KEY)) {
                    try {
                        widgetId = Integer.parseInt(key.substring(SP_WIDGET_KEY.length()));
                        noteId = (Long) pref.getValue();
                        accountId = sharedPreferences.getLong(SP_ACCOUNT_ID_KEY + widgetId, -1);

                        try {
                            themeMode = DarkModeSetting.valueOf(sharedPreferences.getString(SP_DARK_THEME_KEY + widgetId, DarkModeSetting.SYSTEM_DEFAULT.name())).getModeId();
                        } catch (ClassCastException e) {
                            //DARK_THEME was a boolean in older versions of the app. We thereofre have to still support the old setting.
                            themeMode = sharedPreferences.getBoolean(SP_DARK_THEME_KEY + widgetId, false) ? DarkModeSetting.DARK.getModeId() : DarkModeSetting.LIGHT.getModeId();
                        }

                        ContentValues migratedWidgetValues = new ContentValues();
                        migratedWidgetValues.put("ID", widgetId);
                        migratedWidgetValues.put("ACCOUNT_ID", accountId);
                        migratedWidgetValues.put("NOTE_ID", noteId);
                        migratedWidgetValues.put("THEME_MODE", themeMode);
                        db.insert("WIDGET_SINGLE_NOTES", null, migratedWidgetValues);
                    } catch (Throwable t) {
                        Log.e(TAG, "Could not migrate widget {widgetId: " + widgetId + ", accountId: " + accountId + ", noteId: " + noteId + ", themeMode: " + themeMode + "}");
                        t.printStackTrace();
                    } finally {
                        // Clean up old shared preferences
                        editor.remove(SP_WIDGET_KEY + widgetId);
                        editor.remove(SP_DARK_THEME_KEY + widgetId);
                        editor.remove(SP_ACCOUNT_ID_KEY + widgetId);
                    }
                }
            }
            editor.apply();
            notifyWidgets.run();
        }
    }
}