Skip to content
Permalink
Browse files

[Widget] Fixed issue where WidgetListenerService would never stop, co…

…ntinuing to run clearStopForecast() in a thread.
  • Loading branch information...
thecosmicfrog committed May 8, 2019
1 parent 4f8e469 commit 3376fb1f26d95b3e978f4ca6f1f920b66002656d
@@ -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;
@@ -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;
@@ -97,8 +96,6 @@
TEXTVIEW_OUTBOUND_STOP2_TIME
};

private static TimerTask timerTaskStopForecastTimeout;

private EnglishGaeilgeMap mapEnglishGaeilge;
private List<CharSequence> listSelectedStops;
private String localeDefault;
@@ -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.");
@@ -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());
@@ -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;
}
@@ -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);
@@ -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
);
}

/**
@@ -473,6 +473,11 @@ public void run() {
//noinspection unchecked
listSelectedStops = (List<CharSequence>) objectInput.readObject();

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

return listSelectedStops;
} catch (ClassNotFoundException | FileNotFoundException e) {
/*
@@ -693,6 +698,7 @@ private void updateStopForecast(Context context, RemoteViews views, StopForecast

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

notificationManager.createNotificationChannel(notificationChannel);
}
@@ -248,6 +248,11 @@ private static void startWidgetListenerService(@NonNull Context context, int[] a
@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) {
/*
@@ -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.
*/

0 comments on commit 3376fb1

Please sign in to comment.
You can’t perform that action at this time.