Skip to content

Commit

Permalink
[Widget] Fixed issue where WidgetListenerService would never stop, co…
Browse files Browse the repository at this point in the history
…ntinuing to run clearStopForecast() in a thread.
  • Loading branch information
thecosmicfrog committed May 8, 2019
1 parent 4f8e469 commit 3376fb1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
Expand All @@ -55,8 +56,6 @@
import java.io.ObjectInputStream;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import retrofit.Callback;
import retrofit.RestAdapter;
Expand Down Expand Up @@ -97,8 +96,6 @@ public class WidgetListenerService extends Service {
TEXTVIEW_OUTBOUND_STOP2_TIME
};

private static TimerTask timerTaskStopForecastTimeout;

private EnglishGaeilgeMap mapEnglishGaeilge;
private List<CharSequence> listSelectedStops;
private String localeDefault;
Expand All @@ -109,15 +106,13 @@ public WidgetListenerService() {
@Override
public void onCreate() {
super.onCreate();

startForeground(1, buildForegroundNotification().build());
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(1, buildForegroundNotification().build());

final int STOP_FORECAST_TIMEOUT_MILLIS = 20000;
final int STOP_FORECAST_TIMEOUT_MILLIS = 15000;

if (isNetworkAvailable(getApplicationContext())) {
Log.i(LOG_TAG, "Network available. Starting WidgetListenerService.");
Expand All @@ -138,16 +133,6 @@ public int onStartCommand(Intent intent, int flags, int startId) {
R.layout.stop_forecast_widget
);

/*
* Reset the stop forecast timeout.
*/
stopForecastTimeout(
appWidgetManager,
widgetId,
views,
STOP_FORECAST_TIMEOUT_MILLIS
);

if (loadListSelectedStops(getApplicationContext()) != null) {
String selectedStopName;
listSelectedStops = loadListSelectedStops(getApplicationContext());
Expand All @@ -170,12 +155,22 @@ public int onStartCommand(Intent intent, int flags, int startId) {
);

appWidgetManager.partiallyUpdateAppWidget(widgetId, views);

/*
* Reset the stop forecast timeout.
*/
stopForecastTimeout(
appWidgetManager,
widgetId,
views,
STOP_FORECAST_TIMEOUT_MILLIS
);
}
} else {
Log.e(LOG_TAG, "No widget IDs received.");
}
}

stopSelf();

/* Necessary? Trivial? Further research required. */
return START_NOT_STICKY;
}
Expand Down Expand Up @@ -431,12 +426,10 @@ private void stopForecastTimeout(
final int widgetId,
final RemoteViews views,
int timeoutTimeMillis) {
final int DELAY_TIME_MILLIS = 0;

if (timerTaskStopForecastTimeout != null)
timerTaskStopForecastTimeout.cancel();

timerTaskStopForecastTimeout = new TimerTask() {
/*
* Create a Runnable to execute the clearStopForecast() method after a set delay.
*/
Runnable runnableClearStopForecastAfterTimeout = new Runnable() {
@Override
public void run() {
clearStopForecast(views);
Expand All @@ -447,10 +440,17 @@ public void run() {
);

appWidgetManager.partiallyUpdateAppWidget(widgetId, views);

stopSelf();
stopForeground(true);
}
};

new Timer().schedule(timerTaskStopForecastTimeout, DELAY_TIME_MILLIS, timeoutTimeMillis);
/* Wait for a set delay, then trigger the clearing of the stop forecast via the Runnable. */
new Handler().postDelayed(
runnableClearStopForecastAfterTimeout,
timeoutTimeMillis
);
}

/**
Expand All @@ -473,6 +473,11 @@ private List<CharSequence> loadListSelectedStops(Context context) {
//noinspection unchecked
listSelectedStops = (List<CharSequence>) objectInput.readObject();

/* Close files and streams. */
objectInput.close();
buffer.close();
fileInput.close();

return listSelectedStops;
} catch (ClassNotFoundException | FileNotFoundException e) {
/*
Expand Down Expand Up @@ -693,6 +698,7 @@ private NotificationCompat.Builder buildForegroundNotification() {

/* Configure notification channel. */
notificationChannel.setDescription("Widget get stop forecast");
notificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);

notificationManager.createNotificationChannel(notificationChannel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ private static List<CharSequence> loadListSelectedStops(Context context) {
@SuppressWarnings("unchecked")
List<CharSequence> listSelectedStops = (List<CharSequence>) objectInput.readObject();

/* Close files and streams. */
objectInput.close();
buffer.close();
fileInput.close();

return listSelectedStops;
} catch (ClassNotFoundException | FileNotFoundException e) {
/*
Expand All @@ -273,7 +278,6 @@ static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
/* Construct the RemoteViews object. */
RemoteViews views =
new RemoteViews(context.getPackageName(), R.layout.stop_forecast_widget);

/*
* Set up Intents to register taps on the widget.
*/
Expand Down

0 comments on commit 3376fb1

Please sign in to comment.