Skip to content

Commit ee01349

Browse files
TheBuggedYRNHeshamMegid
authored andcommitted
[MOB-10950] Make ArgsRegistry Type-safe on Android (#296)
The two ArgsRegistry methods getDeserializedValue and getRawValue were not type safe at runtime. This PR provide a type-safe way to achieve enum resolution on Android.
1 parent 0975dd8 commit ee01349

File tree

6 files changed

+442
-581
lines changed

6 files changed

+442
-581
lines changed

android/src/main/java/com/instabug/flutter/modules/ApmApi.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ public void setAutoUITraceEnabled(@NonNull Boolean isEnabled) {
5959
@Override
6060
public void setLogLevel(@NonNull String level) {
6161
try {
62-
if (ArgsRegistry.getDeserializedValue(level) == null) {
63-
return;
64-
}
65-
APM.setLogLevel((int) ArgsRegistry.getRawValue(level));
62+
final int resolvedLevel = ArgsRegistry.logLevels.get(level);
63+
APM.setLogLevel(resolvedLevel);
6664
} catch (Exception e) {
6765
e.printStackTrace();
6866
}

android/src/main/java/com/instabug/flutter/modules/BugReportingApi.java

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

3+
import android.annotation.SuppressLint;
4+
35
import androidx.annotation.NonNull;
46
import androidx.annotation.Nullable;
57

@@ -40,13 +42,14 @@ public void setEnabled(@NonNull Boolean isEnabled) {
4042
}
4143
}
4244

45+
@SuppressLint("WrongConstant")
4346
@Override
4447
public void show(@NonNull String reportType, @Nullable List<String> invocationOptions) {
4548
int[] options = new int[invocationOptions.size()];
4649
for (int i = 0; i < invocationOptions.size(); i++) {
47-
options[i] = ArgsRegistry.getDeserializedValue(invocationOptions.get(i));
50+
options[i] = ArgsRegistry.invocationOptions.get(invocationOptions.get(i));
4851
}
49-
int reportTypeInt = ArgsRegistry.getDeserializedValue(reportType);
52+
int reportTypeInt = ArgsRegistry.reportTypes.get(reportType);
5053
BugReporting.show(reportTypeInt, options);
5154
}
5255

@@ -56,49 +59,51 @@ public void setInvocationEvents(@NonNull List<String> events) {
5659

5760
for (int i = 0; i < events.size(); i++) {
5861
String key = events.get(i);
59-
invocationEventsArray[i] = ArgsRegistry.getDeserializedValue(key);
62+
invocationEventsArray[i] = ArgsRegistry.invocationEvents.get(key);
6063
}
6164

6265
BugReporting.setInvocationEvents(invocationEventsArray);
6366
}
6467

68+
@SuppressLint("WrongConstant")
6569
@Override
6670
public void setReportTypes(@NonNull List<String> types) {
6771
int[] reportTypesArray = new int[types.size()];
6872

6973
for (int i = 0; i < types.size(); i++) {
7074
String key = types.get(i);
71-
reportTypesArray[i] = ArgsRegistry.getDeserializedValue(key);
75+
reportTypesArray[i] = ArgsRegistry.reportTypes.get(key);
7276
}
7377

7478
BugReporting.setReportTypes(reportTypesArray);
7579
}
7680

7781
@Override
7882
public void setExtendedBugReportMode(@NonNull String mode) {
79-
final ExtendedBugReport.State resolvedMode = ArgsRegistry.getDeserializedValue(mode);
83+
final ExtendedBugReport.State resolvedMode = ArgsRegistry.extendedBugReportStates.get(mode);
8084
BugReporting.setExtendedBugReportState(resolvedMode);
8185
}
8286

87+
@SuppressLint("WrongConstant")
8388
@Override
8489
public void setInvocationOptions(@NonNull List<String> options) {
8590
int[] resolvedOptions = new int[options.size()];
8691
for (int i = 0; i < options.size(); i++) {
87-
resolvedOptions[i] = ArgsRegistry.getDeserializedValue(options.get(i));
92+
resolvedOptions[i] = ArgsRegistry.invocationOptions.get(options.get(i));
8893
}
8994
BugReporting.setOptions(resolvedOptions);
9095
}
9196

9297
@Override
9398
public void setFloatingButtonEdge(@NonNull String edge, @NonNull Long offset) {
94-
final InstabugFloatingButtonEdge resolvedEdge = ArgsRegistry.getDeserializedValue(edge);
99+
final InstabugFloatingButtonEdge resolvedEdge = ArgsRegistry.floatingButtonEdges.get(edge);
95100
BugReporting.setFloatingButtonEdge(resolvedEdge);
96101
BugReporting.setFloatingButtonOffset(offset.intValue());
97102
}
98103

99104
@Override
100105
public void setVideoRecordingFloatingButtonPosition(@NonNull String position) {
101-
final InstabugVideoRecordingButtonPosition resolvedPosition = ArgsRegistry.getDeserializedValue(position);
106+
final InstabugVideoRecordingButtonPosition resolvedPosition = ArgsRegistry.recordButtonPositions.get(position);
102107
BugReporting.setVideoRecordingFloatingButtonPosition(resolvedPosition);
103108
}
104109

@@ -155,13 +160,14 @@ public void setDisclaimerText(@NonNull String text) {
155160
BugReporting.setDisclaimerText(text);
156161
}
157162

163+
@SuppressLint("WrongConstant")
158164
@Override
159165
public void setCommentMinimumCharacterCount(@NonNull Long limit, @Nullable List<String> reportTypes) {
160166
int[] reportTypesArray = reportTypes == null ? new int[0] : new int[reportTypes.size()];
161167
if(reportTypes != null){
162168
for (int i = 0; i < reportTypes.size(); i++) {
163169
String key = reportTypes.get(i);
164-
reportTypesArray[i] = ArgsRegistry.getDeserializedValue(key);
170+
reportTypesArray[i] = ArgsRegistry.reportTypes.get(key);
165171
}
166172
}
167173
BugReporting.setCommentMinimumCharacterCount(limit.intValue(), reportTypesArray);

android/src/main/java/com/instabug/flutter/modules/FeatureRequestsApi.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.instabug.flutter.modules;
22

3+
import android.annotation.SuppressLint;
4+
35
import androidx.annotation.NonNull;
46

57
import com.instabug.featuresrequest.FeatureRequests;
@@ -22,11 +24,12 @@ public void show() {
2224
FeatureRequests.show();
2325
}
2426

27+
@SuppressLint("WrongConstant")
2528
@Override
2629
public void setEmailFieldRequired(@NonNull Boolean isRequired, @NonNull List<String> actionTypes) {
2730
int[] actions = new int[actionTypes.size()];
2831
for (int i = 0; i < actionTypes.size(); i++) {
29-
actions[i] = ArgsRegistry.getDeserializedValue(actionTypes.get(i));
32+
actions[i] = ArgsRegistry.actionTypes.get(actionTypes.get(i));
3033
}
3134

3235
FeatureRequests.setEmailFieldRequired(isRequired, actions);

android/src/main/java/com/instabug/flutter/modules/InstabugApi.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212

1313
import com.instabug.flutter.util.ArgsRegistry;
1414
import com.instabug.flutter.generated.InstabugPigeon;
15-
import com.instabug.flutter.util.ArgsRegistry;
1615
import com.instabug.flutter.util.Reflection;
1716
import com.instabug.flutter.util.ThreadManager;
1817
import com.instabug.library.Feature;
1918
import com.instabug.library.Instabug;
2019
import com.instabug.library.InstabugColorTheme;
2120
import com.instabug.library.InstabugCustomTextPlaceHolder;
2221
import com.instabug.library.Platform;
22+
import com.instabug.library.internal.module.InstabugLocale;
2323
import com.instabug.library.invocation.InstabugInvocationEvent;
2424
import com.instabug.library.model.NetworkLog;
2525
import com.instabug.library.ui.onboarding.WelcomeMessage;
26+
import com.instabug.library.visualusersteps.State;
2627

2728
import org.json.JSONObject;
2829

@@ -87,7 +88,7 @@ public void start(@NonNull String token, @NonNull List<String> invocationEvents)
8788
InstabugInvocationEvent[] invocationEventsArray = new InstabugInvocationEvent[invocationEvents.size()];
8889
for (int i = 0; i < invocationEvents.size(); i++) {
8990
String key = invocationEvents.get(i);
90-
invocationEventsArray[i] = ArgsRegistry.getDeserializedValue(key);
91+
invocationEventsArray[i] = ArgsRegistry.invocationEvents.get(key);
9192
}
9293

9394
final Application application = (Application) context;
@@ -104,8 +105,8 @@ public void show() {
104105

105106
@Override
106107
public void showWelcomeMessageWithMode(@NonNull String mode) {
107-
WelcomeMessage.State resolvedWelcomeMessageMode = ArgsRegistry.getDeserializedValue(mode);
108-
Instabug.showWelcomeMessage(resolvedWelcomeMessageMode);
108+
WelcomeMessage.State resolvedMode = ArgsRegistry.welcomeMessageStates.get(mode);
109+
Instabug.showWelcomeMessage(resolvedMode);
109110
}
110111

111112
@Override
@@ -130,22 +131,20 @@ public void logOut() {
130131

131132
@Override
132133
public void setLocale(@NonNull String locale) {
133-
Locale resolvedLocale = ArgsRegistry.getDeserializedValue(locale);
134-
Instabug.setLocale(resolvedLocale);
134+
final InstabugLocale resolvedLocale = ArgsRegistry.locales.get(locale);
135+
Instabug.setLocale(new Locale(resolvedLocale.getCode(), resolvedLocale.getCountry()));
135136
}
136137

137138
@Override
138139
public void setColorTheme(@NonNull String theme) {
139-
InstabugColorTheme resolvedTheme = ArgsRegistry.getDeserializedValue(theme);
140-
if (resolvedTheme != null) {
141-
Instabug.setColorTheme(resolvedTheme);
142-
}
140+
InstabugColorTheme resolvedTheme = ArgsRegistry.colorThemes.get(theme);
141+
Instabug.setColorTheme(resolvedTheme);
143142
}
144143

145144
@Override
146145
public void setWelcomeMessageMode(@NonNull String mode) {
147-
WelcomeMessage.State resolvedWelcomeMessageMode = ArgsRegistry.getDeserializedValue(mode);
148-
Instabug.setWelcomeMessageState(resolvedWelcomeMessageMode);
146+
WelcomeMessage.State resolvedMode = ArgsRegistry.welcomeMessageStates.get(mode);
147+
Instabug.setWelcomeMessageState(resolvedMode);
149148
}
150149

151150
@Override
@@ -164,7 +163,7 @@ public void setSessionProfilerEnabled(@NonNull Boolean enabled) {
164163

165164
@Override
166165
public void setValueForStringWithKey(@NonNull String value, @NonNull String key) {
167-
InstabugCustomTextPlaceHolder.Key resolvedKey = ArgsRegistry.getDeserializedValue(key);
166+
InstabugCustomTextPlaceHolder.Key resolvedKey = ArgsRegistry.placeholders.get(key);
168167
placeHolder.set(resolvedKey, value);
169168
Instabug.setCustomTextPlaceHolders(placeHolder);
170169
}
@@ -254,7 +253,8 @@ public void setSdkDebugLogsLevel(@NonNull String level) {
254253
@Override
255254
public void setReproStepsMode(@NonNull String mode) {
256255
try {
257-
Instabug.setReproStepsState(ArgsRegistry.getDeserializedValue(mode));
256+
final State resolvedMode = ArgsRegistry.reproStates.get(mode);
257+
Instabug.setReproStepsState(resolvedMode);
258258
} catch (Exception e) {
259259
e.printStackTrace();
260260
}

0 commit comments

Comments
 (0)