From 4e398123b124e59c995741ccee8ca3d3f38e5a61 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 29 Nov 2017 13:50:46 +0100 Subject: [PATCH] adding the contentprovider used for shared themes --- app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 4 ++ .../contentproviders/MyContentProvider.kt | 25 ++++++++ .../dbhelpers/MyContentProviderDbHelper.kt | 61 +++++++++++++++++++ build.gradle | 2 +- 5 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/thankyou/contentproviders/MyContentProvider.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/thankyou/dbhelpers/MyContentProviderDbHelper.kt diff --git a/app/build.gradle b/app/build.gradle index a7d2fbc..147e4d9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -38,7 +38,7 @@ android { } dependencies { - compile 'com.simplemobiletools:commons:2.41.8' + compile 'com.simplemobiletools:commons:2.41.19' } Properties props = new Properties() diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 44939c7..3065fee 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -47,5 +47,9 @@ android:label="@string/customize_colors" android:parentActivityName=".activities.SettingsActivity"/> + diff --git a/app/src/main/kotlin/com/simplemobiletools/thankyou/contentproviders/MyContentProvider.kt b/app/src/main/kotlin/com/simplemobiletools/thankyou/contentproviders/MyContentProvider.kt new file mode 100644 index 0000000..0209845 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/thankyou/contentproviders/MyContentProvider.kt @@ -0,0 +1,25 @@ +package com.simplemobiletools.thankyou.contentproviders + +import android.content.ContentProvider +import android.content.ContentValues +import android.net.Uri +import com.simplemobiletools.thankyou.dbhelpers.MyContentProviderDbHelper + +class MyContentProvider : ContentProvider() { + lateinit var dbHelper: MyContentProviderDbHelper + + override fun insert(uri: Uri, contentValues: ContentValues) = null + + override fun query(uri: Uri, projection: Array?, selection: String?, selectionArgs: Array?, sortOrder: String?) = dbHelper.getSharedTheme() + + override fun onCreate(): Boolean { + dbHelper = MyContentProviderDbHelper.newInstance(context) + return true + } + + override fun update(uri: Uri, contentValues: ContentValues, selection: String?, selectionArgs: Array?) = dbHelper.updateTheme(contentValues) + + override fun delete(uri: Uri, selection: String, selectionArgs: Array) = 0 + + override fun getType(uri: Uri) = "" +} diff --git a/app/src/main/kotlin/com/simplemobiletools/thankyou/dbhelpers/MyContentProviderDbHelper.kt b/app/src/main/kotlin/com/simplemobiletools/thankyou/dbhelpers/MyContentProviderDbHelper.kt new file mode 100644 index 0000000..190e5b1 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/thankyou/dbhelpers/MyContentProviderDbHelper.kt @@ -0,0 +1,61 @@ +package com.simplemobiletools.thankyou.dbhelpers + +import android.content.ContentValues +import android.content.Context +import android.database.Cursor +import android.database.sqlite.SQLiteDatabase +import android.database.sqlite.SQLiteOpenHelper +import com.simplemobiletools.commons.R +import com.simplemobiletools.commons.helpers.MyContentProvider.Companion.COL_BACKGROUND_COLOR +import com.simplemobiletools.commons.helpers.MyContentProvider.Companion.COL_ID +import com.simplemobiletools.commons.helpers.MyContentProvider.Companion.COL_LAST_UPDATED_TS +import com.simplemobiletools.commons.helpers.MyContentProvider.Companion.COL_PRIMARY_COLOR +import com.simplemobiletools.commons.helpers.MyContentProvider.Companion.COL_TEXT_COLOR +import com.simplemobiletools.commons.helpers.MyContentProvider.Companion.fillThemeContentValues +import com.simplemobiletools.commons.models.SharedTheme + +class MyContentProviderDbHelper private constructor(private val context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) { + private val mDb: SQLiteDatabase = writableDatabase + + companion object { + private val DB_NAME = "Commons.db" + private val DB_VERSION = 1 + private val TABLE_NAME = "commons_colors" + private val THEME_ID = 1 // for now we are storing just 1 theme + + fun newInstance(context: Context) = MyContentProviderDbHelper(context) + } + + override fun onCreate(db: SQLiteDatabase) { + db.execSQL("CREATE TABLE $TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_TEXT_COLOR INTEGER DEFAULT 0, $COL_BACKGROUND_COLOR INTEGER DEFAULT 0," + + " $COL_PRIMARY_COLOR INTEGER DEFAULT 0, $COL_LAST_UPDATED_TS INTEGER DEFAULT 0)") + insertDefaultTheme(db) + } + + override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {} + + private fun insertDefaultTheme(db: SQLiteDatabase) { + val resources = context.resources + val theme = SharedTheme(resources.getColor(R.color.theme_dark_text_color), resources.getColor(R.color.theme_dark_background_color), + resources.getColor(R.color.color_primary)) + insertTheme(theme, db) + } + + private fun insertTheme(sharedTheme: SharedTheme, db: SQLiteDatabase) { + val values = fillThemeContentValues(sharedTheme) + db.insert(TABLE_NAME, null, values) + } + + fun updateTheme(values: ContentValues): Int { + val selection = "$COL_ID = ?" + val selectionArgs = arrayOf(THEME_ID.toString()) + return mDb.update(TABLE_NAME, values, selection, selectionArgs) + } + + fun getSharedTheme(): Cursor? { + val cols = arrayOf(COL_TEXT_COLOR, COL_BACKGROUND_COLOR, COL_PRIMARY_COLOR, COL_LAST_UPDATED_TS) + val selection = "$COL_ID = ?" + val selectionArgs = arrayOf(THEME_ID.toString()) + return mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null) + } +} diff --git a/build.gradle b/build.gradle index 1323953..89e92a1 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.1.61' + ext.kotlin_version = '1.2.0' repositories { jcenter()