Skip to content

Commit

Permalink
Add callbacks for if in the background
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawnnypoo committed Oct 9, 2017
1 parent 73c6f8a commit 98b0c1a
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 52 deletions.
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ android:
components:
- platform-tools
- tools
- build-tools-25.0.2
- android-25
- extra-android-m2repository

before_script:
- chmod +x gradlew
# Install SDK license so Android Gradle plugin can install deps.
- mkdir "$ANDROID_HOME/licenses" || true
- echo "8933bad161af4178b1185d1a37fbf41ea5269c55" > "$ANDROID_HOME/licenses/android-sdk-license"

script: "./gradlew build"
8 changes: 3 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion 26

defaultConfig {
applicationId "com.commit451.lifeline.sample"
minSdkVersion 14
targetSdkVersion 25
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
Expand All @@ -23,7 +22,6 @@ android {
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:appcompat-v7:26.1.0'
compile project(':lifeline')
}
9 changes: 9 additions & 0 deletions app/src/main/java/com/commit451/lifeline/sample/App.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.commit451.lifeline.sample;

import android.app.Application;
import android.widget.Toast;

import com.commit451.lifeline.Lifeline;
import com.commit451.lifeline.OnBackgroundedListener;

/**
* Here is where you would initialize {@link com.commit451.lifeline.Lifeline}
Expand All @@ -13,5 +15,12 @@ public class App extends Application {
public void onCreate() {
super.onCreate();
Lifeline.init(this);
Lifeline.register(new OnBackgroundedListener() {
@Override
public void onBackgrounded() {
Toast.makeText(App.this, "On backgrounded", Toast.LENGTH_SHORT)
.show();
}
});
}
}
9 changes: 3 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:3.0.0-beta7'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter()
google()
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Mar 08 10:06:00 CST 2017
#Mon Oct 09 11:06:41 CDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
10 changes: 3 additions & 7 deletions lifeline/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion 26

defaultConfig {
minSdkVersion 14
targetSdkVersion 25
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
Expand All @@ -22,10 +21,7 @@ android {
}

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

testCompile 'junit:junit:4.12'
compile 'com.android.support:support-annotations:26.1.0'
}

apply from: 'https://raw.githubusercontent.com/Commit451/gradle-android-javadocs/1.0.0/gradle-android-javadocs.gradle'

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.commit451.lifeline;

interface InternalBackgroundListener {

void onBackgrounded();
}
46 changes: 46 additions & 0 deletions lifeline/src/main/java/com/commit451/lifeline/Lifeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

import android.app.Activity;
import android.app.Application;
import android.content.ComponentCallbacks2;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
* Keeps a pulse on your application
*/
public class Lifeline {

private static TrackedLifecycleCallbacks lifecycleHandler;
private static List<OnBackgroundedListener> onBackgroundedListeners = new ArrayList<>();

/**
* Hooks your Application up to this Lifeline
Expand All @@ -21,6 +26,14 @@ public class Lifeline {
*/
public static void init(Application application) {
lifecycleHandler = new TrackedLifecycleCallbacks();
application.registerComponentCallbacks(new BackgroundComponentCallbacks2(new InternalBackgroundListener() {
@Override
public void onBackgrounded() {
for (OnBackgroundedListener listener : onBackgroundedListeners) {
listener.onBackgrounded();
}
}
}));
application.registerActivityLifecycleCallbacks(lifecycleHandler);
}

Expand Down Expand Up @@ -96,12 +109,45 @@ public static Activity getCurrentStartedActivity() {
return null;
}

public static void register(OnBackgroundedListener listener) {
onBackgroundedListeners.add(listener);
}

public static void unregister(OnBackgroundedListener listener) {
onBackgroundedListeners.remove(listener);
}

private static void checkInit() {
if (lifecycleHandler == null) {
throw new IllegalStateException("You need to first call `init()` on this class before using it");
}
}

private static class BackgroundComponentCallbacks2 implements ComponentCallbacks2 {

private InternalBackgroundListener listener;

public BackgroundComponentCallbacks2(InternalBackgroundListener listener) {
this.listener = listener;
}

@Override
public void onTrimMemory(int level) {
if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
// We're in the Background
listener.onBackgrounded();
}
}

@Override
public void onLowMemory() {
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
}
}

/**
* Inspired by
* http://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background/13809991#13809991
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.commit451.lifeline;

/**
* Listener for when the app is backgrounded
*/
public interface OnBackgroundedListener {

/**
* The app has been backgrounded
*/
void onBackgrounded();
}
15 changes: 0 additions & 15 deletions lifeline/src/test/java/com/commit451/lifeline/ExampleUnitTest.java

This file was deleted.

0 comments on commit 98b0c1a

Please sign in to comment.