Skip to content

Commit

Permalink
[refactor] Applies checkstyle to the whole project.
Browse files Browse the repository at this point in the history
  • Loading branch information
coderPaddyS committed Feb 9, 2024
1 parent 37cf8a3 commit 434399b
Show file tree
Hide file tree
Showing 33 changed files with 323 additions and 300 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*/
package org.secuso.privacyfriendlynotes.backup;

import static org.secuso.privacyfriendlynotes.room.NoteDatabase.DATABASE_NAME;
import static java.nio.charset.StandardCharsets.UTF_8;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
Expand All @@ -23,20 +26,16 @@
import androidx.sqlite.db.SupportSQLiteDatabase;
import androidx.sqlite.db.SupportSQLiteOpenHelper;

import org.secuso.privacyfriendlybackup.api.backup.FileUtil;
import org.secuso.privacyfriendlybackup.api.pfa.IBackupCreator;
import org.secuso.privacyfriendlybackup.api.backup.DatabaseUtil;
import org.secuso.privacyfriendlybackup.api.backup.FileUtil;
import org.secuso.privacyfriendlybackup.api.backup.PreferenceUtil;
import org.secuso.privacyfriendlynotes.PFNotesApplication;
import org.secuso.privacyfriendlybackup.api.pfa.IBackupCreator;

import java.io.File;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Arrays;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.secuso.privacyfriendlynotes.room.NoteDatabase.DATABASE_NAME;

public class BackupCreator implements IBackupCreator {

@Override
Expand Down Expand Up @@ -65,7 +64,7 @@ public boolean writeBackup(@NonNull Context context, @NonNull OutputStream outpu
Log.d("PFA BackupCreator", "Writing files");
writer.name("files");
writer.beginObject();
for(String path : Arrays.asList("sketches", "audio_notes")) {
for (String path : Arrays.asList("sketches", "audio_notes")) {
writer.name(path);
FileUtil.writePath(writer, new File(context.getFilesDir().getPath(), path), false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package org.secuso.privacyfriendlynotes.backup;

import static org.secuso.privacyfriendlynotes.room.NoteDatabase.DATABASE_NAME;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
Expand All @@ -34,8 +36,6 @@
import java.io.InputStream;
import java.io.InputStreamReader;

import static org.secuso.privacyfriendlynotes.room.NoteDatabase.DATABASE_NAME;

public class BackupRestorer implements IBackupRestorer {

private void readFiles(@NonNull JsonReader reader, @NonNull Context context) throws IOException {
Expand Down Expand Up @@ -121,7 +121,7 @@ private void readPreferences(@NonNull JsonReader reader, @NonNull Context contex
String name = reader.nextName();

switch (name) {
case "settings_use_custom_font_size",
case "settings_use_custom_font_size",
"settings_del_notes",
"settings_show_preview",
"settings_dialog_on_trashing",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@

import org.secuso.privacyfriendlybackup.api.pfa.PFAAuthService;

public class PFABackupService extends PFAAuthService { }
public class PFABackupService extends PFAAuthService {
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import org.secuso.privacyfriendlynotes.ui.notes.BaseNoteActivity
import org.secuso.privacyfriendlynotes.ui.notes.ChecklistNoteActivity
import org.secuso.privacyfriendlynotes.ui.notes.SketchActivity
import org.secuso.privacyfriendlynotes.ui.notes.TextNoteActivity
import java.lang.IllegalStateException

/**
* This receiver is responsible to create and show notifications to the user.
Expand Down Expand Up @@ -59,13 +58,15 @@ class NotificationReceiver : BroadcastReceiver() {
)
if (notification != -1 && type != -1) {
Log.d(javaClass.simpleName, "Creating intent for $context with type $type and intent $intent and id $notification and name $name")
val pendingIntent = Intent(context, when(type) {
DbContract.NoteEntry.TYPE_TEXT -> TextNoteActivity::class.java
DbContract.NoteEntry.TYPE_AUDIO -> AudioNoteActivity::class.java
DbContract.NoteEntry.TYPE_SKETCH -> SketchActivity::class.java
DbContract.NoteEntry.TYPE_CHECKLIST -> ChecklistNoteActivity::class.java
else -> throw IllegalStateException("Note with type $type does not exist!")
}).apply {
val pendingIntent = Intent(
context, when (type) {
DbContract.NoteEntry.TYPE_TEXT -> TextNoteActivity::class.java
DbContract.NoteEntry.TYPE_AUDIO -> AudioNoteActivity::class.java
DbContract.NoteEntry.TYPE_SKETCH -> SketchActivity::class.java
DbContract.NoteEntry.TYPE_CHECKLIST -> ChecklistNoteActivity::class.java
else -> throw IllegalStateException("Note with type $type does not exist!")
}
).apply {
putExtra(BaseNoteActivity.EXTRA_ID, notification)
}.let {
PendingIntent.getActivity(context, 0, it, PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
* Created by Robin on 11.06.2016.
*/
public class DbContract {
public DbContract(){}
public DbContract() {
}

public static abstract class NoteEntry implements BaseColumns {
public static final String TABLE_NAME = "notes";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import android.database.Cursor;
import android.text.Html;
import android.text.SpannedString;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.core.util.Pair;
Expand Down Expand Up @@ -49,59 +48,7 @@ public abstract class NoteDatabase extends RoomDatabase {

public static final int VERSION = 5;
public static final String DATABASE_NAME = "allthenotes";
private static NoteDatabase instance;

public abstract NoteDao noteDao();

public abstract CategoryDao categoryDao();

public abstract NotificationDao notificationDao();

public static synchronized NoteDatabase getInstance(Context context) {
return getInstance(context, DATABASE_NAME);
}

public static synchronized NoteDatabase getInstance(Context context, String databaseName) {
if (instance == null || !DATABASE_NAME.equals(databaseName)) {
instance = Room.databaseBuilder(context.getApplicationContext(),
NoteDatabase.class, databaseName)
.allowMainThreadQueries()
.addMigrations(MIGRATIONS)
.addCallback(roomCallback)
.build();
}
return instance;
}

public static synchronized NoteDatabase getInstance(Context context, String databaseName, File file) {
if (instance == null) {
instance = Room.databaseBuilder(context.getApplicationContext(),
NoteDatabase.class, databaseName)
.createFromFile(file)
.allowMainThreadQueries()
.addMigrations(MIGRATIONS)
.addCallback(roomCallback)
.build();
}
return instance;
}

private static RoomDatabase.Callback roomCallback = new RoomDatabase.Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
// Adds a trigger to auto-set custom_order to _id
// Room currently supports no DEFAULT = COLUMN or @Trigger Annotation
db.execSQL(
"CREATE TRIGGER [InsertCustomOrder] AFTER INSERT ON notes FOR EACH ROW " +
"BEGIN " +
"UPDATE notes SET custom_order = _id WHERE _id=NEW._id; " +
"END;"
);
super.onCreate(db);
}
};

static final Migration MIGRATION_4_5 = new Migration(4,5) {
static final Migration MIGRATION_4_5 = new Migration(4, 5) {

@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
Expand All @@ -112,22 +59,22 @@ public void migrate(@NonNull SupportSQLiteDatabase database) {
// Adds new fields to sort by
database.execSQL(
"CREATE TABLE notes_new (_id INTEGER NOT NULL DEFAULT 0,"
+ "in_trash INTEGER NOT NULL DEFAULT 0,"
+ "name TEXT NOT NULL DEFAULT 'TEXT',"
+ "type INTEGER NOT NULL DEFAULT 0,"
+ "category INTEGER NOT NULL DEFAULT 0,"
+ "content TEXT NOT NULL DEFAULT 'TEXT',"
+ "last_modified TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,"
+ "custom_order INTEGER NOT NULL DEFAULT 0,"
+ "PRIMARY KEY(_id));");
+ "in_trash INTEGER NOT NULL DEFAULT 0,"
+ "name TEXT NOT NULL DEFAULT 'TEXT',"
+ "type INTEGER NOT NULL DEFAULT 0,"
+ "category INTEGER NOT NULL DEFAULT 0,"
+ "content TEXT NOT NULL DEFAULT 'TEXT',"
+ "last_modified TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,"
+ "custom_order INTEGER NOT NULL DEFAULT 0,"
+ "PRIMARY KEY(_id));");
database.execSQL("INSERT INTO notes_new(_id, in_trash,name,type,category,content,custom_order) SELECT _id, in_trash,name,type,category,content,_id as custom_order FROM notes ORDER BY _id ASC;");
database.execSQL("DROP TABLE notes;");
database.execSQL("ALTER TABLE notes_new RENAME TO notes");
database.execSQL(
"CREATE TRIGGER [UpdateLastModified] AFTER UPDATE ON notes FOR EACH ROW WHEN NEW.last_modified = OLD.last_modified " +
"BEGIN " +
"BEGIN " +
"UPDATE notes SET last_modified = DateTime('now') WHERE _id=NEW._id; " +
"END;"
"END;"
);
database.execSQL(
"CREATE TRIGGER [InsertCustomOrder] AFTER INSERT ON notes FOR EACH ROW " +
Expand All @@ -137,35 +84,6 @@ public void migrate(@NonNull SupportSQLiteDatabase database) {
);
}
};

/**
* Provides data migration from database version 3 to 4 which checks for an error in the previous
* migration when a backup was imported
*/
static final Migration MIGRATION_3_4 = new Migration(3, 4) {
@Override
public void migrate(SupportSQLiteDatabase database) {
// get current schema and check if it needs to be fixed
String result = "";
Cursor c = database.query("SELECT sql FROM sqlite_master WHERE type='table' AND name='notes';");
if (c != null) {
if (c.moveToFirst()) {
while (!c.isAfterLast()) {
result = c.getString(c.getColumnIndexOrThrow("sql"));
c.moveToNext();
}
}
c.close();
}

String categorySQL = result.split("category")[1].split(",")[0];

if (categorySQL != null && categorySQL.toUpperCase().contains("INTEGER") && !categorySQL.toUpperCase().contains("NOT NULL")) {
MIGRATION_1_2.migrate(database);
}
}
};

/**
* Provides data migration from database version 1 (SQLite) to 2 (Room)
*/
Expand Down Expand Up @@ -229,6 +147,33 @@ public void migrate(SupportSQLiteDatabase database) {
}
}
};
/**
* Provides data migration from database version 3 to 4 which checks for an error in the previous
* migration when a backup was imported
*/
static final Migration MIGRATION_3_4 = new Migration(3, 4) {
@Override
public void migrate(SupportSQLiteDatabase database) {
// get current schema and check if it needs to be fixed
String result = "";
Cursor c = database.query("SELECT sql FROM sqlite_master WHERE type='table' AND name='notes';");
if (c != null) {
if (c.moveToFirst()) {
while (!c.isAfterLast()) {
result = c.getString(c.getColumnIndexOrThrow("sql"));
c.moveToNext();
}
}
c.close();
}

String categorySQL = result.split("category")[1].split(",")[0];

if (categorySQL != null && categorySQL.toUpperCase().contains("INTEGER") && !categorySQL.toUpperCase().contains("NOT NULL")) {
MIGRATION_1_2.migrate(database);
}
}
};
static final Migration MIGRATION_1_3 = new Migration(1, 3) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
Expand Down Expand Up @@ -273,12 +218,61 @@ public void migrate(SupportSQLiteDatabase database) {
}
}
};

public static final Migration[] MIGRATIONS = {
MIGRATION_1_2,
MIGRATION_1_3,
MIGRATION_2_3,
MIGRATION_3_4,
MIGRATION_4_5
};
private static final RoomDatabase.Callback roomCallback = new RoomDatabase.Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
// Adds a trigger to auto-set custom_order to _id
// Room currently supports no DEFAULT = COLUMN or @Trigger Annotation
db.execSQL(
"CREATE TRIGGER [InsertCustomOrder] AFTER INSERT ON notes FOR EACH ROW " +
"BEGIN " +
"UPDATE notes SET custom_order = _id WHERE _id=NEW._id; " +
"END;"
);
super.onCreate(db);
}
};
private static NoteDatabase instance;

public static synchronized NoteDatabase getInstance(Context context) {
return getInstance(context, DATABASE_NAME);
}

public static synchronized NoteDatabase getInstance(Context context, String databaseName) {
if (instance == null || !DATABASE_NAME.equals(databaseName)) {
instance = Room.databaseBuilder(context.getApplicationContext(),
NoteDatabase.class, databaseName)
.allowMainThreadQueries()
.addMigrations(MIGRATIONS)
.addCallback(roomCallback)
.build();
}
return instance;
}

public static synchronized NoteDatabase getInstance(Context context, String databaseName, File file) {
if (instance == null) {
instance = Room.databaseBuilder(context.getApplicationContext(),
NoteDatabase.class, databaseName)
.createFromFile(file)
.allowMainThreadQueries()
.addMigrations(MIGRATIONS)
.addCallback(roomCallback)
.build();
}
return instance;
}

public abstract NoteDao noteDao();

public abstract CategoryDao categoryDao();

public abstract NotificationDao notificationDao();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
package org.secuso.privacyfriendlynotes.room.dao

import androidx.lifecycle.LiveData
import androidx.room.*
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy.Companion.REPLACE
import androidx.room.Query
import androidx.room.Update
import kotlinx.coroutines.flow.Flow
import org.secuso.privacyfriendlynotes.room.model.Category

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
*/
package org.secuso.privacyfriendlynotes.room.dao

import androidx.room.*
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import kotlinx.coroutines.flow.Flow
import org.secuso.privacyfriendlynotes.room.model.Note

Expand Down Expand Up @@ -42,7 +46,7 @@ interface NoteDao {
val allTrashedNotes: Flow<List<Note>>

@Query("SELECT * FROM notes")
fun getNotesDebug() : List<Note>
fun getNotesDebug(): List<Note>

@Query("SELECT * FROM notes WHERE _id = :id")
fun getNoteByID(id: Long): Note?
Expand Down
Loading

0 comments on commit 434399b

Please sign in to comment.