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

Migration_15_16.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: 4905623907083cf6ad70e7a9ce7a62fa58fa5f1f (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package it.niedermann.owncloud.notes.persistence.migration;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
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.preferences.DarkModeSetting;

public class Migration_15_16 {

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

    /**
     * Moves note list widget preferences from {@link SharedPreferences} to database
     * https://github.com/stefan-niedermann/nextcloud-notes/issues/832
     */
    public Migration_15_16(SQLiteDatabase db, @NonNull Context context, @NonNull Runnable notifyWidgets) {
        db.execSQL("CREATE TABLE WIDGET_NOTE_LISTS ( " +
                "ID INTEGER PRIMARY KEY, " +
                "ACCOUNT_ID INTEGER, " +
                "CATEGORY_ID INTEGER, " +
                "MODE INTEGER NOT NULL, " +
                "THEME_MODE INTEGER NOT NULL, " +
                "FOREIGN KEY(ACCOUNT_ID) REFERENCES ACCOUNTS(ID), " +
                "FOREIGN KEY(CATEGORY_ID) REFERENCES CATEGORIES(CATEGORY_ID))");

        final String SP_WIDGET_KEY = "NLW_mode";
        final String SP_ACCOUNT_ID_KEY = "NLW_account";
        final String SP_DARK_THEME_KEY = "NLW_darkTheme";
        final String SP_CATEGORY_KEY = "NLW_cat";

        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;
            Integer mode = null;
            Long accountId = null;
            Integer themeMode = null;
            Integer categoryId = null;
            if (key != null && key.startsWith(SP_WIDGET_KEY)) {
                try {
                    widgetId = Integer.parseInt(key.substring(SP_WIDGET_KEY.length()));
                    mode = (Integer) 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();
                    }

                    if (mode == 2) {
                        final String categoryTitle = sharedPreferences.getString(SP_CATEGORY_KEY + widgetId, null);
                        Cursor cursor = db.query(
                                "CATEGORIES",
                                new String[]{"CATEGORY_ID"},
                                "CATEGORY_TITLE = ? AND CATEGORY_ACCOUNT_ID = ? ",
                                new String[]{categoryTitle, String.valueOf(accountId)},
                                null,
                                null,
                                null);
                        if (cursor.moveToNext()) {
                            categoryId = cursor.getInt(0);
                        } else {
                            throw new IllegalStateException("No category id found for title \"" + categoryTitle + "\"");
                        }
                        cursor.close();
                    }

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