mirror of https://github.com/iSoron/uhabits.git
parent
25b25acc94
commit
97dcf98e2b
@ -1,93 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
|
||||||
*
|
|
||||||
* 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,87 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
@ -1,92 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
|
||||||
*
|
|
||||||
* 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue