Skip to content

Commit bf82d82

Browse files
committed
Change the reflecting method of @activity#convertToTranslucent after Android L.
1 parent 30dd1ac commit bf82d82

File tree

1 file changed

+37
-0
lines changed
  • library/src/main/java/me/imid/swipebacklayout/lib

1 file changed

+37
-0
lines changed

library/src/main/java/me/imid/swipebacklayout/lib/Utils.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
package me.imid.swipebacklayout.lib;
33

44
import android.app.Activity;
5+
import android.app.ActivityOptions;
6+
import android.os.Build;
57

68
import java.lang.reflect.Method;
79

@@ -46,6 +48,17 @@ public static void convertActivityFromTranslucent(Activity activity) {
4648
* with the {@link android.R.attr#windowIsFloating} attribute.
4749
*/
4850
public static void convertActivityToTranslucent(Activity activity) {
51+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
52+
convertActivityToTranslucentAfterL(activity);
53+
} else {
54+
convertActivityToTranslucentBeforeL(activity);
55+
}
56+
}
57+
58+
/**
59+
* Calling the convertToTranslucent method on platforms before Android 5.0
60+
*/
61+
public static void convertActivityToTranslucentBeforeL(Activity activity) {
4962
try {
5063
Class<?>[] classes = Activity.class.getDeclaredClasses();
5164
Class<?> translucentConversionListenerClazz = null;
@@ -63,4 +76,28 @@ public static void convertActivityToTranslucent(Activity activity) {
6376
} catch (Throwable t) {
6477
}
6578
}
79+
80+
/**
81+
* Calling the convertToTranslucent method on platforms after Android 5.0
82+
*/
83+
private static void convertActivityToTranslucentAfterL(Activity activity) {
84+
try {
85+
Method getActivityOptions = Activity.class.getDeclaredMethod("getActivityOptions");
86+
getActivityOptions.setAccessible(true);
87+
Object options = getActivityOptions.invoke(activity);
88+
89+
Class<?>[] classes = Activity.class.getDeclaredClasses();
90+
Class<?> translucentConversionListenerClazz = null;
91+
for (Class clazz : classes) {
92+
if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
93+
translucentConversionListenerClazz = clazz;
94+
}
95+
}
96+
Method convertToTranslucent = Activity.class.getDeclaredMethod("convertToTranslucent",
97+
translucentConversionListenerClazz, ActivityOptions.class);
98+
convertToTranslucent.setAccessible(true);
99+
convertToTranslucent.invoke(activity, null, options);
100+
} catch (Throwable t) {
101+
}
102+
}
66103
}

0 commit comments

Comments
 (0)