From 1e92f7c43bf2373342309645c268318bfa18554d Mon Sep 17 00:00:00 2001 From: loadedion Date: Tue, 28 Feb 2017 15:50:45 -0800 Subject: [PATCH 1/2] Increment gradle plugin to 2.2.3 * To support instant run --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 16f4c84..69dcb7c 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.2' + classpath 'com.android.tools.build:gradle:2.2.3' // Used for annotation processing needed by the AndroidAnnotations library classpath 'com.neenbedankt.gradle.plugins:android-apt:1.6' } From 9c0b431b0b2d2c0db3afb46cd047840a8efcc0b9 Mon Sep 17 00:00:00 2001 From: loadedion Date: Tue, 28 Feb 2017 15:55:40 -0800 Subject: [PATCH 2/2] Return to route list after app has been backgrounded for 3+ hours --- .../dougkeen/bart/BartRunnerApplication.java | 55 ++++++++++++++++++- .../bart/activities/AbstractViewActivity.java | 24 ++++++++ .../activities/ViewDeparturesActivity.java | 2 +- .../bart/activities/ViewMapActivity.java | 2 +- 4 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/dougkeen/bart/activities/AbstractViewActivity.java diff --git a/app/src/main/java/com/dougkeen/bart/BartRunnerApplication.java b/app/src/main/java/com/dougkeen/bart/BartRunnerApplication.java index c4b55a0..bacdb0f 100644 --- a/app/src/main/java/com/dougkeen/bart/BartRunnerApplication.java +++ b/app/src/main/java/com/dougkeen/bart/BartRunnerApplication.java @@ -9,10 +9,13 @@ import java.util.List; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.ObjectUtils; +import android.app.Activity; import android.app.AlarmManager; import android.app.Application; import android.content.Context; +import android.content.SharedPreferences; import android.media.MediaPlayer; +import android.os.Bundle; import android.os.Parcel; import android.util.Log; @@ -26,10 +29,14 @@ import com.googlecode.androidannotations.annotations.Bean; import com.googlecode.androidannotations.annotations.EApplication; @EApplication -public class BartRunnerApplication extends Application { +public class BartRunnerApplication extends Application implements + Application.ActivityLifecycleCallbacks { + private static final int FIVE_MINUTES = 5 * 60 * 1000; private static final String CACHE_FILE_NAME = "lastBoardedDeparture"; + private static final String PREFS_NAME = "prefs_bart_runner"; + private static final String PREFS_ACTIVITY_TIMESTAMP = "prefs_activity_timestamp"; private Departure mBoardedDeparture; @@ -39,6 +46,8 @@ public class BartRunnerApplication extends Application { private MediaPlayer mAlarmMediaPlayer; + private SharedPreferences mApplicationPreferences; + private static Context context; @Bean @@ -92,6 +101,8 @@ public class BartRunnerApplication extends Application { public void onCreate() { super.onCreate(); context = getApplicationContext(); + mApplicationPreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); + registerActivityLifecycleCallbacks(this); } public static Context getAppContext() { @@ -222,4 +233,46 @@ public class BartRunnerApplication extends Application { public void setAlarmMediaPlayer(MediaPlayer alarmMediaPlayer) { this.mAlarmMediaPlayer = alarmMediaPlayer; } + + public void setActivityTimestamp(long timestamp) { + mApplicationPreferences.edit().putLong(PREFS_ACTIVITY_TIMESTAMP, timestamp).apply(); + } + + public long getActivityTimestamp() { + return mApplicationPreferences.getLong(PREFS_ACTIVITY_TIMESTAMP, 0L); + } + + + @Override + public void onActivityCreated(Activity activity, Bundle savedInstanceState) { + } + + @Override + public void onActivityStarted(Activity activity) { + + } + + @Override + public void onActivityResumed(Activity activity) { + setActivityTimestamp(System.currentTimeMillis()); + } + + @Override + public void onActivityPaused(Activity activity) { + + } + + @Override + public void onActivityStopped(Activity activity) { + } + + @Override + public void onActivitySaveInstanceState(Activity activity, Bundle outState) { + + } + + @Override + public void onActivityDestroyed(Activity activity) { + + } } diff --git a/app/src/main/java/com/dougkeen/bart/activities/AbstractViewActivity.java b/app/src/main/java/com/dougkeen/bart/activities/AbstractViewActivity.java new file mode 100644 index 0000000..d8fe95b --- /dev/null +++ b/app/src/main/java/com/dougkeen/bart/activities/AbstractViewActivity.java @@ -0,0 +1,24 @@ +package com.dougkeen.bart.activities; + +import android.support.v7.app.AppCompatActivity; + +import com.dougkeen.bart.BartRunnerApplication; + +import java.util.concurrent.TimeUnit; + +public abstract class AbstractViewActivity extends AppCompatActivity { + + private static final int MAXIMUM_IDLE_HOURS = 3; + + @Override + protected void onStart() { + super.onStart(); + BartRunnerApplication application = (BartRunnerApplication) getApplication(); + long lastActivity = application.getActivityTimestamp(); + long currentTime = System.currentTimeMillis(); + long timeDifference = currentTime - lastActivity; + if (TimeUnit.MILLISECONDS.toHours(timeDifference) >= MAXIMUM_IDLE_HOURS) { + finish(); + } + } +} diff --git a/app/src/main/java/com/dougkeen/bart/activities/ViewDeparturesActivity.java b/app/src/main/java/com/dougkeen/bart/activities/ViewDeparturesActivity.java index ae845ae..9827b15 100644 --- a/app/src/main/java/com/dougkeen/bart/activities/ViewDeparturesActivity.java +++ b/app/src/main/java/com/dougkeen/bart/activities/ViewDeparturesActivity.java @@ -53,7 +53,7 @@ import com.dougkeen.util.Assert; import com.dougkeen.util.Observer; import com.dougkeen.util.WakeLocker; -public class ViewDeparturesActivity extends AppCompatActivity implements +public class ViewDeparturesActivity extends AbstractViewActivity implements EtdServiceListener { private StationPair mStationPair; diff --git a/app/src/main/java/com/dougkeen/bart/activities/ViewMapActivity.java b/app/src/main/java/com/dougkeen/bart/activities/ViewMapActivity.java index f53aefa..b0d6616 100644 --- a/app/src/main/java/com/dougkeen/bart/activities/ViewMapActivity.java +++ b/app/src/main/java/com/dougkeen/bart/activities/ViewMapActivity.java @@ -13,7 +13,7 @@ import com.dougkeen.bart.R; import com.dougkeen.util.Assert; -public class ViewMapActivity extends AppCompatActivity { +public class ViewMapActivity extends AbstractViewActivity { @Override protected void onCreate(Bundle savedInstanceState) {