Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fcacdef1 authored by Stefan Niedermann's avatar Stefan Niedermann
Browse files

Upgrade database and add entries for API_VERSION and brand

parent 924fdfb0
Loading
Loading
Loading
Loading
+14 −2
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {

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

    private static final int database_version = 11;
    private static final int database_version = 12;
    @NonNull
    private final Context context;

@@ -55,6 +55,9 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
    protected static final String key_favorite = "FAVORITE";
    protected static final String key_category = "CATEGORY";
    protected static final String key_etag = "ETAG";
    protected static final String key_color = "COLOR";
    protected static final String key_text_color = "TEXT_COLOR";
    protected static final String key_api_version = "API_VERSION";

    protected AbstractNotesDatabase(@NonNull Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory) {
        super(context, name, factory, database_version);
@@ -102,7 +105,10 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
                key_username + " TEXT, " +
                key_account_name + " TEXT UNIQUE, " +
                key_etag + " TEXT, " +
                key_modified + " INTEGER)");
                key_modified + " INTEGER, " +
                key_api_version + " TEXT, " +
                key_color + " VARCHAR(6) NOT NULL DEFAULT '000000', " +
                key_text_color + " VARCHAR(6) NOT NULL DEFAULT '0082C9')");
        createAccountIndexes(db);
    }

@@ -262,6 +268,12 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
            }
            editor.apply();
        }
        if (oldVersion < 12) {
            db.execSQL("ALTER TABLE " + table_accounts + " ADD COLUMN " + key_api_version + " TEXT");
            db.execSQL("ALTER TABLE " + table_accounts + " ADD COLUMN " + key_color + " VARCHAR(6) NOT NULL DEFAULT '000000'");
            db.execSQL("ALTER TABLE " + table_accounts + " ADD COLUMN " + key_text_color + " VARCHAR(6) NOT NULL DEFAULT '0082C9'");
            CapabilitiesWorker.update(context);
        }
    }

    @Override
+65 −0
Original line number Diff line number Diff line
package it.niedermann.owncloud.notes.persistence;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Constraints;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.NetworkType;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.model.SingleSignOnAccount;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

import it.niedermann.owncloud.notes.model.LocalAccount;

public class CapabilitiesWorker extends Worker {

    private static final String TAG = Objects.requireNonNull(CapabilitiesWorker.class.getCanonicalName());
    private static final String WORKER_TAG = "capabilities";

    private static final Constraints constraints = new Constraints.Builder()
            .setRequiredNetworkType(NetworkType.CONNECTED)
            .build();

    private static final PeriodicWorkRequest work = new PeriodicWorkRequest.Builder(CapabilitiesWorker.class, 24, TimeUnit.HOURS)
            .setConstraints(constraints).build();

    public CapabilitiesWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        NotesDatabase db = NotesDatabase.getInstance(getApplicationContext());
        for (LocalAccount account : db.getAccounts()) {
            try {
                SingleSignOnAccount ssoAccount = AccountImporter.getSingleSignOnAccount(getApplicationContext(), account.getAccountName());
                Log.i(TAG, "Refreshing capabilities for " + ssoAccount.name);
            } catch (NextcloudFilesAppAccountNotFoundException e) {
                e.printStackTrace();
            }
        }
        return Result.success();
    }

    public static void update(@NonNull Context context) {
        deregister(context);
        Log.i(TAG, "Registering worker running each 24 hours.");
        WorkManager.getInstance(context.getApplicationContext()).enqueueUniquePeriodicWork(WORKER_TAG, ExistingPeriodicWorkPolicy.REPLACE, work);
    }

    private static void deregister(@NonNull Context context) {
        Log.i(TAG, "Deregistering all workers with tag \"" + WORKER_TAG + "\"");
        WorkManager.getInstance(context.getApplicationContext()).cancelAllWorkByTag(WORKER_TAG);
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -68,12 +68,12 @@ public class SyncWorker extends Worker {
            PeriodicWorkRequest work = new PeriodicWorkRequest.Builder(SyncWorker.class, repeatInterval, unit)
                    .setConstraints(constraints).build();
            WorkManager.getInstance(context.getApplicationContext()).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, work);
            Log.v(TAG, "Registering worker running each " + repeatInterval + " " + unit);
            Log.i(TAG, "Registering worker running each " + repeatInterval + " " + unit);
        }
    }

    private static void deregister(@NonNull Context context) {
        Log.v(TAG, "Deregistering all workers with tag \"" + WORKER_TAG + "\"");
        Log.i(TAG, "Deregistering all workers with tag \"" + WORKER_TAG + "\"");
        WorkManager.getInstance(context.getApplicationContext()).cancelAllWorkByTag(WORKER_TAG);
    }
}