Make chosen background images persistent

- by saving the image Uri in SharedPreferences
This commit is contained in:
Finn M Glas 2020-05-24 19:24:38 +02:00
parent 0eecdff99f
commit 6c5df6189e
No known key found for this signature in database
GPG key ID: 25037A2E81AB459C
2 changed files with 19 additions and 2 deletions

View file

@ -2,8 +2,10 @@ package com.finnmglas.launcher
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri
import android.os.AsyncTask import android.os.AsyncTask
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore
import android.util.DisplayMetrics import android.util.DisplayMetrics
import android.view.* import android.view.*
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -51,8 +53,14 @@ class MainActivity : AppCompatActivity(),
currentTheme = getSavedTheme(this) currentTheme = getSavedTheme(this)
if (currentTheme == "custom" && background == null) if (currentTheme == "custom") {
try {
background = MediaStore.Images.Media.getBitmap(this.contentResolver, Uri.parse(sharedPref.getString("background_uri", "")))
} catch (e: Exception) { }
if (background == null)
currentTheme = saveTheme(this, "finn") currentTheme = saveTheme(this, "finn")
}
setTheme( setTheme(
when (currentTheme) { when (currentTheme) {

View file

@ -1,7 +1,9 @@
package com.finnmglas.launcher.settings package com.finnmglas.launcher.settings
import android.Manifest import android.Manifest
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
@ -92,6 +94,13 @@ class SettingsFragmentTheme : Fragment() {
if (data == null) return if (data == null) return
val imageUri = data.data val imageUri = data.data
/* Save image Uri as string */
val editor: SharedPreferences.Editor = context!!.getSharedPreferences(
context!!.getString(R.string.preference_file_key), Context.MODE_PRIVATE).edit()
editor.putString("background_uri", imageUri.toString())
editor.apply()
background = MediaStore.Images.Media.getBitmap(this.context!!.contentResolver, imageUri) background = MediaStore.Images.Media.getBitmap(this.context!!.contentResolver, imageUri)
saveTheme(this.context!!, "custom") saveTheme(this.context!!, "custom")