com.eg.agent.android.background.ApplicationStateMonitor Maven / Gradle / Ivy
package com.eg.agent.android.background;
import com.eg.agent.android.logging.AgentLog;
import com.eg.agent.android.logging.AgentLogManager;
import com.eg.agent.android.util.NamedThreadFactory;
import java.util.ArrayList;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import static java.lang.System.currentTimeMillis;
public class ApplicationStateMonitor
implements Runnable {
private static final AgentLog log = AgentLogManager.getAgentLog();
private AtomicLong count = new AtomicLong(0L);
private AtomicLong snoozeStartTime = new AtomicLong(0L);
private final Lock snoozeLock = new ReentrantLock();
private final int activitySnoozeTimeInMilliseconds;
protected final ArrayList applicationStateListeners = new ArrayList();
protected AtomicBoolean foregrounded = new AtomicBoolean(true);
private final Lock foregroundLock = new ReentrantLock();
private static ApplicationStateMonitor instance;
protected final ScheduledThreadPoolExecutor executor;
private ApplicationStateMonitor() {
this(5, 5, TimeUnit.SECONDS, 5000);
}
ApplicationStateMonitor(int initialDelay, int period, TimeUnit timeUnit, int snoozeTimeInMilliseconds) {
this.executor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("AppStateMon"));
this.activitySnoozeTimeInMilliseconds = snoozeTimeInMilliseconds;
this.executor.scheduleAtFixedRate(this, initialDelay, period, timeUnit);
log.info("Application state monitor has started");
}
public static ApplicationStateMonitor getInstance() {
if (instance == null) {
instance = new ApplicationStateMonitor();
}
return instance;
}
public void addApplicationStateListener(ApplicationStateListener listener) {
synchronized (this.applicationStateListeners) {
this.applicationStateListeners.add(listener);
}
}
public void removeApplicationStateListener(ApplicationStateListener listener) {
synchronized (this.applicationStateListeners) {
this.applicationStateListeners.remove(listener);
}
}
public void run() {
try {
this.foregroundLock.lock();
if ((this.foregrounded.get()) && (getSnoozeTime() >= this.activitySnoozeTimeInMilliseconds)) {
this.foregrounded.set(false);
notifyApplicationInBackground();
}
} finally {
this.foregroundLock.unlock();
}
}
public void uiHidden() {
/* 84 */
Runnable runner = new Runnable() {
public void run() {
try {
ApplicationStateMonitor.this.foregroundLock.lock();
if (ApplicationStateMonitor.this.foregrounded.get()) {
ApplicationStateMonitor.log.info("UI has become hidden (app backgrounded)");
ApplicationStateMonitor.this.notifyApplicationInBackground();
ApplicationStateMonitor.this.foregrounded.set(false);
}
} finally {
ApplicationStateMonitor.this.foregroundLock.unlock();
}
}
};
this.executor.execute(runner);
}
public void activityStopped() {
Runnable runner = new Runnable() {
public void run() {
try {
/* 110 */
ApplicationStateMonitor.this.foregroundLock.lock();
try {
ApplicationStateMonitor.this.snoozeLock.lock();
if (ApplicationStateMonitor.this.count.decrementAndGet() == 0L) {
ApplicationStateMonitor.this.snoozeStartTime.set(currentTimeMillis());
}
} finally {
ApplicationStateMonitor.this.snoozeLock.unlock();
}
} finally {
ApplicationStateMonitor.this.foregroundLock.unlock();
}
}
};
this.executor.execute(runner);
}
public void activityStarted() {
Runnable runner = new Runnable() {
public void run() {
try {
ApplicationStateMonitor.this.foregroundLock.lock();
try {
ApplicationStateMonitor.this.snoozeLock.lock();
if (ApplicationStateMonitor.this.count.incrementAndGet() == 1L) {
ApplicationStateMonitor.this.snoozeStartTime.set(0L);
}
} finally {
ApplicationStateMonitor.this.snoozeLock.unlock();
}
if (!ApplicationStateMonitor.this.foregrounded.get()) {
ApplicationStateMonitor.log.verbose("Application appears to be in the foreground");
ApplicationStateMonitor.this.foregrounded.set(true);
ApplicationStateMonitor.this.notifyApplicationInForeground();
}
} finally {
ApplicationStateMonitor.this.foregroundLock.unlock();
}
}
};
this.executor.execute(runner);
}
private void notifyApplicationInBackground() {
log.verbose("Application appears to have gone to the background");
ArrayList listeners;
synchronized (this.applicationStateListeners) {
listeners = new ArrayList(this.applicationStateListeners);
}
ApplicationStateEvent e = new ApplicationStateEvent(this);
for (ApplicationStateListener listener : listeners) {
listener.applicationBackgrounded(e);
}
}
private void notifyApplicationInForeground() {
ArrayList listeners;
synchronized (this.applicationStateListeners) {
listeners = new ArrayList(this.applicationStateListeners);
}
ApplicationStateEvent e = new ApplicationStateEvent(this);
for (ApplicationStateListener listener : listeners) {
listener.applicationForegrounded(e);
}
}
private long getSnoozeTime() {
long snoozeValue = 0L;
try {
this.foregroundLock.lock();
try {
this.snoozeLock.lock();
long snoozeTime = this.snoozeStartTime.get();
if (snoozeTime != 0L) {
snoozeValue = currentTimeMillis() - snoozeTime;
}
} finally {
this.snoozeLock.unlock();
}
} finally {
this.foregroundLock.unlock();
}
return snoozeValue;
}
public ScheduledThreadPoolExecutor getExecutor() {
return this.executor;
}
public boolean getForegrounded() {
return this.foregrounded.get();
}
public static boolean isAppInBackground() {
return !getInstance().getForegrounded();
}
}
/*
import com.eg.agent.android.logging.AgentLog;
import com.eg.agent.android.logging.AgentLogManager;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
*/
/**
* Created by Venkateswari.J on 4/25/2017.
*//*
public class ApplicationStateMonitor implements Runnable {
private static final AgentLog log = AgentLogManager.getAgentLog();
private long count;
private long snoozeStartTime;
private final Object snoozeLock;
private final int activitySnoozeTimeInMilliseconds;
private final ArrayList applicationStateListeners;
private boolean foregrounded;
private final Object foregroundLock;
private static ApplicationStateMonitor instance;
private ApplicationStateMonitor() {
this(5, 5, TimeUnit.SECONDS, 5000);
}
ApplicationStateMonitor(int initialDelay, int period, TimeUnit timeUnit, int snoozeTimeInMilliseconds) {
this.count = 0L;
this.snoozeStartTime = 0L;
this.snoozeLock = new Object();
this.applicationStateListeners = new ArrayList();
this.foregrounded = true;
this.foregroundLock = new Object();
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
public Thread newThread(Runnable r) {
return new Thread(r, "[New Relic] App State Monitor");
}
});
this.activitySnoozeTimeInMilliseconds = snoozeTimeInMilliseconds;
executor.scheduleAtFixedRate(this, (long)initialDelay, (long)period, timeUnit);
log.info("Application state monitor has started");
}
public static ApplicationStateMonitor getInstance() {
if(instance == null) {
instance = new ApplicationStateMonitor();
}
return instance;
}
public void addApplicationStateListener(ApplicationStateListener listener) {
ArrayList var2 = this.applicationStateListeners;
synchronized(this.applicationStateListeners) {
this.applicationStateListeners.add(listener);
}
}
public void removeApplicationStateListener(ApplicationStateListener listener) {
ArrayList var2 = this.applicationStateListeners;
synchronized(this.applicationStateListeners) {
this.applicationStateListeners.remove(listener);
}
}
public void run() {
Object var1 = this.foregroundLock;
synchronized(this.foregroundLock) {
if(this.getSnoozeTime() >= (long)this.activitySnoozeTimeInMilliseconds && this.foregrounded) {
log.verbose("Application appears to have gone to the background");
this.notifyApplicationInBackground();
this.foregrounded = false;
}
}
}
public void activityStopped() {
Object var1 = this.foregroundLock;
synchronized(this.foregroundLock) {
Object var2 = this.snoozeLock;
synchronized(this.snoozeLock) {
--this.count;
if(this.count == 0L) {
this.snoozeStartTime = System.currentTimeMillis();
}
}
}
}
public void activityStarted() {
Object var1 = this.foregroundLock;
synchronized(this.foregroundLock) {
Object var2 = this.snoozeLock;
synchronized(this.snoozeLock) {
++this.count;
if(this.count == 1L) {
this.snoozeStartTime = 0L;
}
}
if(!this.foregrounded) {
log.verbose("Application appears to be in the foreground");
this.notifyApplicationInForeground();
this.foregrounded = true;
}
}
}
private void notifyApplicationInBackground() {
ArrayList e = this.applicationStateListeners;
ArrayList listeners;
synchronized(this.applicationStateListeners) {
listeners = new ArrayList(this.applicationStateListeners);
}
ApplicationStateEvent e1 = new ApplicationStateEvent(this);
Iterator i$ = listeners.iterator();
while(i$.hasNext()) {
ApplicationStateListener listener = (ApplicationStateListener)i$.next();
listener.applicationBackgrounded(e1);
}
}
private void notifyApplicationInForeground() {
ArrayList e = this.applicationStateListeners;
ArrayList listeners;
synchronized(this.applicationStateListeners) {
listeners = new ArrayList(this.applicationStateListeners);
}
ApplicationStateEvent e1 = new ApplicationStateEvent(this);
Iterator i$ = listeners.iterator();
while(i$.hasNext()) {
ApplicationStateListener listener = (ApplicationStateListener)i$.next();
listener.applicationForegrounded(e1);
}
}
private long getSnoozeTime() {
Object var1 = this.foregroundLock;
synchronized(this.foregroundLock) {
Object var2 = this.snoozeLock;
long var10000;
synchronized(this.snoozeLock) {
if(this.snoozeStartTime == 0L) {
var10000 = 0L;
return var10000;
}
var10000 = System.currentTimeMillis() - this.snoozeStartTime;
}
return var10000;
}
}
public void uiHidden() {
Runnable runner = new Runnable() {
public void run() {
try {
ApplicationStateMonitor.this.foregroundLock.lock();
if (ApplicationStateMonitor.this.foregrounded.get()) {
ApplicationStateMonitor.log.info("UI has become hidden (app backgrounded)");
ApplicationStateMonitor.this.notifyApplicationInBackground();
ApplicationStateMonitor.this.foregrounded.set(false);
}
} finally {
ApplicationStateMonitor.this.foregroundLock.unlock();
}
}
};
this.executor.execute(runner);
}
}
*/
© 2015 - 2024 Weber Informatics LLC | Privacy Policy