From 72f212ec0876a12d98259cd56dd745f9edf03115 Mon Sep 17 00:00:00 2001 From: HeCodes2Much Date: Mon, 20 May 2024 22:46:49 +0100 Subject: [PATCH] Refactor: Refactor some of the setting to also work other places. --- .../droidworksstudio/launcher/Constants.kt | 9 +++ .../launcher/helper/AppExtensions.kt | 81 +++++++++++++++++++ .../launcher/helper/AppHelper.kt | 14 ---- .../launcher/ui/drawer/DrawAdapter.kt | 9 ++- .../launcher/ui/drawer/DrawFragment.kt | 63 +++++++++------ .../launcher/ui/drawer/DrawViewHolder.kt | 17 +++- .../launcher/ui/home/HomeFragment.kt | 9 ++- app/src/main/res/layout/fragment_draw.xml | 21 +++-- app/src/main/res/values-night/styles.xml | 2 - app/src/main/res/values/strings.xml | 8 ++ app/src/main/res/values/styles.xml | 1 - 11 files changed, 175 insertions(+), 59 deletions(-) create mode 100644 app/src/main/java/com/github/droidworksstudio/launcher/helper/AppExtensions.kt diff --git a/app/src/main/java/com/github/droidworksstudio/launcher/Constants.kt b/app/src/main/java/com/github/droidworksstudio/launcher/Constants.kt index 5d983009..fc1a5b9f 100644 --- a/app/src/main/java/com/github/droidworksstudio/launcher/Constants.kt +++ b/app/src/main/java/com/github/droidworksstudio/launcher/Constants.kt @@ -52,5 +52,14 @@ object Constants { const val TOGGLE_SETTING_LOCK = "TOGGLE_SETTING_LOCK" + const val URL_DUCK_SEARCH = "https://duckduckgo.com/?q=" + const val URL_GOOGLE_SEARCH = "https://google.com/search?q=" + const val URL_YAHOO_SEARCH = "https://search.yahoo.com/search?p=" + const val URL_BING_SEARCH = "https://bing.com/search?q=" + const val URL_BRAVE_SEARCH = "https://search.brave.com/search?q=" + const val URL_SWISSCOW_SEARCH = "https://swisscows.com/web?query=" + const val URL_GOOGLE_PLAY_STORE = "https://play.google.com/store/search?c=apps&q" + const val APP_GOOGLE_PLAY_STORE = "market://search?c=apps&q" + const val REQUEST_CODE_ENABLE_ADMIN = 123 } \ No newline at end of file diff --git a/app/src/main/java/com/github/droidworksstudio/launcher/helper/AppExtensions.kt b/app/src/main/java/com/github/droidworksstudio/launcher/helper/AppExtensions.kt new file mode 100644 index 00000000..447c1a0f --- /dev/null +++ b/app/src/main/java/com/github/droidworksstudio/launcher/helper/AppExtensions.kt @@ -0,0 +1,81 @@ +package com.github.droidworksstudio.launcher.helper + +import android.app.SearchManager +import android.content.Context +import android.content.Intent +import android.content.pm.LauncherApps +import android.net.Uri +import android.os.UserHandle +import android.util.Log +import android.view.View +import android.view.inputmethod.InputMethodManager +import android.widget.EditText +import com.github.droidworksstudio.launcher.Constants + +fun View.hideKeyboard() { + this.clearFocus() + val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + imm.hideSoftInputFromWindow(windowToken, 0) +} + +fun View.showKeyboard(show: Boolean = true) { + if (show.not()) return + if (this.requestFocus()) + this.postDelayed({ + this.findViewById(androidx.appcompat.R.id.search_src_text).apply { + textSize = 28f + isCursorVisible = false + } + val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + @Suppress("DEPRECATION") + imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY) + }, 100) +} + +fun Context.openSearch(query: String? = null) { + val intent = Intent(Intent.ACTION_WEB_SEARCH) + intent.putExtra(SearchManager.QUERY, query ?: "") + startActivity(intent) +} + +fun Context.openUrl(url: String) { + if (url.isEmpty()) return + val intent = Intent(Intent.ACTION_VIEW) + intent.data = Uri.parse(url) + startActivity(intent) +} + +fun Context.searchOnPlayStore(query: String? = null): Boolean { + return try { + val playStoreIntent = Intent(Intent.ACTION_VIEW) + playStoreIntent.data = Uri.parse("${Constants.APP_GOOGLE_PLAY_STORE}=$query") + + // Check if the Play Store app is installed + if (playStoreIntent.resolveActivity(packageManager) != null) { + startActivity(playStoreIntent) + } else { + // If Play Store app is not installed, open Play Store website in browser + playStoreIntent.data = Uri.parse("${Constants.URL_GOOGLE_PLAY_STORE}=$query") + startActivity(playStoreIntent) + } + true + } catch (e: Exception) { + e.printStackTrace() + false + } +} + +fun Context.searchCustomSearchEngine(searchQuery: String? = null): Boolean { + val searchUrl = Constants.URL_GOOGLE_SEARCH + val encodedQuery = Uri.encode(searchQuery) + val fullUrl = "$searchUrl$encodedQuery" + Log.d("fullUrl", fullUrl) + openUrl(fullUrl) + return true +} + +fun Context.isPackageInstalled(packageName: String, userHandle: UserHandle = android.os.Process.myUserHandle()): Boolean { + val launcher = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps + val activityInfo = launcher.getActivityList(packageName, userHandle) + return activityInfo.size > 0 +} \ No newline at end of file diff --git a/app/src/main/java/com/github/droidworksstudio/launcher/helper/AppHelper.kt b/app/src/main/java/com/github/droidworksstudio/launcher/helper/AppHelper.kt index 1a643ce8..a420c042 100644 --- a/app/src/main/java/com/github/droidworksstudio/launcher/helper/AppHelper.kt +++ b/app/src/main/java/com/github/droidworksstudio/launcher/helper/AppHelper.kt @@ -261,19 +261,6 @@ class AppHelper @Inject constructor() { return false } - fun showSoftKeyboard(context: Context, view: View) { - if (view.requestFocus()) { - val inputMethodManager: InputMethodManager = - context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager - inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT) - } - } - - fun hideKeyboard(context: Context, view: View) { - val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager - imm.hideSoftInputFromWindow(view.windowToken, 0) - } - fun wordOfTheDay(resources: Resources): String { val dailyWordsArray = resources.getStringArray(R.array.settings_appearance_daily_word_default) val dayOfYear = Calendar.getInstance().get(Calendar.DAY_OF_YEAR) @@ -329,5 +316,4 @@ class AppHelper @Inject constructor() { } } } - } \ No newline at end of file diff --git a/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawAdapter.kt b/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawAdapter.kt index db795c39..27fa0caf 100644 --- a/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawAdapter.kt +++ b/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawAdapter.kt @@ -7,10 +7,13 @@ import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView import com.github.droidworksstudio.launcher.data.entities.AppInfo import com.github.droidworksstudio.launcher.databinding.ItemDrawBinding +import com.github.droidworksstudio.launcher.helper.PreferenceHelper import com.github.droidworksstudio.launcher.listener.OnItemClickedListener class DrawAdapter(private val onAppClickedListener: OnItemClickedListener.OnAppsClickedListener, - private val onAppLongClickedListener: OnItemClickedListener.OnAppLongClickedListener) : + private val onAppLongClickedListener: OnItemClickedListener.OnAppLongClickedListener, + private val preferenceHelperProvider: PreferenceHelper +) : ListAdapter(DiffCallback()) { override fun onCreateViewHolder( @@ -22,7 +25,9 @@ class DrawAdapter(private val onAppClickedListener: OnItemClickedListener.OnApps parent, false ) - return DrawViewHolder(binding, onAppClickedListener, onAppLongClickedListener) + val preferenceHelper = preferenceHelperProvider + + return DrawViewHolder(binding, onAppClickedListener, onAppLongClickedListener, preferenceHelper) } override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { diff --git a/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawFragment.kt b/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawFragment.kt index cc3cec7c..0367bb04 100644 --- a/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawFragment.kt +++ b/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawFragment.kt @@ -1,14 +1,14 @@ package com.github.droidworksstudio.launcher.ui.drawer +import android.annotation.SuppressLint import android.content.Context import android.os.Bundle -import android.text.Editable -import android.text.TextWatcher import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast +import androidx.appcompat.widget.SearchView import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.coroutineScope @@ -20,6 +20,11 @@ import com.github.droidworksstudio.launcher.databinding.FragmentDrawBinding import com.github.droidworksstudio.launcher.helper.AppHelper import com.github.droidworksstudio.launcher.helper.FingerprintHelper import com.github.droidworksstudio.launcher.helper.PreferenceHelper +import com.github.droidworksstudio.launcher.helper.hideKeyboard +import com.github.droidworksstudio.launcher.helper.openSearch +import com.github.droidworksstudio.launcher.helper.searchCustomSearchEngine +import com.github.droidworksstudio.launcher.helper.searchOnPlayStore +import com.github.droidworksstudio.launcher.helper.showKeyboard import com.github.droidworksstudio.launcher.listener.OnItemClickedListener import com.github.droidworksstudio.launcher.ui.bottomsheetdialog.AppInfoBottomSheetFragment import com.github.droidworksstudio.launcher.viewmodel.AppViewModel @@ -40,9 +45,8 @@ class DrawFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener, private val binding get() = _binding!! - private val viewModel: AppViewModel by viewModels() - - private val drawAdapter: DrawAdapter by lazy { DrawAdapter(this, this) } + @Inject + lateinit var preferenceHelper: PreferenceHelper @Inject lateinit var appHelper: AppHelper @@ -50,8 +54,9 @@ class DrawFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener, @Inject lateinit var fingerHelper: FingerprintHelper - @Inject - lateinit var preferenceHelper: PreferenceHelper + private val viewModel: AppViewModel by viewModels() + + private val drawAdapter: DrawAdapter by lazy { DrawAdapter(this, this, preferenceHelper) } private lateinit var context: Context override fun onCreateView( @@ -96,29 +101,41 @@ class DrawFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener, } } + @SuppressLint("RestrictedApi") private fun setupSearch() { - binding.searchViewText.addTextChangedListener(object: TextWatcher { - override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { - // Do Nothing - } - - override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { - searchApp(s.toString()) + binding.searchViewText.setOnQueryTextListener(object : SearchView.OnQueryTextListener { + override fun onQueryTextSubmit(query: String?): Boolean { + query?.let { + val trimmedQuery = it.trim() + if (trimmedQuery.isNotEmpty()) { + if (trimmedQuery.startsWith("!")) { + val searchQuery = trimmedQuery.substringAfter("!") + requireContext().searchCustomSearchEngine(searchQuery) + } else { + if (!requireContext().searchOnPlayStore(trimmedQuery)) { + requireContext().openSearch(trimmedQuery) + } + return true // Exit the function + } + } + } + return true } - - override fun afterTextChanged(s: Editable?) { - // Do Nothing + override fun onQueryTextChange(newText: String?): Boolean { + searchApp(newText.toString()) + return true } }) + } private fun observeClickListener(){ binding.drawSearchButton.setOnClickListener { - appHelper.showSoftKeyboard(context, binding.searchViewText) + binding.searchViewText.showKeyboard() } } - private fun searchApp(query: String?) { + private fun searchApp(query: String) { val searchQuery = "%$query%" @Suppress("DEPRECATION") viewLifecycleOwner.lifecycle.coroutineScope.launchWhenCreated { @@ -142,7 +159,7 @@ class DrawFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener, } private fun showSelectedApp(appInfo: AppInfo) { - binding.searchViewText.text?.clear() + binding.searchViewText.setQuery("", false) val bottomSheetFragment = AppInfoBottomSheetFragment(appInfo) bottomSheetFragment.setOnBottomSheetDismissedListener(this) @@ -157,19 +174,19 @@ class DrawFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener, override fun onPause() { super.onPause() - binding.searchViewText.text?.clear() + binding.searchViewText.setQuery("", false) } override fun onResume() { super.onResume() observeDrawerApps() binding.drawAdapter.scrollToPosition(0) - if (preferenceHelper.automaticKeyboard) appHelper.showSoftKeyboard(context, binding.searchViewText) + if (preferenceHelper.automaticKeyboard) binding.searchViewText.showKeyboard() } override fun onStop() { super.onStop() - appHelper.hideKeyboard(context, binding.searchView) + binding.searchViewText.hideKeyboard() } override fun onAppClicked(appInfo: AppInfo) { diff --git a/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawViewHolder.kt b/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawViewHolder.kt index 8536206e..0e6f5147 100644 --- a/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawViewHolder.kt +++ b/app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawViewHolder.kt @@ -1,18 +1,33 @@ package com.github.droidworksstudio.launcher.ui.drawer import android.util.Log +import androidx.appcompat.widget.LinearLayoutCompat import androidx.recyclerview.widget.RecyclerView import com.github.droidworksstudio.launcher.data.entities.AppInfo import com.github.droidworksstudio.launcher.databinding.ItemDrawBinding +import com.github.droidworksstudio.launcher.helper.PreferenceHelper import com.github.droidworksstudio.launcher.listener.OnItemClickedListener class DrawViewHolder(private val binding: ItemDrawBinding, private val onAppClickedListener: OnItemClickedListener.OnAppsClickedListener, - private val onAppLongClickedListener: OnItemClickedListener.OnAppLongClickedListener) : + private val onAppLongClickedListener: OnItemClickedListener.OnAppLongClickedListener, + private val preferenceHelper: PreferenceHelper): RecyclerView.ViewHolder(binding.root) { fun bind(appInfo: AppInfo) { binding.apply { + val layoutParams = LinearLayoutCompat.LayoutParams( + LinearLayoutCompat.LayoutParams.WRAP_CONTENT, + LinearLayoutCompat.LayoutParams.WRAP_CONTENT + ).apply { + gravity = preferenceHelper.homeAppAlignment + topMargin = preferenceHelper.homeAppPadding.toInt() + bottomMargin = preferenceHelper.homeAppPadding.toInt() + } + + appDrawName.layoutParams = layoutParams appDrawName.text = appInfo.appName + appDrawName.setTextColor(preferenceHelper.appColor) + appDrawName.textSize = preferenceHelper.appTextSize Log.d("Tag", "Draw Adapter: ${appInfo.appName + appInfo.id}") } diff --git a/app/src/main/java/com/github/droidworksstudio/launcher/ui/home/HomeFragment.kt b/app/src/main/java/com/github/droidworksstudio/launcher/ui/home/HomeFragment.kt index 5abd5b29..db8b2d37 100644 --- a/app/src/main/java/com/github/droidworksstudio/launcher/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/github/droidworksstudio/launcher/ui/home/HomeFragment.kt @@ -25,6 +25,7 @@ import com.github.droidworksstudio.launcher.databinding.FragmentHomeBinding import com.github.droidworksstudio.launcher.helper.AppHelper import com.github.droidworksstudio.launcher.helper.FingerprintHelper import com.github.droidworksstudio.launcher.helper.PreferenceHelper +import com.github.droidworksstudio.launcher.helper.hideKeyboard import com.github.droidworksstudio.launcher.listener.OnItemClickedListener import com.github.droidworksstudio.launcher.listener.OnSwipeTouchListener import com.github.droidworksstudio.launcher.listener.ScrollEventListener @@ -98,7 +99,7 @@ class HomeFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener, @SuppressLint("ClickableViewAccessibility") private fun initializeInjectedDependencies() { context = requireContext() - appHelper.hideKeyboard(context, binding.mainView) + binding.mainView.hideKeyboard() binding.nestScrollView.scrollEventListener = this @@ -158,7 +159,7 @@ class HomeFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener, } private fun observeUserInterfaceSettings() { - appHelper.hideKeyboard(context, binding.mainView) + binding.mainView.hideKeyboard() preferenceViewModel.setShowTime(preferenceHelper.showTime) preferenceViewModel.setShowDate(preferenceHelper.showDate) @@ -301,12 +302,12 @@ class HomeFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener, override fun onPause() { super.onPause() - appHelper.hideKeyboard(context, binding.mainView) + binding.mainView.hideKeyboard() } override fun onResume() { super.onResume() - appHelper.hideKeyboard(context, binding.mainView) + binding.mainView.hideKeyboard() observeUserInterfaceSettings() observeFavoriteAppList() } diff --git a/app/src/main/res/layout/fragment_draw.xml b/app/src/main/res/layout/fragment_draw.xml index 468b629d..f2c3129f 100644 --- a/app/src/main/res/layout/fragment_draw.xml +++ b/app/src/main/res/layout/fragment_draw.xml @@ -24,7 +24,7 @@ android:orientation="horizontal" tools:ignore="DuplicateClickableBoundsCheck"> - - - - - + app:closeIcon="@null" + app:iconifiedByDefault="false" + app:queryBackground="@null" + app:searchIcon="@null" + app:queryHint="@string/search" + tools:ignore="VisualLintTextFieldSize"/> + android:layout_height="wrap_content" /> + 1 2 - - \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6ef017eb..527cb3d0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -549,4 +549,12 @@ Center Right + + Search Engine + Google + Yahoo + DuckDuckGo + Bing + Brave + SwissCows \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 31979127..3c34a133 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -78,5 +78,4 @@ 1 2 - \ No newline at end of file