Guest User

Untitled

a guest
Jul 28th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. package com.sjl.util;
  2.  
  3. import android.app.Activity;
  4. import android.app.Application;
  5. import android.content.Context;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.util.Log;
  9.  
  10. import java.util.List;
  11. import java.util.concurrent.CopyOnWriteArrayList;
  12.  
  13. /**
  14. * Usage:
  15. *
  16. * 1. Get the Foreground Singleton, passing a Context or Application object unless you
  17. * are sure that the Singleton has definitely already been initialised elsewhere.
  18. *
  19. * 2.a) Perform a direct, synchronous check: Foreground.isForeground() / .isBackground()
  20. *
  21. * or
  22. *
  23. * 2.b) Register to be notified (useful in Service or other non-UI components):
  24. *
  25. * Foreground.Listener myListener = new Foreground.Listener(){
  26. * public void onBecameForeground(){
  27. * // ... whatever you want to do
  28. * }
  29. * public void onBecameBackground(){
  30. * // ... whatever you want to do
  31. * }
  32. * }
  33. *
  34. * public void onCreate(){
  35. * super.onCreate();
  36. * Foreground.get(this).addListener(listener);
  37. * }
  38. *
  39. * public void onDestroy(){
  40. * super.onCreate();
  41. * Foreground.get(this).removeListener(listener);
  42. * }
  43. */
  44. public class Foreground implements Application.ActivityLifecycleCallbacks {
  45.  
  46. public static final long CHECK_DELAY = 500;
  47. public static final String TAG = Foreground.class.getName();
  48.  
  49. public interface Listener {
  50.  
  51. public void onBecameForeground();
  52.  
  53. public void onBecameBackground();
  54.  
  55. }
  56.  
  57. private static Foreground instance;
  58.  
  59. private boolean foreground = false, paused = true;
  60. private Handler handler = new Handler();
  61. private List<Listener> listeners = new CopyOnWriteArrayList<Listener>();
  62. private Runnable check;
  63.  
  64. /**
  65. * Its not strictly necessary to use this method - _usually_ invoking
  66. * get with a Context gives us a path to retrieve the Application and
  67. * initialise, but sometimes (e.g. in test harness) the ApplicationContext
  68. * is != the Application, and the docs make no guarantees.
  69. *
  70. * @param application
  71. * @return an initialised Foreground instance
  72. */
  73. public static Foreground init(Application application){
  74. if (instance == null) {
  75. instance = new Foreground();
  76. application.registerActivityLifecycleCallbacks(instance);
  77. }
  78. return instance;
  79. }
  80.  
  81. public static Foreground get(Application application){
  82. if (instance == null) {
  83. init(application);
  84. }
  85. return instance;
  86. }
  87.  
  88. public static Foreground get(Context ctx){
  89. if (instance == null) {
  90. Context appCtx = ctx.getApplicationContext();
  91. if (appCtx instanceof Application) {
  92. init((Application)appCtx);
  93. }
  94. throw new IllegalStateException(
  95. "Foreground is not initialised and " +
  96. "cannot obtain the Application object");
  97. }
  98. return instance;
  99. }
  100.  
  101. public static Foreground get(){
  102. if (instance == null) {
  103. throw new IllegalStateException(
  104. "Foreground is not initialised - invoke " +
  105. "at least once with parameterised init/get");
  106. }
  107. return instance;
  108. }
  109.  
  110. public boolean isForeground(){
  111. return foreground;
  112. }
  113.  
  114. public boolean isBackground(){
  115. return !foreground;
  116. }
  117.  
  118. public void addListener(Listener listener){
  119. listeners.add(listener);
  120. }
  121.  
  122. public void removeListener(Listener listener){
  123. listeners.remove(listener);
  124. }
  125.  
  126. @Override
  127. public void onActivityResumed(Activity activity) {
  128. paused = false;
  129. boolean wasBackground = !foreground;
  130. foreground = true;
  131.  
  132. if (check != null)
  133. handler.removeCallbacks(check);
  134.  
  135. if (wasBackground){
  136. Log.i(TAG, "went foreground");
  137. for (Listener l : listeners) {
  138. try {
  139. l.onBecameForeground();
  140. } catch (Exception exc) {
  141. Log.e(TAG, "Listener threw exception!", exc);
  142. }
  143. }
  144. } else {
  145. Log.i(TAG, "still foreground");
  146. }
  147. }
  148.  
  149. @Override
  150. public void onActivityPaused(Activity activity) {
  151. paused = true;
  152.  
  153. if (check != null)
  154. handler.removeCallbacks(check);
  155.  
  156. handler.postDelayed(check = new Runnable(){
  157. @Override
  158. public void run() {
  159. if (foreground && paused) {
  160. foreground = false;
  161. Log.i(TAG, "went background");
  162. for (Listener l : listeners) {
  163. try {
  164. l.onBecameBackground();
  165. } catch (Exception exc) {
  166. Log.e(TAG, "Listener threw exception!", exc);
  167. }
  168. }
  169. } else {
  170. Log.i(TAG, "still foreground");
  171. }
  172. }
  173. }, CHECK_DELAY);
  174. }
  175.  
  176. @Override
  177. public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
  178.  
  179. @Override
  180. public void onActivityStarted(Activity activity) {}
  181.  
  182. @Override
  183. public void onActivityStopped(Activity activity) {}
  184.  
  185. @Override
  186. public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
  187.  
  188. @Override
  189. public void onActivityDestroyed(Activity activity) {}
  190. }
Add Comment
Please, Sign In to add comment