Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.45 KB | None | 0 0
  1. package com.younglive.livestreaming.utils.view;
  2.  
  3. /*
  4. * Author: Felipe Herranz (felhr85@gmail.com)
  5. * Contributors:Francesco Verheye (verheye.francesco@gmail.com)
  6. * Israel Dominguez (dominguez.israel@gmail.com)
  7. */
  8. import android.os.Handler;
  9. import android.os.Message;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.view.inputmethod.InputMethodManager;
  13. import android.widget.EditText;
  14.  
  15. import java.lang.ref.WeakReference;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import java.util.concurrent.atomic.AtomicBoolean;
  19.  
  20. public class SoftKeyboard implements View.OnFocusChangeListener
  21. {
  22. private static final int CLEAR_FOCUS = 0;
  23.  
  24. private ViewGroup layout;
  25. private int layoutBottom;
  26. private InputMethodManager im;
  27. private int[] coords;
  28. private boolean isKeyboardShow;
  29. private SoftKeyboardChangesThread softKeyboardThread;
  30. private List<EditText> editTextList;
  31.  
  32. private View tempView; // reference to a focused EditText
  33.  
  34. public SoftKeyboard(ViewGroup layout, InputMethodManager im) {
  35. this.layout = layout;
  36. keyboardHideByDefault();
  37. initEditTexts(layout);
  38. this.im = im;
  39. this.coords = new int[2];
  40. this.isKeyboardShow = false;
  41. this.softKeyboardThread = new SoftKeyboardChangesThread(this);
  42. this.softKeyboardThread.start();
  43. mHandler = new InnerHandler(this);
  44. }
  45.  
  46.  
  47. public void openSoftKeyboard()
  48. {
  49. if(!isKeyboardShow)
  50. {
  51. layoutBottom = getLayoutCoordinates();
  52. im.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
  53. softKeyboardThread.keyboardOpened();
  54. isKeyboardShow = true;
  55. }
  56. }
  57.  
  58. public void closeSoftKeyboard()
  59. {
  60. if(isKeyboardShow)
  61. {
  62. im.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
  63. isKeyboardShow = false;
  64. }
  65. }
  66.  
  67. public void setSoftKeyboardCallback(SoftKeyboardChanged mCallback)
  68. {
  69. softKeyboardThread.setCallback(mCallback);
  70. }
  71.  
  72. public void unRegisterSoftKeyboardCallback()
  73. {
  74. softKeyboardThread.stopThread();
  75. }
  76.  
  77. public interface SoftKeyboardChanged
  78. {
  79. public void onSoftKeyboardHide();
  80. public void onSoftKeyboardShow();
  81. }
  82.  
  83. private int getLayoutCoordinates()
  84. {
  85. layout.getLocationOnScreen(coords);
  86. return coords[1] + layout.getHeight();
  87. }
  88.  
  89. private void keyboardHideByDefault()
  90. {
  91. layout.setFocusable(true);
  92. layout.setFocusableInTouchMode(true);
  93. }
  94.  
  95. /*
  96. * InitEditTexts now handles EditTexts in nested views
  97. * Thanks to Francesco Verheye (verheye.francesco@gmail.com)
  98. */
  99. private void initEditTexts(ViewGroup viewgroup)
  100. {
  101. if(editTextList == null)
  102. editTextList = new ArrayList<>();
  103.  
  104. int childCount = viewgroup.getChildCount();
  105. for(int i=0; i<= childCount-1;i++)
  106. {
  107. View v = viewgroup.getChildAt(i);
  108.  
  109. if(v instanceof ViewGroup)
  110. {
  111. initEditTexts((ViewGroup) v);
  112. }
  113.  
  114. if(v instanceof EditText)
  115. {
  116. EditText editText = (EditText) v;
  117. editText.setOnFocusChangeListener(this);
  118. editText.setCursorVisible(true);
  119. editTextList.add(editText);
  120. }
  121. }
  122. }
  123.  
  124. /*
  125. * OnFocusChange does update tempView correctly now when keyboard is still shown
  126. * Thanks to Israel Dominguez (dominguez.israel@gmail.com)
  127. */
  128. @Override
  129. public void onFocusChange(View v, boolean hasFocus)
  130. {
  131. if(hasFocus)
  132. {
  133. tempView = v;
  134. if(!isKeyboardShow)
  135. {
  136. layoutBottom = getLayoutCoordinates();
  137. softKeyboardThread.keyboardOpened();
  138. isKeyboardShow = true;
  139. }
  140. }
  141. }
  142.  
  143. // This handler will clear focus of selected EditText
  144. private final InnerHandler mHandler;
  145.  
  146. private static class SoftKeyboardChangesThread extends Thread {
  147. WeakReference<SoftKeyboard> mSoftKeyboardWeakReference;
  148. private AtomicBoolean started;
  149. private SoftKeyboardChanged mCallback;
  150.  
  151. public SoftKeyboardChangesThread(SoftKeyboard softKeyboard) {
  152. started = new AtomicBoolean(true);
  153. mSoftKeyboardWeakReference = new WeakReference<>(softKeyboard);
  154. }
  155.  
  156. public void setCallback(SoftKeyboardChanged mCallback) {
  157. this.mCallback = mCallback;
  158. }
  159.  
  160. @Override
  161. public void run() {
  162. while(started.get()) {
  163. // Wait until keyboard is requested to open
  164. synchronized(this) {
  165. try {
  166. wait();
  167. } catch (InterruptedException e) {
  168. e.printStackTrace();
  169. }
  170. }
  171.  
  172. if (mSoftKeyboardWeakReference.get() != null &&
  173. mSoftKeyboardWeakReference.get().mHandler != null) {
  174. int currentBottomLocation = mSoftKeyboardWeakReference.get().getLayoutCoordinates();
  175.  
  176. // There is some lag between open soft-keyboard function and when it really appears.
  177. while(currentBottomLocation == mSoftKeyboardWeakReference.get().layoutBottom && started.get()) {
  178. currentBottomLocation = mSoftKeyboardWeakReference.get().getLayoutCoordinates();
  179. }
  180.  
  181. if(started.get()) {
  182. mCallback.onSoftKeyboardShow();
  183. }
  184.  
  185. // When keyboard is opened from EditText, initial bottom location is greater than layoutBottom
  186. // and at some moment equals layoutBottom.
  187. // That broke the previous logic, so I added this new loop to handle this.
  188. while(currentBottomLocation >= mSoftKeyboardWeakReference.get().layoutBottom && started.get()) {
  189. currentBottomLocation = mSoftKeyboardWeakReference.get().getLayoutCoordinates();
  190. }
  191.  
  192. // Now Keyboard is shown, keep checking layout dimensions until keyboard is gone
  193. while(currentBottomLocation != mSoftKeyboardWeakReference.get().layoutBottom && started.get()) {
  194. synchronized(this) {
  195. try {
  196. wait(500);
  197. } catch (InterruptedException e) {
  198. e.printStackTrace();
  199. }
  200. }
  201. currentBottomLocation = mSoftKeyboardWeakReference.get().getLayoutCoordinates();
  202. }
  203.  
  204. if(started.get()) {
  205. mCallback.onSoftKeyboardHide();
  206. }
  207.  
  208. // if keyboard has been opened clicking and EditText.
  209. if(mSoftKeyboardWeakReference.get().isKeyboardShow && started.get()) {
  210. mSoftKeyboardWeakReference.get().isKeyboardShow = false;
  211. }
  212.  
  213. // if an EditText is focused, remove its focus (on UI thread)
  214. if(started.get()) {
  215. mSoftKeyboardWeakReference.get().mHandler.obtainMessage(CLEAR_FOCUS).sendToTarget();
  216. }
  217. }
  218. }
  219. }
  220.  
  221. public void keyboardOpened()
  222. {
  223. synchronized(this)
  224. {
  225. notify();
  226. }
  227. }
  228.  
  229. public void stopThread()
  230. {
  231. synchronized(this)
  232. {
  233. started.set(false);
  234. notify();
  235. }
  236. }
  237.  
  238. }
  239.  
  240. private static final class InnerHandler extends Handler {
  241.  
  242. private WeakReference<SoftKeyboard> mSoftKeyboardWeakReference;
  243.  
  244. private InnerHandler(SoftKeyboard softKeyboard) {
  245. mSoftKeyboardWeakReference = new WeakReference<>(softKeyboard);
  246. }
  247.  
  248. @Override
  249. public void handleMessage(Message m) {
  250. switch(m.what) {
  251. case CLEAR_FOCUS:
  252. if(mSoftKeyboardWeakReference.get() != null &&
  253. mSoftKeyboardWeakReference.get().tempView != null) {
  254. mSoftKeyboardWeakReference.get().tempView.clearFocus();
  255. mSoftKeyboardWeakReference.get().tempView = null;
  256. mSoftKeyboardWeakReference.clear();
  257. }
  258. break;
  259. }
  260. }
  261. }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement