Skip to content

Commit 577079a

Browse files
Refactor and update code formatting
- Improved code formatting and consistency across various files. - Added `@RequiresApi` annotations where necessary. - Updated Android Gradle Plugin (AGP) version to 8.12.2. - Used `Context.withStyledAttributes` for cleaner attribute handling. - Replaced `Color.parseColor` with `String.toColorInt()`. - Replaced `View.isVisible` for checking view visibility. - Used `SharedPreferences.edit { ... }` for more concise SharedPreferences editing. - Changed `Activity.RESULT_OK` to `AppCompatActivity.RESULT_OK`.
1 parent f371ba6 commit 577079a

22 files changed

+355
-267
lines changed

app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import android.content.Intent
55
import android.content.pm.ApplicationInfo
66
import android.content.pm.LauncherActivityInfo
77
import android.content.pm.LauncherApps
8-
import android.net.Uri
98
import android.os.Handler
109
import android.os.Looper
1110
import android.os.UserHandle
@@ -18,6 +17,7 @@ import android.widget.TextView
1817
import android.widget.Toast
1918
import androidx.appcompat.widget.AppCompatButton
2019
import androidx.core.content.ContextCompat.getDrawable
20+
import androidx.core.net.toUri
2121
import androidx.core.view.ViewCompat
2222
import androidx.lifecycle.lifecycleScope
2323
import eu.ottop.yamlauncher.databinding.ActivityMainBinding
@@ -37,7 +37,7 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act
3737
appActivity: LauncherActivityInfo,
3838
userHandle: UserHandle,
3939
workProfile: Int
40-
){
40+
) {
4141
val pinButton = actionMenu.findViewById<TextView>(R.id.pin)
4242
val infoButton = actionMenu.findViewById<TextView>(R.id.info)
4343
val uninstallButton = actionMenu.findViewById<TextView>(R.id.uninstall)
@@ -69,7 +69,9 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act
6969
animations.fadeViewOut(actionMenu)
7070
textView.visibility = View.VISIBLE
7171
}
72-
} else {pinButton.visibility = View.GONE}
72+
} else {
73+
pinButton.visibility = View.GONE
74+
}
7375

7476
if (enableInfo) {
7577
infoButton.visibility = View.VISIBLE
@@ -87,7 +89,9 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act
8789
animations.fadeViewOut(actionMenu)
8890
textView.visibility = View.VISIBLE
8991
}
90-
} else {infoButton.visibility = View.GONE}
92+
} else {
93+
infoButton.visibility = View.GONE
94+
}
9195

9296
if (enableUninstall) {
9397
if (appActivity.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM == 0) {
@@ -107,7 +111,9 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act
107111
animations.fadeViewOut(actionMenu)
108112
textView.visibility = View.VISIBLE
109113
}
110-
} else {uninstallButton.visibility = View.GONE}
114+
} else {
115+
uninstallButton.visibility = View.GONE
116+
}
111117

112118
if (enableRename) {
113119
renameButton.visibility = View.VISIBLE
@@ -123,7 +129,9 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act
123129
renameButton.setOnClickListener {
124130
renameApp(textView, editLayout, actionMenu, appActivity, userHandle, workProfile)
125131
}
126-
} else {renameButton.visibility = View.GONE}
132+
} else {
133+
renameButton.visibility = View.GONE
134+
}
127135

128136
if (enableHide) {
129137
hideButton.visibility = View.VISIBLE
@@ -139,7 +147,9 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act
139147
hideButton.setOnClickListener {
140148
hideApp(editLayout, textView, actionMenu, appActivity, workProfile)
141149
}
142-
} else {hideButton.visibility = View.GONE}
150+
} else {
151+
hideButton.visibility = View.GONE
152+
}
143153

144154
if (enableClose) {
145155
closeButton.visibility = View.VISIBLE
@@ -148,14 +158,16 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act
148158
animations.fadeViewOut(actionMenu)
149159
textView.visibility = View.VISIBLE
150160
}
151-
} else {closeButton.visibility = View.GONE}
161+
} else {
162+
closeButton.visibility = View.GONE
163+
}
152164
}
153165

154166
private fun setPinState(button: TextView, appActivity: LauncherActivityInfo, workProfile: Int) {
155167
val isPinned = sharedPreferenceManager.isAppPinned(appActivity.componentName.flattenToString(), workProfile)
156168
val topDrawable = when (isPinned) {
157169
true -> getDrawable(activity, R.drawable.keep_off_24px)
158-
false -> getDrawable(activity,R.drawable.keep_24px)
170+
false -> getDrawable(activity, R.drawable.keep_24px)
159171
}
160172

161173
button.setCompoundDrawablesWithIntrinsicBounds(null, topDrawable, null, null)
@@ -189,7 +201,7 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act
189201

190202
private fun uninstallApp(appInfo: ApplicationInfo, userHandle: UserHandle) {
191203
val intent = Intent(Intent.ACTION_DELETE)
192-
intent.data = Uri.parse("package:${appInfo.packageName}")
204+
intent.data = "package:${appInfo.packageName}".toUri()
193205
intent.putExtra(Intent.EXTRA_USER, userHandle)
194206
activity.startActivity(intent)
195207
activity.returnAllowed = false

app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import android.content.pm.LauncherActivityInfo
66
import android.content.pm.LauncherApps
77
import android.graphics.BlendMode
88
import android.graphics.BlendModeColorFilter
9+
import android.os.Build
910
import android.os.UserHandle
1011
import android.view.LayoutInflater
1112
import android.view.View
1213
import android.view.ViewGroup
1314
import android.widget.FrameLayout
1415
import android.widget.LinearLayout
1516
import android.widget.TextView
17+
import androidx.annotation.RequiresApi
1618
import androidx.core.content.res.ResourcesCompat
1719
import androidx.core.view.ViewCompat
1820
import androidx.recyclerview.widget.RecyclerView
@@ -34,14 +36,14 @@ class AppMenuAdapter(
3436
) :
3537
RecyclerView.Adapter<AppMenuAdapter.AppViewHolder>() {
3638

37-
// If the menu is opened to select shortcuts, the below variable is set
38-
var shortcutIndex: Int = 0
39-
var shortcutTextView: TextView? = null
39+
// If the menu is opened to select shortcuts, the below variable is set
40+
var shortcutIndex: Int = 0
41+
var shortcutTextView: TextView? = null
4042

41-
private val sharedPreferenceManager = SharedPreferenceManager(activity)
42-
private val uiUtils = UIUtils(activity)
43-
private val appUtils = AppUtils(activity, launcherApps)
44-
private var appActionMenu = AppActionMenu(activity, binding, launcherApps, activity.findViewById(R.id.searchView))
43+
private val sharedPreferenceManager = SharedPreferenceManager(activity)
44+
private val uiUtils = UIUtils(activity)
45+
private val appUtils = AppUtils(activity, launcherApps)
46+
private var appActionMenu = AppActionMenu(activity, binding, launcherApps, activity.findViewById(R.id.searchView))
4547

4648
interface OnItemClickListener {
4749
fun onItemClick(appInfo: LauncherActivityInfo, userHandle: UserHandle)
@@ -67,16 +69,15 @@ class AppMenuAdapter(
6769

6870
init {
6971
textView.setOnClickListener {
70-
val position = bindingAdapterPosition
71-
val app = apps[position].first
72-
73-
// If opened to select a shortcut, set the shortcut instead of launching the app
74-
if (shortcutTextView != null) {
75-
shortcutListener.onShortcut(app, apps[position].second, textView, apps[position].third, shortcutTextView!!, shortcutIndex)
76-
}
77-
else {
78-
itemClickListener.onItemClick(app, apps[position].second)
79-
}
72+
val position = bindingAdapterPosition
73+
val app = apps[position].first
74+
75+
// If opened to select a shortcut, set the shortcut instead of launching the app
76+
if (shortcutTextView != null) {
77+
shortcutListener.onShortcut(app, apps[position].second, textView, apps[position].third, shortcutTextView!!, shortcutIndex)
78+
} else {
79+
itemClickListener.onItemClick(app, apps[position].second)
80+
}
8081
}
8182

8283
textView.setOnLongClickListener {
@@ -90,12 +91,13 @@ class AppMenuAdapter(
9091
return@setOnLongClickListener true
9192
} else {
9293

93-
itemLongClickListener.onItemLongClick(
94-
textView,
95-
actionMenuLayout
96-
)
97-
return@setOnLongClickListener true
98-
}}
94+
itemLongClickListener.onItemLongClick(
95+
textView,
96+
actionMenuLayout
97+
)
98+
return@setOnLongClickListener true
99+
}
100+
}
99101

100102
}
101103
}
@@ -105,29 +107,28 @@ class AppMenuAdapter(
105107
.inflate(R.layout.app_item_layout, parent, false)
106108
return AppViewHolder(view)
107109
}
108-
110+
111+
@RequiresApi(Build.VERSION_CODES.Q)
109112
override fun onBindViewHolder(holder: AppViewHolder, position: Int) {
110113
holder.actionMenuLayout.visibility = View.INVISIBLE
111114
holder.editView.visibility = View.INVISIBLE
112115
val app = apps[position]
113116

114117
if (sharedPreferenceManager.isAppPinned(app.first.componentName.flattenToString(), app.third)) {
115118
if (app.third != 0) {
116-
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.keep_filled_15px, null),null, ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null),null)
117-
}
118-
else {
119-
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.keep_15px, null),null,ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null),null)
119+
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.keep_filled_15px, null), null, ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null), null)
120+
} else {
121+
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.keep_15px, null), null, ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null), null)
120122
}
121123
holder.textView.compoundDrawables[0].colorFilter = BlendModeColorFilter(sharedPreferenceManager.getTextColor(), BlendMode.SRC_ATOP)
122124
}
123125
// Set initial drawables
124126
else if (app.third != 0) {
125-
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_work_app, null),null, ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null),null)
127+
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_work_app, null), null, ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null), null)
126128
holder.textView.compoundDrawables[0].colorFilter =
127129
BlendModeColorFilter(sharedPreferenceManager.getTextColor(), BlendMode.SRC_ATOP)
128-
}
129-
else {
130-
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null),null,ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null),null)
130+
} else {
131+
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null), null, ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null), null)
131132
}
132133

133134
uiUtils.setAppAlignment(holder.textView, holder.editText)
@@ -163,8 +164,7 @@ class AppMenuAdapter(
163164
holder.actionMenuLayout.findViewById<TextView>(R.id.uninstall).visibility =
164165
View.VISIBLE
165166
}
166-
}
167-
else {
167+
} else {
168168
holder.textView.text = activity.getString(R.string.removing)
169169
}
170170

app/src/main/java/eu/ottop/yamlauncher/ContactsAdapter.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ class ContactsAdapter(
2020
) :
2121
RecyclerView.Adapter<ContactsAdapter.AppViewHolder>() {
2222

23-
var shortcutIndex: Int = 0
24-
var shortcutTextView: TextView? = null
23+
var shortcutIndex: Int = 0
24+
var shortcutTextView: TextView? = null
2525

26-
private val uiUtils = UIUtils(activity)
27-
private val sharedPreferenceManager = SharedPreferenceManager(activity)
26+
private val uiUtils = UIUtils(activity)
27+
private val sharedPreferenceManager = SharedPreferenceManager(activity)
2828

2929
interface OnContactClickListener {
3030
fun onContactClick(contactId: Int)
@@ -62,7 +62,8 @@ class ContactsAdapter(
6262
override fun onBindViewHolder(holder: AppViewHolder, position: Int) {
6363
val contact = contacts[position]
6464
holder.textView.setCompoundDrawablesWithIntrinsicBounds(
65-
ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null),null, ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null),null)
65+
ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null), null, ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null), null
66+
)
6667

6768
uiUtils.setAppAlignment(holder.textView)
6869

app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
8181
private lateinit var weatherSystem: WeatherSystem
8282
private lateinit var appUtils: AppUtils
8383
private lateinit var biometricUtils: BiometricUtils
84-
84+
8585
private val stringUtils = StringUtils()
8686
private val permissionUtils = PermissionUtils()
8787
private lateinit var uiUtils: UIUtils
@@ -139,6 +139,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
139139

140140
private val handler = Handler(Looper.getMainLooper())
141141

142+
@RequiresApi(Build.VERSION_CODES.R)
142143
override fun onCreate(savedInstanceState: Bundle?) {
143144
super.onCreate(savedInstanceState)
144145
binding = ActivityMainBinding.inflate(layoutInflater)
@@ -441,6 +442,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
441442
}
442443
}
443444

445+
@RequiresApi(Build.VERSION_CODES.R)
444446
private fun setPreferences() {
445447
uiUtils.setBackground(window)
446448

@@ -623,6 +625,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
623625
}
624626

625627
// Only reload items that have had preferences changed
628+
@RequiresApi(Build.VERSION_CODES.R)
626629
@SuppressLint("UseKtx")
627630
override fun onSharedPreferenceChanged(preferences: SharedPreferences?, key: String?) {
628631
if (preferences != null) {
@@ -869,6 +872,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
869872
}
870873
}
871874

875+
@RequiresApi(Build.VERSION_CODES.R)
872876
private suspend fun updateWeather() {
873877
withContext(Dispatchers.IO) {
874878
if (sharedPreferenceManager.isWeatherEnabled()) {
@@ -1156,7 +1160,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
11561160
appMenuLinearLayoutManager.setScrollEnabled(true)
11571161
appRecycler.layoutManager = appMenuLinearLayoutManager
11581162
}
1159-
1163+
11601164
// On home key or swipe, return to home screen
11611165
override fun onNewIntent(intent: Intent) {
11621166
super.onNewIntent(intent)
@@ -1196,6 +1200,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
11961200
appUtils.launchApp(appInfo.componentName, userHandle)
11971201
}
11981202

1203+
@RequiresApi(Build.VERSION_CODES.Q)
11991204
override fun onShortcut(
12001205
appInfo: LauncherActivityInfo,
12011206
userHandle: UserHandle,

app/src/main/java/eu/ottop/yamlauncher/settings/AppMenuSettingsFragment.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import eu.ottop.yamlauncher.R
99
import eu.ottop.yamlauncher.utils.PermissionUtils
1010
import eu.ottop.yamlauncher.utils.UIUtils
1111

12-
class AppMenuSettingsFragment : PreferenceFragmentCompat(), TitleProvider { private val permissionUtils = PermissionUtils()
12+
class AppMenuSettingsFragment : PreferenceFragmentCompat(), TitleProvider {
13+
private val permissionUtils = PermissionUtils()
1314
private var contactPref: SwitchPreference? = null
1415
private var webSearchPref: SwitchPreference? = null
1516
private var autoLaunchPref: SwitchPreference? = null
@@ -27,11 +28,11 @@ class AppMenuSettingsFragment : PreferenceFragmentCompat(), TitleProvider { priv
2728
contactPref?.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
2829

2930
if (newValue as Boolean && !permissionUtils.hasPermission(requireContext(), Manifest.permission.READ_CONTACTS)) {
30-
(requireActivity() as SettingsActivity).requestContactsPermission()
31-
return@OnPreferenceChangeListener false
32-
} else {
33-
return@OnPreferenceChangeListener true
34-
}
31+
(requireActivity() as SettingsActivity).requestContactsPermission()
32+
return@OnPreferenceChangeListener false
33+
} else {
34+
return@OnPreferenceChangeListener true
35+
}
3536
}
3637

3738
if (webSearchPref != null && autoLaunchPref != null) {
@@ -50,7 +51,8 @@ class AppMenuSettingsFragment : PreferenceFragmentCompat(), TitleProvider { priv
5051
contextMenuSettings?.onPreferenceClickListener =
5152
Preference.OnPreferenceClickListener {
5253
uiUtils.switchFragment(requireActivity(), ContextMenuSettingsFragment())
53-
true }
54+
true
55+
}
5456
}
5557

5658
override fun getTitle(): String {

app/src/main/java/eu/ottop/yamlauncher/settings/GestureAppsAdapter.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,19 @@ class GestureAppsAdapter(
5252
val app = apps[position]
5353

5454
if (app.third != 0) {
55-
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(context.resources,
56-
R.drawable.ic_work_app, null),null,null,null)
57-
}
58-
else {
59-
holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(context.resources,
60-
R.drawable.ic_empty, null),null,null,null)
55+
holder.textView.setCompoundDrawablesWithIntrinsicBounds(
56+
ResourcesCompat.getDrawable(
57+
context.resources,
58+
R.drawable.ic_work_app, null
59+
), null, null, null
60+
)
61+
} else {
62+
holder.textView.setCompoundDrawablesWithIntrinsicBounds(
63+
ResourcesCompat.getDrawable(
64+
context.resources,
65+
R.drawable.ic_empty, null
66+
), null, null, null
67+
)
6168
}
6269

6370
uiUtils.setAppAlignment(holder.textView)

0 commit comments

Comments
 (0)