1010 */
1111package com.omega_r.libs.omegaintentbuilder.builders
1212
13+ import android.app.Activity
14+ import android.app.Fragment
1315import android.content.Context
1416import android.content.Intent
1517import android.net.Uri
18+ import android.os.Bundle
1619import com.omega_r.libs.omegaintentbuilder.OmegaIntentBuilder
20+ import com.omega_r.libs.omegaintentbuilder.handlers.ActivityResultCallback
1721import com.omega_r.libs.omegaintentbuilder.handlers.ContextIntentHandler
1822import com.omega_r.libs.omegaintentbuilder.handlers.FailCallback
23+ import com.omega_r.libs.omegaintentbuilder.interfaces.IntentHandler
1924import com.omega_r.libs.omegaintentbuilder.types.MapTypes
2025import com.omega_r.libs.omegaintentbuilder.types.MapTypes.*
26+ import com.omega_r.libs.omegaintentbuilder.types.MapViewTypes
27+ import com.omega_r.libs.omegaintentbuilder.types.MapViewTypes.*
2128
2229/* *
2330 * MapIntentBuilder is a helper for open Maps applications
@@ -28,6 +35,8 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
2835 private var longitude: Double? = null
2936 private var address: String? = null
3037 private var failtype: MapTypes ? = null
38+ private var viewType: MapViewTypes ? = null
39+ private var zoom: Int? = null
3140
3241 init {
3342 if (types.isEmpty()) types = MapTypes .values()
@@ -84,78 +93,147 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
8493 return this
8594 }
8695
96+ fun zoom (zoom : Int ): MapIntentBuilder {
97+ if (zoom >= 0 ) this .zoom = zoom
98+ return this
99+ }
100+
101+ fun viewType (viewType : MapViewTypes ): MapIntentBuilder {
102+ this .viewType = viewType
103+ return this
104+ }
105+
87106 override fun createIntent (context : Context ): Intent {
88- val uri: Uri = getFormattedUri(0 )
107+ // We take first because next type will be handled in WrapperIntentHandler
108+ val mapType = types.first()
109+ val uri: Uri = getFormattedUri(mapType)
89110 val intent = Intent (Intent .ACTION_VIEW , uri)
90111
91- when (types[ 0 ] ) {
92- GOOGLE_MAP -> intent.setPackage( GOOGLE_MAP .packageName)
93- YANDEX_MAP -> intent.setPackage( YANDEX_MAP .packageName)
94- KAKAO_MAP -> intent.setPackage( KAKAO_MAP .packageName)
95- NAVER_MAP -> intent.setPackage( NAVER_MAP .packageName)
96- }
112+ when (mapType ) {
113+ GOOGLE_MAP -> GOOGLE_MAP .packageName
114+ YANDEX_MAP -> YANDEX_MAP .packageName
115+ KAKAO_MAP -> KAKAO_MAP .packageName
116+ NAVER_MAP -> NAVER_MAP .packageName
117+ }. also { intent.setPackage(it) }
97118
98119 return intent
99120 }
100121
101- private fun getFormattedUri (index : Int ): Uri {
122+ private fun getFormattedUri (mapType : MapTypes ): Uri {
102123 val sb = StringBuilder ()
103- when (types[index]) {
104- GOOGLE_MAP -> {
105- sb.append(" geo:" )
106- if (latitude != null && longitude != null ) {
107- sb.append(latitude, " ," , longitude)
108- }
109- if (address != null ) {
110- sb.append(" ?q=" , Uri .encode(address))
111- }
112- }
113- YANDEX_MAP -> {
114- sb.append(" yandexmaps://" , YANDEX_MAP .packageName, " /?pt=" )
115- if (latitude != null && longitude != null ) {
116- sb.append(longitude, " ," , latitude)
117- }
118- if (address != null ) {
119- sb.append(" &text=" , address)
120- }
121- }
122- KAKAO_MAP -> {
123- sb.append(" daummaps://look?p=" )
124- if (latitude != null && longitude != null ) {
125- sb.append(latitude, " ," , longitude)
126- }
124+ when (mapType) {
125+ GOOGLE_MAP -> formulateGoogleUri(sb)
126+ YANDEX_MAP -> formulateYandexMapUri(sb)
127+ KAKAO_MAP -> formulateKakaoMapUri(sb)
128+ NAVER_MAP -> formulateNaverMapUri(sb)
129+ }
130+ return Uri .parse(sb.toString())
131+ }
132+
133+ private fun formulateGoogleUri (sb : StringBuilder ) {
134+ val viewType = when (viewType) {
135+ MAP -> " m"
136+ SATELLITE -> " k"
137+ HYBRID -> " h"
138+ TERRAIN -> " p"
139+ GOOGLE_EARTH -> " e"
140+ else -> null
141+ }
142+
143+ if (viewType == null ) {
144+ sb.append(" geo:" )
145+ if (latitude != null && longitude != null ) {
146+ sb.append(latitude, " ," , longitude)
127147 }
128- NAVER_MAP -> {
129- sb.append( " geo: " )
130- if (latitude != null && longitude != null ) {
131- sb .append(latitude, " , " , longitude )
132- }
133- if (address != null ) {
134- sb.append(" ?q= " , Uri .encode(address) )
135- }
148+ address?. let { sb.append( " ?q= " , Uri .encode(address)) }
149+ } else {
150+ sb.append( " http://maps.google.com/maps? " )
151+ .append(" t= $viewType " )
152+ address?. let { sb.append( " &q= " , Uri .encode(address)) }
153+ if (latitude != null && longitude != null ) {
154+ sb.append(" &loc: " )
155+ .append(latitude, " + " , longitude)
136156 }
137157 }
138- return Uri .parse(sb.toString())
139158 }
140159
141- override fun createIntentHandler (context : Context ): ContextIntentHandler {
142- val failIntentHandlers = createFailIntentHandler(context)
143- if (failIntentHandlers.isEmpty()) {
144- return super .createIntentHandler(context)
160+ private fun formulateYandexMapUri (sb : StringBuilder ) {
161+ sb.append(" yandexmaps://" , " maps.yandex.ru/?" )
162+ if (latitude != null && longitude != null ) {
163+ sb.append(" pt=" )
164+ .append(longitude, " ," , latitude)
165+ }
166+ val viewType = when (viewType) {
167+ MAP -> " map"
168+ SATELLITE -> " sat"
169+ HYBRID -> " skl"
170+ OPEN_MAP -> " pmap"
171+ else -> null
172+ }
173+ viewType?.let { sb.append(" &l=" , viewType) }
174+ zoom?.let { sb.append(" &z=" , zoom) }
175+ address?.let { sb.append(" &text=" , Uri .encode(address)) }
176+ }
177+
178+ private fun formulateNaverMapUri (sb : StringBuilder ) {
179+ sb.append(" nmap://" )
180+ if (address == null ) sb.append(" map?" ) else sb.append(" place?" )
181+ latitude?.let { sb.append(" lat=$latitude " ) }
182+ longitude?.let { sb.append(" &lng=$longitude " ) }
183+ zoom?.let { sb.append(" &zoom=$zoom " ) }
184+ address?.let { sb.append(" &name=" , Uri .encode(address)) }
185+ }
186+
187+ private fun formulateKakaoMapUri (sb : StringBuilder ) {
188+ sb.append(" daummaps://" )
189+ if (address == null ) {
190+ sb.append(" look?" )
145191 } else {
146- return WrapperIntentHandler (context, createIntent(context), failIntentHandlers.last(), failIntentHandlers.first())
192+ sb.append(" search?" )
193+ .append(" q=" , address, " &" )
194+ }
195+ if (latitude != null && longitude != null ) {
196+ sb.append(" p=" )
197+ .append(latitude, " ," , longitude)
147198 }
199+ }
148200
201+ override fun createIntentHandler (activity : Activity ): IntentHandler {
202+ return createFailIntentHandler(activity) ? : super .createIntentHandler(activity)
149203 }
150204
151- private fun createFailIntentHandler (context : Context ): List <ContextIntentHandler > {
152- val result = ArrayList <ContextIntentHandler >(types.size)
205+ override fun createIntentHandler (fragment : Fragment ): IntentHandler {
206+ return createFailIntentHandler(fragment.activity) ? : super .createIntentHandler(fragment)
207+ }
208+
209+ override fun createIntentHandler (fragment : androidx.fragment.app.Fragment ): IntentHandler {
210+ return fragment.context?.let { createFailIntentHandler(it) } ? : super .createIntentHandler(fragment)
211+ }
212+
213+ override fun createIntentHandler (context : Context ): IntentHandler {
214+ return createFailIntentHandler(context) ? : super .createIntentHandler(context)
215+ }
216+
217+ private fun createFailIntentHandler (context : Context ): WrapperIntentHandler ? {
218+ val list = createFailIntentHandlers(context)
219+ return if (list.isEmpty()) null else WrapperIntentHandler (context, createIntent(context), list.last(), list.first())
220+ }
153221
154- var prevIntentHandler: ContextIntentHandler ? = null
222+ private fun createFailIntentHandlers (context : Context ): List <IntentHandler > {
223+ val result = ArrayList <IntentHandler >(types.size)
224+
225+ var prevIntentHandler: IntentHandler ? = null
155226
156227 for (index in 1 until types.size) {
157228 val intentHandler = OmegaIntentBuilder
158229 .map(types[index])
230+ .also {
231+ it.address = address
232+ it.latitude = latitude
233+ it.longitude = longitude
234+ it.zoom = zoom
235+ it.viewType = viewType
236+ }
159237 .createIntentHandler(context)
160238 result + = intentHandler
161239 prevIntentHandler?.failIntentHandler(intentHandler)
@@ -175,10 +253,15 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
175253 return result
176254 }
177255
178- private class WrapperIntentHandler (context : Context , createdIntent : Intent ,
179- private val handler : ContextIntentHandler ,
180- failIntentHandler : ContextIntentHandler ?
181- ) : ContextIntentHandler(context, createdIntent) {
256+ private class WrapperIntentHandler (
257+ context : Context ,
258+ createdIntent : Intent ,
259+ private val handler : IntentHandler ,
260+ failIntentHandler : IntentHandler ?
261+ ) : ContextIntentHandler(
262+ context,
263+ createdIntent
264+ ) {
182265
183266 init {
184267 super .failIntentHandler(failIntentHandler)
@@ -204,11 +287,40 @@ class MapIntentBuilder(private vararg var types: MapTypes) : BaseActivityBuilder
204287 return this
205288 }
206289
207- override fun failIntentHandler (failIntentHandler : ContextIntentHandler ? ): ContextIntentHandler {
290+ override fun failIntentHandler (failIntentHandler : IntentHandler ? ): IntentHandler {
208291 handler.failIntentHandler(failIntentHandler)
209292 return this
210293 }
211294
295+ override fun chooserTitle (chooserTitle : CharSequence ): IntentHandler {
296+ handler.chooserTitle(chooserTitle)
297+ return this
298+ }
299+
300+ override fun chooserTitle (chooserTitle : String ): IntentHandler {
301+ handler.chooserTitle(chooserTitle)
302+ return this
303+ }
304+
305+ override fun chooserTitle (chooserTitle : Int ): IntentHandler {
306+ handler.chooserTitle(chooserTitle)
307+ return this
308+ }
309+
310+ override fun addFlagsClearBackStack (): IntentHandler {
311+ handler.addFlagsClearBackStack()
312+ return this
313+ }
314+
315+ override fun addFlags (flags : Int ): IntentHandler {
316+ handler.addFlags(flags)
317+ return this
318+ }
319+
320+ override fun setFlags (flags : Int ): IntentHandler {
321+ handler.setFlags(flags)
322+ return this
323+ }
212324 }
213325
214326}
0 commit comments