Shortcut modifications are now saved.

This commit is contained in:
ottoptj 2024-05-19 18:31:22 +03:00
commit 597540b1c6
5 changed files with 53 additions and 10 deletions

View file

@ -44,10 +44,10 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener,
private lateinit var menuMode: String private lateinit var menuMode: String
companion object { companion object {
private lateinit var callback: (Pair<String, Pair<LauncherActivityInfo, UserHandle>>) -> Unit private lateinit var callback: (Pair<Pair<String, Int>, Pair<LauncherActivityInfo, UserHandle>>) -> Unit
private const val MENU_MODE = "abcd" private const val MENU_MODE = "abcd"
fun start(context: Context, param1: String = "app", callback: (Pair<String, Pair<LauncherActivityInfo, UserHandle>>) -> Unit) { fun start(context: Context, param1: String = "app", callback: (Pair<Pair<String, Int>, Pair<LauncherActivityInfo, UserHandle>>) -> Unit) {
val intent = Intent(context, AppMenuActivity::class.java).apply { val intent = Intent(context, AppMenuActivity::class.java).apply {
putExtra(MENU_MODE, param1) putExtra(MENU_MODE, param1)
} }
@ -88,8 +88,8 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener,
} }
} }
override fun onShortcut(appInfo: LauncherActivityInfo, userHandle: UserHandle, textView: TextView) { override fun onShortcut(appInfo: LauncherActivityInfo, userHandle: UserHandle, textView: TextView, userProfile: Int) {
callback.invoke(Pair(textView.text.toString(), Pair(appInfo, userHandle,))) callback.invoke(Pair(Pair(textView.text.toString(), userProfile), Pair(appInfo, userHandle,)))
finish() finish()
} }

View file

@ -31,7 +31,7 @@ class AppMenuAdapter(
} }
interface OnShortcutListener { interface OnShortcutListener {
fun onShortcut(appInfo: LauncherActivityInfo, userHandle: UserHandle, textView: TextView) fun onShortcut(appInfo: LauncherActivityInfo, userHandle: UserHandle, textView: TextView, userProfile: Int)
} }
interface OnItemLongClickListener { interface OnItemLongClickListener {
@ -60,7 +60,7 @@ class AppMenuAdapter(
if (position != RecyclerView.NO_POSITION) { if (position != RecyclerView.NO_POSITION) {
val app = apps[position].first val app = apps[position].first
if (menuMode == "shortcut") { if (menuMode == "shortcut") {
shortcutListener.onShortcut(app, apps[position].second.first, textView) shortcutListener.onShortcut(app, apps[position].second.first, textView, apps[position].second.second )
} }
else if (menuMode == "app") { else if (menuMode == "app") {
itemClickListener.onItemClick(app, apps[position].second.first) itemClickListener.onItemClick(app, apps[position].second.first)

View file

@ -24,6 +24,7 @@ class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding private lateinit var binding: ActivityMainBinding
private lateinit var gestureDetector: GestureDetector private lateinit var gestureDetector: GestureDetector
private lateinit var launcherApps: LauncherApps private lateinit var launcherApps: LauncherApps
private val sharedPreferenceManager = SharedPreferenceManager()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -35,15 +36,25 @@ class MainActivity : AppCompatActivity() {
for (i in findViewById<LinearLayout>(R.id.shortcuts).children) { for (i in findViewById<LinearLayout>(R.id.shortcuts).children) {
var textView = i as TextView val textView = i as TextView
i.setOnClickListener { val savedView = sharedPreferenceManager.getShortcut(this, textView)
Log.d("hHJKJFAF", "Click done")
if (savedView?.get(1) != "e") {
textView.text = savedView?.get(2)
textView.setOnClickListener {
val mainActivity = launcherApps.getActivityList(savedView?.get(0).toString(), launcherApps.profiles[savedView?.get(1)!!.toInt()]).firstOrNull()
if (mainActivity != null) {
launcherApps.startMainActivity(mainActivity.componentName, launcherApps.profiles[savedView[1]!!.toInt()], null, null)
} else {
Toast.makeText(this, "Cannot launch app", Toast.LENGTH_SHORT).show()
}
}
} }
i.setOnLongClickListener { i.setOnLongClickListener {
AppMenuActivity.start(this@MainActivity, "shortcut") { newText -> AppMenuActivity.start(this@MainActivity, "shortcut") { newText ->
textView.text = newText.first textView.text = newText.first.first
i.setOnClickListener { i.setOnClickListener {
val mainActivity = launcherApps.getActivityList(newText.second.first.applicationInfo.packageName, newText.second.second).firstOrNull() val mainActivity = launcherApps.getActivityList(newText.second.first.applicationInfo.packageName, newText.second.second).firstOrNull()
if (mainActivity != null) { if (mainActivity != null) {
@ -52,6 +63,7 @@ class MainActivity : AppCompatActivity() {
Toast.makeText(this, "Cannot launch app", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Cannot launch app", Toast.LENGTH_SHORT).show()
} }
} }
sharedPreferenceManager.setShortcut(this, textView, newText.second.first.applicationInfo.packageName, newText.first.second)
} }
return@setOnLongClickListener true return@setOnLongClickListener true

View file

@ -1,10 +1,25 @@
package eu.ottop.yamlauncher package eu.ottop.yamlauncher
import android.content.Context import android.content.Context
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
class SharedPreferenceManager { class SharedPreferenceManager {
fun setShortcut(cont: Context, textView: TextView, packageName: String, profile: Int) {
val editor = cont.getSharedPreferences("shortcuts", AppCompatActivity.MODE_PRIVATE).edit()
val key = textView.id.toString()
editor.putString(key, "$packageName-$profile-${textView.text}")
editor.apply()
}
fun getShortcut(cont: Context, textView: TextView): List<String>? {
val sharedPref = cont.getSharedPreferences("shortcuts", AppCompatActivity.MODE_PRIVATE)
val key = textView.id.toString()
val value = sharedPref.getString(key, "e-e")
return value?.split("-")
}
fun setAppHidden(cont: Context, packageName: String, profile: Int, hidden: Boolean) { fun setAppHidden(cont: Context, packageName: String, profile: Int, hidden: Boolean) {
val editor = cont.getSharedPreferences("hidden_apps", AppCompatActivity.MODE_PRIVATE).edit() val editor = cont.getSharedPreferences("hidden_apps", AppCompatActivity.MODE_PRIVATE).edit()
val key = "$packageName-$profile" val key = "$packageName-$profile"

View file

@ -93,6 +93,22 @@
android:textColor="#F3F3F3" android:textColor="#F3F3F3"
android:textSize="28sp" android:textSize="28sp"
android:visibility="visible" /> android:visibility="visible" />
<TextView
android:id="@+id/app5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:gravity="start"
android:paddingLeft="40dp"
android:paddingTop="20dp"
android:paddingRight="40dp"
android:paddingBottom="20dp"
android:text="App"
android:textAppearance="@android:style/TextAppearance.DeviceDefault"
android:textColor="#F3F3F3"
android:textSize="28sp"
android:visibility="visible" />
</LinearLayout> </LinearLayout>
<Space <Space