Last active
August 29, 2024 19:43
-
-
Save astraube/db269b72f8c809916a46f0c92236f18a to your computer and use it in GitHub Desktop.
Watcher Top Activity
This file contains hidden or 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.straucorp.core; | |
import android.app.ActivityManager; | |
import android.content.ComponentName; | |
import android.content.Context; | |
import android.os.Handler; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Objects; | |
/** | |
* @author Straube - Created by Andre Straube on 25/03/2020 | |
* | |
* @usage Programmatically | |
* TopActivityWatcher watcher = new TopActivityWatcher(); | |
* watcher.registerListener(listener); | |
* watcher.start(this.applicationContext); | |
*/ | |
public class TopActivityWatcher { | |
static final int DEFAULT_TOLERANCE = 1; | |
static final int DEFAULT_CHECK_INTERVAL_MS = 500; | |
final List<TopActivityListener> mListenerList = new ArrayList<>(); | |
final Object mResLock = new Object(); | |
int mCheckIntervalMs; | |
int mTolerance; | |
String mTopActivity = null; | |
String mPendingActivity = null; | |
int mPendingCount = 0; | |
Handler mHandler = null; | |
ActivityManager mActivityManager = null; | |
boolean mIsRunning = false; | |
Runnable mTopActivityCheckRunnable = new Runnable() { | |
@Override | |
public void run() { | |
String topActivity = getTopActivityClassName(); | |
if (mTopActivity == null && topActivity != null) { | |
updateTopActivity(topActivity); | |
} | |
else if (!Objects.equals(topActivity, mTopActivity)) { | |
if (Objects.equals(topActivity, mPendingActivity)) { | |
mPendingActivity = topActivity; | |
mPendingCount = 1; | |
} else { | |
mPendingCount++; | |
if (mPendingCount > mTolerance) { | |
updateTopActivity(topActivity); | |
} | |
} | |
} | |
synchronized (mResLock) { | |
if (mHandler != null) { | |
mHandler.postDelayed(this, mCheckIntervalMs); | |
} | |
} | |
} | |
}; | |
public TopActivityWatcher() { | |
this(DEFAULT_CHECK_INTERVAL_MS, DEFAULT_TOLERANCE); | |
} | |
public TopActivityWatcher(int checkIntervalMs, int tolerance) { | |
mCheckIntervalMs = checkIntervalMs; | |
mTolerance = tolerance; | |
} | |
public void registerListener(final TopActivityListener aListener) { | |
synchronized (mListenerList) { | |
if (!mListenerList.contains(aListener)) { | |
mListenerList.add(aListener); | |
} | |
} | |
} | |
public void unregisterListener(final TopActivityListener aListener) { | |
synchronized (mListenerList) { | |
if (mListenerList.contains(aListener)) { | |
mListenerList.remove(aListener); | |
} | |
} | |
} | |
public boolean start(Context context) { | |
synchronized (mResLock) { | |
if( !mIsRunning ) { | |
if (mActivityManager == null) { | |
mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); | |
} | |
if (mActivityManager != null) { | |
if (mHandler == null) { | |
mHandler = new Handler(); | |
} | |
mIsRunning = true; | |
mHandler.post(mTopActivityCheckRunnable); | |
} | |
} | |
return mIsRunning; | |
} | |
} | |
public void stop() { | |
synchronized (mResLock) { | |
if(mIsRunning) { | |
if (mActivityManager != null) { | |
mActivityManager = null; | |
} | |
if (mHandler != null) { | |
mHandler.removeCallbacksAndMessages(null); | |
mHandler = null; | |
} | |
mIsRunning = false; | |
} | |
} | |
} | |
private void notifyTopActivityChanged(final String topActivity) { | |
synchronized (mListenerList) { | |
for (TopActivityListener listener : mListenerList) { | |
if (listener != null) { | |
listener.onTopActivityChanged(topActivity); | |
} | |
} | |
} | |
} | |
private void updateTopActivity(final String topActivity) { | |
if (!Objects.equals(mTopActivity, topActivity)) { | |
mTopActivity = topActivity; | |
mPendingActivity = null; | |
mPendingCount = 0; | |
notifyTopActivityChanged(topActivity); | |
} | |
} | |
private String getTopActivityClassName() { | |
String topActivity = null; | |
List<ActivityManager.RunningTaskInfo> runningTasks = null; | |
synchronized (mResLock) { | |
if (mActivityManager != null) { | |
try { | |
runningTasks = mActivityManager.getRunningTasks(1); | |
} catch (SecurityException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
if (runningTasks != null && !runningTasks.isEmpty()) { | |
ActivityManager.RunningTaskInfo runningTaskInfo = runningTasks.get(0); | |
if (runningTaskInfo != null) { | |
ComponentName topComponent = runningTaskInfo.topActivity; | |
if (topComponent != null) { | |
topActivity = topComponent.getClassName(); | |
} | |
} | |
} | |
return topActivity; | |
} | |
public interface TopActivityListener { | |
void onTopActivityChanged(final String newActivity); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment