Skip to content

Commit 33a3c4d

Browse files
Merge pull request #140 from Omega-R/feature/map_make_route
Driving mode flag and any map service choice added
2 parents e05c7af + 0f9e8f4 commit 33a3c4d

1 file changed

Lines changed: 73 additions & 49 deletions

File tree

core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/MapIntentBuilder.kt

Lines changed: 73 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ import android.app.Fragment
1515
import android.content.Context
1616
import android.content.Intent
1717
import android.net.Uri
18-
import android.os.Bundle
1918
import com.omega_r.libs.omegaintentbuilder.IntentBuilderLauncher
2019
import com.omega_r.libs.omegaintentbuilder.OmegaIntentBuilder
21-
import com.omega_r.libs.omegaintentbuilder.handlers.ActivityResultCallback
2220
import com.omega_r.libs.omegaintentbuilder.handlers.ContextIntentHandler
2321
import com.omega_r.libs.omegaintentbuilder.handlers.FailCallback
2422
import com.omega_r.libs.omegaintentbuilder.interfaces.IntentHandler
@@ -40,10 +38,7 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
4038
private var zoom: Int? = null
4139
private var startLatitude: Double? = null
4240
private var startLongitude: Double? = null
43-
44-
init {
45-
if (types.isEmpty()) types = MapTypes.values()
46-
}
41+
private var isDrivingModeEnabled: Boolean = false
4742

4843
/**
4944
* Set a latitude.
@@ -120,25 +115,50 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
120115
return this
121116
}
122117

123-
override fun createIntent(context: Context): Intent {
124-
// We take first because next type will be handled in WrapperIntentHandler
125-
val mapType = types.first()
126-
val uri: Uri = getFormattedUri(mapType)
127-
val intent = Intent(Intent.ACTION_VIEW, uri)
118+
fun enableDrivingMode() {
119+
isDrivingModeEnabled = true
120+
}
128121

129-
when (mapType) {
130-
GOOGLE_MAP -> GOOGLE_MAP.packageName
131-
YANDEX_MAP -> YANDEX_MAP.packageName
132-
KAKAO_MAP -> KAKAO_MAP.packageName
133-
NAVER_MAP -> NAVER_MAP.packageName
134-
}.also { intent.setPackage(it) }
122+
override fun createIntent(context: Context) =
123+
if (types.isEmpty()) {
124+
Intent(Intent.ACTION_VIEW, undefinedMapUri())
125+
} else {
126+
// We take first because next type will be handled in WrapperIntentHandler
127+
val mapType = types.first()
128+
val uri = getFormattedUri(mapType)
129+
val intent = Intent(Intent.ACTION_VIEW, uri)
130+
131+
when (mapType) {
132+
GOOGLE_MAP -> GOOGLE_MAP.packageName
133+
YANDEX_MAP -> YANDEX_MAP.packageName
134+
KAKAO_MAP -> KAKAO_MAP.packageName
135+
NAVER_MAP -> NAVER_MAP.packageName
136+
}.also { intent.setPackage(it) }
137+
intent
138+
}
135139

136-
return intent
140+
private fun undefinedMapUri(): Uri {
141+
val sb = StringBuilder()
142+
if (startLatitude != null && startLongitude != null) {
143+
sb.append("geo:${startLatitude},${startLongitude}")
144+
if (latitude != null && longitude != null) {
145+
sb.append("?q=${latitude},${longitude}")
146+
}
147+
address?.let {
148+
sb.append(" (${Uri.encode(it)})")
149+
}
150+
} else if (latitude != null && longitude != null) {
151+
sb.append("geo:${latitude},${longitude}")
152+
address?.let {
153+
sb.append("?q=${Uri.encode(it)}")
154+
}
155+
}
156+
return Uri.parse(sb.toString())
137157
}
138158

139159
private fun getFormattedUri(mapType: MapTypes): Uri {
140160
val sb = StringBuilder()
141-
when(mapType) {
161+
when (mapType) {
142162
GOOGLE_MAP -> formulateGoogleUri(sb)
143163
YANDEX_MAP -> formulateYandexMapUri(sb)
144164
KAKAO_MAP -> formulateKakaoMapUri(sb)
@@ -157,8 +177,9 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
157177
else -> null
158178
}
159179

160-
if(startLatitude != null && startLongitude != null) {
161-
sb.append("google.navigation:q=", latitude, ",", longitude)
180+
if (startLatitude != null && startLongitude != null) {
181+
if (isDrivingModeEnabled) sb.append("google.navigation:q=", latitude, ",", longitude)
182+
else sb.append("/saddr=${startLatitude},${startLongitude}&addr=${latitude},${longitude}")
162183
} else {
163184
if (viewType == null) {
164185
sb.append("geo:")
@@ -178,18 +199,14 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
178199
} ?: if (latitude != null && longitude != null) {
179200
sb.append("?q=", latitude, ",", longitude)
180201
}
181-
if (latitude != null && longitude != null) {
182-
sb.append("&loc:")
183-
.append(latitude, "+", longitude)
184-
}
185202
}
186203
}
187204
}
188205

189206
private fun formulateYandexMapUri(sb: StringBuilder) {
190207
sb.append("yandexmaps://", "maps.yandex.ru/?")
191-
if(startLongitude != null && startLatitude != null) {
192-
sb.append("rtext=", startLatitude, ",", startLongitude, "~",latitude, ",", longitude)
208+
if (startLongitude != null && startLatitude != null) {
209+
sb.append("rtext=", startLatitude, ",", startLongitude, "~", latitude, ",", longitude)
193210
} else {
194211
if (latitude != null && longitude != null) {
195212
sb.append("pt=")
@@ -223,11 +240,11 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
223240
sb.append("look?")
224241
} else {
225242
sb.append("search?")
226-
.append("q=", address, "&")
243+
.append("q=", address, "&")
227244
}
228245
if (latitude != null && longitude != null) {
229246
sb.append("p=")
230-
.append(latitude, ",", longitude)
247+
.append(latitude, ",", longitude)
231248
}
232249
}
233250

@@ -244,7 +261,9 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
244261
}
245262

246263
override fun createIntentHandler(fragment: androidx.fragment.app.Fragment): IntentHandler {
247-
return fragment.context?.let { createFailIntentHandler(it) } ?: super.createIntentHandler(fragment)
264+
return fragment.context?.let { createFailIntentHandler(it) } ?: super.createIntentHandler(
265+
fragment
266+
)
248267
}
249268

250269
override fun createIntentHandler(context: Context): IntentHandler {
@@ -253,7 +272,12 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
253272

254273
private fun createFailIntentHandler(context: Context): WrapperIntentHandler? {
255274
val list = createFailIntentHandlers(context)
256-
return if (list.isEmpty()) null else WrapperIntentHandler(context, createIntent(context), list.last(), list.first())
275+
return if (list.isEmpty()) null else WrapperIntentHandler(
276+
context,
277+
createIntent(context),
278+
list.last(),
279+
list.first()
280+
)
257281
}
258282

259283
private fun createFailIntentHandlers(context: Context): List<IntentHandler> {
@@ -263,25 +287,25 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
263287

264288
for (index in 1 until types.size) {
265289
val intentHandler = OmegaIntentBuilder
266-
.map(types[index])
267-
.also {
268-
it.address = address
269-
it.latitude = latitude
270-
it.longitude = longitude
271-
it.zoom = zoom
272-
it.viewType = viewType
273-
}
274-
.createIntentHandler(context)
290+
.map(types[index])
291+
.also {
292+
it.address = address
293+
it.latitude = latitude
294+
it.longitude = longitude
295+
it.zoom = zoom
296+
it.viewType = viewType
297+
}
298+
.createIntentHandler(context)
275299
result += intentHandler
276300
prevIntentHandler?.failIntentHandler(intentHandler)
277301
prevIntentHandler = intentHandler
278302
}
279303

280304
if (failtype != null) {
281305
val lastHandler = OmegaIntentBuilder
282-
.playStore()
283-
.packageName(failtype!!.packageName)
284-
.createIntentHandler(context)
306+
.playStore()
307+
.packageName(failtype!!.packageName)
308+
.createIntentHandler(context)
285309
result += lastHandler
286310
prevIntentHandler?.failIntentHandler(lastHandler)
287311

@@ -291,13 +315,13 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
291315
}
292316

293317
private class WrapperIntentHandler(
294-
context: Context,
295-
createdIntent: Intent,
296-
private val handler: IntentHandler,
297-
failIntentHandler: IntentHandler?
318+
context: Context,
319+
createdIntent: Intent,
320+
private val handler: IntentHandler,
321+
failIntentHandler: IntentHandler?
298322
) : ContextIntentHandler(
299-
context,
300-
createdIntent
323+
context,
324+
createdIntent
301325
) {
302326

303327
init {

0 commit comments

Comments
 (0)