Utils.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package me.imid.swipebacklayout.lib;
  2. import android.app.Activity;
  3. import android.app.ActivityOptions;
  4. import android.os.Build;
  5. import java.lang.reflect.Method;
  6. /**
  7. * Created by Chaojun Wang on 6/9/14.
  8. */
  9. public class Utils {
  10. private Utils() {
  11. }
  12. /**
  13. * Convert a translucent themed Activity
  14. * {@link android.R.attr#windowIsTranslucent} to a fullscreen opaque
  15. * Activity.
  16. * <p>
  17. * Call this whenever the background of a translucent Activity has changed
  18. * to become opaque. Doing so will allow the {@link android.view.Surface} of
  19. * the Activity behind to be released.
  20. * <p>
  21. * This call has no effect on non-translucent activities or on activities
  22. * with the {@link android.R.attr#windowIsFloating} attribute.
  23. */
  24. public static void convertActivityFromTranslucent(Activity activity) {
  25. try {
  26. Method method = Activity.class.getDeclaredMethod("convertFromTranslucent");
  27. method.setAccessible(true);
  28. method.invoke(activity);
  29. } catch (Throwable t) {
  30. }
  31. }
  32. /**
  33. * Convert a translucent themed Activity
  34. * {@link android.R.attr#windowIsTranslucent} back from opaque to
  35. * translucent following a call to
  36. * {@link #convertActivityFromTranslucent(android.app.Activity)} .
  37. * <p>
  38. * Calling this allows the Activity behind this one to be seen again. Once
  39. * all such Activities have been redrawn
  40. * <p>
  41. * This call has no effect on non-translucent activities or on activities
  42. * with the {@link android.R.attr#windowIsFloating} attribute.
  43. */
  44. public static void convertActivityToTranslucent(Activity activity) {
  45. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  46. convertActivityToTranslucentAfterL(activity);
  47. } else {
  48. convertActivityToTranslucentBeforeL(activity);
  49. }
  50. }
  51. /**
  52. * Calling the convertToTranslucent method on platforms before Android 5.0
  53. */
  54. public static void convertActivityToTranslucentBeforeL(Activity activity) {
  55. try {
  56. Class<?>[] classes = Activity.class.getDeclaredClasses();
  57. Class<?> translucentConversionListenerClazz = null;
  58. for (Class clazz : classes) {
  59. if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
  60. translucentConversionListenerClazz = clazz;
  61. }
  62. }
  63. Method method = Activity.class.getDeclaredMethod("convertToTranslucent",
  64. translucentConversionListenerClazz);
  65. method.setAccessible(true);
  66. method.invoke(activity, new Object[] {
  67. null
  68. });
  69. } catch (Throwable t) {
  70. }
  71. }
  72. /**
  73. * Calling the convertToTranslucent method on platforms after Android 5.0
  74. */
  75. private static void convertActivityToTranslucentAfterL(Activity activity) {
  76. try {
  77. Method getActivityOptions = Activity.class.getDeclaredMethod("getActivityOptions");
  78. getActivityOptions.setAccessible(true);
  79. Object options = getActivityOptions.invoke(activity);
  80. Class<?>[] classes = Activity.class.getDeclaredClasses();
  81. Class<?> translucentConversionListenerClazz = null;
  82. for (Class clazz : classes) {
  83. if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
  84. translucentConversionListenerClazz = clazz;
  85. }
  86. }
  87. Method convertToTranslucent = Activity.class.getDeclaredMethod("convertToTranslucent",
  88. translucentConversionListenerClazz, ActivityOptions.class);
  89. convertToTranslucent.setAccessible(true);
  90. convertToTranslucent.invoke(activity, null, options);
  91. } catch (Throwable t) {
  92. }
  93. }
  94. }