// YetiPoll for Android. Drop this file into your app. // // YetiPoll.open(context, appKey = "yp_live_pk_...", userId = currentUser.id) // // Pass `signature` when your app requires signed wall links: compute it on // your server (HMAC-SHA256 of the user id with your secret key). package com.yetipoll.sdk import android.annotation.SuppressLint import android.app.Activity import android.content.Context import android.content.Intent import android.net.Uri import android.os.Bundle import android.webkit.WebResourceRequest import android.webkit.WebView import android.webkit.WebViewClient object YetiPoll { private const val HOST = "yetipoll.com" fun wallUrl(appKey: String, userId: String, signature: String? = null): String { val builder = Uri.Builder().scheme("https").authority(HOST).path("wall") .appendQueryParameter("app", appKey) .appendQueryParameter("user", userId) if (!signature.isNullOrEmpty()) builder.appendQueryParameter("sig", signature) return builder.build().toString() } fun open(context: Context, appKey: String, userId: String, signature: String? = null) { val intent = Intent(context, YetiPollActivity::class.java) .putExtra("url", wallUrl(appKey, userId, signature)) if (context !is Activity) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) context.startActivity(intent) } } // Register in AndroidManifest.xml: // class YetiPollActivity : Activity() { private lateinit var web: WebView @SuppressLint("SetJavaScriptEnabled") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) web = WebView(this) web.settings.javaScriptEnabled = true web.settings.domStorageEnabled = true web.settings.setSupportMultipleWindows(false) web.webViewClient = object : WebViewClient() { // The wall stays in the app; surveys open in the user's browser. override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { val url = request.url val onWall = url.host == "yetipoll.com" && !(url.path ?: "").startsWith("/wall/go/") if (onWall) return false startActivity(Intent(Intent.ACTION_VIEW, url)) return true } } setContentView(web) web.loadUrl(intent.getStringExtra("url") ?: return finish()) } override fun onResume() { super.onResume() web.reload() // fresh surveys when the user comes back from one } @Deprecated("Deprecated in Java") override fun onBackPressed() { if (web.canGoBack()) web.goBack() else super.onBackPressed() } }