Only use the central preferences

Only one SharedPreferences object exists (`launcherPreferences`)

Also: remove unused imports
This commit is contained in:
Finn M Glas 2020-06-18 10:48:54 +02:00
parent 68cf994c41
commit baeaac54b6
No known key found for this signature in database
GPG key ID: 902A30146014DFBF
8 changed files with 58 additions and 81 deletions

View file

@ -20,12 +20,13 @@ import android.widget.Toast
import com.finnmglas.launcher.list.ListActivity import com.finnmglas.launcher.list.ListActivity
import com.finnmglas.launcher.settings.SettingsActivity import com.finnmglas.launcher.settings.SettingsActivity
import com.finnmglas.launcher.settings.intendedSettingsPause import com.finnmglas.launcher.settings.intendedSettingsPause
import com.finnmglas.launcher.tutorial.TutorialActivity
import kotlin.math.roundToInt import kotlin.math.roundToInt
/** Preferences (Global, initialised when app is started) */ /** Preferences (global, initialised when app is started) */
lateinit var launcherPreferences: SharedPreferences lateinit var launcherPreferences: SharedPreferences
/** Variables for all of the app */ /** Variables containing settings */
var upApp = "" var upApp = ""
var downApp = "" var downApp = ""
var rightApp = "" var rightApp = ""
@ -138,15 +139,18 @@ private fun getIntent(packageName: String, context: Context): Intent? {
return intent return intent
} }
// select what to launch fun launch(data: String, activity: Activity,
fun launch(data: String, activity: Activity) { animationIn: Int = android.R.anim.fade_in, animationOut: Int = android.R.anim.fade_out) {
if (data.startsWith("launcher:")) // [type]:[info] if (data.startsWith("launcher:")) // [type]:[info]
when(data.split(":")[1]) { when(data.split(":")[1]) {
"settings" -> openSettings(activity) "settings" -> openSettings(activity)
"choose" -> openAppsList(activity) "choose" -> openAppsList(activity)
"tutorial" -> openTutorial(activity)
} }
else launchApp(data, activity) // app else launchApp(data, activity) // app
activity.overridePendingTransition(animationIn, animationOut)
} }
fun launchApp(packageName: String, context: Context) { fun launchApp(packageName: String, context: Context) {
@ -190,19 +194,13 @@ fun openNewTabWindow(urls: String, context : Context) {
/** Settings related functions */ /** Settings related functions */
fun getSavedTheme(context : Context) : String { fun getSavedTheme(context : Context) : String {
val sharedPref = context.getSharedPreferences( return launcherPreferences.getString("theme", "finn").toString()
context.getString(R.string.preference_file_key), Context.MODE_PRIVATE)
return sharedPref.getString("theme", "finn").toString()
} }
fun saveTheme(context : Context, themeName : String) : String { fun saveTheme(themeName : String) : String {
val sharedPref = context.getSharedPreferences( launcherPreferences.edit()
context.getString(R.string.preference_file_key), Context.MODE_PRIVATE) .putString("theme", themeName)
.apply()
val editor: SharedPreferences.Editor = sharedPref.edit()
editor.putString("theme", themeName)
editor.apply()
return themeName return themeName
} }
@ -217,6 +215,10 @@ fun openSettings(activity: Activity){
activity.startActivity(Intent(activity, SettingsActivity::class.java)) activity.startActivity(Intent(activity, SettingsActivity::class.java))
} }
fun openTutorial(activity: Activity){
activity.startActivity(Intent(activity, TutorialActivity::class.java))
}
fun openAppsList(activity: Activity){ fun openAppsList(activity: Activity){
val intent = Intent(activity, ListActivity::class.java) val intent = Intent(activity, ListActivity::class.java)
intent.putExtra("action", "view") intent.putExtra("action", "view")
@ -224,32 +226,32 @@ fun openAppsList(activity: Activity){
activity.startActivity(intent) activity.startActivity(intent)
} }
fun loadSettings(sharedPref : SharedPreferences){ fun loadSettings(){
upApp = sharedPref.getString("action_upApp", "").toString() upApp = launcherPreferences.getString("action_upApp", "").toString()
downApp = sharedPref.getString("action_downApp", "").toString() downApp = launcherPreferences.getString("action_downApp", "").toString()
rightApp = sharedPref.getString("action_rightApp", "").toString() rightApp = launcherPreferences.getString("action_rightApp", "").toString()
leftApp = sharedPref.getString("action_leftApp", "").toString() leftApp = launcherPreferences.getString("action_leftApp", "").toString()
volumeUpApp = sharedPref.getString("action_volumeUpApp", "").toString() volumeUpApp = launcherPreferences.getString("action_volumeUpApp", "").toString()
volumeDownApp = sharedPref.getString("action_volumeDownApp", "").toString() volumeDownApp = launcherPreferences.getString("action_volumeDownApp", "").toString()
doubleClickApp = sharedPref.getString("action_doubleClickApp", "").toString() doubleClickApp = launcherPreferences.getString("action_doubleClickApp", "").toString()
longClickApp = sharedPref.getString("action_longClickApp", "").toString() longClickApp = launcherPreferences.getString("action_longClickApp", "").toString()
calendarApp = sharedPref.getString("action_calendarApp", "").toString() calendarApp = launcherPreferences.getString("action_calendarApp", "").toString()
clockApp = sharedPref.getString("action_clockApp", "").toString() clockApp = launcherPreferences.getString("action_clockApp", "").toString()
dominantColor = sharedPref.getInt("custom_dominant", 0) dominantColor = launcherPreferences.getInt("custom_dominant", 0)
vibrantColor = sharedPref.getInt("custom_vibrant", 0) vibrantColor = launcherPreferences.getInt("custom_vibrant", 0)
} }
fun resetSettings(sharedPref : SharedPreferences, context: Context) : MutableList<String>{ fun resetSettings(context: Context) : MutableList<String>{
// set default theme // set default theme
saveTheme(context, "finn") saveTheme("finn")
val defaultList :MutableList<String> = mutableListOf<String>() val defaultList :MutableList<String> = mutableListOf<String>()
val editor: SharedPreferences.Editor = sharedPref.edit() val editor = launcherPreferences.edit()
val (chosenUpName, chosenUpPackage) = pickDefaultApp( val (chosenUpName, chosenUpPackage) = pickDefaultApp(
"action_upApp", "action_upApp",

View file

@ -46,7 +46,7 @@ class HomeActivity: UIObject, AppCompatActivity(),
launcherPreferences = this.getSharedPreferences( launcherPreferences = this.getSharedPreferences(
getString(R.string.preference_file_key), Context.MODE_PRIVATE) getString(R.string.preference_file_key), Context.MODE_PRIVATE)
loadSettings(launcherPreferences) loadSettings()
currentTheme = getSavedTheme(this) currentTheme = getSavedTheme(this)
// TODO: Don't use actual themes, rather create them on the fly // TODO: Don't use actual themes, rather create them on the fly
@ -83,7 +83,7 @@ class HomeActivity: UIObject, AppCompatActivity(),
windowManager.defaultDisplay.getMetrics(displayMetrics) windowManager.defaultDisplay.getMetrics(displayMetrics)
// for if the settings changed // for if the settings changed
loadSettings(launcherPreferences) loadSettings()
} }
override fun onResume() { override fun onResume() {
@ -143,22 +143,14 @@ class HomeActivity: UIObject, AppCompatActivity(),
val strictness = 4 // how distinguished the swipe has to be to be accepted val strictness = 4 // how distinguished the swipe has to be to be accepted
// Only open if the swipe was not from the phones top edge // Only open if the swipe was not from the phones top edge
if (diffY < -height / 8 && abs(diffY) > strictness * abs(diffX) && e1.y > 100) { if (diffY < -height / 8 && abs(diffY) > strictness * abs(diffX) && e1.y > 100)
launch(downApp,this) launch(downApp,this, R.anim.top_down)
overridePendingTransition(R.anim.top_down, android.R.anim.fade_out) else if (diffY > height / 8 && abs(diffY) > strictness * abs(diffX))
} launch(upApp, this, R.anim.bottom_up)
else if (diffY > height / 8 && abs(diffY) > strictness * abs(diffX)) { else if (diffX > width / 4 && abs(diffX) > strictness * abs(diffY))
launch(upApp, this) launch(leftApp,this, R.anim.right_left)
overridePendingTransition(R.anim.bottom_up, android.R.anim.fade_out) else if (diffX < -width / 4 && abs(diffX) > strictness * abs(diffY))
} launch(rightApp, this, R.anim.left_right)
else if (diffX > width / 4 && abs(diffX) > strictness * abs(diffY)) {
launch(leftApp,this)
overridePendingTransition(R.anim.right_left, android.R.anim.fade_out)
}
else if (diffX < -width / 4 && abs(diffX) > strictness * abs(diffY)) {
launch(rightApp, this)
overridePendingTransition(R.anim.left_right, android.R.anim.fade_out)
}
return true return true
} }
@ -218,7 +210,7 @@ class HomeActivity: UIObject, AppCompatActivity(),
} catch (e: Exception) { } } catch (e: Exception) { }
if (background == null) if (background == null)
currentTheme = saveTheme(this, "finn") currentTheme = saveTheme("finn")
} }
} }

View file

@ -11,7 +11,6 @@ import com.finnmglas.launcher.*
import com.finnmglas.launcher.settings.intendedSettingsPause import com.finnmglas.launcher.settings.intendedSettingsPause
import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.list.* import kotlinx.android.synthetic.main.list.*
import android.content.Context import android.content.Context
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager

View file

@ -4,8 +4,6 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.provider.Settings import android.provider.Settings
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.ViewPager import androidx.viewpager.widget.ViewPager
import com.finnmglas.launcher.* import com.finnmglas.launcher.*
@ -74,7 +72,7 @@ class SettingsActivity: AppCompatActivity(), UIObject {
.putString("action_$forApp", value.toString()) .putString("action_$forApp", value.toString())
.apply() .apply()
loadSettings(launcherPreferences) loadSettings()
} }
else -> super.onActivityResult(requestCode, resultCode, data) else -> super.onActivityResult(requestCode, resultCode, data)
} }

View file

@ -1,7 +1,6 @@
package com.finnmglas.launcher.settings.actions package com.finnmglas.launcher.settings.actions
import android.app.Activity import android.app.Activity
import android.content.Context
import android.content.Intent import android.content.Intent
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
@ -32,9 +31,6 @@ class ActionsRecyclerAdapter(val activity: Activity):
override fun onClick(v: View) { override fun onClick(v: View) {
val pos = adapterPosition val pos = adapterPosition
val context: Context = v.context
val content = actionsList[pos]
} }
init { itemView.setOnClickListener(this) } init { itemView.setOnClickListener(this) }
@ -49,9 +45,9 @@ class ActionsRecyclerAdapter(val activity: Activity):
viewHolder.removeAction.setOnClickListener{ viewHolder.removeAction.setOnClickListener{
val editor = launcherPreferences.edit() launcherPreferences.edit()
editor.putString("action_$actionName", "") // clear it .putString("action_$actionName", "") // clear it
editor.apply() .apply()
viewHolder.fontAwesome.visibility = View.INVISIBLE viewHolder.fontAwesome.visibility = View.INVISIBLE
viewHolder.img.visibility = View.INVISIBLE viewHolder.img.visibility = View.INVISIBLE

View file

@ -122,7 +122,7 @@ class SettingsFragmentMeta : Fragment(), UIObject {
.setMessage(getString(R.string.settings_reset_message)) .setMessage(getString(R.string.settings_reset_message))
.setPositiveButton(android.R.string.yes, .setPositiveButton(android.R.string.yes,
DialogInterface.OnClickListener { _, _ -> DialogInterface.OnClickListener { _, _ ->
resetSettings(launcherPreferences, this.context!!) resetSettings(this.context!!)
activity!!.finish() activity!!.finish()
}) })
.setNegativeButton(android.R.string.no, null) .setNegativeButton(android.R.string.no, null)

View file

@ -1,9 +1,7 @@
package com.finnmglas.launcher.settings.theme package com.finnmglas.launcher.settings.theme
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
@ -95,18 +93,13 @@ class SettingsFragmentTheme : Fragment(), UIObject {
} }
/* Save image Uri as string */ /* Save image Uri as string */
val editor: SharedPreferences.Editor = context!!.getSharedPreferences( launcherPreferences.edit()
context!!.getString(R.string.preference_file_key), Context.MODE_PRIVATE).edit() .putString("background_uri", imageUri.toString())
editor.putString("background_uri", imageUri.toString()) .putInt("custom_dominant", dominantColor)
editor.putInt("custom_dominant", .putInt("custom_vibrant", vibrantColor)
dominantColor .apply()
)
editor.putInt("custom_vibrant",
vibrantColor
)
editor.apply()
saveTheme(context!!, "custom") saveTheme("custom")
intendedSettingsPause = true intendedSettingsPause = true
activity!!.recreate() activity!!.recreate()
} }
@ -146,12 +139,12 @@ class SettingsFragmentTheme : Fragment(), UIObject {
// Theme changing buttons // Theme changing buttons
settings_theme_dark_button_select.setOnClickListener { settings_theme_dark_button_select.setOnClickListener {
intendedSettingsPause = true intendedSettingsPause = true
saveTheme(context!!, "dark") saveTheme("dark")
activity!!.recreate() activity!!.recreate()
} }
settings_theme_finn_button_select.setOnClickListener { settings_theme_finn_button_select.setOnClickListener {
intendedSettingsPause = true intendedSettingsPause = true
saveTheme(context!!, "finn") saveTheme("finn")
activity!!.recreate() activity!!.recreate()
} }
settings_theme_custom_button_select.setOnClickListener { settings_theme_custom_button_select.setOnClickListener {

View file

@ -41,10 +41,7 @@ class TutorialActivity : AppCompatActivity(), UIObject{
isFirstTime = !launcherPreferences.getBoolean("startedBefore", false) isFirstTime = !launcherPreferences.getBoolean("startedBefore", false)
if (isFirstTime) if (isFirstTime)
defaultApps = resetSettings( defaultApps = resetSettings(this) // UP, DOWN, RIGHT, LEFT, VOLUME_UP, VOLUME_DOWN
launcherPreferences,
this
) // UP, DOWN, RIGHT, LEFT, VOLUME_UP, VOLUME_DOWN
else else
tutorial_appbar.visibility = View.VISIBLE tutorial_appbar.visibility = View.VISIBLE
} }