Skip to content

Commit

Permalink
Merge pull request #539 from CleverTap/api-extraction
Browse files Browse the repository at this point in the history
Extract API calls from NetworkManager
  • Loading branch information
darshanclevertap authored Jan 24, 2024
2 parents 40d3416 + 0223bab commit 241a178
Show file tree
Hide file tree
Showing 20 changed files with 892 additions and 451 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.core.app.NotificationManagerCompat
import com.clevertap.android.sdk.events.EventGroup.PUSH_NOTIFICATION_VIEWED
import com.clevertap.android.sdk.task.CTExecutorFactory
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

fun Context.isPackageAndOsTargetsAbove(apiLevel: Int) =
Expand Down Expand Up @@ -272,4 +273,14 @@ fun String?.concatIfNotNull(other: String?, separator: String = ""): String? {
*/
fun Location.isValid(): Boolean {
return this.latitude in -90.0..90.0 && this.longitude in -180.0..180.0
}
}

fun String?.toJsonOrNull(): JSONObject? {
return this?.let {
try {
JSONObject(it)
} catch (e: JSONException) {
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,7 @@ public void setSCDomainListener(SCDomainListener scDomainListener) {

if(coreState.getNetworkManager() != null) {
NetworkManager networkManager = (NetworkManager) coreState.getNetworkManager();
String domain = networkManager.getDomainFromPrefsOrMetadata(EventGroup.REGULAR);
String domain = networkManager.getDomain(EventGroup.REGULAR);
if(domain != null) {
scDomainListener.onSCDomainAvailable(getSCDomain(domain));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.clevertap.android.sdk.network.CompositeBatchListener;
import com.clevertap.android.sdk.network.FetchInAppListener;
import com.clevertap.android.sdk.network.NetworkManager;
import com.clevertap.android.sdk.network.api.CtApiProviderKt;
import com.clevertap.android.sdk.pushnotification.PushProviders;
import com.clevertap.android.sdk.pushnotification.work.CTWorkManager;
import com.clevertap.android.sdk.response.InAppResponse;
Expand Down Expand Up @@ -67,11 +68,13 @@ static CoreState getCoreState(Context context, CleverTapInstanceConfig cleverTap
DBManager baseDatabaseManager = new DBManager(config, ctLockManager);
coreState.setDatabaseManager(baseDatabaseManager);

CryptHandler cryptHandler = new CryptHandler(config.getEncryptionLevel(), CryptHandler.EncryptionAlgorithm.AES, config.getAccountId());
CryptHandler cryptHandler = new CryptHandler(config.getEncryptionLevel(),
CryptHandler.EncryptionAlgorithm.AES, config.getAccountId());
coreState.setCryptHandler(cryptHandler);
Task<Void> task = CTExecutorFactory.executors(config).postAsyncSafelyTask();
task.execute("migratingEncryptionLevel", () -> {
CryptUtils.migrateEncryptionLevel(context, config, cryptHandler, baseDatabaseManager.loadDBAdapter(context));
CryptUtils.migrateEncryptionLevel(context, config, cryptHandler,
baseDatabaseManager.loadDBAdapter(context));
return null;
});

Expand All @@ -84,7 +87,7 @@ static CoreState getCoreState(Context context, CleverTapInstanceConfig cleverTap
DeviceInfo deviceInfo = new DeviceInfo(context, config, cleverTapID, coreMetaData);
coreState.setDeviceInfo(deviceInfo);

CTPreferenceCache.getInstance(context,config);
CTPreferenceCache.getInstance(context, config);

BaseCallbackManager callbackManager = new CallbackManager(config, deviceInfo);
coreState.setCallbackManager(callbackManager);
Expand Down Expand Up @@ -190,6 +193,7 @@ public Void call() throws Exception {
validationResultStack,
controllerManager,
baseDatabaseManager,
CtApiProviderKt.provideDefaultTestCtApi(context, config, deviceInfo),
callbackManager,
ctLockManager,
validator,
Expand Down Expand Up @@ -282,11 +286,11 @@ public Void call() throws Exception {
LocationManager locationManager = new LocationManager(context, config, coreMetaData, baseEventQueueManager);
coreState.setLocationManager(locationManager);

CTWorkManager ctWorkManager = new CTWorkManager(context,config);
CTWorkManager ctWorkManager = new CTWorkManager(context, config);

PushProviders pushProviders = PushProviders
.load(context, config, baseDatabaseManager, validationResultStack,
analyticsManager, controllerManager,ctWorkManager);
analyticsManager, controllerManager, ctWorkManager);
coreState.setPushProviders(pushProviders);

ActivityLifeCycleManager activityLifeCycleManager = new ActivityLifeCycleManager(context, config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,20 @@ public void didShow(final Context context, CTInAppNotification inapp) {
++shownToday);
}

public void attachToHeader(final Context context, JSONObject header) {
try {
// Trigger reset for dates

header.put("imp", getIntFromPrefs(getKeyWithDeviceId(Constants.KEY_COUNTS_SHOWN_TODAY, deviceId), 0));
public int getShownTodayCount() {
return getIntFromPrefs(getKeyWithDeviceId(Constants.KEY_COUNTS_SHOWN_TODAY, deviceId), 0);
}

public JSONArray getInAppsCount(final Context context) {
try {
// tlc: [[targetID, todayCount, lifetime]]
JSONArray arr = new JSONArray();
final SharedPreferences prefs = StorageHelper
.getPreferences(context, storageKeyWithSuffix(getKeyWithDeviceId(Constants.KEY_COUNTS_PER_INAPP, deviceId)));
final Map<String, ?> all = prefs.getAll();
for (String inapp : all.keySet()) {
final Object o = all.get(inapp);
if (o instanceof String) {
final String[] parts = ((String) o).split(",");
for (Map.Entry<String,?> inapp : all.entrySet()) {
if (inapp.getValue() instanceof String) {
final String[] parts = ((String) inapp.getValue()).split(",");
if (parts.length == 2) {
JSONArray a = new JSONArray();
a.put(0, inapp);
Expand All @@ -146,10 +145,10 @@ public void attachToHeader(final Context context, JSONObject header) {
}
}
}

header.put("tlc", arr);
return arr;
} catch (Throwable t) {
Logger.v("Failed to attach FC to header", t);
Logger.v("Failed to get in apps count", t);
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.clevertap.android.sdk.network

import com.clevertap.android.sdk.events.EventGroup
import com.clevertap.android.sdk.events.EventGroup.PUSH_NOTIFICATION_VIEWED
import com.clevertap.android.sdk.events.EventGroup.REGULAR
import com.clevertap.android.sdk.events.EventGroup.VARIABLES
import org.json.JSONObject

interface NetworkHeadersListener {
Expand All @@ -20,5 +24,14 @@ enum class EndpointId(val identifier: String) {
fun fromString(identifier: String): EndpointId {
return values().find { identifier.contains(it.identifier) } ?: ENDPOINT_A1
}

@JvmStatic
fun fromEventGroup(eventGroup: EventGroup): EndpointId {
return when (eventGroup) {
PUSH_NOTIFICATION_VIEWED -> ENDPOINT_SPIKY
REGULAR -> ENDPOINT_A1
VARIABLES -> ENDPOINT_DEFINE_VARS
}
}
}
}
Loading

0 comments on commit 241a178

Please sign in to comment.