Skip to content

Commit 8c01320

Browse files
authored
[Mob 3136] Flutter Issue: Android Screenshots (#46)
* ✨ add enabling screenshot by media projection on Instabug initialization * ✨ add the ios microphone and photo permissions to the example app
1 parent d2f679e commit 8c01320

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

android/src/main/java/com/instabug/instabugflutter/InstabugFlutterPlugin.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.instabug.library.logging.InstabugLog;
1717
import com.instabug.library.ui.onboarding.WelcomeMessage;
1818

19+
import java.lang.reflect.InvocationTargetException;
1920
import java.lang.reflect.Method;
2021
import java.util.ArrayList;
2122
import java.util.HashMap;
@@ -98,6 +99,7 @@ public void start(Application application, String token, ArrayList<String> invoc
9899
invocationEventsArray[i] = ArgsRegistry.getDeserializedValue(key, InstabugInvocationEvent.class);
99100
}
100101
new Instabug.Builder(application, token).setInvocationEvents(invocationEventsArray).build();
102+
enableScreenShotByMediaProjection();
101103
}
102104

103105

@@ -328,7 +330,6 @@ public void logUserEventWithName(String name) {
328330
Instabug.logUserEvent(name);
329331
}
330332

331-
332333
/**
333334
* Overrides any of the strings shown in the SDK with custom ones.
334335
* @param value String value to override the default one.
@@ -338,5 +339,51 @@ public void setValue(String value, String forStringWithKey) {
338339
InstabugCustomTextPlaceHolder.Key key = ArgsRegistry.getDeserializedValue(forStringWithKey, InstabugCustomTextPlaceHolder.Key.class);
339340
placeHolder.set(key, value);
340341
Instabug.setCustomTextPlaceHolders(placeHolder);
342+
}
343+
344+
/**
345+
* Enables taking screenshots by media projection.
346+
*/
347+
private void enableScreenShotByMediaProjection() {
348+
try {
349+
Method method = getMethod(Class.forName("com.instabug.bug.BugReporting"), "setScreenshotByMediaProjectionEnabled", boolean.class);
350+
if (method != null) {
351+
method.invoke(null, true);
352+
}
353+
} catch (ClassNotFoundException e) {
354+
e.printStackTrace();
355+
} catch (IllegalAccessException e) {
356+
e.printStackTrace();
357+
} catch (InvocationTargetException e) {
358+
e.printStackTrace();
359+
}
360+
}
361+
362+
/**
363+
* Gets the private method that matches the class, method name and parameter types given and making it accessible.
364+
* For private use only.
365+
* @param clazz the class the method is in
366+
* @param methodName the method name
367+
* @param parameterType list of the parameter types of the method
368+
* @return the method that matches the class, method name and param types given
369+
*/
370+
public static Method getMethod(Class clazz, String methodName, Class... parameterType) {
371+
final Method[] methods = clazz.getDeclaredMethods();
372+
for (Method method : methods) {
373+
if (method.getName().equals(methodName) && method.getParameterTypes().length ==
374+
parameterType.length) {
375+
for (int i = 0; i < parameterType.length; i++) {
376+
if (method.getParameterTypes()[i] == parameterType[i]) {
377+
if (i == method.getParameterTypes().length - 1) {
378+
method.setAccessible(true);
379+
return method;
380+
}
381+
} else {
382+
break;
383+
}
384+
}
385+
}
386+
}
387+
return null;
341388
}
342389
}

example/ios/Runner/Info.plist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5+
<key>NSMicrophoneUsageDescription</key>
6+
<string>Instabug would like to access your microphone.</string>
7+
<key>NSPhotoLibraryUsageDescription</key>
8+
<string>Instabug would like to access your photos.</string>
59
<key>CFBundleDevelopmentRegion</key>
610
<string>en</string>
711
<key>CFBundleExecutable</key>

0 commit comments

Comments
 (0)