adding the contentprovider used for shared themes
This commit is contained in:
parent
935d7d17b0
commit
4e398123b1
|
@ -38,7 +38,7 @@ android {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
compile 'com.simplemobiletools:commons:2.41.8'
|
||||
compile 'com.simplemobiletools:commons:2.41.19'
|
||||
}
|
||||
|
||||
Properties props = new Properties()
|
||||
|
|
|
@ -47,5 +47,9 @@
|
|||
android:label="@string/customize_colors"
|
||||
android:parentActivityName=".activities.SettingsActivity"/>
|
||||
|
||||
<provider
|
||||
android:name=".contentproviders.MyContentProvider"
|
||||
android:authorities="com.simplemobiletools.commons.provider"
|
||||
android:exported="true"/>
|
||||
</application>
|
||||
</manifest>
|
||||
|
|
|
@ -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<out String>?, selection: String?, selectionArgs: Array<out String>?, 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<out String>?) = dbHelper.updateTheme(contentValues)
|
||||
|
||||
override fun delete(uri: Uri, selection: String, selectionArgs: Array<out String>) = 0
|
||||
|
||||
override fun getType(uri: Uri) = ""
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue