1616import com .instabug .library .logging .InstabugLog ;
1717import com .instabug .library .ui .onboarding .WelcomeMessage ;
1818
19+ import java .lang .reflect .InvocationTargetException ;
1920import java .lang .reflect .Method ;
2021import java .util .ArrayList ;
2122import 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}
0 commit comments