Refactor: Refactor some of the setting to also work other places.

This commit is contained in:
HeCodes2Much
2024-05-20 22:46:49 +01:00
parent 7d04c2070f
commit 72f212ec08
11 changed files with 175 additions and 59 deletions

View File

@@ -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
}

View File

@@ -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<EditText>(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
}

View File

@@ -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() {
}
}
}
}

View File

@@ -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<AppInfo, RecyclerView.ViewHolder>(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) {

View File

@@ -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
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 onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
searchApp(s.toString())
}
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) {

View File

@@ -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}")
}

View File

@@ -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()
}

View File

@@ -24,7 +24,7 @@
android:orientation="horizontal"
tools:ignore="DuplicateClickableBoundsCheck">
<androidx.appcompat.widget.AppCompatEditText
<androidx.appcompat.widget.SearchView
android:id="@+id/search_view_text"
style="@style/TextDefaultStyle"
android:layout_width="match_parent"
@@ -34,23 +34,20 @@
android:inputType="text"
android:textSize="@dimen/text_super_large"
android:textStyle="normal"
tools:ignore="VisualLintTextFieldSize">
</androidx.appcompat.widget.AppCompatEditText>
<androidx.appcompat.widget.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
app:closeIcon="@null"
app:iconifiedByDefault="false"
app:queryBackground="@null"
app:searchIcon="@null"
app:queryHint="@string/search"
tools:ignore="VisualLintTextFieldSize"/>
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/drawAdapter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
android:layout_height="wrap_content" />
</androidx.appcompat.widget.LinearLayoutCompat>
<com.google.android.material.floatingactionbutton.FloatingActionButton

View File

@@ -78,6 +78,4 @@
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">2</item>
</style>
</resources>

View File

@@ -549,4 +549,12 @@
<item>Center</item>
<item>Right</item>
</string-array>
<string name="search_engine">Search Engine</string>
<string name="search_google">Google</string>
<string name="search_yahoo">Yahoo</string>
<string name="search_duckduckgo">DuckDuckGo</string>
<string name="search_bing">Bing</string>
<string name="search_brave">Brave</string>
<string name="search_swisscow">SwissCows</string>
</resources>

View File

@@ -78,5 +78,4 @@
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">2</item>
</style>
</resources>