Skip to content

Commit 94b5e5b

Browse files
feat: enable/disable auto masking screenshots in rn (#1389)
* feat: enable auto masking screenshots in RN * fix: add change log * fix: add unreleased in changelog * fix: linting * fix: linting * fix: update pod lock * run automask in main thread * fix: linting * fix: linting * fix: update pod lock * fix: add change log * fix: change log lint * fix: linting * add js unit test --------- Co-authored-by: Ahmed alaa <[email protected]>
1 parent fa21904 commit 94b5e5b

File tree

16 files changed

+161
-36
lines changed

16 files changed

+161
-36
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Added
66

7+
- Add support enable/disable screenshot auto masking. ([#1389](https://github.com/Instabug/Instabug-React-Native/pull/1389))
8+
79
- Add support for BugReporting user consents. ([#1383](https://github.com/Instabug/Instabug-React-Native/pull/1383))
810

911
- Add support for xCode 16. ([#1370](https://github.com/Instabug/Instabug-React-Native/pull/1370))

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.instabug.library.invocation.util.InstabugVideoRecordingButtonPosition;
1818
import com.instabug.library.sessionreplay.model.SessionMetadata;
1919
import com.instabug.library.ui.onboarding.WelcomeMessage;
20+
import com.instabug.library.MaskingType;
2021

2122
import java.util.ArrayList;
2223
import java.util.HashMap;
@@ -60,6 +61,7 @@ static Map<String, Object> getAll() {
6061
putAll(locales);
6162
putAll(placeholders);
6263
putAll(launchType);
64+
putAll(autoMaskingTypes);
6365
putAll(userConsentActionType);
6466
}};
6567
}
@@ -260,5 +262,10 @@ static Map<String, Object> getAll() {
260262
put(SessionMetadata.LaunchType.COLD,"cold");
261263
put(SessionMetadata.LaunchType.WARM,"warm" );
262264
}};
263-
265+
public static final ArgsMap<Integer> autoMaskingTypes = new ArgsMap<Integer>() {{
266+
put("labels", MaskingType.LABELS);
267+
put("textInputs", MaskingType.TEXT_INPUTS);
268+
put("media", MaskingType.MEDIA);
269+
put("none", MaskingType.MASK_NOTHING);
270+
}};
264271
}

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

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public void setEnabled(final boolean isEnabled) {
120120
@Override
121121
public void run() {
122122
try {
123-
if(isEnabled)
123+
if (isEnabled)
124124
Instabug.enable();
125125
else
126126
Instabug.disable();
@@ -133,10 +133,11 @@ public void run() {
133133

134134
/**
135135
* Initializes the SDK.
136-
* @param token The token that identifies the app. You can find it on your dashboard.
136+
*
137+
* @param token The token that identifies the app. You can find it on your dashboard.
137138
* @param invocationEventValues The events that invoke the SDK's UI.
138-
* @param logLevel The level of detail in logs that you want to print.
139-
* @param codePushVersion The Code Push version to be used for all reports.
139+
* @param logLevel The level of detail in logs that you want to print.
140+
* @param codePushVersion The Code Push version to be used for all reports.
140141
*/
141142
@ReactMethod
142143
public void init(
@@ -162,8 +163,8 @@ public void run() {
162163
.setInvocationEvents(invocationEvents)
163164
.setLogLevel(parsedLogLevel);
164165

165-
if(codePushVersion != null) {
166-
if(Instabug.isBuilt()) {
166+
if (codePushVersion != null) {
167+
if (Instabug.isBuilt()) {
167168
Instabug.setCodePushVersion(codePushVersion);
168169
} else {
169170
builder.setCodePushVersion(codePushVersion);
@@ -329,7 +330,7 @@ public void run() {
329330
*
330331
* @param userEmail User's default email
331332
* @param userName Username.
332-
* @param userId User's ID
333+
* @param userId User's ID
333334
*/
334335
@ReactMethod
335336
public void identifyUser(
@@ -749,15 +750,15 @@ public void addFileAttachmentWithDataToReport(String data, String fileName) {
749750

750751
private WritableMap convertFromHashMapToWriteableMap(HashMap hashMap) {
751752
WritableMap writableMap = new WritableNativeMap();
752-
for(int i = 0; i < hashMap.size(); i++) {
753+
for (int i = 0; i < hashMap.size(); i++) {
753754
Object key = hashMap.keySet().toArray()[i];
754755
Object value = hashMap.get(key);
755-
writableMap.putString((String) key,(String) value);
756+
writableMap.putString((String) key, (String) value);
756757
}
757758
return writableMap;
758759
}
759760

760-
private static JSONObject objectToJSONObject(Object object){
761+
private static JSONObject objectToJSONObject(Object object) {
761762
Object json = null;
762763
JSONObject jsonObject = null;
763764
try {
@@ -774,13 +775,12 @@ private static JSONObject objectToJSONObject(Object object){
774775
private WritableArray convertArrayListToWritableArray(List arrayList) {
775776
WritableArray writableArray = new WritableNativeArray();
776777

777-
for(int i = 0; i < arrayList.size(); i++) {
778+
for (int i = 0; i < arrayList.size(); i++) {
778779
Object object = arrayList.get(i);
779780

780-
if(object instanceof String) {
781+
if (object instanceof String) {
781782
writableArray.pushString((String) object);
782-
}
783-
else {
783+
} else {
784784
JSONObject jsonObject = objectToJSONObject(object);
785785
writableArray.pushMap((WritableMap) jsonObject);
786786
}
@@ -836,7 +836,7 @@ public void run() {
836836
* Shows the welcome message in a specific mode.
837837
*
838838
* @param welcomeMessageMode An enum to set the welcome message mode to
839-
* live, or beta.
839+
* live, or beta.
840840
*/
841841
@ReactMethod
842842
public void showWelcomeMessageWithMode(final String welcomeMessageMode) {
@@ -858,7 +858,7 @@ public void run() {
858858
* Sets the welcome message mode to live, beta or disabled.
859859
*
860860
* @param welcomeMessageMode An enum to set the welcome message mode to
861-
* live, beta or disabled.
861+
* live, beta or disabled.
862862
*/
863863
@ReactMethod
864864
public void setWelcomeMessageMode(final String welcomeMessageMode) {
@@ -993,7 +993,6 @@ public void run() {
993993
* Reports that the screen name been changed (Current View).
994994
*
995995
* @param screenName string containing the screen name
996-
*
997996
*/
998997
@ReactMethod
999998
public void reportCurrentViewChange(final String screenName) {
@@ -1016,7 +1015,6 @@ public void run() {
10161015
* Reports that the screen has been changed (Repro Steps) the screen sent to this method will be the 'current view' on the dashboard
10171016
*
10181017
* @param screenName string containing the screen name
1019-
*
10201018
*/
10211019
@ReactMethod
10221020
public void reportScreenChange(final String screenName) {
@@ -1026,7 +1024,7 @@ public void run() {
10261024
try {
10271025
Method method = getMethod(Class.forName("com.instabug.library.Instabug"), "reportScreenChange", Bitmap.class, String.class);
10281026
if (method != null) {
1029-
method.invoke(null , null, screenName);
1027+
method.invoke(null, null, screenName);
10301028
}
10311029
} catch (Exception e) {
10321030
e.printStackTrace();
@@ -1120,7 +1118,7 @@ public void removeFeatureFlags(final ReadableArray featureFlags) {
11201118
@Override
11211119
public void run() {
11221120
try {
1123-
ArrayList<String> stringArray = ArrayUtil.parseReadableArrayOfStrings(featureFlags);
1121+
ArrayList<String> stringArray = ArrayUtil.parseReadableArrayOfStrings(featureFlags);
11241122
Instabug.removeFeatureFlag(stringArray);
11251123
} catch (Exception e) {
11261124
e.printStackTrace();
@@ -1156,11 +1154,12 @@ public void run() {
11561154
}
11571155
});
11581156
}
1157+
11591158
/**
11601159
* Register a listener for W3C flags value change
11611160
*/
11621161
@ReactMethod
1163-
public void registerW3CFlagsChangeListener(){
1162+
public void registerW3CFlagsChangeListener() {
11641163

11651164
MainThreadHandler.runOnMainThread(new Runnable() {
11661165
@Override
@@ -1177,8 +1176,7 @@ public void invoke(@NonNull CoreFeaturesState featuresState) {
11771176
sendEvent(Constants.IBG_ON_NEW_W3C_FLAGS_UPDATE_RECEIVED_CALLBACK, params);
11781177
}
11791178
});
1180-
}
1181-
catch (Exception e) {
1179+
} catch (Exception e) {
11821180
e.printStackTrace();
11831181
}
11841182

@@ -1189,18 +1187,17 @@ public void invoke(@NonNull CoreFeaturesState featuresState) {
11891187

11901188

11911189
/**
1192-
* Get first time Value of W3ExternalTraceID flag
1190+
* Get first time Value of W3ExternalTraceID flag
11931191
*/
11941192
@ReactMethod
1195-
public void isW3ExternalTraceIDEnabled(Promise promise){
1193+
public void isW3ExternalTraceIDEnabled(Promise promise) {
11961194

11971195
MainThreadHandler.runOnMainThread(new Runnable() {
11981196
@Override
11991197
public void run() {
12001198
try {
12011199
promise.resolve(InternalCore.INSTANCE._isFeatureEnabled(CoreFeature.W3C_EXTERNAL_TRACE_ID));
1202-
}
1203-
catch (Exception e) {
1200+
} catch (Exception e) {
12041201
e.printStackTrace();
12051202
promise.resolve(false);
12061203
}
@@ -1212,18 +1209,17 @@ public void run() {
12121209

12131210

12141211
/**
1215-
* Get first time Value of W3ExternalGeneratedHeader flag
1212+
* Get first time Value of W3ExternalGeneratedHeader flag
12161213
*/
12171214
@ReactMethod
1218-
public void isW3ExternalGeneratedHeaderEnabled(Promise promise){
1215+
public void isW3ExternalGeneratedHeaderEnabled(Promise promise) {
12191216

12201217
MainThreadHandler.runOnMainThread(new Runnable() {
12211218
@Override
12221219
public void run() {
12231220
try {
12241221
promise.resolve(InternalCore.INSTANCE._isFeatureEnabled(CoreFeature.W3C_ATTACHING_GENERATED_HEADER));
1225-
}
1226-
catch (Exception e) {
1222+
} catch (Exception e) {
12271223
e.printStackTrace();
12281224
promise.resolve(false);
12291225
}
@@ -1234,18 +1230,17 @@ public void run() {
12341230
}
12351231

12361232
/**
1237-
* Get first time Value of W3CaughtHeader flag
1233+
* Get first time Value of W3CaughtHeader flag
12381234
*/
12391235
@ReactMethod
1240-
public void isW3CaughtHeaderEnabled(Promise promise){
1236+
public void isW3CaughtHeaderEnabled(Promise promise) {
12411237

12421238
MainThreadHandler.runOnMainThread(new Runnable() {
12431239
@Override
12441240
public void run() {
12451241
try {
12461242
promise.resolve(InternalCore.INSTANCE._isFeatureEnabled(CoreFeature.W3C_ATTACHING_CAPTURED_HEADER));
1247-
}
1248-
catch (Exception e) {
1243+
} catch (Exception e) {
12491244
e.printStackTrace();
12501245
promise.resolve(false);
12511246
}
@@ -1292,4 +1287,29 @@ public void run() {
12921287
}
12931288
});
12941289
}
1290+
/**
1291+
/**
1292+
* Sets the auto mask screenshots types.
1293+
*
1294+
* @param autoMaskingTypes The masking type to be applied.
1295+
*/
1296+
@ReactMethod
1297+
public void enableAutoMasking(@NonNull ReadableArray autoMaskingTypes) {
1298+
MainThreadHandler.runOnMainThread(new Runnable() {
1299+
1300+
@Override
1301+
public void run() {
1302+
int[] autoMassingTypesArray = new int[autoMaskingTypes.size()];
1303+
for (int i = 0; i < autoMaskingTypes.size(); i++) {
1304+
String key = autoMaskingTypes.getString(i);
1305+
1306+
autoMassingTypesArray[i] = ArgsRegistry.autoMaskingTypes.get(key);
1307+
1308+
}
1309+
1310+
Instabug.setAutoMaskScreenshotsTypes(autoMassingTypesArray);
1311+
}
1312+
1313+
});
1314+
}
12951315
}

android/src/test/java/com/instabug/reactlibrary/RNInstabugReactnativeModuleTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.instabug.library.internal.module.InstabugLocale;
2626
import com.instabug.library.ui.onboarding.WelcomeMessage;
2727
import com.instabug.reactlibrary.utils.MainThreadHandler;
28+
import com.instabug.library.MaskingType;
2829

2930
import org.junit.After;
3031
import org.junit.Assert;
@@ -677,4 +678,17 @@ public void testSetNetworkLogBodyDisabled() {
677678

678679
mockInstabug.verify(() -> Instabug.setNetworkLogBodyEnabled(false));
679680
}
681+
682+
@Test
683+
public void testEnableAutoMasking(){
684+
685+
String maskLabel = "labels";
686+
String maskTextInputs = "textInputs";
687+
String maskMedia = "media";
688+
String maskNone = "none";
689+
690+
rnModule.enableAutoMasking(JavaOnlyArray.of(maskLabel, maskMedia, maskTextInputs,maskNone));
691+
692+
mockInstabug.verify(() -> Instabug.setAutoMaskScreenshotsTypes(MaskingType.LABELS,MaskingType.MEDIA,MaskingType.TEXT_INPUTS,MaskingType.MASK_NOTHING));
693+
}
680694
}

examples/default/ios/InstabugTests/InstabugSampleTests.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,22 @@ - (void) testIsW3CaughtHeaderEnabled {
608608
OCMVerify([mock w3CaughtHeaderEnabled]);
609609
}
610610

611+
- (void)testEnableAutoMasking {
612+
id mock = OCMClassMock([Instabug class]);
613+
614+
NSArray *autoMaskingTypes = [NSArray arrayWithObjects:
615+
[NSNumber numberWithInteger:IBGAutoMaskScreenshotOptionLabels],
616+
[NSNumber numberWithInteger:IBGAutoMaskScreenshotOptionTextInputs],
617+
[NSNumber numberWithInteger:IBGAutoMaskScreenshotOptionMedia],
618+
[NSNumber numberWithInteger:IBGAutoMaskScreenshotOptionMaskNothing],
619+
nil];
620+
621+
OCMStub([mock setAutoMaskScreenshots:IBGAutoMaskScreenshotOptionLabels | IBGAutoMaskScreenshotOptionTextInputs | IBGAutoMaskScreenshotOptionMedia | IBGAutoMaskScreenshotOptionMaskNothing]);
622+
623+
[self.instabugBridge enableAutoMasking:autoMaskingTypes];
624+
625+
OCMVerify([mock setAutoMaskScreenshots:IBGAutoMaskScreenshotOptionLabels | IBGAutoMaskScreenshotOptionTextInputs | IBGAutoMaskScreenshotOptionMedia | IBGAutoMaskScreenshotOptionMaskNothing]);
626+
}
611627

612628
- (void)testSetNetworkLogBodyEnabled {
613629
id mock = OCMClassMock([IBGNetworkLogger class]);

ios/RNInstabug/ArgsRegistry.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ typedef NSDictionary<NSString*, NSNumber*> ArgsDictionary;
2626
+ (ArgsDictionary *) userConsentActionTypes;
2727

2828
+ (NSDictionary<NSString *, NSString *> *) placeholders;
29+
+ (ArgsDictionary *)autoMaskingTypes;
2930

3031
@end

ios/RNInstabug/ArgsRegistry.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ + (NSMutableDictionary *) getAll {
2121
[all addEntriesFromDictionary:ArgsRegistry.nonFatalExceptionLevel];
2222
[all addEntriesFromDictionary:ArgsRegistry.placeholders];
2323
[all addEntriesFromDictionary:ArgsRegistry.launchType];
24+
[all addEntriesFromDictionary:ArgsRegistry.autoMaskingTypes];
2425
[all addEntriesFromDictionary:ArgsRegistry.userConsentActionTypes];
2526

2627
return all;
@@ -256,4 +257,12 @@ + (ArgsDictionary *) launchType {
256257
};
257258
}
258259

260+
+ (ArgsDictionary *)autoMaskingTypes {
261+
return @{
262+
@"labels" : @(IBGAutoMaskScreenshotOptionLabels),
263+
@"textInputs" : @(IBGAutoMaskScreenshotOptionTextInputs),
264+
@"media" : @(IBGAutoMaskScreenshotOptionMedia),
265+
@"none" : @(IBGAutoMaskScreenshotOptionMaskNothing)
266+
};
267+
}
259268
@end

ios/RNInstabug/InstabugReactBridge.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,6 @@ w3cExternalTraceAttributes:(NSDictionary * _Nullable)w3cExternalTraceAttributes;
139139
- (void)removeFeatureFlags:(NSArray *)featureFlags;
140140
- (void)removeAllFeatureFlags;
141141
- (void)setNetworkLogBodyEnabled:(BOOL)isEnabled;
142+
- (void)enableAutoMasking:(NSArray *)autoMaskingTypes;
142143

143144
@end

ios/RNInstabug/InstabugReactBridge.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,17 @@ + (BOOL)iOSVersionIsLessThan:(NSString *)iOSVersion {
443443
RCT_EXPORT_METHOD(setNetworkLogBodyEnabled:(BOOL)isEnabled) {
444444
IBGNetworkLogger.logBodyEnabled = isEnabled;
445445
}
446+
447+
RCT_EXPORT_METHOD(enableAutoMasking:(NSArray *)autoMaskingTypes) {
448+
449+
IBGAutoMaskScreenshotOption autoMaskingOptions = 0;
450+
451+
for (NSNumber *event in autoMaskingTypes) {
452+
453+
autoMaskingOptions |= [event intValue];
454+
}
455+
456+
[Instabug setAutoMaskScreenshots: autoMaskingOptions];
457+
458+
};
446459
@end

0 commit comments

Comments
 (0)