Implement choosing of background images

'Custom theme' can now be applied
This commit is contained in:
Finn M Glas 2020-05-23 17:24:55 +02:00
parent 1d540f7d37
commit 03093125f7
No known key found for this signature in database
GPG key ID: 25037A2E81AB459C
4 changed files with 54 additions and 61 deletions

View file

@ -8,6 +8,7 @@ import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.pm.ResolveInfo import android.content.pm.ResolveInfo
import android.graphics.Bitmap
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.Settings import android.provider.Settings
@ -28,11 +29,16 @@ var clockApp = ""
var appsList : MutableList<ResolveInfo> = mutableListOf() var appsList : MutableList<ResolveInfo> = mutableListOf()
var background : Bitmap? = null
/** REQUEST CODES */ /** REQUEST CODES */
val REQUEST_PICK_IMAGE = 1 val REQUEST_PICK_IMAGE = 1
val REQUEST_CHOOSE_APP = 2 val REQUEST_CHOOSE_APP = 2
val REQUEST_UNINSTALL = 3 val REQUEST_UNINSTALL = 3
val REQUEST_PERMISSION_STORAGE = 4
/** Animate */
// Taken from https://stackoverflow.com/questions/47293269 // Taken from https://stackoverflow.com/questions/47293269
fun View.blink( fun View.blink(

View file

@ -6,9 +6,6 @@ import android.os.AsyncTask
import android.os.Bundle import android.os.Bundle
import android.util.DisplayMetrics import android.util.DisplayMetrics
import android.view.* import android.view.*
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GestureDetectorCompat import androidx.core.view.GestureDetectorCompat
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
@ -52,6 +49,7 @@ class MainActivity : AppCompatActivity(),
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
currentTheme = getSavedTheme(this) currentTheme = getSavedTheme(this)
setTheme( setTheme(
when (currentTheme) { when (currentTheme) {
"dark" -> R.style.darkTheme "dark" -> R.style.darkTheme
@ -94,6 +92,9 @@ class MainActivity : AppCompatActivity(),
// TODO: do this immediately after changing preferences // TODO: do this immediately after changing preferences
if (currentTheme != getSavedTheme(this)) recreate() if (currentTheme != getSavedTheme(this)) recreate()
if (background != null) {
background_image.setImageBitmap(background)
}
val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
val timeFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault()) val timeFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault())

View file

@ -3,16 +3,12 @@ package com.finnmglas.launcher
import android.Manifest import android.Manifest
import android.app.AlertDialog import android.app.AlertDialog
import android.content.* import android.content.*
import android.database.Cursor import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.ImageDecoder
import android.graphics.drawable.BitmapDrawable
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore import android.provider.MediaStore
import android.provider.Settings import android.provider.Settings
import android.view.Gravity
import android.view.View import android.view.View
import android.view.WindowManager import android.view.WindowManager
import android.widget.Toast import android.widget.Toast
@ -24,8 +20,6 @@ import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.activity_settings.* import kotlinx.android.synthetic.main.activity_settings.*
import kotlinx.android.synthetic.main.activity_settings.container import kotlinx.android.synthetic.main.activity_settings.container
import kotlinx.android.synthetic.main.fragment_settings_theme.* import kotlinx.android.synthetic.main.fragment_settings_theme.*
import java.io.FileNotFoundException
import java.io.IOException
class SettingsActivity : AppCompatActivity() { class SettingsActivity : AppCompatActivity() {
@ -92,44 +86,8 @@ class SettingsActivity : AppCompatActivity() {
loadSettings(sharedPref) loadSettings(sharedPref)
} }
REQUEST_PICK_IMAGE -> { REQUEST_PERMISSION_STORAGE -> letUserPickImage()
REQUEST_PICK_IMAGE -> handlePickedImage(resultCode, data)
if (resultCode == RESULT_OK) {
if (data != null) {
val selectedImage: Uri? = data.data
var bitmap: Bitmap? = null
try {
// different SDKs, different image choosing
if (Build.VERSION.SDK_INT >= 28) {
container.background = ImageDecoder.decodeDrawable(
ImageDecoder.createSource(
this.contentResolver, selectedImage!!))
} else {
val b = BitmapDrawable(
MediaStore.Images.Media.getBitmap(this.contentResolver, selectedImage)
)
b.gravity = Gravity.CENTER
container.background = b
}
Toast.makeText(this, "Chose", Toast.LENGTH_SHORT).show()
//val _image : ImageView = background_img
//_image.setImageBitmap(bitmap)
} catch (e: FileNotFoundException) {
Toast.makeText(this, "File not found", Toast.LENGTH_SHORT).show()
e.printStackTrace()
} catch (e: IOException) {
Toast.makeText(this, "IO Except", Toast.LENGTH_SHORT).show()
e.printStackTrace()
}
}
}
}
else -> super.onActivityResult(requestCode, resultCode, data) else -> super.onActivityResult(requestCode, resultCode, data)
} }
} }
@ -264,23 +222,43 @@ class SettingsActivity : AppCompatActivity() {
} }
fun chooseCustomTheme(view: View) { fun chooseCustomTheme(view: View) {
/*val intent = Intent()
// Request permission (on newer APIs)
if (Build.VERSION.SDK_INT >= 23) {
when {
ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
-> letUserPickImage()
shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)
-> {}
else
-> requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_PERMISSION_STORAGE)
}
}
else letUserPickImage()
}
private fun letUserPickImage() {
val intent = Intent()
intent.type = "image/*" intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT intent.action = Intent.ACTION_PICK // other option: Intent.ACTION_GET_CONTENT
startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_PICK_IMAGE)*/ //intent.putExtra("crop", "true")
*/
// TODO: Runtime request permisson on newer APIs
val intent : Intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_PICK
intent.putExtra("crop", "true")
//intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()) //intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString())
startActivityForResult(intent, REQUEST_PICK_IMAGE) startActivityForResult(intent, REQUEST_PICK_IMAGE)
}
private fun handlePickedImage(resultCode: Int, data: Intent?) {
if (resultCode == RESULT_OK) {
if (data == null) return
//BitmapFactory.(data.data)
val imageUri = data.data
background = MediaStore.Images.Media.getBitmap(this.contentResolver, imageUri)
saveTheme(this, "custom")
recreate()
}
} }
} }

View file

@ -9,6 +9,14 @@
android:longClickable="false" android:longClickable="false"
tools:context=".MainActivity"> tools:context=".MainActivity">
<ImageView
android:id="@+id/background_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:orientation="vertical"
android:scaleType="centerCrop" />
<TextView <TextView
android:id="@+id/dateView" android:id="@+id/dateView"
android:layout_width="wrap_content" android:layout_width="wrap_content"