Skip to content

Commit c22cb2a

Browse files
add: setOnFeaturesUpdatedListener android listener and refactor instabug init method
1 parent 7b7828e commit c22cb2a

File tree

7 files changed

+106
-56
lines changed

7 files changed

+106
-56
lines changed

android/native.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
project.ext.instabug = [
2-
version: '14.0.0.6273738-SNAPSHOT'
2+
version: '14.0.0.6273368-SNAPSHOT'
33
]
44

55
dependencies {

android/src/main/java/com/instabug/reactlibrary/Constants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ final class Constants {
99

1010
final static String IBG_ON_NEW_MESSAGE_HANDLER = "IBGonNewMessageHandler";
1111
final static String IBG_ON_NEW_REPLY_RECEIVED_CALLBACK = "IBGOnNewReplyReceivedCallback";
12+
13+
final static String IBG_ON_FEATURES_UPDATED_CALLBACK = "IBGOnFeatureUpdatedCallback";
14+
final static String IBG_NETWORK_LOGGER_HANDLER = "IBGNetworkLoggerHandler";
1215
}

android/src/main/java/com/instabug/reactlibrary/RNInstabugNetworkLoggerModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void run() {
130130
networkSnapshotParams.putMap("responseHeader", convertFromMapToWriteableMap(networkLogSnapshot.getResponseHeaders()));
131131
}
132132

133-
sendEvent("IBGNetworkLoggerHandler", networkSnapshotParams);
133+
sendEvent(Constants.IBG_NETWORK_LOGGER_HANDLER, networkSnapshotParams);
134134
});
135135
}
136136
});

android/src/main/java/com/instabug/reactlibrary/RNInstabugReactnativeModule.java

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.instabug.reactlibrary;
22

3+
import static com.instabug.apm.configuration.cp.APMFeature.APM_NETWORK_PLUGIN_INSTALLED;
4+
import static com.instabug.apm.configuration.cp.APMFeature.CP_NATIVE_INTERCEPTION_ENABLED;
35
import static com.instabug.reactlibrary.utils.InstabugUtil.getMethod;
46

57
import android.app.Application;
@@ -22,6 +24,7 @@
2224
import com.facebook.react.bridge.WritableNativeArray;
2325
import com.facebook.react.bridge.WritableNativeMap;
2426
import com.facebook.react.uimanager.UIManagerModule;
27+
import com.instabug.apm.InternalAPM;
2528
import com.instabug.library.Feature;
2629
import com.instabug.library.Instabug;
2730
import com.instabug.library.InstabugColorTheme;
@@ -31,13 +34,14 @@
3134
import com.instabug.library.ReproConfigurations;
3235
import com.instabug.library.core.InstabugCore;
3336
import com.instabug.library.featuresflags.model.IBGFeatureFlag;
37+
import com.instabug.library.internal.crossplatform.InternalCore;
38+
import com.instabug.library.internal.crossplatform.OnFeaturesUpdatedListener;
3439
import com.instabug.library.internal.module.InstabugLocale;
3540
import com.instabug.library.invocation.InstabugInvocationEvent;
3641
import com.instabug.library.logging.InstabugLog;
3742
import com.instabug.library.model.NetworkLog;
3843
import com.instabug.library.model.Report;
3944
import com.instabug.library.ui.onboarding.WelcomeMessage;
40-
import com.instabug.library.util.InstabugSDKLogger;
4145
import com.instabug.reactlibrary.utils.ArrayUtil;
4246
import com.instabug.reactlibrary.utils.EventEmitterModule;
4347
import com.instabug.reactlibrary.utils.MainThreadHandler;
@@ -104,6 +108,7 @@ public void removeListeners(Integer count) {
104108

105109
/**
106110
* Enables or disables Instabug functionality.
111+
*
107112
* @param isEnabled A boolean to enable/disable Instabug.
108113
*/
109114
@ReactMethod
@@ -112,7 +117,7 @@ public void setEnabled(final boolean isEnabled) {
112117
@Override
113118
public void run() {
114119
try {
115-
if(isEnabled)
120+
if (isEnabled)
116121
Instabug.enable();
117122
else
118123
Instabug.disable();
@@ -125,10 +130,11 @@ public void run() {
125130

126131
/**
127132
* Initializes the SDK.
128-
* @param token The token that identifies the app. You can find it on your dashboard.
133+
*
134+
* @param token The token that identifies the app. You can find it on your dashboard.
129135
* @param invocationEventValues The events that invoke the SDK's UI.
130-
* @param logLevel The level of detail in logs that you want to print.
131-
* @param codePushVersion The Code Push version to be used for all reports.
136+
* @param logLevel The level of detail in logs that you want to print.
137+
* @param codePushVersion The Code Push version to be used for all reports.
132138
*/
133139
@ReactMethod
134140
public void init(
@@ -154,8 +160,8 @@ public void run() {
154160
.setInvocationEvents(invocationEvents)
155161
.setLogLevel(parsedLogLevel);
156162

157-
if(codePushVersion != null) {
158-
if(Instabug.isBuilt()) {
163+
if (codePushVersion != null) {
164+
if (Instabug.isBuilt()) {
159165
Instabug.setCodePushVersion(codePushVersion);
160166
} else {
161167
builder.setCodePushVersion(codePushVersion);
@@ -321,7 +327,7 @@ public void run() {
321327
*
322328
* @param userEmail User's default email
323329
* @param userName Username.
324-
* @param userId User's ID
330+
* @param userId User's ID
325331
*/
326332
@ReactMethod
327333
public void identifyUser(
@@ -741,15 +747,15 @@ public void addFileAttachmentWithDataToReport(String data, String fileName) {
741747

742748
private WritableMap convertFromHashMapToWriteableMap(HashMap hashMap) {
743749
WritableMap writableMap = new WritableNativeMap();
744-
for(int i = 0; i < hashMap.size(); i++) {
750+
for (int i = 0; i < hashMap.size(); i++) {
745751
Object key = hashMap.keySet().toArray()[i];
746752
Object value = hashMap.get(key);
747-
writableMap.putString((String) key,(String) value);
753+
writableMap.putString((String) key, (String) value);
748754
}
749755
return writableMap;
750756
}
751757

752-
private static JSONObject objectToJSONObject(Object object){
758+
private static JSONObject objectToJSONObject(Object object) {
753759
Object json = null;
754760
JSONObject jsonObject = null;
755761
try {
@@ -766,13 +772,12 @@ private static JSONObject objectToJSONObject(Object object){
766772
private WritableArray convertArrayListToWritableArray(List arrayList) {
767773
WritableArray writableArray = new WritableNativeArray();
768774

769-
for(int i = 0; i < arrayList.size(); i++) {
775+
for (int i = 0; i < arrayList.size(); i++) {
770776
Object object = arrayList.get(i);
771777

772-
if(object instanceof String) {
778+
if (object instanceof String) {
773779
writableArray.pushString((String) object);
774-
}
775-
else {
780+
} else {
776781
JSONObject jsonObject = objectToJSONObject(object);
777782
writableArray.pushMap((WritableMap) jsonObject);
778783
}
@@ -828,7 +833,7 @@ public void run() {
828833
* Shows the welcome message in a specific mode.
829834
*
830835
* @param welcomeMessageMode An enum to set the welcome message mode to
831-
* live, or beta.
836+
* live, or beta.
832837
*/
833838
@ReactMethod
834839
public void showWelcomeMessageWithMode(final String welcomeMessageMode) {
@@ -850,7 +855,7 @@ public void run() {
850855
* Sets the welcome message mode to live, beta or disabled.
851856
*
852857
* @param welcomeMessageMode An enum to set the welcome message mode to
853-
* live, beta or disabled.
858+
* live, beta or disabled.
854859
*/
855860
@ReactMethod
856861
public void setWelcomeMessageMode(final String welcomeMessageMode) {
@@ -985,7 +990,6 @@ public void run() {
985990
* Reports that the screen name been changed (Current View).
986991
*
987992
* @param screenName string containing the screen name
988-
*
989993
*/
990994
@ReactMethod
991995
public void reportCurrentViewChange(final String screenName) {
@@ -1008,7 +1012,6 @@ public void run() {
10081012
* Reports that the screen has been changed (Repro Steps) the screen sent to this method will be the 'current view' on the dashboard
10091013
*
10101014
* @param screenName string containing the screen name
1011-
*
10121015
*/
10131016
@ReactMethod
10141017
public void reportScreenChange(final String screenName) {
@@ -1018,7 +1021,7 @@ public void run() {
10181021
try {
10191022
Method method = getMethod(Class.forName("com.instabug.library.Instabug"), "reportScreenChange", Bitmap.class, String.class);
10201023
if (method != null) {
1021-
method.invoke(null , null, screenName);
1024+
method.invoke(null, null, screenName);
10221025
}
10231026
} catch (Exception e) {
10241027
e.printStackTrace();
@@ -1112,7 +1115,7 @@ public void removeFeatureFlags(final ReadableArray featureFlags) {
11121115
@Override
11131116
public void run() {
11141117
try {
1115-
ArrayList<String> stringArray = ArrayUtil.parseReadableArrayOfStrings(featureFlags);
1118+
ArrayList<String> stringArray = ArrayUtil.parseReadableArrayOfStrings(featureFlags);
11161119
Instabug.removeFeatureFlag(stringArray);
11171120
} catch (Exception e) {
11181121
e.printStackTrace();
@@ -1153,7 +1156,7 @@ public void run() {
11531156
* Map between the exported JS constant and the arg key in {@link ArgsRegistry}.
11541157
* The constant name and the arg key should match to be able to resolve the
11551158
* constant with its actual value from the {@link ArgsRegistry} maps.
1156-
*
1159+
* <p>
11571160
* This is a workaround, because RN cannot resolve enums in the constants map.
11581161
*/
11591162
@Override
@@ -1167,4 +1170,20 @@ public Map<String, Object> getConstants() {
11671170

11681171
return constants;
11691172
}
1173+
1174+
@ReactMethod
1175+
public void setOnFeaturesUpdatedListener() {
1176+
InternalCore.INSTANCE._setOnFeaturesUpdatedListener(new OnFeaturesUpdatedListener() {
1177+
@Override
1178+
public void invoke() {
1179+
final boolean cpNativeInterceptionEnabled = InternalAPM._isFeatureEnabledCP(CP_NATIVE_INTERCEPTION_ENABLED, "");
1180+
final boolean hasAPMPlugin = InternalAPM._isFeatureEnabledCP(APM_NETWORK_PLUGIN_INSTALLED, "");
1181+
1182+
WritableMap params = Arguments.createMap();
1183+
params.putBoolean("cpNativeInterceptionEnabled", cpNativeInterceptionEnabled);
1184+
params.putBoolean("hasAPMPlugin", hasAPMPlugin);
1185+
sendEvent(Constants.IBG_ON_FEATURES_UPDATED_CALLBACK, params);
1186+
}
1187+
});
1188+
}
11701189
}

examples/default/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ buildscript {
2424
classpath("com.android.tools.build:gradle:8.1.0")
2525
classpath("com.facebook.react:react-native-gradle-plugin")
2626
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
27-
classpath("com.instabug.library:instabug-plugin:14.0.0.6273738-SNAPSHOT")
27+
classpath("com.instabug.library:instabug-plugin:14.0.0.6273368-SNAPSHOT")
2828
}
2929
}
3030

src/modules/Instabug.ts

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,15 @@ function _logFlags() {
9999
* @param config SDK configurations. See {@link InstabugConfig} for more info.
100100
*/
101101
export const init = async (config: InstabugConfig) => {
102-
// Initialize necessary variables
103-
isNativeInterceptionFeatureEnabled = await NativeNetworkLogger.isNativeInterceptionEnabled();
104102
if (Platform.OS === 'android') {
105-
hasAPMNetworkPlugin = await NativeNetworkLogger.hasAPMNetworkPlugin();
106-
shouldEnableNativeInterception =
107-
config.networkInterceptionMode === NetworkInterceptionMode.native;
108-
}
103+
// Add android feature flags listener for android
104+
addOnFeatureUpdatedListener(config);
105+
} else {
106+
isNativeInterceptionFeatureEnabled = await NativeNetworkLogger.isNativeInterceptionEnabled();
109107

110-
// Add app state listener to handle background/foreground transitions
111-
addAppStateListener(async (nextAppState) => handleAppStateChange(nextAppState, config));
108+
// Add app state listener to handle background/foreground transitions
109+
addAppStateListener(async (nextAppState) => handleAppStateChange(nextAppState, config));
110+
}
112111

113112
// Perform platform-specific checks and update interception mode
114113
handleNetworkInterceptionMode(config);
@@ -148,20 +147,12 @@ export const init = async (config: InstabugConfig) => {
148147
const handleAppStateChange = async (nextAppState: AppStateStatus, config: InstabugConfig) => {
149148
// Checks if the app has come to the foreground
150149
if (['inactive', 'background'].includes(_currentAppState) && nextAppState === 'active') {
151-
// Update the APM network flags
152150
const isUpdated = await fetchApmNetworkFlags();
153151

154152
if (isUpdated) {
155153
console.log('Andrew: App has come to the foreground!');
156154
console.log('Andrew: APM network flags updated.');
157-
handleNetworkInterceptionMode(config);
158-
handleIOSNativeInterception(config);
159-
_logFlags();
160-
setApmNetworkFlagsIfChanged({
161-
isNativeInterceptionFeatureEnabled,
162-
hasAPMNetworkPlugin,
163-
shouldEnableNativeInterception,
164-
});
155+
refreshAPMNetworkConfigs(config);
165156
}
166157
}
167158

@@ -251,22 +242,20 @@ function handleAndroidNativeInterception() {
251242
* Control either to enable or disable the native interception for iOS.
252243
*/
253244
function handleIOSNativeInterception(config: InstabugConfig) {
254-
if (Platform.OS === 'ios') {
255-
console.log(
256-
`Andrew: handleIOSNativeInterception(${
257-
shouldEnableNativeInterception &&
258-
config.networkInterceptionMode === NetworkInterceptionMode.native
259-
})`,
260-
);
261-
262-
if (
245+
console.log(
246+
`Andrew: handleIOSNativeInterception(${
263247
shouldEnableNativeInterception &&
264248
config.networkInterceptionMode === NetworkInterceptionMode.native
265-
) {
266-
NativeNetworkLogger.forceStartNetworkLoggingIOS(); // Enable native iOS automatic network logging.
267-
} else {
268-
NativeNetworkLogger.forceStopNetworkLoggingIOS(); // Disable native iOS automatic network logging.
269-
}
249+
})`,
250+
);
251+
252+
if (
253+
shouldEnableNativeInterception &&
254+
config.networkInterceptionMode === NetworkInterceptionMode.native
255+
) {
256+
NativeNetworkLogger.forceStartNetworkLoggingIOS(); // Enable native iOS automatic network logging.
257+
} else {
258+
NativeNetworkLogger.forceStopNetworkLoggingIOS(); // Disable native iOS automatic network logging.
270259
}
271260
}
272261

@@ -323,6 +312,42 @@ const initializeNativeInstabug = (config: InstabugConfig) => {
323312
);
324313
};
325314

315+
/**
316+
* Refresh the APM network configurations.
317+
*/
318+
function refreshAPMNetworkConfigs(config: InstabugConfig) {
319+
handleNetworkInterceptionMode(config);
320+
if (Platform.OS === 'ios') {
321+
handleIOSNativeInterception(config);
322+
}
323+
_logFlags();
324+
setApmNetworkFlagsIfChanged({
325+
isNativeInterceptionFeatureEnabled,
326+
hasAPMNetworkPlugin,
327+
shouldEnableNativeInterception,
328+
});
329+
}
330+
331+
/**
332+
* Add Android Listener for native feature flags changes.
333+
*/
334+
function addOnFeatureUpdatedListener(config: InstabugConfig) {
335+
emitter.addListener(NativeEvents.IBG_ON_FEATURES_UPDATED_CALLBACK, (flags) => {
336+
const { cpNativeInterceptionEnabled, hasAPMPlugin } = flags;
337+
console.log(`Andrew: addOnFeatureUpdatedListener ->
338+
isNativeInterceptionFeatureEnabled: ${cpNativeInterceptionEnabled},
339+
hasAPMNetworkPlugin: ${hasAPMPlugin}.
340+
`);
341+
isNativeInterceptionFeatureEnabled = cpNativeInterceptionEnabled;
342+
hasAPMNetworkPlugin = hasAPMPlugin;
343+
shouldEnableNativeInterception =
344+
config.networkInterceptionMode === NetworkInterceptionMode.native;
345+
refreshAPMNetworkConfigs(config);
346+
});
347+
348+
NativeInstabug.setOnFeaturesUpdatedListener();
349+
}
350+
326351
/**
327352
* Sets the Code Push version to be sent with each report.
328353
* @param version the Code Push version.

src/native/NativeInstabug.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,15 @@ export interface InstabugNativeModule extends NativeModule {
140140
addFileAttachmentWithURLToReport(url: string, filename?: string): void;
141141
addFileAttachmentWithDataToReport(data: string, filename?: string): void;
142142
willRedirectToStore(): void;
143+
144+
setOnFeaturesUpdatedListener(handler?: (params: any) => void): void; // android only
143145
}
144146

145147
export const NativeInstabug = NativeModules.Instabug;
146148

147149
export enum NativeEvents {
148150
PRESENDING_HANDLER = 'IBGpreSendingHandler',
151+
IBG_ON_FEATURES_UPDATED_CALLBACK = 'IBGOnFeatureUpdatedCallback',
149152
}
150153

151154
export const emitter = new NativeEventEmitter(NativeInstabug);

0 commit comments

Comments
 (0)