diff --git a/app/src/main/java/eu/ottop/yamlauncher/AboutFragment.kt b/app/src/main/java/eu/ottop/yamlauncher/AboutFragment.kt index fd04f3c..57e5ce7 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AboutFragment.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AboutFragment.kt @@ -14,12 +14,13 @@ class AboutFragment : Fragment() { inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { - // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_about, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + + // Set up about page links stringUtils.setLink(requireActivity().findViewById(R.id.creditText), getString(R.string.my_website_link)) stringUtils.setLink(requireActivity().findViewById(R.id.codebergLink), getString(R.string.codeberg_link)) stringUtils.setLink(requireActivity().findViewById(R.id.githubLink), getString(R.string.github_link)) diff --git a/app/src/main/java/eu/ottop/yamlauncher/Animations.kt b/app/src/main/java/eu/ottop/yamlauncher/Animations.kt index e080531..2c4a42c 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/Animations.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/Animations.kt @@ -15,6 +15,8 @@ class Animations (context: Context) { private val sharedPreferenceManager = SharedPreferenceManager(context) + // fadeViewIn and fadeViewOut are for smaller item transitions, such as the action menu + fun fadeViewIn(view: View) { view.fadeIn() } @@ -22,6 +24,7 @@ class Animations (context: Context) { fun fadeViewOut(view: View) { view.fadeOut() } + fun showHome(homeView: View, appView: View) { appView.slideOutToBottom() homeView.fadeIn() @@ -35,45 +38,47 @@ class Animations (context: Context) { fun backgroundIn(activity: Activity) { val originalColor = sharedPreferenceManager.getBgColor() - val newColor: Int = if (originalColor == Color.parseColor("#00000000")) { - Color.parseColor("#3F000000") + // Only animate darkness onto the transparent background + if (originalColor == Color.parseColor("#00000000")) { + val newColor = Color.parseColor("#3F000000") + val colorDrawable = ColorDrawable(originalColor) + activity.window.setBackgroundDrawable(colorDrawable) + + val backgroundColorAnimator = ValueAnimator.ofObject(ArgbEvaluator(), originalColor, newColor) + backgroundColorAnimator.addUpdateListener { animator -> + colorDrawable.color = animator.animatedValue as Int + } + + val duration = sharedPreferenceManager.getAnimationSpeed() + backgroundColorAnimator.duration = duration + + backgroundColorAnimator.start() } else { - originalColor + return } - - val colorDrawable = ColorDrawable(originalColor) - activity.window.setBackgroundDrawable(colorDrawable) - - val backgroundColorAnimator = ValueAnimator.ofObject(ArgbEvaluator(), originalColor, newColor) - backgroundColorAnimator.addUpdateListener { animator -> - colorDrawable.color = animator.animatedValue as Int - } - val duration = sharedPreferenceManager.getAnimationSpeed() - backgroundColorAnimator.duration = duration - - backgroundColorAnimator.start() } fun backgroundOut(activity: Activity) { val newColor = sharedPreferenceManager.getBgColor() - val originalColor: Int = if (newColor == Color.parseColor("#00000000")) { - Color.parseColor("#3F000000") + // Only animate darkness onto the transparent background + if (newColor == Color.parseColor("#00000000")) { + val originalColor = Color.parseColor("#3F000000") + val colorDrawable = ColorDrawable(originalColor) + activity.window.setBackgroundDrawable(colorDrawable) + + val backgroundColorAnimator = ValueAnimator.ofObject(ArgbEvaluator(), originalColor, newColor) + backgroundColorAnimator.addUpdateListener { animator -> + colorDrawable.color = animator.animatedValue as Int + } + + val duration = sharedPreferenceManager.getAnimationSpeed() + backgroundColorAnimator.duration = duration + + backgroundColorAnimator.start() } else { - newColor + return } - - val colorDrawable = ColorDrawable(originalColor) - activity.window.setBackgroundDrawable(colorDrawable) - - val backgroundColorAnimator = ValueAnimator.ofObject(ArgbEvaluator(), originalColor, newColor) - backgroundColorAnimator.addUpdateListener { animator -> - colorDrawable.color = animator.animatedValue as Int - } - val duration = sharedPreferenceManager.getAnimationSpeed() - backgroundColorAnimator.duration = duration - - backgroundColorAnimator.start() } private fun View.slideInFromBottom() { if (visibility != View.VISIBLE) { diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt b/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt index 8c5a8a2..0473e9c 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt @@ -39,6 +39,8 @@ class AppActionMenu { val sharedPreferenceManager = SharedPreferenceManager(activity) actionMenu.findViewById(R.id.info).setOnClickListener { + + // Launch app info in phone settings if (mainActivity != null) { launcherApps.startAppDetailsActivity( mainActivity.componentName, @@ -75,6 +77,7 @@ class AppActionMenu { searchView.visibility = View.INVISIBLE editText.requestFocus() + // Open keyboard val handler = Handler(Looper.getMainLooper()) handler.postDelayed({ val imm = @@ -83,6 +86,8 @@ class AppActionMenu { }, 100) binding.root.addOnLayoutChangeListener { _, _, top, _, bottom, _, oldTop, _, oldBottom -> + + // If the keyboard is closed, exit editing mode if (bottom - top > oldBottom - oldTop) { editLayout.clearFocus() @@ -93,6 +98,8 @@ class AppActionMenu { } editText.setOnEditorActionListener { _, actionId, _ -> + + // Once the new name is confirmed, close the keyboard, save the new app name and update the apps on screen if (actionId == EditorInfo.IME_ACTION_DONE) { val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager @@ -113,6 +120,8 @@ class AppActionMenu { } resetButton.setOnClickListener { + + // If reset is pressed, close keyboard, remove saved edited name and update the apps on screen val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(editLayout.windowToken, 0) diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt index 792205a..0cf3e87 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt @@ -30,6 +30,7 @@ class AppMenuAdapter( ) : RecyclerView.Adapter() { + // If the menu is opened to select shortcuts, the below variable is set var shortcutTextView: TextView? = null private val sharedPreferenceManager = SharedPreferenceManager(context) @@ -52,8 +53,7 @@ class AppMenuAdapter( textView: TextView, actionMenuLayout: LinearLayout, editView: LinearLayout, - position: Int, - shortcutTextView: TextView? + position: Int ) } @@ -71,6 +71,8 @@ class AppMenuAdapter( textView.setOnClickListener { val position = bindingAdapterPosition val app = apps[position].first + + // If opened to select a shortcut, set the shortcut instead of launching the app if (shortcutTextView != null) { shortcutListener.onShortcut(app, apps[position].second.first, textView, apps[position].second.second, shortcutTextView!!) } @@ -79,11 +81,17 @@ class AppMenuAdapter( } } - textView.setOnLongClickListener { val position = bindingAdapterPosition val app = apps[position].first + + // If opened to select a shortcut, set the shortcut instead of opening the action menu + if (shortcutTextView != null) { + shortcutListener.onShortcut(app, apps[position].second.first, textView, apps[position].second.second, shortcutTextView!!) + return@setOnLongClickListener true + } else { + itemLongClickListener.onItemLongClick( app, apps[position].second.first, @@ -91,11 +99,10 @@ class AppMenuAdapter( textView, actionMenuLayout, editView, - position, - shortcutTextView + position ) return@setOnLongClickListener true - } + }} } } @@ -109,6 +116,7 @@ class AppMenuAdapter( override fun onBindViewHolder(holder: AppViewHolder, position: Int) { val app = apps[position] + // Set initial drawables if (app.second.second != 0) { holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_work_app, null),null, ResourcesCompat.getDrawable(context.resources, R.drawable.ic_empty, null),null) holder.textView.compoundDrawables[0].colorFilter = @@ -122,12 +130,15 @@ class AppMenuAdapter( uiUtils.setAppSize(holder.textView, holder.editText) + // Update the application information (allows updating apps to work) val appInfo = appUtils.getAppInfo( app.first.applicationInfo.packageName, app.second.second ) holder.textView.setTextColor(sharedPreferenceManager.getTextColor()) + + // Set app name on the menu. If the app has been uninstalled, replace it with "Removing" until the app menu updates. val appLabel: CharSequence = appInfo?.loadLabel(context.packageManager) ?: "Removing..." if (appInfo != null) { @@ -139,6 +150,7 @@ class AppMenuAdapter( holder.editText.setText(holder.textView.text) + // Remove the uninstall icon for system apps if (appInfo.flags and ApplicationInfo.FLAG_SYSTEM != 0) { holder.actionMenuLayout.findViewById(R.id.uninstall).visibility = View.GONE diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppMenuEdgeFactory.kt b/app/src/main/java/eu/ottop/yamlauncher/AppMenuEdgeFactory.kt index 6b6ff51..055c6a8 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppMenuEdgeFactory.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppMenuEdgeFactory.kt @@ -11,7 +11,10 @@ class AppMenuEdgeFactory(private val activity: Activity) : RecyclerView.EdgeEffe } inner class AppMenuEdgeEffect(activity: Activity) : EdgeEffect(activity) { + + //This just speeds up the animation when the scrolling hits the edge so that the app menu can be exited sooner private val animationSpeedFactor = 0.75f + override fun onAbsorb(velocity: Int) { super.onAbsorb((velocity * animationSpeedFactor).toInt()) } diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppMenuLinearLayoutManager.kt b/app/src/main/java/eu/ottop/yamlauncher/AppMenuLinearLayoutManager.kt index 61e3d08..f9ad3ac 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppMenuLinearLayoutManager.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppMenuLinearLayoutManager.kt @@ -17,7 +17,8 @@ class AppMenuLinearLayoutManager(private val activity: MainActivity) : LinearLay val scrollRange = super.scrollVerticallyBy(dy, recycler, state) val overscroll: Int = dy - scrollRange - if (overscroll < 0 && firstVisibleItemPosition == 0 && scrollStarted && activity.isJobActive) { + // If the user scrolls up when already on top, go back to home. Only if the keyboard isn't open, though + if (overscroll < 0 && (firstVisibleItemPosition == 0 || firstVisibleItemPosition < 0) && scrollStarted && activity.canExit) { activity.backToHome() } diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppUtils.kt b/app/src/main/java/eu/ottop/yamlauncher/AppUtils.kt index ce7ff22..e43a397 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppUtils.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppUtils.kt @@ -17,18 +17,19 @@ class AppUtils(private val context: Context, private val launcherApps: LauncherA val allApps = mutableListOf>>() var sortedApps = listOf>>() withContext(Dispatchers.Default) { - for (i in launcherApps.profiles.indices) { + for (i in launcherApps.profiles.indices) { // Check apps on both, normal and work profiles launcherApps.getActivityList(null, launcherApps.profiles[i]).forEach { app -> - if (!sharedPreferenceManager.isAppHidden( + if (!sharedPreferenceManager.isAppHidden( // Only include the app if it isn't set as hidden app.applicationInfo.packageName, i - ) && app.applicationInfo.packageName != context.applicationInfo.packageName + ) && app.applicationInfo.packageName != context.applicationInfo.packageName // Hide the launcher itself ) { allApps.add(Pair(app, Pair(launcherApps.profiles[i], i))) } } } + // Sort apps by name sortedApps = allApps.sortedBy { sharedPreferenceManager.getAppName( it.first.applicationInfo.packageName, @@ -41,8 +42,11 @@ class AppUtils(private val context: Context, private val launcherApps: LauncherA } - fun getHiddenApps(): List>> { + // Get hidden apps for the hidden apps settings + suspend fun getHiddenApps(): List>> { val allApps = mutableListOf>>() + var sortedApps = listOf>>() + withContext(Dispatchers.Default) { for (i in launcherApps.profiles.indices) { launcherApps.getActivityList(null, launcherApps.profiles[i]).forEach { app -> if (sharedPreferenceManager.isAppHidden(app.applicationInfo.packageName, i)) { @@ -50,13 +54,16 @@ class AppUtils(private val context: Context, private val launcherApps: LauncherA } } } - return allApps.sortedBy { + + sortedApps = allApps.sortedBy { sharedPreferenceManager.getAppName( it.first.applicationInfo.packageName, it.second.second, context.packageManager.getApplicationLabel(it.first.applicationInfo) ).toString().lowercase() } + } + return sortedApps } fun getAppInfo( diff --git a/app/src/main/java/eu/ottop/yamlauncher/BatteryReceiver.kt b/app/src/main/java/eu/ottop/yamlauncher/BatteryReceiver.kt index da8c9bc..281e92f 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/BatteryReceiver.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/BatteryReceiver.kt @@ -13,7 +13,7 @@ class BatteryReceiver(private val activity: MainActivity) : BroadcastReceiver() val level = it.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) val scale = it.getIntExtra(BatteryManager.EXTRA_SCALE, -1) val batteryPct = level * 100 / scale.toFloat() - activity.modifyDate("${batteryPct.toInt()}%", 3) + activity.modifyDate("${batteryPct.toInt()}%", 3) // Add battery to the date } } diff --git a/app/src/main/java/eu/ottop/yamlauncher/GestureAppsAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/GestureAppsAdapter.kt index 6c8a528..eb0dfef 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/GestureAppsAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/GestureAppsAdapter.kt @@ -60,7 +60,9 @@ class GestureAppsAdapter( uiUtils.setAppSize(holder.textView) + // Does not need to be specially updated since it's in a separate activity and thus reloads when opened again val appInfo = app.first.activityInfo.applicationInfo + holder.textView.text = sharedPreferenceManager.getAppName( app.first.applicationInfo.packageName, app.second.second, diff --git a/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt b/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt index b6e30f1..b447dfc 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/GestureAppsFragment.kt @@ -32,7 +32,6 @@ class GestureAppsFragment : Fragment(), GestureAppsAdapter.OnItemClickListener { inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { - // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_gesture_apps, container, false) } @@ -46,16 +45,12 @@ class GestureAppsFragment : Fragment(), GestureAppsAdapter.OnItemClickListener { lifecycleScope.launch { - withContext(Dispatchers.Default) { + adapter = GestureAppsAdapter( + requireContext(), + appUtils.getInstalledApps().toMutableList(), + this@GestureAppsFragment + ) - - - adapter = GestureAppsAdapter( - requireContext(), - appUtils.getInstalledApps().toMutableList(), - this@GestureAppsFragment - ) - } val recyclerView = view.findViewById(R.id.gestureAppRecycler) val appMenuEdgeFactory = AppMenuEdgeFactory(requireActivity()) val uiUtils = UIUtils(requireContext()) diff --git a/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsFragment.kt b/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsFragment.kt index 96d2226..6d0bc58 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsFragment.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/HiddenAppsFragment.kt @@ -13,8 +13,10 @@ import android.view.View import android.view.ViewGroup import android.view.inputmethod.InputMethodManager import androidx.fragment.app.Fragment +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.RecyclerView import com.google.android.material.textfield.TextInputEditText +import kotlinx.coroutines.launch class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener { @@ -40,10 +42,12 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener { appUtils = AppUtils(requireContext(), launcherApps) sharedPreferenceManager = SharedPreferenceManager(requireContext()) + lifecycleScope.launch { + val recyclerView = view.findViewById(R.id.hiddenAppRecycler) val appMenuEdgeFactory = AppMenuEdgeFactory(requireActivity()) - adapter = HiddenAppsAdapter(requireContext(), appUtils.getHiddenApps().toMutableList(), this) + adapter = HiddenAppsAdapter(requireContext(), appUtils.getHiddenApps().toMutableList(), this@HiddenAppsFragment) recyclerView.edgeEffectFactory = appMenuEdgeFactory @@ -74,8 +78,9 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener { } override fun afterTextChanged(s: Editable?) { - - filterItems(searchView.text.toString()) + lifecycleScope.launch { + filterItems(searchView.text.toString()) + } } }) @@ -87,8 +92,9 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener { imm.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT) } } + } - private fun filterItems(query: String?) { + private suspend fun filterItems(query: String?) { val cleanQuery = stringUtils.cleanString(query) val newFilteredApps = mutableListOf>>() @@ -128,7 +134,9 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener { setTitle("Confirmation") setMessage("Are you sure you want to unhide $appName?") setPositiveButton("Yes") { _, _ -> - performConfirmedAction(appInfo, profile) + lifecycleScope.launch { + performConfirmedAction(appInfo, profile) + } } setNegativeButton("Cancel") { _, _ -> @@ -136,7 +144,7 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener { }.create().show() } - private fun performConfirmedAction(appInfo: LauncherActivityInfo, profile: Int) { + private suspend fun performConfirmedAction(appInfo: LauncherActivityInfo, profile: Int) { sharedPreferenceManager.setAppVisible(appInfo.applicationInfo.packageName, profile) adapter?.updateApps(appUtils.getHiddenApps()) } diff --git a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt index c31ab83..b3c01f1 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt @@ -74,7 +74,8 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh private lateinit var preferences: SharedPreferences private var isBatteryReceiverRegistered = false - var isJobActive = true + private var isJobActive = true + var canExit = true private val swipeThreshold = 100 private val swipeVelocityThreshold = 100 @@ -502,8 +503,12 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh recyclerView.addOnLayoutChangeListener { _, _, top, _, bottom, _, oldTop, _, oldBottom -> if (bottom - top > oldBottom - oldTop) { + canExit = true searchView.clearFocus() } + else if (bottom - top < oldBottom - oldTop) { + canExit = false + } } searchView.addTextChangedListener(object : @@ -625,7 +630,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh appInfo.applicationInfo.packageName, userProfile ) - uiUtils.setShortcutDrawables(shortcutView, sharedPreferenceManager.getShortcutAlignment()) + uiUtils.setDrawables(shortcutView, sharedPreferenceManager.getShortcutAlignment()) backToHome() } @@ -637,13 +642,8 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh textView: TextView, actionMenuLayout: LinearLayout, editView: LinearLayout, - position: Int, - shortcutTextView: TextView? + position: Int ) { - if (shortcutTextView != null) { - onShortcut(appInfo, userHandle, textView, userProfile, shortcutTextView) - return - } textView.visibility = View.INVISIBLE animations.fadeViewIn(actionMenuLayout) val mainActivity = diff --git a/app/src/main/java/eu/ottop/yamlauncher/UIUtils.kt b/app/src/main/java/eu/ottop/yamlauncher/UIUtils.kt index b9b21f1..094f0a7 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/UIUtils.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/UIUtils.kt @@ -4,7 +4,6 @@ import android.content.Context import android.graphics.BlendMode import android.graphics.BlendModeColorFilter import android.graphics.Color -import android.graphics.drawable.ColorDrawable import android.view.Gravity import android.view.View import android.view.ViewGroup @@ -15,17 +14,14 @@ import android.view.WindowInsetsController import android.widget.LinearLayout import android.widget.TextClock import android.widget.TextView -import androidx.core.content.res.ResourcesCompat import androidx.core.view.children import com.google.android.material.textfield.TextInputEditText -class UIUtils(private val context: Context) { +class UIUtils(context: Context) { private val sharedPreferenceManager = SharedPreferenceManager(context) fun setBackground(window: Window) { - window.decorView.background = ColorDrawable(Color.parseColor("#00000000")) - window.decorView.setBackgroundColor( sharedPreferenceManager.getBgColor() ) @@ -97,90 +93,63 @@ class UIUtils(private val context: Context) { fun setShortcutsAlignment(shortcuts: LinearLayout) { val alignment = sharedPreferenceManager.getShortcutAlignment() shortcuts.children.forEach { - if (it is TextView) { - - try { - setShortcutAlignment(it, alignment) - setShortcutDrawables(it, alignment) - } catch(_: Exception) {} + setTextGravity(it, alignment) + setDrawables(it, alignment) } } } - fun setShortcutAlignment(shortcut: TextView, alignment: String?) { - when (alignment) { - "left" -> { - shortcut.gravity = Gravity.CENTER_VERTICAL or Gravity.START - } - "center" -> { - shortcut.gravity = Gravity.CENTER - } + fun setDrawables(shortcut: TextView, alignment: String?) { + try { + when (alignment) { + "left" -> { + shortcut.setCompoundDrawablesWithIntrinsicBounds( + shortcut.compoundDrawables.filterNotNull().first(), + null, + null, + null + ) + } - "right" -> { - shortcut.gravity = Gravity.CENTER_VERTICAL or Gravity.END - } - } - } + "center" -> { + shortcut.setCompoundDrawablesWithIntrinsicBounds( + shortcut.compoundDrawables.filterNotNull().first(), + null, + shortcut.compoundDrawables.filterNotNull().first(), + null + ) + } - fun setShortcutDrawables(shortcut: TextView, alignment: String?) { - when (alignment) { - "left" -> { - shortcut.setCompoundDrawablesWithIntrinsicBounds( - shortcut.compoundDrawables.filterNotNull().first(), null, null, null - ) + "right" -> { + shortcut.setCompoundDrawablesWithIntrinsicBounds( + null, + null, + shortcut.compoundDrawables.filterNotNull().first(), + null + ) + } } - - "center" -> { - shortcut.setCompoundDrawablesWithIntrinsicBounds( - shortcut.compoundDrawables.filterNotNull().first(), - null, - shortcut.compoundDrawables.filterNotNull().first(), - null - ) - } - - "right" -> { - shortcut.setCompoundDrawablesWithIntrinsicBounds( - null, - null, - shortcut.compoundDrawables.filterNotNull().first(), - null - ) - } - } + } catch (_: Exception) {} } fun setAppAlignment( textView: TextView, - editText: TextInputEditText? = null, + editText: TextView? = null, regionText: TextView? = null ) { val alignment = sharedPreferenceManager.getAppAlignment() setTextGravity(textView, alignment) if (regionText != null) { - setTextGravity(textView, alignment) setTextGravity(regionText, alignment) return } - when (alignment) { - "left" -> { - textView.setCompoundDrawablesWithIntrinsicBounds(textView.compoundDrawables.filterNotNull().first(),null, ResourcesCompat.getDrawable(context.resources, R.drawable.ic_empty, null), null) - editText?.gravity = Gravity.CENTER_VERTICAL or Gravity.START - - } - "center" -> { - textView.setCompoundDrawablesWithIntrinsicBounds(textView.compoundDrawables.filterNotNull().first(),null, textView.compoundDrawables.filterNotNull().first(), null) - editText?.gravity = Gravity.CENTER_VERTICAL or Gravity.END - - } - "right" -> { - textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_empty, null),null, textView.compoundDrawables.filterNotNull().first(), null) - editText?.gravity = Gravity.CENTER_VERTICAL or Gravity.END - } + if (editText != null) { + setDrawables(textView, alignment) + setTextGravity(editText, alignment) } } @@ -195,7 +164,8 @@ class UIUtils(private val context: Context) { } private fun setTextAlignment(view: TextView, alignment: String?) { - view.textAlignment = when (alignment) { + try { + view.textAlignment = when (alignment) { "left" -> View.TEXT_ALIGNMENT_VIEW_START "center" -> View.TEXT_ALIGNMENT_CENTER @@ -203,19 +173,22 @@ class UIUtils(private val context: Context) { "right" -> View.TEXT_ALIGNMENT_VIEW_END else -> View.TEXT_ALIGNMENT_VIEW_START - } + } + } catch (_: Exception) {} } private fun setTextGravity(view: TextView, alignment: String?) { - view.gravity = when (alignment) { - "left" -> Gravity.CENTER_VERTICAL or Gravity.START + try { + view.gravity = when (alignment) { + "left" -> Gravity.CENTER_VERTICAL or Gravity.START - "center" -> Gravity.CENTER + "center" -> Gravity.CENTER - "right" -> Gravity.CENTER_VERTICAL or Gravity.END + "right" -> Gravity.CENTER_VERTICAL or Gravity.END - else -> Gravity.CENTER_VERTICAL or Gravity.START - } + else -> Gravity.CENTER_VERTICAL or Gravity.START + } + } catch (_: Exception) {} } fun setClockSize(clock: TextClock) { @@ -227,6 +200,7 @@ class UIUtils(private val context: Context) { } fun setShortcutsSize(shortcuts: LinearLayout) { + val alignment = sharedPreferenceManager.getShortcutSize() val viewTreeObserver = shortcuts.viewTreeObserver val globalLayoutListener = object : ViewTreeObserver.OnGlobalLayoutListener { @@ -234,30 +208,8 @@ class UIUtils(private val context: Context) { shortcuts.children.forEach { if (it is TextView) { + setShortcutSize(it, alignment) - when (sharedPreferenceManager.getShortcutSize()) { - "small" -> { - it.setPadding( - it.paddingLeft, - it.height / 4, - it.paddingRight, - it.height / 4 - ) - } - - "medium" -> { - it.setPadding( - it.paddingLeft, - (it.height / 4.5).toInt(), - it.paddingRight, - (it.height / 4.5).toInt() - ) - } - - "large" -> { - it.setPadding(it.paddingLeft, 0, it.paddingRight, 0) - } - } } } if (viewTreeObserver.isAlive) { @@ -271,6 +223,34 @@ class UIUtils(private val context: Context) { } } + private fun setShortcutSize(shortcut: TextView, alignment: String?) { + try { + when (alignment) { + "small" -> { + shortcut.setPadding( + shortcut.paddingLeft, + shortcut.height / 4, + shortcut.paddingRight, + shortcut.height / 4 + ) + } + + "medium" -> { + shortcut.setPadding( + shortcut.paddingLeft, + (shortcut.height / 4.5).toInt(), + shortcut.paddingRight, + (shortcut.height / 4.5).toInt() + ) + } + + "large" -> { + shortcut.setPadding(shortcut.paddingLeft, 0, shortcut.paddingRight, 0) + } + } + } catch(_: Exception) {} + } + fun setAppSize( textView: TextView, editText: TextInputEditText? = null, @@ -291,15 +271,19 @@ class UIUtils(private val context: Context) { } private fun setTextSize(view: TextView, size: String?, s: Float, m: Float, l: Float) { - view.textSize = when (size) { - "small" -> s + try { + view.textSize = when (size) { + "small" -> s - "medium" -> m + "medium" -> m - "large" -> l + "large" -> l - else -> {0F} - } + else -> { + 0F + } + } + } catch (_: Exception) {} } fun setStatusBar(window: Window) {