This source file includes following definitions.
- JNINamespace
- getStatus
- setStatus
- getListeners
- onApplicationStateChange
- onActivityStateChange
- initialize
- onStateChange
- onStateChangeForTesting
- getLastTrackedFocusedActivity
- getRunningActivities
- getApplicationContext
- getStateForActivity
- getStateForApplication
- hasVisibleActivities
- isEveryActivityDestroyed
- registerStateListenerForAllActivities
- registerStateListenerForActivity
- unregisterActivityStateListener
- registerApplicationStateListener
- unregisterApplicationStateListener
- registerThreadSafeNativeApplicationStateListener
- determineApplicationState
- nativeOnApplicationStateChange
package org.chromium.base;
import android.app.Activity;
import android.app.Application;
import android.app.Application.ActivityLifecycleCallbacks;
import android.content.Context;
import android.os.Bundle;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@JNINamespace("base::android")
public class ApplicationStatus {
private static class ActivityInfo {
private int mStatus = ActivityState.DESTROYED;
private ObserverList<ActivityStateListener> mListeners =
new ObserverList<ActivityStateListener>();
public int getStatus() {
return mStatus;
}
public void setStatus(int status) {
mStatus = status;
}
public ObserverList<ActivityStateListener> getListeners() {
return mListeners;
}
}
private static Application sApplication;
private static Integer sCachedApplicationState;
private static Activity sActivity;
private static ApplicationStateListener sNativeApplicationStateListener;
private static final Map<Activity, ActivityInfo> sActivityInfo =
new HashMap<Activity, ActivityInfo>();
private static final ObserverList<ActivityStateListener> sGeneralActivityStateListeners =
new ObserverList<ActivityStateListener>();
private static final ObserverList<ApplicationStateListener> sApplicationStateListeners =
new ObserverList<ApplicationStateListener>();
public interface ApplicationStateListener {
public void onApplicationStateChange(int newState);
}
public interface ActivityStateListener {
public void onActivityStateChange(Activity activity, int newState);
}
private ApplicationStatus() {}
public static void initialize(BaseChromiumApplication application) {
sApplication = application;
application.registerWindowFocusChangedListener(
new BaseChromiumApplication.WindowFocusChangedListener() {
@Override
public void onWindowFocusChanged(Activity activity, boolean hasFocus) {
if (!hasFocus || activity == sActivity) return;
int state = getStateForActivity(activity);
if (state != ActivityState.DESTROYED && state != ActivityState.STOPPED) {
sActivity = activity;
}
}
});
application.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(final Activity activity, Bundle savedInstanceState) {
onStateChange(activity, ActivityState.CREATED);
}
@Override
public void onActivityDestroyed(Activity activity) {
onStateChange(activity, ActivityState.DESTROYED);
}
@Override
public void onActivityPaused(Activity activity) {
onStateChange(activity, ActivityState.PAUSED);
}
@Override
public void onActivityResumed(Activity activity) {
onStateChange(activity, ActivityState.RESUMED);
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
@Override
public void onActivityStarted(Activity activity) {
onStateChange(activity, ActivityState.STARTED);
}
@Override
public void onActivityStopped(Activity activity) {
onStateChange(activity, ActivityState.STOPPED);
}
});
}
private static void onStateChange(Activity activity, int newState) {
if (activity == null) throw new IllegalArgumentException("null activity is not supported");
if (sActivity == null
|| newState == ActivityState.CREATED
|| newState == ActivityState.RESUMED
|| newState == ActivityState.STARTED) {
sActivity = activity;
}
int oldApplicationState = getStateForApplication();
if (newState == ActivityState.CREATED) {
assert !sActivityInfo.containsKey(activity);
sActivityInfo.put(activity, new ActivityInfo());
}
sCachedApplicationState = null;
ActivityInfo info = sActivityInfo.get(activity);
info.setStatus(newState);
for (ActivityStateListener listener : info.getListeners()) {
listener.onActivityStateChange(activity, newState);
}
for (ActivityStateListener listener : sGeneralActivityStateListeners) {
listener.onActivityStateChange(activity, newState);
}
int applicationState = getStateForApplication();
if (applicationState != oldApplicationState) {
for (ApplicationStateListener listener : sApplicationStateListeners) {
listener.onApplicationStateChange(applicationState);
}
}
if (newState == ActivityState.DESTROYED) {
sActivityInfo.remove(activity);
if (activity == sActivity) sActivity = null;
}
}
public static void onStateChangeForTesting(Activity activity, int newState) {
onStateChange(activity, newState);
}
public static Activity getLastTrackedFocusedActivity() {
return sActivity;
}
public static List<WeakReference<Activity>> getRunningActivities() {
ThreadUtils.assertOnUiThread();
List<WeakReference<Activity>> activities = new ArrayList<WeakReference<Activity>>();
for (Activity activity : sActivityInfo.keySet()) {
activities.add(new WeakReference<Activity>(activity));
}
return activities;
}
public static Context getApplicationContext() {
return sApplication != null ? sApplication.getApplicationContext() : null;
}
public static int getStateForActivity(Activity activity) {
ActivityInfo info = sActivityInfo.get(activity);
return info != null ? info.getStatus() : ActivityState.DESTROYED;
}
public static int getStateForApplication() {
if (sCachedApplicationState == null) sCachedApplicationState = determineApplicationState();
return sCachedApplicationState.intValue();
}
public static boolean hasVisibleActivities() {
int state = getStateForApplication();
return state == ApplicationState.HAS_RUNNING_ACTIVITIES
|| state == ApplicationState.HAS_PAUSED_ACTIVITIES;
}
public static boolean isEveryActivityDestroyed() {
return sActivityInfo.isEmpty();
}
public static void registerStateListenerForAllActivities(ActivityStateListener listener) {
sGeneralActivityStateListeners.addObserver(listener);
}
public static void registerStateListenerForActivity(ActivityStateListener listener,
Activity activity) {
assert activity != null;
ActivityInfo info = sActivityInfo.get(activity);
assert info != null && info.getStatus() != ActivityState.DESTROYED;
info.getListeners().addObserver(listener);
}
public static void unregisterActivityStateListener(ActivityStateListener listener) {
sGeneralActivityStateListeners.removeObserver(listener);
for (ActivityInfo info : sActivityInfo.values()) {
info.getListeners().removeObserver(listener);
}
}
public static void registerApplicationStateListener(ApplicationStateListener listener) {
sApplicationStateListeners.addObserver(listener);
}
public static void unregisterApplicationStateListener(ApplicationStateListener listener) {
sApplicationStateListeners.removeObserver(listener);
}
@CalledByNative
private static void registerThreadSafeNativeApplicationStateListener() {
ThreadUtils.runOnUiThread(new Runnable () {
@Override
public void run() {
if (sNativeApplicationStateListener != null) return;
sNativeApplicationStateListener = new ApplicationStateListener() {
@Override
public void onApplicationStateChange(int newState) {
nativeOnApplicationStateChange(newState);
}
};
registerApplicationStateListener(sNativeApplicationStateListener);
}
});
}
private static int determineApplicationState() {
boolean hasPausedActivity = false;
boolean hasStoppedActivity = false;
for (ActivityInfo info : sActivityInfo.values()) {
int state = info.getStatus();
if (state != ActivityState.PAUSED
&& state != ActivityState.STOPPED
&& state != ActivityState.DESTROYED) {
return ApplicationState.HAS_RUNNING_ACTIVITIES;
} else if (state == ActivityState.PAUSED) {
hasPausedActivity = true;
} else if (state == ActivityState.STOPPED) {
hasStoppedActivity = true;
}
}
if (hasPausedActivity) return ApplicationState.HAS_PAUSED_ACTIVITIES;
if (hasStoppedActivity) return ApplicationState.HAS_STOPPED_ACTIVITIES;
return ApplicationState.HAS_DESTROYED_ACTIVITIES;
}
private static native void nativeOnApplicationStateChange(int newState);
}