Skip to content

Commit

Permalink
Merge pull request #3 from cere-io/feature/docs
Browse files Browse the repository at this point in the history
Feature/docs
  • Loading branch information
sahataba authored Sep 18, 2020
2 parents 6502039 + 8a97ce3 commit c59dc40
Show file tree
Hide file tree
Showing 16 changed files with 397 additions and 121 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
6 changes: 5 additions & 1 deletion cere_sdk/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.github.dcendents.android-maven'

group='io.cere.cere_sdk'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"


defaultConfig {
minSdkVersion 15
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
Expand Down
13 changes: 11 additions & 2 deletions cere_sdk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.cere.cere_sdk" />
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="io.cere.cere_sdk" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:label="@string/app_name"
android:usesCleartextTraffic="true">
<activity
android:name=".WebviewActivity"
android:excludeFromRecents="true"
android:theme="@style/WidgetDialog"/>
</application>
</manifest>
100 changes: 0 additions & 100 deletions cere_sdk/src/main/java/io/cere/cere_sdk/CereApp.kt

This file was deleted.

197 changes: 197 additions & 0 deletions cere_sdk/src/main/java/io/cere/cere_sdk/CereModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package io.cere.cere_sdk

import android.app.Application
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.webkit.JavascriptInterface
import android.webkit.WebView


const val baseUrl: String = "http://sdk-common.cere.io.s3-website-us-west-2.amazonaws.com/native.html"

/**
* Interface used after `CereModule` init method.
* Executed after successful initialization.
*/
interface OnInitializationFinishedHandler {
fun handle()
}

/**
* Interface used after `CereModule` init method.
* Executed after initialization error.
*/
interface OnInitializationErrorHandler {
fun handle(error: String)
}

/**
* This is the main class which incapsulates all logic (opening/closing activity etc) and
* provides high-level methods to manipulate with.
**
* <p>All you need to start working with the class is to instantiate <tt>CereModule</tt> once and
* initialize it with 2 params. Example:
* </p>
*
* <p>
* <pre>
* {@code
* CereModule cereModule = CereModule.getInstance(context);
* cereModule.init("Your appId", "Your integrationPartnerUserId");
* }
* </pre>
* </p>
*
* <p>That's enough for start loading {@code CereModule}, but note that {@code CereModule} still
* remains hidden. Also, first load of {@code CereModule} takes a some time which depends on
* network connection quality. That's why you need to init {@code CereModule} as soon as possible.
* </p>
*
* <p>If you want to show {@code CereModule} right after it has initialized, you can add listener
* {@see OnInitializationFinishedHandler} implementation which will invoke method <tt>sendEvent</tt> on
* {@code CereModule} instance. Example:
* </p>
*
* <p>
* <pre>
* {@code
* cereModule.onInitializationFinishedHandler(() -> {
* cereModule.sendEvent("APP_LAUNCHED_TEST", "{}");
* });
* }
* </pre>
* </p>
*
* @author Rudolf Markulin
*/
class CereModule(private val context: Context) {

companion object {
const val TAG = "CereModule"
@Volatile
private var instance: CereModule? = null
@JvmStatic private fun make(context: Context): CereModule {
val module = CereModule(context).configureWebView()
instance = module
return module
}
@JvmStatic fun getInstance(application: Application): CereModule {
val inst = this.instance
if (inst != null) {
return inst
} else {
return make(application.applicationContext)
}
}
}

var onInitializationFinishedHandler: OnInitializationFinishedHandler = object: OnInitializationFinishedHandler {
override fun handle() {

}
}

var onInitializationErrorHandler: OnInitializationErrorHandler = object: OnInitializationErrorHandler {
override fun handle(error: String) {

}
}

lateinit var webview: WebView

private lateinit var appId: String
private lateinit var integrationPartnerUserId: String

private var initStatus: InitStatus = InitStatus.Uninitialised

private val version: String = context.packageManager.getPackageInfo(context.packageName, 0).versionName

/**
* @return current sdk initialisation status instance of {@code InitStatus}
*/
fun getInitStatus(): InitStatus {
return this.initStatus
}

/**
* Initializes and prepares the SDK for usage.
* @param appId: identifier of the application from RXB.
* @param integrationPartnerUserId: The user’s id in the system.
*/
fun init(appId: String, integrationPartnerUserId: String) {
this.appId = appId
this.integrationPartnerUserId = integrationPartnerUserId
val url = "${baseUrl}?appId=${appId}&integrationPartnerUserId=${integrationPartnerUserId}&platform=android&version=${version}"
Log.i(TAG, "load url ${url}")
this.initStatus = InitStatus.Initialising
this.webview.loadUrl(url)
}

private fun configureWebView(): CereModule {
val webview = WebView(context)
webview.settings.javaScriptEnabled = true
webview.settings.domStorageEnabled = true
webview.settings.databaseEnabled = true
WebView.setWebContentsDebuggingEnabled(true)

webview.addJavascriptInterface(this, "Android")
this.webview = webview
return this
}

/**
* Send event to RXB.
* @param eventType: Type of event. For example `APP_LAUNCHED`.
* @param payload: Optional parameter which can be passed with event. It should contain serialised json payload associated with eventType.
*/
fun sendEvent(eventType: String, payload: String = "") {
if (this.initStatus == InitStatus.Initialised) {
val script = """
(async function() {
console.log('send event dialog');
return cereSDK.sendEvent('${eventType}', ${payload}).
then(() => {
console.log(`event ${eventType} sent`);
}).
catch(err => {
console.log(`${eventType} sending error` + err);
});
})();""".trimIndent()

val handler = Handler(Looper.getMainLooper())

//todo: remove postDelayed after event queue is added to sdk
handler.postDelayed({
Log.e(TAG, "evaluate send event javascript")
webview.evaluateJavascript(script)
{
Log.i(TAG, "send event $eventType executed")
}
}, 3000)
}
}

@JavascriptInterface
fun engagementReceived() {
Log.i(TAG, "engagement received on android")
val intent = Intent(context, WebviewActivity::class.java)
context.startActivity(intent)
}

@JavascriptInterface
fun sdkInitialized() {
Log.i(TAG, "sdk initialised")
this.initStatus = InitStatus.Initialised
onInitializationFinishedHandler.handle()
}

@JavascriptInterface
fun sdkInitializedError(error: String) {
Log.i(TAG, "sdk initialise error: $error")
this.initStatus = InitStatus.InitialiseError(error)
onInitializationErrorHandler.handle(error)
}
}
8 changes: 8 additions & 0 deletions cere_sdk/src/main/java/io/cere/cere_sdk/InitStatus.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.cere.cere_sdk

sealed class InitStatus {
object Uninitialised : InitStatus()
object Initialising : InitStatus()
object Initialised : InitStatus()
data class InitialiseError(@JvmField val error: String) : InitStatus()
}
Loading

0 comments on commit c59dc40

Please sign in to comment.