Changed how the shortcut information is stored in the sharedpreferences.

This commit is contained in:
ottoptj 2024-11-20 01:12:47 +02:00
commit ec512b25e6
7 changed files with 35 additions and 26 deletions

View file

@ -35,6 +35,7 @@ class AppMenuAdapter(
RecyclerView.Adapter<AppMenuAdapter.AppViewHolder>() {
// If the menu is opened to select shortcuts, the below variable is set
var shortcutIndex: Int = 0
var shortcutTextView: TextView? = null
private val sharedPreferenceManager = SharedPreferenceManager(activity)
@ -47,7 +48,7 @@ class AppMenuAdapter(
}
interface OnShortcutListener {
fun onShortcut(appInfo: LauncherActivityInfo, userHandle: UserHandle, textView: TextView, userProfile: Int, shortcutView: TextView)
fun onShortcut(appInfo: LauncherActivityInfo, userHandle: UserHandle, textView: TextView, userProfile: Int, shortcutView: TextView, shortcutIndex: Int)
}
interface OnItemLongClickListener {
@ -71,7 +72,7 @@ class AppMenuAdapter(
// If opened to select a shortcut, set the shortcut instead of launching the app
if (shortcutTextView != null) {
shortcutListener.onShortcut(app, apps[position].second, textView, apps[position].third, shortcutTextView!!)
shortcutListener.onShortcut(app, apps[position].second, textView, apps[position].third, shortcutTextView!!, shortcutIndex)
}
else {
itemClickListener.onItemClick(app, apps[position].second)
@ -85,7 +86,7 @@ class AppMenuAdapter(
// If opened to select a shortcut, set the shortcut instead of opening the action menu
if (shortcutTextView != null) {
shortcutListener.onShortcut(app, apps[position].second, textView, apps[position].third, shortcutTextView!!)
shortcutListener.onShortcut(app, apps[position].second, textView, apps[position].third, shortcutTextView!!, shortcutIndex)
return@setOnLongClickListener true
} else {

View file

@ -20,6 +20,7 @@ class ContactsAdapter(
) :
RecyclerView.Adapter<ContactsAdapter.AppViewHolder>() {
var shortcutIndex: Int = 0
var shortcutTextView: TextView? = null
private val uiUtils = UIUtils(activity)
@ -30,7 +31,7 @@ class ContactsAdapter(
}
interface OnContactShortcutListener {
fun onContactShortcut(contactId: Int, contactName: String, shortcutView: TextView)
fun onContactShortcut(contactId: Int, contactName: String, shortcutView: TextView, shortcutIndex: Int)
}
inner class AppViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
@ -42,7 +43,7 @@ class ContactsAdapter(
if (shortcutTextView != null) {
val position = bindingAdapterPosition
val contact = contacts[position]
contactShortcutListener.onContactShortcut(contact.second, contact.first, shortcutTextView!!)
contactShortcutListener.onContactShortcut(contact.second, contact.first, shortcutTextView!!, shortcutIndex)
} else {
val position = bindingAdapterPosition
val contact = contacts[position]

View file

@ -211,12 +211,12 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
else {
textView.visibility = View.VISIBLE
val savedView = sharedPreferenceManager.getShortcut(textView)
val savedView = sharedPreferenceManager.getShortcut(i)
// Set the non-work profile drawable by default
textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_empty, null),null,null,null)
shortcutListeners(textView, savedView)
shortcutListeners(i, textView, savedView)
if (savedView?.get(1) != "e") {
setShortcutSetup(textView, savedView)
@ -231,7 +231,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
}
@SuppressLint("ClickableViewAccessibility")
private fun shortcutListeners(textView: TextView, savedView: List<String>?) {
private fun shortcutListeners(index: Int, textView: TextView, savedView: List<String>?) {
// Don't go to settings on long click, but keep other gestures functional
textView.setOnTouchListener {_, event ->
shortcutGestureDetector.onTouchEvent(event)
@ -245,7 +245,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
menuTitle.setText(textView.text)
menuTitle.visibility = View.VISIBLE
if (savedView != null) {
setRenameShortcutListener(textView)
setRenameShortcutListener(index, textView)
}
appAdapter?.shortcutTextView = textView
@ -271,9 +271,11 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
menuTitle.setText(textView.text)
menuTitle.visibility = View.VISIBLE
if (savedView != null) {
setRenameShortcutListener(textView)
setRenameShortcutListener(index, textView)
}
appAdapter?.shortcutIndex = index
appAdapter?.shortcutTextView = textView
contactAdapter?.shortcutIndex = index
contactAdapter?.shortcutTextView = textView
toAppMenu()
@ -281,7 +283,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
}
}
private fun setRenameShortcutListener(textView: TextView) {
private fun setRenameShortcutListener(index: Int, textView: TextView) {
menuTitle.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
@ -292,18 +294,20 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
val imm =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(menuTitle.windowToken, 0)
val savedView = sharedPreferenceManager.getShortcut(textView)!!
val savedView = sharedPreferenceManager.getShortcut(index)!!
textView.text = menuTitle.text
try {
sharedPreferenceManager.setShortcut(
textView,
index,
textView.text,
savedView[0],
savedView[1].toInt(),
savedView.getOrNull(3)?.toBoolean() ?: false
)
} catch (_: NumberFormatException) {
sharedPreferenceManager.setShortcut(
textView,
index,
textView.text,
savedView[0],
0,
savedView.getOrNull(3)?.toBoolean() ?: false
@ -1052,7 +1056,8 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
userHandle: UserHandle,
textView: TextView,
userProfile: Int,
shortcutView: TextView
shortcutView: TextView,
shortcutIndex: Int
) {
if (userProfile != 0) {
shortcutView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_work_app, null),null,null,null)
@ -1070,7 +1075,8 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
appUtils.launchApp(appInfo.applicationInfo.packageName, userHandle)
}
sharedPreferenceManager.setShortcut(
shortcutView,
shortcutIndex,
shortcutView.text,
appInfo.applicationInfo.packageName,
userProfile
)
@ -1183,13 +1189,14 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
returnAllowed = false
}
override fun onContactShortcut(contactId: Int, contactName: String, shortcutView: TextView) {
override fun onContactShortcut(contactId: Int, contactName: String, shortcutView: TextView, shortcutIndex: Int) {
shortcutView.text = contactName
shortcutView.setOnClickListener {
onContactClick(contactId)
}
sharedPreferenceManager.setShortcut(
shortcutView,
shortcutIndex,
shortcutView.text,
contactName,
contactId,
true

View file

@ -4,7 +4,6 @@ import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.util.TypedValue
import android.widget.TextView
import androidx.preference.PreferenceManager
import eu.ottop.yamlauncher.R
@ -89,14 +88,14 @@ class SharedPreferenceManager (private val context: Context) {
return preferences.getString("dateSize", "medium")
}
fun setShortcut(textView: TextView, packageName: String, profile: Int, isContact: Boolean = false) {
fun setShortcut(index: Int, text: CharSequence, packageName: String, profile: Int, isContact: Boolean = false) {
val editor = preferences.edit()
editor.putString("shortcut${textView.id}", "$packageName§splitter§$profile§splitter§${textView.text}§splitter§${isContact}")
editor.putString("shortcut${index}", "$packageName§splitter§$profile§splitter§${text}§splitter§${isContact}")
editor.apply()
}
fun getShortcut(textView: TextView): List<String>? {
val value = preferences.getString("shortcut${textView.id}", "e§splitter§e§splitter§e§splitter§e")
fun getShortcut(index: Int): List<String>? {
val value = preferences.getString("shortcut${index}", "e§splitter§e§splitter§e§splitter§e")
return value?.split("§splitter§")
}

View file

@ -126,6 +126,7 @@
<string name="enable_search">Suche aktivieren</string>
<string name="search_alignment">Ausrichtung der Suche</string>
<string name="search_size">Größe der Suche</string>
<string name="enable_fuzzy_search">Fuzzy Search</string>
<string name="automatically_open_keyboard">Tastatur automatisch öffnen</string>
<string name="automatic_app_opening">Automatisches öffnen der Apps</string>
<string name="auto_launch_summary">App automatisch öffnen, wenn sie das letzte Suchergebnis ist</string>
@ -155,7 +156,7 @@
<string name="stripe_link"><![CDATA[<a href="https://donate.stripe.com/14k6s2bMJdnDgtW288">Stripe</a><br>(one-time)]]></string>
<string name="weather_link"><![CDATA[Weather data by <a href="https://open-meteo.com/">Open-Meteo.com</a><br>(<a href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>)]]></string>
<string name="reset">Zurücksetzen</string>
<string name="reset">Reset</string>
<string name="restart_text">YAM Launcher neu starten</string>
<string name="reset_text">Alle Einstellungen zurücksetzen</string>
<string name="reset_confirm_text">Du wirst ALLE Änderungen verlieren, die du an den Launcher-Einstellungen, Verknüpfungen, versteckten Apps usw. vorgenommen hast.\n\nBist du sicher?</string>

View file

@ -124,7 +124,7 @@
<string name="enable_search">Näytä Hakupalkki</string>
<string name="search_alignment">Hakupalkin Sijainti</string>
<string name="search_size">Hakupalkin Koko</string>
<string name="enable_fuzzy_search">Ota sumea haku käyttöön</string>
<string name="enable_fuzzy_search">Sumea haku</string>
<string name="automatically_open_keyboard">Avaa Näppäimistö Automaattisesti</string>
<string name="automatic_app_opening">Avaa Hakutulos Automaattisesti</string>
<string name="auto_launch_summary">Avaa sovellus automaattisesti kun se on viimeinen hakutulos</string>

View file

@ -126,7 +126,7 @@
<string name="enable_search">Enable Search</string>
<string name="search_alignment">Search Alignment</string>
<string name="search_size">Search Size</string>
<string name="enable_fuzzy_search">Enable Fuzzy Search</string>
<string name="enable_fuzzy_search">Fuzzy Search</string>
<string name="automatically_open_keyboard">Automatically Open Keyboard</string>
<string name="automatic_app_opening">Automatic App Opening</string>
<string name="auto_launch_summary">Automatically launch an app when it\'s the last search result</string>