Advertisement
GeneralGDA

Exception safe LinkMovementMethod

Feb 28th, 2016
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. //LinkMovementMethod will cause app to crash if there is no activity to handle clicked link (link without schema, or with unknown one)
  2. public final class ExceptionSafeLinkMovementMethod extends LinkMovementMethod
  3. {
  4.     @Override
  5.     public boolean onTouchEvent(final TextView widget, final Spannable buffer, final MotionEvent event)
  6.     {
  7.         try
  8.         {
  9.             return super.onTouchEvent(widget, buffer, event);
  10.         }
  11.         catch (final Throwable e)
  12.         {
  13.             return false;
  14.         }
  15.     }
  16.  
  17.     @Override @TargetApi(11) // 11 == Build.VERSION_CODES.HONEYCOMB
  18.     protected boolean handleMovementKey(
  19.         final TextView widget, final Spannable buffer, final int keyCode, final int movementMetaState, final KeyEvent event)
  20.     {
  21.         try
  22.         {
  23.             return super.handleMovementKey(widget, buffer, keyCode, movementMetaState, event);
  24.         }
  25.         catch (final Throwable e)
  26.         {
  27.             return false;
  28.         }
  29.     }
  30.  
  31.     @Override
  32.     protected boolean up(final TextView widget, final Spannable buffer)
  33.     {
  34.         try
  35.         {
  36.             return super.up(widget, buffer);
  37.         }
  38.         catch (final Throwable e)
  39.         {
  40.             return false;
  41.         }
  42.     }
  43.  
  44.     @Override
  45.     protected boolean down(final TextView widget, final Spannable buffer)
  46.     {
  47.         try
  48.         {
  49.             return super.down(widget, buffer);
  50.         }
  51.         catch (final Throwable e)
  52.         {
  53.             return false;
  54.         }
  55.     }
  56.  
  57.     @Override
  58.     protected boolean left(final TextView widget, final Spannable buffer)
  59.     {
  60.         try
  61.         {
  62.             return super.left(widget, buffer);
  63.         }
  64.         catch (final Throwable e)
  65.         {
  66.             return false;
  67.         }
  68.     }
  69.  
  70.     @Override
  71.     protected boolean right(final TextView widget, final Spannable buffer)
  72.     {
  73.         try
  74.         {
  75.             return super.right(widget, buffer);
  76.         }
  77.         catch (final Throwable e)
  78.         {
  79.             return false;
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement