Return to route list after app has been backgrounded for 3+ hours

This commit is contained in:
loadedion 2017-02-28 15:55:40 -08:00
parent 1e92f7c43b
commit 9c0b431b0b
4 changed files with 80 additions and 3 deletions

View File

@ -9,10 +9,13 @@ import java.util.List;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import android.app.Activity;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.Application; import android.app.Application;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Parcel; import android.os.Parcel;
import android.util.Log; import android.util.Log;
@ -26,10 +29,14 @@ import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.EApplication; import com.googlecode.androidannotations.annotations.EApplication;
@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 int FIVE_MINUTES = 5 * 60 * 1000;
private static final String CACHE_FILE_NAME = "lastBoardedDeparture"; 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; private Departure mBoardedDeparture;
@ -39,6 +46,8 @@ public class BartRunnerApplication extends Application {
private MediaPlayer mAlarmMediaPlayer; private MediaPlayer mAlarmMediaPlayer;
private SharedPreferences mApplicationPreferences;
private static Context context; private static Context context;
@Bean @Bean
@ -92,6 +101,8 @@ public class BartRunnerApplication extends Application {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
context = getApplicationContext(); context = getApplicationContext();
mApplicationPreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
registerActivityLifecycleCallbacks(this);
} }
public static Context getAppContext() { public static Context getAppContext() {
@ -222,4 +233,46 @@ public class BartRunnerApplication extends Application {
public void setAlarmMediaPlayer(MediaPlayer alarmMediaPlayer) { public void setAlarmMediaPlayer(MediaPlayer alarmMediaPlayer) {
this.mAlarmMediaPlayer = 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) {
}
} }

View File

@ -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();
}
}
}

View File

@ -53,7 +53,7 @@ import com.dougkeen.util.Assert;
import com.dougkeen.util.Observer; import com.dougkeen.util.Observer;
import com.dougkeen.util.WakeLocker; import com.dougkeen.util.WakeLocker;
public class ViewDeparturesActivity extends AppCompatActivity implements public class ViewDeparturesActivity extends AbstractViewActivity implements
EtdServiceListener { EtdServiceListener {
private StationPair mStationPair; private StationPair mStationPair;

View File

@ -13,7 +13,7 @@ import com.dougkeen.bart.R;
import com.dougkeen.util.Assert; import com.dougkeen.util.Assert;
public class ViewMapActivity extends AppCompatActivity { public class ViewMapActivity extends AbstractViewActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {