From e24b51f3bf583bfdadccf7d1962dcafd25aa1bd8 Mon Sep 17 00:00:00 2001 From: Finn M Glas Date: Sat, 23 May 2020 10:31:17 +0200 Subject: [PATCH] Add image picker functionality Not compledely finished at this point, but will be --- app/src/main/AndroidManifest.xml | 1 + .../com/finnmglas/launcher/ChooseActivity.kt | 8 +- .../java/com/finnmglas/launcher/Functions.kt | 10 ++ .../com/finnmglas/launcher/MainActivity.kt | 9 +- .../finnmglas/launcher/SettingsActivity.kt | 99 +++++++++++++++---- 5 files changed, 102 insertions(+), 25 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 79ccbd6..a6ab6d5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,7 @@ package="com.finnmglas.launcher"> + = mutableListOf() +/** REQUEST CODES */ + +val REQUEST_PICK_IMAGE = 1 +val REQUEST_CHOOSE_APP = 2 +val REQUEST_UNINSTALL = 3 + // Taken from https://stackoverflow.com/questions/47293269 fun View.blink( times: Int = Animation.INFINITE, @@ -159,6 +165,10 @@ fun loadSettings(sharedPref : SharedPreferences){ } fun resetSettings(sharedPref : SharedPreferences, context: Context) : MutableList{ + + // set default theme + saveTheme(context, "finn") + val defaultList :MutableList = mutableListOf() val editor: SharedPreferences.Editor = sharedPref.edit() diff --git a/app/src/main/java/com/finnmglas/launcher/MainActivity.kt b/app/src/main/java/com/finnmglas/launcher/MainActivity.kt index 66fb2eb..32a0f39 100644 --- a/app/src/main/java/com/finnmglas/launcher/MainActivity.kt +++ b/app/src/main/java/com/finnmglas/launcher/MainActivity.kt @@ -88,15 +88,18 @@ class MainActivity : AppCompatActivity(), override fun onResume() { super.onResume() + // TODO: do this immediately after changing preferences if (currentTheme != getSavedTheme(this)) recreate() val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) val timeFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault()) - clockTimer = fixedRateTimer("clockTimer", true, 0L, 1000) { + clockTimer = fixedRateTimer("clockTimer", true, 0L, 100) { this@MainActivity.runOnUiThread { - dateView.text = dateFormat.format(Date()) - timeView.text = timeFormat.format(Date()) + val t = timeFormat.format(Date()) + if (timeView.text != t) timeView.text = t + val d = dateFormat.format(Date()) + if (dateView.text != d) dateView.text = d } } diff --git a/app/src/main/java/com/finnmglas/launcher/SettingsActivity.kt b/app/src/main/java/com/finnmglas/launcher/SettingsActivity.kt index 35e4e5e..9d8f27e 100644 --- a/app/src/main/java/com/finnmglas/launcher/SettingsActivity.kt +++ b/app/src/main/java/com/finnmglas/launcher/SettingsActivity.kt @@ -1,19 +1,29 @@ package com.finnmglas.launcher +import android.Manifest import android.app.AlertDialog import android.content.* +import android.database.Cursor +import android.graphics.Bitmap +import android.graphics.ImageDecoder +import android.graphics.drawable.BitmapDrawable import android.net.Uri import android.os.Build import android.os.Bundle +import android.provider.MediaStore import android.provider.Settings +import android.view.Gravity import android.view.View import android.view.WindowManager import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat import androidx.viewpager.widget.ViewPager import com.finnmglas.launcher.ui.main.SectionsPagerAdapter import com.google.android.material.tabs.TabLayout import kotlinx.android.synthetic.main.activity_settings.* +import java.io.FileNotFoundException +import java.io.IOException class SettingsActivity : AppCompatActivity() { @@ -46,24 +56,64 @@ class SettingsActivity : AppCompatActivity() { close_settings.setOnClickListener() { finish() } } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - if(requestCode == 5000) - { - val value = data?.getStringExtra("value") - val forApp = data?.getStringExtra("forApp") ?: return - // Save the new App to Preferences - val sharedPref = this.getSharedPreferences( - getString(R.string.preference_file_key), Context.MODE_PRIVATE) + when (requestCode) { + REQUEST_CHOOSE_APP -> { + val value = data?.getStringExtra("value") + val forApp = data?.getStringExtra("forApp") ?: return - val editor :SharedPreferences.Editor = sharedPref.edit() - editor.putString("action_$forApp", value.toString()) - editor.apply() + // Save the new App to Preferences + val sharedPref = this.getSharedPreferences( + getString(R.string.preference_file_key), Context.MODE_PRIVATE) - loadSettings(sharedPref) - } - else { - super.onActivityResult(requestCode, resultCode, data) + val editor :SharedPreferences.Editor = sharedPref.edit() + editor.putString("action_$forApp", value.toString()) + editor.apply() + + loadSettings(sharedPref) + } + + REQUEST_PICK_IMAGE -> { + + 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) } } @@ -79,7 +129,7 @@ class SettingsActivity : AppCompatActivity() { val intent = Intent(this, ChooseActivity::class.java) intent.putExtra("action", "pick") intent.putExtra("forApp", forAction) // for which action we choose the app - startActivityForResult(intent, 5000) + startActivityForResult(intent, REQUEST_CHOOSE_APP) } fun chooseUninstallApp(view: View) { @@ -197,8 +247,23 @@ class SettingsActivity : AppCompatActivity() { } fun chooseCustomTheme(view: View) { - Toast.makeText(this, "[not implemented yet]", Toast.LENGTH_SHORT) - .show() + /*val intent = Intent() + intent.type = "image/*" + intent.action = Intent.ACTION_GET_CONTENT + startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_PICK_IMAGE)*/ + + */ + + // 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()) + startActivityForResult(intent, REQUEST_PICK_IMAGE) + } }