Skip to content

Commit

Permalink
Merge branch 'release/0.6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejburda committed Mar 13, 2018
2 parents 6efe426 + 341c720 commit d39fd44
Show file tree
Hide file tree
Showing 29 changed files with 414 additions and 210 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ The `MatchMore` is a static wrapper that provides you all the functions you need

## Usage

Setup application API key and world, get it for free from [http://matchmore.io/](http://matchmore.io/).
Setup application API key, get it for free from [http://matchmore.io/](http://matchmore.io/).

```kotlin
override fun onCreate() {
super.onCreate()
MatchMore.config(MatchMoreConfig(this, SdkConfigTest.API_KEY, SdkConfigTest.WORLD_ID, debugLog = true))
MatchMore.config(context = this, apiKey = SdkConfigTest.API_KEY, debugLog = false)
}
}
```
Expand Down
1 change: 0 additions & 1 deletion config/src/main/java/io/matchmore/config/SdkConfigTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ package io.matchmore.config

object SdkConfigTest {
const val API_KEY = "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhbHBzIiwic3ViIjoiNGUyOGI4YjYtZTIzYS00NmI0LWJiZTQtM2FjYzg3MjkzNmY4IiwiYXVkIjpbIlB1YmxpYyJdLCJuYmYiOjE1MjAzNDA2NjgsImlhdCI6MTUyMDM0MDY2OCwianRpIjoiMSJ9.PIz2RukwPWtEyGmOFSnkr3RqrSjxB9QKM-Ll-KyE65ZnTtVQldm3LwLlUuBIyvxQtmNEJjcBAAHGa0YzDUHWWg"
const val WORLD_ID = "4e28b8b6-e23a-46b4-bbe4-3acc872936f8"
const val TIMEOUT = 20000L // 20s
}
3 changes: 1 addition & 2 deletions example/src/main/java/io/matchmore/sdk/example/ExampleApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package io.matchmore.sdk.example
import android.support.multidex.MultiDexApplication
import io.matchmore.config.SdkConfigTest
import io.matchmore.sdk.MatchMore
import io.matchmore.sdk.MatchMoreConfig

class ExampleApp : MultiDexApplication() {
override fun onCreate() {
super.onCreate()
MatchMore.config(MatchMoreConfig(this, SdkConfigTest.API_KEY, SdkConfigTest.WORLD_ID, debugLog = true))
MatchMore.config(this, SdkConfigTest.API_KEY, true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import io.matchmore.config.SdkConfigTest;
import io.matchmore.sdk.MatchMore;
import io.matchmore.sdk.MatchMoreConfig;
import io.matchmore.sdk.MatchMoreSdk;
import io.matchmore.sdk.api.models.Publication;
import io.matchmore.sdk.api.models.Subscription;
Expand All @@ -29,14 +28,7 @@ protected void onCreate(Bundle savedInstanceState) {

// Configuration of api key/world id
if (!MatchMore.isConfigured()) {
MatchMore.config(new MatchMoreConfig(
this,
SdkConfigTest.API_KEY,
SdkConfigTest.WORLD_ID,
null,
null,
false,
true));
MatchMore.config(this, SdkConfigTest.API_KEY, true);
}

// Getting instance. It's static variable. It's possible to have only one instance of matchmore.
Expand Down Expand Up @@ -72,7 +64,7 @@ protected void onCreate(Bundle savedInstanceState) {
Log.d("JavaExample", device.getId());
return Unit.INSTANCE;
});

matchMore.getMatchMonitor().startPollingMatches();

// Start updating location
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Expand Down
12 changes: 6 additions & 6 deletions rx/src/main/java/io/matchmore/sdk/rx/MathMore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import io.matchmore.sdk.api.SuccessCallback
import io.matchmore.sdk.api.models.*
import io.matchmore.sdk.monitoring.MatchMonitor
import io.matchmore.sdk.store.AsyncClearable
import io.matchmore.sdk.store.AsyncCreateable
import io.matchmore.sdk.store.AsyncDeleteable
import io.matchmore.sdk.store.AsyncUpdateable
import io.matchmore.sdk.store.AsyncCreatable
import io.matchmore.sdk.store.AsyncDeletable
import io.matchmore.sdk.store.AsyncUpdatable
import io.reactivex.*

private fun completableEmitter(emitter: CompletableEmitter): () -> Unit = { if (!emitter.isDisposed) emitter.onComplete() }
Expand Down Expand Up @@ -49,13 +49,13 @@ fun MatchMoreSdk.rxCreateSubscription(subscription: Subscription, deviceWithId:
fun MatchMoreSdk.rxCreatePinDevice(pinDevice: PinDevice): Single<PinDevice> =
singleRx { success, error -> createPinDevice(pinDevice, success, error) }

fun <T> AsyncCreateable<T>.rxCreate(item: T): Single<T> =
fun <T> AsyncCreatable<T>.rxCreate(item: T): Single<T> =
singleRx { success, error -> create(item, success, error) }

fun <T> AsyncUpdateable<T>.rxUpdate(item: T): Single<T> =
fun <T> AsyncUpdatable<T>.rxUpdate(item: T): Single<T> =
singleRx { success, error -> update(item, success, error) }

fun <T> AsyncDeleteable<T>.rxDelete(item: T): Completable =
fun <T> AsyncDeletable<T>.rxDelete(item: T): Completable =
completableRx { complete, error -> delete(item, complete, error) }

fun AsyncClearable.rxDeleteAll(): Completable = completableRx { complete, error -> deleteAll(complete, error) }
Expand Down
10 changes: 3 additions & 7 deletions rx/src/test/java/io/matchmore/sdk/rx/RxBaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import android.content.Context
import android.location.Location
import android.location.LocationManager
import io.matchmore.config.SdkConfigTest
import io.matchmore.sdk.BuildConfig
import io.matchmore.sdk.MatchMore
import io.matchmore.sdk.MatchMoreConfig
import io.matchmore.sdk.api.ApiClient
import io.reactivex.Completable
import io.reactivex.Single
import io.reactivex.observers.TestObserver
Expand All @@ -34,11 +33,8 @@ abstract class RxBaseTest {

fun init() {
if (!MatchMore.isConfigured()) {
MatchMore.config(MatchMoreConfig(
RuntimeEnvironment.application,
SdkConfigTest.API_KEY, SdkConfigTest.WORLD_ID,
callbackInUIThread = false,
debugLog = true))
ApiClient.config.callbackInUIThread = false
MatchMore.config(RuntimeEnvironment.application, SdkConfigTest.API_KEY, true)
}
removeSubscriptions()
removePublications()
Expand Down
20 changes: 13 additions & 7 deletions sdk/src/main/java/io/matchmore/sdk/AlpsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import io.matchmore.sdk.api.ErrorCallback
import io.matchmore.sdk.api.SuccessCallback
import io.matchmore.sdk.api.adapters.ParserBuilder
import io.matchmore.sdk.api.models.*
import io.matchmore.sdk.managers.DefaultLocationProvider
import io.matchmore.sdk.managers.MatchMoreBeaconManager
import io.matchmore.sdk.managers.MatchMoreLocationManager
import io.matchmore.sdk.managers.MatchMoreLocationProvider
import io.matchmore.sdk.monitoring.MatchMonitor
import io.matchmore.sdk.store.DeviceStore
import io.matchmore.sdk.store.IBeaconTriplesStore
import io.matchmore.sdk.store.PublicationStore
import io.matchmore.sdk.store.SubscriptionStore
import io.matchmore.sdk.utils.PersistenceManager

class AlpsManager(matchMoreConfig: MatchMoreConfig) : MatchMoreSdk {
class AlpsManager(private val config: MatchMoreConfig) : MatchMoreSdk {

private val gson = ParserBuilder.gsonBuilder.create()
private val deviceStore by lazy {
Expand All @@ -26,11 +28,11 @@ class AlpsManager(matchMoreConfig: MatchMoreConfig) : MatchMoreSdk {
}
private val publicationStore by lazy { PublicationStore(this) }
private val subscriptionStore by lazy { SubscriptionStore(this) }
private val beaconManager by lazy { MatchMoreBeaconManager(matchMoreConfig.context, apiClient, deviceStore) }
private val beaconManager by lazy { MatchMoreBeaconManager(config.context, apiClient, deviceStore) }
private val iBeaconTriplesStore by lazy { IBeaconTriplesStore(this) }

val apiClient = ApiClient(gson, matchMoreConfig)
val persistenceManager = PersistenceManager(matchMoreConfig.context, gson)
val apiClient = ApiClient(gson, config)
val persistenceManager = PersistenceManager(config, gson)

override val matches: Set<Match>
get() = matchMonitor.deliveredMatches.toSet()
Expand Down Expand Up @@ -59,11 +61,15 @@ class AlpsManager(matchMoreConfig: MatchMoreConfig) : MatchMoreSdk {

override val devices = deviceStore

override val matchMonitor = MatchMonitor(this, matchMoreConfig)
override val matchMonitor = MatchMonitor(this, config)

override val locationManager = MatchMoreLocationManager(matchMoreConfig.context, this)
override val locationManager = MatchMoreLocationManager(this)

override fun startUpdatingLocation() = locationManager.startUpdatingLocation()
override fun startUpdatingLocation() =
locationManager.startUpdatingLocation(DefaultLocationProvider(config.context))

override fun startUpdatingLocation(locationProvider: MatchMoreLocationProvider) =
locationManager.startUpdatingLocation(locationProvider)

override fun stopUpdatingLocation() = locationManager.stopUpdatingLocation()

Expand Down
9 changes: 7 additions & 2 deletions sdk/src/main/java/io/matchmore/sdk/MatchMore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package io.matchmore.sdk

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.support.annotation.RequiresPermission
import io.matchmore.sdk.api.ErrorCallback
import io.matchmore.sdk.api.SuccessCallback
import io.matchmore.sdk.api.models.*
import io.matchmore.sdk.managers.MatchMoreLocationManager
import io.matchmore.sdk.managers.MatchMoreLocationProvider
import io.matchmore.sdk.monitoring.MatchMonitor
import io.matchmore.sdk.store.AsyncReadable
import io.matchmore.sdk.store.CRD
Expand All @@ -23,9 +25,10 @@ object MatchMore {
}

@JvmStatic
fun config(matchMoreConfig: MatchMoreConfig) {
@JvmOverloads
fun config(context: Context, apiKey: String, debugLog: Boolean = false) {
if (isConfigured()) throw IllegalStateException("You can not overwrite the configuration.")
this.matchMoreConfig = matchMoreConfig
this.matchMoreConfig = MatchMoreConfig(context, apiKey, debugLog)
}

@JvmStatic
Expand All @@ -52,6 +55,8 @@ interface MatchMoreSdk {
@RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
fun startUpdatingLocation()

fun startUpdatingLocation(locationProvider: MatchMoreLocationProvider)

fun stopUpdatingLocation()

@RequiresPermission(allOf = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.BLUETOOTH))
Expand Down
20 changes: 14 additions & 6 deletions sdk/src/main/java/io/matchmore/sdk/MatchMoreConfig.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package io.matchmore.sdk

import android.content.Context
import android.util.Base64
import org.json.JSONObject
import java.nio.charset.Charset

data class MatchMoreConfig @JvmOverloads constructor(
data class MatchMoreConfig(
var context: Context,
val apiKey: String,
val worldId: String,
val serverProtocol: String? = null,
val serverUrl: String? = null,
val callbackInUIThread: Boolean = true,
val debugLog: Boolean = false) {
var debugLog: Boolean
) {

init {
context = context.applicationContext
}

val worldId: String by lazy {
val segments = apiKey.split(".")
if (segments.size < 2) throw IllegalArgumentException("Invalid API Key.")
val json = Base64.decode(segments[1], Base64.DEFAULT).toString(Charset.defaultCharset())
return@lazy JSONObject(json).getString("sub")
}
}
16 changes: 9 additions & 7 deletions sdk/src/main/java/io/matchmore/sdk/api/ApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ class ApiClient(gson: Gson, private val matchMoreConfig: MatchMoreConfig) {
.addInterceptor {
it.proceed(it.request().newBuilder().addHeader("api-key", matchMoreConfig.apiKey).build())
}
val url = matchMoreConfig.serverUrl ?: DEFAULT_URL
val protocol = matchMoreConfig.serverProtocol ?: DEFAULT_PROTOCOL
val retrofitBuilder = Retrofit.Builder()
.baseUrl("$protocol://$url/$API_VERSION/")
.baseUrl("${config.serverProtocol}://${config.serverUrl}/${config.version}/")
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
if (!matchMoreConfig.callbackInUIThread) {
if (!config.callbackInUIThread) {
retrofitBuilder.callbackExecutor(Executors.newSingleThreadExecutor())
}
if (matchMoreConfig.debugLog) {
Expand All @@ -39,9 +37,13 @@ class ApiClient(gson: Gson, private val matchMoreConfig: MatchMoreConfig) {
val publicationApi by lazy { retrofit.create(PublicationApi::class.java) }
val subscriptionApi by lazy { retrofit.create(SubscriptionApi::class.java) }

class Config(var serverProtocol: String = "https",
var serverUrl: String = "api.matchmore.io",
var version: String = "v5",
var callbackInUIThread: Boolean = true)

companion object {
private const val DEFAULT_PROTOCOL = "https"
const val DEFAULT_URL = "api.matchmore.io"
const val API_VERSION = "v5"
@JvmStatic
val config = Config()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import io.swagger.annotations.ApiModelProperty
/**
* Location
*/

data class MatchMoreLocation(
data class MatchMoreLocation @JvmOverloads constructor (
/**
* The timestamp of the location creation in seconds since Jan 01 1970 (UTC).
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.matchmore.sdk.managers

import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.content.Context
import android.content.pm.PackageManager
import android.location.Criteria
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
import android.support.v4.content.ContextCompat
import io.matchmore.sdk.utils.mmLocation


class DefaultLocationProvider(private val context: Context): MatchMoreLocationProvider {

private var sender: LocationSender? = null

private val listener: LocationListener = object : LocationListener {
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {}

override fun onProviderEnabled(provider: String?) {}

override fun onProviderDisabled(provider: String?) {}

override fun onLocationChanged(location: Location) {
sender?.sendLocation(location.mmLocation)
}
}

private val locationManager by lazy {
context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
}

private fun findProvider() = locationManager.getBestProvider(Criteria().apply {
accuracy = Criteria.ACCURACY_FINE
}, false)

override fun startUpdatingLocation(sender: LocationSender) {
if (ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
val provider = findProvider()
?: throw IllegalStateException("Can't find location provider.")
val location = locationManager.getLastKnownLocation(provider)
if (location != null) sender.sendLocation(location.mmLocation)
locationManager.requestLocationUpdates(provider, MIN_TIME, MIN_DISTANCE, listener)
} else {
throw SecurityException("You need to get ACCESS_FINE_LOCATION permission first. ")
}
}

override fun stopUpdatingLocation() {
locationManager.removeUpdates(listener)
}

companion object {
private const val MIN_TIME = 10 * 1000L //10s
private const val MIN_DISTANCE = 10f //10m
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ class MatchMoreBeaconManager(

override fun onBeaconServiceConnect() {
beaconManager.addRangeNotifier { beacons, _ ->
beacons.forEach { beacon ->
Log.i("beacon", "${beacon.id1} ${beacon.distance}")
if (shouldTrigger(beacon)) {
val proximityEvent = ProximityEvent(deviceId = beacon.id1.toString(), distance = beacon.distance)
apiClient.deviceApi.triggerProximityEvents(deviceStore.main!!.id!!, proximityEvent)
.async({ beaconsTriggered.put(beacon.id1, TriggerInfo(System.currentTimeMillis(), beacon.distance)) })
deviceStore.main?.id?.let { mainDeviceId ->
beacons.forEach { beacon ->
Log.i("beacon", "${beacon.id1} ${beacon.distance}")
if (shouldTrigger(beacon)) {
val proximityEvent = ProximityEvent(deviceId = beacon.id1.toString(), distance = beacon.distance)
apiClient.deviceApi.triggerProximityEvents(mainDeviceId, proximityEvent)
.async({ beaconsTriggered[beacon.id1] = TriggerInfo(System.currentTimeMillis(), beacon.distance) })
}
}
}
}
Expand Down
Loading

0 comments on commit d39fd44

Please sign in to comment.