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

View file

@ -46,7 +46,7 @@ class HomeActivity: UIObject, AppCompatActivity(),
launcherPreferences = this.getSharedPreferences(
getString(R.string.preference_file_key), Context.MODE_PRIVATE)
loadSettings(launcherPreferences)
loadSettings()
currentTheme = getSavedTheme(this)
// TODO: Don't use actual themes, rather create them on the fly
@ -83,7 +83,7 @@ class HomeActivity: UIObject, AppCompatActivity(),
windowManager.defaultDisplay.getMetrics(displayMetrics)
// for if the settings changed
loadSettings(launcherPreferences)
loadSettings()
}
override fun onResume() {
@ -143,22 +143,14 @@ class HomeActivity: UIObject, AppCompatActivity(),
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
if (diffY < -height / 8 && abs(diffY) > strictness * abs(diffX) && e1.y > 100) {
launch(downApp,this)
overridePendingTransition(R.anim.top_down, android.R.anim.fade_out)
}
else if (diffY > height / 8 && abs(diffY) > strictness * abs(diffX)) {
launch(upApp, this)
overridePendingTransition(R.anim.bottom_up, android.R.anim.fade_out)
}
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)
}
if (diffY < -height / 8 && abs(diffY) > strictness * abs(diffX) && e1.y > 100)
launch(downApp,this, R.anim.top_down)
else if (diffY > height / 8 && abs(diffY) > strictness * abs(diffX))
launch(upApp, this, R.anim.bottom_up)
else if (diffX > width / 4 && abs(diffX) > strictness * abs(diffY))
launch(leftApp,this, R.anim.right_left)
else if (diffX < -width / 4 && abs(diffX) > strictness * abs(diffY))
launch(rightApp, this, R.anim.left_right)
return true
}
@ -218,7 +210,7 @@ class HomeActivity: UIObject, AppCompatActivity(),
} catch (e: Exception) { }
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.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.list.*
import android.content.Context
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager

View file

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

View file

@ -1,7 +1,6 @@
package com.finnmglas.launcher.settings.actions
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
@ -32,9 +31,6 @@ class ActionsRecyclerAdapter(val activity: Activity):
override fun onClick(v: View) {
val pos = adapterPosition
val context: Context = v.context
val content = actionsList[pos]
}
init { itemView.setOnClickListener(this) }
@ -49,9 +45,9 @@ class ActionsRecyclerAdapter(val activity: Activity):
viewHolder.removeAction.setOnClickListener{
val editor = launcherPreferences.edit()
editor.putString("action_$actionName", "") // clear it
editor.apply()
launcherPreferences.edit()
.putString("action_$actionName", "") // clear it
.apply()
viewHolder.fontAwesome.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))
.setPositiveButton(android.R.string.yes,
DialogInterface.OnClickListener { _, _ ->
resetSettings(launcherPreferences, this.context!!)
resetSettings(this.context!!)
activity!!.finish()
})
.setNegativeButton(android.R.string.no, null)

View file

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

View file

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