From bbdc62648a7e2bcf8c0f42ef3097d30147db7c95 Mon Sep 17 00:00:00 2001 From: Yash Garg Date: Tue, 3 Jan 2023 16:12:14 +0530 Subject: [PATCH] feat: add support for mojeek and rewrite file in kotlin --- .../suggestions/SearchSuggestionUtil.java | 52 --------------- .../suggestions/SearchSuggestionUtil.kt | 65 +++++++++++++++++++ 2 files changed, 65 insertions(+), 52 deletions(-) delete mode 100644 app/src/main/java/foundation/e/blisslauncher/features/suggestions/SearchSuggestionUtil.java create mode 100644 app/src/main/java/foundation/e/blisslauncher/features/suggestions/SearchSuggestionUtil.kt diff --git a/app/src/main/java/foundation/e/blisslauncher/features/suggestions/SearchSuggestionUtil.java b/app/src/main/java/foundation/e/blisslauncher/features/suggestions/SearchSuggestionUtil.java deleted file mode 100644 index fe41821476..0000000000 --- a/app/src/main/java/foundation/e/blisslauncher/features/suggestions/SearchSuggestionUtil.java +++ /dev/null @@ -1,52 +0,0 @@ -package foundation.e.blisslauncher.features.suggestions; - -import android.content.ContentResolver; -import android.content.Context; -import android.database.Cursor; -import android.net.Uri; - -public class SearchSuggestionUtil { - - private static final String SPOT_URL = "https://spot.murena.io/"; - - public SuggestionProvider getSuggestionProvider(Context context) { - String defaultSearchEngine = defaultSearchEngine(context); - if (defaultSearchEngine != null && defaultSearchEngine.length() > 0) { - defaultSearchEngine = defaultSearchEngine.toLowerCase(); - if (defaultSearchEngine.contains("qwant")) { - return new QwantProvider(); - } else { - return new DuckDuckGoProvider(); - } - } else { - return new DuckDuckGoProvider(); - } - } - - public Uri getUriForQuery(Context context, String query) { - String defaultSearchEngine = defaultSearchEngine(context); - if (defaultSearchEngine != null && defaultSearchEngine.length() > 0) { - defaultSearchEngine = defaultSearchEngine.toLowerCase(); - if (defaultSearchEngine.contains("qwant")) { - return Uri.parse("https://www.qwant.com/?q=" + query); - } else if (defaultSearchEngine.contains("duckduckgo")) { - return Uri.parse("https://duckduckgo.com/?q=" + query); - } else { - return Uri.parse(SPOT_URL + "?q=" + query); - } - } else { - return Uri.parse(SPOT_URL + "?q=" + query); - } - } - - private String defaultSearchEngine(Context context) { - ContentResolver contentResolver = context.getContentResolver(); - Uri uri = Uri.parse("content://foundation.e.browser.provider").buildUpon().appendPath("search_engine").build(); - Cursor cursor = contentResolver.query(uri, null, null, null, null); - if (cursor != null && cursor.moveToFirst()) { - return cursor.getString(0); - } else { - return ""; - } - } -} diff --git a/app/src/main/java/foundation/e/blisslauncher/features/suggestions/SearchSuggestionUtil.kt b/app/src/main/java/foundation/e/blisslauncher/features/suggestions/SearchSuggestionUtil.kt new file mode 100644 index 0000000000..6e7e1562de --- /dev/null +++ b/app/src/main/java/foundation/e/blisslauncher/features/suggestions/SearchSuggestionUtil.kt @@ -0,0 +1,65 @@ +/* + * Copyright © ECORP SAS 2022. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + */ +package foundation.e.blisslauncher.features.suggestions + +import android.content.Context +import android.net.Uri + +class SearchSuggestionUtil { + fun getSuggestionProvider(context: Context): SuggestionProvider { + val defaultSearchEngine = defaultSearchEngine(context) + + return with(defaultSearchEngine) { + when { + contains(Provider.QWANT.key, true) -> QwantProvider() + else -> DuckDuckGoProvider() + } + } + } + + fun getUriForQuery(context: Context, query: String): Uri { + val defaultSearchEngine = defaultSearchEngine(context) + + return with(defaultSearchEngine) { + val url = + when { + contains(Provider.QWANT.key, true) -> "${Provider.QWANT.url}?q=$query" + contains(Provider.DUCKDUCKGO.key, true) -> "${Provider.DUCKDUCKGO.url}?q=$query" + contains(Provider.MOJEEK.key, true) -> "${Provider.MOJEEK.url}search?q=$query" + else -> "${Provider.SPOT.url}?q=$query" + } + + Uri.parse(url) + } + } + + private fun defaultSearchEngine(context: Context): String { + val contentResolver = context.contentResolver + val uri = + Uri.parse("content://foundation.e.browser.provider") + .buildUpon() + .appendPath("search_engine") + .build() + + val cursor = contentResolver.query(uri, null, null, null, null) + cursor.use { + return if (it != null && it.moveToFirst()) { + it.getString(0) + } else { + "" + } + } + } + + private enum class Provider(val key: String, val url: String) { + DUCKDUCKGO("duckduckgo", "https://duckduckgo.com/"), + QWANT("qwant", "https://www.qwant.com/"), + SPOT("spot", "https://spot.murena.io/"), + MOJEEK("mojeek", "https://www.mojeek.com/") + } +} -- GitLab