Add image picker functionality

Not compledely finished at this point, but will be
This commit is contained in:
Finn M Glas 2020-05-23 10:31:17 +02:00
parent 8a6dd32360
commit e24b51f3bf
No known key found for this signature in database
GPG key ID: 25037A2E81AB459C
5 changed files with 102 additions and 25 deletions

View file

@ -4,6 +4,7 @@
package="com.finnmglas.launcher">
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"

View file

@ -15,8 +15,6 @@ import kotlinx.android.synthetic.main.activity_choose.*
class ChooseActivity : AppCompatActivity() {
val UNINSTALL_REQUEST_CODE = 1
/** Activity Lifecycle functions */
override fun onCreate(savedInstanceState: Bundle?) {
@ -69,7 +67,7 @@ class ChooseActivity : AppCompatActivity() {
returnIntent.putExtra("value", app.packageName)
returnIntent.putExtra("forApp", forApp)
setResult(
5000,
REQUEST_CHOOSE_APP,
returnIntent
)
finish()
@ -80,7 +78,7 @@ class ChooseActivity : AppCompatActivity() {
val intent = Intent(Intent.ACTION_UNINSTALL_PACKAGE)
intent.data = Uri.parse("package:" + app.packageName)
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true)
startActivityForResult(intent, UNINSTALL_REQUEST_CODE)
startActivityForResult(intent, REQUEST_UNINSTALL)
}
}
apps_list.addView(tvdynamic)
@ -89,7 +87,7 @@ class ChooseActivity : AppCompatActivity() {
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == UNINSTALL_REQUEST_CODE) {
if (requestCode == REQUEST_UNINSTALL) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, getString(R.string.choose_removed_toast), Toast.LENGTH_LONG).show()
updateAppList(packageManager)

View file

@ -30,6 +30,12 @@ var clockApp = ""
var appsList : MutableList<ResolveInfo> = 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<String>{
// set default theme
saveTheme(context, "finn")
val defaultList :MutableList<String> = mutableListOf<String>()
val editor: SharedPreferences.Editor = sharedPref.edit()

View file

@ -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
}
}

View file

@ -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)
}
}