Advertisement
Guest User

Edge effect color

a guest
Feb 11th, 2016
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.34 KB | None | 0 0
  1. @TargetApi(21)
  2. public class XpView {
  3.     protected XpView() {}
  4.  
  5.     private static final Class<ScrollView> CLASS_SCROLL_VIEW = ScrollView.class;
  6.     private static final Field SCROLL_VIEW_FIELD_EDGE_GLOW_TOP;
  7.     private static final Field SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM;
  8.  
  9.     private static final Class<NestedScrollView> CLASS_NESTED_SCROLL_VIEW = NestedScrollView.class;
  10.     private static final Field NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_TOP;
  11.     private static final Field NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM;
  12.  
  13.     private static final Class<RecyclerView> CLASS_RECYCLER_VIEW = RecyclerView.class;
  14.     private static final Field RECYCLER_VIEW_FIELD_EDGE_GLOW_TOP;
  15.     private static final Field RECYCLER_VIEW_FIELD_EDGE_GLOW_LEFT;
  16.     private static final Field RECYCLER_VIEW_FIELD_EDGE_GLOW_RIGHT;
  17.     private static final Field RECYCLER_VIEW_FIELD_EDGE_GLOW_BOTTOM;
  18.  
  19.     private static final Class<AbsListView> CLASS_LIST_VIEW = AbsListView.class;
  20.     private static final Field LIST_VIEW_FIELD_EDGE_GLOW_TOP;
  21.     private static final Field LIST_VIEW_FIELD_EDGE_GLOW_BOTTOM;
  22.  
  23.     private static final Field EDGE_GLOW_FIELD_EDGE;
  24.     private static final Field EDGE_GLOW_FIELD_GLOW;
  25.  
  26.     private static final Field EDGE_EFFECT_COMPAT_FIELD_EDGE_EFFECT;
  27.  
  28.     static {
  29.         Field edgeGlowTop = null, edgeGlowBottom = null, edgeGlowLeft = null, edgeGlowRight = null;
  30.  
  31.         for (Field f : CLASS_RECYCLER_VIEW.getDeclaredFields()) {
  32.             switch (f.getName()) {
  33.                 case "mTopGlow":
  34.                     f.setAccessible(true);
  35.                     edgeGlowTop = f;
  36.                     break;
  37.                 case "mBottomGlow":
  38.                     f.setAccessible(true);
  39.                     edgeGlowBottom = f;
  40.                     break;
  41.                 case "mLeftGlow":
  42.                     f.setAccessible(true);
  43.                     edgeGlowLeft = f;
  44.                     break;
  45.                 case "mRightGlow":
  46.                     f.setAccessible(true);
  47.                     edgeGlowRight = f;
  48.                     break;
  49.             }
  50.         }
  51.  
  52.         RECYCLER_VIEW_FIELD_EDGE_GLOW_TOP = edgeGlowTop;
  53.         RECYCLER_VIEW_FIELD_EDGE_GLOW_BOTTOM = edgeGlowBottom;
  54.         RECYCLER_VIEW_FIELD_EDGE_GLOW_LEFT = edgeGlowLeft;
  55.         RECYCLER_VIEW_FIELD_EDGE_GLOW_RIGHT = edgeGlowRight;
  56.  
  57.         for (Field f : CLASS_NESTED_SCROLL_VIEW.getDeclaredFields()) {
  58.             switch (f.getName()) {
  59.                 case "mEdgeGlowTop":
  60.                     f.setAccessible(true);
  61.                     edgeGlowTop = f;
  62.                     break;
  63.                 case "mEdgeGlowBottom":
  64.                     f.setAccessible(true);
  65.                     edgeGlowBottom = f;
  66.                     break;
  67.             }
  68.         }
  69.  
  70.         NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_TOP = edgeGlowTop;
  71.         NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM = edgeGlowBottom;
  72.  
  73.         for (Field f : CLASS_SCROLL_VIEW.getDeclaredFields()) {
  74.             switch (f.getName()) {
  75.                 case "mEdgeGlowTop":
  76.                     f.setAccessible(true);
  77.                     edgeGlowTop = f;
  78.                     break;
  79.                 case "mEdgeGlowBottom":
  80.                     f.setAccessible(true);
  81.                     edgeGlowBottom = f;
  82.                     break;
  83.             }
  84.         }
  85.  
  86.         SCROLL_VIEW_FIELD_EDGE_GLOW_TOP = edgeGlowTop;
  87.         SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM = edgeGlowBottom;
  88.  
  89.         for (Field f : CLASS_LIST_VIEW.getDeclaredFields()) {
  90.             switch (f.getName()) {
  91.                 case "mEdgeGlowTop":
  92.                     f.setAccessible(true);
  93.                     edgeGlowTop = f;
  94.                     break;
  95.                 case "mEdgeGlowBottom":
  96.                     f.setAccessible(true);
  97.                     edgeGlowBottom = f;
  98.                     break;
  99.             }
  100.         }
  101.  
  102.         LIST_VIEW_FIELD_EDGE_GLOW_TOP = edgeGlowTop;
  103.         LIST_VIEW_FIELD_EDGE_GLOW_BOTTOM = edgeGlowBottom;
  104.  
  105.         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  106.             Field edge = null, glow = null;
  107.  
  108.             Class cls = null;
  109.             if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
  110.                 try {
  111.                     cls = Class.forName("android.widget.EdgeGlow");
  112.                 } catch (ClassNotFoundException e) {
  113.                     if (BuildConfig.DEBUG) e.printStackTrace();
  114.                 }
  115.             } else {
  116.                 cls = EdgeEffect.class;
  117.  
  118.             }
  119.  
  120.             if (cls != null) {
  121.                 for (Field f : cls.getDeclaredFields()) {
  122.                     switch (f.getName()) {
  123.                         case "mEdge":
  124.                             f.setAccessible(true);
  125.                             edge = f;
  126.                             break;
  127.                         case "mGlow":
  128.                             f.setAccessible(true);
  129.                             glow = f;
  130.                             break;
  131.                     }
  132.                 }
  133.             }
  134.  
  135.             EDGE_GLOW_FIELD_EDGE = edge;
  136.             EDGE_GLOW_FIELD_GLOW = glow;
  137.         } else {
  138.             EDGE_GLOW_FIELD_EDGE = null;
  139.             EDGE_GLOW_FIELD_GLOW = null;
  140.         }
  141.  
  142.         Field efc = null;
  143.         try {
  144.             efc = EdgeEffectCompat.class.getDeclaredField("mEdgeEffect");
  145.         } catch (NoSuchFieldException e) {
  146.             if (BuildConfig.DEBUG) e.printStackTrace();
  147.         }
  148.         EDGE_EFFECT_COMPAT_FIELD_EDGE_EFFECT = efc;
  149.     }
  150.  
  151.     @IntDef({ALWAYS, PRE_HONEYCOMB, PRE_KITKAT, PRE_LOLLIPOP})
  152.     @Retention(RetentionPolicy.SOURCE)
  153.     public @interface EdgeGlowColorApi {}
  154.  
  155.     public static final int ALWAYS = 0;
  156.     /** Replace yellow glow in vanilla, blue glow on Samsung. */
  157.     public static final int PRE_HONEYCOMB = Build.VERSION_CODES.HONEYCOMB;
  158.     /** Replace Holo blue glow. */
  159.     public static final int PRE_KITKAT = Build.VERSION_CODES.KITKAT;
  160.     /** Replace Holo grey glow. */
  161.     public static final int PRE_LOLLIPOP = Build.VERSION_CODES.LOLLIPOP;
  162.  
  163.     public static void setEdgeGlowColor(AbsListView listView, @ColorInt int color, @EdgeGlowColorApi int when) {
  164.         if (Build.VERSION.SDK_INT < when || when == ALWAYS) {
  165.             setEdgeGlowColor(listView, color);
  166.         }
  167.     }
  168.  
  169.     public static void setEdgeGlowColor(AbsListView listView, @ColorInt int color) {
  170.         try {
  171.             Object ee;
  172.             ee = LIST_VIEW_FIELD_EDGE_GLOW_TOP.get(listView);
  173.             setEdgeGlowColor(ee, color);
  174.             ee = LIST_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(listView);
  175.             setEdgeGlowColor(ee, color);
  176.         } catch (Exception ex) {
  177.             if (BuildConfig.DEBUG) ex.printStackTrace();
  178.         }
  179.     }
  180.  
  181.     public static void setEdgeGlowColor(ScrollView scrollView, @ColorInt int color, @EdgeGlowColorApi int when) {
  182.         if (Build.VERSION.SDK_INT < when || when == ALWAYS) {
  183.             setEdgeGlowColor(scrollView, color);
  184.         }
  185.     }
  186.  
  187.     public static void setEdgeGlowColor(ScrollView scrollView, @ColorInt int color) {
  188.         try {
  189.             Object ee;
  190.             ee = SCROLL_VIEW_FIELD_EDGE_GLOW_TOP.get(scrollView);
  191.             setEdgeGlowColor(ee, color);
  192.             ee = SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(scrollView);
  193.             setEdgeGlowColor(ee, color);
  194.         } catch (Exception ex) {
  195.             if (BuildConfig.DEBUG) ex.printStackTrace();
  196.         }
  197.     }
  198.  
  199.     public static void setEdgeGlowColor(NestedScrollView scrollView, @ColorInt int color, @EdgeGlowColorApi int when) {
  200.         if (Build.VERSION.SDK_INT < when || when == ALWAYS) {
  201.             setEdgeGlowColor(scrollView, color);
  202.         }
  203.     }
  204.  
  205.     public static void setEdgeGlowColor(NestedScrollView scrollView, @ColorInt int color) {
  206.         try {
  207.             Object ee;
  208.             ee = NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_TOP.get(scrollView);
  209.             setEdgeGlowColor(ee, color);
  210.             ee = NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(scrollView);
  211.             setEdgeGlowColor(ee, color);
  212.         } catch (Exception ex) {
  213.             if (BuildConfig.DEBUG) ex.printStackTrace();
  214.         }
  215.     }
  216.  
  217.     public static void setEdgeGlowColor(RecyclerView scrollView, @ColorInt int color, @EdgeGlowColorApi int when) {
  218.         if (Build.VERSION.SDK_INT < when || when == ALWAYS) {
  219.             setEdgeGlowColor(scrollView, color);
  220.         }
  221.     }
  222.  
  223.     public static void setEdgeGlowColor(RecyclerView scrollView, @ColorInt int color) {
  224.         try {
  225.             Object ee;
  226.             ee = RECYCLER_VIEW_FIELD_EDGE_GLOW_TOP.get(scrollView);
  227.             setEdgeGlowColor(ee, color);
  228.             ee = RECYCLER_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(scrollView);
  229.             setEdgeGlowColor(ee, color);
  230.             ee = RECYCLER_VIEW_FIELD_EDGE_GLOW_LEFT.get(scrollView);
  231.             setEdgeGlowColor(ee, color);
  232.             ee = RECYCLER_VIEW_FIELD_EDGE_GLOW_RIGHT.get(scrollView);
  233.             setEdgeGlowColor(ee, color);
  234.         } catch (Exception ex) {
  235.             if (BuildConfig.DEBUG) ex.printStackTrace();
  236.         }
  237.     }
  238.  
  239.     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  240.     private static void setEdgeGlowColor(Object edgeEffect, @ColorInt int color) {
  241.         if (edgeEffect instanceof EdgeEffectCompat) {
  242.             // EdgeEffectCompat
  243.             try {
  244.                 edgeEffect = EDGE_EFFECT_COMPAT_FIELD_EDGE_EFFECT.get(edgeEffect);
  245.             } catch (IllegalAccessException e) {
  246.                 if (BuildConfig.DEBUG) e.printStackTrace();
  247.                 return;
  248.             }
  249.         }
  250.  
  251.         if (edgeEffect == null) return;
  252.  
  253.         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  254.             // EdgeGlow below Android 4 then old EdgeEffect
  255.             try {
  256.                 final Drawable mEdge = (Drawable) EDGE_GLOW_FIELD_EDGE.get(edgeEffect);
  257.                 final Drawable mGlow = (Drawable) EDGE_GLOW_FIELD_GLOW.get(edgeEffect);
  258.                 mEdge.setColorFilter(color, PorterDuff.Mode.SRC_IN);
  259.                 mGlow.setColorFilter(color, PorterDuff.Mode.SRC_IN);
  260.                 mEdge.setCallback(null); // free up any references
  261.                 mGlow.setCallback(null); // free up any references
  262.             } catch (Exception ex) {
  263.                 if (BuildConfig.DEBUG) ex.printStackTrace();
  264.             }
  265.         } else {
  266.             // EdgeEffect
  267.             ((EdgeEffect) edgeEffect).setColor(color);
  268.         }
  269.     }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement