An editable text field for renaming apps has been added

This commit is contained in:
ottoptj 2024-05-08 00:11:15 +03:00
commit 109b849a6e
5 changed files with 83 additions and 22 deletions

View file

@ -40,12 +40,12 @@ android {
dependencies { dependencies {
implementation("androidx.core:core-ktx:1.12.0") implementation("androidx.core:core-ktx:1.13.1")
implementation("androidx.appcompat:appcompat:1.6.1") implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0") implementation("com.google.android.material:material:1.12.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4") implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.navigation:navigation-fragment-ktx:2.6.0") implementation("androidx.navigation:navigation-fragment-ktx:2.7.7")
implementation("androidx.navigation:navigation-ui-ktx:2.6.0") implementation("androidx.navigation:navigation-ui-ktx:2.7.7")
implementation("com.google.code.gson:gson:2.10") implementation("com.google.code.gson:gson:2.10")
testImplementation("junit:junit:4.13.2") testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.ext:junit:1.1.5")

View file

@ -19,7 +19,7 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.YamLauncher" android:theme="@style/Theme.YamLauncher"
tools:targetApi="31"> tools:targetApi="34">
<activity <activity
android:name=".AppMenuActivity" android:name=".AppMenuActivity"
android:screenOrientation="portrait" android:screenOrientation="portrait"

View file

@ -9,22 +9,30 @@ import android.content.res.ColorStateList
import android.graphics.Color import android.graphics.Color
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.UserHandle import android.os.UserHandle
import android.view.Gravity import android.view.Gravity
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.inputmethod.InputMethodManager
import android.webkit.RenderProcessGoneDetail
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.PopupWindow import android.widget.PopupWindow
import android.widget.SearchView import android.widget.SearchView
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.drawable.toDrawable
import androidx.lifecycle.lifecycleScope
import eu.ottop.yamlauncher.databinding.ActivityAppMenuBinding import eu.ottop.yamlauncher.databinding.ActivityAppMenuBinding
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class AppMenuActivity : AppCompatActivity() { class AppMenuActivity : AppCompatActivity() {
private lateinit var binding: ActivityAppMenuBinding private lateinit var binding: ActivityAppMenuBinding
@ -96,7 +104,7 @@ class AppMenuActivity : AppCompatActivity() {
} }
private fun startTask() { private fun startTask() {
checkApps = GlobalScope.launch { checkApps = lifecycleScope.launch {
while (true) { while (true) {
if (!listsEqual(shownApps, getInstalledApps())) { if (!listsEqual(shownApps, getInstalledApps())) {
shownApps = getInstalledApps() shownApps = getInstalledApps()
@ -166,20 +174,39 @@ class AppMenuActivity : AppCompatActivity() {
val appInfo = appInfo.activityInfo.applicationInfo val appInfo = appInfo.activityInfo.applicationInfo
val textView = TextView(this) val textView = TextView(this)
val editLayout = LinearLayout(this)
val editText = EditText(this)
editText.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
val imageView = ImageView(this)
imageView.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
val launcherApps = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps val launcherApps = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
val mainActivity = val mainActivity = launcherApps.getActivityList(appInfo.packageName, userHandle).firstOrNull()
launcherApps.getActivityList(appInfo.packageName, userHandle).firstOrNull()
setupTextView(textView, appInfo, workProfile) editLayout.layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
editLayout.orientation = LinearLayout.HORIZONTAL;
setupTextListeners(textView, appInfo, userHandle, workProfile, launcherApps, mainActivity) setupTextView(textView, editText, appInfo, workProfile)
setupTextListeners(textView, editLayout, appInfo, userHandle, workProfile, launcherApps, mainActivity)
editLayout.addView(editText);
editLayout.addView(imageView);
editLayout.visibility = View.GONE
binding.container.addView(textView) binding.container.addView(textView)
binding.container.addView(editLayout)
return true return true
} }
private fun setupTextView(textView: TextView, appInfo: ApplicationInfo, workProfile: Int) { private fun setupTextView(textView: TextView, editText: EditText, appInfo: ApplicationInfo, workProfile: Int) {
val states = arrayOf( val states = arrayOf(
intArrayOf(-android.R.attr.state_pressed), intArrayOf(-android.R.attr.state_pressed),
intArrayOf(android.R.attr.state_pressed) intArrayOf(android.R.attr.state_pressed)
@ -204,10 +231,27 @@ class AppMenuActivity : AppCompatActivity() {
} }
setTextColor(ColorStateList(states, colors)) setTextColor(ColorStateList(states, colors))
} }
with(editText) {
id = R.id.app_name
textSize = 28f
setPadding(0, 10, 0, 80)
isClickable = true
focusable = View.FOCUSABLE
gravity = Gravity.CENTER_VERTICAL
isElegantTextHeight = false
isFocusable = true
isClickable = true
includeFontPadding = true
isSingleLine = true
setTextColor(ColorStateList(states, colors))
background = null
}
editText.setText(textView.text)
} }
private fun setupTextListeners( private fun setupTextListeners(
textView: TextView, textView: TextView,
editLayout: LinearLayout,
appInfo: ApplicationInfo, appInfo: ApplicationInfo,
userHandle: UserHandle, userHandle: UserHandle,
workProfile: Int, workProfile: Int,
@ -215,7 +259,7 @@ class AppMenuActivity : AppCompatActivity() {
mainActivity: LauncherActivityInfo? mainActivity: LauncherActivityInfo?
) { ) {
textView.setOnLongClickListener { textView.setOnLongClickListener {
appActionMenu(textView, appInfo, userHandle, workProfile, launcherApps, mainActivity) appActionMenu(textView, editLayout, appInfo, userHandle, workProfile, launcherApps, mainActivity)
} }
textView.setOnClickListener { textView.setOnClickListener {
@ -233,6 +277,7 @@ class AppMenuActivity : AppCompatActivity() {
private fun appActionMenu( private fun appActionMenu(
textView: TextView, textView: TextView,
editLayout: LinearLayout,
appInfo: ApplicationInfo, appInfo: ApplicationInfo,
userHandle: UserHandle, userHandle: UserHandle,
workProfile: Int, workProfile: Int,
@ -261,8 +306,9 @@ class AppMenuActivity : AppCompatActivity() {
popupWindow.showAsDropDown(textView, 0, -textView.height) popupWindow.showAsDropDown(textView, 0, -textView.height)
var editing = false
popupWindow.setOnDismissListener { popupWindow.setOnDismissListener {
textView.visibility = View.VISIBLE if (!editing) {textView.visibility = View.VISIBLE}
} }
popupView.findViewById<TextView>(R.id.info).setOnClickListener { popupView.findViewById<TextView>(R.id.info).setOnClickListener {
@ -288,8 +334,19 @@ class AppMenuActivity : AppCompatActivity() {
} }
popupView.findViewById<TextView>(R.id.rename).setOnClickListener { popupView.findViewById<TextView>(R.id.rename).setOnClickListener {
// Handle rename action textView.visibility = View.GONE
editLayout.visibility = View.VISIBLE
editing = true
popupWindow.dismiss() popupWindow.dismiss()
val editText = editLayout.findViewById<EditText>(R.id.app_name)
editText.requestFocus()
val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val handler = Handler(Looper.getMainLooper())
handler.postDelayed({
val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
}, 100) // Adjust delay as needed
} }
popupView.findViewById<TextView>(R.id.hide).setOnClickListener { popupView.findViewById<TextView>(R.id.hide).setOnClickListener {
@ -304,7 +361,7 @@ class AppMenuActivity : AppCompatActivity() {
private fun setAppHidden(packageName: String, profile: Int, hidden: Boolean) { private fun setAppHidden(packageName: String, profile: Int, hidden: Boolean) {
// Get the shared preferences editor // Get the shared preferences editor
val editor = getSharedPreferences("app_data", MODE_PRIVATE).edit() val editor = getSharedPreferences("hidden_apps", MODE_PRIVATE).edit()
val key = "$packageName-${profile}" val key = "$packageName-${profile}"
editor.putBoolean(key, hidden) editor.putBoolean(key, hidden)
editor.apply() editor.apply()
@ -312,14 +369,14 @@ class AppMenuActivity : AppCompatActivity() {
private fun isAppHidden(packageName: String, profile: Int): Boolean { private fun isAppHidden(packageName: String, profile: Int): Boolean {
// Get the shared preferences object // Get the shared preferences object
val sharedPref = getSharedPreferences("app_data", MODE_PRIVATE) val sharedPref = getSharedPreferences("hidden_apps", MODE_PRIVATE)
val key = "$packageName-${profile}" val key = "$packageName-${profile}"
return sharedPref.getBoolean(key, false) // Default to false (visible) return sharedPref.getBoolean(key, false) // Default to false (visible)
} }
private fun setAppVisible(packageName: String, profile: Int) { private fun setAppVisible(packageName: String, profile: Int) {
// Get the shared preferences editor // Get the shared preferences editor
val editor = getSharedPreferences("app_data", MODE_PRIVATE).edit() val editor = getSharedPreferences("hidden_apps", MODE_PRIVATE).edit()
val key = "$packageName-${profile}" val key = "$packageName-${profile}"
editor.remove(key) editor.remove(key)
editor.apply() editor.apply()

View file

@ -9,8 +9,8 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:foreground="@drawable/app_action_foreground"
android:drawableTop="@android:drawable/ic_menu_info_details" android:drawableTop="@android:drawable/ic_menu_info_details"
android:foreground="@drawable/app_action_foreground"
android:text="Info" android:text="Info"
android:textAlignment="center" android:textAlignment="center"
android:textColor="#F3F3F3FF" /> android:textColor="#F3F3F3FF" />
@ -20,8 +20,8 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:foreground="@drawable/app_action_foreground"
android:drawableTop="@android:drawable/ic_menu_delete" android:drawableTop="@android:drawable/ic_menu_delete"
android:foreground="@drawable/app_action_foreground"
android:text="Uninstall" android:text="Uninstall"
android:textAlignment="center" android:textAlignment="center"
android:textColor="#F3F3F3FF" /> android:textColor="#F3F3F3FF" />
@ -31,8 +31,8 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:foreground="@drawable/app_action_foreground"
android:drawableTop="@android:drawable/ic_menu_edit" android:drawableTop="@android:drawable/ic_menu_edit"
android:foreground="@drawable/app_action_foreground"
android:text="Rename" android:text="Rename"
android:textAlignment="center" android:textAlignment="center"
android:textColor="#F3F3F3FF" /> android:textColor="#F3F3F3FF" />
@ -42,8 +42,8 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:foreground="@drawable/app_action_foreground"
android:drawableTop="@android:drawable/ic_menu_view" android:drawableTop="@android:drawable/ic_menu_view"
android:foreground="@drawable/app_action_foreground"
android:text="Hide" android:text="Hide"
android:textAlignment="center" android:textAlignment="center"
android:textColor="#F3F3F3FF" /> android:textColor="#F3F3F3FF" />

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="app_name" type="id" />
</resources>