Giter VIP home page Giter VIP logo

android_traffic_aggregator_example's Introduction

Android Traffic Aggregator SDK example app

This repository holds Firework Android Traffic Aggregator SDK Example Application designed for an easier integration experience. This document provides step-by-step guidance for Android Traffic Aggregator SDK integration.

Download

The SDK is available on Jitpack repository. You have to first add this repository to your project if you don't have it already:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

and then add this dependency to your application gradle:

// Firework Android Traffic Aggregator SDK
val fireworkSdkVersion = "1.0.0-beta.6"
implementation("com.firework.embed:traffic-aggregator:$fireworkSdkVersion")

Min requirements

The min Android API supported by the SDK is 21 and it is targeting API 33.

compileSdk = 31
minSdk = 19
targetSdk = 31

SDK initialization

The SDK has an initialization phase that should be added to a place like the app Application class.

import android.app.Application
import android.util.Log
import android.widget.Toast
import com.firework.sdk.trafficaggregator.FwTrafficAggregatorSdk
import com.firework.sdk.trafficaggregator.FwTrafficAggregatorSdkConfig

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        val sdkConfig = FwTrafficAggregatorSdkConfig.Builder(context = this)
            .setClientId(BuildConfig.FW_CLIENT_ID)
            .setUserId(BuildConfig.FW_USER_ID)
            .build()

        FwTrafficAggregatorSdk.init(
            sdkConfig = sdkConfig,
            onSuccess = {
                Log.i(TAG, "Firework Traffic Aggregator SDK initialized successfully")
            },
            onError = { error ->
                Log.e(TAG, "Something went wrong with Firework Traffic Aggregator SDK initialization: ${error.message}")
                Toast.makeText(this, "SDK initialization error:\n${error.message}", Toast.LENGTH_LONG).show()
            },
        )
    }

    companion object {
        const val TAG = "Firework SDK"
    }
}

The FwTrafficAggregatorSdk init method requires a configuration that can be generated using FwTrafficAggregatorSdkConfig.Builder.

You should set your clientId and optional hostUserID. The hostUserID is the user ID from your app which can be used for fetching the OAuth authentication token. You can also update the hostUserId at runtime in case of user login or logout using FwTrafficAggregatorSdk.updateHostUserId method.

The FwTrafficAggregatorSdk.init function gets two callbacks for success and error cases. They can be used for debugging, error handling, and for informing the host app about successful SDK initialization.

View initialization

Whenever you are ready to open the Boutir URL, you can initialize this view and add it to your layout programmatically or make it visible if you already had it in your XML layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.firework.sdk.trafficaggregator.FwTrafficAggregatorView
        android:id="@+id/trafficAggregatorView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

This view requires initialization and it will inform success/error cases for error handling.

binding.trafficAggregatorView.init(
    aggregatorUrl = URL,
    paymentHandler = this,
    onSuccess = {
        Log.i(TAG, "Firework Traffic Aggregator View initialized successfully")
    },
    onError = { error ->
        Log.e(TAG, "Something went wrong with Firework Traffic Aggregator View initialization: ${error.message}")
        Toast.makeText(this, "View initialization error:\n${error.message}", Toast.LENGTH_LONG).show()
    },
)

The TrafficAggregatorView.init method receives the aggregator URL, a payment handler callback, and success/error callbacks for debugging and error handling.

Payment callback

Whenever it is time for the Octopus API to handle the payment, the TrafficAggregatorView will call the handlePayment method of the PaymentHandler callback and provide the payment URL. Also, this method provides onPaymentDone function which should be called as soon as the payment is done, with a PaymentResult holding a confirmation URL which is later shown on the TrafficAggregatorView.

override fun handlePayment(paymentUrl: String, onPaymentDone: (PaymentResult) -> Unit) {
    // handle the payment and call [onPaymentDone] with proper URL
    AlertDialog.Builder(this)
        .setTitle(R.string.payment_result_title)
        .setMessage(R.string.payment_result_message)
        .setPositiveButton(R.string.yes) { _, _ ->
            onPaymentDone(PaymentResult(confirmationUrl = SUCCESS_URL))
        }
        .setNegativeButton(R.string.no) { _, _ ->
            onPaymentDone(PaymentResult(confirmationUrl = FAILURE_URL))
        }
        .show()
}

Proguard

The SDK contains a consumer Gradle rule which makes sure everything works fine after the host app minification and obfuscation, and no extra rules is required.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.