diff --git a/README.md b/README.md index 2d3e12b..dec5a8f 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Developers often come across a requirement of adding precise location. So, a pla ![image](/media/vanillaplacepicker-autocomplete.gif) ![image](/media/vanillaplacepicker-map.gif) ## Key features -* Android 12 support +* Android 13 support * Simple implementation for place picker either using Autocomplete, Map or both * Set your own custom map styles * Customise map pin icon diff --git a/app/build.gradle b/app/build.gradle index ff72187..56b6bdb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,15 +1,15 @@ plugins { id 'com.android.application' id 'kotlin-android' - id 'kotlin-android-extensions' } android { - compileSdk 32 + namespace 'com.mindinventory.placepicker' + compileSdk 34 defaultConfig { applicationId "com.mindinventory.placepicker" minSdk 21 - targetSdk 32 + targetSdk 34 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -32,11 +32,15 @@ android { buildFeatures { viewBinding true } + compileOptions { + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 + } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation "androidx.appcompat:appcompat:1.5.0" + implementation "androidx.appcompat:appcompat:1.6.1" implementation "androidx.cardview:cardview:1.0.0" testImplementation 'junit:junit:4.13.2' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 545d9e0..c74003b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,8 +1,6 @@ - - - + + @@ -29,6 +27,9 @@ + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 8dcd51a..d958e71 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,10 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { + ext { + agp_version = '8.2.0' + kotlin_version = '1.8.10' + } repositories { google() @@ -8,9 +12,9 @@ buildscript { maven { url "https://jitpack.io" } } dependencies { - classpath 'com.android.tools.build:gradle:7.1.3' - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10" - classpath "com.google.gms:google-services:4.3.13" + classpath "com.android.tools.build:gradle:$agp_version" + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath "com.google.gms:google-services:4.4.1" classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' // NOTE: Do not place your application dependencies here; they belong @@ -26,6 +30,6 @@ allprojects { } } -task clean(type: Delete) { +tasks.register('clean', Delete) { delete rootProject.buildDir } diff --git a/gradle.properties b/gradle.properties index 23339e0..952185b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -19,3 +19,4 @@ android.useAndroidX=true android.enableJetifier=true # Kotlin code style for this project: "official" or "obsolete": kotlin.code.style=official +android.defaults.buildfeatures.buildconfig=true diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 7c1f8d2..0694fc6 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Tue Mar 01 18:23:58 IST 2022 +#Fri Mar 01 12:21:32 IST 2024 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip distributionPath=wrapper/dists -zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists \ No newline at end of file diff --git a/vanillaplacepicker/build.gradle b/vanillaplacepicker/build.gradle index 5257a2b..0badf58 100644 --- a/vanillaplacepicker/build.gradle +++ b/vanillaplacepicker/build.gradle @@ -7,13 +7,12 @@ plugins { group='com.github.Mindinventory' android { - compileSdk 32 + namespace 'com.vanillaplacepicker' + compileSdk 34 defaultConfig { minSdk 21 - targetSdk 32 - versionCode 15 - versionName "0.3.0" + targetSdk 34 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true } @@ -26,12 +25,8 @@ android { } compileOptions { - sourceCompatibility = 1.8 - targetCompatibility = 1.8 - } - - kotlinOptions { - jvmTarget = "1.8" + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 } testOptions { @@ -45,26 +40,28 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'androidx.appcompat:appcompat:1.5.0' - implementation 'androidx.core:core-ktx:1.8.0' + implementation 'androidx.appcompat:appcompat:1.6.1' + implementation 'androidx.core:core-ktx:1.9.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' - implementation "androidx.recyclerview:recyclerview:1.2.1" + implementation "androidx.recyclerview:recyclerview:1.3.2" + implementation 'androidx.test:monitor:1.6.1' testImplementation 'junit:junit:4.13.2' // Location - api 'com.google.android.gms:play-services-location:20.0.0' - api 'com.google.android.gms:play-services-maps:18.1.0' + api 'com.google.android.gms:play-services-location:21.1.0' + api 'com.google.android.gms:play-services-maps:18.2.0' // Google material - implementation 'com.google.android.material:material:1.6.1' + implementation 'com.google.android.material:material:1.11.0' - implementation "com.google.android.libraries.places:places:2.6.0" + implementation "com.google.android.libraries.places:places:3.3.0" //unittest - testImplementation 'org.mockito:mockito-core:3.5.13' + testImplementation 'org.mockito:mockito-core:5.10.0' testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0' - testImplementation 'org.mockito:mockito-inline:3.5.13' + testImplementation 'org.mockito:mockito-inline:5.2.0' testImplementation 'android.arch.core:core-testing:1.1.1' testImplementation 'io.kotlintest:kotlintest-runner-junit5:3.3.2' + androidTestImplementation 'junit:junit:4.13.2' } diff --git a/vanillaplacepicker/src/androidTest/java/com/vanillaplacepicker/ExampleInstrumentedTest.java b/vanillaplacepicker/src/androidTest/java/com/vanillaplacepicker/ExampleInstrumentedTest.java index 0566569..6d040d6 100644 --- a/vanillaplacepicker/src/androidTest/java/com/vanillaplacepicker/ExampleInstrumentedTest.java +++ b/vanillaplacepicker/src/androidTest/java/com/vanillaplacepicker/ExampleInstrumentedTest.java @@ -1,24 +1,26 @@ package com.vanillaplacepicker; +import static org.junit.Assert.assertEquals; + import android.content.Context; -import android.support.test.InstrumentationRegistry; -import android.support.test.runner.AndroidJUnit4; + +import androidx.test.platform.app.InstrumentationRegistry; + import org.junit.Test; import org.junit.runner.RunWith; - -import static org.junit.Assert.*; +import org.junit.runners.JUnit4; /** * Instrumented test, which will execute on an Android device. * * @see Testing documentation */ -@RunWith(AndroidJUnit4.class) +@RunWith(JUnit4.class) public class ExampleInstrumentedTest { @Test public void useAppContext() { // Context of the app under test. - Context appContext = InstrumentationRegistry.getTargetContext(); + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); assertEquals("com.miplacepicker.test", appContext.getPackageName()); } diff --git a/vanillaplacepicker/src/main/AndroidManifest.xml b/vanillaplacepicker/src/main/AndroidManifest.xml index 19b3aed..2782dd5 100644 --- a/vanillaplacepicker/src/main/AndroidManifest.xml +++ b/vanillaplacepicker/src/main/AndroidManifest.xml @@ -1,5 +1,4 @@ - + @@ -8,11 +7,11 @@ + android:name="com.vanillaplacepicker.presentation.map.VanillaMapActivity" + android:theme="@style/AppTheme" /> diff --git a/vanillaplacepicker/src/main/java/com/vanillaplacepicker/domain/common/SafeObserver.kt b/vanillaplacepicker/src/main/java/com/vanillaplacepicker/domain/common/SafeObserver.kt index ffb98ca..186da60 100644 --- a/vanillaplacepicker/src/main/java/com/vanillaplacepicker/domain/common/SafeObserver.kt +++ b/vanillaplacepicker/src/main/java/com/vanillaplacepicker/domain/common/SafeObserver.kt @@ -3,7 +3,7 @@ package com.vanillaplacepicker.domain.common import androidx.lifecycle.Observer class SafeObserver(private val notifier: (T) -> Unit) : Observer { - override fun onChanged(t: T?) { - t?.let { notifier(t) } + override fun onChanged(value: T) { + value?.let { notifier(it) } } } \ No newline at end of file diff --git a/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/builder/SearchZoneRect.kt b/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/builder/SearchZoneRect.kt index e0c3a72..8be7402 100644 --- a/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/builder/SearchZoneRect.kt +++ b/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/builder/SearchZoneRect.kt @@ -2,7 +2,7 @@ package com.vanillaplacepicker.presentation.builder import android.os.Parcelable import com.google.android.gms.maps.model.LatLng -import kotlinx.android.parcel.Parcelize +import kotlinx.parcelize.Parcelize @Parcelize data class SearchZoneRect(val lowerLeft: LatLng, val upperRight: LatLng) : Parcelable \ No newline at end of file diff --git a/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/builder/VanillaPlacePicker.kt b/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/builder/VanillaPlacePicker.kt index 546976e..57ff2da 100644 --- a/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/builder/VanillaPlacePicker.kt +++ b/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/builder/VanillaPlacePicker.kt @@ -3,6 +3,7 @@ package com.vanillaplacepicker.presentation.builder import android.content.Context import android.content.ContextWrapper import android.content.Intent +import android.os.Build import android.os.Bundle import android.util.Log import com.google.android.gms.maps.model.LatLng @@ -11,7 +12,12 @@ import com.vanillaplacepicker.data.VanillaAddress import com.vanillaplacepicker.data.common.AutoCompleteAddressDetailsMapper import com.vanillaplacepicker.extenstion.isRequiredField import com.vanillaplacepicker.presentation.map.VanillaMapActivity -import com.vanillaplacepicker.utils.* +import com.vanillaplacepicker.utils.AutoCompleteUtils +import com.vanillaplacepicker.utils.BundleUtils +import com.vanillaplacepicker.utils.KeyUtils +import com.vanillaplacepicker.utils.MapType +import com.vanillaplacepicker.utils.PickerLanguage +import com.vanillaplacepicker.utils.PickerType import wrap @@ -22,7 +28,15 @@ class VanillaPlacePicker { fun getPlaceResult(data: Intent?): VanillaAddress? { if (data != null) { - val selectedPlace = data.getSerializableExtra(KeyUtils.SELECTED_PLACE) + val selectedPlace = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + data.getSerializableExtra( + KeyUtils.SELECTED_PLACE, + VanillaAddress::class.java + ) + } else { + data.getSerializableExtra(KeyUtils.SELECTED_PLACE) + } return if (selectedPlace is VanillaAddress) { selectedPlace } else { diff --git a/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/common/VanillaBaseViewModelActivity.kt b/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/common/VanillaBaseViewModelActivity.kt index 2e5cf7b..5e1165d 100644 --- a/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/common/VanillaBaseViewModelActivity.kt +++ b/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/common/VanillaBaseViewModelActivity.kt @@ -1,13 +1,14 @@ package com.vanillaplacepicker.presentation.common import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import androidx.annotation.CallSuper import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.ViewModel import androidx.viewbinding.ViewBinding -abstract class VanillaBaseViewModelActivity : AppCompatActivity() { +abstract class VanillaBaseViewModelActivity : AppCompatActivity(){ private var _binding: VB? = null protected val binding get() = _binding!! @@ -40,4 +41,6 @@ abstract class VanillaBaseViewModelActivity : A super.onDestroy() _binding = null } + + } \ No newline at end of file diff --git a/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/map/VanillaMapActivity.kt b/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/map/VanillaMapActivity.kt index 7ff3874..d0ecada 100644 --- a/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/map/VanillaMapActivity.kt +++ b/vanillaplacepicker/src/main/java/com/vanillaplacepicker/presentation/map/VanillaMapActivity.kt @@ -20,6 +20,7 @@ import com.google.android.gms.common.api.ResolvableApiException import com.google.android.gms.location.* import com.google.android.gms.maps.CameraUpdateFactory import com.google.android.gms.maps.GoogleMap +import com.google.android.gms.maps.MapsInitializer import com.google.android.gms.maps.OnMapReadyCallback import com.google.android.gms.maps.SupportMapFragment import com.google.android.gms.maps.model.LatLng @@ -36,11 +37,12 @@ import com.vanillaplacepicker.presentation.builder.VanillaPlacePicker import com.vanillaplacepicker.presentation.common.VanillaBaseViewModelActivity import com.vanillaplacepicker.service.FetchAddressIntentService import com.vanillaplacepicker.utils.* +import com.google.android.gms.maps.OnMapsSdkInitializedCallback +import com.vanillaplacepicker.data.VanillaAddress class VanillaMapActivity : VanillaBaseViewModelActivity(), - OnMapReadyCallback, - View.OnClickListener { + OnMapReadyCallback, View.OnClickListener, OnMapsSdkInitializedCallback { companion object { private val TAG = this::class.java.simpleName } @@ -85,6 +87,14 @@ class VanillaMapActivity : ) } + private val launcher = + registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result -> + when (result.resultCode) { + Activity.RESULT_CANCELED -> viewModel.fetchSavedLocation() + Activity.RESULT_OK -> postDelayed() + } + } + override fun initViews() { super.initViews() // HIDE ActionBar(if exist in style) of root project module @@ -105,6 +115,7 @@ class VanillaMapActivity : if (!isRequestedWithLocation) { startLocationUpdates() } + MapsInitializer.initialize(this, MapsInitializer.Renderer.LATEST, this) mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment? mapFragment?.getMapAsync(this) resultReceiver = AddressResultReceiver(Handler(Looper.getMainLooper())) @@ -112,7 +123,11 @@ class VanillaMapActivity : override fun getBundle() { if (hasExtra(KeyUtils.EXTRA_CONFIG)) { - vanillaConfig = intent.getParcelableExtra(KeyUtils.EXTRA_CONFIG) + vanillaConfig = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + intent.getParcelableExtra(KeyUtils.EXTRA_CONFIG, VanillaConfig::class.java) + } else { + intent.getParcelableExtra(KeyUtils.EXTRA_CONFIG) + } } if (vanillaConfig?.latitude != KeyUtils.DEFAULT_LOCATION && vanillaConfig?.longitude != KeyUtils.DEFAULT_LOCATION) { isRequestedWithLocation = true @@ -148,23 +163,22 @@ class VanillaMapActivity : override fun onClick(v: View?) { when (v?.id) { - R.id.ivBack -> onBackPressed() + R.id.ivBack -> onBackPressedDispatcher.onBackPressed() //onBackPressed() R.id.ivDone -> { selectedPlace ?: return setResult(Activity.RESULT_OK, Intent().apply { - putExtra( - KeyUtils.SELECTED_PLACE, + putExtra(KeyUtils.SELECTED_PLACE, selectedPlace?.let { AddressMapperGoogleMap.apply(it) }) }) finish() } - R.id.tvAddress -> - placePickerResultLauncher.launch(vanillaConfig?.let { - AutoCompleteUtils.getAutoCompleteIntent( - this, - it - ) - }) + + R.id.tvAddress -> placePickerResultLauncher.launch(vanillaConfig?.let { + AutoCompleteUtils.getAutoCompleteIntent( + this, it + ) + }) + R.id.fabLocation -> isGpsEnabled() } } @@ -184,8 +198,18 @@ class VanillaMapActivity : } when (resultCode) { KeyUtils.SUCCESS_RESULT -> { - selectedPlace = + /*selectedPlace = + resultData.getSerializable(KeyUtils.RESULT_DATA_KEY) as GeoCoderAddressResponse*/ + + selectedPlace = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + resultData.getSerializable( + KeyUtils.RESULT_DATA_KEY, + GeoCoderAddressResponse::class.java + ) + } else { resultData.getSerializable(KeyUtils.RESULT_DATA_KEY) as GeoCoderAddressResponse + } + with(binding.customToolbar) { if (selectedPlace?.addressLine.isRequiredField()) { Logger.d( @@ -199,10 +223,12 @@ class VanillaMapActivity : } } } + KeyUtils.FAILURE_RESULT -> { val errorMessage = resultData.getString(KeyUtils.RESULT_MESSAGE_KEY) ToastUtils.showToast(this@VanillaMapActivity, errorMessage) } + else -> { // make address empty } @@ -210,6 +236,18 @@ class VanillaMapActivity : } } + override fun onMapsSdkInitialized(renderer: MapsInitializer.Renderer) { + when (renderer) { + MapsInitializer.Renderer.LATEST -> Log.d( + "MapsDemo", "The latest version of the renderer is used." + ) + + MapsInitializer.Renderer.LEGACY -> Log.d( + "MapsDemo", "The legacy version of the renderer is used." + ) + } + } + /** * Manipulates the map when it's available. * The API invokes this callback when the map is ready for use. @@ -217,21 +255,18 @@ class VanillaMapActivity : override fun onMapReady(googleMap: GoogleMap) { this.googleMap = googleMap this.googleMap?.clear() - if (vanillaConfig?.enableSatelliteView == true) - this.googleMap?.mapType = GoogleMap.MAP_TYPE_SATELLITE + if (vanillaConfig?.enableSatelliteView == true) this.googleMap?.mapType = + GoogleMap.MAP_TYPE_SATELLITE // Customise the styling of the base map using a JSON object defined... try { // ...in a raw resource file. if (vanillaConfig?.mapStyleJSONResId != KeyUtils.DEFAULT_STYLE_JSON_RESID) { - this.googleMap?.setMapStyle( - vanillaConfig?.let { - MapStyleOptions.loadRawResourceStyle( - this@VanillaMapActivity, - it.mapStyleJSONResId - ) - } - ) + this.googleMap?.setMapStyle(vanillaConfig?.let { + MapStyleOptions.loadRawResourceStyle( + this@VanillaMapActivity, it.mapStyleJSONResId + ) + }) } // ...in a string resource file. this.googleMap?.mapType = vanillaConfig.let { it?.mapType?.value!! } @@ -248,9 +283,7 @@ class VanillaMapActivity : } if (cameraUpdateDefaultLocation != null) { this.googleMap?.animateCamera( - cameraUpdateDefaultLocation, - KeyUtils.GOOGLE_MAP_CAMERA_ANIMATE_DURATION, - null + cameraUpdateDefaultLocation, KeyUtils.GOOGLE_MAP_CAMERA_ANIMATE_DURATION, null ) } /** @@ -261,15 +294,13 @@ class VanillaMapActivity : vanillaConfig?.zoneRect?.let { this.googleMap?.setLatLngBoundsForCameraTarget( LatLngBounds( - it.lowerLeft, - it.upperRight + it.lowerLeft, it.upperRight ) ) } this.googleMap?.setOnCameraMoveListener { - with(binding.customToolbar) - { + with(binding.customToolbar) { tvAddress.text = getString(R.string.searching) ivDone.hide() } @@ -309,8 +340,7 @@ class VanillaMapActivity : changeLocationCompassButtonPosition() if (ActivityCompat.checkSelfPermission( - this, - Manifest.permission.ACCESS_FINE_LOCATION + this, Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED ) { this.googleMap?.isMyLocationEnabled = true @@ -327,8 +357,9 @@ class VanillaMapActivity : private fun changeLocationCompassButtonPosition() { try { val locationCompassButton = - (mapFragment?.view?.findViewById(Integer.parseInt("1"))?.parent as View) - .findViewById(Integer.parseInt("5")) + (mapFragment?.view?.findViewById(Integer.parseInt("1"))?.parent as View).findViewById( + Integer.parseInt("5") + ) val rlp = locationCompassButton.layoutParams as RelativeLayout.LayoutParams rlp.addRule(RelativeLayout.ALIGN_PARENT_START, 0) rlp.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE) @@ -345,8 +376,9 @@ class VanillaMapActivity : * */ private fun changeMyLocationButtonPosition() { val locationButton = - (mapFragment?.view?.findViewById(Integer.parseInt("1"))?.parent as View) - .findViewById(Integer.parseInt("2")) + (mapFragment?.view?.findViewById(Integer.parseInt("1"))?.parent as View).findViewById( + Integer.parseInt("2") + ) val rlp = locationButton.layoutParams as RelativeLayout.LayoutParams rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0) rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE) @@ -383,11 +415,8 @@ class VanillaMapActivity : LatLng( vanillaAddress?.latitude ?: KeyUtils.DEFAULT_LOCATION, vanillaAddress?.longitude ?: KeyUtils.DEFAULT_LOCATION - ), - KeyUtils.DEFAULT_ZOOM_LEVEL - ), - KeyUtils.GOOGLE_MAP_CAMERA_ANIMATE_DURATION, - null + ), KeyUtils.DEFAULT_ZOOM_LEVEL + ), KeyUtils.GOOGLE_MAP_CAMERA_ANIMATE_DURATION, null ) } @@ -404,21 +433,22 @@ class VanillaMapActivity : // If user interaction was interrupted, the permission request is cancelled and you receive empty arrays. Log.d(TAG, resources.getString(R.string.user_interaction_was_cancelled)) } + grantResults[0] == PackageManager.PERMISSION_GRANTED -> { if (!isRequestedWithLocation) { startLocationUpdates() } } - else -> showAlertDialog( - R.string.missing_permission_message, + + else -> showAlertDialog(R.string.missing_permission_message, R.string.missing_permission_title, R.string.permission, - R.string.cancel, { + R.string.cancel, + { // this mean user has clicked on permission button to update run time permission. openAppSetting() - } - ) + }) } } } @@ -427,10 +457,11 @@ class VanillaMapActivity : private fun startLocationRequestPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // this mean device os is greater or equal to Marshmallow. - if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) - != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( + if (ActivityCompat.checkSelfPermission( this, - Manifest.permission.ACCESS_COARSE_LOCATION + Manifest.permission.ACCESS_FINE_LOCATION + ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( + this, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) { // here we are going to request location run time permission. @@ -444,13 +475,17 @@ class VanillaMapActivity : } } - private val locationRequest = LocationRequest.create().apply { + private val locationRequest = LocationRequest.Builder( + Priority.PRIORITY_HIGH_ACCURACY, + KeyUtils.DEFAULT_FETCH_LOCATION_INTERVAL + ).build() + /*.apply { this.priority = Priority.PRIORITY_HIGH_ACCURACY this.interval = KeyUtils.DEFAULT_FETCH_LOCATION_INTERVAL - } + }*/ - private val locationSettingRequest = LocationSettingsRequest.Builder() - .addLocationRequest(locationRequest) + private val locationSettingRequest = + LocationSettingsRequest.Builder().addLocationRequest(locationRequest) /** * this method will check required for location and according to result it will go ahead for fetching location. @@ -458,33 +493,26 @@ class VanillaMapActivity : private fun startLocationUpdates() { // Begin by checking if the device has the necessary location settings. LocationServices.getSettingsClient(this) - .checkLocationSettings(locationSettingRequest.build()) - .addOnSuccessListener(this) { + .checkLocationSettings(locationSettingRequest.build()).addOnSuccessListener(this) { getLocationFromFusedLocation() }.addOnFailureListener(this) { e -> when ((e as ApiException).statusCode) { LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> { Logger.i( - TAG, - resources.getString(R.string.location_settings_are_not_satisfied) + TAG, resources.getString(R.string.location_settings_are_not_satisfied) ) try { val rae = e as ResolvableApiException - registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result -> - when (result.resultCode) { - Activity.RESULT_CANCELED -> viewModel.fetchSavedLocation() - Activity.RESULT_OK -> postDelayed() - } - }.launch(IntentSenderRequest.Builder(rae.resolution).build()) + launcher.launch(IntentSenderRequest.Builder(rae.resolution).build()) } catch (sie: IntentSender.SendIntentException) { Logger.i( - TAG, - getString(R.string.pendingintent_unable_to_execute_request) + TAG, getString(R.string.pendingintent_unable_to_execute_request) ) viewModel.fetchSavedLocation() sie.printStackTrace() } } + LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> { val errorMessage = resources.getString(R.string.location_settings_are_inadequate_and_cannot_be_fixed_here) @@ -497,11 +525,11 @@ class VanillaMapActivity : private fun getLocationFromFusedLocation() { if (ActivityCompat.checkSelfPermission( + this, Manifest.permission.ACCESS_FINE_LOCATION + ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( this, - Manifest.permission.ACCESS_FINE_LOCATION + Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED - && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) - != PackageManager.PERMISSION_GRANTED ) { return } @@ -509,8 +537,7 @@ class VanillaMapActivity : fusedLocationProviderClient?.flushLocations() Looper.myLooper()?.let { fusedLocationProviderClient?.requestLocationUpdates( - locationRequest, - object : LocationCallback() { + locationRequest, object : LocationCallback() { override fun onLocationResult(locationResult: LocationResult) { super.onLocationResult(locationResult) val location = locationResult.lastLocation @@ -530,8 +557,7 @@ class VanillaMapActivity : viewModel.fetchSavedLocation() } } - }, - it + }, it ) } } diff --git a/vanillaplacepicker/src/main/java/com/vanillaplacepicker/service/FetchAddressIntentService.kt b/vanillaplacepicker/src/main/java/com/vanillaplacepicker/service/FetchAddressIntentService.kt index 894dca3..213bdcd 100644 --- a/vanillaplacepicker/src/main/java/com/vanillaplacepicker/service/FetchAddressIntentService.kt +++ b/vanillaplacepicker/src/main/java/com/vanillaplacepicker/service/FetchAddressIntentService.kt @@ -75,11 +75,11 @@ class FetchAddressIntentService : IntentService("FetchAddressIntentService") { // surrounding the given latitude and longitude. The results are a best guess and are // not guaranteed to be accurate. addresses = geocoder.getFromLocation( - latlng.latitude, - latlng.longitude, - // In this sample, we get just a single address. - 1 - ) + latlng.latitude, + latlng.longitude, + // In this sample, we get just a single address. + 1 + ) as List
} catch (ioException: IOException) { // Catch network or other I/O problems. Logger.e(TAG, ioException.toString())