Skip to content

Commit 0d2527a

Browse files
Feat: Added extra search engines to the settings.
Signed-off-by: HeCodes2Much <[email protected]>
1 parent e94fc6c commit 0d2527a

File tree

11 files changed

+218
-19
lines changed

11 files changed

+218
-19
lines changed

app/build.gradle.kts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,20 @@ android {
5252
sourceCompatibility = JavaVersion.VERSION_17
5353
targetCompatibility = JavaVersion.VERSION_17
5454
}
55+
5556
kotlinOptions {
5657
jvmTarget = JavaVersion.VERSION_17.toString()
5758
}
59+
5860
buildFeatures {
61+
compose = true
5962
viewBinding = true
6063
dataBinding = true
64+
buildConfig = true
65+
}
66+
67+
composeOptions {
68+
kotlinCompilerExtensionVersion = "1.4.3"
6169
}
6270

6371
applicationVariants.all {
@@ -99,6 +107,8 @@ dependencies {
99107
implementation(libs.biometric.ktx)
100108
implementation(libs.room.ktx)
101109
implementation(libs.room.runtime)
110+
implementation(libs.androidx.runtime.android)
111+
implementation(libs.androidx.ui.android)
102112

103113
//noinspection KaptUsageInsteadOfKsp
104114
kapt(libs.room.compiler)

app/src/main/java/com/github/droidworksstudio/ktx/ContextExtensions.kt

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import androidx.lifecycle.LifecycleObserver
3737
import androidx.lifecycle.LifecycleOwner
3838
import com.github.droidworksstudio.launcher.Constants
3939
import com.github.droidworksstudio.launcher.data.entities.AppInfo
40+
import com.github.droidworksstudio.launcher.helper.PreferenceHelper
4041
import com.github.droidworksstudio.launcher.ui.activities.FakeHomeActivity
4142
import java.io.File
4243
import java.io.IOException
@@ -290,8 +291,37 @@ fun Context.searchOnPlayStore(query: String? = null): Boolean {
290291
}
291292
}
292293

293-
fun Context.searchCustomSearchEngine(searchQuery: String? = null): Boolean {
294-
val searchUrl = Constants.URL_GOOGLE_SEARCH
294+
fun Context.searchCustomSearchEngine(
295+
preferenceHelper: PreferenceHelper,
296+
searchQuery: String? = null
297+
): Boolean {
298+
299+
val searchUrl = when (preferenceHelper.searchEngines) {
300+
Constants.SearchEngines.Google -> {
301+
Constants.URL_GOOGLE_SEARCH
302+
}
303+
304+
Constants.SearchEngines.Yahoo -> {
305+
Constants.URL_YAHOO_SEARCH
306+
}
307+
308+
Constants.SearchEngines.DuckDuckGo -> {
309+
Constants.URL_DUCK_SEARCH
310+
}
311+
312+
Constants.SearchEngines.Bing -> {
313+
Constants.URL_BING_SEARCH
314+
}
315+
316+
Constants.SearchEngines.Brave -> {
317+
Constants.URL_BRAVE_SEARCH
318+
}
319+
320+
Constants.SearchEngines.SwissCow -> {
321+
Constants.URL_SWISSCOW_SEARCH
322+
}
323+
}
324+
295325
val encodedQuery = Uri.encode(searchQuery)
296326
val fullUrl = "$searchUrl$encodedQuery"
297327
Log.d("fullUrl", fullUrl)

app/src/main/java/com/github/droidworksstudio/launcher/Constants.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.droidworksstudio.launcher
22

3+
import android.content.Context
4+
35
object Constants {
46
const val PACKAGE_NAME = "app.easy.launcher"
57
const val PACKAGE_NAME_DEBUG = "$PACKAGE_NAME.debug"
@@ -53,6 +55,7 @@ object Constants {
5355

5456
const val TOGGLE_SETTING_LOCK = "TOGGLE_SETTING_LOCK"
5557

58+
const val SEARCH_ENGINE = "SEARCH_ENGINE"
5659
const val URL_DUCK_SEARCH = "https://duckduckgo.com/?q="
5760
const val URL_GOOGLE_SEARCH = "https://google.com/search?q="
5861
const val URL_YAHOO_SEARCH = "https://search.yahoo.com/search?p="
@@ -65,4 +68,24 @@ object Constants {
6568
const val APP_WIDGET_HOST_ID = 1024
6669
const val TRIPLE_TAP_DELAY_MS = 300
6770
const val LONG_PRESS_DELAY_MS = 500
71+
72+
enum class SearchEngines {
73+
Google,
74+
Yahoo,
75+
DuckDuckGo,
76+
Bing,
77+
Brave,
78+
SwissCow;
79+
80+
fun getString(context: Context): String {
81+
return when (this) {
82+
Google -> context.getString(R.string.search_google)
83+
Yahoo -> context.getString(R.string.search_yahoo)
84+
DuckDuckGo -> context.getString(R.string.search_duckduckgo)
85+
Bing -> context.getString(R.string.search_bing)
86+
Brave -> context.getString(R.string.search_brave)
87+
SwissCow -> context.getString(R.string.search_swisscow)
88+
}
89+
}
90+
}
6891
}

app/src/main/java/com/github/droidworksstudio/launcher/helper/AppHelper.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,20 @@ import android.view.Window
1616
import android.view.WindowInsets
1717
import android.widget.TextView
1818
import androidx.appcompat.widget.LinearLayoutCompat
19+
import androidx.fragment.app.viewModels
1920
import com.github.droidworksstudio.ktx.backupSharedPreferences
2021
import com.github.droidworksstudio.ktx.restoreSharedPreferences
21-
//import com.github.droidworksstudio.ktx.backupSharedPreferences
22-
//import com.github.droidworksstudio.ktx.restoreSharedPreferences
2322
import com.github.droidworksstudio.ktx.showLongToast
2423
import com.google.android.material.dialog.MaterialAlertDialogBuilder
2524
import com.github.droidworksstudio.launcher.Constants
2625
import com.github.droidworksstudio.launcher.R
2726
import com.github.droidworksstudio.launcher.accessibility.ActionService
27+
import com.github.droidworksstudio.launcher.viewmodel.PreferenceViewModel
2828
import java.util.Calendar
2929
import javax.inject.Inject
3030

3131
class AppHelper @Inject constructor() {
32+
3233
@SuppressLint("WrongConstant", "PrivateApi")
3334
fun expandNotificationDrawer(context: Context) {
3435
try {

app/src/main/java/com/github/droidworksstudio/launcher/helper/PreferenceHelper.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class PreferenceHelper @Inject constructor(@ApplicationContext context: Context)
8484

8585
var homeDailyWordAlignment: Int
8686
get() = prefs.getInt(Constants.HOME_DAILY_WORD_ALIGNMENT, Gravity.START)
87-
set(value) = prefs.edit().putInt(Constants.HOME_DAILY_WORD_ALIGNMENT,value).apply()
87+
set(value) = prefs.edit().putInt(Constants.HOME_DAILY_WORD_ALIGNMENT, value).apply()
8888

8989
var batteryTextSize: Float
9090
get() = prefs.getFloat(Constants.BATTERY_TEXT_SIZE, 12f)
@@ -121,4 +121,19 @@ class PreferenceHelper @Inject constructor(@ApplicationContext context: Context)
121121
var swipeSearch: Boolean
122122
get() = prefs.getBoolean(Constants.SWIPE_SEARCH, false)
123123
set(value) = prefs.edit().putBoolean(Constants.SWIPE_SEARCH, value).apply()
124+
125+
var searchEngines: Constants.SearchEngines
126+
get() {
127+
return try {
128+
Constants.SearchEngines.valueOf(
129+
prefs.getString(
130+
Constants.SEARCH_ENGINE,
131+
Constants.SearchEngines.Google.name
132+
).toString()
133+
)
134+
} catch (_: Exception) {
135+
Constants.SearchEngines.Google
136+
}
137+
}
138+
set(value) = prefs.edit().putString(Constants.SEARCH_ENGINE, value.name).apply()
124139
}

app/src/main/java/com/github/droidworksstudio/launcher/ui/drawer/DrawFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class DrawFragment : Fragment(),
125125
if (trimmedQuery.isNotEmpty()) {
126126
if (trimmedQuery.startsWith("!")) {
127127
val searchQuery = trimmedQuery.substringAfter("!")
128-
requireContext().searchCustomSearchEngine(searchQuery)
128+
requireContext().searchCustomSearchEngine(preferenceHelper, searchQuery)
129129
} else {
130130
checkAppThenRun(trimmedQuery)
131131
return true // Exit the function

app/src/main/java/com/github/droidworksstudio/launcher/ui/settings/SettingsFragment.kt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import androidx.navigation.NavController
1313
import androidx.navigation.fragment.findNavController
1414
import com.github.droidworksstudio.ktx.resetDefaultLauncher
1515
import com.github.droidworksstudio.ktx.restartApp
16+
import com.github.droidworksstudio.launcher.Constants
1617
import com.github.droidworksstudio.launcher.R
1718
import com.github.droidworksstudio.launcher.databinding.FragmentSettingsBinding
1819
import com.github.droidworksstudio.launcher.helper.AppHelper
@@ -24,6 +25,7 @@ import com.github.droidworksstudio.launcher.ui.bottomsheetdialog.ColorBottomShee
2425
import com.github.droidworksstudio.launcher.ui.bottomsheetdialog.PaddingBottomSheetDialogFragment
2526
import com.github.droidworksstudio.launcher.ui.bottomsheetdialog.TextBottomSheetDialogFragment
2627
import com.github.droidworksstudio.launcher.viewmodel.PreferenceViewModel
28+
import com.google.android.material.dialog.MaterialAlertDialogBuilder
2729
import dagger.hilt.android.AndroidEntryPoint
2830
import javax.inject.Inject
2931

@@ -76,6 +78,9 @@ class SettingsFragment : Fragment(),
7678
getString(R.string.app_name),
7779
packageInfo.versionName
7880
)
81+
82+
val searchEngine = preferenceHelper.searchEngines.toString()
83+
binding.searchEngineText.text = searchEngine
7984
}
8085

8186
@SuppressLint("SetTextI18n")
@@ -138,6 +143,10 @@ class SettingsFragment : Fragment(),
138143
bottomSheetFragment.show(parentFragmentManager, "BottomSheetDialog")
139144
}
140145

146+
binding.miscellaneousSearchEngine.setOnClickListener {
147+
showSearchEngineDialog()
148+
}
149+
141150
binding.shareView.setOnClickListener {
142151
appHelper.shareAppButton(requireContext())
143152
}
@@ -161,6 +170,7 @@ class SettingsFragment : Fragment(),
161170
}
162171
}
163172

173+
164174
private fun setupSwitchListeners() {
165175
binding.statueBarSwitchCompat.setOnCheckedChangeListener { _, isChecked ->
166176
preferenceViewModel.setShowStatusBar(isChecked)
@@ -234,4 +244,51 @@ class SettingsFragment : Fragment(),
234244

235245
}
236246
}
247+
248+
private fun showSearchEngineDialog() {
249+
// Get the array of SearchEngines enum values
250+
val items = Constants.SearchEngines.values()
251+
252+
// Map the enum values to their string representations
253+
val itemStrings = items.map { it.getString(context) }.toTypedArray()
254+
255+
val dialog = MaterialAlertDialogBuilder(context)
256+
257+
dialog.setTitle("Select a Search Engine")
258+
dialog.setItems(itemStrings) { _, which ->
259+
val selectedItem = items[which]
260+
when (selectedItem) {
261+
Constants.SearchEngines.Google -> {
262+
preferenceViewModel.setSearchEngine(Constants.SearchEngines.Google)
263+
binding.searchEngineText.text = preferenceHelper.searchEngines.name
264+
}
265+
266+
Constants.SearchEngines.Bing -> {
267+
preferenceViewModel.setSearchEngine(Constants.SearchEngines.Bing)
268+
binding.searchEngineText.text = preferenceHelper.searchEngines.name
269+
}
270+
271+
Constants.SearchEngines.Brave -> {
272+
preferenceViewModel.setSearchEngine(Constants.SearchEngines.Brave)
273+
binding.searchEngineText.text = preferenceHelper.searchEngines.name
274+
}
275+
276+
Constants.SearchEngines.Yahoo -> {
277+
preferenceViewModel.setSearchEngine(Constants.SearchEngines.Yahoo)
278+
binding.searchEngineText.text = preferenceHelper.searchEngines.name
279+
}
280+
281+
Constants.SearchEngines.DuckDuckGo -> {
282+
preferenceViewModel.setSearchEngine(Constants.SearchEngines.DuckDuckGo)
283+
binding.searchEngineText.text = preferenceHelper.searchEngines.name
284+
}
285+
286+
Constants.SearchEngines.SwissCow -> {
287+
preferenceViewModel.setSearchEngine(Constants.SearchEngines.SwissCow)
288+
binding.searchEngineText.text = preferenceHelper.searchEngines.name
289+
}
290+
}
291+
}
292+
dialog.show()
293+
}
237294
}

app/src/main/java/com/github/droidworksstudio/launcher/viewmodel/PreferenceViewModel.kt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package com.github.droidworksstudio.launcher.viewmodel
22

33
import androidx.lifecycle.MutableLiveData
44
import androidx.lifecycle.ViewModel
5+
import com.github.droidworksstudio.launcher.Constants
56
import com.github.droidworksstudio.launcher.helper.PreferenceHelper
67
import dagger.hilt.android.lifecycle.HiltViewModel
78
import javax.inject.Inject
89

910
@HiltViewModel
1011
class PreferenceViewModel @Inject constructor(
11-
private val preferenceHelper: PreferenceHelper)
12-
: ViewModel() {
12+
private val preferenceHelper: PreferenceHelper
13+
) : ViewModel() {
1314

1415
private val firstLaunchLiveData: MutableLiveData<Boolean> = MutableLiveData()
1516
val showStatusBarLiveData: MutableLiveData<Boolean> = MutableLiveData()
@@ -39,6 +40,8 @@ class PreferenceViewModel @Inject constructor(
3940
private val lockSettingsLiveData: MutableLiveData<Boolean> = MutableLiveData()
4041
private val appPaddingSizeLiveData: MutableLiveData<Float> = MutableLiveData()
4142

43+
private val searchEngineLiveData: MutableLiveData<Constants.SearchEngines> = MutableLiveData()
44+
4245
fun setFirstLaunch(firstLaunch: Boolean) {
4346
preferenceHelper.firstLaunch = firstLaunch
4447
firstLaunchLiveData.postValue(preferenceHelper.firstLaunch)
@@ -59,7 +62,7 @@ class PreferenceViewModel @Inject constructor(
5962
showDateLiveData.postValue(preferenceHelper.showDate)
6063
}
6164

62-
fun setShowBattery(showBattery: Boolean){
65+
fun setShowBattery(showBattery: Boolean) {
6366
preferenceHelper.showBattery = showBattery
6467
showBatteryLiveData.postValue(preferenceHelper.showBattery)
6568
}
@@ -94,7 +97,7 @@ class PreferenceViewModel @Inject constructor(
9497
timeColorLiveData.postValue(preferenceHelper.timeColor)
9598
}
9699

97-
fun setBatteryColor(batteryColor: Int){
100+
fun setBatteryColor(batteryColor: Int) {
98101
preferenceHelper.batteryColor = batteryColor
99102
batteryColorLiveData.postValue(preferenceHelper.batteryColor)
100103
}
@@ -144,33 +147,38 @@ class PreferenceViewModel @Inject constructor(
144147
appPaddingSizeLiveData.postValue(preferenceHelper.homeAppPadding)
145148
}
146149

147-
fun setDoubleTapLock(tapLockScreen: Boolean){
150+
fun setDoubleTapLock(tapLockScreen: Boolean) {
148151
preferenceHelper.tapLockScreen = tapLockScreen
149152
tapLockScreenLiveData.postValue((preferenceHelper.tapLockScreen))
150153
}
151154

152-
fun setSwipeNotification(swipeNotification: Boolean){
155+
fun setSwipeNotification(swipeNotification: Boolean) {
153156
preferenceHelper.swipeNotification = swipeNotification
154157
swipeNotificationLiveData.postValue((preferenceHelper.swipeNotification))
155158
}
156159

157-
fun setSwipeSearch(swipeSearch: Boolean){
160+
fun setSwipeSearch(swipeSearch: Boolean) {
158161
preferenceHelper.swipeSearch = swipeSearch
159162
swipeSearchLiveData.postValue((preferenceHelper.swipeSearch))
160163
}
161164

162-
fun setAutoKeyboard(autoKeyboard: Boolean){
165+
fun setAutoKeyboard(autoKeyboard: Boolean) {
163166
preferenceHelper.automaticKeyboard = autoKeyboard
164167
autoKeyboardLiveData.postValue((preferenceHelper.automaticKeyboard))
165168
}
166169

167-
fun setAutoOpenApp(autoOpenApp: Boolean){
170+
fun setAutoOpenApp(autoOpenApp: Boolean) {
168171
preferenceHelper.automaticOpenApp = autoOpenApp
169172
autoOpenAppsLiveData.postValue((preferenceHelper.automaticOpenApp))
170173
}
171174

172-
fun setLockSettings(lockSettings: Boolean){
175+
fun setLockSettings(lockSettings: Boolean) {
173176
preferenceHelper.settingsLock = lockSettings
174177
lockSettingsLiveData.postValue((preferenceHelper.settingsLock))
175178
}
179+
180+
fun setSearchEngine(searchEngine: Constants.SearchEngines) {
181+
preferenceHelper.searchEngines = searchEngine
182+
searchEngineLiveData.postValue((preferenceHelper.searchEngines))
183+
}
176184
}

0 commit comments

Comments
 (0)