Skip to content

Instantly share code, notes, and snippets.

@cesclong
Created March 19, 2021 02:05
Show Gist options
  • Save cesclong/194ef0ef350094b497ef8ea81b1bbed3 to your computer and use it in GitHub Desktop.
Save cesclong/194ef0ef350094b497ef8ea81b1bbed3 to your computer and use it in GitHub Desktop.
Android Foreground
package com.cn.mi;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class Foreground implements Application.ActivityLifecycleCallbacks {
public static final long CHECK_DELAY = 500;
public static final String TAG = Foreground.class.getName();
public interface Listener {
void onActivityFirstCreate(Activity activity);
void onBecameForeground(Activity activity);
void onBecameBackground();
}
private static Foreground instance;
private boolean activityCreated = false;
private boolean foreground = false, paused = true;
private Handler handler = new Handler();
private List<Listener> listeners = new CopyOnWriteArrayList<Listener>();
private Runnable check;
private boolean uiActive = false;
/**
* Its not strictly necessary to use this method - _usually_ invoking
* get with a Context gives us a path to retrieve the Application and
* initialise, but sometimes (e.g. in test harness) the ApplicationContext
* is != the Application, and the docs make no guarantees.
*
* @param application
* @return an initialised Foreground instance
*/
public static Foreground init(Application application) {
if (instance == null) {
instance = new Foreground();
application.registerActivityLifecycleCallbacks(instance);
}
instance.uiActive = false;
return instance;
}
public static boolean isInitialized() {
return instance != null;
}
public static Foreground get(Application application) {
if (instance == null) {
init(application);
}
return instance;
}
public static Foreground get(Context ctx) {
if (instance == null) {
Context appCtx = ctx.getApplicationContext();
if (appCtx instanceof Application) {
init((Application) appCtx);
} else {
throw new IllegalStateException("Foreground is not initialised and cannot obtain the Application object");
}
}
return instance;
}
public static Foreground get() {
if (instance == null) {
throw new IllegalStateException("Foreground is not initialised - invoke at least once with parameterised init/get");
}
return instance;
}
public boolean isForeground() {
return foreground;
}
public boolean isBackground() {
return !foreground;
}
public boolean isUiActive() {
return uiActive;
}
public void addListener(Listener listener) {
listeners.add(listener);
}
public void removeListener(Listener listener) {
listeners.remove(listener);
}
@Override
public void onActivityResumed(Activity activity) {
if (!activityCreated) {
for (Listener listener : listeners) {
listener.onActivityFirstCreate(activity);
}
activityCreated = true;
}
paused = false;
boolean wasBackground = !foreground;
foreground = true;
uiActive = true;
if (check != null)
handler.removeCallbacks(check);
if (wasBackground) {
Log.i(TAG, "went foreground");
for (Listener l : listeners) {
try {
l.onBecameForeground(activity);
} catch (Exception exc) {
Log.e(TAG, "Listener threw exception!", exc);
}
}
} else {
Log.i(TAG, "still foreground");
}
}
@Override
public void onActivityPaused(Activity activity) {
paused = true;
if (check != null)
handler.removeCallbacks(check);
handler.postDelayed(check = new Runnable() {
@Override
public void run() {
if (foreground && paused) {
foreground = false;
Log.i(TAG, "went background");
for (Listener l : listeners) {
try {
l.onBecameBackground();
} catch (Exception exc) {
Log.e(TAG, "Listener threw exception!", exc);
}
}
} else {
Log.i(TAG, "still foreground");
}
}
}, CHECK_DELAY);
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment