Skip to content

Commit

Permalink
Add getCurrentActivity and getTargetActivity APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
DevHossamHassan committed Apr 5, 2017
1 parent afa01c7 commit 90885a9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
.DS_Store
/build
/captures
/.idea/
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.brandedme.hossamhassan.instamonitor.model.Session;
import com.brandedme.hossamhassan.instamonitor.service.InstaTaskService;
import com.brandedme.hossamhassan.instamonitor.util.InstaLog;
import com.brandedme.hossamhassan.instamonitor.util.Prefs;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -32,6 +34,7 @@ public class InstaMonitor {
private List<ActivityInfo> list;
private static ArrayList<Class> excludedActivitiesList;
private static Boolean enableDebugMode = false;
private WeakReference<Activity> currentActivity;

/**
* Gets tag.
Expand Down Expand Up @@ -97,7 +100,7 @@ public void init(Application application) {
registerCallbacks();
startInstaService();
getActivitiesList();
excludedActivitiesList=new ArrayList<>();
excludedActivitiesList = new ArrayList<>();
}

/**
Expand Down Expand Up @@ -138,6 +141,7 @@ public void onActivityStarted(Activity activity) {

@Override
public void onActivityResumed(Activity activity) {
currentActivity = new WeakReference<>(activity);
if (!excludedActivitiesList.contains(activity.getClass())) {
String activityName = activity.getClass().getName();
InstaLog.d("onActivityResumed: " + activityName);
Expand Down Expand Up @@ -339,4 +343,30 @@ public void resetSessionsState() {
setStartTime();
}

@Nullable
public Activity getCurrentActivity() {
if (currentActivity != null && currentActivity.get() != null) {
return currentActivity.get();
} else {
return null;
}
}

@Nullable
public Activity getTargetActivity() {
Activity target = null;
if (currentActivity != null
&& currentActivity.get() != null
&& currentActivity.get().getParent() != null) {

target = currentActivity.get().getParent();
while (target.getParent() != null) {
target = target.getParent();
}
} else if (currentActivity != null) {
target = currentActivity.get();
}

return target;
}
}

0 comments on commit 90885a9

Please sign in to comment.