diff --git a/CHANGELOG.md b/CHANGELOG.md index f1178e7ae..784480fd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ # Changelog for the Mapbox Search SDK for Android -## 1.0.0-beta.34-SNAPSHOT +## 1.0.0-beta.34 + +### Breaking changes +- [CORE] Undocumented system property used to enable SBS API Type is deprecated. Use `MapboxSearchSdk.createSearchEngine(ApiType, SearchEngineSettings)`, and `MapboxSearchSdk.createSearchEngineWithBuiltInDataProviders(ApiType, SearchEngineSettings)` instead. Note that SBS Api Type is still in beta and not available publicly. +- [AUTOFILL] `AddressAutofill.suggestions(String, AddressAutofillOptions)` has been replaced with `AddressAutofill.suggestions(Query, AddressAutofillOptions)`. Call `Query.create()` to create an instance of `Query`. + +### New features +- [CORE] `MapboxSearchSdk.createSearchEngine(ApiType, SearchEngineSettings)`, and `MapboxSearchSdk.createSearchEngineWithBuiltInDataProviders(ApiType, SearchEngineSettings)` functions are available that allow to choose API Type. Note that `ApiType.GEOCODING` is the only API Type available publicly. ### Mapbox dependencies - Search Native SDK `0.57.0` diff --git a/MapboxSearch/autofill/api/api-metalava.txt b/MapboxSearch/autofill/api/api-metalava.txt index f960eec74..f4e5b99af 100644 --- a/MapboxSearch/autofill/api/api-metalava.txt +++ b/MapboxSearch/autofill/api/api-metalava.txt @@ -3,7 +3,7 @@ package com.mapbox.search.autofill { public interface AddressAutofill { method public suspend Object? suggestions(com.mapbox.geojson.Point point, com.mapbox.search.autofill.AddressAutofillOptions options, kotlin.coroutines.Continuation p); - method public suspend Object? suggestions(String query, com.mapbox.search.autofill.AddressAutofillOptions options, kotlin.coroutines.Continuation p); + method public suspend Object? suggestions(com.mapbox.search.autofill.Query query, com.mapbox.search.autofill.AddressAutofillOptions options, kotlin.coroutines.Continuation p); } @kotlinx.parcelize.Parcelize public final class AddressAutofillOptions implements android.os.Parcelable { @@ -78,6 +78,13 @@ package com.mapbox.search.autofill { property public final String? street; } + public final class Query { + method public static com.mapbox.search.autofill.Query? create(String query); + method public String getQuery(); + property public final String query; + field public static final int MIN_QUERY_LENGTH = 3; // 0x3 + } + } package com.mapbox.search.autofill.ktx { diff --git a/MapboxSearch/autofill/api/autofill.api b/MapboxSearch/autofill/api/autofill.api index 3df2257bd..d41bc76f6 100644 --- a/MapboxSearch/autofill/api/autofill.api +++ b/MapboxSearch/autofill/api/autofill.api @@ -1,7 +1,7 @@ public abstract interface class com/mapbox/search/autofill/AddressAutofill { public static final field Companion Lcom/mapbox/search/autofill/AddressAutofill$Companion; public abstract fun suggestions (Lcom/mapbox/geojson/Point;Lcom/mapbox/search/autofill/AddressAutofillOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun suggestions (Ljava/lang/String;Lcom/mapbox/search/autofill/AddressAutofillOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public abstract fun suggestions (Lcom/mapbox/search/autofill/Query;Lcom/mapbox/search/autofill/AddressAutofillOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } public final class com/mapbox/search/autofill/AddressAutofill$Companion { @@ -119,3 +119,17 @@ public final class com/mapbox/search/autofill/BuildConfig { public fun ()V } +public final class com/mapbox/search/autofill/Query { + public static final field Companion Lcom/mapbox/search/autofill/Query$Companion; + public static final field MIN_QUERY_LENGTH I + public static final fun create (Ljava/lang/String;)Lcom/mapbox/search/autofill/Query; + public fun equals (Ljava/lang/Object;)Z + public final fun getQuery ()Ljava/lang/String; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + +public final class com/mapbox/search/autofill/Query$Companion { + public final fun create (Ljava/lang/String;)Lcom/mapbox/search/autofill/Query; +} + diff --git a/MapboxSearch/autofill/src/main/java/com/mapbox/search/autofill/AddressAutofill.kt b/MapboxSearch/autofill/src/main/java/com/mapbox/search/autofill/AddressAutofill.kt index 70820c819..97b8508de 100644 --- a/MapboxSearch/autofill/src/main/java/com/mapbox/search/autofill/AddressAutofill.kt +++ b/MapboxSearch/autofill/src/main/java/com/mapbox/search/autofill/AddressAutofill.kt @@ -1,6 +1,7 @@ package com.mapbox.search.autofill import com.mapbox.geojson.Point +import com.mapbox.search.ApiType import com.mapbox.search.MapboxSearchSdk import com.mapbox.search.SearchEngineSettings @@ -29,7 +30,7 @@ public interface AddressAutofill { * @return Result of the search request represented by [AddressAutofillResponse]. */ public suspend fun suggestions( - query: String, + query: Query, options: AddressAutofillOptions ): AddressAutofillResponse @@ -48,7 +49,7 @@ public interface AddressAutofill { public fun create(accessToken: String): AddressAutofill { val settings = SearchEngineSettings(accessToken) return AddressAutofillImpl( - MapboxSearchSdk.internalCreateAutofillSearchEngine(settings) + MapboxSearchSdk.createSearchEngine(ApiType.AUTOFILL, settings) ) } } diff --git a/MapboxSearch/autofill/src/main/java/com/mapbox/search/autofill/AddressAutofillImpl.kt b/MapboxSearch/autofill/src/main/java/com/mapbox/search/autofill/AddressAutofillImpl.kt index e93af5a15..9e46b3b36 100644 --- a/MapboxSearch/autofill/src/main/java/com/mapbox/search/autofill/AddressAutofillImpl.kt +++ b/MapboxSearch/autofill/src/main/java/com/mapbox/search/autofill/AddressAutofillImpl.kt @@ -40,9 +40,9 @@ internal class AddressAutofillImpl(private val searchEngine: SearchEngine) : Add } } - override suspend fun suggestions(query: String, options: AddressAutofillOptions): AddressAutofillResponse { + override suspend fun suggestions(query: Query, options: AddressAutofillOptions): AddressAutofillResponse { val response = searchEngine.search( - query = query, + query = query.query, options = SearchOptions( countries = options.countries?.map { it.toCoreSdkType() }, languages = options.language?.let { listOf(it.toCoreSdkType()) }, diff --git a/MapboxSearch/autofill/src/main/java/com/mapbox/search/autofill/Query.kt b/MapboxSearch/autofill/src/main/java/com/mapbox/search/autofill/Query.kt new file mode 100644 index 000000000..d95c9608a --- /dev/null +++ b/MapboxSearch/autofill/src/main/java/com/mapbox/search/autofill/Query.kt @@ -0,0 +1,71 @@ +package com.mapbox.search.autofill + +/** + * Text query used for autofill forward geocoding. + */ +public class Query internal constructor( + + /** + * Query text. + */ + public val query: String +) { + + init { + if (query.length < MIN_QUERY_LENGTH) { + throw IllegalArgumentException("Query must be at least $MIN_QUERY_LENGTH characters long") + } + } + + /** + * @suppress + */ + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as Query + + if (query != other.query) return false + + return true + } + + /** + * @suppress + */ + override fun hashCode(): Int { + return query.hashCode() + } + + /** + * @suppress + */ + override fun toString(): String { + return "Query(query='$query')" + } + + /** + * @suppress + */ + public companion object { + + /** + * Minimal allowed query length for address autofill. + */ + public const val MIN_QUERY_LENGTH: Int = 3 + + /** + * Creates [Query] instance. + * @param query Query text. + * @return [Query] instance or `null` if [query]'s length is less than [MIN_QUERY_LENGTH]. + */ + @JvmStatic + public fun create(query: String): Query? { + return when { + query.length >= MIN_QUERY_LENGTH -> Query(query) + else -> null + } + } + } +} diff --git a/MapboxSearch/autofill/src/test/java/com/mapbox/search/autofill/QueryTest.kt b/MapboxSearch/autofill/src/test/java/com/mapbox/search/autofill/QueryTest.kt new file mode 100644 index 000000000..7e64a2ee5 --- /dev/null +++ b/MapboxSearch/autofill/src/test/java/com/mapbox/search/autofill/QueryTest.kt @@ -0,0 +1,58 @@ +package com.mapbox.search.autofill + +import com.mapbox.search.common.tests.ToStringVerifier +import com.mapbox.test.dsl.TestCase +import nl.jqno.equalsverifier.EqualsVerifier +import org.junit.jupiter.api.TestFactory + +internal class QueryTest { + + @TestFactory + fun `Check Query's min length`() = TestCase { + Given("${Query::class.java.simpleName} class") { + When("MIN_QUERY_LENGTH accessed") { + Then("MIN_QUERY_LENGTH should be 3", 3, Query.MIN_QUERY_LENGTH) + } + } + } + + @TestFactory + fun `Check Query's create() function`() = TestCase { + Given("${Query::class.java.simpleName} class") { + (0..Query.MIN_QUERY_LENGTH) + .map { length -> "a".repeat(length) } + .forEach { query -> + When("create($query) called") { + val value = if (query.length < 3) { + null + } else { + Query(query) + } + Then("Created value should be $value", value, Query.create(query)) + } + } + } + } + + @TestFactory + fun `Check Query's equals() and hashCode()`() = TestCase { + Given("${Query::class.java.simpleName} class") { + When("Call equals() and hashCode()") { + Then("equals() and hashCode() should be implemented correctly") { + EqualsVerifier.forClass(Query::class.java).verify() + } + } + } + } + + @TestFactory + fun `Check Query's toString()`() = TestCase { + Given("${Query::class.java.simpleName} class") { + When("Call toString()") { + Then("toString() function should be implemented correctly") { + ToStringVerifier(clazz = Query::class).verify() + } + } + } + } +} diff --git a/MapboxSearch/gradle.properties b/MapboxSearch/gradle.properties index ca81ce077..009893773 100644 --- a/MapboxSearch/gradle.properties +++ b/MapboxSearch/gradle.properties @@ -21,7 +21,7 @@ android.enableJetifier=false kotlin.code.style=official # SDK version attributes -VERSION_NAME=1.0.0-beta.34-SNAPSHOT +VERSION_NAME=1.0.0-beta.34 # Artifact attributes mapboxArtifactUserOrg=mapbox diff --git a/MapboxSearch/sample/src/androidTest/java/com/mapbox/search/sample/ExampleActivitiesTest.kt b/MapboxSearch/sample/src/androidTest/java/com/mapbox/search/sample/ExampleActivitiesTest.kt index 64d069d82..d0901ca41 100644 --- a/MapboxSearch/sample/src/androidTest/java/com/mapbox/search/sample/ExampleActivitiesTest.kt +++ b/MapboxSearch/sample/src/androidTest/java/com/mapbox/search/sample/ExampleActivitiesTest.kt @@ -17,6 +17,8 @@ import com.mapbox.search.sample.api.ForwardGeocodingJavaExampleActivity import com.mapbox.search.sample.api.ForwardGeocodingKotlinExampleActivity import com.mapbox.search.sample.api.HistoryDataProviderJavaExample import com.mapbox.search.sample.api.HistoryDataProviderKotlinExample +import com.mapbox.search.sample.api.JapanSearchJavaExampleActivity +import com.mapbox.search.sample.api.JapanSearchKotlinExampleActivity import com.mapbox.search.sample.api.OfflineReverseGeocodingJavaExampleActivity import com.mapbox.search.sample.api.OfflineReverseGeocodingKotlinExampleActivity import com.mapbox.search.sample.api.OfflineSearchJavaExampleActivity @@ -78,6 +80,9 @@ class ExampleActivitiesTest(private val clazz: Class) { OfflineReverseGeocodingKotlinExampleActivity::class.java, OfflineReverseGeocodingJavaExampleActivity::class.java, + JapanSearchKotlinExampleActivity::class.java, + JapanSearchJavaExampleActivity::class.java, + HistoryDataProviderKotlinExample::class.java, HistoryDataProviderJavaExample::class.java, diff --git a/MapboxSearch/sample/src/main/AndroidManifest.xml b/MapboxSearch/sample/src/main/AndroidManifest.xml index 55326b8cc..ca88c30e1 100644 --- a/MapboxSearch/sample/src/main/AndroidManifest.xml +++ b/MapboxSearch/sample/src/main/AndroidManifest.xml @@ -56,6 +56,8 @@ + + diff --git a/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/MainActivity.kt b/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/MainActivity.kt index 8b376994f..182b0271f 100644 --- a/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/MainActivity.kt +++ b/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/MainActivity.kt @@ -42,6 +42,7 @@ import com.mapbox.maps.extension.style.sources.generated.GeoJsonSource import com.mapbox.maps.extension.style.sources.generated.geoJsonSource import com.mapbox.maps.extension.style.sources.getSourceAs import com.mapbox.maps.extension.style.style +import com.mapbox.search.ApiType import com.mapbox.search.MapboxSearchSdk import com.mapbox.search.OfflineSearchEngineSettings import com.mapbox.search.ResponseInfo @@ -62,6 +63,8 @@ import com.mapbox.search.sample.api.ForwardGeocodingJavaExampleActivity import com.mapbox.search.sample.api.ForwardGeocodingKotlinExampleActivity import com.mapbox.search.sample.api.HistoryDataProviderJavaExample import com.mapbox.search.sample.api.HistoryDataProviderKotlinExample +import com.mapbox.search.sample.api.JapanSearchJavaExampleActivity +import com.mapbox.search.sample.api.JapanSearchKotlinExampleActivity import com.mapbox.search.sample.api.OfflineReverseGeocodingJavaExampleActivity import com.mapbox.search.sample.api.OfflineReverseGeocodingKotlinExampleActivity import com.mapbox.search.sample.api.OfflineSearchJavaExampleActivity @@ -127,11 +130,18 @@ class MainActivity : AppCompatActivity() { } searchResultsView = findViewById(R.id.search_results_view).apply { + val apiType = if (BuildConfig.ENABLE_SBS) { + ApiType.SBS + } else { + ApiType.GEOCODING + } + initialize( SearchResultsView.Configuration( commonConfiguration = CommonSearchViewConfiguration(DistanceUnitType.IMPERIAL), searchEngineSettings = SearchEngineSettings(BuildConfig.MAPBOX_API_TOKEN), - offlineSearchEngineSettings = OfflineSearchEngineSettings(BuildConfig.MAPBOX_API_TOKEN) + offlineSearchEngineSettings = OfflineSearchEngineSettings(BuildConfig.MAPBOX_API_TOKEN), + apiType = apiType, ) ) isVisible = false @@ -320,6 +330,14 @@ class MainActivity : AppCompatActivity() { startActivity(Intent(this, ReverseGeocodingJavaExampleActivity::class.java)) true } + R.id.open_japan_search_kt_example -> { + startActivity(Intent(this, JapanSearchKotlinExampleActivity::class.java)) + true + } + R.id.open_japan_search_java_example -> { + startActivity(Intent(this, JapanSearchJavaExampleActivity::class.java)) + true + } R.id.open_category_search_kt_example -> { startActivity(Intent(this, CategorySearchKotlinExampleActivity::class.java)) true diff --git a/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/SampleApplication.kt b/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/SampleApplication.kt index 7cda175fd..896727d87 100644 --- a/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/SampleApplication.kt +++ b/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/SampleApplication.kt @@ -17,7 +17,6 @@ open class SampleApplication : Application() { override fun onCreate() { super.onCreate() - System.setProperty("com.mapbox.mapboxsearch.enableSBS", BuildConfig.ENABLE_SBS.toString()) enableDebugHttpLogs() enableStrictMode() LeakCanaryConfiguration.apply() diff --git a/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/api/AddressAutofillKotlinExampleActivity.kt b/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/api/AddressAutofillKotlinExampleActivity.kt index 2cb4f4b1c..9d8841ed6 100644 --- a/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/api/AddressAutofillKotlinExampleActivity.kt +++ b/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/api/AddressAutofillKotlinExampleActivity.kt @@ -7,6 +7,7 @@ import androidx.lifecycle.lifecycleScope import com.mapbox.search.autofill.AddressAutofill import com.mapbox.search.autofill.AddressAutofillOptions import com.mapbox.search.autofill.AddressAutofillResponse +import com.mapbox.search.autofill.Query import com.mapbox.search.sample.BuildConfig class AddressAutofillKotlinExampleActivity : AppCompatActivity() { @@ -21,8 +22,10 @@ class AddressAutofillKotlinExampleActivity : AppCompatActivity() { ) lifecycleScope.launchWhenCreated { + val query = Query.create("740 15th St NW, Washington") ?: return@launchWhenCreated + val response = addressAutofill.suggestions( - query = "740 15th St NW, Washington", + query = query, options = AddressAutofillOptions() ) diff --git a/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/api/JapanSearchJavaExampleActivity.java b/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/api/JapanSearchJavaExampleActivity.java new file mode 100644 index 000000000..2be62c5c2 --- /dev/null +++ b/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/api/JapanSearchJavaExampleActivity.java @@ -0,0 +1,81 @@ +package com.mapbox.search.sample.api; + +import android.os.Bundle; +import android.util.Log; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; + +import com.mapbox.search.ApiType; +import com.mapbox.search.Country; +import com.mapbox.search.Language; +import com.mapbox.search.MapboxSearchSdk; +import com.mapbox.search.ResponseInfo; +import com.mapbox.search.SearchEngine; +import com.mapbox.search.SearchEngineSettings; +import com.mapbox.search.SearchOptions; +import com.mapbox.search.SearchRequestTask; +import com.mapbox.search.SearchSelectionCallback; +import com.mapbox.search.result.SearchResult; +import com.mapbox.search.result.SearchSuggestion; +import com.mapbox.search.sample.BuildConfig; + +import java.util.List; + +public class JapanSearchJavaExampleActivity extends AppCompatActivity { + + private SearchEngine searchEngine; + private SearchRequestTask searchRequestTask; + + private final SearchSelectionCallback searchCallback = new SearchSelectionCallback() { + + @Override + public void onSuggestions(@NonNull List suggestions, @NonNull ResponseInfo responseInfo) { + if (suggestions.isEmpty()) { + Log.i("SearchApiExample", "No suggestions found"); + } else { + Log.i("SearchApiExample", "Search suggestions: " + suggestions + "\nSelecting first..."); + searchRequestTask = searchEngine.select(suggestions.get(0), this); + } + } + + @Override + public void onResult(@NonNull SearchSuggestion suggestion, @NonNull SearchResult result, @NonNull ResponseInfo info) { + Log.i("SearchApiExample", "Search result: " + result); + } + + @Override + public void onCategoryResult(@NonNull SearchSuggestion suggestion, @NonNull List results, @NonNull ResponseInfo responseInfo) { + Log.i("SearchApiExample", "Category search results: " + results); + } + + @Override + public void onError(@NonNull Exception e) { + Log.i("SearchApiExample", "Search error: ", e); + } + }; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + searchEngine = MapboxSearchSdk.createSearchEngineWithBuiltInDataProviders( + ApiType.SBS, + new SearchEngineSettings(BuildConfig.MAPBOX_API_TOKEN) + ); + + final SearchOptions options = new SearchOptions.Builder() + .countries(Country.JAPAN) + .languages(Language.JAPANESE) + .build(); + + searchRequestTask = searchEngine.search("東京", options, searchCallback); + } + + @Override + protected void onDestroy() { + searchRequestTask.cancel(); + super.onDestroy(); + } +} diff --git a/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/api/JapanSearchKotlinExampleActivity.kt b/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/api/JapanSearchKotlinExampleActivity.kt new file mode 100644 index 000000000..20064511b --- /dev/null +++ b/MapboxSearch/sample/src/main/java/com/mapbox/search/sample/api/JapanSearchKotlinExampleActivity.kt @@ -0,0 +1,79 @@ +package com.mapbox.search.sample.api + +import android.app.Activity +import android.os.Bundle +import android.util.Log +import com.mapbox.search.ApiType +import com.mapbox.search.Country +import com.mapbox.search.Language +import com.mapbox.search.MapboxSearchSdk +import com.mapbox.search.ResponseInfo +import com.mapbox.search.SearchEngine +import com.mapbox.search.SearchEngineSettings +import com.mapbox.search.SearchOptions +import com.mapbox.search.SearchRequestTask +import com.mapbox.search.SearchSelectionCallback +import com.mapbox.search.result.SearchResult +import com.mapbox.search.result.SearchSuggestion +import com.mapbox.search.sample.BuildConfig + +class JapanSearchKotlinExampleActivity : Activity() { + + private lateinit var searchEngine: SearchEngine + private lateinit var searchRequestTask: SearchRequestTask + + private val searchCallback = object : SearchSelectionCallback { + + override fun onSuggestions(suggestions: List, responseInfo: ResponseInfo) { + if (suggestions.isEmpty()) { + Log.i("SearchApiExample", "No suggestions found") + } else { + Log.i("SearchApiExample", "Search suggestions: $suggestions.\nSelecting first suggestion...") + searchRequestTask = searchEngine.select(suggestions.first(), this) + } + } + + override fun onResult( + suggestion: SearchSuggestion, + result: SearchResult, + responseInfo: ResponseInfo + ) { + Log.i("SearchApiExample", "Search result: $result") + } + + override fun onCategoryResult( + suggestion: SearchSuggestion, + results: List, + responseInfo: ResponseInfo + ) { + Log.i("SearchApiExample", "Category search results: $results") + } + + override fun onError(e: Exception) { + Log.i("SearchApiExample", "Search error", e) + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + searchEngine = MapboxSearchSdk.createSearchEngineWithBuiltInDataProviders( + ApiType.SBS, + SearchEngineSettings(BuildConfig.MAPBOX_API_TOKEN) + ) + + searchRequestTask = searchEngine.search( + "東京", + SearchOptions( + countries = listOf(Country.JAPAN), + languages = listOf(Language.JAPANESE), + ), + searchCallback + ) + } + + override fun onDestroy() { + searchRequestTask.cancel() + super.onDestroy() + } +} diff --git a/MapboxSearch/sample/src/main/res/menu/main_activity_options_menu.xml b/MapboxSearch/sample/src/main/res/menu/main_activity_options_menu.xml index 3a6dff3d2..47e6076be 100644 --- a/MapboxSearch/sample/src/main/res/menu/main_activity_options_menu.xml +++ b/MapboxSearch/sample/src/main/res/menu/main_activity_options_menu.xml @@ -93,6 +93,16 @@ android:title="@string/action_open_offline_reverse_geocoding_kt_example" /> + + + + Open offline search Java example Open offline reverse geocoding Kotlin example Open offline reverse geocoding Java example + Open Japan Search Kotlin example + Open Japan Search Java example Open History Data Provider Kotlin example Open History Data Provider Java example Open Favorites Data Provider Kotlin example diff --git a/MapboxSearch/sdk/api/api-metalava.txt b/MapboxSearch/sdk/api/api-metalava.txt index 39ce96f79..32a69168c 100644 --- a/MapboxSearch/sdk/api/api-metalava.txt +++ b/MapboxSearch/sdk/api/api-metalava.txt @@ -423,11 +423,14 @@ package com.mapbox.search { public final class MapboxSearchSdk { method public static com.mapbox.search.OfflineSearchEngine createOfflineSearchEngine(com.mapbox.search.OfflineSearchEngineSettings settings); method public static com.mapbox.search.SearchEngine createSearchEngine(com.mapbox.search.SearchEngineSettings settings); + method public static com.mapbox.search.SearchEngine createSearchEngine(com.mapbox.search.ApiType apiType, com.mapbox.search.SearchEngineSettings settings); method public static com.mapbox.search.SearchEngine createSearchEngineWithBuiltInDataProviders(com.mapbox.search.SearchEngineSettings settings, java.util.concurrent.Executor executor = SearchSdkMainThreadWorker.mainExecutor, com.mapbox.search.CompletionCallback callback = com.mapbox.search.StubCompletionCallback()); method public static com.mapbox.search.SearchEngine createSearchEngineWithBuiltInDataProviders(com.mapbox.search.SearchEngineSettings settings, java.util.concurrent.Executor executor = SearchSdkMainThreadWorker.mainExecutor); method public static com.mapbox.search.SearchEngine createSearchEngineWithBuiltInDataProviders(com.mapbox.search.SearchEngineSettings settings); + method public static com.mapbox.search.SearchEngine createSearchEngineWithBuiltInDataProviders(com.mapbox.search.ApiType apiType, com.mapbox.search.SearchEngineSettings settings, java.util.concurrent.Executor executor = SearchSdkMainThreadWorker.mainExecutor, com.mapbox.search.CompletionCallback callback = com.mapbox.search.StubCompletionCallback()); + method public static com.mapbox.search.SearchEngine createSearchEngineWithBuiltInDataProviders(com.mapbox.search.ApiType apiType, com.mapbox.search.SearchEngineSettings settings, java.util.concurrent.Executor executor = SearchSdkMainThreadWorker.mainExecutor); + method public static com.mapbox.search.SearchEngine createSearchEngineWithBuiltInDataProviders(com.mapbox.search.ApiType apiType, com.mapbox.search.SearchEngineSettings settings); method public static com.mapbox.search.ServiceProvider getServiceProvider(); - method public static com.mapbox.search.SearchEngine internalCreateAutofillSearchEngine(com.mapbox.search.SearchEngineSettings settings); field public static final com.mapbox.search.MapboxSearchSdk INSTANCE; } diff --git a/MapboxSearch/sdk/api/sdk.api b/MapboxSearch/sdk/api/sdk.api index 290574264..97fb2d8b0 100644 --- a/MapboxSearch/sdk/api/sdk.api +++ b/MapboxSearch/sdk/api/sdk.api @@ -447,13 +447,17 @@ public final class com/mapbox/search/Language$Companion { public final class com/mapbox/search/MapboxSearchSdk { public static final field INSTANCE Lcom/mapbox/search/MapboxSearchSdk; public static final fun createOfflineSearchEngine (Lcom/mapbox/search/OfflineSearchEngineSettings;)Lcom/mapbox/search/OfflineSearchEngine; + public static final fun createSearchEngine (Lcom/mapbox/search/ApiType;Lcom/mapbox/search/SearchEngineSettings;)Lcom/mapbox/search/SearchEngine; public static final fun createSearchEngine (Lcom/mapbox/search/SearchEngineSettings;)Lcom/mapbox/search/SearchEngine; + public static final fun createSearchEngineWithBuiltInDataProviders (Lcom/mapbox/search/ApiType;Lcom/mapbox/search/SearchEngineSettings;)Lcom/mapbox/search/SearchEngine; + public static final fun createSearchEngineWithBuiltInDataProviders (Lcom/mapbox/search/ApiType;Lcom/mapbox/search/SearchEngineSettings;Ljava/util/concurrent/Executor;)Lcom/mapbox/search/SearchEngine; + public static final fun createSearchEngineWithBuiltInDataProviders (Lcom/mapbox/search/ApiType;Lcom/mapbox/search/SearchEngineSettings;Ljava/util/concurrent/Executor;Lcom/mapbox/search/CompletionCallback;)Lcom/mapbox/search/SearchEngine; public static final fun createSearchEngineWithBuiltInDataProviders (Lcom/mapbox/search/SearchEngineSettings;)Lcom/mapbox/search/SearchEngine; public static final fun createSearchEngineWithBuiltInDataProviders (Lcom/mapbox/search/SearchEngineSettings;Ljava/util/concurrent/Executor;)Lcom/mapbox/search/SearchEngine; public static final fun createSearchEngineWithBuiltInDataProviders (Lcom/mapbox/search/SearchEngineSettings;Ljava/util/concurrent/Executor;Lcom/mapbox/search/CompletionCallback;)Lcom/mapbox/search/SearchEngine; + public static synthetic fun createSearchEngineWithBuiltInDataProviders$default (Lcom/mapbox/search/ApiType;Lcom/mapbox/search/SearchEngineSettings;Ljava/util/concurrent/Executor;Lcom/mapbox/search/CompletionCallback;ILjava/lang/Object;)Lcom/mapbox/search/SearchEngine; public static synthetic fun createSearchEngineWithBuiltInDataProviders$default (Lcom/mapbox/search/SearchEngineSettings;Ljava/util/concurrent/Executor;Lcom/mapbox/search/CompletionCallback;ILjava/lang/Object;)Lcom/mapbox/search/SearchEngine; public static final fun getServiceProvider ()Lcom/mapbox/search/ServiceProvider; - public static final fun internalCreateAutofillSearchEngine (Lcom/mapbox/search/SearchEngineSettings;)Lcom/mapbox/search/SearchEngine; } public final class com/mapbox/search/OfflineIndexChangeEvent { diff --git a/MapboxSearch/sdk/src/main/java/com/mapbox/search/MapboxSearchSdk.kt b/MapboxSearch/sdk/src/main/java/com/mapbox/search/MapboxSearchSdk.kt index c85e4c988..5a3dae0bc 100755 --- a/MapboxSearch/sdk/src/main/java/com/mapbox/search/MapboxSearchSdk.kt +++ b/MapboxSearch/sdk/src/main/java/com/mapbox/search/MapboxSearchSdk.kt @@ -13,6 +13,7 @@ import com.mapbox.search.analytics.AnalyticsServiceImpl import com.mapbox.search.analytics.SearchFeedbackEventsFactory import com.mapbox.search.common.BuildConfig import com.mapbox.search.common.concurrent.CommonMainThreadChecker +import com.mapbox.search.common.logger.logw import com.mapbox.search.core.CoreEngineOptions import com.mapbox.search.core.CoreSearchEngine import com.mapbox.search.core.CoreSearchEngineInterface @@ -203,7 +204,7 @@ public object MapboxSearchSdk { } /** - * Creates a new instance of the [SearchEngine]. + * Creates a new instance of the [SearchEngine] with a default [ApiType]. * A new instance doesn't have any [IndexableDataProvider] registered by default. * * @param settings [SearchEngine] settings. @@ -217,17 +218,27 @@ public object MapboxSearchSdk { } /** - * Internal function that must not be used by external customers. WIll be removed in the next Search SDK updates. - * TODO https://github.com/mapbox/mapbox-search-android-internal/issues/799 + * Experimental API, can be changed or removed in the next SDK releases. + * + * Creates a new instance of the [SearchEngine]. + * A new instance doesn't have any [IndexableDataProvider] registered by default. + * + * @param settings [SearchEngine] settings. + * @param apiType The type of the API used by the Search Engines. + * Note that [ApiType.GEOCODING] is the only available publicly. + * You might need to [contact sales](https://www.mapbox.com/contact/sales/) to enable access for other API types. + * + * @return a new instance instance of [SearchEngine]. + * @see createSearchEngineWithBuiltInDataProviders */ @JvmStatic - public fun internalCreateAutofillSearchEngine(settings: SearchEngineSettings): SearchEngine { - return createSearchEngine(ApiType.AUTOFILL, settings) + public fun createSearchEngine(apiType: ApiType, settings: SearchEngineSettings): SearchEngine { + return internalCreateSearchEngine(apiType, settings) } /** - * Creates a new instance of the [SearchEngine] with - * [com.mapbox.search.record.HistoryDataProvider] and [com.mapbox.search.record.FavoritesDataProvider] + * Creates a new instance of the [SearchEngine] with a default [ApiType] and default data providers ( + * [com.mapbox.search.record.HistoryDataProvider] and [com.mapbox.search.record.FavoritesDataProvider]) * registered by default. * * @param settings [SearchEngine] settings. @@ -244,24 +255,41 @@ public object MapboxSearchSdk { executor: Executor = SearchSdkMainThreadWorker.mainExecutor, callback: CompletionCallback = StubCompletionCallback() ): SearchEngine { - return createSearchEngineWithBuiltInDataProviders(getDefaultApiType(), settings, null, executor, callback) + return createSearchEngineWithBuiltInDataProviders(getDefaultApiType(), settings, executor, callback) } - @JvmSynthetic - internal fun createSearchEngineWithBuiltInDataProviders( + /** + * Experimental API, can be changed or removed in the next SDK releases. + * + * Creates a new instance of the [SearchEngine] with default data providers ( + * [com.mapbox.search.record.HistoryDataProvider] and [com.mapbox.search.record.FavoritesDataProvider]) + * registered by default. + * + * @param settings [SearchEngine] settings. + * @param apiType The type of the API used by the Search Engines. By default [ApiType.GEOCODING] will be used. + * Note that [ApiType.GEOCODING] is the only available publicly. + * You might need to [contact sales](https://www.mapbox.com/contact/sales/) to enable access for other API types. + * @param executor Executor used for events dispatching. By default events are dispatched on the main thread. + * @param callback Callback to handle result. + * + * @return a new instance of [SearchEngine]. + * @see createSearchEngine + */ + @JvmOverloads + @JvmStatic + public fun createSearchEngineWithBuiltInDataProviders( apiType: ApiType, settings: SearchEngineSettings, - analyticsService: AnalyticsService? = null, executor: Executor = SearchSdkMainThreadWorker.mainExecutor, callback: CompletionCallback = StubCompletionCallback() ): SearchEngine { val coreEngine = createCoreEngineByApiType(apiType, settings) - val searchEngine = createSearchEngine( + val searchEngine = internalCreateSearchEngine( apiType, settings, coreEngine, - analyticsService ?: createAnalyticsService(settings, coreEngine) + createAnalyticsService(settings, coreEngine) ) val compoundCallback = CompoundCompletionCallback(2, executor, callback) @@ -276,7 +304,7 @@ public object MapboxSearchSdk { } @JvmSynthetic - internal fun createSearchEngine( + internal fun internalCreateSearchEngine( apiType: ApiType, settings: SearchEngineSettings, coreEngine: CoreSearchEngineInterface = createCoreEngineByApiType(apiType, settings), @@ -366,6 +394,7 @@ public object MapboxSearchSdk { private fun getDefaultApiType(): ApiType { return if (System.getProperty("com.mapbox.mapboxsearch.enableSBS")?.toBoolean() == true) { + logw("\"com.mapbox.mapboxsearch.enableSBS\" flag is DEPRECATED. Specify ApiType explicitly") ApiType.SBS } else { ApiType.GEOCODING diff --git a/MapboxSearch/sdk/src/sharedTest/java/com/mapbox/search/tests_support/SearchEngine.kt b/MapboxSearch/sdk/src/sharedTest/java/com/mapbox/search/tests_support/SearchEngine.kt index 3485fefe2..319a2459d 100644 --- a/MapboxSearch/sdk/src/sharedTest/java/com/mapbox/search/tests_support/SearchEngine.kt +++ b/MapboxSearch/sdk/src/sharedTest/java/com/mapbox/search/tests_support/SearchEngine.kt @@ -6,7 +6,6 @@ import com.mapbox.search.SearchEngine import com.mapbox.search.SearchEngineSettings import com.mapbox.search.SearchOptions import com.mapbox.search.SelectOptions -import com.mapbox.search.analytics.AnalyticsService import com.mapbox.search.record.IndexableDataProvider import com.mapbox.search.record.IndexableRecord import com.mapbox.search.result.SearchSuggestion @@ -54,11 +53,10 @@ internal fun SearchEngine.unregisterDataProviderBlocking( internal fun MapboxSearchSdk.createSearchEngineWithBuiltInDataProvidersBlocking( apiType: ApiType, settings: SearchEngineSettings, - analyticsService: AnalyticsService? = null, executor: Executor = SearchSdkMainThreadWorker.mainExecutor, ): SearchEngine { val callback = BlockingCompletionCallback() - val searchEngine = createSearchEngineWithBuiltInDataProviders(apiType, settings, analyticsService, executor, callback) + val searchEngine = createSearchEngineWithBuiltInDataProviders(apiType, settings, executor, callback) val result = callback.getResultBlocking() require(result.isResult) return searchEngine diff --git a/MapboxSearch/ui/api/api-metalava.txt b/MapboxSearch/ui/api/api-metalava.txt index 24b885a48..6e1a4dc74 100644 --- a/MapboxSearch/ui/api/api-metalava.txt +++ b/MapboxSearch/ui/api/api-metalava.txt @@ -73,10 +73,13 @@ package com.mapbox.search.ui.view { } public static final class SearchResultsView.Configuration { + ctor public SearchResultsView.Configuration(com.mapbox.search.ui.view.CommonSearchViewConfiguration commonConfiguration, com.mapbox.search.SearchEngineSettings searchEngineSettings, com.mapbox.search.OfflineSearchEngineSettings offlineSearchEngineSettings, com.mapbox.search.ApiType apiType = ApiType.GEOCODING); ctor public SearchResultsView.Configuration(com.mapbox.search.ui.view.CommonSearchViewConfiguration commonConfiguration, com.mapbox.search.SearchEngineSettings searchEngineSettings, com.mapbox.search.OfflineSearchEngineSettings offlineSearchEngineSettings); + method public com.mapbox.search.ApiType getApiType(); method public com.mapbox.search.ui.view.CommonSearchViewConfiguration getCommonConfiguration(); method public com.mapbox.search.OfflineSearchEngineSettings getOfflineSearchEngineSettings(); method public com.mapbox.search.SearchEngineSettings getSearchEngineSettings(); + property public final com.mapbox.search.ApiType apiType; property public final com.mapbox.search.ui.view.CommonSearchViewConfiguration commonConfiguration; property public final com.mapbox.search.OfflineSearchEngineSettings offlineSearchEngineSettings; property public final com.mapbox.search.SearchEngineSettings searchEngineSettings; diff --git a/MapboxSearch/ui/api/ui.api b/MapboxSearch/ui/api/ui.api index f0621b820..6f96a3204 100644 --- a/MapboxSearch/ui/api/ui.api +++ b/MapboxSearch/ui/api/ui.api @@ -53,6 +53,9 @@ public final class com/mapbox/search/ui/view/SearchResultsView : androidx/recycl public final class com/mapbox/search/ui/view/SearchResultsView$Configuration { public fun (Lcom/mapbox/search/ui/view/CommonSearchViewConfiguration;Lcom/mapbox/search/SearchEngineSettings;Lcom/mapbox/search/OfflineSearchEngineSettings;)V + public fun (Lcom/mapbox/search/ui/view/CommonSearchViewConfiguration;Lcom/mapbox/search/SearchEngineSettings;Lcom/mapbox/search/OfflineSearchEngineSettings;Lcom/mapbox/search/ApiType;)V + public synthetic fun (Lcom/mapbox/search/ui/view/CommonSearchViewConfiguration;Lcom/mapbox/search/SearchEngineSettings;Lcom/mapbox/search/OfflineSearchEngineSettings;Lcom/mapbox/search/ApiType;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public final fun getApiType ()Lcom/mapbox/search/ApiType; public final fun getCommonConfiguration ()Lcom/mapbox/search/ui/view/CommonSearchViewConfiguration; public final fun getOfflineSearchEngineSettings ()Lcom/mapbox/search/OfflineSearchEngineSettings; public final fun getSearchEngineSettings ()Lcom/mapbox/search/SearchEngineSettings; diff --git a/MapboxSearch/ui/src/androidTest/java/com/mapbox/search/ui/TestActivity.kt b/MapboxSearch/ui/src/androidTest/java/com/mapbox/search/ui/TestActivity.kt index bc757ea6e..e0de86886 100644 --- a/MapboxSearch/ui/src/androidTest/java/com/mapbox/search/ui/TestActivity.kt +++ b/MapboxSearch/ui/src/androidTest/java/com/mapbox/search/ui/TestActivity.kt @@ -26,6 +26,7 @@ import com.mapbox.android.core.location.LocationEngineRequest import com.mapbox.android.core.location.LocationEngineResult import com.mapbox.android.core.permissions.PermissionsManager import com.mapbox.geojson.Point +import com.mapbox.search.ApiType import com.mapbox.search.MapboxSearchSdk import com.mapbox.search.OfflineSearchEngineSettings import com.mapbox.search.ResponseInfo @@ -67,8 +68,6 @@ public class TestActivity : AppCompatActivity() { locationEngine = FixedPointLocationEngine(Point.fromLngLat(-122.084000, 37.421998)) - System.setProperty("com.mapbox.mapboxsearch.enableSBS", true.toString()) - // Ensure distance formatting uses miles (not meters). if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { val localeList = LocaleList(Locale.US, Locale.ENGLISH) @@ -89,7 +88,8 @@ public class TestActivity : AppCompatActivity() { offlineSearchEngineSettings = OfflineSearchEngineSettings( accessToken = BuildConfig.MAPBOX_API_TOKEN, locationEngine = locationEngine, - ) + ), + apiType = ApiType.SBS, ) ) isVisible = false diff --git a/MapboxSearch/ui/src/main/java/com/mapbox/search/ui/view/SearchResultsView.kt b/MapboxSearch/ui/src/main/java/com/mapbox/search/ui/view/SearchResultsView.kt index cd5d41dcd..63b87ea0c 100644 --- a/MapboxSearch/ui/src/main/java/com/mapbox/search/ui/view/SearchResultsView.kt +++ b/MapboxSearch/ui/src/main/java/com/mapbox/search/ui/view/SearchResultsView.kt @@ -10,6 +10,7 @@ import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.SimpleItemAnimator import com.mapbox.common.ReachabilityFactory import com.mapbox.common.ReachabilityInterface +import com.mapbox.search.ApiType import com.mapbox.search.CompletionCallback import com.mapbox.search.MapboxSearchSdk import com.mapbox.search.OfflineSearchEngine @@ -217,7 +218,10 @@ public class SearchResultsView @JvmOverloads constructor( super.setLayoutManager(LinearLayoutManager(context)) super.setAdapter(searchAdapter) - searchEngine = MapboxSearchSdk.createSearchEngineWithBuiltInDataProviders(configuration.searchEngineSettings) + searchEngine = MapboxSearchSdk.createSearchEngineWithBuiltInDataProviders( + configuration.apiType, + configuration.searchEngineSettings + ) offlineSearchEngine = MapboxSearchSdk.createOfflineSearchEngine(configuration.offlineSearchEngineSettings) @@ -668,9 +672,9 @@ public class SearchResultsView @JvmOverloads constructor( } /** - * Configuration option used for [SearchResultsView] configeration. + * Options used for [SearchResultsView] configuration. */ - public class Configuration( + public class Configuration @JvmOverloads public constructor( /** * Common configuration options used for Search SDK views. */ @@ -685,6 +689,15 @@ public class SearchResultsView @JvmOverloads constructor( * Settings used for [OfflineSearchEngine] configuration. */ public val offlineSearchEngineSettings: OfflineSearchEngineSettings, + + /** + * Experimental API, can be changed or removed in the next SDK releases. + * + * The type of the API used by the Search Engines. + * Note that [ApiType.GEOCODING] (default) is the only available publicly. + * You might need to [contact sales](https://www.mapbox.com/contact/sales/) to enable access for other API types. + */ + public val apiType: ApiType = ApiType.GEOCODING, ) private companion object {