diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/utils/AttributeSetUtils.java b/uhabits-android/src/main/java/org/isoron/uhabits/utils/AttributeSetUtils.java deleted file mode 100644 index db66fd774..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/utils/AttributeSetUtils.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) 2016-2021 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.utils; - -import android.content.*; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import android.util.*; - -import org.jetbrains.annotations.*; - -public class AttributeSetUtils -{ - public static final String ISORON_NAMESPACE = "http://isoron.org/android"; - - @Nullable - public static String getAttribute(@NonNull Context context, - @NonNull AttributeSet attrs, - @NonNull String name, - @Nullable String defaultValue) - { - int resId = attrs.getAttributeResourceValue(ISORON_NAMESPACE, name, 0); - if (resId != 0) return context.getResources().getString(resId); - - String value = attrs.getAttributeValue(ISORON_NAMESPACE, name); - if (value != null) return value; - else return defaultValue; - } - - public static boolean getBooleanAttribute(@NonNull Context context, - @NonNull AttributeSet attrs, - @NonNull String name, - boolean defaultValue) - { - String boolText = getAttribute(context, attrs, name, null); - if (boolText != null) return Boolean.parseBoolean(boolText); - else return defaultValue; - } - - @Contract("_,_,_,!null -> !null") - public static Integer getColorAttribute(@NonNull Context context, - @NonNull AttributeSet attrs, - @NonNull String name, - @Nullable Integer defaultValue) - { - int resId = attrs.getAttributeResourceValue(ISORON_NAMESPACE, name, 0); - if (resId != 0) return context.getResources().getColor(resId); - else return defaultValue; - } - - public static float getFloatAttribute(@NonNull Context context, - @NonNull AttributeSet attrs, - @NonNull String name, - float defaultValue) - { - try - { - String number = getAttribute(context, attrs, name, null); - if (number != null) return Float.parseFloat(number); - else return defaultValue; - } catch(NumberFormatException e) { - return defaultValue; - } - } - - public static int getIntAttribute(@NonNull Context context, - @NonNull AttributeSet attrs, - @NonNull String name, - int defaultValue) - { - String number = getAttribute(context, attrs, name, null); - if (number != null) return Integer.parseInt(number); - else return defaultValue; - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/utils/AttributeSetUtils.kt b/uhabits-android/src/main/java/org/isoron/uhabits/utils/AttributeSetUtils.kt new file mode 100644 index 000000000..7ab5a1e34 --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/utils/AttributeSetUtils.kt @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2016-2021 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ +package org.isoron.uhabits.utils + +import android.content.Context +import android.util.AttributeSet +import org.jetbrains.annotations.Contract + +object AttributeSetUtils { + const val ISORON_NAMESPACE = "http://isoron.org/android" + @JvmStatic + fun getAttribute( + context: Context, + attrs: AttributeSet, + name: String, + defaultValue: String? + ): String? { + val resId = attrs.getAttributeResourceValue(ISORON_NAMESPACE, name, 0) + if (resId != 0) return context.resources.getString(resId) + val value = attrs.getAttributeValue(ISORON_NAMESPACE, name) + return value ?: defaultValue + } + + @JvmStatic + fun getBooleanAttribute( + context: Context, + attrs: AttributeSet, + name: String, + defaultValue: Boolean + ): Boolean { + val boolText = getAttribute(context, attrs, name, null) + return if (boolText != null) java.lang.Boolean.parseBoolean(boolText) else defaultValue + } + + @JvmStatic + @Contract("_,_,_,!null -> !null") + fun getColorAttribute( + context: Context, + attrs: AttributeSet, + name: String, + defaultValue: Int? + ): Int? { + val resId = attrs.getAttributeResourceValue(ISORON_NAMESPACE, name, 0) + return if (resId != 0) context.resources.getColor(resId) else defaultValue + } + + @JvmStatic + fun getFloatAttribute( + context: Context, + attrs: AttributeSet, + name: String, + defaultValue: Float + ): Float { + return try { + val number = getAttribute(context, attrs, name, null) + number?.toFloat() ?: defaultValue + } catch (e: NumberFormatException) { + defaultValue + } + } + + fun getIntAttribute( + context: Context, + attrs: AttributeSet, + name: String, + defaultValue: Int + ): Int { + val number = getAttribute(context, attrs, name, null) + return number?.toInt() ?: defaultValue + } +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.java b/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.java deleted file mode 100644 index 78aa5a3e7..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (C) 2016-2021 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.utils; - -import android.content.*; -import android.database.sqlite.*; -import android.util.*; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import org.isoron.uhabits.*; -import org.isoron.uhabits.core.utils.*; - -import java.io.*; -import java.text.*; - -import static org.isoron.uhabits.core.ConstantsKt.*; - -public abstract class DatabaseUtils -{ - @Nullable - private static HabitsDatabaseOpener opener = null; - - @NonNull - public static File getDatabaseFile(Context context) - { - String databaseFilename = getDatabaseFilename(); - String root = context.getFilesDir().getPath(); - - String format = "%s/../databases/%s"; - String filename = String.format(format, root, databaseFilename); - - return new File(filename); - } - - @NonNull - public static String getDatabaseFilename() - { - String databaseFilename = DATABASE_FILENAME; - if (HabitsApplication.Companion.isTestMode()) databaseFilename = "test.db"; - return databaseFilename; - } - - @SuppressWarnings("unchecked") - public static void initializeDatabase(Context context) - { - opener = new HabitsDatabaseOpener(context, getDatabaseFilename(), - DATABASE_VERSION); - } - - @SuppressWarnings("ResultOfMethodCallIgnored") - public static String saveDatabaseCopy(Context context, File dir) - throws IOException - { - SimpleDateFormat dateFormat = DateFormats.getBackupDateFormat(); - String date = dateFormat.format(DateUtils.getLocalTime()); - String format = "%s/Loop Habits Backup %s.db"; - String filename = String.format(format, dir.getAbsolutePath(), date); - Log.i("DatabaseUtils", "Writing: " + filename); - - File db = getDatabaseFile(context); - File dbCopy = new File(filename); - FileUtilsKt.copyTo(db, dbCopy); - - return dbCopy.getAbsolutePath(); - } - - @NonNull - public static SQLiteDatabase openDatabase() - { - if (opener == null) throw new IllegalStateException(); - return opener.getWritableDatabase(); - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.kt b/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.kt new file mode 100644 index 000000000..db25f279d --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.kt @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2016-2021 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ +package org.isoron.uhabits.utils + +import android.content.Context +import android.database.sqlite.SQLiteDatabase +import android.util.Log +import org.isoron.uhabits.HabitsApplication.Companion.isTestMode +import org.isoron.uhabits.HabitsDatabaseOpener +import org.isoron.uhabits.core.DATABASE_FILENAME +import org.isoron.uhabits.core.DATABASE_VERSION +import org.isoron.uhabits.core.utils.DateFormats.Companion.getBackupDateFormat +import org.isoron.uhabits.core.utils.DateUtils.Companion.getLocalTime +import java.io.File +import java.io.IOException +import java.text.SimpleDateFormat + +object DatabaseUtils { + private var opener: HabitsDatabaseOpener? = null + @JvmStatic + fun getDatabaseFile(context: Context): File { + val databaseFilename = databaseFilename + val root = context.filesDir.path + return File("$root/../databases/$databaseFilename") + } + + private val databaseFilename: String + get() { + var databaseFilename: String = DATABASE_FILENAME + if (isTestMode()) databaseFilename = "test.db" + return databaseFilename + } + + fun initializeDatabase(context: Context?) { + opener = HabitsDatabaseOpener( + context!!, + databaseFilename, + DATABASE_VERSION + ) + } + + @JvmStatic + @Throws(IOException::class) + fun saveDatabaseCopy(context: Context, dir: File): String { + val dateFormat: SimpleDateFormat = getBackupDateFormat() + val date = dateFormat.format(getLocalTime()) + val filename = "${dir.absolutePath}/Loop Habits Backup $date.db" + Log.i("DatabaseUtils", "Writing: $filename") + val db = getDatabaseFile(context) + val dbCopy = File(filename) + db.copyTo(dbCopy) + return dbCopy.absolutePath + } + + fun openDatabase(): SQLiteDatabase { + checkNotNull(opener) + return opener!!.writableDatabase + } +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/utils/SystemUtils.java b/uhabits-android/src/main/java/org/isoron/uhabits/utils/SystemUtils.kt similarity index 55% rename from uhabits-android/src/main/java/org/isoron/uhabits/utils/SystemUtils.java rename to uhabits-android/src/main/java/org/isoron/uhabits/utils/SystemUtils.kt index d848a90eb..a7d805103 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/utils/SystemUtils.java +++ b/uhabits-android/src/main/java/org/isoron/uhabits/utils/SystemUtils.kt @@ -16,30 +16,24 @@ * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ +package org.isoron.uhabits.utils -package org.isoron.uhabits.utils; +import android.app.Activity +import android.app.KeyguardManager +import android.content.Context +import android.os.Build +import android.view.WindowManager -import android.app.*; -import android.content.*; -import android.os.*; -import android.view.*; +object SystemUtils { + val isAndroidOOrLater: Boolean + get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O - -public class SystemUtils -{ - public static boolean isAndroidOOrLater() - { - return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O; - } - - public static void unlockScreen(Activity activity) - { - if (isAndroidOOrLater()) { - KeyguardManager km = - (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE); - km.requestDismissKeyguard(activity, null); + fun unlockScreen(activity: Activity) { + if (isAndroidOOrLater) { + val km = activity.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager + km.requestDismissKeyguard(activity, null) } else { - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); + activity.window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD) } } }