Skip to content

Commit

Permalink
Add optional tenant configuration for private Applivery instances
Browse files Browse the repository at this point in the history
  • Loading branch information
imablanco committed Nov 18, 2024
1 parent 673ab15 commit d2cb57f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ import com.applivery.base.domain.model.AppData
object AppliveryDataManager {
var appData: AppData? = null
var appToken: String? = null
var tenant: String? = null
}
14 changes: 8 additions & 6 deletions applivery-data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ apply from: '../dependencies.gradle'
apply from: rootProject.file('gradle/checkstyle.gradle')

def STRING = "String"
def tenantPlaceholder = "{tenant}"

ext {
PUBLISH_ARTIFACT_ID = 'applivery-data'
Expand All @@ -42,22 +43,23 @@ android {
buildConfigField STRING, "LibraryVersion", "\"$PUBLISH_VERSION\""
buildConfigField STRING, "MinSdk", "\"$versions.minSdk\""
buildConfigField STRING, "TargetSdk", "\"$versions.targetSdk\""
buildConfigField STRING, "TenantPlaceholder", "\"$tenantPlaceholder\""
}

buildTypes {
debug {
buildConfigField STRING, "API_URL", '"https://sdk-api.applivery.io"'
buildConfigField STRING, "DOWNLOAD_API_URL", '"https://sdk-api.applivery.io"'
buildConfigField STRING, "API_URL", "\"https://sdk-api.${tenantPlaceholder}applivery.io/\""
buildConfigField STRING, "DOWNLOAD_API_URL", "\"https://sdk-api.${tenantPlaceholder}applivery.io/\""
}

prerelease {
buildConfigField STRING, "API_URL", '"https://sdk-api.applivery.io"'
buildConfigField STRING, "DOWNLOAD_API_URL", '"https://download-api.applivery.io/"'
buildConfigField STRING, "API_URL", "\"https://sdk-api.${tenantPlaceholder}applivery.io/\""
buildConfigField STRING, "DOWNLOAD_API_URL", "\"https://download-api.${tenantPlaceholder}applivery.io/\""
}

release {
buildConfigField STRING, "API_URL", '"https://sdk-api.applivery.io"'
buildConfigField STRING, "DOWNLOAD_API_URL", '"https://download-api.applivery.io/"'
buildConfigField STRING, "API_URL", "\"https://sdk-api.${tenantPlaceholder}applivery.io/\""
buildConfigField STRING, "DOWNLOAD_API_URL", "\"https://download-api.${tenantPlaceholder}applivery.io/\""

minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
Expand Down
10 changes: 10 additions & 0 deletions applivery-data/src/main/java/com/applivery/data/ApiUriBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.applivery.data

internal object ApiUriBuilder {

private const val TenantPlaceholder = BuildConfig.TenantPlaceholder

fun String.buildUponTenant(tenant: String? = null): String {
return replace(TenantPlaceholder, tenant?.let { "$it." }.orEmpty())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.applivery.data

import com.applivery.base.AppliveryDataManager
import com.applivery.data.ApiUriBuilder.buildUponTenant
import com.applivery.data.di.InjectorUtils
import com.applivery.data.request.BindUserRequest
import com.applivery.data.request.FeedbackRequest
Expand Down Expand Up @@ -67,7 +69,8 @@ interface AppliveryApiService {
}

private fun provideAppliveryApiService(): AppliveryApiService {
return Retrofit.Builder().baseUrl(BuildConfig.API_URL)
val url = BuildConfig.API_URL.buildUponTenant(AppliveryDataManager.tenant)
return Retrofit.Builder().baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.client(InjectorUtils.provideOkHttpClient())
.build()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.applivery.data

import com.applivery.base.AppliveryDataManager
import com.applivery.data.ApiUriBuilder.buildUponTenant
import com.applivery.data.di.InjectorUtils
import okhttp3.ResponseBody
import retrofit2.Call
Expand Down Expand Up @@ -29,7 +31,8 @@ interface DownloadApiService {
}

private fun provideDownloadApiService(): DownloadApiService {
return Retrofit.Builder().baseUrl(BuildConfig.DOWNLOAD_API_URL)
val url = BuildConfig.DOWNLOAD_API_URL.buildUponTenant(AppliveryDataManager.tenant)
return Retrofit.Builder().baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.client(InjectorUtils.provideOkHttpClient())
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* Date 17/1/16.
*/
public class Applivery {

/**
* Initializes Sdk for the current app and developer. Call this method from your application
* instance when onCreate method is called. Pay attention to description of isPlayStoreRelease
Expand All @@ -38,7 +39,21 @@ public class Applivery {
* app settings section
*/
public static void init(@NonNull Application app, @NonNull String appToken) {
AppliverySdk.sdkInitialize(app, appToken);
AppliverySdk.sdkInitialize(app, appToken, null);
}

/**
* Initializes Sdk for the current app and developer. Call this method from your application
* instance when onCreate method is called. Pay attention to description of isPlayStoreRelease
* param.
*
* @param app your app instance, it can't be null.
* @param appToken your app tokenoken. You can find this value at applivery dashboard in your
* app settings section
* @param tenant tenant for private Applivery instances
*/
public static void init(@NonNull Application app, @NonNull String appToken, @NonNull String tenant) {
AppliverySdk.sdkInitialize(app, appToken, tenant);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
package com.applivery.applvsdklib;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.applivery.applvsdklib.domain.exceptions.NotForegroundActivityAvailable;
import com.applivery.applvsdklib.domain.login.BindUserInteractor;
import com.applivery.applvsdklib.domain.login.GetUserProfileInteractor;
Expand All @@ -46,8 +47,9 @@
import com.applivery.base.AppliveryLifecycleCallbacks;
import com.applivery.base.domain.model.UserData;
import com.applivery.base.domain.model.UserProfile;

import java.util.Collection;
import java.util.concurrent.Executor;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;
Expand All @@ -61,9 +63,6 @@ public class AppliverySdk {
// TODO This class is already using too many Static fields, consider redesign.
// TODO Hold static reference only to AppliverySdk object and wrap smaller objects inside
private static final String TAG = AppliverySdk.class.getCanonicalName();
private static volatile Executor executor;
private static volatile String appToken;
private static volatile String fileProviderAuthority;
private static boolean lockedApp = false;
private static volatile boolean isDebugEnabled = BuildConfig.DEBUG;
private static Context applicationContext;
Expand All @@ -75,15 +74,14 @@ public class AppliverySdk {
private static Boolean checkForUpdatesBackground = BuildConfig.CHECK_FOR_UPDATES_BACKGROUND;
private static Boolean isUpdating = false;

public static synchronized void sdkInitialize(Application app, String appToken) {
init(app, appToken);
public static synchronized void sdkInitialize(Application app, String appToken, String tenant) {
init(app, appToken, tenant);
}

@TargetApi(14)
private static void init(Application app, String appToken) {
private static void init(Application app, String appToken, String tenant) {
if (!sdkInitialized) {
sdkInitialized = true;
initializeAppliveryConstants(app, appToken);
initializeAppliveryConstants(app, appToken, tenant);
app.registerActivityLifecycleCallbacks(new AppliveryLifecycleCallbacks());
registerActivityLifecyleCallbacks(app);
obtainAppConfig(false);
Expand All @@ -93,7 +91,6 @@ private static void init(Application app, String appToken) {
/**
* @return true if success false otherwise
*/
@TargetApi(14)
private static boolean registerActivityLifecyleCallbacks(Application app) {
try {
app.registerActivityLifecycleCallbacks(activityLifecycle);
Expand All @@ -111,24 +108,21 @@ public static synchronized Boolean isUpdating() {
return isUpdating;
}

private static void initializeAppliveryConstants(Application app, String appToken) {
private static void initializeAppliveryConstants(Application app, String appToken, String tenant) {

//region validate some requirements
Context applicationContext = Validate.notNull(app, "Application").getApplicationContext();
Validate.notNull(applicationContext, "applicationContext");
Validate.hasInternetPermissions(applicationContext, false);
//endregion

AppliverySdk.appToken = appToken;
AppliveryDataManager.INSTANCE.setAppToken(appToken);

AppliverySdk.fileProviderAuthority = composeFileProviderAuthority(app);
AppliveryDataManager.INSTANCE.setTenant(tenant);

AppliverySdk.applicationContext = applicationContext;

AppliverySdk.activityLifecycle = new AppliveryActivityLifecycleCallbacks(applicationContext);
AppliverySdk.permissionRequestManager =
new AndroidPermissionCheckerImpl(AppliverySdk.activityLifecycle);
AppliverySdk.permissionRequestManager = new AndroidPermissionCheckerImpl(AppliverySdk.activityLifecycle);
}

private static void obtainAppConfig(boolean checkForUpdates) {
Expand All @@ -138,10 +132,6 @@ private static void obtainAppConfig(boolean checkForUpdates) {
}
}

private static String composeFileProviderAuthority(Application application) {
return application.getPackageName() + ".provider";
}

@Nullable
public static Context getApplicationContext() {
return applicationContext;
Expand Down

0 comments on commit d2cb57f

Please sign in to comment.