Feat: Added a default search engine so can use other apps other then ones selected
This commit is contained in:
@@ -140,11 +140,38 @@ fun Context.showShortToast(message: String) {
|
||||
}
|
||||
|
||||
fun Context.openSearch(query: String? = null) {
|
||||
val intent = Intent(Intent.ACTION_WEB_SEARCH)
|
||||
intent.putExtra(SearchManager.QUERY, query ?: "")
|
||||
startActivity(intent)
|
||||
val intent = Intent(Intent.ACTION_WEB_SEARCH).apply {
|
||||
putExtra(SearchManager.QUERY, query ?: "")
|
||||
}
|
||||
// Check if there is an app that can handle the web search intent
|
||||
val resolvedActivity = intent.resolveActivity(packageManager)
|
||||
|
||||
if (resolvedActivity != null) {
|
||||
try {
|
||||
// Get the package name of the app that can handle the intent
|
||||
val packageName = resolvedActivity.packageName
|
||||
val appInfo = packageManager.getApplicationInfo(packageName, 0)
|
||||
val appName = packageManager.getApplicationLabel(appInfo).toString()
|
||||
|
||||
// Log the name of the app that will handle the search
|
||||
Log.d("WebSearchApp", "Search will be handled by: $appName ($packageName)")
|
||||
|
||||
startActivity(intent)
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
} else {
|
||||
val fallbackUrl = "${Constants.URL_GOOGLE_SEARCH}${Uri.encode(query ?: "")}"
|
||||
val fallbackIntent = Intent(Intent.ACTION_VIEW, Uri.parse(fallbackUrl))
|
||||
try {
|
||||
startActivity(fallbackIntent)
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun Context.openUrl(url: String) {
|
||||
if (url.isEmpty()) return
|
||||
val intent = Intent(Intent.ACTION_VIEW)
|
||||
@@ -167,12 +194,6 @@ fun Context.resetDefaultLauncher() {
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.searchView() {
|
||||
val intent = Intent(Intent.ACTION_WEB_SEARCH)
|
||||
intent.putExtra(SearchManager.QUERY, "")
|
||||
this.startActivity(intent)
|
||||
}
|
||||
|
||||
fun Context.unInstallApp(appInfo: AppInfo) {
|
||||
val intent = Intent(Intent.ACTION_DELETE)
|
||||
intent.data = Uri.parse("package:${appInfo.packageName}")
|
||||
@@ -318,13 +339,20 @@ fun Context.searchCustomSearchEngine(
|
||||
Constants.SearchEngines.SwissCow -> {
|
||||
Constants.URL_SWISSCOW_SEARCH
|
||||
}
|
||||
}
|
||||
|
||||
val encodedQuery = Uri.encode(searchQuery)
|
||||
val fullUrl = "$searchUrl$encodedQuery"
|
||||
Log.d("fullUrl", fullUrl)
|
||||
openUrl(fullUrl)
|
||||
return true
|
||||
else -> {
|
||||
openSearch(searchQuery)
|
||||
null // Returning null because we're launching the intent directly
|
||||
}
|
||||
}
|
||||
if (searchUrl != null) {
|
||||
val encodedQuery = Uri.encode(searchQuery)
|
||||
val fullUrl = "$searchUrl$encodedQuery"
|
||||
Log.d("fullUrl", fullUrl)
|
||||
openUrl(fullUrl)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fun Context.isWorkProfileEnabled(): Boolean {
|
||||
|
||||
@@ -161,11 +161,11 @@ class PreferenceHelper @Inject constructor(@ApplicationContext context: Context)
|
||||
Constants.SearchEngines.valueOf(
|
||||
prefs.getString(
|
||||
Constants.SEARCH_ENGINE,
|
||||
Constants.SearchEngines.Google.name
|
||||
Constants.SearchEngines.Default.name
|
||||
).toString()
|
||||
)
|
||||
} catch (_: Exception) {
|
||||
Constants.SearchEngines.Google
|
||||
Constants.SearchEngines.Default
|
||||
}
|
||||
}
|
||||
set(value) = prefs.edit().putString(Constants.SEARCH_ENGINE, value.name).apply()
|
||||
|
||||
@@ -97,6 +97,7 @@ object Constants {
|
||||
const val LONG_PRESS_DELAY_MS = 500
|
||||
|
||||
enum class SearchEngines {
|
||||
Default,
|
||||
Google,
|
||||
Yahoo,
|
||||
DuckDuckGo,
|
||||
@@ -106,6 +107,7 @@ object Constants {
|
||||
|
||||
fun getString(context: Context): String {
|
||||
return when (this) {
|
||||
Default -> context.getString(R.string.search_default)
|
||||
Google -> context.getString(R.string.search_google)
|
||||
Yahoo -> context.getString(R.string.search_yahoo)
|
||||
DuckDuckGo -> context.getString(R.string.search_duckduckgo)
|
||||
|
||||
@@ -145,6 +145,7 @@
|
||||
<string name="admin_permission_message">Easy launcher promises to use lock screen permission responsibly. Easy launcher does not collect or share any data.</string>
|
||||
|
||||
<string name="search_engine">Search Engine</string>
|
||||
<string name="search_default">Default</string>
|
||||
<string name="search_google">Google</string>
|
||||
<string name="search_yahoo">Yahoo</string>
|
||||
<string name="search_duckduckgo">DuckDuckGo</string>
|
||||
|
||||
Reference in New Issue
Block a user