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"> package="com.finnmglas.launcher">
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" /> <uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application <application
android:allowBackup="true" android:allowBackup="true"

View file

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

View file

@ -30,6 +30,12 @@ var clockApp = ""
var appsList : MutableList<ResolveInfo> = mutableListOf() 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 // Taken from https://stackoverflow.com/questions/47293269
fun View.blink( fun View.blink(
times: Int = Animation.INFINITE, times: Int = Animation.INFINITE,
@ -159,6 +165,10 @@ fun loadSettings(sharedPref : SharedPreferences){
} }
fun resetSettings(sharedPref : SharedPreferences, context: Context) : MutableList<String>{ fun resetSettings(sharedPref : SharedPreferences, context: Context) : MutableList<String>{
// set default theme
saveTheme(context, "finn")
val defaultList :MutableList<String> = mutableListOf<String>() val defaultList :MutableList<String> = mutableListOf<String>()
val editor: SharedPreferences.Editor = sharedPref.edit() val editor: SharedPreferences.Editor = sharedPref.edit()

View file

@ -88,15 +88,18 @@ class MainActivity : AppCompatActivity(),
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
// TODO: do this immediately after changing preferences
if (currentTheme != getSavedTheme(this)) recreate() if (currentTheme != getSavedTheme(this)) recreate()
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())
clockTimer = fixedRateTimer("clockTimer", true, 0L, 1000) { clockTimer = fixedRateTimer("clockTimer", true, 0L, 100) {
this@MainActivity.runOnUiThread { this@MainActivity.runOnUiThread {
dateView.text = dateFormat.format(Date()) val t = timeFormat.format(Date())
timeView.text = 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 package com.finnmglas.launcher
import android.Manifest
import android.app.AlertDialog import android.app.AlertDialog
import android.content.* 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.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.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
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.viewpager.widget.ViewPager import androidx.viewpager.widget.ViewPager
import com.finnmglas.launcher.ui.main.SectionsPagerAdapter import com.finnmglas.launcher.ui.main.SectionsPagerAdapter
import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.activity_settings.* import kotlinx.android.synthetic.main.activity_settings.*
import java.io.FileNotFoundException
import java.io.IOException
class SettingsActivity : AppCompatActivity() { class SettingsActivity : AppCompatActivity() {
@ -46,24 +56,64 @@ class SettingsActivity : AppCompatActivity() {
close_settings.setOnClickListener() { finish() } close_settings.setOnClickListener() { finish() }
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 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 when (requestCode) {
val sharedPref = this.getSharedPreferences( REQUEST_CHOOSE_APP -> {
getString(R.string.preference_file_key), Context.MODE_PRIVATE) val value = data?.getStringExtra("value")
val forApp = data?.getStringExtra("forApp") ?: return
val editor :SharedPreferences.Editor = sharedPref.edit() // Save the new App to Preferences
editor.putString("action_$forApp", value.toString()) val sharedPref = this.getSharedPreferences(
editor.apply() getString(R.string.preference_file_key), Context.MODE_PRIVATE)
loadSettings(sharedPref) val editor :SharedPreferences.Editor = sharedPref.edit()
} editor.putString("action_$forApp", value.toString())
else { editor.apply()
super.onActivityResult(requestCode, resultCode, data)
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) val intent = Intent(this, ChooseActivity::class.java)
intent.putExtra("action", "pick") intent.putExtra("action", "pick")
intent.putExtra("forApp", forAction) // for which action we choose the app intent.putExtra("forApp", forAction) // for which action we choose the app
startActivityForResult(intent, 5000) startActivityForResult(intent, REQUEST_CHOOSE_APP)
} }
fun chooseUninstallApp(view: View) { fun chooseUninstallApp(view: View) {
@ -197,8 +247,23 @@ class SettingsActivity : AppCompatActivity() {
} }
fun chooseCustomTheme(view: View) { fun chooseCustomTheme(view: View) {
Toast.makeText(this, "[not implemented yet]", Toast.LENGTH_SHORT) /*val intent = Intent()
.show() 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)
} }
} }