Skip to content

Commit

Permalink
feat: ❇️ added android support using middleware-android as upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
Archish27 committed Jan 25, 2024
1 parent e44cf80 commit edfea8b
Show file tree
Hide file tree
Showing 23 changed files with 1,400 additions and 13 deletions.
5 changes: 5 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,10 @@ dependencies {
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation 'io.github.middleware-labs:android-sdk:1.0.7'
implementation 'io.opentelemetry.android:instrumentation:+'
implementation 'io.opentelemetry:opentelemetry-sdk:+'
implementation 'io.opentelemetry.semconv:opentelemetry-semconv:+'
implementation 'io.opentelemetry:opentelemetry-exporter-otlp:+'
}

2 changes: 2 additions & 0 deletions android/src/main/AndroidManifestNew.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.middlewarereactnative;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

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

public class ActivityCallbacks implements Application.ActivityLifecycleCallbacks {
private boolean isRegisteredForLifecycleCallbacks;

public void registerActivityLifecycleCallbacks(Context context) {
// Make sure the callback is registered only once.
if (isRegisteredForLifecycleCallbacks) {
return;
}
Context appContext = context.getApplicationContext();
if (appContext instanceof Application) {
((Application) appContext).registerActivityLifecycleCallbacks(this);
isRegisteredForLifecycleCallbacks = true;
}
}

@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
boolean isColdStart = savedInstanceState == null;
Log.d("MiddlewareReactNative", "onActivityCreated: " + isColdStart);
AppStartTracker.getInstance().setColdStart(isColdStart);
}

@Override
public void onActivityStarted(@NonNull Activity activity) {

}

@Override
public void onActivityResumed(@NonNull Activity activity) {
String simpleName = activity.getClass().getSimpleName();
Log.d("MiddlewareReactNative", "onActivityResumed: " + simpleName);
}

@Override
public void onActivityPaused(@NonNull Activity activity) {

}

@Override
public void onActivityStopped(@NonNull Activity activity) {

}

@Override
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

}

@Override
public void onActivityDestroyed(@NonNull Activity activity) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.middlewarereactnative;

import org.jetbrains.annotations.NotNull;

public class AppStartTracker {
private static final @NotNull AppStartTracker instance = new AppStartTracker();

private boolean isColdStart = false;

public static @NotNull AppStartTracker getInstance() {
return instance;
}

public boolean isColdStart() {
return isColdStart;
}

public void setColdStart(boolean coldStart) {
isColdStart = coldStart;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.middlewarereactnative;

import com.facebook.react.bridge.ReadableMap;

public class ConfigMapReader extends MapReader {
private final ReadableMap map;

public ConfigMapReader(ReadableMap map) {
this.map = map;
}

public String getTarget() {
return Keys.TARGET.get(map);
}

public String getAccountKey() {
return Keys.ACCOUNT_KEY.get(map);
}


public String getProjectName() {
return Keys.PROJECT_NAME.get(map);
}

public String getServiceName() {
return Keys.SERVICE_NAME.get(map);
}

public String getDeploymentEnvironment() {
return Keys.DEPLOYMENT_ENVIRONMENT.get(map);
}

public ReadableMap getGlobalAttributes() {
return Keys.GLOBAL_ATTRIBUTES.getMap(map);
}

private interface Keys {
StringKey TARGET = new StringKey("target");
StringKey ACCOUNT_KEY = new StringKey("accountKey");
StringKey PROJECT_NAME = new StringKey("projectName");
StringKey SERVICE_NAME = new StringKey("serviceName");
StringKey DEPLOYMENT_ENVIRONMENT = new StringKey("deploymentEnvironment");
MapKey GLOBAL_ATTRIBUTES = new MapKey("globalAttributes");
}
}
70 changes: 70 additions & 0 deletions android/src/main/java/com/middlewarereactnative/MapReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.middlewarereactnative;

import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableType;

public abstract class MapReader {
protected static class StringKey {
private final String key;

public StringKey(String key) {
this.key = key;
}

public String get(ReadableMap map) {
if (map.hasKey(key) && map.getType(key) == ReadableType.String) {
return map.getString(key);
} else {
return null;
}
}
}

protected static class BooleanKey {
private final String key;

public BooleanKey(String key) {
this.key = key;
}

public Boolean get(ReadableMap map) {
if (map.hasKey(key) && map.getType(key) == ReadableType.Boolean) {
return map.getBoolean(key);
} else {
return null;
}
}
}

protected static class NumberKey {
private final String key;

public NumberKey(String key) {
this.key = key;
}

public Long getLong(ReadableMap map) {
if (map.hasKey(key) && map.getType(key) == ReadableType.Number) {
return (long) map.getDouble(key);
} else {
return null;
}
}
}

protected static class MapKey {
private final String key;

public MapKey(String key) {
this.key = key;
}

public ReadableMap getMap(ReadableMap map) {
if (map.hasKey(key) && map.getType(key) == ReadableType.Map) {
return map.getMap(key);
} else {
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.middlewarereactnative;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

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

public class MiddlewarePreferenceProvider extends ContentProvider {

private static final long appStartTime = System.currentTimeMillis();

public static long getAppStartTime() {
return appStartTime;
}

@Override
public boolean onCreate() {
Log.d("MiddlewareReactNative", "perfProvider onCreate: " + appStartTime);

ActivityCallbacks activityCallbacks = new ActivityCallbacks();
activityCallbacks.registerActivityLifecycleCallbacks(getContext());

//TODO deregister callbacks?
return false;
}

@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {
return null;
}

@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}

@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
return null;
}

@Override
public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) {
return 0;
}

@Override
public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
return 0;
}
}
Loading

0 comments on commit edfea8b

Please sign in to comment.