Skip to content

Commit

Permalink
Provide access to current activities
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawnnypoo committed Mar 16, 2017
1 parent b539d23 commit 73c6f8a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.commit451.lifeline.Lifeline;
Expand All @@ -22,7 +23,7 @@ public static void check(Context context) {
}

public CheckIfForegroundService() {
super("skldfjla");
super(CheckIfForegroundService.class.getSimpleName());
}

@Override
Expand All @@ -31,6 +32,8 @@ protected void onHandleIntent(Intent intent) {
@Override
public void run() {
Toast.makeText(CheckIfForegroundService.this, "In foreground: " + Lifeline.isInForeground(), Toast.LENGTH_SHORT).show();
Log.d("TEST", "Current visible activity: " + Lifeline.getCurrentVisibleActivity());
Log.d("TEST", "Current created activity: " + Lifeline.getCurrentCreatedActivity());
}
});
try {
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/com/commit451/lifeline/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.commit451.lifeline.sample;

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.commit451.lifeline.Lifeline;
Expand All @@ -12,15 +15,32 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.button_dialog).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Hi")
.setMessage("Hello there")
.show();
}
});
CheckIfForegroundService.check(this);
}

@Override
protected void onResume() {
super.onResume();
Log.d("TEST", "onResume");
long timeSpentOutsideApp = Lifeline.getTimeSpentOutsideApp();
if (timeSpentOutsideApp > 0) {
Toast.makeText(MainActivity.this, "time spend outside of app: " + timeSpentOutsideApp + " ms", Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onPause() {
super.onPause();
Log.d("TEST", "onPause");
}
}
11 changes: 9 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
Expand All @@ -8,10 +8,17 @@
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.commit451.lifeline.sample.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</RelativeLayout>

<Button
android:id="@+id/button_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show a Dialog"/>
</LinearLayout>
4 changes: 3 additions & 1 deletion lifeline/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-annotations:25.3.0'

testCompile 'junit:junit:4.12'
}

Expand Down
56 changes: 56 additions & 0 deletions lifeline/src/main/java/com/commit451/lifeline/Lifeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.annotation.Nullable;

import java.lang.ref.WeakReference;

/**
* Keeps a pulse on your application
Expand All @@ -13,6 +16,7 @@ public class Lifeline {

/**
* Hooks your Application up to this Lifeline
*
* @param application application
*/
public static void init(Application application) {
Expand Down Expand Up @@ -50,6 +54,48 @@ public static long getTimeSpentOutsideApp() {
return lifecycleHandler.timeSpentOutsideApp;
}

/**
* Get the currently visible activity. This could be null in certain cases, such as if a
* dialog is showing on top of the current activity
*
* @return the activity that is showing (resumed) or null if none exists
*/
@Nullable
public static Activity getCurrentVisibleActivity() {
if (lifecycleHandler.currentVisibleActivityRef != null) {
return lifecycleHandler.currentVisibleActivityRef.get();
}
return null;
}

/**
* Get the currently created activity. This could be null in certain cases, such as if a
* no activities have been created (just services or broadcast receivers in existence)
*
* @return the latest created activity or null if none exists
*/
@Nullable
public static Activity getCurrentCreatedActivity() {
if (lifecycleHandler.currentCreatedActivityRef != null) {
return lifecycleHandler.currentCreatedActivityRef.get();
}
return null;
}

/**
* Get the currently started activity. This could be null in certain cases, such as if a
* no activities have been started (just services or broadcast receivers in existence)
*
* @return the latest started activity or null if none exists
*/
@Nullable
public static Activity getCurrentStartedActivity() {
if (lifecycleHandler.currentStartedActivityRef != null) {
return lifecycleHandler.currentStartedActivityRef.get();
}
return null;
}

private static void checkInit() {
if (lifecycleHandler == null) {
throw new IllegalStateException("You need to first call `init()` on this class before using it");
Expand All @@ -69,12 +115,18 @@ private static class TrackedLifecycleCallbacks implements Application.ActivityLi
private long timeStartedPause;
private long timeSpentOutsideApp;

private WeakReference<Activity> currentVisibleActivityRef;
private WeakReference<Activity> currentCreatedActivityRef;
private WeakReference<Activity> currentStartedActivityRef;

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
currentCreatedActivityRef = new WeakReference<>(activity);
}

@Override
public void onActivityDestroyed(Activity activity) {
currentVisibleActivityRef = null;
}

@Override
Expand All @@ -84,12 +136,14 @@ public void onActivityResumed(Activity activity) {
timeSpentOutsideApp = System.currentTimeMillis() - timeStartedPause;
timeStartedPause = 0;
}
currentVisibleActivityRef = new WeakReference<>(activity);
}

@Override
public void onActivityPaused(Activity activity) {
paused = paused + 1;
timeStartedPause = System.currentTimeMillis();
currentVisibleActivityRef = null;
}

@Override
Expand All @@ -99,11 +153,13 @@ public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
@Override
public void onActivityStarted(Activity activity) {
++started;
currentStartedActivityRef = new WeakReference<>(activity);
}

@Override
public void onActivityStopped(Activity activity) {
++stopped;
currentStartedActivityRef = null;
}
}
}

0 comments on commit 73c6f8a

Please sign in to comment.