Last active
August 29, 2015 14:24
-
-
Save crowjdh/10d51ea4aff0bb689878 to your computer and use it in GitHub Desktop.
Observes and supplies information whether an Android app is in foreground or background.(API 14+)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.what.ever; | |
import android.app.Activity; | |
import android.app.Application; | |
import android.os.Bundle; | |
import java.util.ArrayList; | |
/** | |
* Created by Dongheyon Jeong in ActivityLifecycleCallbacksTest on July 9th, 2015 | |
* | |
* AppActiveStateObserver | |
* Supplies information of app's active state(foreground / background) | |
* | |
* Usage: | |
* 1. Add below in onCreate of your Application class. | |
* registerActivityLifecycleCallbacks(AppActiveStateObserver.getInstance()); | |
* 2. Use isInForeground() and isInBackground() method to determine active state of the application. | |
* | |
* | |
* Additional function: | |
* Implement "OnStateChangeCallback" and register it by using registerCallback(OnStateChangeCallback callback) method | |
* to get notified when the active state changes. | |
*/ | |
public class AppActiveStateObserver implements Application.ActivityLifecycleCallbacks { | |
public interface OnStateChangeCallback { | |
void onStateForeground(Activity activity); | |
void onStateBackground(Activity activity); | |
} | |
private volatile static AppActiveStateObserver instance; | |
private final ArrayList<OnStateChangeCallback> mCallbacks = new ArrayList<>(); | |
private int mStartedActivityCount = 0; | |
private boolean mIsInForeground = false; | |
private AppActiveStateObserver() { } | |
public static AppActiveStateObserver getInstance() { | |
if (instance == null) { | |
synchronized (AppActiveStateObserver.class) { | |
if (instance == null) { | |
instance = new AppActiveStateObserver(); | |
} | |
} | |
} | |
return instance; | |
} | |
public boolean isInForeground() { | |
return mIsInForeground; | |
} | |
public boolean isInBackground() { | |
return !isInForeground(); | |
} | |
public void registerCallback(OnStateChangeCallback callback) { | |
synchronized (mCallbacks) { | |
mCallbacks.add(callback); | |
} | |
} | |
public void unregisterCallback(OnStateChangeCallback callback) { | |
synchronized (mCallbacks) { | |
mCallbacks.remove(callback); | |
} | |
} | |
private void increaseStartedActivityCount(Activity activity) { | |
adjustStartedActivityCount(activity, true); | |
} | |
private void decreaseStartedActivityCount(Activity activity) { | |
adjustStartedActivityCount(activity, false); | |
} | |
private void adjustStartedActivityCount(Activity activity, boolean hasActivityStarted) { | |
boolean wasInForeground = mIsInForeground; | |
if (hasActivityStarted) { | |
mStartedActivityCount++; | |
} else { | |
mStartedActivityCount--; | |
} | |
mIsInForeground = mStartedActivityCount > 0; | |
boolean activityStateChanged = wasInForeground != mIsInForeground; | |
if (activityStateChanged) { | |
if (isInForeground()) { | |
dispatchStateForeground(activity); | |
} else { | |
dispatchStateBackground(activity); | |
} | |
} | |
} | |
private Object[] collectCallbacks() { | |
Object[] callbacks = null; | |
synchronized (mCallbacks) { | |
if (mCallbacks.size() > 0) { | |
callbacks = mCallbacks.toArray(); | |
} | |
} | |
return callbacks; | |
} | |
private void dispatchStateForeground(Activity activity) { | |
Object[] callbacks = collectCallbacks(); | |
if (callbacks != null) { | |
for (Object callback : callbacks) { | |
((OnStateChangeCallback)callback).onStateForeground(activity); | |
} | |
} | |
} | |
private void dispatchStateBackground(Activity activity) { | |
Object[] callbacks = collectCallbacks(); | |
if (callbacks != null) { | |
for (Object callback : callbacks) { | |
((OnStateChangeCallback)callback).onStateBackground(activity); | |
} | |
} | |
} | |
@Override | |
public void onActivityStarted(Activity activity) { | |
increaseStartedActivityCount(activity); | |
} | |
@Override | |
public void onActivityStopped(Activity activity) { | |
decreaseStartedActivityCount(activity); | |
} | |
// region Unused callbacks | |
@Override | |
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { } | |
@Override | |
public void onActivityResumed(Activity activity) { } | |
@Override | |
public void onActivityPaused(Activity activity) { } | |
@Override | |
public void onActivitySaveInstanceState(Activity activity, Bundle outState) { } | |
@Override | |
public void onActivityDestroyed(Activity activity) { } | |
// endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment